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,48 @@
1
+ require 'singleton'
2
+
3
+ module Antlr4::Runtime
4
+ class ATNDeserializationOptions
5
+ include Singleton
6
+
7
+ def initialize(read_only = true, options = nil)
8
+ @read_only = read_only
9
+ if !options.nil?
10
+ @verify_atn = options.verify_atn
11
+ @generate_rule_bypass_transitions = options.generate_rule_bypass_transitions
12
+ else
13
+ @verify_atn = true
14
+ @generate_rule_bypass_transitions = false
15
+ end
16
+ end
17
+
18
+ def read_only?
19
+ @read_only
20
+ end
21
+
22
+ def make_read_only
23
+ @read_only = true
24
+ end
25
+
26
+ def verify_atn?
27
+ @verify_atn
28
+ end
29
+
30
+ def verify_atn(verify_atn)
31
+ throw_if_read_only
32
+ @verify_atn = verify_atn
33
+ end
34
+
35
+ def generate_rule_bypass_transitions?
36
+ @generate_rule_bypass_transitions
37
+ end
38
+
39
+ def generate_rule_bypass_transitions(generate_rule_bypass_transitions)
40
+ throw_if_read_only
41
+ @generate_rule_bypass_transitions = generate_rule_bypass_transitions
42
+ end
43
+
44
+ def throw_if_read_only
45
+ raise IllegalStateException, 'The object is read only.' if read_only?
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,737 @@
1
+ require 'antlr4/runtime/uuid'
2
+ require 'antlr4/runtime/atn_deserialization_options'
3
+ require 'antlr4/runtime/atn_type'
4
+ require 'antlr4/runtime/atn_state'
5
+ require 'antlr4/runtime/interval_set'
6
+ require 'antlr4/runtime/transition'
7
+ require 'antlr4/runtime/epsilon_transition'
8
+ require 'antlr4/runtime/set_transition'
9
+ require 'antlr4/runtime/not_set_transition'
10
+ require 'antlr4/runtime/action_transition'
11
+ require 'antlr4/runtime/atom_transition'
12
+ require 'antlr4/runtime/rule_transition'
13
+ require 'antlr4/runtime/range_transition'
14
+ require 'antlr4/runtime/lexer_action_type'
15
+ require 'antlr4/runtime/lexer_skip_action'
16
+ require 'antlr4/runtime/lexer_push_mode_action'
17
+ require 'antlr4/runtime/lexer_pop_mode_action'
18
+ require 'antlr4/runtime/lexer_type_action'
19
+ require 'antlr4/runtime/lexer_channel_action'
20
+
21
+ require 'antlr4/runtime/tokens_start_state'
22
+ require 'antlr4/runtime/block_start_state'
23
+ require 'antlr4/runtime/basic_block_start_state'
24
+ require 'antlr4/runtime/star_block_start_state'
25
+ require 'antlr4/runtime/star_loop_entry_state'
26
+ require 'antlr4/runtime/block_end_state'
27
+ require 'antlr4/runtime/loop_end_state'
28
+ require 'antlr4/runtime/rule_start_state'
29
+ require 'antlr4/runtime/rule_stop_state'
30
+ require 'antlr4/runtime/basic_state'
31
+ require 'antlr4/runtime/plus_block_start_state'
32
+ require 'antlr4/runtime/plus_loopback_state'
33
+ require 'antlr4/runtime/star_loopback_state'
34
+
35
+ module Antlr4::Runtime
36
+
37
+ class UnsupportedOperationException < StandardError
38
+ end
39
+
40
+ class IllegalStateException < StandardError
41
+ end
42
+
43
+ class ATNDeserializer
44
+ SERIALIZED_VERSION = 3
45
+
46
+ class << self
47
+ attr_accessor :SERIALIZED_Uuid
48
+ attr_accessor :SERIALIZED_VERSION
49
+ @@base_serialized_uuid = UUID.from_string('33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3')
50
+ @@added_precedence_transitions = UUID.from_string('1DA0C57D-6C06-438A-9B27-10BCB3CE0F61')
51
+ @@added_lexer_actions = UUID.from_string('AADB8D7E-AEEF-4415-AD2B-8204D6CF042E')
52
+ @@added_unicode_smp = UUID.from_string('59627784-3BE5-417A-B9EB-8131A7286089')
53
+ @@supported_uuids = [@@base_serialized_uuid, @@added_precedence_transitions, @@added_lexer_actions, @@added_unicode_smp]
54
+
55
+ @@serialized_uuid = @@added_unicode_smp
56
+ end
57
+
58
+ class UnicodeDeserializer
59
+ def read_unicode(data, p)
60
+ data[p]
61
+ end
62
+
63
+ def size
64
+ 1
65
+ end
66
+ end
67
+
68
+ class UnicodeDeserializingMode
69
+ UNICODE_BMP = 1
70
+ UNICODE_SMP = 2
71
+ end
72
+
73
+ def self.unicode_deserializer(mode)
74
+ if mode == UnicodeDeserializingMode::UNICODE_BMP
75
+ UnicodeDeserializer.new
76
+ else
77
+ UnicodeDeserializer.new
78
+ end
79
+ end
80
+
81
+ def initialize(deserialization_options = nil)
82
+ @deserialization_options = if deserialization_options.nil?
83
+ ATNDeserializationOptions.instance
84
+ else
85
+ deserialization_options
86
+ end
87
+ end
88
+
89
+ def feature_supported?(feature, actual_uuid)
90
+ feature_index = @@supported_uuids.index(feature)
91
+ return false if feature_index < 0
92
+
93
+ @@supported_uuids.index(actual_uuid) >= feature_index
94
+ end
95
+
96
+ def deserialize(serialized_data)
97
+ data = serialized_data.codepoints
98
+
99
+ i = 1
100
+ while i < data.length
101
+ data[i] = data[i] - 2
102
+ i += 1
103
+ end
104
+
105
+ p = 0
106
+ version = data[p]
107
+ p += 1
108
+ if version != SERIALIZED_VERSION
109
+ reason = sprintf format("Could not deserialize ATN with version %d (expected %d).\n", version, SERIALIZED_VERSION)
110
+ raise UnsupportedOperationException, reason
111
+ end
112
+
113
+ uuid = to_uuid(data, p)
114
+ p += 8
115
+ unless @@supported_uuids.include?(uuid)
116
+ reason = sprintf format("Could not deserialize ATN with Uuid %s (expected %s or a legacy Uuid).\n", uuid, @@serialized_uuid)
117
+ raise UnsupportedOperationException, reason
118
+ end
119
+
120
+ supports_precedence_predicates = feature_supported?(@@added_precedence_transitions, uuid)
121
+ supports_lexer_actions = feature_supported?(@@added_lexer_actions, uuid)
122
+
123
+ grammar_type = ATNType::VALUES[data[p]]
124
+ p += 1
125
+ max_token_type = data[p]
126
+ p += 1
127
+ atn = ATN.new(grammar_type, max_token_type)
128
+
129
+ loop_back_state_numbers = []
130
+ end_state_numbers = []
131
+ n_states = data[p]
132
+ p += 1
133
+ i = 0
134
+ while i < n_states
135
+ s_type = data[p]
136
+ p += 1
137
+ if s_type == ATNState::INVALID_TYPE
138
+ atn.add_state(nil)
139
+ i += 1
140
+ next
141
+ end
142
+
143
+ rule_index = data[p]
144
+ p += 1
145
+ rule_index = -1 if rule_index == 0xFFFF
146
+
147
+ s = state_factory(s_type, rule_index)
148
+ if s_type == ATNState::LOOP_END
149
+ loop_back_state_number = data[p]
150
+ p += 1
151
+ pair = OpenStruct.new
152
+ pair.a = s
153
+ pair.b = loop_back_state_number
154
+ loop_back_state_numbers << pair
155
+ elsif s.is_a? BlockStartState
156
+ end_state_number = data[p]
157
+ p += 1
158
+ pair = OpenStruct.new
159
+ pair.a = s
160
+ pair.b = end_state_number
161
+ end_state_numbers << pair
162
+ end
163
+ atn.add_state(s)
164
+ i += 1
165
+ end
166
+
167
+ i = 0
168
+ while i < loop_back_state_numbers.length
169
+ pair = loop_back_state_numbers[i]
170
+ pair.a.loopback_state = atn.states[pair.b]
171
+ i += 1
172
+ end
173
+
174
+ i = 0
175
+ while i < end_state_numbers.length
176
+ pair = end_state_numbers[i]
177
+ pair.a.end_state = atn.states[pair.b]
178
+ i += 1
179
+ end
180
+
181
+ num_non_greedy_states = data[p]
182
+ p += 1
183
+ i = 0
184
+ while i < num_non_greedy_states
185
+ state_number = data[p]
186
+ p += 1
187
+ atn.states[state_number].non_greedy = true
188
+ i += 1
189
+ end
190
+
191
+ if supports_precedence_predicates
192
+ num_precedence_states = data[p]
193
+ p += 1
194
+ i = 0
195
+ while i < num_precedence_states
196
+ state_number = data[p]
197
+ p += 1
198
+ atn.states[state_number].is_left_recursive_rule = true
199
+ i += 1
200
+ end
201
+ end
202
+
203
+ nrules = data[p]
204
+ p += 1
205
+ atn.rule_to_token_type = [] if atn.grammar_type == ATNType::LEXER
206
+
207
+ atn.rule_to_start_state = []
208
+ i = 0
209
+ while i < nrules
210
+ s = data[p]
211
+ p += 1
212
+ start_state = atn.states[s]
213
+ atn.rule_to_start_state[i] = start_state
214
+ if atn.grammar_type == ATNType::LEXER
215
+ token_type = data[p]
216
+ p += 1
217
+ token_type = Token::EOF if token_type == 0xFFFF
218
+
219
+ atn.rule_to_token_type[i] = token_type
220
+
221
+ unless feature_supported?(@@added_lexer_actions, uuid)
222
+ @action_index_ignored = data[p]
223
+ p += 1
224
+ end
225
+ end
226
+ i += 1
227
+ end
228
+
229
+ atn.rule_to_stop_state = []
230
+ i = 0
231
+ while i < atn.states.length
232
+ state = atn.states[i]
233
+ unless state.is_a? RuleStopState
234
+ i += 1
235
+ next
236
+ end
237
+
238
+ stop_state = state
239
+ atn.rule_to_stop_state[state.rule_index] = stop_state
240
+ atn.rule_to_start_state[state.rule_index].stop_state = stop_state
241
+ i += 1
242
+ end
243
+
244
+ n_modes = data[p]
245
+ p += 1
246
+ i = 0
247
+ while i < n_modes
248
+ s = data[p]
249
+ p += 1
250
+ atn.mode_to_start_state << atn.states[s]
251
+ i += 1
252
+ end
253
+
254
+ sets = []
255
+
256
+ p = deserialize_sets(data, p, sets, ATNDeserializer.unicode_deserializer(UnicodeDeserializingMode::UNICODE_BMP))
257
+
258
+ if feature_supported?(@@added_unicode_smp, uuid)
259
+ p = deserialize_sets(data, p, sets, ATNDeserializer.unicode_deserializer(UnicodeDeserializingMode::UNICODE_SMP))
260
+ end
261
+
262
+ n_edges = data[p]
263
+ p += 1
264
+ i = 0
265
+ while i < n_edges
266
+ src = data[p]
267
+ trg = data[p + 1]
268
+ ttype = data[p + 2]
269
+ arg1 = data[p + 3]
270
+ arg2 = data[p + 4]
271
+ arg3 = data[p + 5]
272
+ trans = edge_factory(atn, ttype, src, trg, arg1, arg2, arg3, sets)
273
+ src_state = atn.states[src]
274
+ src_state.add_transition(trans)
275
+ p += 6
276
+ i += 1
277
+ end
278
+
279
+ k = 0
280
+ while k < atn.states.length
281
+ state = atn.states[k]
282
+ i = 0
283
+ while i < state.number_of_transitions
284
+ t = state.transition(i)
285
+ unless t.is_a? RuleTransition
286
+ i += 1
287
+ next
288
+ end
289
+
290
+ rule_transition = t
291
+ outermost_precedence_return = -1
292
+ if atn.rule_to_start_state[rule_transition.target.rule_index].is_left_recursive_rule
293
+ if rule_transition.precedence == 0
294
+ outermost_precedence_return = rule_transition.target.rule_index
295
+ end
296
+ end
297
+
298
+ return_transition = EpsilonTransition.new(rule_transition.follow_state, outermost_precedence_return)
299
+ atn.rule_to_stop_state[rule_transition.target.rule_index].add_transition(return_transition)
300
+ i += 1
301
+ end
302
+ k += 1
303
+ end
304
+
305
+ k = 0
306
+ while k < atn.states.length
307
+ state = atn.states[k]
308
+ if state.is_a? BlockStartState
309
+ raise IllegalStateException if state.end_state.nil?
310
+
311
+ raise IllegalStateException unless state.end_state.start_state.nil?
312
+
313
+ state.end_state.start_state = state
314
+ end
315
+
316
+ if state.is_a? PlusLoopbackState
317
+ loopback_state = state
318
+ i = 0
319
+ while i < loopback_state.number_of_transitions
320
+ target = loopback_state.transition(i).target
321
+ if target.is_a? PlusBlockStartState
322
+ target.loopback_state = loopback_state
323
+ end
324
+ i += 1
325
+ end
326
+ elsif state.is_a? StarLoopbackState
327
+ loopback_state = state
328
+ i = 0
329
+ while i < loopback_state.number_of_transitions
330
+ target = loopback_state.transition(i).target
331
+ if target.is_a? StarLoopEntryState
332
+ target.loopback_state = loopback_state
333
+ end
334
+ i += 1
335
+ end
336
+ end
337
+ k += 1
338
+ end
339
+
340
+ n_decisions = data[p]
341
+ p += 1
342
+ i = 1
343
+ while i <= n_decisions
344
+ s = data[p]
345
+ p += 1
346
+ dec_state = atn.states[s]
347
+ atn.decision_to_state << dec_state
348
+ dec_state.decision = i - 1
349
+ i += 1
350
+ end
351
+
352
+ if atn.grammar_type == ATNType::LEXER
353
+ if supports_lexer_actions
354
+ atn._a = Array.new(data[p])
355
+ p += 1
356
+ i = 0
357
+ while i < atn._a.length
358
+ action_type = data[p]
359
+ p += 1
360
+ data1 = data[p]
361
+ p += 1
362
+ data1 = -1 if data1 == 0xFFFF
363
+
364
+ data2 = data[p]
365
+ p += 1
366
+ data2 = -1 if data2 == 0xFFFF
367
+
368
+ lexer_action = lexer_action_factory(action_type, data1, data2)
369
+
370
+ atn._a[i] = lexer_action
371
+ i += 1
372
+ end
373
+ else
374
+ legacy_lexer_actions = []
375
+ k = 0
376
+ while k < atn.states.length
377
+ state = atn.states[k]
378
+ i = 0
379
+ while i < state.number_of_transitions
380
+ transition = state.transition(i)
381
+ unless transition.is_a? ActionTransition
382
+ i += 1
383
+ next
384
+ end
385
+
386
+ rule_index = transition.rule_index
387
+ action_index = transition.action_index
388
+ lexer_action = LexerCustomAction.new(rule_index, action_index)
389
+ state.set_transition(i, ActionTransition.new(transition.target, rule_index, legacy_lexer_actions.length, false))
390
+ legacy_lexer_actions << lexer_action
391
+ i += 1
392
+ end
393
+ k += 1
394
+ end
395
+
396
+ atn._a = legacy_lexer_actions
397
+ end
398
+ end
399
+
400
+ mark_precedence_decisions(atn)
401
+
402
+ verify_atn(atn) if @deserialization_options.verify_atn?
403
+
404
+ if @deserialization_options.generate_rule_bypass_transitions? && atn.grammar_type == ATNType::PARSER
405
+ atn.rule_to_token_type = []
406
+ i = 0
407
+ while i < atn.rule_to_start_state.length
408
+ atn.rule_to_token_type[i] = atn.max_token_type + i + 1
409
+ i += 1
410
+ end
411
+
412
+ i = 0
413
+ while i < atn.rule_to_start_state.length
414
+ bypass_start = BasicBlockStartState.new
415
+ bypass_start.rule_index = i
416
+ atn.add_state(bypass_start)
417
+
418
+ bypass_stop = BlockEndState.new
419
+ bypass_stop.rule_index = i
420
+ atn.add_state(bypass_stop)
421
+
422
+ bypass_start.end_state = bypass_stop
423
+ atn.define_decision_state(bypass_start)
424
+
425
+ bypass_stop.start_state = bypass_start
426
+
427
+ exclude_transition = nil
428
+ if atn.rule_to_start_state[i].is_left_recursive_rule
429
+ end_state = nil
430
+ i = 0
431
+ while i < atn.states.length
432
+ state = atn.states[i]
433
+ if state.rule_index != i
434
+ i += 1
435
+ next
436
+ end
437
+
438
+ unless state.is_a? StarLoopEntryState
439
+ i += 1
440
+ next
441
+ end
442
+
443
+ maybe_loop_end_state = state.transition(state.number_of_transitions - 1).target
444
+ unless maybe_loop_end_state.is_a? LoopEndState
445
+ i += 1
446
+ next
447
+ end
448
+
449
+ if maybe_loop_end_state.epsilon_only_transitions && maybe_loop_end_state.transition(0).target.is_a?(RuleStopState)
450
+ end_state = state
451
+ break
452
+ end
453
+ i += 1
454
+ end
455
+
456
+ if end_state.nil?
457
+ raise UnsupportedOperationException, "Couldn't identify final state of the precedence rule prefix section."
458
+ end
459
+
460
+ exclude_transition = end_state.loopback_state.transition(0)
461
+ else
462
+ end_state = atn.rule_to_stop_state[i]
463
+ end
464
+
465
+ i = 0
466
+ while i < atn.states.length
467
+ state = atn.states[i]
468
+ j = 0
469
+ while j < state.transitions.length
470
+ transition = state.transitions[j]
471
+ if transition == exclude_transition
472
+ j += 1
473
+ next
474
+ end
475
+
476
+ transition.target = bypass_stop if transition.target == end_state
477
+ j += 1
478
+ end
479
+ i += 1
480
+ end
481
+
482
+ while atn.rule_to_start_state[i].number_of_transitions > 0
483
+ transition = atn.rule_to_start_state[i].remove_transition(atn.rule_to_start_state[i].number_of_transitions - 1)
484
+ bypass_start.add_transition(transition)
485
+ end
486
+
487
+ atn.rule_to_start_state[i].add_transition(EpsilonTransition.new(bypass_start))
488
+ bypass_stop.add_transition(EpsilonTransition.new(end_state))
489
+
490
+ match_state = BasicState.new
491
+ atn.add_state(match_state)
492
+ match_state.add_transition(AtomTransition.new(bypass_stop, atn.rule_to_token_type[i]))
493
+ bypass_start.add_transition(EpsilonTransition.new(match_state))
494
+ end
495
+
496
+ verify_atn(atn) if @deserialization_options.verify_atn?
497
+ end
498
+
499
+ atn
500
+ end
501
+
502
+ def deserialize_sets(data, p, sets, unicode_deserializer)
503
+ n_sets = data[p]
504
+ p += 1
505
+ i = 0
506
+ while i < n_sets
507
+ n_intervals = data[p]
508
+ p += 1
509
+ set = IntervalSet.new
510
+ sets << set
511
+
512
+ contains_eof = data[p] != 0
513
+ p += 1
514
+ set.add(-1) if contains_eof
515
+
516
+ j = 0
517
+ while j < n_intervals
518
+ a = unicode_deserializer.read_unicode(data, p)
519
+ p += unicode_deserializer.size
520
+ b = unicode_deserializer.read_unicode(data, p)
521
+ p += unicode_deserializer.size
522
+ set.add(a, b)
523
+ j += 1
524
+ end
525
+ i += 1
526
+ end
527
+ p
528
+ end
529
+
530
+ def mark_precedence_decisions(atn)
531
+ i = 0
532
+ while i < atn.states.length
533
+ state = atn.states[i]
534
+ unless state.is_a? StarLoopEntryState
535
+ i += 1
536
+ next
537
+ end
538
+
539
+ unless atn.rule_to_start_state[state.rule_index].is_left_recursive_rule
540
+ i += 1
541
+ next
542
+ end
543
+
544
+ maybeLoopEndState = state.transition(state.number_of_transitions - 1).target
545
+ unless maybeLoopEndState.is_a? LoopEndState
546
+ i += 1
547
+ next
548
+ end
549
+
550
+ if maybeLoopEndState.epsilon_only_transitions && maybeLoopEndState.transition(0).target.is_a?(RuleStopState)
551
+ state.is_precedence_pecision = true
552
+ end
553
+ i += 1
554
+ end
555
+ end
556
+
557
+ def verify_atn(atn)
558
+ i = 0
559
+ while i < atn.states.length
560
+ state = atn.states[i]
561
+ if state.nil?
562
+ i += 1
563
+ next
564
+ end
565
+
566
+ check_condition(state.only_has_epsilon_transitions || state.number_of_transitions <= 1)
567
+
568
+ if state.is_a? PlusBlockStartState
569
+ check_condition(!state.loopback_state.nil?)
570
+ end
571
+
572
+ if state.is_a? StarLoopEntryState
573
+ star_loop_entry_state = state
574
+ check_condition(!star_loop_entry_state.loopback_state.nil?)
575
+ check_condition(star_loop_entry_state.number_of_transitions == 2)
576
+
577
+ if star_loop_entry_state.transition(0).target.is_a? StarBlockStartState
578
+ check_condition(star_loop_entry_state.transition(1).target.is_a?(LoopEndState))
579
+ check_condition(!star_loop_entry_state.non_greedy)
580
+ elsif star_loop_entry_state.transition(0).target.is_a? LoopEndState
581
+ check_condition(star_loop_entry_state.transition(1).target.is_a?(StarBlockStartState))
582
+ check_condition(star_loop_entry_state.non_greedy)
583
+ else
584
+ raise IllegalStateException
585
+ end
586
+ end
587
+
588
+ if state.is_a? StarLoopbackState
589
+ check_condition(state.number_of_transitions == 1)
590
+ check_condition(state.transition(0).target.is_a?(StarLoopEntryState))
591
+ end
592
+
593
+ check_condition(!state.loopback_state.nil?) if state.is_a? LoopEndState
594
+
595
+ check_condition(!state.stop_state.nil?) if state.is_a? RuleStartState
596
+
597
+ check_condition(!state.end_state.nil?) if state.is_a? BlockStartState
598
+
599
+ check_condition(!state.start_state.nil?) if state.is_a? BlockEndState
600
+
601
+ if state.is_a? DecisionState
602
+ decision_state = state
603
+ check_condition(decision_state.number_of_transitions <= 1 || decision_state.decision >= 0)
604
+ else
605
+ check_condition(state.number_of_transitions <= 1 || state.is_a?(RuleStopState))
606
+ end
607
+ i += 1
608
+ end
609
+ end
610
+
611
+ def check_condition(condition, message = nil)
612
+ raise IllegalStateException, message unless condition
613
+ end
614
+
615
+ def to_int32(data, offset)
616
+ data[offset] | (data[offset + 1] << 16)
617
+ end
618
+
619
+ def to_long(data, offset)
620
+ low_order = to_int32(data, offset) & 0x00000000FFFFFFFF
621
+ low_order | (to_int32(data, offset + 2) << 32)
622
+ end
623
+
624
+ def to_uuid(data, offset)
625
+ least_sig_bits = to_long(data, offset)
626
+ mostSigBits = to_long(data, offset + 4)
627
+ UUID.new(mostSigBits, least_sig_bits)
628
+ end
629
+
630
+ def edge_factory(atn, type, _src, trg, arg1, arg2, arg3, sets)
631
+ target = atn.states[trg]
632
+ case type
633
+ when Transition::EPSILON
634
+ return EpsilonTransition.new(target)
635
+ when Transition::RANGE
636
+ if arg3 != 0
637
+ return RangeTransition.new(target, Token::EOF, arg2)
638
+ else
639
+ return RangeTransition.new(target, arg1, arg2)
640
+ end
641
+ when Transition::RULE
642
+ rt = RuleTransition.new(atn.states[arg1], arg2, arg3, target)
643
+ return rt
644
+ when Transition::PREDICATE
645
+ pt = PredicateTransition.new(target, arg1, arg2, arg3 != 0)
646
+ return pt
647
+ when Transition::PRECEDENCE
648
+ return PrecedencePredicateTransition.new(target, arg1)
649
+ when Transition::ATOM
650
+ if arg3 != 0
651
+ return AtomTransition.new(target, Token::EOF)
652
+ else
653
+ return AtomTransition.new(target, arg1)
654
+ end
655
+ when Transition::ACTION
656
+ a = ActionTransition.new(target, arg1, arg2, arg3 != 0)
657
+ return a
658
+ when Transition::SET
659
+ return SetTransition.new(target, sets[arg1])
660
+ when Transition::NOT_SET
661
+ return NotSetTransition.new(target, sets[arg1])
662
+ when Transition::WILDCARD
663
+ return WildcardTransition.new(target)
664
+ else
665
+ raise IllegalArgumentException, ' The specified transition type is not valid.'
666
+ end
667
+ end
668
+
669
+ def state_factory(type, rule_index)
670
+ case type
671
+ when ATNState::INVALID_TYPE
672
+ return nil
673
+ when ATNState::BASIC
674
+ s = BasicState.new
675
+ when ATNState::RULE_START
676
+ s = RuleStartState.new
677
+ when ATNState::BLOCK_START
678
+ s = BasicBlockStartState.new
679
+ when ATNState::PLUS_BLOCK_START
680
+ s = PlusBlockStartState.new
681
+ when ATNState::STAR_BLOCK_START
682
+ s = StarBlockStartState.new
683
+ when ATNState::TOKEN_START
684
+ s = TokensStartState.new
685
+ when ATNState::RULE_STOP
686
+ s = RuleStopState.new
687
+ when ATNState::BLOCK_END
688
+ s = BlockEndState.new
689
+ when ATNState::STAR_LOOP_BACK
690
+ s = StarLoopbackState.new
691
+ when ATNState::STAR_LOOP_ENTRY
692
+ s = StarLoopEntryState.new
693
+ when ATNState::PLUS_LOOP_BACK
694
+ s = PlusLoopbackState.new
695
+ when ATNState::LOOP_END
696
+ s = LoopEndState.new
697
+ else
698
+ message = sprintf format(" The specified state type % d is not valid.\n", type)
699
+ raise IllegalArgumentException, message
700
+ end
701
+ s.rule_index = rule_index
702
+ s
703
+ end
704
+
705
+ def lexer_action_factory(type, data1, data2)
706
+ case type
707
+ when LexerActionType::CHANNEL
708
+ LexerChannelAction.new(data1)
709
+
710
+ when LexerActionType::CUSTOM
711
+ LexerCustomAction.new(data1, data2)
712
+
713
+ when LexerActionType::MODE
714
+ LexerModeAction.new(data1)
715
+
716
+ when LexerActionType::MORE
717
+ LexerMoreAction.instance
718
+
719
+ when LexerActionType::POP_MODE
720
+ LexerPopModeAction.instance
721
+
722
+ when LexerActionType::PUSH_MODE
723
+ LexerPushModeAction.new(data1)
724
+
725
+ when LexerActionType::SKIP
726
+ LexerSkipAction.instance
727
+
728
+ when LexerActionType::TYPE
729
+ LexerTypeAction.new(data1)
730
+
731
+ else
732
+ message = sprintf format(" The specified lexer action type % d is not valid.\n", type)
733
+ raise IllegalArgumentException, message
734
+ end
735
+ end
736
+ end
737
+ end