Module lualite::parser::expression
source · [−]Expand description
Expression parsers
The primary parser to use in this module is expression
, which will parse any
expression. It’s used in the examples below. The other parsers in this module
will only parse a specific type of expression. For example function_call
will
only parse an expressions where a function call is the root of the expression tree.
Examples:
let source_code = "x + 1.0 * y";
let (_remaining, parsed_ast) = expression(source_code).expect("parse error");
let expected_ast = Expression::Binary {
left: Box::new(Expression::Identifier(Identifier("x".to_owned()))),
op: BinaryOperator::Add,
right: Box::new(Expression::Binary {
left: Box::new(Expression::Float(FloatLiteral(1.0_f64))),
op: BinaryOperator::Mul,
right: Box::new(Expression::Identifier(Identifier("y".to_owned()))),
})
};
assert_eq!(parsed_ast, expected_ast);
Order of operations can be changed with parentheses:
let source_code = "(x + 1.0) * y";
let (_remaining, parsed_ast) = expression(source_code).expect("parse error");
let expected_ast = Expression::Binary {
left: Box::new(Expression::Binary {
left: Box::new(Expression::Identifier(Identifier("x".to_owned()))),
op: BinaryOperator::Add,
right: Box::new(Expression::Float(FloatLiteral(1.0_f64))),
}),
op: BinaryOperator::Mul,
right: Box::new(Expression::Identifier(Identifier("y".to_owned()))),
};
assert_eq!(parsed_ast, expected_ast);
Functions
Additive binary operator expressions (+, -)
Argument list for a function call
Comparison expressions (==, !=, <, >=, etc.)
Any possible expression with arbitrary nesting
Function call expression
Indexed container as an r-value
Expressions with the highest precedence
Multiplicative binary operator expressions (*, /, %)
Parenthesized expression to modify operator precedence order
Post-fix operators (call and index)
An expression raised to the power of another expression
Unary prefix operator expressions