Skip to content

Commit def13a3

Browse files
committed
Fix the handling of do on commands
Introduce keyword_do_block to match parse.y behavior, as well as do a big audit of behavior to line up as closely as possible.
1 parent b548656 commit def13a3

14 files changed

Lines changed: 708 additions & 131 deletions

File tree

config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ tokens:
494494
comment: "def"
495495
- name: KEYWORD_DEFINED
496496
comment: "defined?"
497+
- name: KEYWORD_DO_BLOCK
498+
comment: "do keyword for a block attached to a command"
497499
- name: KEYWORD_DO_LOOP
498500
comment: "do keyword for a predicate in a while, until, or for loop"
499501
- name: KEYWORD_END_UPCASE

include/prism/parser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,13 @@ struct pm_parser {
885885
/** Whether or not we're at the beginning of a command. */
886886
bool command_start;
887887

888+
/**
889+
* Whether or not we're currently parsing the body of an endless method
890+
* definition. In this context, PM_TOKEN_KEYWORD_DO_BLOCK should not be
891+
* consumed by commands (it should bubble up to the outer context).
892+
*/
893+
bool in_endless_def_body;
894+
888895
/** Whether or not we're currently recovering from a syntax error. */
889896
bool recovering;
890897

lib/prism/lex_compat.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def deconstruct_keys(keys) # :nodoc:
134134
KEYWORD_DEF: :on_kw,
135135
KEYWORD_DEFINED: :on_kw,
136136
KEYWORD_DO: :on_kw,
137+
KEYWORD_DO_BLOCK: :on_kw,
137138
KEYWORD_DO_LOOP: :on_kw,
138139
KEYWORD_ELSE: :on_kw,
139140
KEYWORD_ELSIF: :on_kw,

lib/prism/translation/parser/lexer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Lexer # :nodoc:
8787
KEYWORD_DEF: :kDEF,
8888
KEYWORD_DEFINED: :kDEFINED,
8989
KEYWORD_DO: :kDO,
90+
KEYWORD_DO_BLOCK: :kDO_BLOCK,
9091
KEYWORD_DO_LOOP: :kDO_COND,
9192
KEYWORD_END: :kEND,
9293
KEYWORD_END_UPCASE: :klEND,

snapshots/4.0/endless_methods_command_call.txt

Lines changed: 182 additions & 49 deletions
Large diffs are not rendered by default.

snapshots/blocks.txt

Lines changed: 263 additions & 20 deletions
Large diffs are not rendered by default.

snapshots/endless_methods.txt

Lines changed: 126 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
@ ProgramNode (location: (1,0)-(7,15))
1+
@ ProgramNode (location: (1,0)-(11,18))
22
├── flags: ∅
33
├── locals: [:x]
44
└── statements:
5-
@ StatementsNode (location: (1,0)-(7,15))
5+
@ StatementsNode (location: (1,0)-(11,18))
66
├── flags: ∅
7-
└── body: (length: 4)
7+
└── body: (length: 6)
88
├── @ DefNode (location: (1,0)-(1,11))
99
│ ├── flags: newline
1010
│ ├── name: :foo
@@ -116,44 +116,128 @@
116116
│ ├── rparen_loc: ∅
117117
│ ├── equal_loc: (5,11)-(5,12) = "="
118118
│ └── end_keyword_loc: ∅
119-
└── @ LocalVariableWriteNode (location: (7,0)-(7,15))
119+
├── @ LocalVariableWriteNode (location: (7,0)-(7,15))
120+
│ ├── flags: newline
121+
│ ├── name: :x
122+
│ ├── depth: 0
123+
│ ├── name_loc: (7,0)-(7,1) = "x"
124+
│ ├── value:
125+
│ │ @ DefNode (location: (7,4)-(7,15))
126+
│ │ ├── flags: ∅
127+
│ │ ├── name: :f
128+
│ │ ├── name_loc: (7,8)-(7,9) = "f"
129+
│ │ ├── receiver: ∅
130+
│ │ ├── parameters: ∅
131+
│ │ ├── body:
132+
│ │ │ @ StatementsNode (location: (7,12)-(7,15))
133+
│ │ │ ├── flags: ∅
134+
│ │ │ └── body: (length: 1)
135+
│ │ │ └── @ CallNode (location: (7,12)-(7,15))
136+
│ │ │ ├── flags: ignore_visibility
137+
│ │ │ ├── receiver: ∅
138+
│ │ │ ├── call_operator_loc: ∅
139+
│ │ │ ├── name: :p
140+
│ │ │ ├── message_loc: (7,12)-(7,13) = "p"
141+
│ │ │ ├── opening_loc: ∅
142+
│ │ │ ├── arguments:
143+
│ │ │ │ @ ArgumentsNode (location: (7,14)-(7,15))
144+
│ │ │ │ ├── flags: ∅
145+
│ │ │ │ └── arguments: (length: 1)
146+
│ │ │ │ └── @ IntegerNode (location: (7,14)-(7,15))
147+
│ │ │ │ ├── flags: static_literal, decimal
148+
│ │ │ │ └── value: 1
149+
│ │ │ ├── closing_loc: ∅
150+
│ │ │ ├── equal_loc: ∅
151+
│ │ │ └── block: ∅
152+
│ │ ├── locals: []
153+
│ │ ├── def_keyword_loc: (7,4)-(7,7) = "def"
154+
│ │ ├── operator_loc: ∅
155+
│ │ ├── lparen_loc: ∅
156+
│ │ ├── rparen_loc: ∅
157+
│ │ ├── equal_loc: (7,10)-(7,11) = "="
158+
│ │ └── end_keyword_loc: ∅
159+
│ └── operator_loc: (7,2)-(7,3) = "="
160+
├── @ DefNode (location: (9,0)-(9,17))
161+
│ ├── flags: newline
162+
│ ├── name: :foo
163+
│ ├── name_loc: (9,4)-(9,7) = "foo"
164+
│ ├── receiver: ∅
165+
│ ├── parameters: ∅
166+
│ ├── body:
167+
│ │ @ StatementsNode (location: (9,10)-(9,17))
168+
│ │ ├── flags: ∅
169+
│ │ └── body: (length: 1)
170+
│ │ └── @ CallNode (location: (9,10)-(9,17))
171+
│ │ ├── flags: ignore_visibility
172+
│ │ ├── receiver: ∅
173+
│ │ ├── call_operator_loc: ∅
174+
│ │ ├── name: :bar
175+
│ │ ├── message_loc: (9,10)-(9,13) = "bar"
176+
│ │ ├── opening_loc: ∅
177+
│ │ ├── arguments:
178+
│ │ │ @ ArgumentsNode (location: (9,14)-(9,17))
179+
│ │ │ ├── flags: ∅
180+
│ │ │ └── arguments: (length: 1)
181+
│ │ │ └── @ CallNode (location: (9,14)-(9,17))
182+
│ │ │ ├── flags: variable_call, ignore_visibility
183+
│ │ │ ├── receiver: ∅
184+
│ │ │ ├── call_operator_loc: ∅
185+
│ │ │ ├── name: :baz
186+
│ │ │ ├── message_loc: (9,14)-(9,17) = "baz"
187+
│ │ │ ├── opening_loc: ∅
188+
│ │ │ ├── arguments: ∅
189+
│ │ │ ├── closing_loc: ∅
190+
│ │ │ ├── equal_loc: ∅
191+
│ │ │ └── block: ∅
192+
│ │ ├── closing_loc: ∅
193+
│ │ ├── equal_loc: ∅
194+
│ │ └── block: ∅
195+
│ ├── locals: []
196+
│ ├── def_keyword_loc: (9,0)-(9,3) = "def"
197+
│ ├── operator_loc: ∅
198+
│ ├── lparen_loc: ∅
199+
│ ├── rparen_loc: ∅
200+
│ ├── equal_loc: (9,8)-(9,9) = "="
201+
│ └── end_keyword_loc: ∅
202+
└── @ DefNode (location: (11,0)-(11,18))
120203
├── flags: newline
121-
├── name: :x
122-
├── depth: 0
123-
├── name_loc: (7,0)-(7,1) = "x"
124-
├── value:
125-
│ @ DefNode (location: (7,4)-(7,15))
204+
├── name: :foo
205+
├── name_loc: (11,4)-(11,7) = "foo"
206+
├── receiver: ∅
207+
├── parameters: ∅
208+
├── body:
209+
│ @ StatementsNode (location: (11,10)-(11,18))
126210
│ ├── flags: ∅
127-
── name: :f
128-
── name_loc: (7,8)-(7,9) = "f"
129-
│ ├── receiver: ∅
130-
│ ├── parameters: ∅
131-
│ ├── body:
132-
@ StatementsNode (location: (7,12)-(7,15))
133-
├── flags: ∅
134-
── body: (length: 1)
135-
── @ CallNode (location: (7,12)-(7,15))
136-
├── flags: ignore_visibility
137-
├── receiver: ∅
138-
── call_operator_loc: ∅
139-
── name: :p
140-
│ │ ├── message_loc: (7,12)-(7,13) = "p"
141-
│ │ ├── opening_loc: ∅
142-
│ │ ├── arguments:
143-
@ ArgumentsNode (location: (7,14)-(7,15))
144-
├── flags: ∅
145-
── arguments: (length: 1)
146-
── @ IntegerNode (location: (7,14)-(7,15))
147-
│ ├── flags: static_literal, decimal
148-
── value: 1
149-
│ │ ── closing_loc: ∅
150-
├── equal_loc: ∅
151-
── block: ∅
152-
── locals: []
153-
├── def_keyword_loc: (7,4)-(7,7) = "def"
154-
├── operator_loc: ∅
155-
├── lparen_loc: ∅
156-
├── rparen_loc: ∅
157-
├── equal_loc: (7,10)-(7,11) = "="
158-
│ └── end_keyword_loc: ∅
159-
└── operator_loc: (7,2)-(7,3) = "="
211+
── body: (length: 1)
212+
── @ CallNode (location: (11,10)-(11,18))
213+
├── flags: ignore_visibility
214+
├── receiver: ∅
215+
├── call_operator_loc: ∅
216+
├── name: :bar
217+
├── message_loc: (11,10)-(11,13) = "bar"
218+
── opening_loc: (11,13)-(11,14) = "("
219+
── arguments:
220+
@ ArgumentsNode (location: (11,14)-(11,17))
221+
├── flags: ∅
222+
── arguments: (length: 1)
223+
── @ CallNode (location: (11,14)-(11,17))
224+
│ ├── flags: variable_call, ignore_visibility
225+
│ ├── receiver: ∅
226+
│ ├── call_operator_loc: ∅
227+
├── name: :baz
228+
├── message_loc: (11,14)-(11,17) = "baz"
229+
── opening_loc: ∅
230+
── arguments: ∅
231+
│ │ ├── closing_loc: ∅
232+
│ │ ── equal_loc: ∅
233+
── block: ∅
234+
│ ├── closing_loc: (11,17)-(11,18) = ")"
235+
── equal_loc: ∅
236+
── block: ∅
237+
├── locals: []
238+
├── def_keyword_loc: (11,0)-(11,3) = "def"
239+
├── operator_loc: ∅
240+
├── lparen_loc: ∅
241+
├── rparen_loc: ∅
242+
── equal_loc: (11,8)-(11,9) = "="
243+
└── end_keyword_loc: ∅

0 commit comments

Comments
 (0)