parser 2.4.0.0 → 2.4.0.1

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.
@@ -7,7 +7,7 @@ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
7
7
  kUNTIL_MOD kRESCUE_MOD kALIAS kDEFINED klBEGIN klEND k__LINE__
8
8
  k__FILE__ k__ENCODING__ tIDENTIFIER tFID tGVAR tIVAR tCONSTANT
9
9
  tLABEL tCVAR tNTH_REF tBACK_REF tSTRING_CONTENT tINTEGER tFLOAT
10
- tUPLUS tUMINUS tUMINUS_NUM tPOW tCMP tEQ tEQQ tNEQ
10
+ tUPLUS tUMINUS tUNARY_NUM tPOW tCMP tEQ tEQQ tNEQ
11
11
  tGEQ tLEQ tANDOP tOROP tMATCH tNMATCH tDOT tDOT2 tDOT3 tAREF
12
12
  tASET tLSHFT tRSHFT tCOLON2 tCOLON3 tOP_ASGN tASSOC tLPAREN
13
13
  tLPAREN2 tRPAREN tLPAREN_ARG tLBRACK tLBRACK2 tRBRACK tLBRACE
@@ -21,7 +21,7 @@ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
21
21
  prechigh
22
22
  right tBANG tTILDE tUPLUS
23
23
  right tPOW
24
- right tUMINUS_NUM tUMINUS
24
+ right tUNARY_NUM tUMINUS
25
25
  left tSTAR2 tDIVIDE tPERCENT
26
26
  left tPLUS tMINUS
27
27
  left tLSHFT tRSHFT
@@ -646,14 +646,14 @@ rule
646
646
  {
647
647
  result = @builder.binary_op(val[0], val[1], val[2])
648
648
  }
649
- | tUMINUS_NUM tINTEGER tPOW arg
649
+ | tUNARY_NUM tINTEGER tPOW arg
650
650
  {
651
651
  result = @builder.unary_op(val[0],
652
652
  @builder.binary_op(
653
653
  @builder.integer(val[1]),
654
654
  val[2], val[3]))
655
655
  }
