diff --git a/parser/src/error.rs b/parser/src/error.rs index 03c60d2..e1dd861 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -1,5 +1,7 @@ use std::fmt::Debug; +use pest::iterators::Pair; + use crate::Rule; pub type AstResult<'a, T, ET = T> = Result>; @@ -45,6 +47,15 @@ impl<'a, F> AstError<'a, F> { recovered: None, } } + + pub fn bug_unimplemented(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 { diff --git a/parser/src/parser/common/decl.rs b/parser/src/parser/common/decl.rs index 248fb3f..4cf8bfb 100644 --- a/parser/src/parser/common/decl.rs +++ b/parser/src/parser/common/decl.rs @@ -20,7 +20,7 @@ impl<'a> TryFrom> for VarDeclStmt { }) } - _ => unimplemented!(), + _ => AstError::bug_unimplemented(pair), } } } @@ -39,7 +39,7 @@ impl<'a> TryFrom> for FieldDeclStmt { }) } - _ => unimplemented!(), + _ => AstError::bug_unimplemented(pair), } } } @@ -74,7 +74,7 @@ impl<'a> TryFrom> for VarDecl { }) } - _ => unimplemented!("{:?}", pair.as_rule()), + _ => AstError::bug_unimplemented(pair), } } } @@ -94,7 +94,7 @@ impl<'a> TryFrom> for FieldDecl { }) } - _ => unimplemented!("{:?}", pair.as_rule()), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 4e4a02a..e49680d 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -31,10 +31,12 @@ impl<'a> TryFrom> 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> 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> for Postfix { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { 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> 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> for Postfix { Rule::macro_call_px => Ok(Postfix::MacroCall(inner.as_str().to_string())), - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/common/mod.rs b/parser/src/parser/common/mod.rs index 5c57d99..206e3c9 100644 --- a/parser/src/parser/common/mod.rs +++ b/parser/src/parser/common/mod.rs @@ -27,7 +27,7 @@ impl<'a> TryFrom> for Path { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { 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> for Literal { Rule::boolean => Literal::Bool(pair.as_str().parse::().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> for Pattern { Rule::static_path => ast_expr!(Pattern::Path(pair.try_into())), - _ => unimplemented!("{rule:?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/common/statement.rs b/parser/src/parser/common/statement.rs index 5dd84b2..9de2546 100644 --- a/parser/src/parser/common/statement.rs +++ b/parser/src/parser/common/statement.rs @@ -119,7 +119,7 @@ impl<'a> TryFrom> for Statement { }); } - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/common/types.rs b/parser/src/parser/common/types.rs index 2ec02a4..7e64d1c 100644 --- a/parser/src/parser/common/types.rs +++ b/parser/src/parser/common/types.rs @@ -11,7 +11,7 @@ impl<'a> TryFrom> for TypePostfix { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { 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> for TypePostfix { } }) } - _ => unimplemented!("{rule:#?}"), + + _ => AstError::bug_unimplemented(pair), } } } @@ -45,7 +46,7 @@ impl<'a> TryFrom> for TypeExprKind { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { 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> for TypeExprKind { ast_expr!(TypeExprKind::PathParams(path, params)) } } - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } @@ -69,7 +70,7 @@ impl<'a> TryFrom> for TypeExpr { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { 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> 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> for Generics { match rule { Rule::generics => ast_expr!(Generics(collect_recovered(inner))), - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/items/class.rs b/parser/src/parser/items/class.rs index 6694f7f..86eef27 100644 --- a/parser/src/parser/items/class.rs +++ b/parser/src/parser/items/class.rs @@ -44,7 +44,7 @@ impl<'a> TryFrom> for ClassItem { Rule::method => Ok(ClassItem::Method(pair.try_into().get()?)), - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/items/enums.rs b/parser/src/parser/items/enums.rs index d04f5c8..0f6d8fe 100644 --- a/parser/src/parser/items/enums.rs +++ b/parser/src/parser/items/enums.rs @@ -29,7 +29,7 @@ impl<'a> TryFrom> for EnumItem { .unwrap_or_default(), )), - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/items/impl_decl.rs b/parser/src/parser/items/impl_decl.rs index dfcd457..6ad4ca4 100644 --- a/parser/src/parser/items/impl_decl.rs +++ b/parser/src/parser/items/impl_decl.rs @@ -35,7 +35,7 @@ impl<'a> TryFrom> for ImplDecl { methods: collect_recovered(inner).get()?, }), - _ => unimplemented!("{rule:#?}"), + _ => AstError::bug_unimplemented(pair), } } } diff --git a/parser/src/parser/items/mod.rs b/parser/src/parser/items/mod.rs index 5ea9b56..f5079c2 100644 --- a/parser/src/parser/items/mod.rs +++ b/parser/src/parser/items/mod.rs @@ -123,7 +123,7 @@ impl<'a> TryFrom> for TopLevelKind { items: collect_recovered(inner).get()?, }, - _ => unimplemented!("{rule:#?}"), + _ => return AstError::bug_unimplemented(pair), }) } }