parser 2.7.2.0 → 3.0.0.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.
@@ -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__}"
@@ -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,52 @@ 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
307
306
  end
308
307
 
309
308
  private
310
309
 
310
+ # @returns [0, line_begin_of_line_1, ..., source.size + 1]
311
311
  def line_begins
312
- unless @line_begins
313
- @line_begins, index = [ [ 0, 0 ] ], 0
314
-
312
+ @line_begins ||= begin
313
+ begins = [0]
314
+ index = 0
315
315
  while index = @source.index("\n".freeze, index)
316
316
  index += 1
317
- @line_begins.unshift [ @line_begins.length, index ]
317
+ begins << index
318
318
  end
319
+ begins << @source.size + 1
320
+ begins
319
321
  end
322
+ end
320
323
 
321
- @line_begins
324
+ # @returns 0-based line index of position
325
+ def line_index_for_position(position)
326
+ @line_index_for_position[position] || begin
327
+ index = bsearch(line_begins, position) - 1
328
+ @line_index_for_position[position] = index unless @line_index_for_position.frozen?
329
+ index
330
+ end
322
331
  end
323
332
 
324
- def line_for(position)
325
- line_begins.bsearch do |line, line_begin|
326
- line_begin <= position
333
+ if Array.method_defined?(:bsearch_index) # RUBY_VERSION >= 2.3
334
+ def bsearch(line_begins, position)
335
+ line_begins.bsearch_index do |line_begin|
336
+ position < line_begin
337
+ end || line_begins.size - 1 # || only for out of bound values
338
+ end
339
+ else
340
+ def bsearch(line_begins, position)
341
+ @line_range ||= 0...line_begins.size
342
+ @line_range.bsearch do |i|
343
+ position < line_begins[i]
344
+ end || line_begins.size - 1 # || only for out of bound values
327
345
  end
328
346
  end
329
347
  end
@@ -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.2.0'
4
+ VERSION = '3.0.0.0'
5
5
  end
@@ -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.0.0.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: 2020-12-25 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
@@ -243,9 +244,9 @@ licenses:
243
244
  - MIT
244
245
  metadata:
245
246
  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
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
249
250
  post_install_message:
250
251
  rdoc_options: []
251
252
  require_paths: