Might consider removing ast_expr

This commit is contained in:
2026-05-15 02:02:43 +02:00
parent 6fbc2a086e
commit 933f102680
2 changed files with 34 additions and 18 deletions
+19 -3
View File
@@ -29,12 +29,16 @@ pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<To
#[macro_export] #[macro_export]
macro_rules! ast_expr { macro_rules! ast_expr {
($inner:tt@$item:path { $($name:ident $t:tt $val:tt $($val2:tt)?),*}) => {{ ($inner:tt @ $($item:ident)::+ ( $($t:tt $val:tt $val2:tt),* )) => {{
$($item)::+ ( $( $crate::_ast_t!($t $crate::_ast_ti!($inner, $val $val2)) )* )
}};
($inner:tt @ $($item:ident)::+ { $($name:ident $t:tt $val:tt $val2:tt),*}) => {{
$( $(
let $name = $crate::_ast_t!($t $crate::_ast_ti!($inner, $val $($val2)?)); let $name = $crate::_ast_t!($t $crate::_ast_ti!($inner, $val $val2));
)* )*
$item { $($name,)* } $($item)::+ { $($name,)* }
}}; }};
} }
@@ -58,6 +62,18 @@ macro_rules! _ast_t {
#[macro_export] #[macro_export]
macro_rules! _ast_ti { macro_rules! _ast_ti {
($inner:ident, !*) => {
$crate::error::collect_recovered($inner)
};
($inner:ident, @*) => {
$crate::error::collect_recovered($inner.next.unwrap().into_inner())
};
($inner:ident, @ @) => {
$inner.next().unwrap().try_into()
};
($inner:ident, ! $val:ident) => { ($inner:ident, ! $val:ident) => {
$val::try_from(&mut $inner) $val::try_from(&mut $inner)
}; };
+15 -15
View File
@@ -1,7 +1,8 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, AstResult, GetParseError}, ast_expr,
error::{AstError, AstResult, GetParseError, collect_recovered},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
@@ -32,15 +33,16 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
prefixes, prefixes,
postfixes: inner postfixes: inner
.map(|p| Postfix::try_from(p)) .map(|p| Postfix::try_from(p))
.collect::<AstResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()
.get()?,
}) })
} else { } else {
Ok(exp) Ok(exp)
} }
} }
Rule::primary => Expression::try_from(inner.next().unwrap()), Rule::primary => Expression::try_from(inner.next().unwrap()),
Rule::static_path => Ok(Expression::Path(Path::try_from(pair)?)), Rule::static_path => Ok(Expression::Path(Path::try_from(pair).get()?)),
Rule::literal => Ok(Expression::Literal(Literal::try_from(pair)?)), Rule::literal => Ok(Expression::Literal(Literal::try_from(pair).get()?)),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
@@ -72,27 +74,24 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Ok(match rule { Ok(match rule {
Rule::postfix => Postfix::try_from(inner.next().unwrap())?, Rule::postfix => Postfix::try_from(inner.next().unwrap())?,
Rule::field_px => Postfix::FieldAccess(Identifier::try_from(inner.next().unwrap()).get()?), Rule::field_px => ast_expr!(inner@Postfix::FieldAccess(:@@)),
Rule::call_px => Postfix::Call(inner.map(Expression::try_from).collect::<AstResult< Rule::call_px => ast_expr!(inner@Postfix::Call(:!*)),
'a,
Vec<_>,
_,
>>()?),
Rule::struct_px => Postfix::StructCall( Rule::struct_px => Postfix::StructCall(
inner inner
.map(|p| { .map(|p| {
let mut pi = p.into_inner(); let mut pi = p.into_inner();
Ok(( Ok((
Identifier::try_from(pi.next().unwrap())?, Identifier::try_from(pi.next().unwrap()).get()?,
Expression::try_from(pi.next().unwrap())?, Expression::try_from(pi.next().unwrap()).get()?,
)) ))
}) })
.collect::<AstResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()
.get()?,
), ),
Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap())?), Rule::index_px => ast_expr!(inner@Postfix::Index(:@@)),
Rule::binary_px => { Rule::binary_px => {
let op_pair = inner.next().unwrap(); let op_pair = inner.next().unwrap();
@@ -115,7 +114,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
unimplemented!("Binary operator not implemented yet: {}", op_pair.as_str()) unimplemented!("Binary operator not implemented yet: {}", op_pair.as_str())
} }
}; };
Postfix::Binary(op, Expression::try_from(inner.next().unwrap())?)
ast_expr!(inner@Postfix::Binary(:&op, :@@))
} }
Rule::macro_call_px => Postfix::MacroCall(inner.as_str().to_string()), Rule::macro_call_px => Postfix::MacroCall(inner.as_str().to_string()),