antlr4-runtime 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 997ff14a7653228bf76c3d4d73fd9142681b71c8b561483612980271994ccda4
4
- data.tar.gz: 0de09a9522e1be8df33d96c4e7a9ab3f5a1b249bfd387f32711ff7ff76d778a5
3
+ metadata.gz: 5165fac45561907bd5e9cde985579f737341e00a9be7ce3778702e8069c6bc01
4
+ data.tar.gz: 84cc7c66ea621d2e44005b38fa0cf990789cac845521f8c9825a1f0b4df2492b
5
5
  SHA512:
6
- metadata.gz: f9b785bcdea37bfd5503584560edd44cb34f938b674924e17b7105d9b26f2b91321a6e7b275b14de62647693c679207594aba615d13d6403b34abd6803f9aca4
7
- data.tar.gz: 392d73af0a74e61cde891d94afce425c4283880faf30a140c2bc1488c3ff843e7497417d5e2fceae5ed94bf40bf492bf26c5642e10c67221c0c4eba652714eba
6
+ metadata.gz: 4ec4d1b05f35fe1ce3fc9cd4478ab8ce70dad8457ff39310429839e48dbe4dc7bbd258f5fefc2caee95728c5353ccb8e1d01f26082dbb71b827d3b113c90b012
7
+ data.tar.gz: c2ac519ccaadc6c7481a9a4d39b2c8a7542fb3726f934626d074e63195c20da28f6a5beef68ad5c49e983716d199a7dfbaf79e02d582f5306e0ed9256aceafd4
data/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ 0.2.2
2
+ ===
3
+ - Added a unit test for IntervalSet, fixed some bugs, removed some unused code, renamed `or_sets` to `or`.
4
+ - Fixed eql? and <=> for ATNConfig
5
+ - Fixed compare method for ATNConfigSet
6
+ - Fixed a ParseATNSimulator problem when checking for values added to closure_busy.
7
+ - Fixed a loop error in PredictionContextUtils
8
+ - Fixed use of ObjectEqualityComparator in Triple.
9
+
10
+ 0.2.1
11
+ ===
12
+ - Call IntervalSet#or_sets (instead of #or or ||) to better match the Java version.
13
+
14
+ 0.2.0
15
+ ===
16
+ - A few small fixes:
17
+ * Fixed a misspelled attr_reader in LexerCustomAction.
18
+ * Fixed a reference to IntervalSet#or, which doesn't exist.
19
+ * A few requires were missing. Decided to move all requires to autoloads.
20
+ * Added require 'spec_helper' to all spec files.
21
+ * Removed bundler as a dev dependency.
22
+ * Added LexerATNConfig.create_from_config and LexerATNConfig.create_from_config2.
23
+ * Fixed instance variable reference in LexerATNSimulator.
24
+ * Removed Gemfile.lock from source control.
25
+
26
+ 0.1.0
27
+ ===
28
+ - Initial release
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
27
  spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "simplecov", "~> 0.16.1"
28
29
  spec.extensions = %w[ext/rumourhash/extconf.rb]
29
30
  end
@@ -1,10 +1,12 @@
1
1
  module Antlr4::Runtime
2
2
 
3
3
  class Array2DHashSet
4
- INITIAL_CAPACITY = 16 # must be power of 2
5
- INITIAL_BUCKET_CAPACITY = 8
4
+ INITIAL_CAPACITY = 32 # must be power of 2
5
+ INITIAL_BUCKET_CAPACITY = 4
6
6
  LOAD_FACTOR = 0.75
7
7
 
8
+ attr_reader :buckets
9
+
8
10
  def initialize(comparator = nil, initial_capacity = INITIAL_CAPACITY, initial_bucket_capacity = INITIAL_BUCKET_CAPACITY)
9
11
  comparator.nil? ? @comparator = ObjectEqualityComparator.instance : @comparator = comparator
10
12
 
@@ -41,7 +43,7 @@ module Antlr4::Runtime
41
43
  @n_elements += 1
42
44
  return o
43
45
  end
44
- if @comparator.equals(existing, o)
46
+ if @comparator.compare(existing, o).zero?
45
47
  return existing # found existing, quit
46
48
  end
47
49
 
@@ -68,17 +70,18 @@ module Antlr4::Runtime
68
70
  while i < bucket.length
69
71
  e = bucket[i]
70
72
  if e.nil?
71
- return nil # empty slot not there
73
+ i += 1
74
+ next
72
75
  end
73
- return e if @comparator.equals(e, o)
76
+ return e if @comparator.compare(e, o).zero?
74
77
  i += 1
