ruby_parser 3.6.6 → 3.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -91,7 +91,7 @@ class RPStringScanner < StringScanner
91
91
  end
92
92
 
93
93
  module RubyParserStuff
94
- VERSION = "3.6.6" unless constants.include? "VERSION" # SIGH
94
+ VERSION = "3.7.0" unless constants.include? "VERSION" # SIGH
95
95
 
96
96
  attr_accessor :lexer, :in_def, :in_single, :file
97
97
  attr_reader :env, :comments
@@ -1319,6 +1319,10 @@ module RubyParserStuff
1319
1319
  end
1320
1320
  end
1321
1321
 
1322
+ class Ruby22Parser < Racc::Parser
1323
+ include RubyParserStuff
1324
+ end
1325
+
1322
1326
  class Ruby21Parser < Racc::Parser
1323
1327
  include RubyParserStuff
1324
1328
  end
@@ -1348,11 +1352,12 @@ class RubyParser
1348
1352
  @p19 = Ruby19Parser.new
1349
1353
  @p20 = Ruby20Parser.new
1350
1354
  @p21 = Ruby21Parser.new
1355
+ @p22 = Ruby22Parser.new
1351
1356
  end
1352
1357
 
1353
1358
  def process s, f = "(string)", t = 10
1354
1359
  e = nil
1355
- [@p21, @p20, @p19, @p18].each do |parser|
1360
+ [@p22, @p21, @p20, @p19, @p18].each do |parser|
1356
1361
  begin
1357
1362
  return parser.process s, f, t
1358
1363
  rescue Racc::ParseError, RubyParser::SyntaxError => exc
@@ -1369,6 +1374,7 @@ class RubyParser
1369
1374
  @p19.reset
1370
1375
  @p20.reset
1371
1376
  @p21.reset
1377
+ @p22.reset
1372
1378
  end
1373
1379
 
1374
1380
  def self.for_current_ruby
@@ -1381,6 +1387,8 @@ class RubyParser
1381
1387
  Ruby20Parser.new
1382
1388
  when /^2.1/ then
1383
1389
  Ruby21Parser.new
1390
+ when /^2.2/ then
1391
+ Ruby22Parser.new
1384
1392
  else
1385
1393
  raise "unrecognized RUBY_VERSION #{RUBY_VERSION}"
1386
1394
  end
@@ -518,10 +518,10 @@ class TestRubyLexer < Minitest::Test
518
518
  def test_yylex_const_call_same_name
