Skip to content

Commit

Permalink
Migrate to miette for error output
Browse files Browse the repository at this point in the history
* Refactored codebase to use Span instead of TokenLocation
  • Loading branch information
timfennis committed Sep 16, 2024
1 parent 6f88c1e commit 6727fb7
Show file tree
Hide file tree
Showing 25 changed files with 990 additions and 746 deletions.
435 changes: 307 additions & 128 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ name = "ndc_lib"
path = "src/lib.rs"

[profile.release]
debug = true # true for flamegraph, false otherwise
debug = true # true for flamegraph, false otherwise
strip = false # false for flamegraph, true otherwise
lto = true

Expand All @@ -34,6 +34,7 @@ ahash = { version = "0.8.11", optional = true }
ordered-float = "4.2.0"
regex = "1.10.4"
self_cell = "1.0.4"
miette = { version = "7.2.0", features = ["fancy"] }

[features]
default = ["repl", "ahash"]
Expand Down
23 changes: 9 additions & 14 deletions src/ast/expression.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ast::operator::{BinaryOperator, LogicalOperator, UnaryOperator};
use crate::ast::parser::Error as ParseError;
use crate::interpreter::evaluate::EvaluationError;
use crate::lexer::Location;
use crate::lexer::Span;
use either::Either;
use num::complex::Complex64;
use num::BigInt;
Expand All @@ -12,8 +12,7 @@ use std::rc::Rc;
#[derive(Eq, PartialEq)]
pub struct ExpressionLocation {
pub expression: Expression,
pub start: Location,
pub end: Location,
pub span: Span,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -150,11 +149,10 @@ impl Eq for Expression {}

impl Expression {
#[must_use]
pub fn to_location(self, start: Location, end: Location) -> ExpressionLocation {
pub fn to_location(self, span: Span) -> ExpressionLocation {
ExpressionLocation {
start,
end,
expression: self,
span,
}
}
}
Expand All @@ -163,8 +161,7 @@ impl ExpressionLocation {
#[must_use]
pub fn to_statement(self) -> Self {
Self {
start: self.start,
end: self.end,
span: self.span,
expression: Expression::Statement(Box::new(self)),
}
}
Expand All @@ -176,8 +173,7 @@ impl ExpressionLocation {
Expression::Identifier(i) => Ok(i.clone()),
_ => Err(EvaluationError::syntax_error(
"expected identifier",
self.start,
self.end,
self.span,
)),
}
}
Expand All @@ -194,8 +190,7 @@ impl ExpressionLocation {
.collect::<Result<Vec<String>, EvaluationError>>(),
_ => Err(EvaluationError::syntax_error(
"expected a parameter list",
self.start,
self.end,
self.span,
)),
}
}
Expand Down Expand Up @@ -258,13 +253,13 @@ impl TryFrom<ExpressionLocation> for Lvalue {
.collect::<Result<Vec<Self>, Self::Error>>()?,
)),
Expression::Grouping(value) => Ok(Lvalue::Sequence(vec![Self::try_from(*value)?])),
expr => Err(ParseError::InvalidLvalue(expr)),
_expr => Err(ParseError::text("invalid l-value".to_string(), value.span)),
}
}
}

impl fmt::Debug for ExpressionLocation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "[{:?} on {}]", self.expression, self.start)
write!(f, "{{{:?} at {:?}}}", self.expression, self.span)
}
}
52 changes: 22 additions & 30 deletions src/ast/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ impl TryFrom<TokenLocation> for UnaryOperator {
Token::Minus => Self::Neg,
Token::Bang | Token::LogicNot => Self::Not,
_ => {
return Err(ParseError::ExpectedToken {
expected_tokens: vec![Token::Minus, Token::Bang],
actual_token: value,
})
// This is essentially an internal error since the parser should check the token before trying to convert it into a UnaryOperator
// maybe in the future it would be better to use From and panic!
return Err(ParseError::text(
format!("Expected '-' or '!' but got {} instead", value.token),
value.span,
));
}
})
}
Expand Down Expand Up @@ -61,10 +63,14 @@ impl TryFrom<TokenLocation> for LogicalOperator {
Token::LogicAnd => Self::And,
Token::LogicOr => Self::Or,
_ => {
return Err(ParseError::ExpectedToken {
actual_token: value,
expected_tokens: vec![Token::LogicAnd, Token::LogicOr],
})
// This is more of an internal parser error than a parser error caused by the user
return Err(ParseError::text(
format!(
"Expected either 'and' or 'or' but got {} instead",
value.token
),
value.span,
));
}
})
}
Expand Down Expand Up @@ -93,28 +99,14 @@ impl TryFrom<TokenLocation> for BinaryOperator {
Token::In => Self::In,
Token::Concat => Self::Concat,
_ => {
return Err(ParseError::ExpectedToken {
actual_token: value,
expected_tokens: vec![
Token::Equality,
Token::Inequality,
Token::Greater,
Token::GreaterEquals,
Token::Less,
Token::LessEquals,
Token::Plus,
Token::Minus,
Token::Multiply,
Token::Divide,
Token::CModulo,
Token::EuclideanModulo,
Token::Caret,
Token::Ampersand,
Token::Pipe,
Token::In,
Token::Concat,
],
})
// NOTE: this is more of an internal error than a user caused error since the parser should check the token prior to converting it.
return Err(ParseError::text(
format!(
"Expected a valid binary operator but got {} instead",
value.token
),
value.span,
));
}
})
}
Expand Down
Loading

0 comments on commit 6727fb7

Please sign in to comment.