Skip to content

Commit ef1d543

Browse files
committed
Pass more information in user defined parse error
1 parent 22c0b08 commit ef1d543

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

compiler/src/error.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ impl fmt::Display for CompileError {
5656
}?;
5757

5858
// Print line number:
59-
write!(f, " at line {:?}", self.location.row())
59+
match &self.error {
60+
CompileErrorType::Parse(..) => Ok(()),
61+
_ => write!(f, " at line {:?}", self.location.row()),
62+
}
6063
}
6164
}
6265

parser/src/error.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum ParseError {
2424
/// Parser encountered an unexpected token
2525
UnrecognizedToken(TokSpan, Vec<String>),
2626
/// Maps to `User` type from `lalrpop-util`
27-
Other,
27+
Other(LexicalError),
2828
}
2929

3030
/// Convert `lalrpop_util::ParseError` to our internal type
@@ -34,8 +34,7 @@ impl From<InnerError<Location, Tok, LexicalError>> for ParseError {
3434
// TODO: Are there cases where this isn't an EOF?
3535
InnerError::InvalidToken { location } => ParseError::EOF(Some(location)),
3636
InnerError::ExtraToken { token } => ParseError::ExtraToken(token),
37-
// Inner field is a unit-like enum `LexicalError::StringError` with no useful info
38-
InnerError::User { .. } => ParseError::Other,
37+
InnerError::User { error } => ParseError::Other(error),
3938
InnerError::UnrecognizedToken { token, expected } => {
4039
match token {
4140
Some(tok) => ParseError::UnrecognizedToken(tok, expected),
@@ -66,8 +65,7 @@ impl fmt::Display for ParseError {
6665
ParseError::UnrecognizedToken(ref t_span, _) => {
6766
write!(f, "Got unexpected token: {:?} at {:?}", t_span.1, t_span.0)
6867
}
69-
// This is user defined, it probably means a more useful error should have been given upstream.
70-
ParseError::Other => write!(f, "Got unsupported token(s)"),
68+
ParseError::Other(ref error) => write!(f, "{}", error),
7169
}
7270
}
7371
}

parser/src/lexer.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use num_traits::Num;
1010
use serde::{Deserialize, Serialize};
1111
use std::cmp::Ordering;
1212
use std::collections::HashMap;
13+
use std::fmt;
1314
use std::str::FromStr;
1415
use unic_emoji_char::is_emoji_presentation;
1516
use unicode_xid::UnicodeXID;
@@ -59,13 +60,13 @@ pub struct Lexer<T: Iterator<Item = char>> {
5960
keywords: HashMap<String, Tok>,
6061
}
6162

62-
#[derive(Debug)]
63+
#[derive(Debug, PartialEq)]
6364
pub struct LexicalError {
6465
pub error: LexicalErrorType,
6566
pub location: Location,
6667
}
6768

68-
#[derive(Debug)]
69+
#[derive(Debug, PartialEq)]
6970
pub enum LexicalErrorType {
7071
StringError,
7172
UnicodeError,
@@ -74,6 +75,26 @@ pub enum LexicalErrorType {
7475
OtherError(String),
7576
}
7677

78+
impl fmt::Display for LexicalError {
79+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80+
match self.error {
81+
LexicalErrorType::StringError => write!(f, "Got unexpected string"),
82+
LexicalErrorType::UnicodeError => write!(f, "Got unexpected unicode"),
83+
LexicalErrorType::NestingError => write!(f, "Got unexpected nesting"),
84+
LexicalErrorType::UnrecognizedToken { tok } => {
85+
write!(f, "Got unexpected token {}", tok)
86+
}
87+
LexicalErrorType::OtherError(ref msg) => write!(f, "{}", msg),
88+
}?;
89+
write!(
90+
f,
91+
" at line {} column {}",
92+
self.location.row(),
93+
self.location.column()
94+
)
95+
}
96+
}
97+
7798
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
7899
pub struct Location {
79100
row: usize,

0 commit comments

Comments
 (0)