parser 2.0.0.pre3 → 2.0.0.pre4

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.
data/lib/parser/ruby21.y CHANGED
@@ -16,7 +16,7 @@ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
16
16
  tBACK_REF2 tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tREGEXP_OPT
17
17
  tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG tSTRING_DBEG
18
18
  tSTRING_DVAR tSTRING_END tSTRING_DEND tSTRING tSYMBOL
19
- tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA tLAMBEG
19
+ tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA tLAMBEG tCHARACTER
20
20
 
21
21
  prechigh
22
22
  right tBANG tTILDE tUPLUS
@@ -1675,6 +1675,10 @@ opt_block_args_tail:
1675
1675
  {
1676
1676
  result = @builder.string(val[0])
1677
1677
  }
1678
+ | tCHARACTER
1679
+ {
1680
+ result = @builder.character(val[0])
1681
+ }
1678
1682
 
1679
1683
  xstring: tXSTRING_BEG xstring_contents tSTRING_END
1680
1684
  {
@@ -1740,7 +1744,7 @@ opt_block_args_tail:
1740
1744
  }
1741
1745
  | qword_list tSTRING_CONTENT tSPACE
1742
1746
  {
1743
- result = val[0] << @builder.string(val[1])
1747
+ result = val[0] << @builder.string_internal(val[1])
1744
1748
  }
1745
1749
 
1746
1750
  qsym_list: # nothing
@@ -1749,7 +1753,7 @@ opt_block_args_tail:
1749
1753
  }
1750
1754
  | qsym_list tSTRING_CONTENT tSPACE
1751
1755
  {
1752
- result = val[0] << @builder.symbol(val[1])
1756
+ result = val[0] << @builder.symbol_internal(val[1])
1753
1757
  }
1754
1758
 
1755
1759
  string_contents: # nothing
@@ -1781,7 +1785,7 @@ regexp_contents: # nothing
1781
1785
 
1782
1786
  string_content: tSTRING_CONTENT
1783
1787
  {
1784
- result = @builder.string(val[0])
1788
+ result = @builder.string_internal(val[0])
1785
1789
  }
1786
1790
  | tSTRING_DVAR string_dvar
1787
1791
  {
@@ -2,18 +2,32 @@ module Parser
2
2
  module Source
3
3
 
4
4
  ##
5
+ #
6
+ # @!attribute skip_directives
7
+ # Skip file processing directives disguised as comments,
8
+ # namely:
9
+ #
10
+ # * Shebang line,
11
+ # * Magic encoding comment.
12
+ #
5
13
  # @api public
6
14
  #
7
15
  class Comment::Associator
16
+ attr_accessor :skip_directives
17
+
8
18
  def initialize(comments, ast)
9
19
  @comments = comments
10
20
  @ast = ast
21
+
22
+ @skip_directives = true
11
23
  end
12
24
 
13
25
  def associate
14
26
  @mapping = Hash.new { |h, k| h[k] = [] }
15
27
  @comment_num = 0
16
28
 
29
+ advance_through_directives if @skip_directives
30
+
17
31
  process(nil, @ast)
18
32
 
19
33
  @mapping
@@ -52,7 +66,7 @@ module Parser
52
66
  def current_comment_between?(prev_node, next_node)
53
67
  return false if current_comment.nil?
54
68
 
55
- comment_loc = current_comment.location
69
+ comment_loc = current_comment.location.expression
56
70
  next_loc = next_node.location.expression
57
71
 
58
72
  if prev_node.nil?
@@ -69,6 +83,18 @@ module Parser
69
83
  @mapping[node] << current_comment
70
84
  advance_comment
71
85
  end
86
+
87
+ def advance_through_directives
88
+ # Skip shebang.
89
+ if current_comment.text =~ /^#!/
90
+ advance_comment
91
+ end
92
+
93
+ # Skip encoding line.
94
+ if current_comment.text =~ Buffer::ENCODING_RE
95
+ advance_comment
96
+ end
97
+ end
72
98
  end
73
99
 
74
100
  end
@@ -8,7 +8,7 @@ module Parser
8
8
  # @return String
9
9
  #
10
10
  # @!attribute [r] location
11
- # @return Parser::Source::Range
11
+ # @return Parser::Source::Map
12
12
  class Comment
13
13
  attr_reader :text
14
14
 
@@ -23,10 +23,10 @@ module Parser
23
23
  end
24
24
 
25
25
  ##
26
- # @param [Parser::Source::Range] location
27
- def initialize(location)
28
- @location = location
29
- @text = location.source.freeze
26
+ # @param [Parser::Source::Range] range
27
+ def initialize(range)
28
+ @location = Parser::Source::Map.new(range)
29
+ @text = range.source.freeze
30
30
 
31
31
  freeze
32
32
  end
@@ -25,6 +25,14 @@ module Parser
25
25
  with { |map| map.update_expression(expression_l) }
26
26
  end
27
27
 
28
+ def ==(other)
29
+ other.class == self.class &&
30
+ instance_variables.map do |ivar|
31
+ instance_variable_get(ivar) ==
32
+ other.send(:instance_variable_get, ivar)
33
+ end.reduce(:&)
34
+ end
35
+
28
36
  def to_hash
29
37
  Hash[instance_variables.map do |ivar|
30
38
  [ ivar[1..-1].to_sym, instance_variable_get(ivar) ]
@@ -1,3 +1,3 @@
1
1
  module Parser
2
- VERSION = '2.0.0.pre3'
2
+ VERSION = '2.0.0.pre4'
3
3
  end
data/test/test_lexer.rb CHANGED
@@ -1023,7 +1023,7 @@ class TestLexer < Minitest::Test
1023
1023
  def test_question_eh_a__19
1024
1024
  setup_lexer 19
1025
1025
 
1026
- assert_scanned '?a', :tSTRING, "a"
1026
+ assert_scanned '?a', :tCHARACTER, "a"
1027
1027
  end
1028
1028
 
1029
1029
  def test_question_eh_escape_M_escape_C__18
@@ -1035,7 +1035,7 @@ class TestLexer < Minitest::Test
1035
1035
  def test_question_eh_escape_M_escape_C__19
1036
1036
  setup_lexer 19
1037
1037
 
1038
- assert_scanned '?\M-\C-a', :tSTRING, "\M-\C-a"
1038
+ assert_scanned '?\M-\C-a', :tCHARACTER, "\M-\C-a"
1039
1039
  end
1040
1040
 
1041
1041
  def test_integer_hex
@@ -1368,7 +1368,7 @@ class TestLexer < Minitest::Test
1368
1368
  def test_question__19
1369
1369
  setup_lexer 19
1370
1370
 
1371
- assert_scanned "?*", :tSTRING, "*"
1371
+ assert_scanned "?*", :tCHARACTER, "*"
1372
1372
  end
1373
1373
 
1374
1374
  def test_question_bad_eos
@@ -1405,17 +1405,17 @@ class TestLexer < Minitest::Test
1405
1405
  setup_lexer 19
1406
1406
 
1407
1407
  @lex.state = :expr_beg
1408
- assert_scanned "?\\ ", :tSTRING, " "
1408
+ assert_scanned "?\\ ", :tCHARACTER, " "
1409
1409
  @lex.state = :expr_beg
1410
- assert_scanned "?\\n", :tSTRING, "\n"
1410
+ assert_scanned "?\\n", :tCHARACTER, "\n"
1411
1411
  @lex.state = :expr_beg
1412
- assert_scanned "?\\t", :tSTRING, "\t"
1412
+ assert_scanned "?\\t", :tCHARACTER, "\t"
1413
1413
  @lex.state = :expr_beg
1414
- assert_scanned "?\\v", :tSTRING, "\v"
1414
+ assert_scanned "?\\v", :tCHARACTER, "\v"
1415
1415
  @lex.state = :expr_beg
1416
- assert_scanned "?\\r", :tSTRING, "\r"
1416
+ assert_scanned "?\\r", :tCHARACTER, "\r"
1417
1417
  @lex.state = :expr_beg
1418
- assert_scanned "?\\f", :tSTRING, "\f"
1418
+ assert_scanned "?\\f", :tCHARACTER, "\f"
1419
1419
  end
1420
1420
 
1421
1421
  def test_rbracket
@@ -2807,8 +2807,8 @@ class TestLexer < Minitest::Test
2807
2807
  def test_bug_fid_char
2808
2808
  setup_lexer(19)
2809
2809
  assert_scanned(%Q{eof??a},
2810
- :tFID, 'eof?',
2811
- :tSTRING, 'a')
2810
+ :tFID, 'eof?',
2811
+ :tCHARACTER, 'a')
2812
2812
  end
2813
2813
 
2814
2814
  def test_bug_nonlabel_context__18
data/test/test_parser.rb CHANGED
@@ -180,13 +180,29 @@ class TestParser < Minitest::Test
180
180
  %q{~~~~~~~~ expression})
