parser 2.7.1.5 → 3.0.2.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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parser/all.rb +1 -0
  3. data/lib/parser/ast/processor.rb +3 -0
  4. data/lib/parser/base.rb +1 -0
  5. data/lib/parser/builders/default.rb +126 -13
  6. data/lib/parser/context.rb +4 -0
  7. data/lib/parser/current.rb +16 -7
  8. data/lib/parser/current_arg_stack.rb +5 -2
  9. data/lib/parser/lexer.rb +872 -802
  10. data/lib/parser/max_numparam_stack.rb +12 -4
  11. data/lib/parser/messages.rb +1 -0
  12. data/lib/parser/meta.rb +4 -3
  13. data/lib/parser/ruby18.rb +6 -2
  14. data/lib/parser/ruby27.rb +3763 -3704
  15. data/lib/parser/ruby28.rb +8047 -0
  16. data/lib/parser/ruby30.rb +3657 -3649
  17. data/lib/parser/ruby31.rb +8226 -0
  18. data/lib/parser/runner.rb +6 -1
  19. data/lib/parser/source/buffer.rb +50 -27
  20. data/lib/parser/source/comment.rb +13 -0
  21. data/lib/parser/source/comment/associator.rb +17 -4
  22. data/lib/parser/source/tree_rewriter.rb +27 -0
  23. data/lib/parser/static_environment.rb +4 -0
  24. data/lib/parser/variables_stack.rb +4 -0
  25. data/lib/parser/version.rb +1 -1
  26. data/parser.gemspec +1 -20
  27. metadata +8 -95
  28. data/.travis.yml +0 -41
  29. data/.yardopts +0 -21
  30. data/CHANGELOG.md +0 -1137
  31. data/CONTRIBUTING.md +0 -17
  32. data/Gemfile +0 -10
  33. data/README.md +0 -309
  34. data/Rakefile +0 -167
  35. data/ci/run_rubocop_specs +0 -14
  36. data/doc/AST_FORMAT.md +0 -2284
  37. data/doc/CUSTOMIZATION.md +0 -37
  38. data/doc/INTERNALS.md +0 -21
  39. data/doc/css/.gitkeep +0 -0
  40. data/doc/css/common.css +0 -68
  41. data/lib/parser/lexer.rl +0 -2550
  42. data/lib/parser/macruby.y +0 -2208
  43. data/lib/parser/ruby18.y +0 -1936
  44. data/lib/parser/ruby19.y +0 -2185
  45. data/lib/parser/ruby20.y +0 -2363
  46. data/lib/parser/ruby21.y +0 -2364
  47. data/lib/parser/ruby22.y +0 -2371
  48. data/lib/parser/ruby23.y +0 -2377
  49. data/lib/parser/ruby24.y +0 -2415
  50. data/lib/parser/ruby25.y +0 -2412
  51. data/lib/parser/ruby26.y +0 -2420
  52. data/lib/parser/ruby27.y +0 -2949
  53. data/lib/parser/ruby30.y +0 -3048
  54. data/lib/parser/rubymotion.y +0 -2192
  55. data/test/bug_163/fixtures/input.rb +0 -5
  56. data/test/bug_163/fixtures/output.rb +0 -5
  57. data/test/bug_163/rewriter.rb +0 -20
  58. data/test/helper.rb +0 -103
  59. data/test/parse_helper.rb +0 -328
  60. data/test/racc_coverage_helper.rb +0 -133
  61. data/test/test_ast_processor.rb +0 -32
  62. data/test/test_base.rb +0 -31
  63. data/test/test_current.rb +0 -31
  64. data/test/test_diagnostic.rb +0 -95
  65. data/test/test_diagnostic_engine.rb +0 -59
  66. data/test/test_encoding.rb +0 -99
  67. data/test/test_lexer.rb +0 -3617
  68. data/test/test_lexer_stack_state.rb +0 -78
  69. data/test/test_meta.rb +0 -12
  70. data/test/test_parse_helper.rb +0 -83
  71. data/test/test_parser.rb +0 -9986
  72. data/test/test_runner_parse.rb +0 -56
  73. data/test/test_runner_rewrite.rb +0 -47
  74. data/test/test_source_buffer.rb +0 -165
  75. data/test/test_source_comment.rb +0 -36
  76. data/test/test_source_comment_associator.rb +0 -399
  77. data/test/test_source_map.rb +0 -14
  78. data/test/test_source_range.rb +0 -192
  79. data/test/test_source_rewriter.rb +0 -541
  80. data/test/test_source_rewriter_action.rb +0 -46
  81. data/test/test_source_tree_rewriter.rb +0 -361
  82. data/test/test_static_environment.rb +0 -45
  83. data/test/using_tree_rewriter/fixtures/input.rb +0 -3
  84. data/test/using_tree_rewriter/fixtures/output.rb +0 -3
  85. data/test/using_tree_rewriter/using_tree_rewriter.rb +0 -9
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,11 @@ 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
+
121
126
  opts.on '--mac', 'Parse as MacRuby 0.12 would' do
