pub fn parse_postfix(input: &str) -> IResult<&str, Postfix>Expand description
Parse a postfix expression from a string.
This is the main entry point for parsing postfix expressions, which include
identifiers, function calls, array indexing, method calls, and inference calls.
The parser handles left-to-right associativity and chaining of postfix operations,
allowing for complex expressions like data.filter().map().reduce().
The parser works by first attempting to parse a function call (which includes
the special empty placeholder _). If that fails, it parses a primary expression
and then checks for special cases like inference calls (references followed by
parentheses). Finally, it processes any chained postfix operations like indexing
or method calls.
§Arguments
input- A string slice containing the postfix expression to parse
§Returns
Returns an [IResult] containing:
- The remaining unparsed input (should be empty for successful parsing)
- A
Postfixenum representing the parsed abstract syntax tree
§Complex Parsing Logic
The actual parsing logic is more complex than a simple grammar rule:
- Try parsing as a function call (including special
_case) - If that fails, parse as a primary expression
- Check if the next token is
(for potential inference calls - Parse chained postfix operations (indexing
[]or method calls.)
§Examples
use aimx::expressions::postfix::{parse_postfix, Postfix};
// Parse empty placeholder
let (remaining, postfix) = parse_postfix("_").unwrap();
assert_eq!(remaining, "");
assert!(matches!(postfix, Postfix::Empty));
// Parse function call
let (remaining, postfix) = parse_postfix("sum(1, 2, 3)").unwrap();
assert_eq!(remaining, "");
assert!(matches!(postfix, Postfix::Function(_, _)));
// Parse array indexing
let (remaining, postfix) = parse_postfix("array[0]").unwrap();
assert_eq!(remaining, "");
assert!(matches!(postfix, Postfix::Index(_, _)));
// Parse method call
let (remaining, postfix) = parse_postfix("data.filter()").unwrap();
assert_eq!(remaining, "");
assert!(matches!(postfix, Postfix::Method(_, _, _)));
// Parse chained operations
let (remaining, postfix) = parse_postfix("matrix[0][1]").unwrap();
assert_eq!(remaining, "");
assert!(matches!(postfix, Postfix::Index(_, _)));
// Parse complex chaining
let (remaining, postfix) = parse_postfix("data.filter().map().reduce()").unwrap();
assert_eq!(remaining, "");
assert!(matches!(postfix, Postfix::Method(_, _, _)));§Error Handling
This function returns [IResult] which uses Rust’s Result type for error handling.
Parsing errors are captured as [nom] error variants. The parser handles
whitespace gracefully and provides detailed error information when parsing fails.
§See Also
Postfix- The abstract syntax tree returned by this functionparse_accessor- Function for parsing dot notation accessorsExpressionLike- Trait for evaluating parsed expressionscrate::aimx_parse- Public API function for parsing complete expressions