519
519
  assert_lex("X = a { }; b { f :c }",
520
520
  s(:block,
521
- s(:cdecl, :X, s(:iter, s(:call, nil, :a), s(:args))),
521
+ s(:cdecl, :X, s(:iter, s(:call, nil, :a), 0)),
522
522
  s(:iter,
523
523
  s(:call, nil, :b),
524
- s(:args),
524
+ 0,
525
525
  s(:call, nil, :f, s(:lit, :c)))),
526
526
 
527
527
  :tCONSTANT, "X", :expr_cmdarg, 0, 0,
@@ -539,10 +539,10 @@ class TestRubyLexer < Minitest::Test
539
539
 
540
540
  assert_lex("X = a { }; b { X :c }",
541
541
  s(:block,
542
- s(:cdecl, :X, s(:iter, s(:call, nil, :a), s(:args))),
542
+ s(:cdecl, :X, s(:iter, s(:call, nil, :a), 0)),
543
543
  s(:iter,
544
544
  s(:call, nil, :b),
545
- s(:args),
545
+ 0,
546
546
  s(:call, nil, :X, s(:lit, :c)))),
547
547
 
548
548
  :tCONSTANT, "X", :expr_cmdarg, 0, 0,
@@ -2294,10 +2294,12 @@ class TestRubyLexer < Minitest::Test
2294
2294
  end
2295
2295
 
2296
2296
  def test_yylex_string_double_pound_dollar_bad
2297
+ skip if Ruby18Parser === lexer.parser
2298
+
2297
2299
  assert_lex3('"#$%"', nil,
2298
2300
 
2299
2301
  :tSTRING_BEG, "\"", :expr_beg,
2300
- :tSTRING_CONTENT, "#$%", :expr_beg,
2302
+ :tSTRING_CONTENT, '#$%', :expr_beg,
2301
2303
  :tSTRING_END, "\"", :expr_end)
2302
2304
  end
2303
2305
 
@@ -2641,13 +2643,13 @@ class TestRubyLexer < Minitest::Test
2641
2643
  end
2642
2644
 
2643
2645
  def test_yylex_hash_colon_quoted_22
2644
- skip "Waiting for 2.2 parser"
2646
+ setup_lexer_class Ruby22Parser
2645
2647
 
2646
2648
  assert_lex("{'a':1}",
2647
2649
  s(:hash, s(:lit, :a), s(:lit, 1)),
2648
2650
 
2649
2651
  :tLBRACE, "{", :expr_beg, 0, 1,
2650
- :tSYMBOL, "a", :expr_end, 0, 1,
2652
+ :tLABEL, "a", :expr_end, 0, 1,
2651
2653
  :tINTEGER, 1, :expr_end, 0, 1,
2652
2654
  :tRCURLY, "}", :expr_endarg, 0, 0)
2653
2655
  end
@@ -121,7 +121,7 @@ module TestRubyParserShared
121
121
  rb = "a.b (1) {c}"
122
122
  pt = s(:iter,
123
123
  s(:call, s(:call, nil, :a), :b, s(:lit, 1)),
124
- s(:args),
124
+ 0,
125
125
  s(:call, nil, :c))
126
126
 
127
127
  assert_parse rb, pt
@@ -131,7 +131,7 @@ module TestRubyParserShared
131
131
  rb = "a::b (1) {c}"
132
132
  pt = s(:iter,
133
133
  s(:call, s(:call, nil, :a), :b, s(:lit, 1)),
134
- s(:args),
134
+ 0,
135
135
  s(:call, nil, :c))
136
136
 
137
137
  assert_parse rb, pt
@@ -226,7 +226,7 @@ module TestRubyParserShared
226
226
  rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
227
227
  pt = s(:iter,
228
228
  s(:call, nil, :a),
229
- s(:args),
229
+ 0,
230
230
  s(:block,
231
231
  s(:lasgn, :v, s(:nil)),
232
232
  s(:rescue,
@@ -942,6 +942,10 @@ module TestRubyParserShared
942
942
  Ruby21Parser === self.processor
943
943
  end
944
944
 
945
+ def ruby22
946
+ Ruby22Parser === self.processor
947
+ end
948
+
945
949
  def test_bug_comma
946
950
  val = if ruby18 then
947
951
  s(:lit, 100)
@@ -997,7 +1001,7 @@ module TestRubyParserShared
997
1001
  rb = "not(a)"
998
1002
  pt = if ruby18 then
999
1003
  s(:not, s(:call, nil, :a))
1000
- elsif ruby19 or ruby20 or ruby21 then
1004
+ elsif ruby19 or ruby20 or ruby21 or ruby22 then
1001
1005
  s(:call, s(:call, nil, :a), :"!")
1002
1006
  else
1003
1007
  raise "wtf"
@@ -1008,7 +1012,7 @@ module TestRubyParserShared
1008
1012
 
1009
1013
  def test_pipe_space
1010
1014
  rb = "a.b do | | end"
1011
- pt = s(:iter, s(:call, s(:call, nil, :a), :b), 0)
1015
+ pt = s(:iter, s(:call, s(:call, nil, :a), :b), s(:args))
1012
1016
 
1013
1017
  assert_parse rb, pt
1014
1018
  end
@@ -1591,7 +1595,7 @@ module TestRubyParserShared
1591
1595
  end
1592
1596
  end
1593
1597
 
1594
- module TestRubyParserShared19to21
1598
+ module TestRubyParserShared19to22
1595
1599
  def test_aref_args_lit_assocs
1596
1600
  rb = "[1, 2 => 3]"
1597
1601
  pt = s(:array, s(:lit, 1), s(:hash, s(:lit, 2), s(:lit, 3)))
@@ -1610,7 +1614,7 @@ module TestRubyParserShared19to21
1610
1614
  rb = "a.b c do end.d"
1611
1615
  pt = s(:call,
1612
1616
  s(:iter,
1613
- s(:call, s(:call, nil, :a), :b, s(:call, nil, :c)), s(:args)),
1617
+ s(:call, s(:call, nil, :a), :b, s(:call, nil, :c)), 0),
1614
1618
  :d)
1615
1619
 
1616
1620
  assert_parse rb, pt
@@ -1620,7 +1624,7 @@ module TestRubyParserShared19to21
1620
1624
  rb = "a.b c do end::d"
1621
1625
  pt = s(:call,
1622
1626
  s(:iter,
1623
- s(:call, s(:call, nil, :a), :b, s(:call, nil, :c)), s(:args)),
1627
+ s(:call, s(:call, nil, :a), :b, s(:call, nil, :c)), 0),
1624
1628
  :d)
1625
1629
 
1626
1630
  assert_parse rb, pt
@@ -1629,7 +1633,7 @@ module TestRubyParserShared19to21
1629
1633
  def test_block_command_operation_dot
1630
1634
  rb = "a :b do end.c :d"
1631
1635
  pt = s(:call,
1632
- s(:iter, s(:call, nil, :a, s(:lit, :b)), s(:args)),
1636
+ s(:iter, s(:call, nil, :a, s(:lit, :b)), 0),
1633
1637
  :c,
1634
1638
  s(:lit, :d))
1635
1639
 
@@ -1639,7 +1643,7 @@ module TestRubyParserShared19to21
1639
1643
  def test_block_command_operation_colon
1640
1644
  rb = "a :b do end::c :d"
1641
1645
  pt = s(:call,
1642
- s(:iter, s(:call, nil, :a, s(:lit, :b)), s(:args)),
1646
+ s(:iter, s(:call, nil, :a, s(:lit, :b)), 0),
1643
1647
  :c,
1644
1648
  s(:lit, :d))
1645
1649
 
@@ -1707,6 +1711,17 @@ module TestRubyParserShared19to21
1707
1711
  assert_parse rb, pt
1708
1712
  end
1709
1713
 
1714
+ def test_bug_187
1715
+ rb = "private def f\na.b do end\nend"
1716
+ pt = s(:call,
1717
+ nil,
1718
+ :private,
1719
+ s(:defn, :f, s(:args),
1720
+ s(:iter, s(:call, s(:call, nil, :a), :b), 0)))
1721
+
1722
+ assert_parse rb, pt
1723
+ end
1724
+
1710
1725
  def test_defn_opt_reg
1711
1726
  rb = "def f(a=nil, b) end"
1712
1727
  pt = s(:defn, :f, s(:args, s(:lasgn, :a, s(:nil)), :b), s(:nil))
@@ -2006,7 +2021,7 @@ module TestRubyParserShared19to21
2006
2021
 
2007
2022
  def test_do_lambda
2008
2023
  rb = "->() do end"
2009
- pt = s(:iter, s(:call, nil, :lambda), 0)
2024
+ pt = s(:iter, s(:call, nil, :lambda), s(:args))
2010
2025
 
2011
2026
  assert_parse rb, pt
2012
2027
  end
@@ -2129,9 +2144,26 @@ module TestRubyParserShared19to21
2129
2144
 
2130
2145
  assert_parse rb, pt
2131
2146
  end
2147
+
2148
+ def test_multiline_hash_declaration
2149
+ pt = s(:call, nil, :f, s(:hash, s(:lit, :state), s(:hash)))
2150
+
2151
+ assert_parse "f(state: {})", pt
2152
+ assert_parse "f(state: {\n})", pt
2153
+ assert_parse "f(state:\n {\n})", pt
2154
+ end
2132
2155
  end
2133
2156
 
2134
- module TestRubyParserShared20to21
2157
+ module TestRubyParserShared20to22
2158
+ def test_defs_kwarg
2159
+ skip "not yet"
2160
+
2161
+ rb = "def self.a b: 1\nend"
2162
+ pt = s(:defs, s(:self), :a, s(:args, s(:kwarg, :b, s(:lit, 1))), s(:nil))
2163
+
2164
+ assert_parse rb, pt
2165
+ end
2166
+
2135
2167
  def test_defn_kwarg_kwsplat
2136
2168
  rb = "def a(b: 1, **c) end"
2137
2169
  pt = s(:defn, :a, s(:args, s(:kwarg, :b, s(:lit, 1)), :"**c"), s(:nil))
@@ -2442,7 +2474,7 @@ end
2442
2474
 
2443
2475
  class TestRuby19Parser < RubyParserTestCase
2444
2476
  include TestRubyParserShared
2445
- include TestRubyParserShared19to21
2477
+ include TestRubyParserShared19to22
2446
2478
 
2447
2479
  def setup
2448
2480
  super
@@ -2681,7 +2713,7 @@ class TestRuby19Parser < RubyParserTestCase
2681
2713
  s(:lit, :a),
2682
2714
  s(:iter,
2683
2715
  s(:call, nil, :lambda),
2684
- s(:args),
2716
+ 0,
2685
2717
  s(:if, s(:call, nil, :b), s(:call, nil, :c), s(:call, nil, :d))),
2686
2718
 
2687
2719
  s(:lit, :e),
@@ -2690,12 +2722,14 @@ class TestRuby19Parser < RubyParserTestCase
2690
2722
  assert_parse rb, pt
2691
2723
  end
2692
2724
 
2693
- # def test_pipe_semicolon # HACK
2694
- # rb = "a.b do | ; c | end"
2695
- # pt = s(:iter, s(:call, s(:call, nil, :a), :b), 0)
2696
- #
2697
- # assert_parse rb, pt
2698
- # end
2725
+ def test_pipe_semicolon
2726
+ skip "not yet"
2727
+
2728
+ rb = "a.b do | ; c | end"
2729
+ pt = s(:iter, s(:call, s(:call, nil, :a), :b), 0)
2730
+
2731
+ assert_parse rb, pt
2732
+ end
2699
2733
 
2700
2734
  def test_wtf
2701
2735
  # lambda -> f_larglist lambda_body
@@ -2799,12 +2833,14 @@ class TestRuby19Parser < RubyParserTestCase
2799
2833
  assert_parse rb, pt
2800
2834
  end
2801
2835
 
2802
- # def test_kill_me5
2803
- # rb = "f ->() { g do end }"
2804
- # pt = 42
2805
- #
2806
- # assert_parse rb, pt
2807
- # end
2836
+ def test_kill_me5
2837
+ skip "not yet"
2838
+
2839
+ rb = "f ->() { g do end }"
2840
+ pt = 42
2841
+
2842
+ assert_parse rb, pt
2843
+ end
2808
2844
 
2809
2845
  def test_iter_args_4
2810
2846
  rb = "f { |a, *b, c| }"
@@ -2814,6 +2850,8 @@ class TestRuby19Parser < RubyParserTestCase
2814
2850
  end
2815
2851
 
2816
2852
  def test_iter_args_5
2853
+ skip "not yet"
2854
+
2817
2855
  rb = "f { |a, &b| }"
2818
2856
  pt = s(:iter, s(:call, nil, :f), s(:args, :a, :"&b"))
2819
2857
 
@@ -3013,13 +3051,21 @@ class TestRuby19Parser < RubyParserTestCase
3013
3051
  end
3014
3052
 
3015
3053
  def test_lambda_do_vs_brace
3016
- pt = s(:call, nil, :f, s(:iter, s(:call, nil, :lambda), 0))
3054
+ pt = s(:call, nil, :f, s(:iter, s(:call, nil, :lambda), s(:args)))
3017
3055
 
3018
3056
  rb = "f ->() {}"
3019
3057
  assert_parse rb, pt
3020
3058
 
3021
3059
  rb = "f ->() do end"
3022
3060
  assert_parse rb, pt
3061
+
3062
+ pt = s(:call, nil, :f, s(:iter, s(:call, nil, :lambda), 0))
3063
+
3064
+ rb = "f -> {}"
3065
+ assert_parse rb, pt
3066
+
3067
+ rb = "f -> do end"
3068
+ assert_parse rb, pt
3023
3069
  end
3024
3070
 
3025
3071
  def test_thingy
@@ -3042,8 +3088,8 @@ end
3042
3088
 
3043
3089
  class TestRuby20Parser < RubyParserTestCase
3044
3090
  include TestRubyParserShared
3045
- include TestRubyParserShared20to21
3046
- include TestRubyParserShared19to21
3091
+ include TestRubyParserShared20to22
3092
+ include TestRubyParserShared19to22
3047
3093
 
3048
3094
  def setup
3049
3095
  super
@@ -3057,7 +3103,7 @@ class TestRuby20Parser < RubyParserTestCase
3057
3103
  s(:call,
3058
3104
  s(:iter,
3059
3105
  s(:call, s(:call, nil, :a), :b, s(:call, nil, :c)),
3060
- s(:args),
3106
+ 0,
3061
3107
  s(:call, nil, :d)),
3062
3108
  :e),
3063
3109
  s(:args, :f),
@@ -3073,7 +3119,7 @@ class TestRuby20Parser < RubyParserTestCase
3073
3119
  s(:call,
3074
3120
  s(:iter,
3075
3121
  s(:call, s(:call, nil, :a), :b, s(:call, nil, :c)),
3076
- s(:args),
3122
+ 0,
3077
3123
  s(:call, nil, :d)),
3078
3124
  :e,
3079
3125
  s(:call, nil, :f)),
@@ -3213,8 +3259,8 @@ end
3213
3259
 
3214
3260
  class TestRuby21Parser < RubyParserTestCase
3215
3261
  include TestRubyParserShared
3216
- include TestRubyParserShared19to21
3217
- include TestRubyParserShared20to21
3262
+ include TestRubyParserShared19to22
3263
+ include TestRubyParserShared20to22
3218
3264
 
3219
3265
  def setup
3220
3266
  super
@@ -3289,3 +3335,75 @@ class TestRuby21Parser < RubyParserTestCase
3289
3335
  assert_parse rb, pt
3290
3336
  end
3291
3337
  end
3338
+
3339
+ class TestRuby22Parser < RubyParserTestCase
3340
+ include TestRubyParserShared
3341
+ include TestRubyParserShared19to22
3342
+ include TestRubyParserShared20to22
3343
+
3344
+ def setup
3345
+ super
3346
+
3347
+ self.processor = Ruby22Parser.new
3348
+ end
3349
+
3350
+ def test_call_args_assoc_quoted
3351
+ pt = s(:call, nil, :x, s(:hash, s(:lit, :k), s(:lit, 42)))
3352
+
3353
+ rb = "x 'k':42"
3354
+ assert_parse rb, pt
3355
+
3356
+ rb = 'x "k":42'
3357
+ assert_parse rb, pt
3358
+
3359
+ rb = 'x "#{k}":42'
3360
+ pt = s(:call, nil, :x, s(:hash, s(:dsym, "", s(:evstr, s(:call, nil, :k))), s(:lit, 42)))
3361
+
3362
+ assert_parse rb, pt
3363
+ end
3364
+
3365
+ def test_bug191
3366
+ pt = s(:if, s(:call, nil, :a), s(:str, ""), s(:call, nil, :b))
3367
+
3368
+ rb = "a ? '': b"
3369
+ assert_parse rb, pt
3370
+
3371
+ rb = "a ? \"\": b"
3372
+ assert_parse rb, pt
3373
+ end
3374
+ end
3375
+
3376
+ [18, 19, 20, 21, 22].each do |v|
3377
+ describe "block args arity #{v}" do
3378
+ attr_accessor :parser
3379
+
3380
+ before do
3381
+ self.parser = Object.const_get("Ruby#{v}Parser").new
3382
+ end
3383
+
3384
+ {
3385
+ "-> { }" => s(:iter, s(:call, nil, :lambda), 0),
3386
+ "lambda { }" => s(:iter, s(:call, nil, :lambda), 0),
3387
+ "proc { }" => s(:iter, s(:call, nil, :proc), 0),
3388
+ "Proc.new { }" => s(:iter, s(:call, s(:const, :Proc), :new), 0),
3389
+
3390
+ "-> () { }" => s(:iter, s(:call, nil, :lambda), s(:args)),
3391
+ "lambda { || }" => s(:iter, s(:call, nil, :lambda), s(:args)),
3392
+ "proc { || }" => s(:iter, s(:call, nil, :proc), s(:args)),
3393
+ "Proc.new { || }" => s(:iter, s(:call, s(:const, :Proc), :new), s(:args)),
3394
+
3395
+ }.each do |input, expected|
3396
+ next if v == 18 and input =~ /->/
3397
+ next if v == 19 and input =~ /-> \(\)/
3398
+
3399
+ it "parses '#{input}'" do
3400
+ assert_equal expected, parser.parse(input)
3401
+ end
3402
+
3403
+ input = input.sub(/\{/, "do").sub(/\}/, "end")
3404
+ it "parses '#{input}'" do
3405
+ assert_equal expected, parser.parse(input)
3406
+ end
3407
+ end
3408
+ end
3409
+ end