75
78
  end
76
79
  nil
77
80
  end
78
81
 
79
82
  def get_bucket(o)
80
- hash = @comparator.hash(o)
81
- hash & (@buckets.length - 1) # assumes len is power of 2
83
+ h = @comparator.hash(o)
84
+ h & (@buckets.length - 1) # assumes len is power of 2
82
85
  end
83
86
 
84
87
  def hash
@@ -94,7 +97,10 @@ module Antlr4::Runtime
94
97
  j = 0
95
98
  while j < bucket.length
96
99
  o = bucket[j]
97
- break if o.nil?
100
+ if o.nil?
101
+ j += 1
102
+ next
103
+ end
98
104
 
99
105
  objs << o
100
106
  j += 1
@@ -102,26 +108,16 @@ module Antlr4::Runtime
102
108
  i += 1
103
109
  end
104
110
 
105
- hash_code = MurmurHash.hash_objs(objs)
106
-
107
- if !@_hash.nil?
108
- if hash_code == @_hash
109
- puts 'Same hash_code for Array2DHashSet'
110
- else
111
- puts 'Different hash_code for Array2DHashSet'
112
- end
113
- end
114
- @_hash = hash_code
111
+ @_hash = MurmurHash.hash_objs(objs)
115
112
  end
116
113
 
117
- def equals(o)
118
- return true if o == self
114
+ def ==(o)
115
+ return true if o.equal?(self)
119
116
  return false unless o.is_a? Array2DHashSet
120
117
 
121
- other = o
122
- return false if other.size != size
118
+ return false if o.size != size
123
119
 
124
- contains_all(other)
120
+ contains_all(o)
125
121
  end
126
122
 
127
123
  def add(t)
@@ -149,12 +145,12 @@ module Antlr4::Runtime
149
145
 
150
146
  def iterator
151
147
  a = to_a
152
- a.sort(@comparator) unless @comparator.nil?
148
+ a.sort! {|a, b| @comparator.compare(a, b)} unless @comparator.nil?
153
149
  SetIterator.new(a, self)
154
150
  end
155
151
 
156
152
  def to_a
157
- a = create_bucket(size)
153
+ a = []
158
154
  i = 0
159
155
  j = 0
160
156
  while j < @buckets.length
@@ -167,9 +163,12 @@ module Antlr4::Runtime
167
163
  k = 0
168
164
  while k < bucket.length
169
165
  o = bucket[k]
170
- break if o.nil?
166
+ if o.nil?
167
+ k += 1
168
+ next
169
+ end
171
170
 
172
- a[i] = o
171
+ a << o
173
172
  i += 1
174
173
  k += 1
175
174
  end
@@ -196,12 +195,13 @@ module Antlr4::Runtime
196
195
  while i < bucket.length
197
196
  e = bucket[i]
198
197
  if e.nil?
199
- # empty slot not there
200
- return false
198
+ i += 1
199
+ next
201
200
  end
202
201
 
203
- if @comparator.eql?(e, obj) # found it
202
+ if @comparator.compare(e, obj).zero? # found it
204
203
  bucket[i] = nil
204
+ @n_elements -= 1
205
205
  return true
206
206
  end
207
207
  i += 1
@@ -210,9 +210,8 @@ module Antlr4::Runtime
210
210
  false
211
211
  end
212
212
 
213
- def contains_all(collection)
214
- if collection.is_a? Array2DHashSet
215
- s = collection
213
+ def contains_all(s)
214
+ if s.is_a? Array2DHashSet
216
215
  i = 0
217
216
  while i < s.buckets.length
218
217
  bucket = s.buckets[i]
@@ -224,7 +223,10 @@ module Antlr4::Runtime
224
223
  j = 0
225
224
  while j < bucket.length
226
225
  o = bucket[j]
227
- break if o.nil?
226
+ if o.nil?
227
+ j += 1
228
+ next
229
+ end
228
230
  return false unless contains_fast(o)
229
231
  j += 1
230
232
  end
@@ -232,8 +234,8 @@ module Antlr4::Runtime
232
234
  end
233
235
  else
234
236
  i = 0
235
- while i < collection.length
236
- o = collection[i]
237
+ while i < s.length
238
+ o = s[i]
237
239
  return false unless contains_fast(o)
238
240
  i += 1
239
241
  end
@@ -266,9 +268,12 @@ module Antlr4::Runtime
266
268
  i = 0
267
269
  j = 0
268
270
  while i < bucket.length
269
- break if bucket[i].nil?
271
+ if bucket[i].nil?
272
+ i += 1
273
+ next
274
+ end
270
275
 
