fix(rust): clarify error message for non-token reserved words#4323
Merged
WillLillis merged 1 commit intotree-sitter:masterfrom Apr 9, 2025
Merged
Conversation
Contributor
Author
|
I’d also be happy to change this error to a warning. I can’t think of any reason it needs to be an error instead of a warning, but maybe I am missing something. |
WillLillis
reviewed
Apr 6, 2025
Improve the `NonTokenReservedWord` error message by including the specific reserved word that was not used as a token.
e549b38 to
d9c4709
Compare
WillLillis
approved these changes
Apr 9, 2025
Member
|
This is a great improvement to the existing error message. For a future improvement, it might be nice to cover the other rule types. I came up with this first pass, but it needs some more testing/cleanup and I don't want that to hold up this change :) ...
let rule = match &reserved_rule {
Rule::String(s) | Rule::Pattern(s, _) | Rule::NamedSymbol(s) => {
format!("'{s}'")
}
Rule::Blank => "`blank`".to_string(),
Rule::Choice(c) => {
if c.len() == 2 && matches!((&c[0], &c[1]), (Rule::Repeat(_), Rule::Blank))
{
"`repeat`".to_string()
} else if c.len() == 2 && matches!(&c[1], Rule::Blank) {
"`optional`".to_string()
} else {
"`choice`".to_string()
}
}
Rule::Repeat(_) => "`repeat1`".to_string(),
Rule::Seq(_) => "`seq`".to_string(),
Rule::Metadata { params, .. } => {
if params.alias.is_some() {
"`alias`".to_string()
} else if params.field_name.is_some() {
"`field`".to_string()
} else if params.associativity.is_some()
|| params.precedence != Precedence::None
|| params.dynamic_precedence != 0
{
"`prec`".to_string()
} else if params.is_token {
"`token`".to_string()
} else {
"<unexpected rule>".to_string() // unreachable?
}
}
// unreachable as well?
Rule::Symbol(_) | Rule::Reserved { .. } => "<unexpected rule>".to_string(),
};
Err(ExtractTokensError::NonTokenReservedWord(rule))?;
... |
|
Successfully created backport PR for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve the
NonTokenReservedWorderror message by including the specific reserved word that was not used as a token. The Updated message also clarifies that reserved words must be defined as tokens appearing in the grammar rules.