181
181
  end
182
182
 
183
+ def test_character
184
+ assert_parses(
185
+ s(:str, 'a'),
186
+ %q{?a},
187
+ %q{^ begin
188
+ |~~ expression},
189
+ ALL_VERSIONS - %w(1.8))
190
+
191
+ assert_parses(
192
+ s(:int, 97),
193
+ %q{?a},
194
+ %q{~~ expression},
195
+ %w(1.8))
196
+ end
197
+
183
198
  # Symbols
184
199
 
185
200
  def test_symbol_plain
186
201
  assert_parses(
187
202
  s(:sym, :foo),
188
203
  %q{:foo},
189
- %q{~~~~ expression})
204
+ %q{~ begin
205
+ |~~~~ expression})
190
206
 
191
207
  assert_parses(
192
208
  s(:sym, :foo),
@@ -4547,7 +4563,7 @@ class TestParser < Minitest::Test
4547
4563
  assert_parses_with_comments(
4548
4564
  s(:send, s(:int, 1), :+, s(:int, 2)),
4549
4565
  %Q{1 + # foo\n 2},
4550
- [ [4, 9] ])
4566
+ [ [4, 9] ])
4551
4567
  end
4552
4568
 
4553
4569
  def test_comment_single
@@ -16,6 +16,8 @@ class TestSourceCommentAssociator < Minitest::Test
16
16
 
17
17
  def test_associate
18
18
  ast, associations = associate(<<-END)
19
+ #!/usr/bin/env ruby
20
+ # coding: utf-8
19
21
  # Class comment
20
22
  # another class comment
21
23
  class Foo
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre3
4
+ version: 2.0.0.pre4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Zotov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-26 00:00:00.000000000 Z
11
+ date: 2013-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -265,7 +265,6 @@ files:
265
265
  - lib/parser/source/comment.rb
266
266
  - lib/parser/source/comment/associator.rb
267
267
  - lib/parser/source/map.rb
268
- - lib/parser/source/map/block.rb
269
268
  - lib/parser/source/map/collection.rb
270
269
  - lib/parser/source/map/condition.rb
271
270
  - lib/parser/source/map/constant.rb
@@ -1,16 +0,0 @@
1
- module Parser
2
- module Source
3
-
4
- class Map::Block < Map
5
- attr_reader :begin
6
- attr_reader :end
7
-
8
- def initialize(begin_l, end_l, expression_l)
9
- @begin, @end = begin_l, end_l
10
-
11
- super(expression_l)
12
- end
13
- end
14
-
15
- end
16
- end