Repository navigation
Expand file tree
/
Copy patharg_position_command_test.go
More file actions
570 lines (567 loc) · 16.2 KB
/
Copy patharg_position_command_test.go
File metadata and controls
570 lines (567 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package parser
import (
"strings"
"testing"
)
// A PAREN-LESS COMMAND CALL — MRI's `command` (parse.y v3_4_0 3464-3536) — is
// legal only where the grammar reaches `command_call`. It reaches it from
// `stmt`/`expr` (3116, 3334, 3437), from `compstmt`, which every statement body
// and every parenthesised group is (3070), from `expr_value`, which the
// if/unless/while/until/for/case headers take, and from `call_args: command`
// (4223), which `paren_args` (4171) and an index bracket
// (`primary_value '[' opt_call_args rbracket`) go through.
//
// It does NOT reach it through `arg_value: arg` (4133). An array LITERAL's
// contents are `aref_args: args` → `args: arg_value` (4427, 4140, 4314); a hash
// entry is `assoc: arg_value tASSOC arg_value | tLABEL arg_value | tDSTAR
// arg_value` (6789); a splat is `arg_splat: tSTAR arg_value`; a block-pass is
// `block_arg: tAMPER arg_value`; every argument AFTER the first comes through
// `args ',' arg_value`; a `when` candidate, a `rescue` class, a parameter
// default, an operator operand and a ternary arm are all `arg` too. `arg` has no
// `command` alternative, so a paren-less command call is a SyntaxError in every
// one of those places — which is why `[foo bar]` and `{k: foo bar}` are
// SyntaxErrors while `(foo bar)`, `f(foo bar)` and `a[foo bar]` are legal.
//
// Every source in both tables was judged on ruby 4.0.5 by BOTH its parsers,
// `ruby -c` and `ruby --parser=parse.y -c`, which agree on all of them. The one
// shape where they do NOT agree is `a[foo bar do end]` — prism accepts it, parse.y
// refuses it — so it is asserted in neither table; this parser accepts it, which
// is the prism answer.
//
// This is the one direction in which a parser change can break real code, so the
// second table is the load-bearing one: it is the set of shapes MRI admits that
// must keep parsing.
func TestParenLessCommandCallRefusedInAnArgPosition(t *testing.T) {
for _, src := range []string{
`+foo bar`,
`-foo bar`,
`1 + foo bar`,
`1 .. foo bar`,
`[!foo bar]`,
`[$x = foo bar]`,
`[*a, foo bar]`,
`[*foo bar]`,
`[1, foo bar]`,
`[@x = foo bar]`,
`[A = foo bar]`,
`[Foo 1]`,
`[Foo :a]`,
`[Foo.bar baz]`,
`[[1][0] baz]`,
`[[foo bar]]`,
`[a += foo bar]`,
`[a = foo bar]`,
`[a, *foo bar]`,
`[a, foo bar do end]`,
`[a.b = foo bar]`,
`[a.b.c d]`,
`[a::B = foo bar]`,
`[a[0] = foo bar]`,
`[foo 1]`,
`[foo :sym]`,
`[foo bar and baz]`,
`[foo bar do end]`,
`[foo bar rescue 1]`,
`[foo bar { }]`,
`[foo bar, 1]`,
`[foo bar, baz qux]`,
`[foo bar]`,
`[foo"bar"]`,
`[foo(bar) baz]`,
`[foo.bar baz]`,
`[foo.y"z"]`,
`[not foo bar]`,
`[obj::Const 1]`,
`[obj::Meth 1]`,
`[super 1]`,
`[yield 1]`,
`a ? 1 : foo bar`,
`a ? foo bar : 1`,
`a[1, foo bar]`,
`case x; when foo bar then 1; end`,
`def m(a = 1, b = foo bar); end`,
`def m(a = foo bar); end`,
`def m(a: foo bar); end`,
`def m; [yield 1]; end`,
`def m; return 1, foo bar; end`,
`defined? foo bar`,
`f(*foo bar)`,
`f(1, foo bar)`,
`f(a, *foo bar)`,
`f(a, b, foo bar)`,
`f(k: foo bar)`,
`foo = 1; [foo "x"]`,
`foo a, bar baz`,
`foo(&bar baz)`,
`foo(**bar baz)`,
`p [foo bar]`,
`p({k: foo bar})`,
`until [foo bar]; end`,
`while [foo bar]; end`,
`while begin; y do end; end; end`,
`while true; break 1, foo bar; end`,
`while x = y do end; end`,
`while y do end; end`,
`x = *foo bar`,
`x = 1, foo bar`,
`x = [foo bar do end]`,
`x = [foo bar]`,
`x, y = *foo bar`,
`x, y = 1, foo bar`,
`{**foo bar}`,
`{1 => foo bar}`,
`{foo bar => 1}`,
`{k: a = foo bar}`,
`{k: foo bar rescue 1}`,
`{k: foo bar}`,
`{k: foo.bar baz}`,
`~foo bar`,
// --- v0.6.1 additions. Three further productions that have no
// `command`/`modifier_rescue`/`keyword_not` alternative, plus the clause
// terminator that makes the refusal observable. Every shape judged on ruby
// 4.0.5 by BOTH `ruby -c` and `ruby --parser=parse.y -c`, which agree on all
// of them.
//
// `arg: '!' arg` (4009) vs `expr: '!' command_call` (3350): the operand of
// `!` may be a paren-less command ONLY at the `expr` level, and a
// `command_call` is not itself a unary expression — hence `!foo bar` legal,
// `!!foo bar` not.
//
// `arg` has no `modifier_rescue` arm — only `stmt` (3184) and the assignment
// right-hand sides `arg_rhs` (4162) / `command_rhs` / `endless_arg` do — and
// `expr` has none either, which covers the `expr_value` headers.
//
// `arg` has no `keyword_not` arm: `not` is `expr: keyword_not '\n'? expr`
// (3345) only.
//
// And `then`/`do` — `term | KEYWORD | term KEYWORD` — is REQUIRED by every
// clause head (`opt_rescue` 5926, `case_body` 5397, `k_if`/`k_unless`,
// `expr_value_do`, `p_case_body`). Without it the stray token of an
// `arg_value` already refused upstream was read as the clause's first
// statement and the clause parsed anyway.
"begin\nx\nrescue foo bar\ny\nend",
"case x\nwhen foo bar\n1\nend",
`!! foo bar`,
`!!!!foo bar`,
`!!!foo bar`,
`!!Foo 1`,
`!![foo bar]`,
`!!a.b foo bar`,
`!!defined? foo bar`,
`!!foo bar, baz`,
`!!foo bar`,
`!!foo.bar baz`,
`!!not foo bar`,
`!!super 1`,
`!!x = foo bar`,
`!-foo bar`,
`!a = foo bar`,
`!~foo bar`,
`+!foo bar`,
`-!foo bar`,
`->() { x; rescue foo bar }`,
`-not x`,
`1 + [x rescue nil]`,
`1 + not x`,
`Foo(x rescue nil)`,
`[!!foo bar]`,
`[*not x]`,
`[*x rescue nil]`,
`[1, 2 rescue nil]`,
`[1, not x]`,
`[1, x rescue nil]`,
`[1].each do; x; rescue foo bar; end`,
`[1].each { x; rescue foo bar; }`,
`[[x rescue nil]]`,
`[not x]`,
`[x rescue nil]`,
`[{k: x rescue nil}]`,
`a = !!foo bar`,
`a ? not x : 1`,
`a ? x rescue nil : 1`,
`a&.b(x rescue nil)`,
`a.b(1, x rescue nil)`,
`a.b(x rescue nil)`,
`a::b(x rescue nil)`,
`a[1, not x]`,
`a[1, x rescue nil]`,
`a[x rescue nil]`,
`begin; x; rescue !foo bar; y; end`,
`begin; x; rescue *foo bar; y; end`,
`begin; x; rescue Foo => e end`,
`begin; x; rescue Foo bar; y; end`,
`begin; x; rescue Foo end`,
`begin; x; rescue Foo if c; y; end`,
`begin; x; rescue Foo rescue Bar; y; end`,
`begin; x; rescue Foo rescue nil; y; end`,
`begin; x; rescue Foo, Bar end`,
`begin; x; rescue Foo, bar baz => e; y; end`,
`begin; x; rescue Foo, bar baz; y; end`,
`begin; x; rescue a = foo bar; y; end`,
`begin; x; rescue end`,
`begin; x; rescue foo bar => e; y; end`,
`begin; x; rescue foo bar baz qux; y; end`,
`begin; x; rescue foo bar then y; end`,
`begin; x; rescue foo bar; y; end`,
`begin; y; rescue not x; z; end`,
`case x rescue nil; when 1; end`,
`case x; in 1 end`,
`case x; when 1 end`,
`case x; when 1, 2 end`,
`case x; when Foo, bar baz; 1; end`,
`case x; when foo bar; 1; end`,
`case x; when foo bar; end`,
`case x; when y rescue nil then 1; end`,
`case x; when y rescue nil; end`,
`case y; when not x; end`,
`class C; x; rescue foo bar; end`,
`def m(*a rescue nil); end`,
`def m(a = not x); end`,
`def m(a = x rescue nil); end`,
`def m(a: not x); end`,
`def m(a: x rescue nil); end`,
`def m; super(x rescue nil); end`,
`def m; x; rescue *foo bar; end`,
`def m; x; rescue Foo, bar baz; end`,
`def m; x; rescue foo bar; end`,
`def m; yield(x rescue nil); end`,
`elsif_check = 1; if a; elsif b end`,
`f(!x rescue nil)`,
`f(¬ x)`,
`f(&x rescue nil)`,
`f(**not x)`,
`f(**x rescue nil)`,
`f(*a, x rescue nil)`,
`f(*x rescue nil)`,
`f(-x rescue nil)`,
`f(1, 2 rescue nil)`,
`f(1, not x)`,
`f(1, x rescue nil)`,
`f(a: 1, b: x rescue nil)`,
`f(defined?(x) rescue nil)`,
`f(k: x rescue nil)`,
`f(x + 1 rescue nil)`,
`f(x ? 1 : 2 rescue nil)`,
`f(x rescue nil)`,
`f(x rescue nil, 1)`,
`f(x, y rescue nil)`,
`f(y, x rescue nil, 1)`,
`foo(bar rescue baz)`,
`for i in a end`,
`for i in x rescue nil; end`,
`h = {k: v rescue nil}`,
`if !!foo bar then end`,
`if !!foo bar; end`,
`if x 1 end`,
`if x end`,
`if x rescue nil; end`,
`module M; x; rescue foo bar; end`,
`not !!foo bar`,
`p !!foo bar`,
`p(x rescue nil)`,
`super(x rescue nil)`,
`unless x end`,
`unless x rescue nil; end`,
`until x end`,
`until x rescue nil; end`,
`while !!foo bar; end`,
`while [x rescue nil]; end`,
`while x end`,
`while x rescue nil; end`,
`x = [x rescue nil]`,
`{1 => not x}`,
`{1 => x rescue nil}`,
`{k: [x rescue nil]}`,
`{k: not x}`,
`{k: x rescue nil}`,
`~!foo bar`,
} {
_, err := Parse(src)
if err == nil {
t.Errorf("Parse(%q) = nil error, want a parse error (MRI 4.0.5 refuses it)", src)
continue
}
if !strings.Contains(err.Error(), "parse error") {
t.Errorf("Parse(%q) error = %q, want a parse error", src, err)
}
}
}
// The other direction, and the gate on the change: every shape here is one MRI
// ACCEPTS, and refusing any of them would be a regression against real Ruby. The
// contrasts that name the rule sit next to each other on purpose: `(foo bar)` vs
// `[foo bar]`, `a[foo bar]` vs `[foo bar]`, `f(foo bar, 1)` vs `f(1, foo bar)`,
// `[foo(bar baz)]` vs `[foo bar]`, `[begin; foo bar; end]` vs `[foo bar]`.
func TestParenLessCommandCallStillAcceptedWhereMRIAdmitsIt(t *testing.T) {
for _, src := range []string{
`!foo bar`,
`(foo bar do end)`,
`(foo bar)`,
`(foo bar; qux)`,
`[(foo bar)]`,
`[(foo bar; baz)]`,
`[*(foo bar)]`,
`[**a]`,
`[1 => a]`,
`[1].each do foo bar end`,
`[[1, foo(bar baz)]]`,
`[a ? foo(b c) : 1]`,
`[a[foo bar]]`,
`[begin; foo bar; end]`,
`[def m; foo bar; end]`,
`[defined?(break 1)]`,
`[defined?(foo bar)]`,
`[foo(bar baz), 1]`,
`[foo(bar baz)]`,
`[if foo bar then 1 end]`,
`[lambda { foo bar }]`,
`[proc do foo bar end]`,
`[while foo bar; end]`,
`a = [1][foo bar]`,
`a&.b foo bar`,
`a.b = foo bar`,
`a.b foo bar`,
`a[1] = foo bar`,
`a[foo bar, 1]`,
`a[foo bar]`,
`a[foo bar] += 1`,
`a[foo bar] = 1`,
`begin; foo bar; end`,
`case foo bar` + "\n" + `when 1` + "\n" + `end`,
`def m; defined? yield; end`,
`def m; return foo bar, 1; end`,
`def m; return foo bar; end`,
`def m; yield foo bar; end`,
`def m; yield(foo bar); end`,
`defined? @x`,
`defined? Foo::Bar`,
`defined? foo.bar`,
`defined? super`,
`defined? x`,
`defined? x = 1`,
`defined?(foo bar)`,
`f(foo bar)`,
`f(foo bar) { }`,
`f(foo bar) { } .z`,
`f(foo bar, 1)`,
`foo :a, (y do end)`,
`foo :a, [y do end]`,
`foo :a, {k: y do end}`,
`foo = 1; [foo -1]`,
`foo bar`,
`foo bar ? 1 : 2`,
`foo bar and baz`,
`foo bar baz`,
`foo bar baz, 1`,
`foo bar rescue baz`,
`foo bar, baz`,
`for i in foo bar; end`,
`h.each { |k, v| p k }`,
`if foo bar; end`,
`not foo bar`,
`p defined? x`,
`p foo bar`,
`p(foo bar)`,
`puts [1].map { |x| foo bar }`,
`return foo bar`,
`super foo bar`,
`super(foo bar)`,
`unless foo bar; end`,
`until [y do end]; end`,
`until foo bar; end`,
`while (y do end); end`,
`while [y do end]; end`,
`while a[y do end]; end`,
`while foo bar; end`,
`while true; break foo bar; end`,
`while true; next foo bar; end`,
`while {k: y do end}; end`,
`x += foo bar`,
`x = (y do end)`,
`x = 1; x[foo bar]`,
`x = [foo(bar baz)]`,
`x = defined? y`,
`x = foo bar`,
`x = foo bar, 1`,
`x = foo bar, baz`,
`x ||= foo bar`,
`x, y = foo bar`,
`{k: (foo bar)}`,
`{k: -> { foo bar }}`,
`{k: a[foo bar]}`,
`{k: defined?(foo bar)}`,
`{k: foo(bar baz)}`,
// --- v0.6.1 additions: the other direction. The contrasts that name each
// rule sit beside their refused twin above — `f((x rescue nil))` vs
// `f(x rescue nil)`, `f(a = 1 rescue nil)` vs `f(x rescue nil)` (the
// `arg_rhs` carve-out), `!!(foo bar)` vs `!!foo bar`, `when (y rescue nil)`
// vs `when y rescue nil`. `defined? x` in an `arg` position is here because
// `arg: keyword_defined '\n'? begin_defined arg` (4021) IS an `arg`
// production: `[defined? x]` is legal MRI and v0.6.0 refused it.
"begin\nx\nrescue Foo\ny\nend",
"case x\nwhen Foo\n1\nend",
`!!!defined? x`,
`!!!x`,
`!!(begin; foo bar; end)`,
`!!(foo bar)`,
`!!a[foo bar]`,
`!!begin; foo bar; end`,
`!!defined? x`,
`!!defined?(foo bar)`,
`!!foo(bar baz)`,
`!!foo(bar)`,
`!!if foo bar then 1 end`,
`!!lambda { foo bar }`,
`!!while foo bar; end`,
`!!x.y`,
`!!x[0]`,
`!!x`,
`!(foo bar)`,
`!Foo 1`,
`!a.b foo bar`,
`!foo bar rescue 1`,
`!foo bar, baz`,
`!foo.bar baz`,
`(not x)`,
`(x rescue nil)`,
`-(x rescue nil)`,
`-> { x rescue nil }`,
`-defined? x`,
`-x rescue nil`,
`1 + (x rescue nil)`,
`1 + defined? x`,
`1 + x rescue nil`,
`@x = y rescue nil`,
`X = y rescue nil`,
`[!x]`,
`[(x rescue nil)]`,
`[1].map { |x| x rescue nil }`,
`[a += x rescue nil]`,
`[a = 1 rescue nil, 2]`,
`[a = b = 1 rescue nil]`,
`[a = x rescue nil]`,
`[defined? foo]`,
`[defined? x]`,
`[defined?(x)]`,
`a += x rescue nil`,
`a = [1, 2] rescue nil`,
`a = b = x rescue nil`,
`a = x rescue nil`,
`a ? 1 : x rescue nil`,
`a, b = x rescue nil`,
`a.b = y rescue nil`,
`a[(x rescue nil)]`,
`a[0] = y rescue nil`,
`a[a = 1 rescue nil]`,
`a[defined? x]`,
`begin; x rescue nil; end`,
`begin; x; ensure; foo bar; end`,
`begin; x; rescue (Foo rescue nil); y; end`,
`begin; x; rescue (foo bar); y; end`,
`begin; x; rescue *A, B; y; end`,
`begin; x; rescue *Foo; y; end`,
`begin; x; rescue ::Foo; y; end`,
`begin; x; rescue => e; y; end`,
`begin; x; rescue Foo => e then y; end`,
`begin; x; rescue Foo => e; y; end`,
`begin; x; rescue Foo then y; end`,
`begin; x; rescue Foo then; y; end`,
`begin; x; rescue Foo, ::Bar; y; end`,
`begin; x; rescue Foo, Bar => e; y; end`,
`begin; x; rescue Foo, Bar then y; end`,
`begin; x; rescue Foo, Bar; y; end`,
`begin; x; rescue Foo, foo(bar baz); y; end`,
`begin; x; rescue Foo::Bar; y; end`,
`begin; x; rescue Foo; else; y; end`,
`begin; x; rescue Foo; end`,
`begin; x; rescue Foo; rescue Bar; end`,
`begin; x; rescue Foo; y; end rescue nil`,
`begin; x; rescue Foo; y; end`,
`begin; x; rescue [Foo, Bar].first; y; end`,
`begin; x; rescue a.b; y; end`,
`begin; x; rescue a::B; y; end`,
`begin; x; rescue a[foo bar]; y; end`,
`begin; x; rescue foo(bar); y; end`,
`begin; x; rescue; foo bar; end`,
`begin; x; rescue; y; end`,
`begin; y; rescue defined? x; z; end`,
`case (x rescue nil); when 1; end`,
`case x; in 1 then 2; end`,
`case x; in 1; 2; end`,
`case x; when (y rescue nil); end`,
`case x; when *a; 3; end`,
`case x; when 1 then 2; end`,
`case x; when 1 then; 2; end`,
`case x; when 1, 2; 3; end`,
`case x; when 1; 2; end`,
`case x; when 1; end`,
`case x; when Foo; end`,
`case x; when a.b; end`,
`case x; when a[0]; end`,
`case y; when defined? x; end`,
`def m = x rescue nil`,
`def m(a = (x rescue nil)); end`,
`def m(a = defined? x); end`,
`def m(a: (x rescue nil)); end`,
`def m; return x rescue nil; end`,
`def m; super x rescue nil; end`,
`def m; x rescue nil; end`,
`def m; x; ensure; y; end`,
`def m; yield x rescue nil; end`,
`f { x rescue nil }`,
`f($x = 1 rescue nil)`,
`f((x rescue nil))`,
`f(1) { x rescue nil }`,
`f(1, (x rescue nil))`,
`f(1, a = 2 rescue nil)`,
`f(1, defined? x)`,
`f(::A = 1 rescue nil)`,
`f(@x = 1 rescue nil)`,
`f(A = 1 rescue nil)`,
`f(a &&= 1 rescue nil)`,
`f(a = 1 rescue nil)`,
`f(a = b = 1 rescue nil)`,
`f(a ||= 1 rescue nil)`,
`f(a.b = 1 rescue nil)`,
`f(a::B = 1 rescue nil)`,
`f(a[0] = 1 rescue nil)`,
`f(x) rescue nil`,
`foo a = 1 rescue nil`,
`foo x rescue nil`,
`for i in (x rescue nil); end`,
`for i in a do end`,
`if (x rescue nil); end`,
`if a; elsif b; end`,
`if foo bar then 1 end`,
`if not x; end`,
`if x then 1 end`,
`if x; 1; end`,
`if x; end`,
`lambda { x rescue nil }`,
`loop do x rescue nil end`,
`not !foo bar`,
`not not foo bar`,
`not not x`,
`not x`,
`p x rescue nil`,
`return x rescue nil`,
`unless (x rescue nil); end`,
`unless not done; end`,
`until (x rescue nil); end`,
`while (x rescue nil); end`,
`while not x; end`,
`while x do end`,
`while x; end`,
`x = (not y)`,
`x = begin; y; rescue; 1; end rescue nil`,
`x = y rescue nil`,
`x rescue nil`,
`x ||= y rescue nil`,
`{k: (x rescue nil)}`,
`{k: a = x rescue nil}`,
`{k: defined? x}`,
} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) = %v, want no error (MRI 4.0.5 accepts it)", src, err)
}
}
}