antlr4-runtime 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +35 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +65 -0
  10. data/Rakefile +6 -0
  11. data/antlr4-runtime.gemspec +30 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/ext/rumourhash/Makefile +264 -0
  15. data/ext/rumourhash/extconf.rb +3 -0
  16. data/ext/rumourhash/rumourhash.c +59 -0
  17. data/lib/antlr4/runtime.rb +37 -0
  18. data/lib/antlr4/runtime/abstract_parse_tree_visitor.rb +43 -0
  19. data/lib/antlr4/runtime/abstract_predicate_transition.rb +11 -0
  20. data/lib/antlr4/runtime/action_transition.rb +29 -0
  21. data/lib/antlr4/runtime/ambiguity_info.rb +10 -0
  22. data/lib/antlr4/runtime/antlr_error_listener.rb +15 -0
  23. data/lib/antlr4/runtime/antlr_error_strategy.rb +24 -0
  24. data/lib/antlr4/runtime/antlr_file_stream.rb +17 -0
  25. data/lib/antlr4/runtime/antlr_input_stream.rb +6 -0
  26. data/lib/antlr4/runtime/array_2d_hash_set.rb +471 -0
  27. data/lib/antlr4/runtime/array_prediction_context.rb +76 -0
  28. data/lib/antlr4/runtime/atn.rb +100 -0
  29. data/lib/antlr4/runtime/atn_config.rb +140 -0
  30. data/lib/antlr4/runtime/atn_config_set.rb +150 -0
  31. data/lib/antlr4/runtime/atn_deserialization_options.rb +48 -0
  32. data/lib/antlr4/runtime/atn_deserializer.rb +737 -0
  33. data/lib/antlr4/runtime/atn_simulator.rb +69 -0
  34. data/lib/antlr4/runtime/atn_state.rb +118 -0
  35. data/lib/antlr4/runtime/atn_type.rb +8 -0
  36. data/lib/antlr4/runtime/atom_transition.rb +27 -0
  37. data/lib/antlr4/runtime/bail_error_strategy.rb +31 -0
  38. data/lib/antlr4/runtime/base_error_listener.rb +18 -0
  39. data/lib/antlr4/runtime/basic_block_start_state.rb +12 -0
  40. data/lib/antlr4/runtime/basic_state.rb +11 -0
  41. data/lib/antlr4/runtime/bit_set.rb +54 -0
  42. data/lib/antlr4/runtime/block_end_state.rb +15 -0
  43. data/lib/antlr4/runtime/block_start_state.rb +12 -0
  44. data/lib/antlr4/runtime/buffered_token_stream.rb +335 -0
  45. data/lib/antlr4/runtime/char_stream.rb +6 -0
  46. data/lib/antlr4/runtime/char_streams.rb +12 -0
  47. data/lib/antlr4/runtime/chunk.rb +4 -0
  48. data/lib/antlr4/runtime/code_point_char_stream.rb +83 -0
  49. data/lib/antlr4/runtime/common_token.rb +125 -0
  50. data/lib/antlr4/runtime/common_token_factory.rb +30 -0
  51. data/lib/antlr4/runtime/common_token_stream.rb +63 -0
  52. data/lib/antlr4/runtime/console_error_listener.rb +12 -0
  53. data/lib/antlr4/runtime/context_sensitivity_info.rb +7 -0
  54. data/lib/antlr4/runtime/decision_event_info.rb +19 -0
  55. data/lib/antlr4/runtime/decision_info.rb +36 -0
  56. data/lib/antlr4/runtime/decision_state.rb +15 -0
  57. data/lib/antlr4/runtime/default_error_strategy.rb +314 -0
  58. data/lib/antlr4/runtime/dfa.rb +97 -0
  59. data/lib/antlr4/runtime/dfa_serializer.rb +62 -0
  60. data/lib/antlr4/runtime/dfa_state.rb +109 -0
  61. data/lib/antlr4/runtime/diagnostic_error_listener.rb +58 -0
  62. data/lib/antlr4/runtime/double_key_map.rb +49 -0
  63. data/lib/antlr4/runtime/empty_prediction_context.rb +35 -0
  64. data/lib/antlr4/runtime/epsilon_transition.rb +27 -0
  65. data/lib/antlr4/runtime/equality_comparator.rb +4 -0
  66. data/lib/antlr4/runtime/error_info.rb +7 -0
  67. data/lib/antlr4/runtime/error_node.rb +5 -0
  68. data/lib/antlr4/runtime/error_node_impl.rb +12 -0
  69. data/lib/antlr4/runtime/failed_predicate_exception.rb +33 -0
  70. data/lib/antlr4/runtime/flexible_hash_map.rb +232 -0
  71. data/lib/antlr4/runtime/input_mismatch_exception.rb +20 -0
  72. data/lib/antlr4/runtime/int_stream.rb +31 -0
  73. data/lib/antlr4/runtime/integer.rb +14 -0
  74. data/lib/antlr4/runtime/interval.rb +111 -0
  75. data/lib/antlr4/runtime/interval_set.rb +540 -0
  76. data/lib/antlr4/runtime/lexer.rb +257 -0
  77. data/lib/antlr4/runtime/lexer_action.rb +12 -0
  78. data/lib/antlr4/runtime/lexer_action_executor.rb +75 -0
  79. data/lib/antlr4/runtime/lexer_action_type.rb +12 -0
  80. data/lib/antlr4/runtime/lexer_atn_config.rb +50 -0
  81. data/lib/antlr4/runtime/lexer_atn_simulator.rb +522 -0
  82. data/lib/antlr4/runtime/lexer_channel_action.rb +51 -0
  83. data/lib/antlr4/runtime/lexer_custom_action.rb +49 -0
  84. data/lib/antlr4/runtime/lexer_dfa_serializer.rb +12 -0
  85. data/lib/antlr4/runtime/lexer_indexed_custom_action.rb +49 -0
  86. data/lib/antlr4/runtime/lexer_mode_action.rb +51 -0
  87. data/lib/antlr4/runtime/lexer_more_action.rb +41 -0
  88. data/lib/antlr4/runtime/lexer_no_viable_alt_exception.rb +4 -0
  89. data/lib/antlr4/runtime/lexer_pop_mode_action.rb +41 -0
  90. data/lib/antlr4/runtime/lexer_push_mode_action.rb +51 -0
  91. data/lib/antlr4/runtime/lexer_skip_action.rb +43 -0
  92. data/lib/antlr4/runtime/lexer_type_action.rb +51 -0
  93. data/lib/antlr4/runtime/ll1_analyzer.rb +133 -0
  94. data/lib/antlr4/runtime/lookahead_event_info.rb +10 -0
  95. data/lib/antlr4/runtime/loop_end_state.rb +15 -0
  96. data/lib/antlr4/runtime/murmur_hash.rb +99 -0
  97. data/lib/antlr4/runtime/no_viable_alt_exception.rb +7 -0
  98. data/lib/antlr4/runtime/not_set_transition.rb +20 -0
  99. data/lib/antlr4/runtime/object_equality_comparator.rb +18 -0
  100. data/lib/antlr4/runtime/ordered_atn_config_set.rb +15 -0
  101. data/lib/antlr4/runtime/parse_cancellation_exception.rb +5 -0
  102. data/lib/antlr4/runtime/parse_tree.rb +7 -0
  103. data/lib/antlr4/runtime/parse_tree_listener.rb +4 -0
  104. data/lib/antlr4/runtime/parse_tree_visitor.rb +4 -0
  105. data/lib/antlr4/runtime/parser.rb +522 -0
  106. data/lib/antlr4/runtime/parser_atn_simulator.rb +1171 -0
  107. data/lib/antlr4/runtime/parser_rule_context.rb +186 -0
  108. data/lib/antlr4/runtime/plus_block_start_state.rb +11 -0
  109. data/lib/antlr4/runtime/plus_loopback_state.rb +12 -0
  110. data/lib/antlr4/runtime/precedence_predicate_transition.rb +31 -0
  111. data/lib/antlr4/runtime/predicate.rb +6 -0
  112. data/lib/antlr4/runtime/predicate_eval_info.rb +16 -0
  113. data/lib/antlr4/runtime/predicate_transition.rb +35 -0
  114. data/lib/antlr4/runtime/prediction_context.rb +103 -0
  115. data/lib/antlr4/runtime/prediction_context_cache.rb +28 -0
  116. data/lib/antlr4/runtime/prediction_context_utils.rb +407 -0
  117. data/lib/antlr4/runtime/prediction_mode.rb +213 -0
  118. data/lib/antlr4/runtime/profiling_atn_simulator.rb +149 -0
  119. data/lib/antlr4/runtime/proxy_error_listener.rb +33 -0
  120. data/lib/antlr4/runtime/range_transition.rb +29 -0
  121. data/lib/antlr4/runtime/recognition_exception.rb +17 -0
  122. data/lib/antlr4/runtime/recognizer.rb +136 -0
  123. data/lib/antlr4/runtime/rule_context.rb +131 -0
  124. data/lib/antlr4/runtime/rule_context_with_alt_num.rb +11 -0
  125. data/lib/antlr4/runtime/rule_node.rb +8 -0
  126. data/lib/antlr4/runtime/rule_start_state.rb +17 -0
  127. data/lib/antlr4/runtime/rule_stop_state.rb +12 -0
  128. data/lib/antlr4/runtime/rule_tag_token.rb +64 -0
  129. data/lib/antlr4/runtime/rule_transition.rb +29 -0
  130. data/lib/antlr4/runtime/semantic_context.rb +313 -0
  131. data/lib/antlr4/runtime/set_transition.rb +29 -0
  132. data/lib/antlr4/runtime/singleton_prediction_context.rb +56 -0
  133. data/lib/antlr4/runtime/star_block_start_state.rb +12 -0
  134. data/lib/antlr4/runtime/star_loop_entry_state.rb +17 -0
  135. data/lib/antlr4/runtime/star_loopback_state.rb +16 -0
  136. data/lib/antlr4/runtime/syntax_tree.rb +6 -0
  137. data/lib/antlr4/runtime/tag_chunk.rb +22 -0
  138. data/lib/antlr4/runtime/terminal_node.rb +5 -0
  139. data/lib/antlr4/runtime/terminal_node_impl.rb +50 -0
  140. data/lib/antlr4/runtime/text_chunk.rb +16 -0
  141. data/lib/antlr4/runtime/token.rb +13 -0
  142. data/lib/antlr4/runtime/token_stream.rb +13 -0
  143. data/lib/antlr4/runtime/token_tag_token.rb +22 -0
  144. data/lib/antlr4/runtime/tokens_start_state.rb +14 -0
  145. data/lib/antlr4/runtime/transition.rb +51 -0
  146. data/lib/antlr4/runtime/tree.rb +4 -0
  147. data/lib/antlr4/runtime/trees.rb +195 -0
  148. data/lib/antlr4/runtime/triple.rb +40 -0
  149. data/lib/antlr4/runtime/utils.rb +117 -0
  150. data/lib/antlr4/runtime/uuid.rb +46 -0
  151. data/lib/antlr4/runtime/version.rb +5 -0
  152. data/lib/antlr4/runtime/vocabulary.rb +12 -0
  153. data/lib/antlr4/runtime/vocabulary_impl.rb +82 -0
  154. data/lib/antlr4/runtime/wildcard_transition.rb +20 -0
  155. data/lib/antlr4/runtime/writable_token.rb +7 -0
  156. metadata +243 -0
