Skip to content

Commit 16683e2

Browse files
committed
Support bare : as keyword for true
1 parent 7534097 commit 16683e2

6 files changed

Lines changed: 20 additions & 6 deletions

File tree

snapshots/booleans.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
@ ProgramNode (location: (1,0)-(3,4))
1+
@ ProgramNode (location: (1,0)-(5,1))
22
├── flags: ∅
33
├── locals: []
44
└── statements:
5-
@ StatementsNode (location: (1,0)-(3,4))
5+
@ StatementsNode (location: (1,0)-(5,1))
66
├── flags: ∅
7-
└── body: (length: 2)
7+
└── body: (length: 3)
88
├── @ FalseNode (location: (1,0)-(1,5))
99
│ └── flags: newline, static_literal
10-
└── @ TrueNode (location: (3,0)-(3,4))
10+
├── @ TrueNode (location: (3,0)-(3,4))
11+
│ └── flags: newline, static_literal
12+
└── @ TrueNode (location: (5,0)-(5,1))
1113
└── flags: newline, static_literal

src/prism.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6889,7 +6889,7 @@ pm_symbol_node_to_string_node(pm_parser_t *parser, pm_symbol_node_t *node) {
68896889
*/
68906890
static pm_true_node_t *
68916891
pm_true_node_create(pm_parser_t *parser, const pm_token_t *token) {
6892-
assert(token->type == PM_TOKEN_KEYWORD_TRUE);
6892+
assert(token->type == PM_TOKEN_KEYWORD_TRUE || token->type == PM_TOKEN_COLON);
68936893

68946894
return pm_true_node_new(
68956895
parser->arena,
@@ -11009,7 +11009,7 @@ parser_lex(pm_parser_t *parser) {
1100911009
LEX(PM_TOKEN_COLON_COLON);
1101011010
}
1101111011

11012-
if (lex_state_end_p(parser) || pm_char_is_whitespace(peek(parser)) || peek(parser) == '#') {
11012+
if (lex_state_end_p(parser) || pm_char_is_whitespace(peek(parser)) || peek(parser) == '#' || peek(parser) == '\0') {
1101311013
lex_state_set(parser, PM_LEX_STATE_BEG);
1101411014
LEX(PM_TOKEN_COLON);
1101511015
}
@@ -19989,6 +19989,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, u
1998919989
case PM_TOKEN_KEYWORD_SELF:
1999019990
parser_lex(parser);
1999119991
return UP(pm_self_node_create(parser, &parser->previous));
19992+
case PM_TOKEN_COLON:
1999219993
case PM_TOKEN_KEYWORD_TRUE:
1999319994
parser_lex(parser);
1999419995
return UP(pm_true_node_create(parser, &parser->previous));

test/prism/fixtures/booleans.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
false
22

33
true
4+
5+
:

test/prism/ruby/parser_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class ParserTest < TestCase
6868

6969
# https://bugs.ruby-lang.org/issues/21168#note-5
7070
"command_method_call_2.txt",
71+
72+
# Contains standalone `:` keyword (not supported by parser gem).
73+
"booleans.txt",
7174
]
7275

7376
# These files contain code that is being parsed incorrectly by the parser

test/prism/ruby/ripper_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class RipperTest < TestCase
4444
]
4545
end
4646

47+
# CRuby doesn't support standalone `:` as a keyword (equivalent to `true`).
48+
incorrect << "booleans.txt"
49+
4750
# https://bugs.ruby-lang.org/issues/21669
4851
incorrect << "4.1/void_value.txt"
4952
# https://bugs.ruby-lang.org/issues/19107

test/prism/ruby/ruby_parser_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class RubyParserTest < TestCase
8888

8989
# https://bugs.ruby-lang.org/issues/21168#note-5
9090
"command_method_call_2.txt",
91+
92+
# Contains standalone `:` keyword (not supported by ruby_parser).
93+
"booleans.txt",
9194
]
9295

9396
Fixture.each_for_version(version: "3.3", except: failures) do |fixture|

0 commit comments

Comments
 (0)