271
- if c.contains(bucket[i])
276
+ if c.include?(bucket[i])
272
277
  # keep
273
278
  bucket[j] = bucket[i] if i != j
274
279
 
@@ -317,27 +322,24 @@ module Antlr4::Runtime
317
322
  buf = ''
318
323
  buf << '{'
319
324
  first = true
325
+ items = to_a
326
+ items.sort! {|a, b| @comparator.compare(a, b)} unless @comparator.nil?
327
+
320
328
  i = 0
321
- while i < @buckets.length
322
- bucket = @buckets[i]
323
- if bucket.nil?
329
+ while i < items.length
330
+ item = items[i]
331
+ if item.nil?
324
332
  i += 1
325
333
  next
326
334
  end
327
335
 
328
- j = 0
329
- while j < bucket.length
330
- o = bucket[j]
331
- break if o.nil?
332
-
333
- if first
334
- first = false
335
- else
336
- buf << ', '
337
- buf << o.to_s
338
- end
339
- j += 1
336
+ if first
337
+ first = false
338
+ else
339
+ buf << ', '
340
340
  end
341
+ buf << item.to_s
342
+
341
343
  i += 1
342
344
  end
343
345
  buf << '}'
@@ -119,7 +119,19 @@ module Antlr4::Runtime
119
119
  return false
120
120
  end
121
121
 
122
- @state.state_number == other.state.state_number && @alt == other.alt && (@context == other.context || (!@context.nil? && @context.<=>(other.context))) && @semantic_context.<=>(other.semantic_context) && precedence_filter_suppressed? == other.precedence_filter_suppressed?
122
+ return true if @state.state_number == other.state.state_number && @alt == other.alt && (@context == other.context || (!@context.nil? && @context.<=>(other.context))) && @semantic_context.<=>(other.semantic_context) && precedence_filter_suppressed? == other.precedence_filter_suppressed?
123
+ false
124
+ end
125
+
126
+ def <=>(other)
127
+ if self == other
128
+ return 0
129
+ elsif other.nil?
130
+ return 1
131
+ end
132
+
133
+ return 0 if @state.state_number == other.state.state_number && @alt == other.alt && (@context == other.context || (!@context.nil? && @context.<=>(other.context))) && @semantic_context.<=>(other.semantic_context) && precedence_filter_suppressed? == other.precedence_filter_suppressed?
134
+ -1
123
135
  end
124
136
 
125
137
  def hash
@@ -103,7 +103,7 @@ module Antlr4::Runtime
103
103
 
104
104
  class AbstractConfigHashSet < Array2DHashSet
105
105
  def initialize(comparator)
106
- super(comparator, 64, 64)
106
+ super(comparator, 128, 8)
107
107
  end
108
108
  end
109
109
 
@@ -132,11 +132,10 @@ module Antlr4::Runtime
132
132
  o.bucket_hash
133
133
  end
134
134
 
135
- def equals(a, b)
136
- return true if a == b
137
- return false if a.nil? || b.nil?
138
-
139
- a.state.state_number == b.state.state_number && a.alt == b.alt && a.semantic_context.eql?(b.semantic_context)
135
+ def compare(a, b)
136
+ return 1 if a.nil? || b.nil?
137
+ return 0 if a.state.state_number == b.state.state_number && a.alt == b.alt && a.semantic_context.eql?(b.semantic_context)
138
+ -1
140
139
  end
141
140
  end
142
141
 
@@ -111,7 +111,7 @@ module Antlr4::Runtime
111
111
  # System.err.println("at loop back: "+s.getClass().getSimpleName())
112
112
  report_unwanted_token(recognizer)
113
113
  expecting = recognizer.expected_tokens
114
- what_follows_loop_iteration_or_rule = expecting.or_sets(error_recovery_set(recognizer))
114
+ what_follows_loop_iteration_or_rule = expecting.or(error_recovery_set(recognizer))
115
115
  consume_until(recognizer, what_follows_loop_iteration_or_rule)
116
116
 
117
117
  else # do nothing if we can't identify the exact kind of ATN state end
@@ -46,9 +46,10 @@ module Antlr4::Runtime
46
46
  end
47
47
 
48
48
  def hash
49
- hash = 23
50
- hash = hash * 31 + @a
51
- hash * 31 + @b
49
+ return @_hash unless @_hash.nil?
50
+ @_hash = 23
51
+ @_hash = @_hash * 31 + @a
52
+ @_hash = @_hash * 31 + @b
52
53
  end
53
54
 
54
55
  def starts_before_disjoint(other)
