diff --git a/parser/src/parser/common/types.rs b/parser/src/parser/common/types.rs index 89493f3..19a91c9 100644 --- a/parser/src/parser/common/types.rs +++ b/parser/src/parser/common/types.rs @@ -1,7 +1,7 @@ use crate::{ Rule, ast::*, - error::{AstError, AstResult}, + error::{AstError, GetParseError, collect_recovered}, parser::{consume_rule, listen_rule}, }; @@ -17,7 +17,8 @@ impl<'a> TryFrom> for TypePostfix { let mutable = listen_rule(&mut inner, Rule::mutable); let lifetime = consume_rule(&mut inner, Rule::lifetime) .map(|pair| Identifier::try_from(pair.into_inner().next().unwrap())) - .transpose()?; + .transpose() + .get()?; Ok(if mutable { if let Some(lifetime) = lifetime { @@ -46,16 +47,10 @@ impl<'a> TryFrom> for TypeExprKind { let mut inner = pair.into_inner(); match rule { - Rule::tuple_type => Ok(TypeExprKind::Tuple( - inner - .map(TypeExpr::try_from) - .collect::>()?, - )), + Rule::tuple_type => Ok(TypeExprKind::Tuple(collect_recovered(inner).get()?)), Rule::path_type => { - let path = Path::try_from(inner.next().unwrap())?; - let params = inner - .map(TypeExpr::try_from) - .collect::>>()?; + let path = Path::try_from(inner.next().unwrap()).get()?; + let params = collect_recovered(inner).get()?; if params.len() == 0 { Ok(TypeExprKind::Path(path)) @@ -77,14 +72,12 @@ impl<'a> TryFrom> for TypeExpr { match rule { Rule::type_expr => Ok(TypeExpr( - TypeExprKind::try_from(inner.next().unwrap())?, - inner - .map(TypePostfix::try_from) - .collect::>()?, + inner.next().unwrap().try_into().get()?, + collect_recovered(inner).get()?, )), Rule::type_expr_param => Self::try_from(inner.next().unwrap()), Rule::lifetime => Ok(TypeExpr( - TypeExprKind::Lifetime(Identifier::try_from(inner.next().unwrap())?), + TypeExprKind::Lifetime(inner.next().unwrap().try_into().get()?), Vec::new(), )), _ => unimplemented!("{rule:#?}"), @@ -100,28 +93,27 @@ impl<'a> TryFrom> for Generics { let inner = pair.clone().into_inner(); match rule { - Rule::generics => Ok(Generics( - inner - .map(|pair| -> AstResult<'a, Generic> { - let mut inner = pair.into_inner(); - Ok( - if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) { - Generic::Lifetime(Identifier::try_from( - pair.into_inner().next().unwrap(), - )?) - } else { - Generic::Type( - Identifier::try_from(inner.next().unwrap())?, - inner - .map(TypeExpr::try_from) - .collect::>>()?, - ) - }, - ) - }) - .collect::>>()?, - )), + Rule::generics => Ok(Generics(collect_recovered(inner).get()?)), _ => unimplemented!("{rule:#?}"), } } } + +impl<'a> TryFrom> for Generic { + type Error = AstError<'a, Self>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + let mut inner = pair.clone().into_inner(); + + if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) { + Ok(Generic::Lifetime( + pair.into_inner().next().unwrap().try_into().get()?, + )) + } else { + Ok(Generic::Type( + inner.next().unwrap().try_into().get()?, + collect_recovered(inner).get()?, + )) + } + } +}