-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Pass more information in user defined parse error #1106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ pub enum ParseError { | |
| /// Parser encountered an unexpected token | ||
| UnrecognizedToken(TokSpan, Vec<String>), | ||
| /// Maps to `User` type from `lalrpop-util` | ||
| Other, | ||
| Other(LexicalError), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be good to rename this error variant into
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| } | ||
|
|
||
| /// Convert `lalrpop_util::ParseError` to our internal type | ||
|
|
@@ -34,8 +34,7 @@ impl From<InnerError<Location, Tok, LexicalError>> for ParseError { | |
| // TODO: Are there cases where this isn't an EOF? | ||
| InnerError::InvalidToken { location } => ParseError::EOF(Some(location)), | ||
| InnerError::ExtraToken { token } => ParseError::ExtraToken(token), | ||
| // Inner field is a unit-like enum `LexicalError::StringError` with no useful info | ||
| InnerError::User { .. } => ParseError::Other, | ||
| InnerError::User { error } => ParseError::Other(error), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very good to propagate the lexer errors! |
||
| InnerError::UnrecognizedToken { token, expected } => { | ||
| match token { | ||
| Some(tok) => ParseError::UnrecognizedToken(tok, expected), | ||
|
|
@@ -66,8 +65,7 @@ impl fmt::Display for ParseError { | |
| ParseError::UnrecognizedToken(ref t_span, _) => { | ||
| write!(f, "Got unexpected token: {:?} at {:?}", t_span.1, t_span.0) | ||
| } | ||
| // This is user defined, it probably means a more useful error should have been given upstream. | ||
| ParseError::Other => write!(f, "Got unsupported token(s)"), | ||
| ParseError::Other(ref error) => write!(f, "{}", error), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ use num_traits::Num; | |
| use serde::{Deserialize, Serialize}; | ||
| use std::cmp::Ordering; | ||
| use std::collections::HashMap; | ||
| use std::fmt; | ||
| use std::str::FromStr; | ||
| use unic_emoji_char::is_emoji_presentation; | ||
| use unicode_xid::UnicodeXID; | ||
|
|
@@ -59,13 +60,13 @@ pub struct Lexer<T: Iterator<Item = char>> { | |
| keywords: HashMap<String, Tok>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| #[derive(Debug, PartialEq)] | ||
| pub struct LexicalError { | ||
| pub error: LexicalErrorType, | ||
| pub location: Location, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| #[derive(Debug, PartialEq)] | ||
| pub enum LexicalErrorType { | ||
| StringError, | ||
| UnicodeError, | ||
|
|
@@ -74,6 +75,26 @@ pub enum LexicalErrorType { | |
| OtherError(String), | ||
| } | ||
|
|
||
| impl fmt::Display for LexicalError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| match self.error { | ||
| LexicalErrorType::StringError => write!(f, "Got unexpected string"), | ||
| LexicalErrorType::UnicodeError => write!(f, "Got unexpected unicode"), | ||
| LexicalErrorType::NestingError => write!(f, "Got unexpected nesting"), | ||
| LexicalErrorType::UnrecognizedToken { tok } => { | ||
| write!(f, "Got unexpected token {}", tok) | ||
| } | ||
| LexicalErrorType::OtherError(ref msg) => write!(f, "{}", msg), | ||
| }?; | ||
| write!( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to add the line and column info later on, so in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| f, | ||
| " at line {} column {}", | ||
| self.location.row(), | ||
| self.location.column() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] | ||
| pub struct Location { | ||
| row: usize, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. This exception for the parse error is not ideal, but we can improve on this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please implement the todo at this line:
RustPython/compiler/src/error.rs
Line 17 in ef1d543
Then we have proper locations on all compiler errors (where possible).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done