Skip to content

Commit

Permalink
Handle single global variable character name
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Oct 3, 2024
1 parent 9f4b24d commit 7a0af49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -9051,6 +9051,10 @@ lex_global_variable(pm_parser_t *parser) {
return PM_TOKEN_GLOBAL_VARIABLE;
}

// True if multiple characters are allowed after the declaration of the
// global variable. Not true when it starts with "$-".
bool allow_multiple = true;

switch (*parser->current.end) {
case '~': // $~: match-data
case '*': // $*: argv
Expand Down Expand Up @@ -9109,14 +9113,15 @@ lex_global_variable(pm_parser_t *parser) {

case '-':
parser->current.end++;
allow_multiple = false;
/* fallthrough */
default: {
size_t width;

if ((width = char_is_identifier(parser, parser->current.end)) > 0) {
do {
parser->current.end += width;
} while (parser->current.end < parser->end && (width = char_is_identifier(parser, parser->current.end)) > 0);
} while (allow_multiple && parser->current.end < parser->end && (width = char_is_identifier(parser, parser->current.end)) > 0);
} else if (pm_char_is_whitespace(peek(parser))) {
// If we get here, then we have a $ followed by whitespace,
// which is not allowed.
Expand Down
9 changes: 8 additions & 1 deletion test/prism/magic_comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ def assert_magic_encoding(expected, line)

# Compare against Ruby's expectation.
if defined?(RubyVM::InstructionSequence)
expected = RubyVM::InstructionSequence.compile(source).eval.encoding
previous = $VERBOSE
expected =
begin
$VERBOSE = nil
RubyVM::InstructionSequence.compile(source).eval.encoding
ensure
$VERBOSE = previous
end
assert_equal expected, actual
end
end
Expand Down

0 comments on commit 7a0af49

Please sign in to comment.