parser 2.7.2.0 → 3.1.1.0

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +25 -0
  3. data/lib/parser/all.rb +2 -0
  4. data/lib/parser/ast/processor.rb +3 -0
  5. data/lib/parser/base.rb +1 -0
  6. data/lib/parser/builders/default.rb +225 -27
  7. data/lib/parser/context.rb +22 -37
  8. data/lib/parser/current.rb +28 -10
  9. data/lib/parser/current_arg_stack.rb +5 -2
  10. data/lib/parser/lexer/dedenter.rb +7 -1
  11. data/lib/parser/lexer/explanation.rb +1 -1
  12. data/lib/parser/lexer.rb +12728 -11483
  13. data/lib/parser/macruby.rb +2476 -2448
  14. data/lib/parser/max_numparam_stack.rb +16 -8
  15. data/lib/parser/messages.rb +5 -0
  16. data/lib/parser/meta.rb +2 -1
  17. data/lib/parser/ruby18.rb +2161 -2116
  18. data/lib/parser/ruby19.rb +2458 -2424
  19. data/lib/parser/ruby20.rb +2634 -2598
  20. data/lib/parser/ruby21.rb +2700 -2667
  21. data/lib/parser/ruby22.rb +2533 -2491
  22. data/lib/parser/ruby23.rb +2616 -2571
  23. data/lib/parser/ruby24.rb +2651 -2609
  24. data/lib/parser/ruby25.rb +2545 -2513
  25. data/lib/parser/ruby26.rb +2669 -2624
  26. data/lib/parser/ruby27.rb +3953 -3842
  27. data/lib/parser/ruby30.rb +4089 -4045
  28. data/lib/parser/ruby31.rb +8354 -0
  29. data/lib/parser/ruby32.rb +8330 -0
  30. data/lib/parser/rubymotion.rb +2454 -2415
  31. data/lib/parser/runner.rb +12 -1
  32. data/lib/parser/source/buffer.rb +50 -27
  33. data/lib/parser/source/comment/associator.rb +17 -4
  34. data/lib/parser/source/comment.rb +13 -0
  35. data/lib/parser/source/tree_rewriter.rb +27 -0
  36. data/lib/parser/static_environment.rb +13 -0
  37. data/lib/parser/variables_stack.rb +4 -0
  38. data/lib/parser/version.rb +1 -1
  39. data/parser.gemspec +1 -1
  40. metadata +9 -6
data/lib/parser/runner.rb CHANGED
@@ -37,7 +37,7 @@ module Parser
37
37
 
38
38
  private
39
39
 
40
- LEGACY_MODES = %i[lambda procarg0 encoding index arg_inside_procarg0 forward_arg].freeze
40
+ LEGACY_MODES = %i[lambda procarg0 encoding index arg_inside_procarg0 forward_arg kwargs match_pattern].freeze
41
41
 
42
42
  def runner_name
43
43
  raise NotImplementedError, "implement #{self.class}##{__callee__}"
@@ -118,6 +118,16 @@ module Parser
118
118
  @parser_class = Parser::Ruby30
119
119
  end
120
120
 
121
+ opts.on '--31', 'Parse as Ruby 3.1 would' do
122
+ require 'parser/ruby31'
123
+ @parser_class = Parser::Ruby31
124
+ end
125
+
126
+ opts.on '--32', 'Parse as Ruby 3.2 would' do
127
+ require 'parser/ruby32'
128
+ @parser_class = Parser::Ruby32
129
+ end
130
+
121
131
  opts.on '--mac', 'Parse as MacRuby 0.12 would' do
122
132
  require 'parser/macruby'
123
133
  @parser_class = Parser::MacRuby
@@ -156,6 +166,7 @@ module Parser
156
166
  @option_parser.parse!(options)
157
167
 
158
168
  # Slop has just removed recognized options from `options`.
169
+ @fragments << $stdin.read if options.delete('-')
159
170
  options.each do |file_or_dir|
160
171
  if File.directory?(file_or_dir)
161
172
  Find.find(file_or_dir) do |path|
@@ -114,8 +114,7 @@ module Parser
114
114
  @slice_source = nil
115
115
 
116
116
  # Cache for fast lookup