656
- | tUMINUS_NUM tFLOAT tPOW arg
656
+ | tUNARY_NUM tFLOAT tPOW arg
657
657
  {
658
658
  result = @builder.unary_op(val[0],
659
659
  @builder.binary_op(
@@ -1762,15 +1762,25 @@ regexp_contents: # nothing
1762
1762
  {
1763
1763
  result = @builder.float(val[0])
1764
1764
  }
1765
- | tUMINUS_NUM tINTEGER =tLOWEST
1765
+ | tUNARY_NUM tINTEGER =tLOWEST
1766
1766
  {
1767
- result = @builder.negate(val[0],
1768
- @builder.integer(val[1]))
1767
+ num = @builder.integer(val[1])
1768
+ if @builder.respond_to? :negate
1769
+ # AST builder interface compatibility
1770
+ result = @builder.negate(val[0], num)
1771
+ else
1772
+ result = @builder.unary_num(val[0], num)
1773
+ end
1769
1774
  }
1770
- | tUMINUS_NUM tFLOAT =tLOWEST
1775
+ | tUNARY_NUM tFLOAT =tLOWEST
1771
1776
  {
1772
- result = @builder.negate(val[0],
1773
- @builder.float(val[1]))
1777
+ num = @builder.float(val[1])
1778
+ if @builder.respond_to? :negate
1779
+ # AST builder interface compatibility
1780
+ result = @builder.negate(val[0], num)
1781
+ else
1782
+ result = @builder.unary_num(val[0], num)
1783
+ end
1774
1784
  }
1775
1785
 
1776
1786
  variable: tIDENTIFIER
@@ -94,6 +94,11 @@ module Parser
94
94
  @parser_class = Parser::Ruby24
95
95
  end
96
96
 
97
+ opts.on '--25', 'Parse as Ruby 2.5 would' do
98
+ require 'parser/ruby25'
99
+ @parser_class = Parser::Ruby25
100
+ end
101
+
97
102
  opts.on '--mac', 'Parse as MacRuby 0.12 would' do
98
103
  require 'parser/macruby'
99
104
  @parser_class = Parser::MacRuby
@@ -32,7 +32,7 @@ module Parser
32
32
  def load_and_discover(file)
33
33
  load file
34
34
 
35
- const_name = file.
35
+ const_name = File.basename(file).
36
36
  sub(/\.rb$/, '').
37
37
  gsub(/(^|_)([a-z])/) do |m|
38
38
  "#{$2.upcase}"
@@ -54,7 +54,7 @@ module Parser
54
54
  string =~ /\A(.*)\n?(.*\n)?/
55
55
  first_line, second_line = $1, $2
56
56
 
57
- if first_line =~ /\A\xef\xbb\xbf/ # BOM
57
+ if first_line.start_with?("\xef\xbb\xbf".freeze) # BOM
58
58
  return Encoding::UTF_8
59
59
  elsif first_line[0, 2] == '#!'.freeze
60
60
  encoding_line = second_line
@@ -72,10 +72,9 @@ module Parser
72
72
  # @return [Symbol]
73
73
  #
74
74
  def type
75
- case text
76
- when /^#/
75
+ if text.start_with?("#".freeze)
77
76
  :inline
78
- when /^=begin/
77
+ elsif text.start_with?("=begin".freeze)
79
78
  :document
80
79
  end
81
80
  end
@@ -184,9 +184,16 @@ module Parser
184
184
  advance_comment
185
185
  end
186
186
 
187
+ MAGIC_COMMENT_RE = /^#\s*(-\*-|)\s*(frozen_string_literal|warn_indent|warn_past_scope):.*\1$/
188
+
187
189
  def advance_through_directives
188
190
  # Skip shebang.
189
- if @current_comment && @current_comment.text =~ /^#!/
191
+ if @current_comment && @current_comment.text.start_with?('#!'.freeze)
192
+ advance_comment
193
+ end
194
+
195
+ # Skip magic comments.
196
+ if @current_comment && @current_comment.text =~ MAGIC_COMMENT_RE
190
197
  advance_comment
191
198
  end
192
199
 
@@ -42,6 +42,10 @@ module Parser
42
42
 
43
43
  @insert_before_multi_order = 0
44
44
  @insert_after_multi_order = 0
45
+
46
+ @pending_queue = nil
47
+ @pending_clobber = nil
48
+ @pending_insertions = nil
45
49
  end
46
50
 
47
51
  ##
@@ -1,3 +1,3 @@
1
1
  module Parser
2
- VERSION = '2.4.0.0'
2
+ VERSION = '2.4.0.1'
3
3
  end
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  lib/parser/ruby22.rb
23
23
  lib/parser/ruby23.rb
24
24
  lib/parser/ruby24.rb
25
+ lib/parser/ruby25.rb
25
26
  lib/parser/macruby.rb
26
27
  lib/parser/rubymotion.rb
27
28
  )
@@ -29,18 +30,18 @@ Gem::Specification.new do |spec|
29
30
  spec.test_files = spec.files.grep(%r{^test/})
30
31
  spec.require_paths = ['lib']
31
32
 
32
- spec.add_dependency 'ast', '~> 2.2'
33
+ spec.add_dependency 'ast', '~> 2.3'
33
34
 
34
- spec.add_development_dependency 'bundler', '~> 1.2'
35
+ spec.add_development_dependency 'bundler', '~> 1.16'
35
36
  spec.add_development_dependency 'rake', '~> 10.0'
36
37
  spec.add_development_dependency 'racc', '= 1.4.14'
37
- spec.add_development_dependency 'cliver', '~> 0.3.0'
38
+ spec.add_development_dependency 'cliver', '~> 0.3.2'
38
39
 
39
40
  spec.add_development_dependency 'yard'
40
41
  spec.add_development_dependency 'kramdown'
41
42
 
42
- spec.add_development_dependency 'minitest', '~> 5.0'
43
- spec.add_development_dependency 'simplecov', '~> 0.8.2'
43
+ spec.add_development_dependency 'minitest', '~> 5.10'
44
+ spec.add_development_dependency 'simplecov', '~> 0.15.1'
44
45
 
45
46
  spec.add_development_dependency 'gauntlet'
46
47
  end
@@ -7,7 +7,7 @@ if ENV.include?('COVERAGE') && SimpleCov.usable?
7
7
  if defined?(TracePoint)
8
8
  require_relative 'racc_coverage_helper'
9
9
 
10
- RaccCoverage.start(%w(ruby18.y ruby19.y ruby20.y ruby21.y ruby22.y ruby23.y ruby24.y),
10
+ RaccCoverage.start(%w(ruby18.y ruby19.y ruby20.y ruby21.y ruby22.y ruby23.y ruby24.y ruby25.y),
11
11
  File.expand_path('../../lib/parser', __FILE__))
12
12
 
13
13
  # Report results faster.
@@ -10,7 +10,7 @@ module ParseHelper
10
10
  require 'parser/macruby'
11
11
  require 'parser/rubymotion'
12
12
 
13
- ALL_VERSIONS = %w(1.8 1.9 2.0 2.1 2.2 2.3 2.4 mac ios)
13
+ ALL_VERSIONS = %w(1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 mac ios)
14
14
  end
15
15
 
16
16
  def setup
@@ -28,6 +28,7 @@ module ParseHelper
28
28
  when '2.2' then parser = Parser::Ruby22.new
29
29
  when '2.3' then parser = Parser::Ruby23.new
30
30
  when '2.4' then parser = Parser::Ruby24.new
31
+ when '2.5' then parser = Parser::Ruby25.new
31
32
  when 'mac' then parser = Parser::MacRuby.new
32
33
  when 'ios' then parser = Parser::RubyMotion.new
33
34
  else raise "Unrecognized Ruby version #{version}"
@@ -98,6 +99,11 @@ module ParseHelper
98
99
  raise
99
100
  end
100
101
 
102
+ if ast.nil?
103
+ assert_nil parsed_ast, "(#{version}) AST equality"
104
+ return
105
+ end
106
+
101
107
  assert_equal ast, parsed_ast,
102
108
  "(#{version}) AST equality"
103
109
 
@@ -15,7 +15,7 @@ module RaccCoverage
15
15
  @coverage[parser] = extract_interesting_lines(parser, base_path)
16
16
  end
17
17
 
18
- @trace = TracePoint.trace(:line) do |trace|
18
+ @trace = TracePoint.new(:line) do |trace|
19
19
  lineno = trace.lineno - 1
20
20
 
21
21
  if (line_coverage = @coverage[trace.path])
@@ -24,6 +24,7 @@ module RaccCoverage
24
24
  end
25
25
  end
26
26
  end
27
+ @trace.enable
27
28
  end
28
29
 
29
30
  def self.stop
@@ -23,7 +23,7 @@ class TestBase < Minitest::Test
23
23
 
24
24
  def test_loc_dup
25
25
  ast = Parser::CurrentRuby.parse('1')
26
- assert_equal nil, ast.loc.dup.node
26
+ assert_nil ast.loc.dup.node
27
27
  Parser::AST::Node.new(:root, [], :location => ast.loc)
28
28
  end
29
29
  end
@@ -18,6 +18,8 @@ class TestCurrent < Minitest::Test
18
18
  assert_equal Parser::Ruby23, Parser::CurrentRuby
19
19
  when /^2\.4\.\d+/
20
20
  assert_equal Parser::Ruby24, Parser::CurrentRuby
21
+ when /^2\.5\.\d+/
22
+ assert_equal Parser::Ruby25, Parser::CurrentRuby
21
23
  else
22
24
  flunk "Update test_current for #{RUBY_VERSION}"
23
25
  end
@@ -13,7 +13,7 @@ class TestEncoding < Minitest::Test
13
13
  require 'parser/all'
14
14
 
15
15
  def test_default
16
- assert_equal nil, recognize('foobar')
16
+ assert_nil recognize('foobar')
17
17
  end
18
18
 
19
19
  def test_bom
@@ -38,16 +38,16 @@ class TestEncoding < Minitest::Test
38
38
  end
39
39
 
40
40
  def test_empty
41
- assert_equal nil, recognize('')
41
+ assert_nil recognize('')
42
42
  end
43
43
 
44
44
  def test_no_comment
45
- assert_equal nil, recognize(%{require 'cane/encoding_aware_iterator'})
45
+ assert_nil recognize(%{require 'cane/encoding_aware_iterator'})
46
46
  end
47
47
 
48
48
  def test_adjacent
49
- assert_equal nil, recognize('# codingkoi8-r')
50
- assert_equal nil, recognize('# coding koi8-r')
49
+ assert_nil recognize('# codingkoi8-r')
50
+ assert_nil recognize('# coding koi8-r')
51
51
  end
52
52
 
53
53
  def test_utf8_mac
@@ -60,7 +60,7 @@ class TestEncoding < Minitest::Test
60
60
  assert_equal Encoding::UTF_8, recognize('# coding: utf-8-mac')
61
61
 
62
62
  assert_raises(ArgumentError) do
63
- assert_equal nil, recognize('# coding: utf-8-dicks')
63
+ assert_nil recognize('# coding: utf-8-dicks')
64
64
  end
65
65
  end
66
66
 
@@ -178,13 +178,14 @@ class TestLexer < Minitest::Test
178
178
  def test_ambiguous_uminus
179
179
  assert_scanned("m -3",
180
180
  :tIDENTIFIER, "m", [0, 1],
181
- :tUMINUS_NUM, "-", [2, 3],
181
+ :tUNARY_NUM, "-", [2, 3],
182
182
  :tINTEGER, 3, [3, 4])
183
183
  end
184
184
 
185
185
  def test_ambiguous_uplus
186
186
  assert_scanned("m +3",
187
187
  :tIDENTIFIER, "m", [0, 1],
188
+ :tUNARY_NUM, "+", [2, 3],
188
189
  :tINTEGER, 3, [3, 4])
189
190
  end
190
191
 
@@ -569,16 +570,6 @@ class TestLexer < Minitest::Test
569
570
  :kEND, "end", [8, 11])
570
571
  end
571
572
 
572
- def test_do_cond
573
- @lex.cond.push(true)
574
-
575
- assert_scanned("x do 42 end",
576
- :tIDENTIFIER, "x", [0, 1],
577
- :kDO_COND, "do", [2, 4],
578
- :tINTEGER, 42, [5, 7],
579
- :kEND, "end", [8, 11])
580
- end
581
-
582
573
  def test_do_block
583
574
  @lex.state = :expr_endarg
584
575
 
@@ -655,7 +646,13 @@ class TestLexer < Minitest::Test
655
646
 
656
647
  def test_float_dot_E_neg
657
648
  assert_scanned("-1.0E10",
658
- :tUMINUS_NUM, "-", [0, 1],
649
+ :tUNARY_NUM, "-", [0, 1],
650
+ :tFLOAT, 1.0e10, [1, 7])
651
+ end
652
+
653
+ def test_float_dot_E_pos
654
+ assert_scanned("+1.0E10",
655
+ :tUNARY_NUM, "+", [0, 1],
659
656
  :tFLOAT, 1.0e10, [1, 7])
660
657
  end
661
658
 
@@ -665,7 +662,13 @@ class TestLexer < Minitest::Test
665
662
 
666
663
  def test_float_dot_e_neg
667
664
  assert_scanned("-1.0e10",
668
- :tUMINUS_NUM, "-", [0, 1],
665
+ :tUNARY_NUM, "-", [0, 1],
666
+ :tFLOAT, 1.0e10, [1, 7])
667
+ end
668
+
669
+ def test_float_dot_e_pos
670
+ assert_scanned("+1.0e10",
671
+ :tUNARY_NUM, "+", [0, 1],
669
672
  :tFLOAT, 1.0e10, [1, 7])
670
673
  end
671
674
 
@@ -683,19 +686,37 @@ class TestLexer < Minitest::Test
683
686
 
684
687
  def test_float_e_neg
685
688
  assert_scanned("-1e10",
686
- :tUMINUS_NUM, "-", [0, 1],
689
+ :tUNARY_NUM, "-", [0, 1],
687
690
  :tFLOAT, 1e10, [1, 5])
688
691
  end
689
692
 
690
693
  def test_float_e_neg_minus
691
694
  assert_scanned("-1e-10",
692
- :tUMINUS_NUM, "-", [0, 1],
695
+ :tUNARY_NUM, "-", [0, 1],
693
696
  :tFLOAT, 1e-10, [1, 6])
694
697
  end
695
698
 
696
699
  def test_float_e_neg_plus
697
700
  assert_scanned("-1e+10",
698
- :tUMINUS_NUM, "-", [0, 1],
701
+ :tUNARY_NUM, "-", [0, 1],
702
+ :tFLOAT, 1e10, [1, 6])
703
+ end
704
+
705
+ def test_float_e_pos
706
+ assert_scanned("+1e10",
707
+ :tUNARY_NUM, "+", [0, 1],
708
+ :tFLOAT, 1e10, [1, 5])
709
+ end
710
+
711
+ def test_float_e_pos_minus
712
+ assert_scanned("+1e-10",
713
+ :tUNARY_NUM, "+", [0, 1],
714
+ :tFLOAT, 1e-10, [1, 6])
715
+ end
716
+
717
+ def test_float_e_pos_plus
718
+ assert_scanned("+1e+10",
719
+ :tUNARY_NUM, "+", [0, 1],
699
720
  :tFLOAT, 1e10, [1, 6])
700
721
  end
701
722
 
@@ -727,7 +748,13 @@ class TestLexer < Minitest::Test
727
748
 
728
749
  def test_float_neg
729
750
  assert_scanned("-1.0",
730
- :tUMINUS_NUM, "-", [0, 1],
751
+ :tUNARY_NUM, "-", [0, 1],
752
+ :tFLOAT, 1.0, [1, 4])
753
+ end
754
+
755
+ def test_float_pos
756
+ assert_scanned("+1.0",
757
+ :tUNARY_NUM, "+", [0, 1],
731
758
  :tFLOAT, 1.0, [1, 4])
732
759
  end
733
760
 
@@ -962,12 +989,14 @@ class TestLexer < Minitest::Test
962
989
  end
963
990
 
964
991
  def test_identifier_eh
992
+ setup_lexer 19
993
+
965
994
  assert_scanned("identifier?",
966
995
  :tFID, "identifier?", [0, 11])
967
996
 
968
997
  assert_scanned("identifier?=",
969
998
  :tIDENTIFIER, "identifier", [0, 10],
970
- :tNEQ, "?=", [10, 12])
999
+ :tCHARACTER, "=", [10, 12])
971
1000
  end
972
1001
 
973
1002
  def test_identifier_cmp
@@ -978,10 +1007,6 @@ class TestLexer < Minitest::Test
978
1007
  assert_lex_fname "identifier", :tIDENTIFIER, [0, 10]
979
1008
  end
980
1009
 
981
- def test_identifier_eh
982
- assert_scanned("identifier?", :tFID, "identifier?", [0, 11])
983
- end
984
-
985
1010
  def test_identifier_equals_arrow
986
1011
  assert_scanned(":blah==>",
987
1012
  :tSYMBOL, "blah=", [0, 6],
@@ -1319,10 +1344,16 @@ class TestLexer < Minitest::Test
1319
1344
 
1320
1345
  def test_minus_unary_number
1321
1346
  assert_scanned("-42",
1322
- :tUMINUS_NUM, "-", [0, 1],
1347
+ :tUNARY_NUM, "-", [0, 1],
1323
1348
  :tINTEGER, 42, [1, 3])
1324
1349
  end
1325
1350
 
1351
+ def test_minus_unary_whitespace_number
1352
+ assert_scanned("- 42",
1353
+ :tUNARY_NUM, "-", [0, 1],
1354
+ :tINTEGER, 42, [2, 4])
1355
+ end
1356
+
1326
1357
  def test_nth_ref
1327
1358
  assert_scanned('[$1, $2, $3]',
1328
1359
  :tLBRACK, "[", [0, 1],
@@ -1448,6 +1479,18 @@ class TestLexer < Minitest::Test
1448
1479
  assert_scanned "+@", :tUPLUS, "+@", [0, 2]
1449
1480
  end
1450
1481
 
1482
+ def test_plus_unary_number
1483
+ assert_scanned("+42",
1484
+ :tUNARY_NUM, "+", [0, 1],
1485
+ :tINTEGER, 42, [1, 3])
1486
+ end
1487
+
1488
+ def test_plus_unary_whitespace_number
1489
+ assert_scanned("+ 42",
1490
+ :tUNARY_NUM, "+", [0, 1],
1491
+ :tINTEGER, 42, [2, 4])
1492
+ end
1493
+
1451
1494
  def test_numbers
1452
1495
  assert_scanned "0b10", :tINTEGER, 2, [0, 4]
1453
1496
  assert_scanned "0B10", :tINTEGER, 2, [0, 4]
@@ -1492,11 +1535,6 @@ class TestLexer < Minitest::Test
1492
1535
  refute_scanned "1.1end"
1493
1536
  end
1494
1537
 
1495
- def test_plus_unary_number
1496
- assert_scanned("+42",
1497
- :tINTEGER, 42, [1, 3])
1498
- end
1499
-
1500
1538
  def test_question__18
1501
1539
  setup_lexer 18
1502
1540
 
@@ -2765,7 +2803,7 @@ class TestLexer < Minitest::Test
2765
2803
  @lex.state = :expr_end
2766
2804
  assert_scanned("\n+ 1",
2767
2805
  :tNL, nil, [0, 1],
2768
- :tUPLUS, '+', [1, 2],
2806
+ :tUNARY_NUM, '+', [1, 2],
2769
2807
  :tINTEGER, 1, [3, 4])
2770
2808
 
2771
2809
  @lex.state = :expr_end
@@ -2776,7 +2814,7 @@ class TestLexer < Minitest::Test
2776
2814
  @lex.state = :expr_end
2777
2815
  assert_scanned("#foo\n+ 1",
2778
2816
  :tNL, nil, [4, 5],
2779
- :tUPLUS, '+', [5, 6],
2817
+ :tUNARY_NUM, '+', [5, 6],
2780
2818
  :tINTEGER, 1, [7, 8])
2781
2819
  end
2782
2820