Skip to content

Parser

The parser is recursive descent. One function reads one grammar rule and calls another function for the next tighter rule. That keeps the grammar in the Reference visible in the code: if you can read the EBNF, you can find the function that implements it.

The parser is separate from the lexer so the grammar is checked once and turned into a tree. Execution then walks that tree. It does not scan the source a second time.

lupe ast file.lupe prints the tree. lupe check builds it and then stops.

Precedence

Each precedence level is its own function. The call chain from loosest to tightest is or , and , not , comparison, addition, multiplication, unary plus and minus, and exponentiation. Exponentiation calls itself on the right, so 2 ** 3 ** 2 is 2 ** (3 ** 2) . The other binary operators consume their left side and then fold. Comparisons do not chain, so 1 < 2 < 3 is a syntax error rather than a quiet boolean.

Parentheses are not stored. They only force the parser into the expression rule. The tree shows the resulting nesting.

Where a Construct May Appear

Some rules are only legal in a particular place, and the parser rejects them before any code runs.

  • function and import are only legal at the top level of a file.
  • return is only legal inside a function.
  • break and continue are only legal inside a loop.
  • const must have = .
  • import must be given a string literal.

If the tokens run out while a statement is still open, the error is marked incomplete. The REPL uses that flag to ask for another line. Any other mismatch is a syntax error.