117
- @line_for_position = {}
118
- @column_for_position = {}
117
+ @line_index_for_position = {}
119
118
 
120
119
  self.source = source if source
121
120
  end
@@ -207,9 +206,10 @@ module Parser
207
206
  # @return [[Integer, Integer]] `[line, column]`
208
207
  #
209
208
  def decompose_position(position)
210
- line_no, line_begin = line_for(position)
209
+ line_index = line_index_for_position(position)
210
+ line_begin = line_begins[line_index]
211
211
 
212
- [ @first_line + line_no, position - line_begin ]
212
+ [ @first_line + line_index , position - line_begin ]
213
213
  end
214
214
 
215
215
  ##
@@ -220,10 +220,7 @@ module Parser
220
220
  # @api private
221
221
  #
222
222
  def line_for_position(position)
223
- @line_for_position[position] ||= begin
224
- line_no, _ = line_for(position)
225
- @first_line + line_no
226
- end
223
+ line_index_for_position(position) + @first_line
227
224
  end
228
225
 
229
226
  ##
@@ -234,10 +231,8 @@ module Parser
234
231
  # @api private
235
232
  #
236
233
  def column_for_position(position)
237
- @column_for_position[position] ||= begin
238
- _, line_begin = line_for(position)
239
- position - line_begin
240
- end
234
+ line_index = line_index_for_position(position)
235
+ position - line_begins[line_index]
241
236
  end
242
237
 
243
238
  ##
@@ -278,15 +273,13 @@ module Parser
278
273
  # @raise [IndexError] if `lineno` is out of bounds
279
274
  #
280
275
  def line_range(lineno)
281
- index = lineno - @first_line + 1
282
- if index <= 0 || index > line_begins.size
276
+ index = lineno - @first_line
277
+ if index < 0 || index + 1 >= line_begins.size
283
278
  raise IndexError, 'Parser::Source::Buffer: range for line ' \
284
279
  "#{lineno} requested, valid line numbers are #{@first_line}.." \
285
- "#{@first_line + line_begins.size - 1}"
286
- elsif index == line_begins.size
287
- Range.new(self, line_begins[-index][1], @source.size)
280
+ "#{@first_line + line_begins.size - 2}"
288
281
  else
289
- Range.new(self, line_begins[-index][1], line_begins[-index - 1][1] - 1)
282
+ Range.new(self, line_begins[index], line_begins[index + 1] - 1)
290
283
  end
291
284
  end
292
285
 
@@ -303,27 +296,57 @@ module Parser
303
296
  # @return [Integer]
304
297
  #
305
298
  def last_line
306
- line_begins.size + @first_line - 1
299
+ line_begins.size + @first_line - 2
300
+ end
301
+
302
+ # :nodoc:
303
+ def freeze
304
+ source_lines; line_begins; source_range # build cache
305
+ super
306
+ end
307
+
308
+ # :nodoc:
309
+ def inspect
310
+ "#<#{self.class} #{name}>"
307
311
  end
308
312
 
309
313
  private
310
314
 
315
+ # @returns [0, line_begin_of_line_1, ..., source.size + 1]
311
316
  def line_begins
312
- unless @line_begins
313
- @line_begins, index = [ [ 0, 0 ] ], 0
314
-
317
+ @line_begins ||= begin
318
+ begins = [0]
319
+ index = 0
315
320
  while index = @source.index("\n".freeze, index)
316
321
  index += 1
317
- @line_begins.unshift [ @line_begins.length, index ]
322
+ begins << index
318
323
  end
324
+ begins << @source.size + 1
325
+ begins
319
326
  end
327
+ end
320
328
 
321
- @line_begins
329
+ # @returns 0-based line index of position
330
+ def line_index_for_position(position)
331
+ @line_index_for_position[position] || begin
332
+ index = bsearch(line_begins, position) - 1
333
+ @line_index_for_position[position] = index unless @line_index_for_position.frozen?
334
+ index
335
+ end
322
336
  end
323
337
 
