ruby-next-parser 3.0.1.0 → 3.1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,7 +17,8 @@ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
17
17
  tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG tSTRING_DBEG
18
18
  tSTRING_DVAR tSTRING_END tSTRING_DEND tSTRING tSYMBOL
19
19
  tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA tLAMBEG tCHARACTER
20
- tRATIONAL tIMAGINARY tLABEL_END tANDDOT tMETHREF tBDOT2 tBDOT3
20
+ tRATIONAL tIMAGINARY tLABEL_END tANDDOT tBDOT2 tBDOT3
21
+ tMETHREF
21
22
 
22
23
  prechigh
23
24
  right tBANG tTILDE tUPLUS
@@ -48,7 +49,7 @@ rule
48
49
 
49
50
  program: {
50
51
  @current_arg_stack.push(nil)
51
- @max_numparam_stack.push
52
+ @max_numparam_stack.push(static: true)
52
53
  }
53
54
  top_compstmt
54
55
  {
@@ -263,21 +264,19 @@ rule
263
264
  }
264
265
  | defn_head f_opt_paren_args tEQL command
265
266
  {
266
- _def_t, name_t = val[0]
267
+ def_t, (name_t, ctx) = val[0]
267
268
  endless_method_name(name_t)
268
269
 
269
- result = @builder.def_endless_method(*val[0],
270
+ result = @builder.def_endless_method(def_t, name_t,
270
271
  val[1], val[2], val[3])
271
272
 
272
- @lexer.cmdarg.pop
273
- @lexer.cond.pop
274
- @static_env.unextend
275
- @context.pop
273
+ local_pop
276
274
  @current_arg_stack.pop
275
+ @context.in_def = ctx.in_def
277
276
  }
278
277
  | defn_head f_opt_paren_args tEQL command kRESCUE_MOD arg
279
278
  {
280
- _def_t, name_t = val[0]
279
+ def_t, (name_t, ctx) = val[0]
281
280
  endless_method_name(name_t)
282
281
 
283
282
  rescue_body = @builder.rescue_body(val[4],
@@ -286,32 +285,28 @@ rule
286
285
 
287
286
  method_body = @builder.begin_body(val[3], [ rescue_body ])
288
287
 
289
- result = @builder.def_endless_method(*val[0],
288
+ result = @builder.def_endless_method(def_t, name_t,
290
289
  val[1], val[2], method_body)
291
290
 
292
- @lexer.cmdarg.pop
293
- @lexer.cond.pop
294
- @static_env.unextend
295
- @context.pop
291
+ local_pop
296
292
  @current_arg_stack.pop
293
+ @context.in_def = ctx.in_def
297
294
  }
298
295
  | defs_head f_opt_paren_args tEQL command
299
296
  {
300
- _def_t, _recv, _dot_t, name_t = val[0]
297
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
301
298
  endless_method_name(name_t)
302
299
 
303
- result = @builder.def_endless_singleton(*val[0],
300
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
304
301
  val[1], val[2], val[3])
305
302
 
306
- @lexer.cmdarg.pop
307
- @lexer.cond.pop
308
- @static_env.unextend
309
- @context.pop
303
+ local_pop
310
304
  @current_arg_stack.pop
305
+ @context.in_def = ctx.in_def
311
306
  }
312
307
  | defs_head f_opt_paren_args tEQL command kRESCUE_MOD arg
313
308
  {
314
- _def_t, _recv, _dot_t, name_t = val[0]
309
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
315
310
  endless_method_name(name_t)
316
311
 
317
312
  rescue_body = @builder.rescue_body(val[4],
@@ -320,14 +315,12 @@ rule
320
315
 
321
316
  method_body = @builder.begin_body(val[3], [ rescue_body ])
322
317
 
323
- result = @builder.def_endless_singleton(*val[0],
318
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
324
319
  val[1], val[2], method_body)
325
320
 
326
- @lexer.cmdarg.pop
327
- @lexer.cond.pop
328
- @static_env.unextend
329
- @context.pop
321
+ local_pop
330
322
  @current_arg_stack.pop
323
+ @context.in_def = ctx.in_def
331
324
  }
332
325
  | backref tOP_ASGN command_rhs
333
326
  {
@@ -368,13 +361,13 @@ rule
368
361
  @lexer.command_start = false
369
362
  @pattern_variables.push
370
363
 
371
- result = @lexer.in_kwarg
372
- @lexer.in_kwarg = true
364
+ result = @context.in_kwarg
365
+ @context.in_kwarg = true
373
366
  }
374
- p_expr
367
+ p_top_expr_body
375
368
  {
376
369
  @pattern_variables.pop
377
- @lexer.in_kwarg = val[2]
370
+ @context.in_kwarg = val[2]
378
371
  result = @builder.match_pattern(val[0], val[1], val[3])
379
372
  }
380
373
  | arg kIN
@@ -383,13 +376,13 @@ rule
383
376
  @lexer.command_start = false
384
377
  @pattern_variables.push
385
378
 
386
- result = @lexer.in_kwarg
387
- @lexer.in_kwarg = true
379
+ result = @context.in_kwarg
380
+ @context.in_kwarg = true
388
381
  }
389
- p_expr
382
+ p_top_expr_body
390
383
  {
391
384
  @pattern_variables.pop
392
- @lexer.in_kwarg = val[2]
385
+ @context.in_kwarg = val[2]
393
386
  result = @builder.match_pattern_p(val[0], val[1], val[3])
394
387
  }
395
388
  | arg =tLBRACE_ARG
@@ -407,29 +400,25 @@ rule
407
400
 
408
401
  def_name: fname
409
402
  {
410
- @static_env.extend_static
411
- @lexer.cmdarg.push(false)
412
- @lexer.cond.push(false)
403
+ local_push
413
404
  @current_arg_stack.push(nil)
414
405
 
415
- result = val[0]
406
+ result = [ val[0], @context.dup ]
407
+ @context.in_def = true
416
408
  }
417
409
 
418
- defn_head: kDEF def_name
410
+ defn_head: k_def def_name
419
411
  {
420
- @context.push(:def)
421
-
422
412
  result = [ val[0], val[1] ]
423
413
  }
424
414
 
425
- defs_head: kDEF singleton dot_or_colon
415
+ defs_head: k_def singleton dot_or_colon
426
416
  {
427
417
  @lexer.state = :expr_fname
418
+ @context.in_argdef = true
428
419
  }
429
420
  def_name
430
421
  {
431
- @context.push(:defs)
432
-
433
422
  result = [ val[0], val[1], val[2], val[4] ]
434
423
  }
435
424
 
@@ -446,12 +435,13 @@ rule
446
435
 
447
436
  cmd_brace_block: tLBRACE_ARG
448
437
  {
449
- @context.push(:block)
438
+ result = @context.dup
439
+ @context.in_block = true
450
440
  }
451
441
  brace_body tRCURLY
452
442
  {
443
+ @context.in_block = val[1].in_block
453
444
  result = [ val[0], *val[2], val[3] ]
454
- @context.pop
455
445
  }
456
446
 
457
447
  fcall: operation
@@ -932,9 +922,14 @@ rule
932
922
  {
933
923
  result = @builder.logical_op(:or, val[0], val[1], val[2])
934
924
  }
935
- | kDEFINED opt_nl arg
925
+ | kDEFINED opt_nl
936
926
  {
937
- result = @builder.keyword_cmd(:defined?, val[0], nil, [ val[2] ], nil)
927
+ @context.in_defined = true
928
+ }
929
+ arg
930
+ {
931
+ @context.in_defined = false
932
+ result = @builder.keyword_cmd(:defined?, val[0], nil, [ val[3] ], nil)
938
933
  }
939
934
  | arg tEH arg opt_nl tCOLON arg
940
935
  {
@@ -943,21 +938,19 @@ rule
943
938
  }
944
939
  | defn_head f_opt_paren_args tEQL arg
945
940
  {
946
- _def_t, name_t = val[0]
941
+ def_t, (name_t, ctx) = val[0]
947
942
  endless_method_name(name_t)
948
943
 
949
- result = @builder.def_endless_method(*val[0],
944
+ result = @builder.def_endless_method(def_t, name_t,
950
945
  val[1], val[2], val[3])
951
946
 
952
- @lexer.cmdarg.pop
953
- @lexer.cond.pop
954
- @static_env.unextend
955
- @context.pop
947
+ local_pop
956
948
  @current_arg_stack.pop
949
+ @context.in_def = ctx.in_def
957
950
  }
958
951
  | defn_head f_opt_paren_args tEQL arg kRESCUE_MOD arg
959
952
  {
960
- _def_t, name_t = val[0]
953
+ def_t, (name_t, ctx) = val[0]
961
954
  endless_method_name(name_t)
962
955
 
963
956
  rescue_body = @builder.rescue_body(val[4],
@@ -966,32 +959,28 @@ rule
966
959
 
967
960
  method_body = @builder.begin_body(val[3], [ rescue_body ])
968
961
 
969
- result = @builder.def_endless_method(*val[0],
962
+ result = @builder.def_endless_method(def_t, name_t,
970
963
  val[1], val[2], method_body)
971
964
 
972
- @lexer.cmdarg.pop
973
- @lexer.cond.pop
974
- @static_env.unextend
975
- @context.pop
965
+ local_pop
976
966
  @current_arg_stack.pop
967
+ @context.in_def = ctx.in_def
977
968
  }
978
969
  | defs_head f_opt_paren_args tEQL arg
979
970
  {
980
- _def_t, _recv, _dot_t, name_t = val[0]
971
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
981
972
  endless_method_name(name_t)
982
973
 
983
- result = @builder.def_endless_singleton(*val[0],
974
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
984
975
  val[1], val[2], val[3])
985
976
 
986
- @lexer.cmdarg.pop
987
- @lexer.cond.pop
988
- @static_env.unextend
989
- @context.pop
977
+ local_pop
990
978
  @current_arg_stack.pop
979
+ @context.in_def = ctx.in_def
991
980
  }
992
981
  | defs_head f_opt_paren_args tEQL arg kRESCUE_MOD arg
993
982
  {
994
- _def_t, _recv, _dot_t, name_t = val[0]
983
+ def_t, recv, dot_t, (name_t, ctx) = val[0]
995
984
  endless_method_name(name_t)
996
985
 
997
986
  rescue_body = @builder.rescue_body(val[4],
@@ -1000,14 +989,12 @@ rule
1000
989
 
1001
990
  method_body = @builder.begin_body(val[3], [ rescue_body ])
1002
991
 
1003
- result = @builder.def_endless_singleton(*val[0],
992
+ result = @builder.def_endless_singleton(def_t, recv, dot_t, name_t,
1004
993
  val[1], val[2], method_body)
1005
994
 
1006
- @lexer.cmdarg.pop
1007
- @lexer.cond.pop
1008
- @static_env.unextend
1009
- @context.pop
995
+ local_pop
1010
996
  @current_arg_stack.pop
997
+ @context.in_def = ctx.in_def
1011
998
  }
1012
999
  | primary
1013
1000
 
@@ -1159,6 +1146,14 @@ rule
1159
1146
  {
1160
1147
  result = @builder.block_pass(val[0], val[1])
1161
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
+ }
1162
1157
 
1163
1158
  opt_block_arg: tCOMMA block_arg
1164
1159
  {
@@ -1281,10 +1276,15 @@ rule
1281
1276
  {
1282
1277
  result = @builder.keyword_cmd(:yield, val[0])
1283
1278
  }
1284
- | kDEFINED opt_nl tLPAREN2 expr rparen
1279
+ | kDEFINED opt_nl tLPAREN2
1285
1280
  {
1281
+ @context.in_defined = true
1282
+ }
1283
+ expr rparen
1284
+ {
1285
+ @context.in_defined = false
1286
1286
  result = @builder.keyword_cmd(:defined?, val[0],
1287
- val[2], [ val[3] ], val[4])
1287
+ val[2], [ val[4] ], val[5])
1288
1288
  }
1289
1289
  | kNOT tLPAREN2 expr rparen
1290
1290
  {
@@ -1360,86 +1360,77 @@ rule
1360
1360
  {
1361
1361
  result = @builder.for(val[0], val[1], val[2], *val[3], val[4], val[5])
1362
1362
  }
1363
- | kCLASS cpath superclass
1363
+ | k_class cpath superclass
1364
1364
  {
1365
- @static_env.extend_static
1366
- @lexer.cmdarg.push(false)
1367
- @lexer.cond.push(false)
1368
- @context.push(:class)
1365
+ @context.in_class = true
1366
+ local_push
1369
1367
  }
1370
1368
  bodystmt kEND
1371
1369
  {
1372
- unless @context.class_definition_allowed?
1373
- 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
1374
1373
  end
1375
-
1376
1374
  lt_t, superclass = val[2]
1377
- result = @builder.def_class(val[0], val[1],
1375
+ result = @builder.def_class(k_class, val[1],
1378
1376
  lt_t, superclass,
1379
1377
  val[4], val[5])
1380
1378
 
1381
- @lexer.cmdarg.pop
1382
- @lexer.cond.pop
1383
- @static_env.unextend
1384
- @context.pop
1379
+ local_pop
1380
+ @context.in_class = ctx.in_class
1385
1381
  }
1386
- | kCLASS tLSHFT expr term
1382
+ | k_class tLSHFT expr term
1387
1383
  {
1388
- @static_env.extend_static
1389
- @lexer.cmdarg.push(false)
1390
- @lexer.cond.push(false)
1391
- @context.push(:sclass)
1384
+ @context.in_def = false
1385
+ @context.in_class = false
1386
+ local_push
1392
1387
  }
1393
1388
  bodystmt kEND
1394
1389
  {
1395
- 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],
1396
1392
  val[5], val[6])
1397
1393
 
1398
- @lexer.cmdarg.pop
1399
- @lexer.cond.pop
1400
- @static_env.unextend
1401
- @context.pop
1394
+ local_pop
1395
+ @context.in_def = ctx.in_def
1396
+ @context.in_class = ctx.in_class
1402
1397
  }
1403
- | kMODULE cpath
1398
+ | k_module cpath
1404
1399
  {
1405
- @static_env.extend_static
1406
- @lexer.cmdarg.push(false)
1407
- @context.push(:module)
1400
+ @context.in_class = true
1401
+ local_push
1408
1402
  }
1409
1403
  bodystmt kEND
1410
1404
  {
1411
- unless @context.module_definition_allowed?
1412
- 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
1413
1408
  end
1414
-
1415
- result = @builder.def_module(val[0], val[1],
1409
+ result = @builder.def_module(k_mod, val[1],
1416
1410
  val[3], val[4])
1417
1411
 
1418
- @lexer.cmdarg.pop
1419
- @static_env.unextend
1420
- @context.pop
1412
+ local_pop
1413
+ @context.in_class = ctx.in_class
1421
1414
  }
1422
1415
  | defn_head f_arglist bodystmt kEND
1423
1416
  {
1424
- 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],
1425
1419
  val[2], val[3])
1426
1420
 
1427
- @lexer.cmdarg.pop
1428
- @lexer.cond.pop
1429
- @static_env.unextend
1430
- @context.pop
1421
+ local_pop
1431
1422
  @current_arg_stack.pop
1423
+ @context.in_def = ctx.in_def
1432
1424
  }
1433
1425
  | defs_head f_arglist bodystmt kEND
1434
1426
  {
1435
- 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],
1436
1429
  val[2], val[3])
1437
1430
 
1438
- @lexer.cmdarg.pop
1439
- @lexer.cond.pop
1440
- @static_env.unextend
1441
- @context.pop
1431
+ local_pop
1442
1432
  @current_arg_stack.pop
1433
+ @context.in_def = ctx.in_def
1443
1434
  }
1444
1435
  | kBREAK
1445
1436
  {
@@ -1464,9 +1455,25 @@ rule
1464
1455
 
1465
1456
  primary_value: primary
1466
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
+
1467
1474
  k_return: kRETURN
1468
1475
  {
1469
- if @context.in_class?
1476
+ if @context.in_class && !@context.in_def && !(context.in_block || context.in_lambda)
1470
1477
  diagnostic :error, :invalid_return, nil, val[0]
1471
1478
  end
1472
1479
  }
@@ -1552,6 +1559,15 @@ rule
1552
1559
  f_any_kwrest: f_kwrest
1553
1560
  | f_no_kwarg
1554
1561
 
1562
+
1563
+ f_eq: {
1564
+ @context.in_argdef = false
1565
+ }
1566
+ tEQL
1567
+ {
1568
+ result = val[1]
1569
+ }
1570
+
1555
1571
  block_args_tail: f_block_kwarg tCOMMA f_kwrest opt_f_block_arg
1556
1572
  {
1557
1573
  result = val[0].concat(val[2]).concat(val[3])
@@ -1681,12 +1697,14 @@ opt_block_args_tail:
1681
1697
  {
1682
1698
  @max_numparam_stack.has_ordinary_params!
1683
1699
  @current_arg_stack.set(nil)
1700
+ @context.in_argdef = false
1684
1701
  result = @builder.args(val[0], val[1], val[2])
1685
1702
  }
1686
1703
  | tPIPE block_param opt_bv_decl tPIPE
1687
1704
  {
1688
1705
  @max_numparam_stack.has_ordinary_params!
1689
1706
  @current_arg_stack.set(nil)
1707
+ @context.in_argdef = false
1690
1708
  result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1691
1709
  }
1692
1710
 
@@ -1718,12 +1736,12 @@ opt_block_args_tail:
1718
1736
  lambda: tLAMBDA
1719
1737
  {
1720
1738
  @static_env.extend_dynamic
1721
- @max_numparam_stack.push
1722
- @context.push(:lambda)
1739
+ @max_numparam_stack.push(static: false)
1740
+ result = @context.dup
1741
+ @context.in_lambda = true
1723
1742
  }
1724
1743
  f_larglist
1725
1744
  {
1726
- @context.pop
1727
1745
  @lexer.cmdarg.push(false)
1728
1746
  }
1729
1747
  lambda_body
@@ -1735,6 +1753,7 @@ opt_block_args_tail:
1735
1753
  @max_numparam_stack.pop
1736
1754
  @static_env.unextend
1737
1755
  @lexer.cmdarg.pop
1756
+ @context.in_lambda = val[1].in_lambda
1738
1757
 
1739
1758
  result = @builder.block(lambda_call,
1740
1759
  begin_t, args, body, end_t)
@@ -1742,11 +1761,13 @@ opt_block_args_tail:
1742
1761
 
1743
1762
  f_larglist: tLPAREN2 f_args opt_bv_decl tRPAREN
1744
1763
  {
1764
+ @context.in_argdef = false
1745
1765
  @max_numparam_stack.has_ordinary_params!
1746
1766
  result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1747
1767
  }
1748
1768
  | f_args
1749
1769
  {
1770
+ @context.in_argdef = false
1750
1771
  if val[0].any?
1751
1772
  @max_numparam_stack.has_ordinary_params!
1752
1773
  end
@@ -1755,31 +1776,34 @@ opt_block_args_tail:
1755
1776
 
1756
1777
  lambda_body: tLAMBEG
1757
1778
  {
1758
- @context.push(:lambda)
1779
+ result = @context.dup
1780
+ @context.in_lambda = true
1759
1781
  }
1760
1782
  compstmt tRCURLY
1761
1783
  {
1784
+ @context.in_lambda = val[1].in_lambda
1762
1785
  result = [ val[0], val[2], val[3] ]
1763
- @context.pop
1764
1786
  }
1765
1787
  | kDO_LAMBDA
1766
1788
  {
1767
- @context.push(:lambda)
1789
+ result = @context.dup
1790
+ @context.in_lambda = true
1768
1791
  }
1769
1792
  bodystmt kEND
1770
1793
  {
1794
+ @context.in_lambda = val[1].in_lambda
1771
1795
  result = [ val[0], val[2], val[3] ]
1772
- @context.pop
1773
1796
  }
1774
1797
 
1775
1798
  do_block: kDO_BLOCK
1776
1799
  {
1777
- @context.push(:block)
1800
+ result = @context.dup
1801
+ @context.in_block = true
1778
1802
  }
1779
1803
  do_body kEND
1780
1804
  {
1805
+ @context.in_block = val[1].in_block
1781
1806
  result = [ val[0], *val[2], val[3] ]
1782
- @context.pop
1783
1807
  }
1784
1808
 
1785
1809
  block_call: command do_block
@@ -1865,26 +1889,28 @@ opt_block_args_tail:
1865
1889
 
1866
1890
  brace_block: tLCURLY
1867
1891
  {
1868
- @context.push(:block)
1892
+ result = @context.dup
1893
+ @context.in_block = true
1869
1894
  }
1870
1895
  brace_body tRCURLY
1871
1896
  {
1897
+ @context.in_block = val[1].in_block
1872
1898
  result = [ val[0], *val[2], val[3] ]
1873
- @context.pop
1874
1899
  }
1875
1900
  | kDO
1876
1901
  {
1877
- @context.push(:block)
1902
+ result = @context.dup
1903
+ @context.in_block = true
1878
1904
  }
1879
1905
  do_body kEND
1880
1906
  {
1907
+ @context.in_block = val[1].in_block
1881
1908
  result = [ val[0], *val[2], val[3] ]
1882
- @context.pop
1883
1909
  }
1884
1910
 
1885
1911
  brace_body: {
1886
1912
  @static_env.extend_dynamic
1887
- @max_numparam_stack.push
1913
+ @max_numparam_stack.push(static: false)
1888
1914
  }
1889
1915
  opt_block_param compstmt
1890
1916
  {
@@ -1897,7 +1923,7 @@ opt_block_args_tail:
1897
1923
 
1898
1924
  do_body: {
1899
1925
  @static_env.extend_dynamic
1900
- @max_numparam_stack.push
1926
+ @max_numparam_stack.push(static: false)
1901
1927
  }
1902
1928
  {
1903
1929
  @lexer.cmdarg.push(false)
@@ -1931,14 +1957,14 @@ opt_block_args_tail:
1931
1957
  @pattern_variables.push
1932
1958
  @pattern_hash_keys.push
1933
1959
 
1934
- result = @lexer.in_kwarg
1935
- @lexer.in_kwarg = true
1960
+ result = @context.in_kwarg
1961
+ @context.in_kwarg = true
1936
1962
  }
1937
1963
  p_top_expr then
1938
1964
  {
1939
1965
  @pattern_variables.pop
1940
1966
  @pattern_hash_keys.pop
1941
- @lexer.in_kwarg = val[1]
1967
+ @context.in_kwarg = val[1]
1942
1968
  }
1943
1969
  compstmt p_cases
1944
1970
  {
@@ -2080,13 +2106,13 @@ opt_block_args_tail:
2080
2106
  | tLBRACE
2081
2107
  {
2082
2108
  @pattern_hash_keys.push
2083
- result = @lexer.in_kwarg
2084
- @lexer.in_kwarg = false
2109
+ result = @context.in_kwarg
2110
+ @context.in_kwarg = false
2085
2111
  }
2086
2112
  p_kwargs rbrace
2087
2113
  {
2088
2114
  @pattern_hash_keys.pop
2089
- @lexer.in_kwarg = val[1]
2115
+ @context.in_kwarg = val[1]
2090
2116
  result = @builder.hash_pattern(val[0], val[2], val[3])
2091
2117
  }
2092
2118
  | tLBRACE rbrace
@@ -2115,23 +2141,13 @@ opt_block_args_tail:
2115
2141
  {
2116
2142
  result = [ *val[0], val[1] ]
2117
2143
  }
2118
- | p_args_head tSTAR tIDENTIFIER
2119
- {
2120
- match_rest = @builder.match_rest(val[1], val[2])
2121
- result = [ *val[0], match_rest ]
2122
- }
2123
- | p_args_head tSTAR tIDENTIFIER tCOMMA p_args_post
2124
- {
2125
- match_rest = @builder.match_rest(val[1], val[2])
2126
- result = [ *val[0], match_rest, *val[4] ]
2127
- }
2128
- | p_args_head tSTAR
2144
+ | p_args_head p_rest
2129
2145
  {
2130
- result = [ *val[0], @builder.match_rest(val[1]) ]
2146
+ result = [ *val[0], val[1] ]
2131
2147
  }
2132
- | p_args_head tSTAR tCOMMA p_args_post
2148
+ | p_args_head p_rest tCOMMA p_args_post
2133
2149
  {
2134
- result = [ *val[0], @builder.match_rest(val[1]), *val[3] ]
2150
+ result = [ *val[0], val[1], *val[3] ]
2135
2151
  }
2136
2152
  | p_args_tail
2137
2153
 
@@ -2170,6 +2186,11 @@ opt_block_args_tail:
2170
2186
  {
2171
2187
  result = @builder.match_rest(val[0], val[1])
2172
2188
  }
2189
+ | tSTAR nonlocal_var
2190
+ {
2191
+ non_lvar = @builder.accessible(val[1])
2192
+ result = @builder.match_rest(val[0], non_lvar)
2193
+ }
2173
2194
  | tSTAR
2174
2195
  {
2175
2196
  result = @builder.match_rest(val[0])
@@ -2292,7 +2313,12 @@ opt_block_args_tail:
2292
2313
 
2293
2314
  p_variable: tIDENTIFIER
2294
2315
  {
2295
- result = @builder.match_var(val[0])
2316
+ result = @builder.assignable(@builder.match_var(val[0]))
2317
+ }
2318
+ | nonlocal_var
2319
+ {
2320
+ non_lvar = @builder.accessible(val[0])
2321
+ result = @builder.assignable(@builder.match_var(non_lvar))
2296
2322
  }
2297
2323
 
2298
2324
  p_var_ref: tCARET tIDENTIFIER
@@ -2305,11 +2331,19 @@ opt_block_args_tail:
2305
2331
  lvar = @builder.accessible(@builder.ident(val[1]))
2306
2332
  result = @builder.pin(val[0], lvar)
2307
2333
  }
2334
+
2335
+ | tCARET nonlocal_var
2336
+ {
2337
+ non_lvar = @builder.accessible(val[1])
2338
+ result = @builder.pin(val[0], non_lvar)
2339
+ }
2340
+
2308
2341
  p_expr_ref: tCARET tLPAREN expr_value tRPAREN
2309
2342
  {
2310
2343
  expr = @builder.begin(val[1], val[2], val[3])
2311
2344
  result = @builder.pin(val[0], expr)
2312
2345
  }
2346
+
2313
2347
  p_const: tCOLON3 cname
2314
2348
  {
2315
2349
  result = @builder.const_global(val[0], val[1])
@@ -2581,6 +2615,19 @@ regexp_contents: # nothing
2581
2615
  result = @builder.complex(val[0])
2582
2616
  }
2583
2617
 
2618
+ nonlocal_var: tIVAR
2619
+ {
2620
+ result = @builder.ivar(val[0])
2621
+ }
2622
+ | tGVAR
2623
+ {
2624
+ result = @builder.gvar(val[0])
2625
+ }
2626
+ | tCVAR
2627
+ {
2628
+ result = @builder.cvar(val[0])
2629
+ }
2630
+
2584
2631
  user_variable: tIDENTIFIER
2585
2632
  {
2586
2633
  result = @builder.ident(val[0])
@@ -2633,46 +2680,6 @@ keyword_variable: kNIL
2633
2680
 
2634
2681
  var_ref: user_variable
2635
2682
  {
2636
- if (node = val[0]) && node.type == :ident
2637
- name = node.children[0]
2638
-
2639
- if name =~ /\A_[1-9]\z/ && !static_env.declared?(name) && context.in_dynamic_block?
2640
- # definitely an implicit param
2641
- location = node.loc.expression
2642
-
2643
- if max_numparam_stack.has_ordinary_params?
2644
- diagnostic :error, :ordinary_param_defined, nil, [nil, location]
2645
- end
2646
-
2647
- raw_context = context.stack.dup
2648
- raw_max_numparam_stack = max_numparam_stack.stack.dup
2649
-
2650
- # ignore current block scope
2651
- raw_context.pop
2652
- raw_max_numparam_stack.pop
2653
-
2654
- raw_context.reverse_each do |outer_scope|
2655
- if outer_scope == :block || outer_scope == :lambda
2656
- outer_scope_has_numparams = raw_max_numparam_stack.pop > 0
2657
-
2658
- if outer_scope_has_numparams
2659
- diagnostic :error, :numparam_used_in_outer_scope, nil, [nil, location]
2660
- else
2661
- # for now it's ok, but an outer scope can also be a block
2662
- # with numparams, so we need to continue
2663
- end
2664
- else
2665
- # found an outer scope that can't have numparams
2666
- # like def/class/etc
2667
- break
2668
- end
2669
- end
2670
-
2671
- static_env.declare(name)
2672
- max_numparam_stack.register(name[1].to_i)
2673
- end
2674
- end
2675
-
2676
2683
  result = @builder.accessible(val[0])
2677
2684
  }
2678
2685
  | keyword_variable
@@ -2714,6 +2721,7 @@ keyword_variable: kNIL
2714
2721
  f_opt_paren_args: f_paren_args
2715
2722
  | none
2716
2723
  {
2724
+ @context.in_argdef = false
2717
2725
  result = @builder.args(nil, [], nil)
2718
2726
  }
2719
2727
 
@@ -2722,30 +2730,19 @@ f_opt_paren_args: f_paren_args
2722
2730
  result = @builder.args(val[0], val[1], val[2])
2723
2731
 
2724
2732
  @lexer.state = :expr_value
2725
- }
2726
- | tLPAREN2 f_arg tCOMMA args_forward rparen
2727
- {
2728
- args = [ *val[1], @builder.forward_arg(val[3]) ]
2729
- result = @builder.args(val[0], args, val[4])
2730
-
2731
- @static_env.declare_forward_args
2732
- }
2733
- | tLPAREN2 args_forward rparen
2734
- {
2735
- result = @builder.forward_only_args(val[0], val[1], val[2])
2736
- @static_env.declare_forward_args
2737
-
2738
- @lexer.state = :expr_value
2733
+ @context.in_argdef = false
2739
2734
  }
2740
2735
 
2741
2736
  f_arglist: f_paren_args
2742
2737
  | {
2743
- result = @lexer.in_kwarg
2744
- @lexer.in_kwarg = true
2738
+ result = @context.dup
2739
+ @context.in_kwarg = true
2740
+ @context.in_argdef = true
2745
2741
  }
2746
2742
  f_args term
2747
2743
  {
2748
- @lexer.in_kwarg = val[0]
2744
+ @context.in_kwarg = val[0].in_kwarg
2745
+ @context.in_argdef = false
2749
2746
  result = @builder.args(nil, val[1], nil)
2750
2747
  }
2751
2748
 
@@ -2765,6 +2762,11 @@ f_opt_paren_args: f_paren_args
2765
2762
  {
2766
2763
  result = [ val[0] ]
2767
2764
  }
2765
+ | args_forward
2766
+ {
2767
+ @static_env.declare_forward_args
2768
+ result = [ @builder.forward_arg(val[0]) ]
2769
+ }
2768
2770
 
2769
2771
  opt_args_tail: tCOMMA args_tail
2770
2772
  {
@@ -2931,6 +2933,7 @@ f_opt_paren_args: f_paren_args
2931
2933
  @max_numparam_stack.has_ordinary_params!
2932
2934
 
2933
2935
  @current_arg_stack.set(val[0][0])
2936
+ @context.in_argdef = false
2934
2937
 
2935
2938
  result = val[0]
2936
2939
  }
@@ -2938,20 +2941,24 @@ f_opt_paren_args: f_paren_args
2938
2941
  f_kw: f_label arg_value
2939
2942
  {
2940
2943
  @current_arg_stack.set(nil)
2944
+ @context.in_argdef = true
2941
2945
  result = @builder.kwoptarg(val[0], val[1])
2942
2946
  }
2943
2947
  | f_label
2944
2948
  {
2945
2949
  @current_arg_stack.set(nil)
2950
+ @context.in_argdef = true
2946
2951
  result = @builder.kwarg(val[0])
2947
2952
  }
2948
2953
 
2949
2954
  f_block_kw: f_label primary_value
2950
2955
  {
2956
+ @context.in_argdef = true
2951
2957
  result = @builder.kwoptarg(val[0], val[1])
2952
2958
  }
2953
2959
  | f_label
2954
2960
  {
2961
+ @context.in_argdef = true
2955
2962
  result = @builder.kwarg(val[0])
2956
2963
  }
2957
2964
 
@@ -2991,15 +2998,17 @@ f_opt_paren_args: f_paren_args
2991
2998
  result = [ @builder.kwrestarg(val[0]) ]
2992
2999
  }
2993
3000
 
2994
- f_opt: f_arg_asgn tEQL arg_value
3001
+ f_opt: f_arg_asgn f_eq arg_value
2995
3002
  {
2996
3003
  @current_arg_stack.set(0)
3004
+ @context.in_argdef = true
2997
3005
  result = @builder.optarg(val[0], val[1], val[2])
2998
3006
  }
2999
3007
 
3000
- f_block_opt: f_arg_asgn tEQL primary_value
3008
+ f_block_opt: f_arg_asgn f_eq primary_value
3001
3009
  {
3002
3010
  @current_arg_stack.set(0)
3011
+ @context.in_argdef = true
3003
3012
  result = @builder.optarg(val[0], val[1], val[2])
3004
3013
  }
3005
3014
 
@@ -3042,6 +3051,12 @@ f_opt_paren_args: f_paren_args
3042
3051
 
3043
3052
  result = @builder.blockarg(val[0], val[1])
3044
3053
  }
3054
+ | blkarg_mark
3055
+ {
3056
+ @static_env.declare_anonymous_blockarg
3057
+
3058
+ result = @builder.blockarg(val[0], nil)
3059
+ }
3045
3060
 
3046
3061
  opt_f_block_arg: tCOMMA f_block_arg
3047
3062
  {
@@ -3062,24 +3077,7 @@ f_opt_paren_args: f_paren_args
3062
3077
  {
3063
3078
  result = []
3064
3079
  }
3065
- | assoc_items trailer
3066
-
3067
- assoc_items : assoc_item
3068
- {
3069
- result = [ val[0] ]
3070
- }
3071
- | assoc_items tCOMMA assoc_item
3072
- {
3073
- result = val[0] << val[2]
3074
- }
3075
-
3076
- assoc_item: assoc
3077
- |
3078
- tIDENTIFIER
3079
- {
3080
- lvar = @builder.accessible(@builder.ident(val[0]))
3081
- result = @builder.ipair(lvar)
3082
- }
3080
+ | assocs trailer
3083
3081
 
3084
3082
  assocs: assoc
3085
3083
  {
@@ -3100,8 +3098,7 @@ f_opt_paren_args: f_paren_args
3100
3098
  }
3101
3099
  | tLABEL
3102
3100
  {
3103
- lvar = @builder.accessible(@builder.ident(val[0]))
3104
- result = @builder.ipair_keyword(lvar)
3101
+ result = @builder.pair_label(val[0])
3105
3102
  }
3106
3103
  | tSTRING_BEG string_contents tLABEL_END arg_value
3107
3104
  {
@@ -3162,10 +3159,10 @@ require 'parser/ruby-next/parser_ext'
3162
3159
 
3163
3160
  ---- inner
3164
3161
 
3165
- prepend Parser::NextExt
3162
+ prepend Parser::NextExt
3166
3163
 
3167
3164
  def version
3168
- 31
3165
+ 32
3169
3166
  end
3170
3167
 
3171
3168
  def default_encoding
@@ -3177,3 +3174,59 @@ require 'parser/ruby-next/parser_ext'
3177
3174
  diagnostic :error, :endless_setter, nil, name_t
3178
3175
  end
3179
3176
  end
3177
+
3178
+ def local_push
3179
+ @static_env.extend_static
3180
+ @lexer.cmdarg.push(false)
3181
+ @lexer.cond.push(false)
3182
+ @max_numparam_stack.push(static: true)
3183
+ end
3184
+
3185
+ def local_pop
3186
+ @static_env.unextend
3187
+ @lexer.cmdarg.pop
3188
+ @lexer.cond.pop
3189
+ @max_numparam_stack.pop
3190
+ end
3191
+
3192
+ def try_declare_numparam(node)
3193
+ name = node.children[0]
3194
+
3195
+ if name =~ /\A_[1-9]\z/ && !static_env.declared?(name) && @context.in_dynamic_block?
3196
+ # definitely an implicit param
3197
+ location = node.loc.expression
3198
+
3199
+ if max_numparam_stack.has_ordinary_params?
3200
+ diagnostic :error, :ordinary_param_defined, nil, [nil, location]
3201
+ end
3202
+
3203
+ raw_max_numparam_stack = max_numparam_stack.stack.dup
3204
+ # ignore current block scope
3205
+ raw_max_numparam_stack.pop
3206
+
3207
+ raw_max_numparam_stack.reverse_each do |outer_scope|
3208
+ if outer_scope[:static]
3209
+ # found an outer scope that can't have numparams
3210
+ # like def/class/etc
3211
+ break
3212
+ else
3213
+ outer_scope_has_numparams = outer_scope[:value] > 0
3214
+
3215
+ if outer_scope_has_numparams
3216
+ diagnostic :error, :numparam_used_in_outer_scope, nil, [nil, location]
3217
+ else
3218
+ # for now it's ok, but an outer scope can also be a block
3219
+ # like proc { _1; proc { proc { proc { _2 }} }}
3220
+ # with numparams, so we need to continue
3221
+ end
3222
+ end
3223
+ end
3224
+
3225
+ static_env.declare(name)
3226
+ max_numparam_stack.register(name[1].to_i)
3227
+
3228
+ true
3229
+ else
3230
+ false
3231
+ end
3232
+ end