types error handling
This commit is contained in:
@@ -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<pest::iterators::Pair<'a, Rule>> 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<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::tuple_type => Ok(TypeExprKind::Tuple(
|
||||
inner
|
||||
.map(TypeExpr::try_from)
|
||||
.collect::<AstResult<'a, _>>()?,
|
||||
)),
|
||||
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::<AstResult<'a, Vec<_>>>()?;
|
||||
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<pest::iterators::Pair<'a, Rule>> for TypeExpr {
|
||||
|
||||
match rule {
|
||||
Rule::type_expr => Ok(TypeExpr(
|
||||
TypeExprKind::try_from(inner.next().unwrap())?,
|
||||
inner
|
||||
.map(TypePostfix::try_from)
|
||||
.collect::<AstResult<'a, _>>()?,
|
||||
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<pest::iterators::Pair<'a, Rule>> 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::<AstResult<'a, Vec<_>>>()?,
|
||||
)
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect::<AstResult<'a, Vec<_>>>()?,
|
||||
)),
|
||||
Rule::generics => Ok(Generics(collect_recovered(inner).get()?)),
|
||||
_ => unimplemented!("{rule:#?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generic {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
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()?,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user