Install nimly via nimble
masterTo use nimly in your Nim project, install it using the Nim package manager:
nimble install nimlyThen, import it in your source code using import nimly.
repository·master·Indexed 41 days ago
https://github.com/loloicci/nimlyA macro-based library for Nim that generates lexers and LALR(1) parsers at compile-time using syntax similar to lex/yacc. It provides the `niml` macro for lexer generation and the `nimy` macro for parser generation, eliminating the need for an external build step.
To use nimly in your Nim project, install it using the Nim package manager:
nimble install nimlyThen, import it in your source code using import nimly.
To inspect debug information during the compilation of lexers and parsers, you can use the conditional symbols nimldebug and nimydebug.
Compile your code with the following flags:
nim c -d:nimldebug -d:nimydebug -r <your_file>.nimnim c -d:nimldebug -d:nimydebug -r tests/test_readme_example.nimIf you encounter the error interpretation requires too many iterations during compilation, it means the macro expansion is exceeding the compiler's default limits.
You can resolve this by increasing the limit using the Nim compiler option --maxLoopIterationsVM:N (available since Nim v1.0.6).
The niml macro generates a lexer at compile-time. You provide a type (e.g., a variant or enum) that the lexer will return when a token is matched.
niml LexDataName[TokenType]:.r"...") to define regex patterns.TokenType.token (of type LToken) is available within the pattern blocks to access the raw text of the matched token.setUp and tearDown blocks to run code during the lexer's lifecycle (open/newWithString/initWithString calls setUp; close calls tearDown)..: match any character[: start of character class|: OR operator( / ): subpattern grouping?: 0 or 1 quantifier*: 0 or more quantifier+: 1 or more quantifier{n,m}: n to m quantifierIn character classes []:
^: negate (only at the start)-: range (or use ..)Supported character sets:
\d: [0..9]\D: [^0..9]\s: [ \t\n\r\f\v]\S: [^ \t\n\r\f\v]\w: [a..zA..Z0..9_]\W: [^a..zA..Z0..9_]niml myLexer[Token]:
r"if":
return TokenIf()
r"else":
return TokenElse()
r"[a..zA..Z][a-zA-Z0-9]*":
return TokenIdentifier(token)
setUp:
doSomething()
tearDown:
doSomething()The nimy macro generates a LALR(1) parser at compile-time. The parser consumes tokens produced by a lexer.
nimy ParserName[TokenType]:.top level of the BNF. It must specify a return type: top[ReturnType]:.nonTerminalName[ReturnType]:.$n to refer to the $n$-th position in the pattern (e.g., $1 is the first element).XXX[]: Option (0 or 1 occurrence of XXX). The resulting type is seq[xxx].XXX{}: Repeat (0 or more occurrences of XXX). The resulting type is seq[xxx].nimy myParser[Token]:
top[Expr]:
expr:
return $1
expr[Expr]:
LPAR expr RPAR:
return $2
expr PLUS expr:
return $2