@@ -30,6 +30,8 @@ module Antlr4::Runtime
30
30
 
31
31
  if el1.is_a? Interval
32
32
  add_interval(el1)
33
+ elsif el1.is_a? IntervalSet
34
+ add_all(el1)
33
35
  elsif el2.nil?
34
36
  add_interval(Interval.of(el1, el1))
35
37
  else
@@ -76,7 +78,7 @@ module Antlr4::Runtime
76
78
  @intervals << addition
77
79
  end
78
80
 
79
- def or_sets(sets)
81
+ def self.or_sets(sets)
80
82
  r = IntervalSet.new
81
83
  i = 0
82
84
  while i < sets.length
@@ -99,13 +101,6 @@ module Antlr4::Runtime
99
101
  add(interval.a, interval.b)
100
102
  i += 1
101
103
  end
102
- else
103
- i = 0
104
- list = set.to_list
105
- while i < list.length
106
- add(list[i])
107
- i += 1
108
- end
109
104
  end
110
105
 
111
106
  self
@@ -115,30 +110,21 @@ module Antlr4::Runtime
115
110
  complement_interval_set(IntervalSet.of(min_element, max_element))
116
111
  end
117
112
 
118
- def complement_interval_set(vocabulary)
119
- if vocabulary.nil? || vocabulary.is_nil
113
+ def complement_interval_set(intervalSet)
114
+ if intervalSet.nil? || intervalSet.is_nil
120
115
  return nil # nothing in common with nil set
121
116
  end
122
117
 
123
- vocabularyIS = nil
124
- if vocabulary.is_a? IntervalSet
125
- vocabularyIS = vocabulary
126
- else
127
- vocabularyIS = IntervalSet.new
128
- vocabularyIS.add_all(vocabulary)
118
+ if intervalSet.is_a? IntervalSet
119
+ intervalSet.subtract(self)
129
120
  end
130
121
 
131
- vocabularyIS.subtract(self)
132
122
  end
133
123
 
134
124
  def subtract(a)
135
125
  return IntervalSet.new self if a.nil? || a.is_nil
136
126
 
137
- return subtract_interval_sets(self, a) if a.is_a? IntervalSet
138
-
139
- other = IntervalSet.new
140
- other.add_all(a)
141
- subtract_interval_sets(self, other)
127
+ return subtract_interval_sets(self, a)
142
128
  end
143
129
 
144
130
  def subtract_interval_sets(left, right)
@@ -176,23 +162,23 @@ module Antlr4::Runtime
176
162
  if !before_current.nil?
177
163
  if !after_current.nil?
178
164
  # split the current interval into two
179
- result.intervals.set(result_i, before_current)
180
- result.intervals.add(result_i + 1, after_current)
165
+ result.intervals[result_i] = before_current
166
+ result.intervals[result_i + 1] = after_current
181
167
  result_i += 1
182
168
  right_i += 1
183
169
  next
184
170
  else # replace the current interval
185
- result.intervals.set(result_i, before_current)
171
+ result.intervals[result_i] = before_current
186
172
  result_i += 1
187
173
  next
188
174
  end
189
175
  else
190
176
  if !after_current.nil?
191
- result.intervals.set(result_i, after_current)
177
+ result.intervals[result_i] = after_current
192
178
  right_i += 1
193
179
  next
194
180
  else
195
- result.intervals.remove(result_i)
181
+ result.intervals.delete_at(result_i)
196
182
  next
197
183
  end
198
184
  end
@@ -201,7 +187,7 @@ module Antlr4::Runtime
201
187
  result
202
188
  end
203
189
 
204
- def or_list(a)
190
+ def or(a)
205
191
  o = IntervalSet.new
206
192
  o.add_all(self)
207
193
  o.add_all(a)
@@ -308,8 +294,8 @@ module Antlr4::Runtime
308
294
  i = 0
309
295
  while i < @intervals.length
310
296
  interval = @intervals[i]
311
- ints << interval.a
312
- ints << interval.b
297
+ ints << interval.a
298
+ ints << interval.b
313
299
  i += 1
314
300
  end
315
301
 
@@ -365,10 +351,6 @@ module Antlr4::Runtime
365
351
  buf
366
352
  end
367
353
 
368
- # def toString(tokenNames)
369
- # return toString(VocabularyImpl.fromTokenNames(tokenNames))
370
- # end
371
-
372
354
  def to_string_from_vocabulary(vocabulary)
373
355
  buf = ''
374
356
  return 'end' if @intervals.nil? || @intervals.empty?
