Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stagas/tokenizer-next

iterator based tokenizer for writing parsers
https://github.com/stagas/tokenizer-next

iterable iterator lexer parser parsing regexp regexp-match tokenizer

Last synced: about 1 month ago
JSON representation

iterator based tokenizer for writing parsers

Awesome Lists containing this project

README

        


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


    To capture, RegExps must use a [named group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges#using_named_groups).

    ```ts
    const tokenize = createTokenizer(
    /(?[a-z]+)/, // named groups determine token `group`
    /(?[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.


      ```ts
      // 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


    To capture, RegExps must use a [named group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges#using_named_groups).

    ```ts
    const tokenize = createTokenizer(
    /(?[a-z]+)/, // named groups determine token `group`
    /(?[0-9]+)/
    )
    ```


    # regexps – RegExps to match.

      RegExp []

    createTokenizer(regexps)  =>


## Credits

- [match-to-token](https://npmjs.org/package/match-to-token) by [stagas](https://github.com/stagas) – transform a RegExp named group match to a more useful object

## Contributing

[Fork](https://github.com/stagas/tokenizer-next/fork) or [edit](https://github.dev/stagas/tokenizer-next) and submit a PR.

All contributions are welcome!

## License

MIT © 2022 [stagas](https://github.com/stagas)