Lua.Parser.Pratt (Lua v1.0.0-rc.2)

View Source

Pratt 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):

  1. or
  2. and
  3. < > <= >= ~= ==
  4. |
  5. ~
  6. &
  7. << >>
  8. ..
    • -
    • / // %
  9. unary (not # - ~)
  10. ^

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

binding_power(arg1)

@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.

is_binary_op?(op)

@spec is_binary_op?(atom()) :: boolean()

Checks if a token is a binary operator.

is_prefix_op?(op)

@spec is_prefix_op?(atom()) :: boolean()

Checks if a token is a prefix unary operator.

prefix_binding_power(arg1)

@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.

token_to_binop(arg1)

@spec token_to_binop(atom()) :: Lua.AST.Expr.BinOp.op() | nil

Maps token operators to AST binary operators.

token_to_unop(arg1)

@spec token_to_unop(atom()) :: Lua.AST.Expr.UnOp.op() | nil

Maps token operators to AST unary operators.