-
-
Notifications
You must be signed in to change notification settings - Fork 686
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
Add Information About Found Token in UnexpectedToken Error Type #3299
Add Information About Found Token in UnexpectedToken Error Type #3299
Conversation
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.
Lovely! Thank you! I've left some small notes inline
compiler-core/src/parse.rs
Outdated
else { | ||
format!("{base}") | ||
} | ||
} |
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.
This is error presentation logic so it needs to live in the warning printer, and the data in the warning needs to be designed data structures rather than strings. Perhaps it could contain the token itself.
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.
@lpil Updated to include an UnexpectedTokenInfo
that handles this. Thanks for the feedback. This makes the original PR desc outdated... lmk if I should update this too. (Updated the description of the PR accordingly too).
@@ -8,6 +8,6 @@ error: Syntax error | |||
3 │ let <<b1, pub>> = <<24, 3>> | |||
│ ^^^ I was not expecting this | |||
|
|||
Expected one of: | |||
Found "pub" (a keyword), expected one of: |
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.
Found "pub" (a keyword), expected one of: | |
Found the keyword `pub`, expected one of: |
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.
@lpil Thanks for the suggestion. Backticks are definitely better on the eyes here.
Should this be the case for non-keywords as well, or shall we keep double quotes? I ask because currently the expected
token that gets printed is also wrapped in double quotes.
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.
@lpil Pushed some updates per your feedback and kept the behavior of non-keywords the same. Lmk if that works... thanks!
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.
Very nice! Could we have a test for an unexpected string and an unexpected int too please 🙏
compiler-core/src/parse/error.rs
Outdated
write!(f, "\"{}\"", base) | ||
} | ||
} | ||
} |
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.
Do not implement this using traits please. We avoid using traits, and this specifically is a misuse of Display as this isn't a general purpose to-string impl, it's specific to that one function.
@@ -8,6 +8,6 @@ error: Syntax error | |||
3 │ add(_name, 1) | |||
│ ^^^^^ I was not expecting this | |||
|
|||
Expected one of: | |||
Found "_name", expected one of: |
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.
Let's say "a name". They can already see what the name is 2 lines above
compiler-core/src/parse.rs
Outdated
// there are still more tokens | ||
let expected = vec!["An import, const, type, or function.".into()]; | ||
return parse_error( | ||
ParseErrorType::UnexpectedToken { | ||
found: UnexpectedTokenInfo { | ||
display: tok.to_string(), |
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.
Use an enum rather than a string here. It could be the token. I would probably put the entire token as found
and not have this struct. The is_reserved_word
can be moved to be a method on Token
@lpil Thanks for all the detailed feedback! Pushed some updates per your comments... Do let me know if anything else appears off |
compiler-core/src/parse.rs
Outdated
@@ -3606,7 +3525,9 @@ pub fn make_call( | |||
if name != "_" { | |||
return parse_error( | |||
ParseErrorType::UnexpectedToken { | |||
found: ParserErrorCause::Descriptor("a name".into()), |
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.
We can get rid of the ParserErrorCause
enum by making a token here!
found: ParserErrorCause::Descriptor("a name".into()), | |
found: Token::Name(name), |
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.
Oh, man! This was so obvious... thanks for pointing that out!
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.
Looking great!!
@@ -8,6 +8,6 @@ error: Syntax error | |||
2 │ const a = <<1, 2, <->> | |||
│ ^^ I was not expecting this | |||
|
|||
Expected one of: | |||
Found "<-", expected one of: |
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.
Where do these double quotes come from? I couldn't find them in the codebase 😅
Could you replace them all with backticks please, to match the keywords.
2 │ fn f(a, "b") -> String { | ||
│ ^^^ I was not expecting this | ||
|
||
Found "b", expected one of: |
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.
For any token with content can you say "a string", "a float", "an int" etc rather than display the content a second time in the error. Thank you
@lpil Pushed up a few more updates per your feedback. Thank you! |
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.
Beautiful work, thank you. Would you mind updating the changelog file also?
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.
Beautiful work, thank you. Would you mind updating the changelog file also?
We don't permit merge commits in this repo. Would you like to rebase to remove the ones from this branch, or should I squash-merge this PR? |
…hether tok is keyword
…ails with backticks
…saging for reserved words
…ding token variants and replace ParseErrorCause enum with Token
8a55084
to
824a324
Compare
…hether tok is keyword
…ails with backticks
…saging for reserved words
…ding token variants and replace ParseErrorCause enum with Token
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.
Wonderful!! Thank you very much!!
I think something may have gone a bit wrong with the rebase, there's multiple copies of each commit in the history, and there's conflicts preventing a rebase into main. I'll squash merge for this one. Thank you |
Addresses #3286.
This commit introduces:
found
member toParserErrorType::UnexpectedToken
of type structUnexpectedTokenInfo
UnexpectedTokenInfo
that specifies whether the unexpected token is a reserved word (i.e., keyword).This commit changes:
details
ofParserErrorType::UnexpectedToken
to read"I was not expecting this. Found {.found}, expected..."
details
ofParserErrorType::UnexpectedToken
read as"I was not expecting this. Found the keyword {.found}, expected..."
(see updated snapshots).Would appreciate any and all feedback.
Thanks!