From 6a352567c40b5c8c6c37ab86b934f5908838bcfd Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Tue, 25 Jun 2024 17:37:22 +0700 Subject: [PATCH] changed ast error behaviour --- src/cli.rs | 4 ++-- src/compiler/ast/error.rs | 19 +++++++++++++++++++ src/compiler/ast/mod.rs | 4 +++- src/compiler/ast/parser.rs | 21 ++++++++------------- src/compiler/compiler.rs | 4 ++-- 5 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 src/compiler/ast/error.rs diff --git a/src/cli.rs b/src/cli.rs index 4854560..1aa1f2c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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)] @@ -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), } diff --git a/src/compiler/ast/error.rs b/src/compiler/ast/error.rs new file mode 100644 index 0000000..c4d56b3 --- /dev/null +++ b/src/compiler/ast/error.rs @@ -0,0 +1,19 @@ +use std::fmt::Display; +use crate::compiler::ast::parser::Rule; + +#[derive(Debug)] +pub(crate) enum Error { + PestError(pest::error::Error), + 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 {} diff --git a/src/compiler/ast/mod.rs b/src/compiler/ast/mod.rs index 7dbee1f..faa3547 100644 --- a/src/compiler/ast/mod.rs +++ b/src/compiler/ast/mod.rs @@ -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; diff --git a/src/compiler/ast/parser.rs b/src/compiler/ast/parser.rs index 2337cfd..8774d55 100644 --- a/src/compiler/ast/parser.rs +++ b/src/compiler/ast/parser.rs @@ -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), - UnknownRule { rule_name: String }, -} +use crate::compiler::ast::error::Error; #[derive(Parser)] #[grammar = "grammar.pest"] @@ -22,9 +17,9 @@ impl AstParser { AstParser {} } - pub(crate) fn parse(&self, source: String) -> Result { + pub(crate) fn parse(&self, source: String) -> Result { let pairs = AstPestParser::parse(Rule::file, source.as_str()) - .map_err(AstParserError::PestError)?; + .map_err(Error::PestError)?; let children: Result, _> = pairs .map(parse_ast_pair) @@ -36,8 +31,8 @@ impl AstParser { } fn filter_ignored_token( - result: Result, AstParserError>, -) -> Option> { + result: Result, Error>, +) -> Option> { match result { Ok(None) => None, Ok(Some(value)) => Some(Ok(value)), @@ -45,7 +40,7 @@ fn filter_ignored_token( } } -fn parse_ast_pair(p: Pair) -> Result, AstParserError> { +fn parse_ast_pair(p: Pair) -> Result, Error> { let node_type = match p.as_rule() { Rule::atom_utf8 => AstNodeType::AtomUtf8, Rule::atom_hex => AstNodeType::AtomHex, @@ -72,7 +67,7 @@ fn parse_ast_pair(p: Pair) -> Result, AstParserError> { Rule::EOI => return Ok(None), _ => { - return Err(AstParserError::UnknownRule { + return Err(Error::UnknownRule { rule_name: format!("{:?}", p.as_rule()), }) } diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index 42fd669..2e02fb8 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -1,4 +1,4 @@ -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}; @@ -6,7 +6,7 @@ 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), }