Skip to content

Commit

Permalink
Refactor error output
Browse files Browse the repository at this point in the history
- Debug output now has normal debug _and_ pretty output.
- Alternate debug output just shows the normal alternate debug output.
- Display output no longer shows the pretty output and just shows a
  simple message and reason.
- Alternate display output shows the pretty output.
  • Loading branch information
rossmacarthur committed Oct 21, 2022
1 parent 0043d74 commit 8a2e80f
Show file tree
Hide file tree
Showing 13 changed files with 598 additions and 384 deletions.
8 changes: 4 additions & 4 deletions src/compile/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,23 +378,23 @@ impl<'engine, 'source> Lexer<'engine, 'source> {
}

fn err_unclosed(&self, begin: Span, end: Token) -> Error {
Error::new(
Error::syntax(
format!("unclosed {}", end.pair().human()),
self.source,
begin,
)
}

fn err_unexpected_token(&self, tk: Token, span: impl Into<Span>) -> Error {
Error::new(format!("unexpected {}", tk.human()), self.source, span)
Error::syntax(format!("unexpected {}", tk.human()), self.source, span)
}

fn err_unexpected_character(&self, span: impl Into<Span>) -> Error {
Error::new("unexpected character", self.source, span)
Error::syntax("unexpected character", self.source, span)
}

fn err_undelimited_string(&self, span: impl Into<Span>) -> Error {
Error::new("undelimited string", self.source, span)
Error::syntax("undelimited string", self.source, span)
}
}

Expand Down
29 changes: 15 additions & 14 deletions src/compile/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
// for the `if`.
Block::ElseIf(not, cond) => {
let err =
|| Error::new("unexpected `else if` block", self.source(), span);
|| Error::syntax("unexpected `else if` block", self.source(), span);
match blocks.last_mut().ok_or_else(err)? {
State::If {
has_else: has_else @ false,
Expand Down Expand Up @@ -197,7 +197,8 @@ impl<'engine, 'source> Parser<'engine, 'source> {
// scope stack since an `else` clause starts a new
// scope.
Block::Else => {
let err = || Error::new("unexpected `else` block", self.source(), span);
let err =
|| Error::syntax("unexpected `else` block", self.source(), span);
match blocks.last_mut().ok_or_else(err)? {
State::If {
has_else: has_else @ false,
Expand All @@ -220,7 +221,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
// the way are desugared into an `if` statement.
Block::EndIf => {
let err =
|| Error::new("unexpected `endif` block", self.source(), span);
|| Error::syntax("unexpected `endif` block", self.source(), span);

loop {
match blocks.pop().ok_or_else(err)? {
Expand Down Expand Up @@ -273,7 +274,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
// We expect that the previous block was a `for` block.
Block::EndFor => {
let err =
|| Error::new("unexpected `endfor` block", self.source(), span);
|| Error::syntax("unexpected `endfor` block", self.source(), span);

let for_loop = match blocks.pop().ok_or_else(err)? {
State::For { vars, iterable, .. } => {
Expand Down Expand Up @@ -309,7 +310,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
// We expect that the previous block was a `with` block.
Block::EndWith => {
let err =
|| Error::new("unexpected `endwith` block", self.source(), span);
|| Error::syntax("unexpected `endwith` block", self.source(), span);

let with = match blocks.pop().ok_or_else(err)? {
State::With { expr, name, .. } => {
Expand Down Expand Up @@ -343,7 +344,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
State::For { span, .. } => ("unclosed `for` block", span),
State::With { span, .. } => ("unclosed `with` block", span),
};
return Err(Error::new(msg, self.source(), *span));
return Err(Error::syntax(msg, self.source(), *span));
}

assert!(
Expand Down Expand Up @@ -626,14 +627,14 @@ impl<'engine, 'source> Parser<'engine, 'source> {
.try_fold(0i64, |acc, (j, &d)| {
let x = (d as char).to_digit(radix).ok_or_else(|| {
let m = span.m + i + j;
Error::new(
Error::syntax(
format!("invalid digit for base {} literal", radix),
self.source(),
m..m + 1,
)
})?;
let err = || {
Error::new(
Error::syntax(
format!("base {} literal out of range for 64-bit integer", radix),
self.source(),
span,
Expand All @@ -654,7 +655,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
fn parse_literal_float(&self, raw: &str, span: Span, sign: Sign) -> Result<ast::Literal> {
let float: f64 = raw
.parse()
.map_err(|_| Error::new("invalid float literal", self.source(), span))?;
.map_err(|_| Error::syntax("invalid float literal", self.source(), span))?;
let value = match sign {
Sign::Neg => Value::Float(-float),
Sign::Pos => Value::Float(float),
Expand Down Expand Up @@ -687,7 +688,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
'"' => '"',
_ => {
let j = iter.next().unwrap().0;
return Err(Error::new(
return Err(Error::syntax(
"unknown escape character",
self.source(),
i..j,
Expand All @@ -710,7 +711,7 @@ impl<'engine, 'source> Parser<'engine, 'source> {
fn expect_keyword(&mut self, exp: Keyword) -> Result<Span> {
let (kw, span) = self.parse_keyword()?;
if kw != exp {
return Err(Error::new(
return Err(Error::syntax(
format!(
"expected keyword `{}`, found keyword `{}`",
exp.human(),
Expand Down Expand Up @@ -789,19 +790,19 @@ impl<'engine, 'source> Parser<'engine, 'source> {

fn err_unexpected_eof(&self, exp: impl Display) -> Error {
let n = self.source().len();
Error::new(format!("expected {}, found EOF", exp), self.source(), n..n)
Error::syntax(format!("expected {}, found EOF", exp), self.source(), n..n)
}

fn err_unexpected_token(&self, exp: impl Display, got: Token, span: Span) -> Error {
Error::new(
Error::syntax(
format!("expected {}, found {}", exp, got.human()),
self.source(),
span,
)
}

fn err_unexpected_keyword(&self, kw: impl Display, span: Span) -> Error {
Error::new(format!("unexpected keyword `{}`", kw), self.source(), span)
Error::syntax(format!("unexpected keyword `{}`", kw), self.source(), span)
}
}

Expand Down
Loading

0 comments on commit 8a2e80f

Please sign in to comment.