ruby-next-parser 3.1.0.0 → 3.1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,7 +49,7 @@ rule
49
49
 
50
50
  program: {
51
51
  @current_arg_stack.push(nil)
52
- @max_numparam_stack.push
52
+ @max_numparam_stack.push(static: true)
53
53
  }
54
54
  top_compstmt
55
55
  {
@@ -264,21 +264,19 @@ rule
264
264
  }
265
265
  | defn_head f_opt_paren_args tEQL command
266
266
  {
267
- _def_t, name_t = val[0]
267
+ def_t, (name_t, ctx) = val[0]
268
268
  endless_method_name(name_t)
269
269
 
270
- result = @builder.def_endless_method(*val[0],
270
+ result = @builder.def_endless_method(def_t, name_t,
271
271
  val[1], val[2], val[3])
272
272
 
273
- @lexer.cmdarg.pop
274
- @lexer.cond.pop
275
- @static_env.unextend
276
- @context.pop
273
+ local_pop
277
274
  @current_arg_stack.pop
275
+ @context.in_def = ctx.in_def
278
276
  }
279
277
  | defn_head f_opt_paren_args tEQL command kRESCUE_MOD arg
280
278
  {
281
- _def_t, name_t = val[0]
279
+ def_t, (name_t, ctx) = val[0]
282
280
  endless_method_name(name_t)
283
281
 
284
282
  rescue_body = @builder.rescue_body(val[4],
@@ -287,32 +285,28 @@ rule
287
285
 
288
286
  method_body = @builder.begin_body(val[3], [ rescue_body ])
289
287
 
290
- result = @builder.def_endless_method(*val[0],
288
+ result = @builder.def_endless_method(def_t, name_t,
291
289
  val[1], val[2], method_body)
292
290
 
293
- @lexer.cmdarg.pop
294
- @lexer.cond.pop
295
- @static_env.unextend
296
- @context.pop
291
+ local_pop
297
292
  @current_arg_stack.pop
293
+ @context.in_def = ctx.in_def
298
294
  }
299
295
  | defs_head f_opt_paren_args tEQL command
300
296
  {
301
- _def_t, _recv, _dot_t, name_t = val[0]
297
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
302
298
  endless_method_name(name_t)
303
299
 
304
- result = @builder.def_endless_singleton(*val[0],
300
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
305
301
  val[1], val[2], val[3])
306
302
 
307
- @lexer.cmdarg.pop
308
- @lexer.cond.pop
309
- @static_env.unextend
310
- @context.pop
303
+ local_pop
311
304
  @current_arg_stack.pop
305
+ @context.in_def = ctx.in_def
312
306
  }
313
307
  | defs_head f_opt_paren_args tEQL command kRESCUE_MOD arg
314
308
  {
315
- _def_t, _recv, _dot_t, name_t = val[0]
309
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
316
310
  endless_method_name(name_t)
317
311
 
318
312
  rescue_body = @builder.rescue_body(val[4],
@@ -321,14 +315,12 @@ rule
321
315
 
322
316
  method_body = @builder.begin_body(val[3], [ rescue_body ])
323
317
 
324
- result = @builder.def_endless_singleton(*val[0],
318
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
325
319
  val[1], val[2], method_body)
326
320
 
327
- @lexer.cmdarg.pop
328
- @lexer.cond.pop
329
- @static_env.unextend
330
- @context.pop
321
+ local_pop
331
322
  @current_arg_stack.pop
323
+ @context.in_def = ctx.in_def
332
324
  }
333
325
  | backref tOP_ASGN command_rhs
334
326
  {
@@ -369,13 +361,13 @@ rule
369
361
  @lexer.command_start = false
370
362
  @pattern_variables.push
371
363
 
372
- result = @lexer.in_kwarg
373
- @lexer.in_kwarg = true
364
+ result = @context.in_kwarg
365
+ @context.in_kwarg = true
374
366
  }
375
367
  p_top_expr_body
376
368
  {
377
369
  @pattern_variables.pop
378
- @lexer.in_kwarg = val[2]
370
+ @context.in_kwarg = val[2]
379
371
  result = @builder.match_pattern(val[0], val[1], val[3])
380
372
  }
381
373
  | arg kIN
@@ -384,13 +376,13 @@ rule
384
376
  @lexer.command_start = false
385
377
  @pattern_variables.push
386
378
 
387
- result = @lexer.in_kwarg
388
- @lexer.in_kwarg = true
379
+ result = @context.in_kwarg
380
+ @context.in_kwarg = true
389
381
  }
390
382
  p_top_expr_body
391
383
  {
392
384
  @pattern_variables.pop
393
- @lexer.in_kwarg = val[2]
385
+ @context.in_kwarg = val[2]
394
386
  result = @builder.match_pattern_p(val[0], val[1], val[3])
395
387
  }
396
388
  | arg =tLBRACE_ARG
@@ -408,29 +400,25 @@ rule
408
400
 
409
401
  def_name: fname
410
402
  {
411
- @static_env.extend_static
412
- @lexer.cmdarg.push(false)
413
- @lexer.cond.push(false)
403
+ local_push
414
404
  @current_arg_stack.push(nil)
415
405
 
416
- result = val[0]
406
+ result = [ val[0], @context.dup ]
407
+ @context.in_def = true
417
408
  }
418
409
 
419
- defn_head: kDEF def_name
410
+ defn_head: k_def def_name
420
411
  {
421
- @context.push(:def)
422
-
423
412
  result = [ val[0], val[1] ]
424
413
  }
425
414
 
426
- defs_head: kDEF singleton dot_or_colon
415
+ defs_head: k_def singleton dot_or_colon
427
416
  {
428
417
  @lexer.state = :expr_fname
418
+ @context.in_argdef = true
429
419
  }
430
420
  def_name
431
421
  {
432
- @context.push(:defs)
433
-
434
422
  result = [ val[0], val[1], val[2], val[4] ]
435
423
  }
436
424
 
@@ -447,12 +435,13 @@ rule
447
435
 
448
436
  cmd_brace_block: tLBRACE_ARG
449
437
  {
450
- @context.push(:block)
438
+ result = @context.dup
439
+ @context.in_block = true
451
440
  }
452
441
  brace_body tRCURLY
453
442
  {
443
+ @context.in_block = val[1].in_block
454
444
  result = [ val[0], *val[2], val[3] ]
455
- @context.pop
456
445
  }
457
446
 
458
447
  fcall: operation
@@ -933,9 +922,14 @@ rule
933
922
  {
934
923
  result = @builder.logical_op(:or, val[0], val[1], val[2])
935
924
  }
936
- | kDEFINED opt_nl arg
925
+ | kDEFINED opt_nl
926
+ {
927
+ @context.in_defined = true
928
+ }
929
+ arg
937
930
  {
938
- result = @builder.keyword_cmd(:defined?, val[0], nil, [ val[2] ], nil)
931
+ @context.in_defined = false
932
+ result = @builder.keyword_cmd(:defined?, val[0], nil, [ val[3] ], nil)
939
933
  }
940
934
  | arg tEH arg opt_nl tCOLON arg
941
935
  {
@@ -944,21 +938,19 @@ rule
944
938
  }
945
939
  | defn_head f_opt_paren_args tEQL arg
946
940
  {
947
- _def_t, name_t = val[0]
941
+ def_t, (name_t, ctx) = val[0]
948
942
  endless_method_name(name_t)
949
943
 
950
- result = @builder.def_endless_method(*val[0],
944
+ result = @builder.def_endless_method(def_t, name_t,
951
945
  val[1], val[2], val[3])
952
946
 
953
- @lexer.cmdarg.pop
954
- @lexer.cond.pop
955
- @static_env.unextend
956
- @context.pop
947
+ local_pop
957
948
  @current_arg_stack.pop
949
+ @context.in_def = ctx.in_def
958
950
  }
959
951
  | defn_head f_opt_paren_args tEQL arg kRESCUE_MOD arg
960
952
  {
961
- _def_t, name_t = val[0]
953
+ def_t, (name_t, ctx) = val[0]
962
954
  endless_method_name(name_t)
963
955
 
964
956
  rescue_body = @builder.rescue_body(val[4],
@@ -967,32 +959,28 @@ rule
967
959
 
968
960
  method_body = @builder.begin_body(val[3], [ rescue_body ])
969
961
 
970
- result = @builder.def_endless_method(*val[0],
962
+ result = @builder.def_endless_method(def_t, name_t,
971
963
  val[1], val[2], method_body)
972
964
 
973
- @lexer.cmdarg.pop
974
- @lexer.cond.pop
975
- @static_env.unextend
976
- @context.pop
965
+ local_pop
977
966
  @current_arg_stack.pop
967
+ @context.in_def = ctx.in_def
978
968
  }
979
969
  | defs_head f_opt_paren_args tEQL arg
980
970
  {
981
- _def_t, _recv, _dot_t, name_t = val[0]
971
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
982
972
  endless_method_name(name_t)
983
973
 
984
- result = @builder.def_endless_singleton(*val[0],
974
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
985
975
  val[1], val[2], val[3])
986
976
 
987
- @lexer.cmdarg.pop
988
- @lexer.cond.pop
989
- @static_env.unextend
990
- @context.pop
977
+ local_pop
991
978
  @current_arg_stack.pop
979
+ @context.in_def = ctx.in_def
992
980
  }
993
981
  | defs_head f_opt_paren_args tEQL arg kRESCUE_MOD arg
994
982
  {
995
- _def_t, _recv, _dot_t, name_t = val[0]
983
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
996
984
  endless_method_name(name_t)
997
985
 
998
986
  rescue_body = @builder.rescue_body(val[4],
@@ -1001,14 +989,12 @@ rule
1001
989
 
1002
990
  method_body = @builder.begin_body(val[3], [ rescue_body ])
1003
991
 
1004
- result = @builder.def_endless_singleton(*val[0],
992
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
1005
993
  val[1], val[2], method_body)
1006
994
 
1007
- @lexer.cmdarg.pop
1008
- @lexer.cond.pop
1009
- @static_env.unextend
1010
- @context.pop
995
+ local_pop
1011
996
  @current_arg_stack.pop
997
+ @context.in_def = ctx.in_def
1012
998
  }
1013
999
  | primary
1014
1000
 
@@ -1160,6 +1146,14 @@ rule
1160
1146
  {
1161
1147
  result = @builder.block_pass(val[0], val[1])
1162
1148
  }
1149
+ | tAMPER
1150
+ {
1151
+ if !@static_env.declared_anonymous_blockarg?
1152
+ diagnostic :error, :no_anonymous_blockarg, nil, val[0]
1153
+ end
1154
+
1155
+ result = @builder.block_pass(val[0], nil)
1156
+ }
1163
1157
 
1164
1158
  opt_block_arg: tCOMMA block_arg
1165
1159
  {
@@ -1282,10 +1276,15 @@ rule
1282
1276
  {
1283
1277
  result = @builder.keyword_cmd(:yield, val[0])
1284
1278
  }
1285
- | kDEFINED opt_nl tLPAREN2 expr rparen
1279
+ | kDEFINED opt_nl tLPAREN2
1280
+ {
1281
+ @context.in_defined = true
1282
+ }
1283
+ expr rparen
1286
1284
  {
1285
+ @context.in_defined = false
1287
1286
  result = @builder.keyword_cmd(:defined?, val[0],
1288
- val[2], [ val[3] ], val[4])
1287
+ val[2], [ val[4] ], val[5])
1289
1288
  }
1290
1289
  | kNOT tLPAREN2 expr rparen
1291
1290
  {
@@ -1361,86 +1360,77 @@ rule
1361
1360
  {
1362
1361
  result = @builder.for(val[0], val[1], val[2], *val[3], val[4], val[5])
1363
1362
  }
1364
- | kCLASS cpath superclass
1363
+ | k_class cpath superclass
1365
1364
  {
1366
- @static_env.extend_static
1367
- @lexer.cmdarg.push(false)
1368
- @lexer.cond.push(false)
1369
- @context.push(:class)
1365
+ @context.in_class = true
1366
+ local_push
1370
1367
  }
1371
1368
  bodystmt kEND
1372
1369
  {
1373
- unless @context.class_definition_allowed?
1374
- diagnostic :error, :class_in_def, nil, val[0]
1370
+ k_class, ctx = val[0]
1371
+ if @context.in_def
1372
+ diagnostic :error, :class_in_def, nil, k_class
1375
1373
  end
1376
-
1377
1374
  lt_t, superclass = val[2]
1378
- result = @builder.def_class(val[0], val[1],
1375
+ result = @builder.def_class(k_class, val[1],
1379
1376
  lt_t, superclass,
1380
1377
  val[4], val[5])
1381
1378
 
1382
- @lexer.cmdarg.pop
1383
- @lexer.cond.pop
1384
- @static_env.unextend
1385
- @context.pop
1379
+ local_pop
1380
+ @context.in_class = ctx.in_class
1386
1381
  }
1387
- | kCLASS tLSHFT expr term
1382
+ | k_class tLSHFT expr term
1388
1383
  {
1389
- @static_env.extend_static
1390
- @lexer.cmdarg.push(false)
1391
- @lexer.cond.push(false)
1392
- @context.push(:sclass)
1384
+ @context.in_def = false
1385
+ @context.in_class = false
1386
+ local_push
1393
1387
  }
1394
1388
  bodystmt kEND
1395
1389
  {
1396
- result = @builder.def_sclass(val[0], val[1], val[2],
1390
+ k_class, ctx = val[0]
1391
+ result = @builder.def_sclass(k_class, val[1], val[2],
1397
1392
  val[5], val[6])
1398
1393
 
1399
- @lexer.cmdarg.pop
1400
- @lexer.cond.pop
1401
- @static_env.unextend
1402
- @context.pop
1394
+ local_pop
1395
+ @context.in_def = ctx.in_def
1396
+ @context.in_class = ctx.in_class
1403
1397
  }
1404
- | kMODULE cpath
1398
+ | k_module cpath
1405
1399
  {
1406
- @static_env.extend_static
1407
- @lexer.cmdarg.push(false)
1408
- @context.push(:module)
1400
+ @context.in_class = true
1401
+ local_push
1409
1402
  }
1410
1403
  bodystmt kEND
1411
1404
  {
1412
- unless @context.module_definition_allowed?
1413
- diagnostic :error, :module_in_def, nil, val[0]
1405
+ k_mod, ctx = val[0]
1406
+ if @context.in_def
1407
+ diagnostic :error, :module_in_def, nil, k_mod
1414
1408
  end
1415
-
1416
- result = @builder.def_module(val[0], val[1],
1409
+ result = @builder.def_module(k_mod, val[1],
1417
1410
  val[3], val[4])
1418
1411
 
1419
- @lexer.cmdarg.pop
1420
- @static_env.unextend
1421
- @context.pop
1412
+ local_pop
1413
+ @context.in_class = ctx.in_class
1422
1414
  }
1423
1415
  | defn_head f_arglist bodystmt kEND
1424
1416
  {
1425
- result = @builder.def_method(*val[0], val[1],
1417
+ def_t, (name_t, ctx) = val[0]
1418
+ result = @builder.def_method(def_t, name_t, val[1],
1426
1419
  val[2], val[3])
1427
1420
 
1428
- @lexer.cmdarg.pop
1429
- @lexer.cond.pop
1430
- @static_env.unextend
1431
- @context.pop
1421
+ local_pop
1432
1422
  @current_arg_stack.pop
1423
+ @context.in_def = ctx.in_def
1433
1424
  }
1434
1425
  | defs_head f_arglist bodystmt kEND
1435
1426
  {
1436
- result = @builder.def_singleton(*val[0], val[1],
1427
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
1428
+ result = @builder.def_singleton(def_t, recv, dot_t, name_t, val[1],
1437
1429
  val[2], val[3])
1438
1430
 
1439
- @lexer.cmdarg.pop
1440
- @lexer.cond.pop
1441
- @static_env.unextend
1442
- @context.pop
1431
+ local_pop
1443
1432
  @current_arg_stack.pop
1433
+ @context.in_def = ctx.in_def
1444
1434
  }
1445
1435
  | kBREAK
1446
1436
  {
@@ -1465,9 +1455,25 @@ rule
1465
1455
 
1466
1456
  primary_value: primary
1467
1457
 
1458
+ k_class: kCLASS
1459
+ {
1460
+ result = [ val[0], @context.dup ]
1461
+ }
1462
+
1463
+ k_module: kMODULE
1464
+ {
1465
+ result = [ val[0], @context.dup ]
1466
+ }
1467
+
1468
+ k_def: kDEF
1469
+ {
1470
+ result = val[0]
1471
+ @context.in_argdef = true
1472
+ }
1473
+
1468
1474
  k_return: kRETURN
1469
1475
  {
1470
- if @context.in_class?
1476
+ if @context.in_class && !@context.in_def && !(context.in_block || context.in_lambda)
1471
1477
  diagnostic :error, :invalid_return, nil, val[0]
1472
1478
  end
1473
1479
  }
@@ -1553,6 +1559,15 @@ rule
1553
1559
  f_any_kwrest: f_kwrest
1554
1560
  | f_no_kwarg
1555
1561
 
1562
+
1563
+ f_eq: {
1564
+ @context.in_argdef = false
1565
+ }
1566
+ tEQL
1567
+ {
1568
+ result = val[1]
1569
+ }
1570
+
1556
1571
  block_args_tail: f_block_kwarg tCOMMA f_kwrest opt_f_block_arg
1557
1572
  {
1558
1573
  result = val[0].concat(val[2]).concat(val[3])
@@ -1682,12 +1697,14 @@ opt_block_args_tail:
1682
1697
  {
1683
1698
  @max_numparam_stack.has_ordinary_params!
1684
1699
  @current_arg_stack.set(nil)
1700
+ @context.in_argdef = false
1685
1701
  result = @builder.args(val[0], val[1], val[2])
1686
1702
  }
1687
1703
  | tPIPE block_param opt_bv_decl tPIPE
1688
1704
  {
1689
1705
  @max_numparam_stack.has_ordinary_params!
1690
1706
  @current_arg_stack.set(nil)
1707
+ @context.in_argdef = false
1691
1708
  result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1692
1709
  }
1693
1710
 
@@ -1719,12 +1736,12 @@ opt_block_args_tail:
1719
1736
  lambda: tLAMBDA
1720
1737
  {
1721
1738
  @static_env.extend_dynamic
1722
- @max_numparam_stack.push
1723
- @context.push(:lambda)
1739
+ @max_numparam_stack.push(static: false)
1740
+ result = @context.dup
1741
+ @context.in_lambda = true
1724
1742
  }
1725
1743
  f_larglist
1726
1744
  {
1727
- @context.pop
1728
1745
  @lexer.cmdarg.push(false)
1729
1746
  }
1730
1747
  lambda_body
@@ -1736,6 +1753,7 @@ opt_block_args_tail:
1736
1753
  @max_numparam_stack.pop
1737
1754
  @static_env.unextend
1738
1755
  @lexer.cmdarg.pop
1756
+ @context.in_lambda = val[1].in_lambda
1739
1757
 
1740
1758
  result = @builder.block(lambda_call,
1741
1759
  begin_t, args, body, end_t)
@@ -1743,11 +1761,13 @@ opt_block_args_tail:
1743
1761
 
1744
1762
  f_larglist: tLPAREN2 f_args opt_bv_decl tRPAREN
1745
1763
  {
1764
+ @context.in_argdef = false
1746
1765
  @max_numparam_stack.has_ordinary_params!
1747
1766
  result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1748
1767
  }
1749
1768
  | f_args
1750
1769
  {
1770
+ @context.in_argdef = false
1751
1771
  if val[0].any?
1752
1772
  @max_numparam_stack.has_ordinary_params!
1753
1773
  end
@@ -1756,31 +1776,34 @@ opt_block_args_tail:
1756
1776
 
1757
1777
  lambda_body: tLAMBEG
1758
1778
  {
1759
- @context.push(:lambda)
1779
+ result = @context.dup
1780
+ @context.in_lambda = true
1760
1781
  }
1761
1782
  compstmt tRCURLY
1762
1783
  {
1784
+ @context.in_lambda = val[1].in_lambda
1763
1785
  result = [ val[0], val[2], val[3] ]
1764
- @context.pop
1765
1786
  }
1766
1787
  | kDO_LAMBDA
1767
1788
  {
1768
- @context.push(:lambda)
1789
+ result = @context.dup
1790
+ @context.in_lambda = true
1769
1791
  }
1770
1792
  bodystmt kEND
1771
1793
  {
1794
+ @context.in_lambda = val[1].in_lambda
1772
1795
  result = [ val[0], val[2], val[3] ]
1773
- @context.pop
1774
1796
  }
1775
1797
 
1776
1798
  do_block: kDO_BLOCK
1777
1799
  {
1778
- @context.push(:block)
1800
+ result = @context.dup
1801
+ @context.in_block = true
1779
1802
  }
1780
1803
  do_body kEND
1781
1804
  {
1805
+ @context.in_block = val[1].in_block
1782
1806
  result = [ val[0], *val[2], val[3] ]
1783
- @context.pop
1784
1807
  }
1785
1808
 
1786
1809
  block_call: command do_block
@@ -1866,26 +1889,28 @@ opt_block_args_tail:
1866
1889
 
1867
1890
  brace_block: tLCURLY
1868
1891
  {
1869
- @context.push(:block)
1892
+ result = @context.dup
1893
+ @context.in_block = true
1870
1894
  }
1871
1895
  brace_body tRCURLY
1872
1896
  {
1897
+ @context.in_block = val[1].in_block
1873
1898
  result = [ val[0], *val[2], val[3] ]
1874
- @context.pop
1875
1899
  }
1876
1900
  | kDO
1877
1901
  {
1878
- @context.push(:block)
1902
+ result = @context.dup
1903
+ @context.in_block = true
1879
1904
  }
1880
1905
  do_body kEND
1881
1906
  {
1907
+ @context.in_block = val[1].in_block
1882
1908
  result = [ val[0], *val[2], val[3] ]
1883
- @context.pop
1884
1909
  }
1885
1910
 
1886
1911
  brace_body: {
1887
1912
  @static_env.extend_dynamic
1888
- @max_numparam_stack.push
1913
+ @max_numparam_stack.push(static: false)
1889
1914
  }
1890
1915
  opt_block_param compstmt
1891
1916
  {
@@ -1898,7 +1923,7 @@ opt_block_args_tail:
1898
1923
 
1899
1924
  do_body: {
1900
1925
  @static_env.extend_dynamic
1901
- @max_numparam_stack.push
1926
+ @max_numparam_stack.push(static: false)
1902
1927
  }
1903
1928
  {
1904
1929
  @lexer.cmdarg.push(false)
@@ -1932,14 +1957,14 @@ opt_block_args_tail:
1932
1957
  @pattern_variables.push
1933
1958
  @pattern_hash_keys.push
1934
1959
 
1935
- result = @lexer.in_kwarg
1936
- @lexer.in_kwarg = true
1960
+ result = @context.in_kwarg
1961
+ @context.in_kwarg = true
1937
1962
  }
1938
1963
  p_top_expr then
1939
1964
  {
1940
1965
  @pattern_variables.pop
1941
1966
  @pattern_hash_keys.pop
1942
- @lexer.in_kwarg = val[1]
1967
+ @context.in_kwarg = val[1]
1943
1968
  }
1944
1969
  compstmt p_cases
1945
1970
  {
@@ -2081,13 +2106,13 @@ opt_block_args_tail:
2081
2106
  | tLBRACE
2082
2107
  {
2083
2108
  @pattern_hash_keys.push
2084
- result = @lexer.in_kwarg
2085
- @lexer.in_kwarg = false
2109
+ result = @context.in_kwarg
2110
+ @context.in_kwarg = false
2086
2111
  }
2087
2112
  p_kwargs rbrace
2088
2113
  {
2089
2114
  @pattern_hash_keys.pop
2090
- @lexer.in_kwarg = val[1]
2115
+ @context.in_kwarg = val[1]
2091
2116
  result = @builder.hash_pattern(val[0], val[2], val[3])
2092
2117
  }
2093
2118
  | tLBRACE rbrace
@@ -2116,23 +2141,13 @@ opt_block_args_tail:
2116
2141
  {
2117
2142
  result = [ *val[0], val[1] ]
2118
2143
  }
2119
- | p_args_head tSTAR tIDENTIFIER
2120
- {
2121
- match_rest = @builder.match_rest(val[1], val[2])
2122
- result = [ *val[0], match_rest ]
2123
- }
2124
- | p_args_head tSTAR tIDENTIFIER tCOMMA p_args_post
2144
+ | p_args_head p_rest
2125
2145
  {
2126
- match_rest = @builder.match_rest(val[1], val[2])
2127
- result = [ *val[0], match_rest, *val[4] ]
2128
- }
2129
- | p_args_head tSTAR
2130
- {
2131
- result = [ *val[0], @builder.match_rest(val[1]) ]
2146
+ result = [ *val[0], val[1] ]
2132
2147
  }
2133
- | p_args_head tSTAR tCOMMA p_args_post
2148
+ | p_args_head p_rest tCOMMA p_args_post
2134
2149
  {
2135
- result = [ *val[0], @builder.match_rest(val[1]), *val[3] ]
2150
+ result = [ *val[0], val[1], *val[3] ]
2136
2151
  }
2137
2152
  | p_args_tail
2138
2153
 
@@ -2171,6 +2186,11 @@ opt_block_args_tail:
2171
2186
  {
2172
2187
  result = @builder.match_rest(val[0], val[1])
2173
2188
  }
2189
+ | tSTAR nonlocal_var
2190
+ {
2191
+ non_lvar = @builder.accessible(val[1])
2192
+ result = @builder.match_rest(val[0], non_lvar)
2193
+ }
2174
2194
  | tSTAR
2175
2195
  {
2176
2196
  result = @builder.match_rest(val[0])
@@ -2235,6 +2255,10 @@ opt_block_args_tail:
2235
2255
  {
2236
2256
  result = [ @builder.match_rest(val[0], val[1]) ]
2237
2257
  }
2258
+ | kwrest_mark nonlocal_var
2259
+ {
2260
+ result = [ @builder.match_rest(val[0], val[1]) ]
2261
+ }
2238
2262
  | kwrest_mark
2239
2263
  {
2240
2264
  result = [ @builder.match_rest(val[0], nil) ]
@@ -2295,6 +2319,11 @@ opt_block_args_tail:
2295
2319
  {
2296
2320
  result = @builder.assignable(@builder.match_var(val[0]))
2297
2321
  }
2322
+ | nonlocal_var
2323
+ {
2324
+ non_lvar = @builder.accessible(val[0])
2325
+ result = @builder.assignable(@builder.match_var(non_lvar))
2326
+ }
2298
2327
 
2299
2328
  p_var_ref: tCARET tIDENTIFIER
2300
2329
  {
@@ -2696,6 +2725,7 @@ keyword_variable: kNIL
2696
2725
  f_opt_paren_args: f_paren_args
2697
2726
  | none
2698
2727
  {
2728
+ @context.in_argdef = false
2699
2729
  result = @builder.args(nil, [], nil)
2700
2730
  }
2701
2731
 
@@ -2704,30 +2734,19 @@ f_opt_paren_args: f_paren_args
2704
2734
  result = @builder.args(val[0], val[1], val[2])
2705
2735
 
2706
2736
  @lexer.state = :expr_value
2707
- }
2708
- | tLPAREN2 f_arg tCOMMA args_forward rparen
2709
- {
2710
- args = [ *val[1], @builder.forward_arg(val[3]) ]
2711
- result = @builder.args(val[0], args, val[4])
2712
-
2713
- @static_env.declare_forward_args
2714
- }
2715
- | tLPAREN2 args_forward rparen
2716
- {
2717
- result = @builder.forward_only_args(val[0], val[1], val[2])
2718
- @static_env.declare_forward_args
2719
-
2720
- @lexer.state = :expr_value
2737
+ @context.in_argdef = false
2721
2738
  }
2722
2739
 
2723
2740
  f_arglist: f_paren_args
2724
2741
  | {
2725
- result = @lexer.in_kwarg
2726
- @lexer.in_kwarg = true
2742
+ result = @context.dup
2743
+ @context.in_kwarg = true
2744
+ @context.in_argdef = true
2727
2745
  }
2728
2746
  f_args term
2729
2747
  {
2730
- @lexer.in_kwarg = val[0]
2748
+ @context.in_kwarg = val[0].in_kwarg
2749
+ @context.in_argdef = false
2731
2750
  result = @builder.args(nil, val[1], nil)
2732
2751
  }
2733
2752
 
@@ -2747,6 +2766,11 @@ f_opt_paren_args: f_paren_args
2747
2766
  {
2748
2767
  result = [ val[0] ]
2749
2768
  }
2769
+ | args_forward
2770
+ {
2771
+ @static_env.declare_forward_args
2772
+ result = [ @builder.forward_arg(val[0]) ]
2773
+ }
2750
2774
 
2751
2775
  opt_args_tail: tCOMMA args_tail
2752
2776
  {
@@ -2913,6 +2937,7 @@ f_opt_paren_args: f_paren_args
2913
2937
  @max_numparam_stack.has_ordinary_params!
2914
2938
 
2915
2939
  @current_arg_stack.set(val[0][0])
2940
+ @context.in_argdef = false
2916
2941
 
2917
2942
  result = val[0]
2918
2943
  }
@@ -2920,20 +2945,24 @@ f_opt_paren_args: f_paren_args
2920
2945
  f_kw: f_label arg_value
2921
2946
  {
2922
2947
  @current_arg_stack.set(nil)
2948
+ @context.in_argdef = true
2923
2949
  result = @builder.kwoptarg(val[0], val[1])
2924
2950
  }
2925
2951
  | f_label
2926
2952
  {
2927
2953
  @current_arg_stack.set(nil)
2954
+ @context.in_argdef = true
2928
2955
  result = @builder.kwarg(val[0])
2929
2956
  }
2930
2957
 
2931
2958
  f_block_kw: f_label primary_value
2932
2959
  {
2960
+ @context.in_argdef = true
2933
2961
  result = @builder.kwoptarg(val[0], val[1])
2934
2962
  }
2935
2963
  | f_label
2936
2964
  {
2965
+ @context.in_argdef = true
2937
2966
  result = @builder.kwarg(val[0])
2938
2967
  }
2939
2968
 
@@ -2973,15 +3002,17 @@ f_opt_paren_args: f_paren_args
2973
3002
  result = [ @builder.kwrestarg(val[0]) ]
2974
3003
  }
2975
3004
 
2976
- f_opt: f_arg_asgn tEQL arg_value
3005
+ f_opt: f_arg_asgn f_eq arg_value
2977
3006
  {
2978
3007
  @current_arg_stack.set(0)
3008
+ @context.in_argdef = true
2979
3009
  result = @builder.optarg(val[0], val[1], val[2])
2980
3010
  }
2981
3011
 
2982
- f_block_opt: f_arg_asgn tEQL primary_value
3012
+ f_block_opt: f_arg_asgn f_eq primary_value
2983
3013
  {
2984
3014
  @current_arg_stack.set(0)
3015
+ @context.in_argdef = true
2985
3016
  result = @builder.optarg(val[0], val[1], val[2])
2986
3017
  }
2987
3018
 
@@ -3024,6 +3055,12 @@ f_opt_paren_args: f_paren_args
3024
3055
 
3025
3056
  result = @builder.blockarg(val[0], val[1])
3026
3057
  }
3058
+ | blkarg_mark
3059
+ {
3060
+ @static_env.declare_anonymous_blockarg
3061
+
3062
+ result = @builder.blockarg(val[0], nil)
3063
+ }
3027
3064
 
3028
3065
  opt_f_block_arg: tCOMMA f_block_arg
3029
3066
  {
@@ -3126,10 +3163,10 @@ require 'parser/ruby-next/parser_ext'
3126
3163
 
3127
3164
  ---- inner
3128
3165
 
3129
- prepend Parser::NextExt
3166
+ prepend Parser::NextExt
3130
3167
 
3131
3168
  def version
3132
- 31
3169
+ 32
3133
3170
  end
3134
3171
 
3135
3172
  def default_encoding
@@ -3142,10 +3179,24 @@ require 'parser/ruby-next/parser_ext'
3142
3179
  end
3143
3180
  end
3144
3181
 
3182
+ def local_push
3183
+ @static_env.extend_static
3184
+ @lexer.cmdarg.push(false)
3185
+ @lexer.cond.push(false)
3186
+ @max_numparam_stack.push(static: true)
3187
+ end
3188
+
3189
+ def local_pop
3190
+ @static_env.unextend
3191
+ @lexer.cmdarg.pop
3192
+ @lexer.cond.pop
3193
+ @max_numparam_stack.pop
3194
+ end
3195
+
3145
3196
  def try_declare_numparam(node)
3146
3197
  name = node.children[0]
3147
3198
 
3148
- if name =~ /\A_[1-9]\z/ && !static_env.declared?(name) && context.in_dynamic_block?
3199
+ if name =~ /\A_[1-9]\z/ && !static_env.declared?(name) && @context.in_dynamic_block?
3149
3200
  # definitely an implicit param
3150
3201
  location = node.loc.expression
3151
3202
 
@@ -3153,27 +3204,25 @@ require 'parser/ruby-next/parser_ext'
3153
3204
  diagnostic :error, :ordinary_param_defined, nil, [nil, location]
3154
3205
  end
3155
3206
 
3156
- raw_context = context.stack.dup
3157
3207
  raw_max_numparam_stack = max_numparam_stack.stack.dup
3158
-
3159
3208
  # ignore current block scope
3160
- raw_context.pop
3161
3209
  raw_max_numparam_stack.pop
3162
3210
 
3163
- raw_context.reverse_each do |outer_scope|
3164
- if outer_scope == :block || outer_scope == :lambda
3165
- outer_scope_has_numparams = raw_max_numparam_stack.pop > 0
3211
+ raw_max_numparam_stack.reverse_each do |outer_scope|
3212
+ if outer_scope[:static]
3213
+ # found an outer scope that can't have numparams
3214
+ # like def/class/etc
3215
+ break
3216
+ else
3217
+ outer_scope_has_numparams = outer_scope[:value] > 0
3166
3218
 
3167
3219
  if outer_scope_has_numparams
3168
3220
  diagnostic :error, :numparam_used_in_outer_scope, nil, [nil, location]
3169
3221
  else
3170
3222
  # for now it's ok, but an outer scope can also be a block
3223
+ # like proc { _1; proc { proc { proc { _2 }} }}
3171
3224
  # with numparams, so we need to continue
3172
3225
  end
3173
- else
3174
- # found an outer scope that can't have numparams
3175
- # like def/class/etc
3176
- break
3177
3226
  end
3178
3227
  end
3179
3228