Skip to content

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

Merged
merged 3 commits into from
Jul 6, 2019

Conversation

palaviv
Copy link
Contributor

@palaviv palaviv commented Jul 5, 2019

Before the message was:

Traceback (most recent call last):
SyntaxError: 'Got unsupported token(s) at line 0'

And now it is:

Traceback (most recent call last):
SyntaxError: 'Got unexpected token \t at line 4 column 25'

@palaviv palaviv requested a review from windelbouwman July 5, 2019 08:04
@@ -56,7 +56,10 @@ impl fmt::Display for CompileError {
}?;

// Print line number:
write!(f, " at line {:?}", self.location.row())
match &self.error {
CompileErrorType::Parse(..) => Ok(()),
Copy link
Contributor

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.

Copy link
Contributor

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:

location: Default::default(), // TODO: extract location from parse error!

Then we have proper locations on all compiler errors (where possible).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to rename this error variant into Lexical(LexicalError).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very good to propagate the lexer errors!

}
LexicalErrorType::OtherError(ref msg) => write!(f, "{}", msg),
}?;
write!(
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Display method of the CompilerError struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@windelbouwman
Copy link
Contributor

This change looks very good now. The only thing left is to take a look at the wasm build.

@palaviv
Copy link
Contributor Author

palaviv commented Jul 6, 2019

I am trying to fix the wasm but the error currently look like:

SyntaxError: Error parsing Python code: Got unexpected token: DoubleStar at Location { row: 1, column: 8 } at line 0

Not sure why the fmt::Display I defined is not working.

);
}
}
let _ =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, adding the location attribute clearly was beneficial!

@windelbouwman windelbouwman merged commit 70c2054 into RustPython:master Jul 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants