Function parse_expression

Source
pub fn parse_expression(input: &str) -> IResult<&str, Expression>
Expand description

Parse a complete AIMX expression string into an abstract syntax tree.

This is the main parsing function that handles the top-level grammar rule: expression = S? array S?

The function orchestrates the parsing of complex expressions by delegating to specialized sub-parsers for different operator precedence levels. It handles the complete AIMX grammar including arrays, conditionals, and all operator types.

§Arguments

  • input - The input string containing the AIMX expression to parse

§Returns

Returns an [IResult] containing:

  • The remaining unparsed input (should be empty for successful parsing)
  • An Expression representing the parsed abstract syntax tree

§Grammar

The expression grammar follows this hierarchy:

expression     = S? array S?
array          = conditional (S? ',' S? conditional)*
conditional    = closure ('?' expression ':' expression)?
closure        = logical_or ('=>' procedure)?
logical_or     = logical_and (('|' | '||') logical_and)*
logical_and    = equality (('&' | '&&') equality)*
equality       = relational (('=' | '!=') relational)*
relational     = additive (('<' | '<=' | '>' | '>=') additive)*
additive       = multiplicative (('+' | '-') multiplicative)*
multiplicative = unary (('*' | '/' | '%') unary)*
unary          = ('!' | '+' | '-' | '(' type ')')? postfix
postfix        = primary (('.' method_call) | function_call | indexing)*
primary        = literal | reference | '(' expression ')'

§Examples

Parsing expressions directly with this function:

use aimx::expression::{parse_expression, Expression};

// Parse arithmetic expressions
let result = parse_expression("2 + 3 * 4");
assert!(result.is_ok());

// Parse array expressions
let result = parse_expression("(1, 2, 3)");
assert!(result.is_ok());

// Parse conditional expressions
let result = parse_expression("5 > 3 ? 'yes' : 'no'");
assert!(result.is_ok());

// Parse flattened primary expressions
let result = parse_expression("42");
assert!(result.is_ok());

§Error Handling

This function returns [IResult] which uses Rust’s Result type for error handling. Parsing errors are captured as [nom] error variants. For a more user-friendly parsing interface, use aimx_parse which wraps this function and provides better error messages.

§See Also