Skip to content

Commit

Permalink
Parse to AST
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdean-digicatapult committed Oct 2, 2023
1 parent 2d1d9cf commit f6053e2
Show file tree
Hide file tree
Showing 8 changed files with 804 additions and 203 deletions.
25 changes: 15 additions & 10 deletions tools/lang/src/ast/errors.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
use std::fmt;

use pest::Span;
use crate::{parser::Rule, CompilationStage};

use crate::parser::Rule;
pub(crate) type PestError = pest::error::Error<Rule>;
pub(crate) type ErrorVariant = pest::error::ErrorVariant<Rule>;

pub struct AstBuildError<'a> {
pub(crate) message: String,
pub(crate) span: Span<'a>
pub struct AstBuildError {
pub(crate) stage: CompilationStage,
pub(crate) inner: PestError
}

impl<'a> fmt::Display for AstBuildError<'a> {
impl fmt::Display for AstBuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "An Error Occurred, Please Try Again!") // user-facing output
write!(f, "Error occurred when {}: {}", self.stage, self.inner) // user-facing output
}
}

impl<'a> fmt::Debug for AstBuildError<'a> {
impl fmt::Debug for AstBuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{{ file: {}, line: {} }}", file!(), line!()) // programmer-facing output
}
}

pub fn produce_unexpected_pair_error<R>(pair: pest::iterators::Pair<Rule>) -> Result<R, AstBuildError> {
let rule = pair.as_rule();
let span = pair.as_span();
let pair = pair.as_str().clone();
let message = format!("Unexpected rule {}", pair);
Err(AstBuildError { message, span })
let message = format!("Unexpected rule {:?} ({})", rule, pair);
Err(AstBuildError {
stage: CompilationStage::BuildAst,
inner: PestError::new_from_span(pest::error::ErrorVariant::CustomError { message }, span)
})
}
14 changes: 8 additions & 6 deletions tools/lang/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod errors;
pub mod nodes;
pub mod types;
mod errors;
mod parse;
mod types;

pub use errors::*;
pub use nodes::*;
pub use types::*;
pub(crate) use errors::*;

pub use parse::parse_ast;

pub(crate) use types::*;
159 changes: 0 additions & 159 deletions tools/lang/src/ast/nodes.rs

This file was deleted.

Loading

0 comments on commit f6053e2

Please sign in to comment.