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; 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)] #[derive(Debug, Clone)]
pub enum ParseError<'a, T> { pub enum ParseError<'a, T> {
PreAst(pest::error::Error<Rule>), PreAst(pest::error::Error<Rule>),
Ast { Ast(AstError<'a, T>),
}
#[derive(Debug, Clone)]
pub struct AstError<'a, T> {
span: pest::Span<'a>, span: pest::Span<'a>,
error_code: ErrorCode, error_code: ErrorCode,
error_message: String, error_message: String,
recovered: Option<T>, recovered: Option<T>,
},
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -18,37 +21,35 @@ pub enum ErrorCode {
InvalidStatement = 200, 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> { impl<T> From<pest::error::Error<Rule>> for ParseError<'_, T> {
fn from(value: pest::error::Error<Rule>) -> Self { fn from(value: pest::error::Error<Rule>) -> Self {
Self::PreAst(value) 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> { 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> { impl<'a, F> GetParseError<'a, F> for AstResult<'a, F> {
fn get<T>(self) -> ParseResult<'a, F, T> { fn get<T>(self) -> AstResult<'a, F, T> {
match self { match self {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(e) => Err(e.get()), 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>> { impl<'a, F> GetParseError<'a, Option<F>> for Result<Option<F>, AstError<'a, F>> {
fn get<T>(self) -> ParseResult<'a, Option<F>, T> { fn get<T>(self) -> AstResult<'a, Option<F>, T> {
match self { match self {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(e) => Err(e.get()), 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 ast::*;
use crate::error::{GetParseError, ParseResult}; use crate::error::{GetParseError, ParseError};
#[derive(Parser)] #[derive(Parser)]
#[grammar = "./src/grammar.pest"] #[grammar = "./src/grammar.pest"]
pub struct MistParser; 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 pairs = MistParser::parse(Rule::program, source)?;
let mut statements = vec![]; let mut statements = vec![];
+5 -5
View File
@@ -1,12 +1,12 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{GetParseError, ParseError}, error::{AstError, GetParseError},
parser::listen_rule, parser::listen_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
+8 -8
View File
@@ -1,11 +1,11 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -19,7 +19,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
p.into_inner() p.into_inner()
.into_iter() .into_iter()
.map(Prefix::try_from) .map(Prefix::try_from)
.collect::<ParseResult<'a, Vec<_>, _>>() .collect::<AstResult<'a, Vec<_>, _>>()
}) })
.transpose()? .transpose()?
.unwrap_or_default(); .unwrap_or_default();
@@ -32,7 +32,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
prefixes, prefixes,
postfixes: inner postfixes: inner
.map(|p| Postfix::try_from(p)) .map(|p| Postfix::try_from(p))
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
}) })
} else { } else {
Ok(exp) 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(match pair.as_rule() { 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); 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::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, 'a,
Vec<_>, Vec<_>,
_, _,
@@ -89,7 +89,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Expression::try_from(pi.next().unwrap())?, 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())?), Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap())?),
+12 -12
View File
@@ -6,12 +6,12 @@ pub mod types;
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
parser::consume_rule, parser::consume_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Identifier { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(Identifier(pair.as_str().to_string())) 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::static_path => Ok(Path( Rule::static_path => Ok(Path(
pair.into_inner() pair.into_inner()
.map(Identifier::try_from) .map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
)), )),
_ => unimplemented!("{pair:#?}"), _ => 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -50,7 +50,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
Rule::tuple => Literal::Tuple( Rule::tuple => Literal::Tuple(
inner inner
.map(Expression::try_from) .map(Expression::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
), ),
_ => unimplemented!("{rule:#?}"), _ => 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -68,21 +68,21 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
Rule::tuple_pattern => Pattern::Tuple( Rule::tuple_pattern => Pattern::Tuple(
inner inner
.map(Identifier::try_from) .map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
), ),
Rule::named_tuple_pattern => Pattern::NamedTuple( Rule::named_tuple_pattern => Pattern::NamedTuple(
Path::try_from(inner.next().unwrap())?, Path::try_from(inner.next().unwrap())?,
inner inner
.map(Identifier::try_from) .map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
), ),
Rule::struct_pattern => Pattern::Struct( Rule::struct_pattern => Pattern::Struct(
Path::try_from(inner.next().unwrap())?, Path::try_from(inner.next().unwrap())?,
inner inner
.map(Identifier::try_from) .map(Identifier::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
), ),
Rule::literal => Pattern::Literal(Literal::try_from(pair)?), 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 { 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> { fn try_from(pairs: &mut pest::iterators::Pairs<'a, Rule>) -> Result<Self, Self::Error> {
Ok(consume_rule(pairs, Rule::visibility) 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() { if let Some(path) = pair.into_inner().next() {
Ok(Visibility::PublicTarget(Path::try_from(path)?)) Ok(Visibility::PublicTarget(Path::try_from(path)?))
} else { } else {
+8 -8
View File
@@ -1,12 +1,12 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ErrorCode, ParseError, ParseResult}, error::{ErrorCode, AstError, AstResult},
parser::listen_rule, parser::listen_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let statements = pair let statements = pair
@@ -18,14 +18,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
vec![Statement::try_from(pair)] vec![Statement::try_from(pair)]
} }
}) })
.collect::<ParseResult<'a, Vec<_>>>()?; .collect::<AstResult<'a, Vec<_>>>()?;
Ok(Block(statements)) Ok(Block(statements))
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -58,7 +58,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
.unwrap() .unwrap()
.into_inner() .into_inner()
.map(StatementBranch::try_from) .map(StatementBranch::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
else_branch: inner else_branch: inner
.next() .next()
.map(Statement::try_from) .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())?, Block::try_from(match_inner.next().unwrap())?,
)) ))
}) })
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
), ),
Rule::unexpected_statement => { Rule::unexpected_statement => {
return Err(ParseError::Ast { return Err(AstError::Ast {
span: pair.as_span(), span: pair.as_span(),
error_code: ErrorCode::InvalidStatement, error_code: ErrorCode::InvalidStatement,
error_message: "Invalid Statement".to_string(), 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
+11 -11
View File
@@ -1,12 +1,12 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); 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( Rule::tuple_type => Ok(TypeExprKind::Tuple(
inner inner
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.collect::<ParseResult<'a, _>>()?, .collect::<AstResult<'a, _>>()?,
)), )),
Rule::path_type => { Rule::path_type => {
let path = Path::try_from(inner.next().unwrap())?; let path = Path::try_from(inner.next().unwrap())?;
let params = inner let params = inner
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?; .collect::<AstResult<'a, Vec<_>>>()?;
if params.len() == 0 { if params.len() == 0 {
Ok(TypeExprKind::Path(path)) 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); 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())?, TypeExprKind::try_from(inner.next().unwrap())?,
inner inner
.map(TypePostfix::try_from) .map(TypePostfix::try_from)
.collect::<ParseResult<'a, _>>()?, .collect::<AstResult<'a, _>>()?,
)), )),
Rule::type_expr_param => Self::try_from(inner.next().unwrap()), Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
Rule::lifetime => Ok(TypeExpr( 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -102,7 +102,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
match rule { match rule {
Rule::generics => Ok(Generics( Rule::generics => Ok(Generics(
inner inner
.map(|pair| -> ParseResult<'a, Generic> { .map(|pair| -> AstResult<'a, Generic> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
Ok( Ok(
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) { 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())?, Identifier::try_from(inner.next().unwrap())?,
inner inner
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
) )
}, },
) )
}) })
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
)), )),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
+3 -3
View File
@@ -1,11 +1,11 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
@@ -41,7 +41,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
let items = next let items = next
.into_inner() .into_inner()
.map(Attribute::try_from) .map(Attribute::try_from)
.collect::<ParseResult<'a, Vec<_>, _>>()?; .collect::<AstResult<'a, Vec<_>, _>>()?;
Ok(Attribute::List { path, items }) 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
+4 -4
View File
@@ -1,11 +1,11 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -23,7 +23,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
.unwrap() .unwrap()
.into_inner() .into_inner()
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
)), )),
Rule::enum_struct => Ok(EnumItem::Struct( Rule::enum_struct => Ok(EnumItem::Struct(
@@ -33,7 +33,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
.map(|pair| { .map(|pair| {
pair.into_inner() pair.into_inner()
.map(FieldDecl::try_from) .map(FieldDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>() .collect::<AstResult<'a, Vec<_>>>()
}) })
.transpose()? .transpose()?
.unwrap_or_default(), .unwrap_or_default(),
+5 -5
View File
@@ -1,12 +1,12 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); 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) let params = consume_rule(&mut inner, Rule::param_list)
.map({ .map({
let self_param = self_param.clone(); let self_param = self_param.clone();
|params_pair| -> ParseResult<'a, ParamList> { |params_pair| -> AstResult<'a, ParamList> {
let mut params = ParamList::try_from(params_pair)?; let mut params = ParamList::try_from(params_pair)?;
if let Some(x) = self_param { if let Some(x) = self_param {
params.0.insert(0, x); 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(ParamList( Ok(ParamList(
pair.into_inner() pair.into_inner()
.map(VarDecl::try_from) .map(VarDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
)) ))
} }
} }
+4 -4
View File
@@ -1,12 +1,12 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
parser::consume_rule, parser::consume_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); 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())?, target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner methods: inner
.map(FunctionDecl::try_from) .map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
}), }),
Rule::impl_decl => Ok(ImplDecl { 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())?, target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner methods: inner
.map(FunctionDecl::try_from) .map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
}), }),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
+10 -10
View File
@@ -7,12 +7,12 @@ pub mod impl_decl;
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{ParseError, ParseResult}, error::{AstError, AstResult},
parser::consume_rule, parser::consume_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -22,7 +22,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
.unwrap() .unwrap()
.into_inner() .into_inner()
.map(Attribute::try_from) .map(Attribute::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?; .collect::<AstResult<'a, Vec<_>>>()?;
Ok(TopLevel( Ok(TopLevel(
inner 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 { 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> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -61,7 +61,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.map(|pair| { .map(|pair| {
pair.into_inner() pair.into_inner()
.map(FieldDecl::try_from) .map(FieldDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>() .collect::<AstResult<'a, Vec<_>>>()
}) })
.transpose()? .transpose()?
.unwrap_or_default(), .unwrap_or_default(),
@@ -79,12 +79,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.unwrap() .unwrap()
.into_inner() .into_inner()
.map(FieldDeclStmt::try_from) .map(FieldDeclStmt::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
constructor: ClassConstructor::try_from(inner.next().unwrap())?, constructor: ClassConstructor::try_from(inner.next().unwrap())?,
items: inner items: inner
.into_iter() .into_iter()
.map(ClassItem::try_from) .map(ClassItem::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
}, },
Rule::enum_decl => TopLevelKind::EnumDecl { Rule::enum_decl => TopLevelKind::EnumDecl {
@@ -96,7 +96,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.unwrap_or_default(), .unwrap_or_default(),
fields: inner fields: inner
.map(EnumItem::try_from) .map(EnumItem::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
}, },
Rule::mod_package => TopLevelKind::Mod( Rule::mod_package => TopLevelKind::Mod(
@@ -119,13 +119,13 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.map(|pair| { .map(|pair| {
pair.into_inner() pair.into_inner()
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>() .collect::<AstResult<'a, Vec<_>>>()
}) })
.transpose()? .transpose()?
.unwrap_or_default(), .unwrap_or_default(),
items: inner items: inner
.map(FunctionDecl::try_from) .map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<AstResult<'a, Vec<_>>>()?,
}, },
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),