summaryrefslogtreecommitdiff
diff options
authorKevin Newton <[email protected]>2024-04-01 15:03:41 -0400
committergit <[email protected]>2024-04-01 19:28:05 +0000
commitd6c1cc5532e5d31e5f5abe171fd759e3f4a099f7 (patch)
treeeb637fdc20d5fc2d6cd53b021623bc35e1156913
parentd898f00fe16f598474aeea06d6000ac10740236a (diff)
[ruby/prism] Fix up error messages for empty global variable
https://github.com/ruby/prism/commit/fa7559d40b
-rw-r--r--prism/config.yml1
-rw-r--r--prism/prism.c10
-rw-r--r--prism/templates/src/diagnostic.c.erb1
3 files changed, 7 insertions, 5 deletions
diff --git a/prism/config.yml b/prism/config.yml
index 8b9bb911e5..c813b02d9f 100644
--- a/prism/config.yml
+++ b/prism/config.yml
@@ -109,6 +109,7 @@ errors:
- FOR_IN
- FOR_INDEX
- FOR_TERM
+ - GLOBAL_VARIABLE_BARE
- HASH_EXPRESSION_AFTER_LABEL
- HASH_KEY
- HASH_ROCKET
diff --git a/prism/prism.c b/prism/prism.c
index abd1914f4e..6631ef71ae 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -7994,8 +7994,7 @@ lex_numeric(pm_parser_t *parser) {
static pm_token_type_t
lex_global_variable(pm_parser_t *parser) {
if (parser->current.end >= parser->end) {
- pm_diagnostic_id_t diag_id = parser->version == PM_OPTIONS_VERSION_CRUBY_3_3_0 ? PM_ERR_INVALID_VARIABLE_GLOBAL_3_3_0 : PM_ERR_INVALID_VARIABLE_GLOBAL;
- PM_PARSER_ERR_TOKEN_FORMAT_CONTENT(parser, parser->current, diag_id);
+ pm_parser_err_token(parser, &parser->current, PM_ERR_GLOBAL_VARIABLE_BARE);
return PM_TOKEN_GLOBAL_VARIABLE;
}
@@ -8066,10 +8065,11 @@ lex_global_variable(pm_parser_t *parser) {
parser->current.end += width;
} while (parser->current.end < parser->end && (width = char_is_identifier(parser, parser->current.end)) > 0);
} else {
- // If we get here, then we have a $ followed by something that isn't
- // recognized as a global variable.
+ // If we get here, then we have a $ followed by something that
+ // isn't recognized as a global variable.
pm_diagnostic_id_t diag_id = parser->version == PM_OPTIONS_VERSION_CRUBY_3_3_0 ? PM_ERR_INVALID_VARIABLE_GLOBAL_3_3_0 : PM_ERR_INVALID_VARIABLE_GLOBAL;
- PM_PARSER_ERR_TOKEN_FORMAT_CONTENT(parser, parser->current, diag_id);
+ size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
+ PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, diag_id, (int) ((parser->current.end + width) - parser->current.start), (const char *) parser->current.start);
}
return PM_TOKEN_GLOBAL_VARIABLE;
diff --git a/prism/templates/src/diagnostic.c.erb b/prism/templates/src/diagnostic.c.erb
index b91bd973d5..3d5073147a 100644
--- a/prism/templates/src/diagnostic.c.erb
+++ b/prism/templates/src/diagnostic.c.erb
@@ -193,6 +193,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_ERR_FOR_INDEX] = { "expected an index after `for`", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_FOR_IN] = { "expected an `in` after the index in a `for` statement", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_FOR_TERM] = { "expected an `end` to close the `for` loop", PM_ERROR_LEVEL_SYNTAX },
+ [PM_ERR_GLOBAL_VARIABLE_BARE] = { "'$' without identifiers is not allowed as a global variable name", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_HASH_EXPRESSION_AFTER_LABEL] = { "expected an expression after the label in a hash", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_HASH_KEY] = { "unexpected %s, expecting '}' or a key in the hash literal", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_HASH_ROCKET] = { "expected a `=>` between the hash key and value", PM_ERROR_LEVEL_SYNTAX },