Skip to content
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

Merged
merged 24 commits into from
Jun 30, 2024

Conversation

rdghosal
Copy link
Contributor

@rdghosal rdghosal commented Jun 20, 2024

Addresses #3286.

This commit introduces:

  • A found member to ParserErrorType::UnexpectedToken of type struct UnexpectedTokenInfo
  • An impl of std::fmt::Display in UnexpectedTokenInfo that specifies whether the unexpected token is a reserved word (i.e., keyword).

This commit changes:

  • The details of ParserErrorType::UnexpectedToken to read "I was not expecting this. Found {.found}, expected..."
  • In the case of a keyword, details of ParserErrorType::UnexpectedToken read as "I was not expecting this. Found the keyword {.found}, expected..." (see updated snapshots).

Would appreciate any and all feedback.
Thanks!

Copy link
Member

@lpil lpil left a 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

else {
format!("{base}")
}
}
Copy link
Member

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.

Copy link
Contributor Author

@rdghosal rdghosal Jun 20, 2024

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
3let <<b1, pub>> = <<24, 3>>
^^^ I was not expecting this

Expected one of:
Found "pub" (a keyword), expected one of:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Found "pub" (a keyword), expected one of:
Found the keyword `pub`, expected one of:

Copy link
Contributor Author

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.

Copy link
Contributor Author

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!

Copy link
Member

@lpil lpil left a 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 🙏

write!(f, "\"{}\"", base)
}
}
}
Copy link
Member

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
3add(_name, 1)
^^^^^ I was not expecting this

Expected one of:
Found "_name", expected one of:
Copy link
Member

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

// 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(),
Copy link
Member

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

@rdghosal
Copy link
Contributor Author

@lpil Thanks for all the detailed feedback! Pushed some updates per your comments... Do let me know if anything else appears off

@@ -3606,7 +3525,9 @@ pub fn make_call(
if name != "_" {
return parse_error(
ParseErrorType::UnexpectedToken {
found: ParserErrorCause::Descriptor("a name".into()),
Copy link
Member

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!

Suggested change
found: ParserErrorCause::Descriptor("a name".into()),
found: Token::Name(name),

Copy link
Contributor Author

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!

Copy link
Member

@lpil lpil left a 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
2const a = <<1, 2, <->>
^^ I was not expecting this

Expected one of:
Found "<-", expected one of:
Copy link
Member

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:
Copy link
Member

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

@rdghosal
Copy link
Contributor Author

@lpil Pushed up a few more updates per your feedback. Thank you!

Copy link
Member

@lpil lpil left a 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?

Copy link
Member

@lpil lpil left a 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?

@lpil
Copy link
Member

lpil commented Jun 30, 2024

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?

@rdghosal rdghosal force-pushed the unclear-unexpected-tok-details branch from 8a55084 to 824a324 Compare June 30, 2024 13:17
Copy link
Member

@lpil lpil left a 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!!

@lpil
Copy link
Member

lpil commented Jun 30, 2024

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

@lpil lpil merged commit a2b12fa into gleam-lang:main Jun 30, 2024
12 checks passed
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.

None yet

2 participants