324
- def line_for(position)
325
- line_begins.bsearch do |line, line_begin|
326
- line_begin <= position
338
+ if Array.method_defined?(:bsearch_index) # RUBY_VERSION >= 2.3
339
+ def bsearch(line_begins, position)
340
+ line_begins.bsearch_index do |line_begin|
341
+ position < line_begin
342
+ end || line_begins.size - 1 # || only for out of bound values
343
+ end
344
+ else
345
+ def bsearch(line_begins, position)
346
+ @line_range ||= 0...line_begins.size
347
+ @line_range.bsearch do |i|
348
+ position < line_begins[i]
349
+ end || line_begins.size - 1 # || only for out of bound values
327
350
  end
328
351
  end
329
352
  end
@@ -84,12 +84,24 @@ module Parser
84
84
  #
85
85
  # Note that {associate} produces unexpected result for nodes which are
86
86
  # equal but have distinct locations; comments for these nodes are merged.
87
+ # You may prefer using {associate_by_identity} or {associate_locations}.
87
88
  #
88
89
  # @return [Hash<Parser::AST::Node, Array<Parser::Source::Comment>>]
89
90
  # @deprecated Use {associate_locations}.
90
91
  #
91
92
  def associate
92
- @map_using_locations = false
93
+ @map_using = :eql
94
+ do_associate
95
+ end
96
+
97
+ ##
98
+ # Same as {associate}, but compares by identity, thus producing an unambiguous
99
+ # result even in presence of equal nodes.
100
+ #
101
+ # @return [Hash<Parser::Source::Node, Array<Parser::Source::Comment>>]
102
+ #
103
+ def associate_locations
104
+ @map_using = :location
93
105
  do_associate
94
106
  end
95
107
 
@@ -100,8 +112,8 @@ module Parser
100
112
  #
101
113
  # @return [Hash<Parser::Source::Map, Array<Parser::Source::Comment>>]
102
114
  #
103
- def associate_locations
104
- @map_using_locations = true
115
+ def associate_by_identity
116
+ @map_using = :identity
105
117
  do_associate
106
118
  end
107
119
 
@@ -122,6 +134,7 @@ module Parser
122
134
 
123
135
  def do_associate
124
136
  @mapping = Hash.new { |h, k| h[k] = [] }
137
+ @mapping.compare_by_identity if @map_using == :identity
125
138
  @comment_num = -1
126
139
  advance_comment
127
140
 
@@ -191,7 +204,7 @@ module Parser
191
204
  end
192
205
 
193
206
  def associate_and_advance_comment(node)
194
- key = @map_using_locations ? node.location : node
207
+ key = @map_using == :location ? node.location : node
195
208
  @mapping[key] << @current_comment
196
209
  advance_comment
197
210
  end
@@ -48,6 +48,19 @@ module Parser
48
48
  associator.associate_locations
49
49
  end
50
50
 
51
+ ##
52
+ # Associate `comments` with `ast` nodes using identity.
53
+ #
54
+ # @param [Parser::AST::Node] ast
55
+ # @param [Array<Comment>] comments
56
+ # @return [Hash<Parser::Source::Node, Array<Comment>>]
57
+ # @see Parser::Source::Comment::Associator#associate_by_identity
58
+ #
59
+ def self.associate_by_identity(ast, comments)
60
+ associator = Associator.new(ast, comments)
61
+ associator.associate_by_identity
62
+ end
63
+
51
64
  ##
52
65
  # @param [Parser::Source::Range] range
53
66
  #
@@ -330,6 +330,11 @@ module Parser
330
330
  @in_transaction
331
331
  end
332
332
 
333
+ # :nodoc:
334
+ def inspect
335
+ "#<#{self.class} #{source_buffer.name}: #{action_summary}>"
336
+ end
337
+
333
338
  ##
334
339
  # @api private
335
340
  # @deprecated Use insert_after or wrap
@@ -361,6 +366,28 @@ module Parser
361
366
 
362
367
  private
363
368
 
369
+ def action_summary
370
+ replacements = as_replacements
371
+ case replacements.size
372
+ when 0 then return 'empty'
373
+ when 1..3 then #ok
374
+ else
375
+ replacements = replacements.first(3)
376
+ suffix = '…'
377
+ end
378
+ parts = replacements.map do |(range, str)|
379
+ if str.empty? # is this a deletion?
380
+ "-#{range.to_range}"
381
+ elsif range.size == 0 # is this an insertion?
382
+ "+#{str.inspect}@#{range.begin_pos}"
383
+ else # it is a replacement
384
+ "^#{str.inspect}@#{range.to_range}"
385
+ end
386
+ end
387
+ parts << suffix if suffix
388
+ parts.join(', ')
389
+ end
390
+
364
391
  ACTIONS = %i[accept warn raise].freeze
