Skip to main content

Module expr

Module expr 

Source
Expand description

The restricted expression grammar and parser.

The parser produces an AST. Function resolution and evaluation live in the engine crate and use exactly the same registry and validation as the typed calculate path. Nothing here evaluates Rust, JavaScript, shell commands, imports, or remote code.

Grammar (whitespace-insensitive; // line comments and /* */ block comments are supported):

expr        := or_expr
or_expr     := and_expr (('or' | '||') and_expr)*
and_expr    := not_expr (('and' | '&&') not_expr)*
not_expr    := ('not' | '!') not_expr | comparison
comparison  := additive (comp_op additive)+          // chained, e.g. 0 < x < 1
additive    := multiplicative (('+' | '-') multiplicative)*
multiplicative := unary (('*' | '/' | '%') unary)*
unary       := ('-' | '+') unary | power
power       := primary ('^' unary)?                  // right associative
primary     := NUMBER | STRING | 'true' | 'false' | IDENT
             | IDENT ('.' IDENT)+ '(' arguments? ')'
             | IDENT '(' arguments? ')'             // unqualified calls rejected
             | '(' expr ')' | '[' array ']' | '{' record '}'
arguments   := argument (',' argument)*
argument    := (IDENT '=')? expr
array       := expr (',' expr)* ','?
record      := (IDENT | STRING) ':' expr (',' ...)* ','?

Precedence (loosest to tightest): or, and, not, comparison, + -, * / %, unary sign, ^. Exponentiation binds tighter than unary minus, so -2^2 == -4, and is right associative, so 2^3^2 == 2^9.

Structs§

CallArg

Enums§

BinaryOp
CompareOp
Expr
UnaryOp

Functions§

parse_expression
Parse a restricted expression. This performs no function resolution.