122
127
  require 'parser/macruby'
123
128
  @parser_class = Parser::MacRuby
@@ -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
@@ -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
  #
@@ -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
@@ -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
@@ -51,6 +51,10 @@ module Parser
51
51
  def declared_forward_args?
52
52
  declared?(FORWARD_ARGS)
53
53
  end
54
+
55
+ def empty?
56
+ @stack.empty?
57
+ end
54
58
  end
55
59
 
56
60
  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.1.5'
4
+ VERSION = '3.0.2.0'
5
5
  end
data/parser.gemspec CHANGED
@@ -20,27 +20,8 @@ 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 = `git ls-files`.split + %w(
24
- lib/parser/lexer.rb
25
- lib/parser/ruby18.rb
26
- lib/parser/ruby19.rb
27
- lib/parser/ruby20.rb
28
- lib/parser/ruby21.rb
29
- lib/parser/ruby22.rb
30
- lib/parser/ruby23.rb
31
- lib/parser/ruby24.rb
32
- lib/parser/ruby25.rb
33
- lib/parser/ruby26.rb
34
- lib/parser/ruby27.rb
35
- lib/parser/ruby30.rb
36
- lib/parser/macruby.rb
37
- lib/parser/rubymotion.rb
38
- ) - %w(
39
- .gitignore
40
- )
41
-
23
+ spec.files = Dir['bin/*', 'lib/**/*.rb', 'parser.gemspec', 'LICENSE.txt']
42
24
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
43
- spec.test_files = spec.files.grep(%r{^test/})
44
25
  spec.require_paths = ['lib']
45
26
 
46
27
  spec.required_ruby_version = '>= 2.0.0'
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.1.5
4
+ version: 3.0.2.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-09-24 00:00:00.000000000 Z
11
+ date: 2021-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -165,22 +165,9 @@ executables:
165
165
  extensions: []
166
166
  extra_rdoc_files: []
167
167
  files:
168
- - ".travis.yml"
169
- - ".yardopts"
170
- - CHANGELOG.md
171
- - CONTRIBUTING.md
172
- - Gemfile
173
168
  - LICENSE.txt
174
- - README.md
175
- - Rakefile
176
169
  - bin/ruby-parse
177
170
  - bin/ruby-rewrite
178
- - ci/run_rubocop_specs
179
- - doc/AST_FORMAT.md
180
- - doc/CUSTOMIZATION.md
181
- - doc/INTERNALS.md
182
- - doc/css/.gitkeep
183
- - doc/css/common.css
184
171
  - lib/gauntlet_parser.rb
185
172
  - lib/parser.rb
186
173
  - lib/parser/all.rb
@@ -197,41 +184,29 @@ files:
197
184
  - lib/parser/diagnostic.rb
198
185
  - lib/parser/diagnostic/engine.rb
199
186
  - lib/parser/lexer.rb
200
- - lib/parser/lexer.rl
201
187
  - lib/parser/lexer/dedenter.rb
202
188
  - lib/parser/lexer/explanation.rb
203
189
  - lib/parser/lexer/literal.rb
204
190
  - lib/parser/lexer/stack_state.rb
205
191
  - lib/parser/macruby.rb
206
- - lib/parser/macruby.y
207
192
  - lib/parser/max_numparam_stack.rb
208
193
  - lib/parser/messages.rb
209
194
  - lib/parser/meta.rb
210
195
  - lib/parser/rewriter.rb
211
196
  - lib/parser/ruby18.rb
212
- - lib/parser/ruby18.y
213
197
  - lib/parser/ruby19.rb
214
- - lib/parser/ruby19.y
215
198
  - lib/parser/ruby20.rb
216
- - lib/parser/ruby20.y
217
199
  - lib/parser/ruby21.rb
218
- - lib/parser/ruby21.y
219
200
  - lib/parser/ruby22.rb
220
- - lib/parser/ruby22.y
221
201
  - lib/parser/ruby23.rb
222
- - lib/parser/ruby23.y
223
202
  - lib/parser/ruby24.rb
224
- - lib/parser/ruby24.y
225
203
  - lib/parser/ruby25.rb
226
- - lib/parser/ruby25.y
227
204
  - lib/parser/ruby26.rb
228
- - lib/parser/ruby26.y
229
205
  - lib/parser/ruby27.rb
