Function parse_reference

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

Parse a reference chain: identifier (accessor identifier)*

This function parses variable references that can consist of one to three chained identifiers separated by accessors (dots). The parser handles inference calls (prefixed with $) and respects AIMX’s syntax rules for method calls and function references.

§Syntax Rules

  • Simple references: variable, _assignment, $inference
  • Chained references: object.property, object.property.method
  • Inference calls: $tool.summarize, $workflow.process
  • Method calls: When followed by (, chained references are truncated to support method syntax like object.property.method()

§Arguments

  • input - The input string to parse

§Returns

Returns a IResult<&str, Reference> containing the remaining unparsed input and the parsed Reference.

§Examples

use aimx::expressions::reference::{parse_reference, Reference};

// Parse simple references
assert_eq!(parse_reference("variable").unwrap().1, Reference::One("variable".to_string()));
assert_eq!(parse_reference("_assignment").unwrap().1, Reference::One("_assignment".to_string()));
assert_eq!(parse_reference("$inference").unwrap().1, Reference::One("inference".to_string()));

// Parse chained references
let (_, reference) = parse_reference("object.property").unwrap();
assert_eq!(reference, Reference::Two("object".to_string(), "property".to_string()));

let (_, reference) = parse_reference("parent.child.grandchild").unwrap();
assert_eq!(reference, Reference::Three("parent".to_string(), "child".to_string(), "grandchild".to_string()));

// Parse inference calls
let (_, reference) = parse_reference("$tool.summarize").unwrap();
assert_eq!(reference, Reference::Two("tool".to_string(), "summarize".to_string()));

§Error Cases

The parser will fail for:

  • Empty identifiers (_ followed by accessor)
  • Identifiers starting with _ followed by parentheses
  • References that would exceed three parts