@@ -0,0 +1,540 @@
1
+ require 'antlr4/runtime/lexer'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class IntervalSet
6
+ attr_accessor :intervals
7
+
8
+ def initialize(a = nil, b = nil)
9
+ @readonly = false
10
+ @intervals = []
11
+ add(a, b) unless a.nil?
12
+ end
13
+
14
+ def self.of(a, b = nil)
15
+ if b.nil?
16
+ IntervalSet.new(a)
17
+ else
18
+ IntervalSet.new(a, b)
19
+ end
20
+ end
21
+
22
+ def clear
23
+ raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly
24
+
25
+ @intervals.clear
26
+ end
27
+
28
+ def add(el1, el2 = nil)
29
+ raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly
30
+
31
+ if el1.is_a? Interval
32
+ add_interval(el1)
33
+ elsif el2.nil?
34
+ add_interval(Interval.of(el1, el1))
35
+ else
36
+ add_interval(Interval.of(el1, el2))
37
+ end
38
+ end
39
+
40
+ def add_interval(addition)
41
+ raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly
42
+
43
+ return if addition.b < addition.a
44
+
45
+ # Find where to insert the Interval and remember where it went
46
+
47
+ i = 0 # An index into @intervals
48
+ while i < @intervals.length
49
+ r = @intervals[i]
50
+ return if addition == r
51
+
52
+ if addition.adjacent(r) || !addition.disjoint(r)
53
+ bigger = addition.union(r)
54
+ @intervals[i] = bigger
55
+
56
+ while i < (@intervals.length - 1)
57
+ i += 1
58
+ next_interval = @intervals[i]
59
+ if !bigger.adjacent(next_interval) && bigger.disjoint(next_interval)
60
+ break
61
+ end
62
+
63
+ @intervals.delete_at i
64
+ i -= 1
65
+ @intervals[i] = bigger.union(next_interval)
66
+ end
67
+ return
68
+ end
69
+
70
+ if addition.starts_before_disjoint r
71
+ @intervals.insert(i, addition)
72
+ return
73
+ end
74
+ i += 1
75
+ end
76
+ @intervals << addition
77
+ end
78
+
79
+ def or_sets(sets)
80
+ r = IntervalSet.new
81
+ i = 0
82
+ while i < sets.length
83
+ r.add_all(sets[i])
84
+ i += 1
85
+ end
86
+ r
87
+ end
88
+
89
+ def add_all(set)
90
+ return this if set.nil?
91
+
92
+ if set.is_a? IntervalSet
93
+ other = set
94
+
95
+ n = other.intervals.length
96
+ i = 0
97
+ while i < n
98
+ interval = other.intervals[i]
99
+ add(interval.a, interval.b)
100
+ i += 1
101
+ 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
+ end
110
+
111
+ self
112
+ end
113
+
114
+ def complement(min_element, max_element)
115
+ complement_interval_set(IntervalSet.of(min_element, max_element))
116
+ end
117
+
118
+ def complement_interval_set(vocabulary)
119
+ if vocabulary.nil? || vocabulary.is_nil
120
+ return nil # nothing in common with nil set
121
+ end
122
+
123
+ vocabularyIS = nil
124
+ if vocabulary.is_a? IntervalSet
125
+ vocabularyIS = vocabulary
126
+ else
127
+ vocabularyIS = IntervalSet.new
128
+ vocabularyIS.add_all(vocabulary)
129
+ end
130
+
131
+ vocabularyIS.subtract(self)
132
+ end
133
+
134
+ def subtract(a)
135
+ return IntervalSet.new self if a.nil? || a.is_nil
136
+
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)
142
+ end
143
+
144
+ def subtract_interval_sets(left, right)
145
+ return new IntervalSet if left.nil? || left.is_nil
146
+
147
+ result = IntervalSet.new(left)
148
+ return result if right.nil? || right.is_nil
149
+
150
+ result_i = 0
151
+ right_i = 0
152
+ while result_i < result.intervals.length && right_i < right.intervals.length
153
+ result_interval = result.intervals[result_i]
154
+ right_interval = right.intervals[right_i]
155
+
156
+ if right_interval.b < result_interval.a
157
+ right_i += 1
158
+ next
159
+ end
160
+
161
+ if right_interval.a > result_interval.b
162
+ result_i += 1
163
+ next
164
+ end
165
+
166
+ before_current = nil
167
+ after_current = nil
168
+ if right_interval.a > result_interval.a
169
+ before_current = Interval.new(result_interval.a, right_interval.a - 1)
170
+ end
171
+
172
+ if right_interval.b < result_interval.b
173
+ after_current = Interval.new(right_interval.b + 1, result_interval.b)
174
+ end
175
+
176
+ if !before_current.nil?
177
+ if !after_current.nil?
178
+ # split the current interval into two
179
+ result.intervals.set(result_i, before_current)
180
+ result.intervals.add(result_i + 1, after_current)
181
+ result_i += 1
182
+ right_i += 1
183
+ next
184
+ else # replace the current interval
185
+ result.intervals.set(result_i, before_current)
186
+ result_i += 1
187
+ next
188
+ end
189
+ else
190
+ if !after_current.nil?
191
+ result.intervals.set(result_i, after_current)
192
+ right_i += 1
193
+ next
194
+ else
195
+ result.intervals.remove(result_i)
196
+ next
197
+ end
198
+ end
199
+ end
200
+
201
+ result
202
+ end
203
+
204
+ def or_list(a)
205
+ o = IntervalSet.new
206
+ o.add_all(self)
207
+ o.add_all(a)
208
+ o
209
+ end
210
+
211
+ def and(other)
212
+ if other.nil? # || !(other.is_a? IntervalSet) )
213
+ return nil # nothing in common with nil set
214
+ end
215
+
216
+ my_intervals = @intervals
217
+ their_intervals = other.intervals
218
+ intersection = nil
219
+ my_size = my_intervals.length
220
+ their_size = their_intervals.length
221
+ i = 0
222
+ j = 0
223
+
224
+ while i < my_size && j < their_size
225
+ mine = my_intervals[i]
226
+ theirs = their_intervals[j]
227
+ # System.out.println("mine="+mine+" and theirs="+theirs)
228
+ if mine.starts_before_disjoint(theirs)
229
+ # move this iterator looking for interval that might overlap
230
+ i += 1
231
+ elsif theirs.starts_before_disjoint(mine)
232
+ # move other iterator looking for interval that might overlap
233
+ j += 1
234
+ elsif mine.properlyContains(theirs)
235
+ # overlap, add intersection, get next theirs
236
+ intersection = IntervalSet.new if intersection.nil?
237
+ intersection.add(mine.intersection(theirs))
238
+ j += 1
239
+ elsif theirs.properlyContains(mine)
240
+ # overlap, add intersection, get next mine
241
+ intersection = IntervalSet.new if intersection.nil?
242
+ intersection.add(mine.intersection(theirs))
243
+ i += 1
244
+ elsif !mine.disjoint(theirs)
245
+ # overlap, add intersection
246
+ intersection = IntervalSet.new if intersection.nil?
247
+ intersection.add(mine.intersection(theirs))
248
+ # Move the iterator of lower range [a..b], but not
249
+ # the upper range as it may contain elements that will collide
250
+ # with the next iterator. So, if mine=[0..115] and
251
+ # theirs=[115..200], then intersection is 115 and move mine
252
+ # but not theirs as theirs may collide with the next range
253
+ # in thisIter.
254
+ # move both iterators to next ranges
255
+ if mine.startsAfterNonDisjoint(theirs)
256
+ j += 1
257
+ elsif theirs.startsAfterNonDisjoint(mine)
258
+ i += 1
259
+ end
260
+ end
261
+ end
262
+ return IntervalSet.new if intersection.nil?
263
+
264
+ intersection
265
+ end
266
+
267
+ def contains(el)
268
+ n = @intervals.length
269
+ l = 0
270
+ r = n - 1
271
+ # Binary search for the element in the (sorted,
272
+ # disjoint) array of @intervals.
273
+ while l <= r
274
+ m = (l + r) / 2
275
+ interval = @intervals[m]
276
+ a = interval.a
277
+ b = interval.b
278
+ if b < el
279
+ l = m + 1
280
+ elsif a > el
281
+ r = m - 1
282
+ else # el >= a && el <= b
283
+ return true
284
+ end
285
+ end
286
+ false
287
+ end
288
+
289
+ def is_nil
290
+ @intervals.nil? || @intervals.empty?
291
+ end
292
+
293
+ def max_element
294
+ raise StandardEror, 'set is empty' if is_nil
295
+
296
+ last = @intervals[-1]
297
+ last.b
298
+ end
299
+
300
+ def min_element
301
+ raise StandardEror, 'set is empty' if is_nil
302
+
303
+ @intervals[0].a
304
+ end
305
+
306
+ def hash
307
+ ints = []
308
+ i = 0
309
+ while i < @intervals.length
310
+ interval = @intervals[i]
311
+ ints << interval.a
312
+ ints << interval.b
313
+ i += 1
314
+ end
315
+
316
+ hash_code = MurmurHash.hash_ints(ints)
317
+
318
+ if !@_hash.nil?
319
+ if hash_code == @_hash
320
+ puts 'Same hash_code for IntervalSet'
321
+ else
322
+ puts 'Different hash_code for IntervalSet'
323
+ end
324
+ end
325
+ @_hash = hash_code
326
+ end
327
+
328
+ def ==(obj)
329
+ return false if obj.nil? || !(obj.is_a? IntervalSet)
330
+
331
+ other = obj
332
+ @intervals == other.intervals
333
+ end
334
+
335
+ def to_string(elem_are_char = false)
336
+ buf = ''
337
+ return 'end' if @intervals.nil? || @intervals.empty?
338
+
339
+ buf << '' if size > 1
340
+
341
+ i = 0
342
+ while i < @intervals.length
343
+ interval = @intervals[i]
344
+ a = interval.a
345
+ b = interval.b
346
+ if a == b
347
+ if a == Token::EOF
348
+ buf << '<EOF>'
349
+ elsif elem_are_char
350
+ buf << "'" << a << "'"
351
+ else
352
+ buf << a
353
+ end
354
+ else
355
+ if elem_are_char
356
+ buf << "'" << a << "'..'" << b << "'"
357
+ else
358
+ buf << a << '..' << b
359
+ end
360
+ end
361
+ buf << ', ' if i < @intervals.length
362
+ i += 1
363
+ end
364
+ buf << 'end' if size > 1
365
+ buf
366
+ end
367
+
368
+ # def toString(tokenNames)
369
+ # return toString(VocabularyImpl.fromTokenNames(tokenNames))
370
+ # end
371
+
372
+ def to_string_from_vocabulary(vocabulary)
373
+ buf = ''
374
+ return 'end' if @intervals.nil? || @intervals.empty?
375
+
376
+ buf << '' if size > 1
377
+ i = 0
378
+ while i < @intervals.length
379
+ interval = @intervals[i]
380
+ a = interval.a
381
+ b = interval.b
382
+ if a == b
383
+ buf << element_name_in_vocabulary(vocabulary, a)
384
+ else
385
+ j = a
386
+ while j <= b
387
+ buf << ', ' if j > a
388
+ buf << element_name_in_vocabulary(vocabulary, j)
389
+ j += 1
390
+ end
391
+ end
392
+ buf << ', ' if i < @intervals.length
393
+ i += 1
394
+ end
395
+ buf << 'end' if size > 1
396
+ buf
397
+ end
398
+
399
+ def element_name_in_vocabulary(vocabulary, a)
400
+ if a == Token::EOF
401
+ '<EOF>'
402
+ elsif a == Token::EPSILON
403
+ '<EPSILON>'
404
+ else
405
+ vocabulary.display_name(a)
406
+ end
407
+ end
408
+
409
+ def size
410
+ n = 0
411
+ num_intervals = @intervals.length
412
+ if num_intervals == 1
413
+ first_interval = @intervals[0]
414
+ return first_interval.b - first_interval.a + 1
415
+ end
416
+ i = 0
417
+ while i < num_intervals
418
+ interval = @intervals[i]
419
+ n += (interval.b - interval.a + 1)
420
+ i += 1
421
+ end
422
+ n
423
+ end
424
+
425
+ def to_integer_list
426
+ values = IntegerList.new
427
+ n = @intervals.length
428
+ i = 0
429
+ while i < n
430
+ interval = @intervals[i]
431
+ a = interval.a
432
+ b = interval.b
433
+ v = a
434
+ while v <= b
435
+ values.add(v)
436
+ v += 1
437
+ end
438
+ i += 1
439
+ end
440
+ values
441
+ end
442
+
443
+ def to_list
444
+ to_integer_list
445
+ end
446
+
447
+ def to_set
448
+ s = Set.new
449
+ k = 0
450
+ while k < @intervals.length
451
+ i = @intervals[k]
452
+ a = i.a
453
+ b = i.b
454
+ v = a
455
+ while v <= b
456
+ s.add(v)
457
+ v += 1
458
+ end
459
+ k += 1
460
+ end
461
+ s
462
+ end
463
+
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
+ def to_a
485
+ to_integer_list
486
+ end
487
+
488
+ def remove(el)
489
+ raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly
490
+
491
+ n = @intervals.length
492
+ i = 0
493
+ while i < n
494
+ interval = @intervals[i]
495
+ a = interval.a
496
+ b = interval.b
497
+ if el < a
498
+ break # list is sorted and el is before this interval not here
499
+ end
500
+
501
+ # if whole interval x..x, rm
502
+ if el == a && el == b
503
+ @intervals.delete_at(i)
504
+ break
505
+ end
506
+ # if on left edge x..b, adjust left
507
+ if el == a
508
+ interval.a += 1
509
+ break
510
+ end
511
+ # if on right edge a..x, adjust right
512
+ if el == b
513
+ interval.b -= 1
514
+ break
515
+ end
516
+ # if in middle a..x..b, split interval
517
+ if el > a && el < b # found in this interval
518
+ int oldb = interval.b
519
+ interval.b = el - 1 # [a..x-1]
520
+ add(el + 1, oldb) # add [x+1..b]
521
+ end
522
+ i += 1
523
+ end
524
+ end
525
+
526
+ def readonly(readonly)
527
+ if @readonly && !readonly
528
+ raise IllegalStateException, "can't alter readonly IntervalSet"
529
+ end
530
+
531
+ @readonly = readonly
532
+ end
533
+
534
+ @@complete_char_set = IntervalSet.of(Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE)
535
+ @@complete_char_set.readonly(true)
536
+
537
+ @@empty_set = IntervalSet.new
538
+ @@empty_set.readonly(true)
539
+ end
540
+ end