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)]
pub struct AstError<'a, T> {
pub span: pest::Span<'a>,
pub error_code: ErrorCode,
pub error_message: String,
pub recovered: Option<T>,
span: pest::Span<'a>,
error_code: ErrorCode,
error_message: String,
recovered: Option<T>,
}
#[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),
}
-68
View File
@@ -26,71 +26,3 @@ pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<To
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
};
}
+39 -36
View File
@@ -1,8 +1,8 @@
use crate::{
Rule,
ast::*,
ast_expr,
error::{AstError, GetParseError},
parser::listen_rule,
};
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 => {
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<pest::iterators::Pair<'a, Rule>> 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<pest::iterators::Pair<'a, Rule>> 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<pest::iterators::Pair<'a, Rule>> 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()),
+15 -15
View File
@@ -1,8 +1,7 @@
use crate::{
Rule,
ast::*,
ast_expr,
error::{AstError, AstResult, GetParseError, collect_recovered},
error::{AstError, AstResult},
};
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,
postfixes: inner
.map(|p| Postfix::try_from(p))
.collect::<AstResult<'a, Vec<_>>>()
.get()?,
.collect::<AstResult<'a, Vec<_>>>()?,
})
} 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<pest::iterators::Pair<'a, Rule>> 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::<AstResult<
'a,
Vec<_>,
_,
>>()?),
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::<AstResult<'a, Vec<_>>>()
.get()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
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<pest::iterators::Pair<'a, Rule>> 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()),
+3 -6
View File
@@ -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>(