parser 3.0.0.0 → 3.0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/parser/runner.rb CHANGED
@@ -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
@@ -305,6 +305,11 @@ module Parser
305
305
  super
306
306
  end
307
307
 
308
+ # :nodoc:
309
+ def inspect
310
+ "#<#{self.class} #{name}>"
311
+ end
312
+
308
313
  private
309
314
 
310
315
  # @returns [0, line_begin_of_line_1, ..., source.size + 1]
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Parser
4
- VERSION = '3.0.0.0'
4
+ VERSION = '3.0.3.0'
5
5
  end
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: 3.0.0.0
4
+ version: 3.0.3.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-12-25 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -205,6 +205,7 @@ files:
205
205
  - lib/parser/ruby27.rb
206
206
  - lib/parser/ruby28.rb
207
207
  - lib/parser/ruby30.rb
208
+ - lib/parser/ruby31.rb
208
209
  - lib/parser/rubymotion.rb
209
210
  - lib/parser/runner.rb
210
211
  - lib/parser/runner/ruby_parse.rb
@@ -244,9 +245,9 @@ licenses:
244
245
  - MIT
245
246
  metadata:
246
247
  bug_tracker_uri: https://github.com/whitequark/parser/issues
247
- changelog_uri: https://github.com/whitequark/parser/blob/v3.0.0.0/CHANGELOG.md
248
- documentation_uri: https://www.rubydoc.info/gems/parser/3.0.0.0
249
- source_code_uri: https://github.com/whitequark/parser/tree/v3.0.0.0
248
+ changelog_uri: https://github.com/whitequark/parser/blob/v3.0.3.0/CHANGELOG.md
249
+ documentation_uri: https://www.rubydoc.info/gems/parser/3.0.3.0
250
+ source_code_uri: https://github.com/whitequark/parser/tree/v3.0.3.0
250
251
  post_install_message:
251
252
  rdoc_options: []
252
253
  require_paths:
@@ -262,7 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
263
  - !ruby/object:Gem::Version
263
264
  version: '0'
264
265
  requirements: []
265
- rubygems_version: 3.0.6
266
+ rubygems_version: 3.2.3
266
267
  signing_key:
267
268
  specification_version: 4
268
269
  summary: A Ruby parser written in pure Ruby.