tokenizer-next
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

tokenizer-next

iterator based tokenizer for writing parsers

npm i tokenizer-next pnpm add tokenizer-next yarn add tokenizer-next

API

# createTokenizer(regexps) – Create a {@link TokenizerFactory} for the given RegExps. src/index.ts#L19
const tokenize = createTokenizer(
  /(?<ident>[a-z]+)/, // named groups determine token `group`
  /(?<number>[0-9]+)/
)

# regexps – RegExps to match.

    RegExp []

createTokenizer(regexps)  =>

# Token – Token interface src/match-to-token/dist/types/token.d.ts#L5
# is(group, value) src/match-to-token/dist/types/token.d.ts#L24

    # group

      string

    # value

      string

    is(group, value)  =>

      boolean
# create(value, group, source) src/match-to-token/dist/types/token.d.ts#L6
# RegExpMatchArrayLike src/match-to-token/dist/types/index.d.ts#L2
# Token src/match-to-token/dist/types/index.d.ts#L6
# TokenizerCallableIterable – Can be called to return next <a href="https://github.com/stagas/match-to-token#token">Token</a> or can be used as an Iterable on for-of and spread operations. src/index.ts#L74
& Iterable<Token>
# TokenizerFactory src/index.ts#L67

    # (input) – Create a {@link TokenizerCallableIterable} for given input string.

    // using next()
    const next = tokenize('hello 123')
    console.log(next()) // => {group: 'ident', value: 'hello', index: 0}
    console.log(next()) // => {group: 'number', value: '123', index: 6}
    console.log(next()) // => undefined
    
    // using for of
    for (const token of tokenize('hello 123')) {
      console.log(token)
      // => {group: 'ident', value: 'hello', index: 0}
      // => {group: 'number', value: '123', index: 6}
    }
    
    // using spread
    const tokens = [...tokenize('hello 123')]
    console.log(tokens)
    // => [
    //   {group: 'ident', value: 'hello', index: 0},
    //   {group: 'number', value: '123', index: 6}
    // ]

    # input – The string to tokenize.

      string

    (input)  =>

# createTokenizer(regexps) – Create a {@link TokenizerFactory} for the given RegExps. src/index.ts#L19
const tokenize = createTokenizer(
  /(?<ident>[a-z]+)/, // named groups determine token `group`
  /(?<number>[0-9]+)/
)
# regexps – RegExps to match.

    RegExp []

createTokenizer(regexps)  =>

Credits

Contributing

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2022 stagas

Package Sidebar

Install

npm i tokenizer-next

Weekly Downloads

11

Version

3.0.1

License

MIT

Unpacked Size

49 kB

Total Files

18

Last publish

Collaborators

  • stagas