pub fn parse_parentheses(input: &str) -> IResult<&str, Expression>Expand description
Parse a parenthesized expression.
This function parses expressions enclosed in parentheses, which are used to override operator precedence or group subexpressions. Parentheses allow explicit control over the evaluation order of complex expressions.
§Arguments
input- A string slice containing the parenthesized expression to parse
§Returns
Returns an [IResult<&str, Expression>] containing:
- The remaining unparsed input
- A parsed
Expressionrepresenting the parenthesized content
§Grammar
The parenthesized expression grammar follows:
parentheses = '(' S? expression S? ')'Where expression represents any valid AIMX expression. Empty parentheses
() are parsed as Expression::Empty.
§Examples
use aimx::{Primary};
use aimx::expressions::{parse_parentheses};
use aimx::{Expression, Literal};
// Parse empty parentheses
let (remaining, expr) = parse_parentheses("()").unwrap();
assert_eq!(remaining, "");
assert!(matches!(expr, Expression::Empty));
// Parse a simple parenthesized literal
let (remaining, expr) = parse_parentheses("(42)").unwrap();
assert_eq!(remaining, "");
// The result will depend on how the inner expression "42" is parsed
// It could be Expression::Primary(_) for simple literals
// Parse a complex parenthesized expression
let (remaining, expr) = parse_parentheses("(1 + 2 * 3)").unwrap();
assert_eq!(remaining, "");
// The result depends on the specific expression parsed§Implementation Details
This function handles:
- Empty parentheses
()by returningExpression::Empty - Whitespace around the parentheses
- Recursive parsing of the inner expression
- Proper error handling for mismatched parentheses
§See Also
crate::expression::parse_expression- Function for parsing the inner expressionExpression::Empty- The variant returned for empty parenthesesPrimary::Parentheses- The variant used when parenthesized expressions are parsed as primary expressions