Bug implementations

This commit is contained in:
2026-05-16 06:50:24 +02:00
parent ced2d47977
commit 3619f0e7dc
10 changed files with 40 additions and 26 deletions
+11
View File
@@ -1,5 +1,7 @@
use std::fmt::Debug;
use pest::iterators::Pair;
use crate::Rule;
pub type AstResult<'a, T, ET = T> = Result<T, AstError<'a, ET>>;
@@ -45,6 +47,15 @@ impl<'a, F> AstError<'a, F> {
recovered: None,
}
}
pub fn bug_unimplemented<T>(pair: Pair<'a, Rule>) -> AstResult<'a, T, F> {
Err(Self {
span: pair.as_span(),
error_code: ErrorCode::AstGenBug,
error_message: format!("Possible bug, unimplemented: {:#?}", pair),
recovered: None,
})
}
}
pub trait IntoErr<T, FA, FR> {
+4 -4
View File
@@ -20,7 +20,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
})
}
_ => unimplemented!(),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -39,7 +39,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
})
}
_ => unimplemented!(),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -74,7 +74,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
})
}
_ => unimplemented!("{:?}", pair.as_rule()),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -94,7 +94,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
})
}
_ => unimplemented!("{:?}", pair.as_rule()),
_ => AstError::bug_unimplemented(pair),
}
}
}
+8 -7
View File
@@ -31,10 +31,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
exp
}
}
Rule::primary => inner.next().unwrap().try_into(),
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -50,7 +52,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
Rule::ref_px => Self::Ref,
Rule::new_px => Self::New,
Rule::not_px => Self::Not,
_ => unimplemented!("{pair:#?}"),
_ => return AstError::bug_unimplemented(pair),
})
}
}
@@ -60,7 +63,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.into_inner();
let mut inner = pair.clone().into_inner();
match rule {
Rule::postfix => Postfix::try_from(inner.next().unwrap()),
@@ -100,9 +103,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
"&&" => BinaryOp::And,
"||" => BinaryOp::Or,
_ => {
unimplemented!("Binary operator not implemented yet: {}", op_pair.as_str())
}
_ => return AstError::bug_unimplemented(op_pair),
};
Ok(Postfix::Binary(op, inner.next().unwrap().try_into().get()?))
@@ -110,7 +111,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Rule::macro_call_px => Ok(Postfix::MacroCall(inner.as_str().to_string())),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+3 -3
View File
@@ -27,7 +27,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Path {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
Rule::static_path => Ok(Path(collect_recovered(pair.into_inner()).get()?)),
_ => unimplemented!("{pair:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -47,7 +47,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
Rule::boolean => Literal::Bool(pair.as_str().parse::<bool>().unwrap()),
Rule::string_lit => Literal::String(inner.as_str().to_string()),
Rule::tuple => Literal::Tuple(collect_recovered(inner).get()?),
_ => unimplemented!("{rule:#?}"),
_ => return AstError::bug_unimplemented(pair),
})
}
}
@@ -78,7 +78,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
Rule::static_path => ast_expr!(Pattern::Path(pair.try_into())),
_ => unimplemented!("{rule:?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+1 -1
View File
@@ -119,7 +119,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
});
}
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+9 -7
View File
@@ -11,7 +11,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.into_inner();
let mut inner = pair.clone().into_inner();
match rule {
Rule::ref_type => {
@@ -35,7 +35,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
}
})
}
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -45,7 +46,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.into_inner();
let mut inner = pair.clone().into_inner();
match rule {
Rule::tuple_type => ast_expr!(TypeExprKind::Tuple(collect_recovered(inner))),
@@ -59,7 +60,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
ast_expr!(TypeExprKind::PathParams(path, params))
}
}
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -69,7 +70,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.into_inner();
let mut inner = pair.clone().into_inner();
match rule {
Rule::type_expr => ast_expr!(TypeExpr(
@@ -80,7 +81,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
Rule::lifetime => ast_expr!(TypeExprKind::Lifetime(inner.next().unwrap().try_into()))
.get_map(TypeExpr::no_px)
.map(TypeExpr::no_px),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
@@ -94,7 +96,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
match rule {
Rule::generics => ast_expr!(Generics(collect_recovered(inner))),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassItem {
Rule::method => Ok(ClassItem::Method(pair.try_into().get()?)),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+1 -1
View File
@@ -29,7 +29,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
.unwrap_or_default(),
)),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+1 -1
View File
@@ -35,7 +35,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
methods: collect_recovered(inner).get()?,
}),
_ => unimplemented!("{rule:#?}"),
_ => AstError::bug_unimplemented(pair),
}
}
}
+1 -1
View File
@@ -123,7 +123,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
items: collect_recovered(inner).get()?,
},
_ => unimplemented!("{rule:#?}"),
_ => return AstError::bug_unimplemented(pair),
})
}
}