aimx/aim/parser.rs
1//! AIMX expression parsing entry point.
2//!
3//! Provides a minimal wrapper around the internal `parse_expression` logic, returning
4//! an [`Expression`] or [`Errata`] for a single AIMX expression.
5
6use crate::{
7 expressions::{Expression, parse_expression},
8 values::Errata,
9};
10use std::sync::Arc;
11
12/// Parse a single AIMX expression into an [`Expression`] or [`Errata`].
13///
14/// On success returns a fully parsed [`Expression`].
15/// If parsing fails, returns an [`Errata`] describing the syntax error or unexpected
16/// trailing input.
17pub fn aimx_parse(input: &str) -> Expression {
18 match parse_expression(input) {
19 Ok((remaining, expression)) => {
20 if remaining.is_empty() {
21 expression
22 } else {
23 // Unexpected input remaining that wasn't parsed
24 Errata::new_expression_location(
25 Arc::from("Unexpected Input Remaining"),
26 Arc::from(input),
27 Arc::from(remaining),
28 )
29 }
30 }
31 Err(nom::Err::Incomplete(_)) => {
32 // Incomplete input: treat as a syntax error with no specific location
33 Errata::new_expression(
34 Arc::from("Syntax Error"),
35 Arc::from(input)
36 )
37 }
38 Err(nom::Err::Error(e)) | Err(nom::Err::Failure(e)) => {
39 Errata::new_expression_location(
40 Arc::from(format!("Syntax Error ({})", e)),
41 Arc::from(input.to_string()),
42 Arc::from(e.input.to_string()),
43 )
44 }
45 }
46}