365
392
  def check_policy_validity
366
393
  invalid = @policy.values - ACTIONS
@@ -4,6 +4,7 @@ module Parser
4
4
 
5
5
  class StaticEnvironment
6
6
  FORWARD_ARGS = :FORWARD_ARGS
7
+ ANONYMOUS_BLOCKARG = :ANONYMOUS_BLOCKARG
7
8
 
8
9
  def initialize
9
10
  reset
@@ -51,6 +52,18 @@ module Parser
51
52
  def declared_forward_args?
52
53
  declared?(FORWARD_ARGS)
53
54
  end
55
+
56
+ def declare_anonymous_blockarg
57
+ declare(ANONYMOUS_BLOCKARG)
58
+ end
59
+
60
+ def declared_anonymous_blockarg?
61
+ declared?(ANONYMOUS_BLOCKARG)
62
+ end
63
+
64
+ def empty?
65
+ @stack.empty?
66
+ end
54
67
  end
55
68
 
56
69
  end
@@ -8,6 +8,10 @@ module Parser
8
8
  push
9
9
  end
10
10
 
11
+ def empty?
12
+ @stack.empty?
13
+ end
14
+
11
15
  def push
12
16
  @stack << Set.new
13
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Parser
4
- VERSION = '2.7.2.0'
4
+ VERSION = '3.1.1.0'
5
5
  end
data/parser.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  'source_code_uri' => "https://github.com/whitequark/parser/tree/v#{spec.version}"
21
21
  }
22
22
 
23
- spec.files = Dir['bin/*', 'lib/**/*.rb', 'parser.gemspec']
23
+ spec.files = Dir['bin/*', 'lib/**/*.rb', 'parser.gemspec', 'LICENSE.txt']
24
24
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ['lib']
26
26
 
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.7.2.0
4
+ version: 3.1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - whitequark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-06 00:00:00.000000000 Z
11
+ date: 2022-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -165,6 +165,7 @@ executables:
165
165
  extensions: []
166
166
  extra_rdoc_files: []
167
167
  files:
168
+ - LICENSE.txt
168
169
  - bin/ruby-parse
169
170
  - bin/ruby-rewrite
170
171
  - lib/gauntlet_parser.rb
@@ -204,6 +205,8 @@ files:
204
205
  - lib/parser/ruby27.rb
205
206
  - lib/parser/ruby28.rb
206
207
  - lib/parser/ruby30.rb
208
+ - lib/parser/ruby31.rb
209
+ - lib/parser/ruby32.rb
207
210
  - lib/parser/rubymotion.rb
208
211
  - lib/parser/runner.rb
209
212
  - lib/parser/runner/ruby_parse.rb
@@ -243,9 +246,9 @@ licenses:
243
246
  - MIT
244
247
  metadata:
245
248
  bug_tracker_uri: https://github.com/whitequark/parser/issues
246
- changelog_uri: https://github.com/whitequark/parser/blob/v2.7.2.0/CHANGELOG.md
247
- documentation_uri: https://www.rubydoc.info/gems/parser/2.7.2.0
248
- source_code_uri: https://github.com/whitequark/parser/tree/v2.7.2.0
249
+ changelog_uri: https://github.com/whitequark/parser/blob/v3.1.1.0/CHANGELOG.md
250
+ documentation_uri: https://www.rubydoc.info/gems/parser/3.1.1.0
251
+ source_code_uri: https://github.com/whitequark/parser/tree/v3.1.1.0
249
252
  post_install_message:
250
253
  rdoc_options: []
251
254
  require_paths:
@@ -261,7 +264,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
264
  - !ruby/object:Gem::Version
262
265
  version: '0'
263
266
  requirements: []
264
- rubygems_version: 3.0.6
267
+ rubygems_version: 3.2.3
265
268
  signing_key:
266
269
  specification_version: 4
267
270
  summary: A Ruby parser written in pure Ruby.