Function parse_postfix

Source
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 Postfix enum representing the parsed abstract syntax tree

§Complex Parsing Logic

The actual parsing logic is more complex than a simple grammar rule:

  1. Try parsing as a function call (including special _ case)
  2. If that fails, parse as a primary expression
  3. Check if the next token is ( for potential inference calls
  4. 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