Getting error handling to work

This commit is contained in:
2026-05-15 00:03:04 +02:00
parent cdb34984db
commit 25f4b5470d
13 changed files with 136 additions and 106 deletions
+57 -27
View File
@@ -1,16 +1,19 @@
use crate::Rule;
pub type ParseResult<'a, T, ET = T> = Result<T, ParseError<'a, ET>>;
pub type AstResult<'a, T, ET = T> = Result<T, AstError<'a, ET>>;
#[derive(Debug, Clone)]
pub enum ParseError<'a, T> {
PreAst(pest::error::Error<Rule>),
Ast {
Ast(AstError<'a, T>),
}
#[derive(Debug, Clone)]
pub struct AstError<'a, T> {
span: pest::Span<'a>,
error_code: ErrorCode,
error_message: String,
recovered: Option<T>,
},
}
#[derive(Debug, Clone)]
@@ -18,37 +21,35 @@ pub enum ErrorCode {
InvalidStatement = 200,
}
impl<'a, F> ParseError<'a, F> {
pub fn get<T>(self) -> ParseError<'a, T> {
match self {
Self::Ast {
span,
error_code,
error_message,
..
} => ParseError::Ast {
span,
error_code,
error_message,
recovered: None,
},
Self::PreAst(pest_err) => ParseError::PreAst(pest_err),
}
}
}
impl<T> From<pest::error::Error<Rule>> for ParseError<'_, T> {
fn from(value: pest::error::Error<Rule>) -> Self {
Self::PreAst(value)
}
}
impl<'a, T> From<AstError<'a, T>> for ParseError<'a, T> {
fn from(value: AstError<'a, T>) -> Self {
Self::Ast(value)
}
}
impl<'a, F> AstError<'a, F> {
pub fn get<T>(self) -> AstError<'a, T> {
AstError {
span: self.span,
error_code: self.error_code,
error_message: self.error_message,
recovered: None,
}
}
}
pub trait GetParseError<'a, F> {
fn get<T>(self) -> ParseResult<'a, F, T>;
fn get<T>(self) -> AstResult<'a, F, T>;
}
impl<'a, F> GetParseError<'a, F> for ParseResult<'a, F> {
fn get<T>(self) -> ParseResult<'a, F, T> {
impl<'a, F> GetParseError<'a, F> for AstResult<'a, F> {
fn get<T>(self) -> AstResult<'a, F, T> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.get()),
@@ -56,11 +57,40 @@ impl<'a, F> GetParseError<'a, F> for ParseResult<'a, F> {
}
}
impl<'a, F> GetParseError<'a, Option<F>> for Result<Option<F>, ParseError<'a, F>> {
fn get<T>(self) -> ParseResult<'a, Option<F>, T> {
impl<'a, F> GetParseError<'a, Option<F>> for Result<Option<F>, AstError<'a, F>> {
fn get<T>(self) -> AstResult<'a, Option<F>, T> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.get()),
}
}
}
pub fn collect_recovered<'a, T, ET>(
pairs: impl Iterator<Item = pest::iterators::Pair<'a, Rule>>,
) -> AstResult<'a, Vec<T>, Vec<T>>
where
T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, ET>>,
{
let mut items = Vec::new();
let mut last_error: Option<AstError<'a, ET>> = None;
for pair in pairs {
match T::try_from(pair) {
Ok(item) => items.push(item),
Err(e) => {
last_error = Some(e);
}
}
}
match last_error {
Some(ast_err) => Err(AstError {
span: ast_err.span,
error_code: ast_err.error_code,
error_message: ast_err.error_message,
recovered: None,
}),
None => Ok(items),
}
}
+2 -2
View File
@@ -7,13 +7,13 @@ pub mod parser;
use ast::*;
use crate::error::{GetParseError, ParseResult};
use crate::error::{GetParseError, ParseError};
#[derive(Parser)]
#[grammar = "./src/grammar.pest"]
pub struct MistParser;
pub fn parse<'a>(source: &'a str) -> ParseResult<'a, Vec<TopLevel>> {
pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<TopLevel>>> {
let mut pairs = MistParser::parse(Rule::program, source)?;
let mut statements = vec![];
+5 -5
View File
@@ -1,12 +1,12 @@
use crate::{
Rule,
ast::*,
error::{GetParseError, ParseError},
error::{AstError, GetParseError},
parser::listen_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
@@ -26,7 +26,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
@@ -46,7 +46,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
@@ -82,7 +82,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
+8 -8
View File
@@ -1,11 +1,11 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -19,7 +19,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
p.into_inner()
.into_iter()
.map(Prefix::try_from)
.collect::<ParseResult<'a, Vec<_>, _>>()
.collect::<AstResult<'a, Vec<_>, _>>()
})
.transpose()?
.unwrap_or_default();
@@ -32,7 +32,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
prefixes,
postfixes: inner
.map(|p| Postfix::try_from(p))
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
})
} else {
Ok(exp)
@@ -47,7 +47,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(match pair.as_rule() {
@@ -63,7 +63,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -74,7 +74,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Rule::field_px => Postfix::FieldAccess(Identifier::try_from(inner.next().unwrap())?),
Rule::call_px => Postfix::Call(inner.map(Expression::try_from).collect::<ParseResult<
Rule::call_px => Postfix::Call(inner.map(Expression::try_from).collect::<AstResult<
'a,
Vec<_>,
_,
@@ -89,7 +89,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Expression::try_from(pi.next().unwrap())?,
))
})
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap())?),
+12 -12
View File
@@ -6,12 +6,12 @@ pub mod types;
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
parser::consume_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Identifier {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(Identifier(pair.as_str().to_string()))
@@ -19,14 +19,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Identifier {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Path {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
Rule::static_path => Ok(Path(
pair.into_inner()
.map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
)),
_ => unimplemented!("{pair:#?}"),
}
@@ -34,7 +34,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Path {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -50,7 +50,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
Rule::tuple => Literal::Tuple(
inner
.map(Expression::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
_ => unimplemented!("{rule:#?}"),
})
@@ -58,7 +58,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -68,21 +68,21 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
Rule::tuple_pattern => Pattern::Tuple(
inner
.map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
Rule::named_tuple_pattern => Pattern::NamedTuple(
Path::try_from(inner.next().unwrap())?,
inner
.map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
Rule::struct_pattern => Pattern::Struct(
Path::try_from(inner.next().unwrap())?,
inner
.map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
Rule::literal => Pattern::Literal(Literal::try_from(pair)?),
@@ -97,11 +97,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
}
impl<'a> TryFrom<&mut pest::iterators::Pairs<'a, Rule>> for Visibility {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pairs: &mut pest::iterators::Pairs<'a, Rule>) -> Result<Self, Self::Error> {
Ok(consume_rule(pairs, Rule::visibility)
.map(|pair| -> Result<Visibility, ParseError<'a, Self>> {
.map(|pair| -> Result<Visibility, AstError<'a, Self>> {
if let Some(path) = pair.into_inner().next() {
Ok(Visibility::PublicTarget(Path::try_from(path)?))
} else {
+8 -8
View File
@@ -1,12 +1,12 @@
use crate::{
Rule,
ast::*,
error::{ErrorCode, ParseError, ParseResult},
error::{ErrorCode, AstError, AstResult},
parser::listen_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let statements = pair
@@ -18,14 +18,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
vec![Statement::try_from(pair)]
}
})
.collect::<ParseResult<'a, Vec<_>>>()?;
.collect::<AstResult<'a, Vec<_>>>()?;
Ok(Block(statements))
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -58,7 +58,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
.unwrap()
.into_inner()
.map(StatementBranch::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
else_branch: inner
.next()
.map(Statement::try_from)
@@ -98,11 +98,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
Block::try_from(match_inner.next().unwrap())?,
))
})
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
),
Rule::unexpected_statement => {
return Err(ParseError::Ast {
return Err(AstError::Ast {
span: pair.as_span(),
error_code: ErrorCode::InvalidStatement,
error_message: "Invalid Statement".to_string(),
@@ -115,7 +115,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for StatementBranch {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner();
+11 -11
View File
@@ -1,12 +1,12 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
parser::{consume_rule, listen_rule},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -39,7 +39,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -49,13 +49,13 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
Rule::tuple_type => Ok(TypeExprKind::Tuple(
inner
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, _>>()?,
.collect::<AstResult<'a, _>>()?,
)),
Rule::path_type => {
let path = Path::try_from(inner.next().unwrap())?;
let params = inner
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?;
.collect::<AstResult<'a, Vec<_>>>()?;
if params.len() == 0 {
Ok(TypeExprKind::Path(path))
@@ -69,7 +69,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -80,7 +80,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
TypeExprKind::try_from(inner.next().unwrap())?,
inner
.map(TypePostfix::try_from)
.collect::<ParseResult<'a, _>>()?,
.collect::<AstResult<'a, _>>()?,
)),
Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
Rule::lifetime => Ok(TypeExpr(
@@ -93,7 +93,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -102,7 +102,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
match rule {
Rule::generics => Ok(Generics(
inner
.map(|pair| -> ParseResult<'a, Generic> {
.map(|pair| -> AstResult<'a, Generic> {
let mut inner = pair.into_inner();
Ok(
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
@@ -114,12 +114,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
Identifier::try_from(inner.next().unwrap())?,
inner
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
)
},
)
})
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
)),
_ => unimplemented!("{rule:#?}"),
}
+3 -3
View File
@@ -1,11 +1,11 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() {
@@ -41,7 +41,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
let items = next
.into_inner()
.map(Attribute::try_from)
.collect::<ParseResult<'a, Vec<_>, _>>()?;
.collect::<AstResult<'a, Vec<_>, _>>()?;
Ok(Attribute::List { path, items })
}
+3 -3
View File
@@ -1,7 +1,7 @@
use crate::{Rule, ast::*, error::ParseError, parser::consume_rule};
use crate::{Rule, ast::*, error::AstError, parser::consume_rule};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner();
@@ -28,7 +28,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassItem {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
+4 -4
View File
@@ -1,11 +1,11 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -23,7 +23,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
.unwrap()
.into_inner()
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
)),
Rule::enum_struct => Ok(EnumItem::Struct(
@@ -33,7 +33,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
.map(|pair| {
pair.into_inner()
.map(FieldDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()
.collect::<AstResult<'a, Vec<_>>>()
})
.transpose()?
.unwrap_or_default(),
+5 -5
View File
@@ -1,12 +1,12 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
parser::{consume_rule, listen_rule},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner();
@@ -47,7 +47,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
let params = consume_rule(&mut inner, Rule::param_list)
.map({
let self_param = self_param.clone();
|params_pair| -> ParseResult<'a, ParamList> {
|params_pair| -> AstResult<'a, ParamList> {
let mut params = ParamList::try_from(params_pair)?;
if let Some(x) = self_param {
params.0.insert(0, x);
@@ -72,13 +72,13 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(ParamList(
pair.into_inner()
.map(VarDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
))
}
}
+4 -4
View File
@@ -1,12 +1,12 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
parser::consume_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -22,7 +22,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
}),
Rule::impl_decl => Ok(ImplDecl {
@@ -34,7 +34,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
}),
_ => unimplemented!("{rule:#?}"),
+10 -10
View File
@@ -7,12 +7,12 @@ pub mod impl_decl;
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
error::{AstError, AstResult},
parser::consume_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner();
@@ -22,7 +22,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
.unwrap()
.into_inner()
.map(Attribute::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?;
.collect::<AstResult<'a, Vec<_>>>()?;
Ok(TopLevel(
inner
@@ -35,7 +35,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
type Error = ParseError<'a, Self>;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
@@ -61,7 +61,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.map(|pair| {
pair.into_inner()
.map(FieldDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()
.collect::<AstResult<'a, Vec<_>>>()
})
.transpose()?
.unwrap_or_default(),
@@ -79,12 +79,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.unwrap()
.into_inner()
.map(FieldDeclStmt::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
constructor: ClassConstructor::try_from(inner.next().unwrap())?,
items: inner
.into_iter()
.map(ClassItem::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
},
Rule::enum_decl => TopLevelKind::EnumDecl {
@@ -96,7 +96,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.unwrap_or_default(),
fields: inner
.map(EnumItem::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
},
Rule::mod_package => TopLevelKind::Mod(
@@ -119,13 +119,13 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.map(|pair| {
pair.into_inner()
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()
.collect::<AstResult<'a, Vec<_>>>()
})
.transpose()?
.unwrap_or_default(),
items: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
.collect::<AstResult<'a, Vec<_>>>()?,
},
_ => unimplemented!("{rule:#?}"),