@@ -423,7 +405,7 @@ module Antlr4::Runtime
423
405
  end
424
406
 
425
407
  def to_integer_list
426
- values = IntegerList.new
408
+ values = []
427
409
  n = @intervals.length
428
410
  i = 0
429
411
  while i < n
@@ -432,7 +414,7 @@ module Antlr4::Runtime
432
414
  b = interval.b
433
415
  v = a
434
416
  while v <= b
435
- values.add(v)
417
+ values.push(v)
436
418
  v += 1
437
419
  end
438
420
  i += 1
@@ -461,26 +443,6 @@ module Antlr4::Runtime
461
443
  s
462
444
  end
463
445
 
464
- def get(i)
465
- n = @intervals.length
466
- index = 0
467
- j = 0
468
- while j < n
469
- interval = @intervals[j]
470
- a = interval.a
471
- b = interval.b
472
- v = a
473
- while v <= b
474
- return v if index == i
475
-
476
- index += 1
477
- v += 1
478
- end
479
- j += 1
480
- end
481
- -1
482
- end
483
-
484
446
  def to_a
485
447
  to_integer_list
486
448
  end
@@ -515,7 +477,7 @@ module Antlr4::Runtime
515
477
  end
516
478
  # if in middle a..x..b, split interval
517
479
  if el > a && el < b # found in this interval
518
- int oldb = interval.b
480
+ oldb = interval.b
519
481
  interval.b = el - 1 # [a..x-1]
520
482
  add(el + 1, oldb) # add [x+1..b]
521
483
  end
@@ -9,10 +9,10 @@ module Antlr4::Runtime
9
9
  obj.hash
10
10
  end
11
11
 
12
- def equals(a, b)
13
- return b.nil? if a.nil?
12
+ def compare(a, b)
13
+ return 1 if a.nil? || b.nil?
14
14
 
15
- a.eql?(b)
15
+ a <=> b
16
16
  end
17
17
  end
18
18
  end
@@ -792,7 +792,7 @@ module Antlr4::Runtime
792
792
 
793
793
  c.reaches_into_outer_context += 1
794
794
 
795
- added = closure_busy.add?(c)
795
+ added = !closure_busy.add?(c).nil?
796
796
  unless added
797
797
  i += 1
798
798
  next
@@ -802,7 +802,7 @@ module Antlr4::Runtime
802
802
  new_depth -= 1
803
803
  puts('dips into outer ctx: ' << c.to_s) if @debug
804
804
  else
805
- added = closure_busy.add?(c)
805
+ added = !closure_busy.add?(c).nil?
806
806
  if !t.epsilon? && !added
807
807
  i += 1
808
808
  next
@@ -329,7 +329,7 @@ module Antlr4::Runtime
329
329
  changed = false
330
330
  parents = []
331
331
  i = 0
332
- while i < parents.length
332
+ while i < context.size
333
333
  parent = cached_context(context.get_parent(i), context_cache, visited)
334
334
  if changed || parent != context.get_parent(i)
335
335
  unless changed
@@ -89,8 +89,6 @@ module Antlr4::Runtime
89
89
  end
90
90
 
91
91
  class Operator < SemanticContext
92
- def operands
93
- end
94
92
  end
95
93
 
96
94
  class AND < Operator
@@ -17,7 +17,7 @@ module Antlr4::Runtime
17
17
  return false unless obj.is_a? Triple
18
18
  end
19
19
 
20
- ObjectEqualityComparator.instance.eql?(a, obj.a) && ObjectEqualityComparator.instance.eql?(b, obj.b) && ObjectEqualityComparator.instance.eql?(c, obj.c)
20
+ ObjectEqualityComparator.instance.compare(a, obj.a).zero? && ObjectEqualityComparator.instance.compare(b, obj.b).zero? && ObjectEqualityComparator.instance.compare(c, obj.c).zero?
21
21
  end
22
22
 
23
23
  def hash
@@ -1,5 +1,5 @@
1
1
  module Antlr4
2
2
  module Runtime
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antlr4-runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Walmsley
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-07 00:00:00.000000000 Z
11
+ date: 2019-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.16.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.16.1
41
55
  description: This gem implements a runtime for ANTLR4 in Ruby for lexers and parsers
42
56
  generated using the Ruby language target.
43
57
  email:
@@ -50,6 +64,7 @@ files:
50
64
  - ".gitignore"
51
65
  - ".rspec"
52
66
  - ".travis.yml"
67
+ - CHANGELOG.md
53
68
  - CODE_OF_CONDUCT.md
54
69
  - Gemfile
55
70
  - LICENSE.txt