From 459c4b1ee01fc8338018af378f8b5c444b106aa0 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 15 May 2026 02:23:54 +0200 Subject: [PATCH] Reverted back --- parser/src/error.rs | 10 ++--- parser/src/lib.rs | 68 ----------------------------- parser/src/parser/common/decl.rs | 75 +++++++++++++++++--------------- parser/src/parser/common/expr.rs | 30 ++++++------- parser/src/parser/mod.rs | 9 ++-- 5 files changed, 62 insertions(+), 130 deletions(-) diff --git a/parser/src/error.rs b/parser/src/error.rs index 9724c31..a5d50dc 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -10,10 +10,10 @@ pub enum ParseError<'a, T> { #[derive(Debug, Clone)] pub struct AstError<'a, T> { - pub span: pest::Span<'a>, - pub error_code: ErrorCode, - pub error_message: String, - pub recovered: Option, + span: pest::Span<'a>, + error_code: ErrorCode, + error_message: String, + recovered: Option, } #[derive(Debug, Clone)] @@ -89,7 +89,7 @@ where span: ast_err.span, error_code: ast_err.error_code, error_message: ast_err.error_message, - recovered: Some(items), + recovered: None, }), None => Ok(items), } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index f0ee50c..5ab2bbf 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -26,71 +26,3 @@ pub fn parse<'a>(source: &'a str) -> Result, ParseError<'a, Vec {{ - $($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)); - )* - - $($item)::+ { $($name,)* } - }}; -} - -#[macro_export] -macro_rules! _ast_t { - (: $val:expr) => { - $val.get()? - }; - - (? $val:expr) => { - $val.or_else(|e| { - if let Some(recovered) = e.recovered { - Ok(recovered) - } else { - Err(e) - } - }) - .get()? - }; -} - -#[macro_export] -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) => { - $val::try_from(&mut $inner) - }; - - ($inner:ident, @ $val:ident) => { - $val::try_from($inner.next().unwrap()) - }; - - ($inner:ident, ? $val:ident) => { - $inner.next().map($val::try_from).transpose() - }; - - ($inner:ident, ? ($rule:path)) => { - $crate::parser::listen_rule(&mut $inner, $rule) - }; - - ($inner:ident, & $val:expr) => { - $val - }; -} diff --git a/parser/src/parser/common/decl.rs b/parser/src/parser/common/decl.rs index 99cd76e..d9a3dc5 100644 --- a/parser/src/parser/common/decl.rs +++ b/parser/src/parser/common/decl.rs @@ -1,8 +1,8 @@ use crate::{ Rule, ast::*, - ast_expr, error::{AstError, GetParseError}, + parser::listen_rule, }; impl<'a> TryFrom> for VarDeclStmt { @@ -13,12 +13,11 @@ impl<'a> TryFrom> for VarDeclStmt { Rule::var_decl_statement => { let mut inner = pair.into_inner(); - Ok(ast_expr!( - inner@VarDeclStmt { - decl: @VarDecl, - init: ?Expression - } - )) + let decl = VarDecl::try_from(inner.next().unwrap()).get()?; + + let init = inner.next().map(Expression::try_from).transpose().get()?; + + Ok(VarDeclStmt { decl, init }) } _ => unimplemented!(), @@ -34,12 +33,11 @@ impl<'a> TryFrom> for FieldDeclStmt { Rule::class_field => { let mut inner = pair.into_inner(); - Ok(ast_expr!( - inner@FieldDeclStmt { - decl: @FieldDecl, - init: ?Expression - } - )) + let decl = FieldDecl::try_from(inner.next().unwrap()).get()?; + + let init = inner.next().map(Expression::try_from).transpose().get()?; + + Ok(FieldDeclStmt { decl, init }) } _ => unimplemented!(), @@ -55,24 +53,27 @@ impl<'a> TryFrom> for VarDecl { Rule::var_decl => { let mut inner = pair.into_inner(); - Ok(ast_expr!( - inner@VarDecl { - type_: & (inner - .next() - .and_then(|pair| { - if pair.as_str().trim() == "var" { - None - } else { - Some(TypeExpr::try_from(pair)) - } - }) - .transpose()), + let type_ = inner + .next() + .and_then(|pair| { + if pair.as_str().trim() == "var" { + None + } else { + Some(TypeExpr::try_from(pair)) + } + }) + .transpose() + .get()?; - mutable: ?(Rule::mutable), + let mutable = listen_rule(&mut inner, Rule::mutable); - name: @Pattern - } - )) + let name = Pattern::try_from(inner.next().unwrap()).get()?; + + Ok(VarDecl { + mutable, + name, + type_, + }) } _ => unimplemented!("{:?}", pair.as_rule()), @@ -88,13 +89,15 @@ impl<'a> TryFrom> for FieldDecl { Rule::field => { let mut inner = pair.into_inner(); - Ok(ast_expr!( - inner@FieldDecl { - visibility: !Visibility, - type_: @TypeExpr, - name: @Identifier - } - )) + let visibility = Visibility::try_from(&mut inner).get()?; + let type_ = TypeExpr::try_from(inner.next().unwrap()).get()?; + let name = Identifier::try_from(inner.next().unwrap()).get()?; + + Ok(FieldDecl { + visibility, + type_, + name, + }) } _ => unimplemented!("{:?}", pair.as_rule()), diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 8a1dd53..0d25e58 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -1,8 +1,7 @@ use crate::{ Rule, ast::*, - ast_expr, - error::{AstError, AstResult, GetParseError, collect_recovered}, + error::{AstError, AstResult}, }; impl<'a> TryFrom> for Expression { @@ -33,16 +32,15 @@ impl<'a> TryFrom> for Expression { prefixes, postfixes: inner .map(|p| Postfix::try_from(p)) - .collect::>>() - .get()?, + .collect::>>()?, }) } else { Ok(exp) } } Rule::primary => Expression::try_from(inner.next().unwrap()), - Rule::static_path => Ok(Expression::Path(Path::try_from(pair).get()?)), - Rule::literal => Ok(Expression::Literal(Literal::try_from(pair).get()?)), + Rule::static_path => Ok(Expression::Path(Path::try_from(pair)?)), + Rule::literal => Ok(Expression::Literal(Literal::try_from(pair)?)), _ => unimplemented!("{rule:#?}"), } } @@ -74,24 +72,27 @@ impl<'a> TryFrom> for Postfix { Ok(match rule { Rule::postfix => Postfix::try_from(inner.next().unwrap())?, - Rule::field_px => ast_expr!(inner@Postfix::FieldAccess(:@@)), + Rule::field_px => Postfix::FieldAccess(Identifier::try_from(inner.next().unwrap())?), - Rule::call_px => ast_expr!(inner@Postfix::Call(:!*)), + Rule::call_px => Postfix::Call(inner.map(Expression::try_from).collect::, + _, + >>()?), Rule::struct_px => Postfix::StructCall( inner .map(|p| { let mut pi = p.into_inner(); Ok(( - Identifier::try_from(pi.next().unwrap()).get()?, - Expression::try_from(pi.next().unwrap()).get()?, + Identifier::try_from(pi.next().unwrap())?, + Expression::try_from(pi.next().unwrap())?, )) }) - .collect::>>() - .get()?, + .collect::>>()?, ), - Rule::index_px => ast_expr!(inner@Postfix::Index(:@@)), + Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap())?), Rule::binary_px => { let op_pair = inner.next().unwrap(); @@ -114,8 +115,7 @@ impl<'a> TryFrom> for Postfix { unimplemented!("Binary operator not implemented yet: {}", op_pair.as_str()) } }; - - ast_expr!(inner@Postfix::Binary(:&op, :@@)) + Postfix::Binary(op, Expression::try_from(inner.next().unwrap())?) } Rule::macro_call_px => Postfix::MacroCall(inner.as_str().to_string()), diff --git a/parser/src/parser/mod.rs b/parser/src/parser/mod.rs index 6900d7b..d7dad09 100644 --- a/parser/src/parser/mod.rs +++ b/parser/src/parser/mod.rs @@ -1,12 +1,9 @@ pub mod common; pub mod items; -use crate::{Rule, error::AstResult}; +use crate::Rule; -pub fn listen_rule<'a>( - pairs: &mut pest::iterators::Pairs<'_, Rule>, - rule: Rule, -) -> AstResult<'a, bool> { +pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool { let consumed = pairs .peek() .map(|p| p.as_rule() == rule) @@ -16,7 +13,7 @@ pub fn listen_rule<'a>( pairs.next(); } - Ok(consumed) + consumed } pub fn consume_rule<'a>(