Skip to content

Commit 768d6e6

Browse files
committed
parser: add error file
1 parent bc0e98a commit 768d6e6

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

crates/leanVm/src/parser/error.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::parser::Rule;
2+
3+
#[derive(Debug)]
4+
pub enum ParseError {
5+
PestError(Box<pest::error::Error<Rule>>),
6+
SemanticError(String),
7+
}
8+
9+
impl From<pest::error::Error<Rule>> for ParseError {
10+
fn from(error: pest::error::Error<Rule>) -> Self {
11+
Self::PestError(Box::new(error))
12+
}
13+
}
14+
15+
impl std::fmt::Display for ParseError {
16+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17+
match self {
18+
Self::PestError(e) => write!(f, "Parse error: {e}"),
19+
Self::SemanticError(e) => write!(f, "Semantic error: {e}"),
20+
}
21+
}
22+
}
23+
24+
impl std::error::Error for ParseError {}

crates/leanVm/src/parser/mod.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,13 @@ use crate::{
1313
},
1414
};
1515

16+
pub mod error;
17+
pub use error::*;
18+
1619
#[derive(Parser, Debug)]
1720
#[grammar = "grammar.pest"]
1821
pub struct LangParser;
1922

20-
#[derive(Debug)]
21-
pub enum ParseError {
22-
PestError(Box<pest::error::Error<Rule>>),
23-
SemanticError(String),
24-
}
25-
26-
impl From<pest::error::Error<Rule>> for ParseError {
27-
fn from(error: pest::error::Error<Rule>) -> Self {
28-
Self::PestError(Box::new(error))
29-
}
30-
}
31-
32-
impl std::fmt::Display for ParseError {
33-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34-
match self {
35-
Self::PestError(e) => write!(f, "Parse error: {e}"),
36-
Self::SemanticError(e) => write!(f, "Semantic error: {e}"),
37-
}
38-
}
39-
}
40-
41-
impl std::error::Error for ParseError {}
42-
4323
pub fn parse_program(input: &str) -> Result<Program, ParseError> {
4424
let input = remove_comments(input);
4525
let mut pairs = LangParser::parse(Rule::program, &input)?;

0 commit comments

Comments
 (0)