Reverted back

This commit is contained in:
2026-05-15 02:23:54 +02:00
parent 933f102680
commit 459c4b1ee0
5 changed files with 62 additions and 130 deletions
+5 -5
View File
@@ -10,10 +10,10 @@ pub enum ParseError<'a, T> {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AstError<'a, T> { pub struct AstError<'a, T> {
pub span: pest::Span<'a>, span: pest::Span<'a>,
pub error_code: ErrorCode, error_code: ErrorCode,
pub error_message: String, error_message: String,
pub recovered: Option<T>, recovered: Option<T>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -89,7 +89,7 @@ where
span: ast_err.span, span: ast_err.span,
error_code: ast_err.error_code, error_code: ast_err.error_code,
error_message: ast_err.error_message, error_message: ast_err.error_message,
recovered: Some(items), recovered: None,
}), }),
None => Ok(items), None => Ok(items),
} }
-68
View File
@@ -26,71 +26,3 @@ pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<To
Ok(statements) Ok(statements)
} }
#[macro_export]
macro_rules! ast_expr {
($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));
)*
$($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
};
}
+31 -28
View File
@@ -1,8 +1,8 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_expr,
error::{AstError, GetParseError}, error::{AstError, GetParseError},
parser::listen_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
@@ -13,12 +13,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
Rule::var_decl_statement => { Rule::var_decl_statement => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
Ok(ast_expr!( let decl = VarDecl::try_from(inner.next().unwrap()).get()?;
inner@VarDeclStmt {
decl: @VarDecl, let init = inner.next().map(Expression::try_from).transpose().get()?;
init: ?Expression
} Ok(VarDeclStmt { decl, init })
))
} }
_ => unimplemented!(), _ => unimplemented!(),
@@ -34,12 +33,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
Rule::class_field => { Rule::class_field => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
Ok(ast_expr!( let decl = FieldDecl::try_from(inner.next().unwrap()).get()?;
inner@FieldDeclStmt {
decl: @FieldDecl, let init = inner.next().map(Expression::try_from).transpose().get()?;
init: ?Expression
} Ok(FieldDeclStmt { decl, init })
))
} }
_ => unimplemented!(), _ => unimplemented!(),
@@ -55,9 +53,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
Rule::var_decl => { Rule::var_decl => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
Ok(ast_expr!( let type_ = inner
inner@VarDecl {
type_: & (inner
.next() .next()
.and_then(|pair| { .and_then(|pair| {
if pair.as_str().trim() == "var" { if pair.as_str().trim() == "var" {
@@ -66,13 +62,18 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
Some(TypeExpr::try_from(pair)) Some(TypeExpr::try_from(pair))
} }
}) })
.transpose()), .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()), _ => unimplemented!("{:?}", pair.as_rule()),
@@ -88,13 +89,15 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
Rule::field => { Rule::field => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
Ok(ast_expr!( let visibility = Visibility::try_from(&mut inner).get()?;
inner@FieldDecl { let type_ = TypeExpr::try_from(inner.next().unwrap()).get()?;
visibility: !Visibility, let name = Identifier::try_from(inner.next().unwrap()).get()?;
type_: @TypeExpr,
name: @Identifier Ok(FieldDecl {
} visibility,
)) type_,
name,
})
} }
_ => unimplemented!("{:?}", pair.as_rule()), _ => unimplemented!("{:?}", pair.as_rule()),
+15 -15
View File
@@ -1,8 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_expr, error::{AstError, AstResult},
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 {
@@ -33,16 +32,15 @@ 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).get()?)), Rule::static_path => Ok(Expression::Path(Path::try_from(pair)?)),
Rule::literal => Ok(Expression::Literal(Literal::try_from(pair).get()?)), Rule::literal => Ok(Expression::Literal(Literal::try_from(pair)?)),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
@@ -74,24 +72,27 @@ 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 => 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::<AstResult<
'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()).get()?, Identifier::try_from(pi.next().unwrap())?,
Expression::try_from(pi.next().unwrap()).get()?, Expression::try_from(pi.next().unwrap())?,
)) ))
}) })
.collect::<AstResult<'a, Vec<_>>>() .collect::<AstResult<'a, Vec<_>>>()?,
.get()?,
), ),
Rule::index_px => ast_expr!(inner@Postfix::Index(:@@)), Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap())?),
Rule::binary_px => { Rule::binary_px => {
let op_pair = inner.next().unwrap(); let op_pair = inner.next().unwrap();
@@ -114,8 +115,7 @@ 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()),
+3 -6
View File
@@ -1,12 +1,9 @@
pub mod common; pub mod common;
pub mod items; pub mod items;
use crate::{Rule, error::AstResult}; use crate::Rule;
pub fn listen_rule<'a>( pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
pairs: &mut pest::iterators::Pairs<'_, Rule>,
rule: Rule,
) -> AstResult<'a, bool> {
let consumed = pairs let consumed = pairs
.peek() .peek()
.map(|p| p.as_rule() == rule) .map(|p| p.as_rule() == rule)
@@ -16,7 +13,7 @@ pub fn listen_rule<'a>(
pairs.next(); pairs.next();
} }
Ok(consumed) consumed
} }
pub fn consume_rule<'a>( pub fn consume_rule<'a>(