Skip to content

Commit

Permalink
changed ast error behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jun 25, 2024
1 parent ede5909 commit 6a35256
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use notify::event::ModifyKind;
use notify::EventKind::Modify;
use notify::{Event, RecursiveMode, Watcher};

use crate::compiler::ast::AstParserError;
use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext};
use crate::compiler::ast::Error;

#[derive(Parser)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -50,7 +50,7 @@ pub(crate) enum CliError {
CantStartWatcher(notify::Error),
CantCrateOutputFile(std::io::Error),
CantReadInputFile(std::io::Error),
AstParsingFailed(AstParserError),
AstParsingFailed(Error),
CompilationError(crate::compiler::CompilerError),
}

Expand Down
19 changes: 19 additions & 0 deletions src/compiler/ast/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::fmt::Display;
use crate::compiler::ast::parser::Rule;

#[derive(Debug)]
pub(crate) enum Error {
PestError(pest::error::Error<Rule>),
UnknownRule { rule_name: String },
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::PestError(e) => write!(f, "Parsing error: {}", e),
Error::UnknownRule { rule_name } => write!(f, "Unknown rule: {}", rule_name),
}
}
}

impl std::error::Error for Error {}
4 changes: 3 additions & 1 deletion src/compiler/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod node;
mod parser;
mod error;

pub(crate) use node::{AstNode, AstNodeType};
pub(crate) use parser::{AstParser, AstParserError};
pub(crate) use error::Error;
pub(crate) use parser::AstParser;
21 changes: 8 additions & 13 deletions src/compiler/ast/parser.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use pest::error::Error;
use std::fmt::Display;
use pest::iterators::Pair;
use pest::Parser;
use pest_derive::Parser;

use crate::compiler::ast::{AstNode, AstNodeType};

#[derive(Debug)]
pub(crate) enum AstParserError {
PestError(Error<Rule>),
UnknownRule { rule_name: String },
}
use crate::compiler::ast::error::Error;

#[derive(Parser)]
#[grammar = "grammar.pest"]
Expand All @@ -22,9 +17,9 @@ impl AstParser {
AstParser {}
}

pub(crate) fn parse(&self, source: String) -> Result<AstNode, AstParserError> {
pub(crate) fn parse(&self, source: String) -> Result<AstNode, Error> {
let pairs = AstPestParser::parse(Rule::file, source.as_str())
.map_err(AstParserError::PestError)?;
.map_err(Error::PestError)?;

let children: Result<Vec<AstNode>, _> = pairs
.map(parse_ast_pair)
Expand All @@ -36,16 +31,16 @@ impl AstParser {
}

fn filter_ignored_token(
result: Result<Option<AstNode>, AstParserError>,
) -> Option<Result<AstNode, AstParserError>> {
result: Result<Option<AstNode>, Error>,
) -> Option<Result<AstNode, Error>> {
match result {
Ok(None) => None,
Ok(Some(value)) => Some(Ok(value)),
Err(error) => Some(Err(error)),
}
}

fn parse_ast_pair(p: Pair<Rule>) -> Result<Option<AstNode>, AstParserError> {
fn parse_ast_pair(p: Pair<Rule>) -> Result<Option<AstNode>, Error> {
let node_type = match p.as_rule() {
Rule::atom_utf8 => AstNodeType::AtomUtf8,
Rule::atom_hex => AstNodeType::AtomHex,
Expand All @@ -72,7 +67,7 @@ fn parse_ast_pair(p: Pair<Rule>) -> Result<Option<AstNode>, AstParserError> {

Rule::EOI => return Ok(None),
_ => {
return Err(AstParserError::UnknownRule {
return Err(Error::UnknownRule {
rule_name: format!("{:?}", p.as_rule()),
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::compiler::ast::{AstNode, AstParser, AstParserError};
use crate::compiler::ast::{AstNode, AstParser};
use crate::compiler::cst::{CstFile, CstParser, CstParserError};
use crate::compiler::rst::{HexoFile, RstCompiler, RstCompilerError};
use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext};

#[derive(Debug)]
pub(crate) enum CompilerError {
IO(std::io::Error),
AST(AstParserError),
AST(crate::compiler::ast::Error),
CST(CstParserError),
RST(RstCompilerError),
}
Expand Down

0 comments on commit 6a35256

Please sign in to comment.