230
- - lib/parser/ruby27.y
206
+ - lib/parser/ruby28.rb
231
207
  - lib/parser/ruby30.rb
232
- - lib/parser/ruby30.y
208
+ - lib/parser/ruby31.rb
233
209
  - lib/parser/rubymotion.rb
234
- - lib/parser/rubymotion.y
235
210
  - lib/parser/runner.rb
236
211
  - lib/parser/runner/ruby_parse.rb
237
212
  - lib/parser/runner/ruby_rewrite.rb
@@ -265,45 +240,14 @@ files:
265
240
  - lib/parser/variables_stack.rb
266
241
  - lib/parser/version.rb
267
242
  - parser.gemspec
268
- - test/bug_163/fixtures/input.rb
269
- - test/bug_163/fixtures/output.rb
270
- - test/bug_163/rewriter.rb
271
- - test/helper.rb
272
- - test/parse_helper.rb
273
- - test/racc_coverage_helper.rb
274
- - test/test_ast_processor.rb
275
- - test/test_base.rb
276
- - test/test_current.rb
277
- - test/test_diagnostic.rb
278
- - test/test_diagnostic_engine.rb
279
- - test/test_encoding.rb
280
- - test/test_lexer.rb
281
- - test/test_lexer_stack_state.rb
282
- - test/test_meta.rb
283
- - test/test_parse_helper.rb
284
- - test/test_parser.rb
285
- - test/test_runner_parse.rb
286
- - test/test_runner_rewrite.rb
287
- - test/test_source_buffer.rb
288
- - test/test_source_comment.rb
289
- - test/test_source_comment_associator.rb
290
- - test/test_source_map.rb
291
- - test/test_source_range.rb
292
- - test/test_source_rewriter.rb
293
- - test/test_source_rewriter_action.rb
294
- - test/test_source_tree_rewriter.rb
295
- - test/test_static_environment.rb
296
- - test/using_tree_rewriter/fixtures/input.rb
297
- - test/using_tree_rewriter/fixtures/output.rb
298
- - test/using_tree_rewriter/using_tree_rewriter.rb
299
243
  homepage: https://github.com/whitequark/parser
300
244
  licenses:
301
245
  - MIT
302
246
  metadata:
303
247
  bug_tracker_uri: https://github.com/whitequark/parser/issues
304
- changelog_uri: https://github.com/whitequark/parser/blob/v2.7.1.5/CHANGELOG.md
305
- documentation_uri: https://www.rubydoc.info/gems/parser/2.7.1.5
306
- source_code_uri: https://github.com/whitequark/parser/tree/v2.7.1.5
248
+ changelog_uri: https://github.com/whitequark/parser/blob/v3.0.2.0/CHANGELOG.md
249
+ documentation_uri: https://www.rubydoc.info/gems/parser/3.0.2.0
250
+ source_code_uri: https://github.com/whitequark/parser/tree/v3.0.2.0
307
251
  post_install_message:
308
252
  rdoc_options: []
309
253
  require_paths:
@@ -323,35 +267,4 @@ rubygems_version: 3.0.6
323
267
  signing_key:
324
268
  specification_version: 4
325
269
  summary: A Ruby parser written in pure Ruby.
326
- test_files:
327
- - test/bug_163/fixtures/input.rb
328
- - test/bug_163/fixtures/output.rb
329
- - test/bug_163/rewriter.rb
330
- - test/helper.rb
331
- - test/parse_helper.rb
332
- - test/racc_coverage_helper.rb
333
- - test/test_ast_processor.rb
334
- - test/test_base.rb
335
- - test/test_current.rb
336
- - test/test_diagnostic.rb
337
- - test/test_diagnostic_engine.rb
338
- - test/test_encoding.rb
339
- - test/test_lexer.rb
340
- - test/test_lexer_stack_state.rb
341
- - test/test_meta.rb
342
- - test/test_parse_helper.rb
343
- - test/test_parser.rb
344
- - test/test_runner_parse.rb
345
- - test/test_runner_rewrite.rb
346
- - test/test_source_buffer.rb
347
- - test/test_source_comment.rb
348
- - test/test_source_comment_associator.rb
349
- - test/test_source_map.rb
350
- - test/test_source_range.rb
351
- - test/test_source_rewriter.rb
352
- - test/test_source_rewriter_action.rb
353
- - test/test_source_tree_rewriter.rb
354
- - test/test_static_environment.rb
355
- - test/using_tree_rewriter/fixtures/input.rb
356
- - test/using_tree_rewriter/fixtures/output.rb
357
- - test/using_tree_rewriter/using_tree_rewriter.rb
270
+ test_files: []