No description
Find a file
George Lemon 5100e0442e add handleUnknown
Signed-off-by: George Lemon <georgelemon@protonmail.com>
2024-04-18 21:31:08 +03:00
.github Update docs.yml 2023-01-25 22:45:17 +02:00
examples update 2022-12-14 23:32:02 +02:00
src add handleUnknown 2024-04-18 21:31:08 +03:00
tests fix test 2023-08-10 16:31:08 +03:00
.gitignore update 2022-05-14 00:36:28 +03:00
LICENSE Create LICENSE 2022-02-19 17:41:20 +02:00
README.md Update README.md 2023-10-10 14:14:38 +03:00
toktok.nimble bump 2024-02-24 00:09:29 +02:00


Generic tokenizer written in Nim language, powered by Nim's Macros 👑

nimble install toktok

API reference

Github Actions Github Actions

😍 Key Features

  • Powered by Nim's Macros
  • 🪄 Based on std/lexbase / Zero Regular Expression
  • Compile-time generation using macro-based TokenKind enum, lexbase
  • Runtime generation using TokenKind tables, lexbase
  • Open Source | MIT

Note

This is a generic Lexer, based on std/ streams, lexbase and macros. It is meant to be used by higher level parsers for writing any kind of tools or programs.

Note

Compile with -d:toktokdebug to inspect the generated code.

Quick Example

# Register your custom handlers
handlers:
  proc handleImport(lex: var Lexer, kind: TokenKind) =
    # tokenize `import x, y, z`
    lex.kind = kind

# Register your tokens
registerTokens defaultSettings:
  `const` = "const"
  `echo` = "hello"
  asgn = '=':   # `=` is tkAsgn
    eq = '='    # `==` is tkEQ
  excl = '!':
    ne = '='
  at = '@':
    import = tokenizer(handleImport, "import") 


# Tokenizing...
var
  tok = lexer.init(sample)
  prev: TokenTuple
  curr: TokenTuple = tok.getToken
  next: TokenTuple = tok.getToken 

proc walk(tok: var Lexer) =
  prev = curr
  curr = next
  next = tok.getToken

while likely(curr.kind != tkEOF):
  if tok.hasError: break
  echo curr # use `getToken` consumer to get token by token
  walk tok

TODO

  • Runtime Token generation using tables/critbits

❤ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright © 2023 OpenPeeps & Contributors — All rights reserved.