0.3.2 • Published 7 years ago

scanty v0.3.2

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

Scanty

TravisCI Coverage Dependencies Dev Dependencies npm version MIT License

A small library for building toy lexers.

Install

npm --save install scanty

Examples

Lex input into tokens:

import { Lexer } from 'scanty'

const rules = [
  { name: 'word',    match: /[a-zA-Z]+/ },
  { name: 'float',   match: /\d*\.\d+/ },
  { name: 'integer', match: /\d+/ },
  { name: 'space',   match: /\s+/ },
]

const common = new Lexer(rules)
common.scan('one 2 3.0')
// [
//   { type: 'word',    value: 'one', position: 0 },
//   { type: 'space',   value: ' ',   position: 3 },
//   { type: 'integer', value: '2',   position: 4 },
//   { type: 'space',   value: ' ',   position: 5 },
//   { type: 'float',   value: '3.0', position: 6 },
// ]

Order matters! Rules are matched in the order they were created with lexer.rule() or new Lexer(). If we swap the order of float and integer in our example, the float match has a value of '.0' instead of the intended '3.0'

You can add more rules with lexer.rule() and call lexer.scan() on additional input.

common.rule('bang', '!')
common.scan("I ate 10 tacos!")
// [
//   { type: 'word',    value: 'I',     position: 0   },
//   { type: 'space',   value: ' ',     position: 1   },
//   { type: 'word',    value: 'ate',   position: 2   },
//   { type: 'space',   value: ' ',     position: 5   },
//   { type: 'integer', value: '10',    position: 6   },
//   { type: 'space',   value: ' ',     position: 8   },
//   { type: 'word',    value: 'tacos', position: 9   },
//   { type: 'bang',    value: '!',     position: 14  },
// ]
0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1

10 years ago