Lua.Parser.Pratt (Lua v1.0.0-rc.2)
View SourcePratt parser for Lua expressions.
Implements operator precedence parsing using binding powers. Handles all Lua 5.3 precedence levels including bitwise operators.
Precedence (lowest to highest):
- or
- and
- < > <= >= ~= ==
- |
- ~
- &
- << >>
- ..
- -
- / // %
- unary (not # - ~)
- ^
Summary
Functions
Returns the binding power (precedence) for binary operators.
Checks if a token is a binary operator.
Checks if a token is a prefix unary operator.
Returns the binding power for unary prefix operators.
Maps token operators to AST binary operators.
Maps token operators to AST unary operators.
Functions
@spec binding_power(atom()) :: {non_neg_integer(), non_neg_integer()} | nil
Returns the binding power (precedence) for binary operators.
Returns {left_bp, right_bp} where:
- left_bp: minimum precedence of left operand
- right_bp: minimum precedence of right operand
Right associative operators have left_bp < right_bp. Left associative operators have left_bp >= right_bp.
Checks if a token is a binary operator.
Checks if a token is a prefix unary operator.
@spec prefix_binding_power(atom()) :: non_neg_integer() | nil
Returns the binding power for unary prefix operators.
This is the minimum precedence required for the operand.
Note: In Lua, unary minus has an unusual precedence - it's lower than power (^). So -2^3 = -(2^3), not (-2)^3. To achieve this: unary minus binding power (13) < power left_bp (16), allowing power to bind within the unary's operand. But 13 > multiplication left_bp (11), so -ab = (-a)b.
@spec token_to_binop(atom()) :: Lua.AST.Expr.BinOp.op() | nil
Maps token operators to AST binary operators.
@spec token_to_unop(atom()) :: Lua.AST.Expr.UnOp.op() | nil
Maps token operators to AST unary operators.