antlr4-runtime 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/antlr4-runtime.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/rumourhash/Makefile +264 -0
- data/ext/rumourhash/extconf.rb +3 -0
- data/ext/rumourhash/rumourhash.c +59 -0
- data/lib/antlr4/runtime.rb +37 -0
- data/lib/antlr4/runtime/abstract_parse_tree_visitor.rb +43 -0
- data/lib/antlr4/runtime/abstract_predicate_transition.rb +11 -0
- data/lib/antlr4/runtime/action_transition.rb +29 -0
- data/lib/antlr4/runtime/ambiguity_info.rb +10 -0
- data/lib/antlr4/runtime/antlr_error_listener.rb +15 -0
- data/lib/antlr4/runtime/antlr_error_strategy.rb +24 -0
- data/lib/antlr4/runtime/antlr_file_stream.rb +17 -0
- data/lib/antlr4/runtime/antlr_input_stream.rb +6 -0
- data/lib/antlr4/runtime/array_2d_hash_set.rb +471 -0
- data/lib/antlr4/runtime/array_prediction_context.rb +76 -0
- data/lib/antlr4/runtime/atn.rb +100 -0
- data/lib/antlr4/runtime/atn_config.rb +140 -0
- data/lib/antlr4/runtime/atn_config_set.rb +150 -0
- data/lib/antlr4/runtime/atn_deserialization_options.rb +48 -0
- data/lib/antlr4/runtime/atn_deserializer.rb +737 -0
- data/lib/antlr4/runtime/atn_simulator.rb +69 -0
- data/lib/antlr4/runtime/atn_state.rb +118 -0
- data/lib/antlr4/runtime/atn_type.rb +8 -0
- data/lib/antlr4/runtime/atom_transition.rb +27 -0
- data/lib/antlr4/runtime/bail_error_strategy.rb +31 -0
- data/lib/antlr4/runtime/base_error_listener.rb +18 -0
- data/lib/antlr4/runtime/basic_block_start_state.rb +12 -0
- data/lib/antlr4/runtime/basic_state.rb +11 -0
- data/lib/antlr4/runtime/bit_set.rb +54 -0
- data/lib/antlr4/runtime/block_end_state.rb +15 -0
- data/lib/antlr4/runtime/block_start_state.rb +12 -0
- data/lib/antlr4/runtime/buffered_token_stream.rb +335 -0
- data/lib/antlr4/runtime/char_stream.rb +6 -0
- data/lib/antlr4/runtime/char_streams.rb +12 -0
- data/lib/antlr4/runtime/chunk.rb +4 -0
- data/lib/antlr4/runtime/code_point_char_stream.rb +83 -0
- data/lib/antlr4/runtime/common_token.rb +125 -0
- data/lib/antlr4/runtime/common_token_factory.rb +30 -0
- data/lib/antlr4/runtime/common_token_stream.rb +63 -0
- data/lib/antlr4/runtime/console_error_listener.rb +12 -0
- data/lib/antlr4/runtime/context_sensitivity_info.rb +7 -0
- data/lib/antlr4/runtime/decision_event_info.rb +19 -0
- data/lib/antlr4/runtime/decision_info.rb +36 -0
- data/lib/antlr4/runtime/decision_state.rb +15 -0
- data/lib/antlr4/runtime/default_error_strategy.rb +314 -0
- data/lib/antlr4/runtime/dfa.rb +97 -0
- data/lib/antlr4/runtime/dfa_serializer.rb +62 -0
- data/lib/antlr4/runtime/dfa_state.rb +109 -0
- data/lib/antlr4/runtime/diagnostic_error_listener.rb +58 -0
- data/lib/antlr4/runtime/double_key_map.rb +49 -0
- data/lib/antlr4/runtime/empty_prediction_context.rb +35 -0
- data/lib/antlr4/runtime/epsilon_transition.rb +27 -0
- data/lib/antlr4/runtime/equality_comparator.rb +4 -0
- data/lib/antlr4/runtime/error_info.rb +7 -0
- data/lib/antlr4/runtime/error_node.rb +5 -0
- data/lib/antlr4/runtime/error_node_impl.rb +12 -0
- data/lib/antlr4/runtime/failed_predicate_exception.rb +33 -0
- data/lib/antlr4/runtime/flexible_hash_map.rb +232 -0
- data/lib/antlr4/runtime/input_mismatch_exception.rb +20 -0
- data/lib/antlr4/runtime/int_stream.rb +31 -0
- data/lib/antlr4/runtime/integer.rb +14 -0
- data/lib/antlr4/runtime/interval.rb +111 -0
- data/lib/antlr4/runtime/interval_set.rb +540 -0
- data/lib/antlr4/runtime/lexer.rb +257 -0
- data/lib/antlr4/runtime/lexer_action.rb +12 -0
- data/lib/antlr4/runtime/lexer_action_executor.rb +75 -0
- data/lib/antlr4/runtime/lexer_action_type.rb +12 -0
- data/lib/antlr4/runtime/lexer_atn_config.rb +50 -0
- data/lib/antlr4/runtime/lexer_atn_simulator.rb +522 -0
- data/lib/antlr4/runtime/lexer_channel_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_custom_action.rb +49 -0
- data/lib/antlr4/runtime/lexer_dfa_serializer.rb +12 -0
- data/lib/antlr4/runtime/lexer_indexed_custom_action.rb +49 -0
- data/lib/antlr4/runtime/lexer_mode_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_more_action.rb +41 -0
- data/lib/antlr4/runtime/lexer_no_viable_alt_exception.rb +4 -0
- data/lib/antlr4/runtime/lexer_pop_mode_action.rb +41 -0
- data/lib/antlr4/runtime/lexer_push_mode_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_skip_action.rb +43 -0
- data/lib/antlr4/runtime/lexer_type_action.rb +51 -0
- data/lib/antlr4/runtime/ll1_analyzer.rb +133 -0
- data/lib/antlr4/runtime/lookahead_event_info.rb +10 -0
- data/lib/antlr4/runtime/loop_end_state.rb +15 -0
- data/lib/antlr4/runtime/murmur_hash.rb +99 -0
- data/lib/antlr4/runtime/no_viable_alt_exception.rb +7 -0
- data/lib/antlr4/runtime/not_set_transition.rb +20 -0
- data/lib/antlr4/runtime/object_equality_comparator.rb +18 -0
- data/lib/antlr4/runtime/ordered_atn_config_set.rb +15 -0
- data/lib/antlr4/runtime/parse_cancellation_exception.rb +5 -0
- data/lib/antlr4/runtime/parse_tree.rb +7 -0
- data/lib/antlr4/runtime/parse_tree_listener.rb +4 -0
- data/lib/antlr4/runtime/parse_tree_visitor.rb +4 -0
- data/lib/antlr4/runtime/parser.rb +522 -0
- data/lib/antlr4/runtime/parser_atn_simulator.rb +1171 -0
- data/lib/antlr4/runtime/parser_rule_context.rb +186 -0
- data/lib/antlr4/runtime/plus_block_start_state.rb +11 -0
- data/lib/antlr4/runtime/plus_loopback_state.rb +12 -0
- data/lib/antlr4/runtime/precedence_predicate_transition.rb +31 -0
- data/lib/antlr4/runtime/predicate.rb +6 -0
- data/lib/antlr4/runtime/predicate_eval_info.rb +16 -0
- data/lib/antlr4/runtime/predicate_transition.rb +35 -0
- data/lib/antlr4/runtime/prediction_context.rb +103 -0
- data/lib/antlr4/runtime/prediction_context_cache.rb +28 -0
- data/lib/antlr4/runtime/prediction_context_utils.rb +407 -0
- data/lib/antlr4/runtime/prediction_mode.rb +213 -0
- data/lib/antlr4/runtime/profiling_atn_simulator.rb +149 -0
- data/lib/antlr4/runtime/proxy_error_listener.rb +33 -0
- data/lib/antlr4/runtime/range_transition.rb +29 -0
- data/lib/antlr4/runtime/recognition_exception.rb +17 -0
- data/lib/antlr4/runtime/recognizer.rb +136 -0
- data/lib/antlr4/runtime/rule_context.rb +131 -0
- data/lib/antlr4/runtime/rule_context_with_alt_num.rb +11 -0
- data/lib/antlr4/runtime/rule_node.rb +8 -0
- data/lib/antlr4/runtime/rule_start_state.rb +17 -0
- data/lib/antlr4/runtime/rule_stop_state.rb +12 -0
- data/lib/antlr4/runtime/rule_tag_token.rb +64 -0
- data/lib/antlr4/runtime/rule_transition.rb +29 -0
- data/lib/antlr4/runtime/semantic_context.rb +313 -0
- data/lib/antlr4/runtime/set_transition.rb +29 -0
- data/lib/antlr4/runtime/singleton_prediction_context.rb +56 -0
- data/lib/antlr4/runtime/star_block_start_state.rb +12 -0
- data/lib/antlr4/runtime/star_loop_entry_state.rb +17 -0
- data/lib/antlr4/runtime/star_loopback_state.rb +16 -0
- data/lib/antlr4/runtime/syntax_tree.rb +6 -0
- data/lib/antlr4/runtime/tag_chunk.rb +22 -0
- data/lib/antlr4/runtime/terminal_node.rb +5 -0
- data/lib/antlr4/runtime/terminal_node_impl.rb +50 -0
- data/lib/antlr4/runtime/text_chunk.rb +16 -0
- data/lib/antlr4/runtime/token.rb +13 -0
- data/lib/antlr4/runtime/token_stream.rb +13 -0
- data/lib/antlr4/runtime/token_tag_token.rb +22 -0
- data/lib/antlr4/runtime/tokens_start_state.rb +14 -0
- data/lib/antlr4/runtime/transition.rb +51 -0
- data/lib/antlr4/runtime/tree.rb +4 -0
- data/lib/antlr4/runtime/trees.rb +195 -0
- data/lib/antlr4/runtime/triple.rb +40 -0
- data/lib/antlr4/runtime/utils.rb +117 -0
- data/lib/antlr4/runtime/uuid.rb +46 -0
- data/lib/antlr4/runtime/version.rb +5 -0
- data/lib/antlr4/runtime/vocabulary.rb +12 -0
- data/lib/antlr4/runtime/vocabulary_impl.rb +82 -0
- data/lib/antlr4/runtime/wildcard_transition.rb +20 -0
- data/lib/antlr4/runtime/writable_token.rb +7 -0
- metadata +243 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
require 'antlr4/runtime/flexible_hash_map'
|
|
2
|
+
require 'antlr4/runtime/bit_set'
|
|
3
|
+
|
|
4
|
+
module Antlr4::Runtime
|
|
5
|
+
|
|
6
|
+
class PredictionMode
|
|
7
|
+
SLL = 0
|
|
8
|
+
LL = 1
|
|
9
|
+
LL_EXACT_AMBIG_DETECTION = 2
|
|
10
|
+
|
|
11
|
+
class AltAndContextMap < FlexibleHashMap
|
|
12
|
+
def initialize
|
|
13
|
+
super(AltAndContextConfigEqualityComparator.instance)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class AltAndContextConfigEqualityComparator
|
|
18
|
+
include Singleton
|
|
19
|
+
|
|
20
|
+
def hash(o)
|
|
21
|
+
MurmurHash.hash_int_obj(o.state.state_number, o.context)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def equals(a, b)
|
|
25
|
+
return true if a == b
|
|
26
|
+
return false if a.nil? || b.nil?
|
|
27
|
+
|
|
28
|
+
a.state.state_number == b.state.state_number && a.context.eql?(b.context)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.has_sll_conflict_terminating_prediction(mode, configs)
|
|
33
|
+
return true if all_configs_in_rule_stop_states?(configs)
|
|
34
|
+
|
|
35
|
+
# pure SLL mode parsing
|
|
36
|
+
if mode == PredictionMode::SLL
|
|
37
|
+
# Don't bother with combining configs from different semantic
|
|
38
|
+
# contexts if we can fail over to full LL costs more time
|
|
39
|
+
# since we'll often fail over anyway.
|
|
40
|
+
if configs.has_semantic_context
|
|
41
|
+
# dup configs, tossing out semantic predicates
|
|
42
|
+
dup = ATNConfigSet.new
|
|
43
|
+
i = 0
|
|
44
|
+
while i < configs.configs.length
|
|
45
|
+
cfg = configs.configs[i]
|
|
46
|
+
c = ATNConfig.new
|
|
47
|
+
c.atn_config5(cfg, SemanticContext::NONE)
|
|
48
|
+
dup.add(c)
|
|
49
|
+
|
|
50
|
+
i += 1
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
configs = dup
|
|
54
|
+
end
|
|
55
|
+
# now we have combined contexts for configs with dissimilar preds
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# pure SLL or combined SLL+LL mode parsing
|
|
59
|
+
|
|
60
|
+
alt_sets = conflicting_alt_subsets(configs)
|
|
61
|
+
heuristic = has_conflicting_alt_set?(alt_sets) && !has_state_associated_with_one_alt?(configs)
|
|
62
|
+
heuristic
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.has_config_in_rule_stop_state?(configs)
|
|
66
|
+
i = 0
|
|
67
|
+
while i < configs.configs.length
|
|
68
|
+
return true if configs.configs[i].state.is_a? RuleStopState
|
|
69
|
+
i += 1
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
false
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.all_configs_in_rule_stop_states?(configs)
|
|
76
|
+
i = 0
|
|
77
|
+
while i < configs.configs.length
|
|
78
|
+
return false unless configs.configs[i].state.is_a? RuleStopState
|
|
79
|
+
i += 1
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
true
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.resolves_to_just_one_viable_alt?(altsets)
|
|
86
|
+
single_viable_alt(altsets)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def self.all_subsets_conflict?(altsets)
|
|
90
|
+
!has_non_conflicting_alt_set?(altsets)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.has_non_conflicting_alt_set?(altsets)
|
|
94
|
+
i = 0
|
|
95
|
+
while i < altsets.length
|
|
96
|
+
alts = altsets[i]
|
|
97
|
+
return true if alts.cardinality == 1
|
|
98
|
+
i += 1
|
|
99
|
+
end
|
|
100
|
+
false
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def self.has_conflicting_alt_set?(altsets)
|
|
104
|
+
i = 0
|
|
105
|
+
while i < altsets.length
|
|
106
|
+
alts = altsets[i]
|
|
107
|
+
return true if alts.cardinality > 1
|
|
108
|
+
i += 1
|
|
109
|
+
end
|
|
110
|
+
false
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def all_subsets_equal?(altsets)
|
|
114
|
+
first = nil
|
|
115
|
+
i = 0
|
|
116
|
+
while i < altsets.length
|
|
117
|
+
alt = altsets[i]
|
|
118
|
+
if i == 0
|
|
119
|
+
first = altsets[0]
|
|
120
|
+
else
|
|
121
|
+
return false unless alt.eql?(first)
|
|
122
|
+
end
|
|
123
|
+
i += 1
|
|
124
|
+
end
|
|
125
|
+
true
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def self.unique_alt(altsets)
|
|
129
|
+
all = get_alts1(altsets)
|
|
130
|
+
return all.next_set_bit(0) if all.cardinality == 1
|
|
131
|
+
|
|
132
|
+
ATN::INVALID_ALT_NUMBER
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def self.get_alts1(altsets)
|
|
136
|
+
all = BitSet.new
|
|
137
|
+
i = 0
|
|
138
|
+
while i < altsets.length
|
|
139
|
+
alts = altsets[i]
|
|
140
|
+
all.or(alts)
|
|
141
|
+
i += 1
|
|
142
|
+
end
|
|
143
|
+
all
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def get_alts2(configs)
|
|
147
|
+
alts = BitSet.new
|
|
148
|
+
i = 0
|
|
149
|
+
while i < configs.length
|
|
150
|
+
config = configs[i]
|
|
151
|
+
alts.set(config.alt)
|
|
152
|
+
i += 1
|
|
153
|
+
end
|
|
154
|
+
alts
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def self.conflicting_alt_subsets(configs)
|
|
158
|
+
config_to_alts = AltAndContextMap.new
|
|
159
|
+
i = 0
|
|
160
|
+
while i < configs.configs.length
|
|
161
|
+
c = configs.configs[i]
|
|
162
|
+
alts = config_to_alts.get(c)
|
|
163
|
+
if alts.nil?
|
|
164
|
+
alts = BitSet.new
|
|
165
|
+
config_to_alts.put(c, alts)
|
|
166
|
+
end
|
|
167
|
+
alts.set(c.alt)
|
|
168
|
+
i += 1
|
|
169
|
+
end
|
|
170
|
+
config_to_alts.values
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def self.state_to_alt_map(configs)
|
|
174
|
+
m = {}
|
|
175
|
+
i = 0
|
|
176
|
+
while i < configs.configs.length
|
|
177
|
+
c = configs.configs[i]
|
|
178
|
+
alts = m[c.state]
|
|
179
|
+
if alts.nil?
|
|
180
|
+
alts = BitSet.new
|
|
181
|
+
m[c.state] = alts
|
|
182
|
+
end
|
|
183
|
+
alts.set(c.alt)
|
|
184
|
+
i += 1
|
|
185
|
+
end
|
|
186
|
+
m
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def self.has_state_associated_with_one_alt?(configs)
|
|
190
|
+
x = state_to_alt_map(configs)
|
|
191
|
+
i = 0
|
|
192
|
+
while i < x.values.length
|
|
193
|
+
alts = x.values[i]
|
|
194
|
+
return true if alts.cardinality == 1
|
|
195
|
+
i += 1
|
|
196
|
+
end
|
|
197
|
+
false
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def self.single_viable_alt(altsets)
|
|
201
|
+
viable_alts = BitSet.new
|
|
202
|
+
i = 0
|
|
203
|
+
while i < altsets.length
|
|
204
|
+
alts = altsets[i]
|
|
205
|
+
min_alt = alts.next_set_bit(0)
|
|
206
|
+
viable_alts.set(min_alt)
|
|
207
|
+
return ATN::INVALID_ALT_NUMBER if viable_alts.cardinality > 1 # more than 1 viable alt
|
|
208
|
+
i += 1
|
|
209
|
+
end
|
|
210
|
+
viable_alts.next_set_bit(0)
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class ProfilingATNSimulator < ParserATNSimulator
|
|
3
|
+
def initialize(parser)
|
|
4
|
+
super(parser, parser._interp.atn, parser._interp.decision_to_dfa, parser._interp.shared_context_cache)
|
|
5
|
+
|
|
6
|
+
@num_decisions = @atn.decision_to_state.size
|
|
7
|
+
@_sll_stop_index = 0
|
|
8
|
+
@_ll_stop_index = 0
|
|
9
|
+
@current_decision = 0
|
|
10
|
+
@current_state = nil
|
|
11
|
+
@conflicting_alt_resolved_by_sll = 0
|
|
12
|
+
|
|
13
|
+
@decisions = Array.new(@numDecisions)
|
|
14
|
+
i = 0
|
|
15
|
+
while i < @num_decisions
|
|
16
|
+
@decisions[i] = DecisionInfo.new(i)
|
|
17
|
+
i += 1
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def adaptive_predict(input, decision, outer_ctx)
|
|
22
|
+
@_sll_stop_index = -1
|
|
23
|
+
@_ll_stop_index = -1
|
|
24
|
+
@current_decision = decision
|
|
25
|
+
start = Time.now # expensive but useful info
|
|
26
|
+
alt = super.adaptive_predict(input, decision, outer_ctx)
|
|
27
|
+
stop = Time.now
|
|
28
|
+
@decisions[decision].timeInPrediction += (stop - start)
|
|
29
|
+
@decisions[decision].invocations += 1
|
|
30
|
+
|
|
31
|
+
_s_ll_k = @_sll_stop_index - @_start_index + 1
|
|
32
|
+
@decisions[decision].SLL_TotalLook += _s_ll_k
|
|
33
|
+
@decisions[decision].SLL_MinLook = @decisions[decision].SLL_MinLook == 0 ? _s_ll_k : Math.min(@decisions[decision].SLL_MinLook, _s_ll_k)
|
|
34
|
+
if _s_ll_k > @decisions[decision].SLL_MaxLook
|
|
35
|
+
@decisions[decision].SLL_MaxLook = _s_ll_k
|
|
36
|
+
@decisions[decision].SLL_MaxLookEvent = LookaheadEventInfo.new(decision, nil, alt, input, @_start_index, @_sll_stop_index, false)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if @_ll_stop_index >= 0
|
|
40
|
+
_ll_k = @_ll_stop_index - @_start_index + 1
|
|
41
|
+
@decisions[decision].LL_TotalLook += _ll_k
|
|
42
|
+
@decisions[decision].LL_MinLook = @decisions[decision].LL_MinLook == 0 ? _ll_k : Math.min(@decisions[decision].LL_MinLook, _ll_k)
|
|
43
|
+
if _ll_k > @decisions[decision].LL_MaxLook
|
|
44
|
+
@decisions[decision].LL_MaxLook = _ll_k
|
|
45
|
+
@decisions[decision].LL_MaxLookEvent = LookaheadEventInfo.new(decision, nil, alt, input, @_start_index, @_ll_stop_index, true)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
alt
|
|
50
|
+
ensure
|
|
51
|
+
@current_decision = -1
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def existing_target_state(prev_d, t) # this method is called after each time the input position advances
|
|
55
|
+
# during SLL prediction
|
|
56
|
+
@_sll_stop_index = @_input.index
|
|
57
|
+
|
|
58
|
+
existing_tgt_state = super.existing_target_state(prev_d, t)
|
|
59
|
+
unless existing_tgt_state.nil?
|
|
60
|
+
@decisions[@current_decision].SLL_DFATransitions += 1 # count only if we transition over a DFA state
|
|
61
|
+
if existing_tgt_state == @@error
|
|
62
|
+
@decisions[@current_decision].errors.add(ErrorInfo.new(@current_decision, prev_d.configs, @_input, @_start_index, @_sll_stop_index, false))
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
@current_state = existing_tgt_state
|
|
67
|
+
existing_tgt_state
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def compute_target_state(dfa, prev_d, t)
|
|
71
|
+
state = super.compute_target_state(dfa, prev_d, t)
|
|
72
|
+
@current_state = state
|
|
73
|
+
state
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def compute_reach_set(closure, t, full_ctx)
|
|
77
|
+
if full_ctx
|
|
78
|
+
# this method is called after each time the input position advances
|
|
79
|
+
# during full context prediction
|
|
80
|
+
@_ll_stop_index = @_input.index
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
reach_configs = super.compute_reach_set(closure, t, full_ctx)
|
|
84
|
+
if full_ctx
|
|
85
|
+
@decisions[@current_decision].LL_ATNTransitions += 1 # count computation even if error
|
|
86
|
+
if !reach_configs.nil?
|
|
87
|
+
else # no reach on current lookahead symbol. ERROR.
|
|
88
|
+
# TODO: does not handle delayed errors per getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule()
|
|
89
|
+
@decisions[@current_decision].errors.add(ErrorInfo.new(@current_decision, closure, @_input, @_start_index, @_ll_stop_index, true))
|
|
90
|
+
end
|
|
91
|
+
else
|
|
92
|
+
@decisions[@current_decision].SLL_ATNTransitions += 1
|
|
93
|
+
if !reach_configs.nil?
|
|
94
|
+
else # no reach on current lookahead symbol. ERROR.
|
|
95
|
+
@decisions[@current_decision].errors.add(ErrorInfo.new(@current_decision, closure, @_input, @_start_index, @_sll_stop_index, false))
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
reach_configs
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def eval_semantic_context(pred, parser_call_stack, alt, full_ctx)
|
|
103
|
+
result = super.eval_semantic_context(pred, parser_call_stack, alt, full_ctx)
|
|
104
|
+
unless pred.is_a? SemanticContext.PrecedencePredicate
|
|
105
|
+
full_context = (@_ll_stop_index >= 0)
|
|
106
|
+
stop_index = full_context ? @_ll_stop_index : @_sll_stop_index
|
|
107
|
+
@decisions[@current_decision].predicate_evals.add(PredicateEvalInfo.new(@current_decision, @_input, @_start_index, stop_index, pred, result, alt, full_ctx))
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
result
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def report_attempting_full_context(dfa, conflicting_alts, configs, start_index, stop_index)
|
|
114
|
+
if !conflicting_alts.nil?
|
|
115
|
+
@conflicting_alt_resolved_by_sll = conflicting_alts.next_set_bit(0)
|
|
116
|
+
else
|
|
117
|
+
@conflicting_alt_resolved_by_sll = configs.alts.next_set_bit(0)
|
|
118
|
+
end
|
|
119
|
+
@decisions[@current_decision].LL_Fallback += 1
|
|
120
|
+
super.report_attempting_full_context(dfa, conflicting_alts, configs, start_index, stop_index)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def report_context_sensitivity(dfa, prediction, configs, start_index, stop_index)
|
|
124
|
+
if prediction != @conflicting_alt_resolved_by_sll
|
|
125
|
+
@decisions[@current_decision].context_sensitivities.add(ContextSensitivityInfo.new(@current_decision, configs, @_input, start_index, stop_index))
|
|
126
|
+
end
|
|
127
|
+
super.report_context_sensitivity(dfa, prediction, configs, start_index, stop_index)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def report_ambiguity(dfa, _d, start_index, stop_index, exact, ambig_alts, configs)
|
|
131
|
+
prediction = if !ambig_alts.nil?
|
|
132
|
+
ambig_alts.next_set_bit(0)
|
|
133
|
+
else
|
|
134
|
+
configs.alts.next_set_bit(0)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
if configs.full_ctx && prediction != @conflicting_alt_resolved_by_sll
|
|
138
|
+
# Even though this is an ambiguity we are reporting, we can
|
|
139
|
+
# still detect some context sensitivities. Both SLL and LL
|
|
140
|
+
# are showing a conflict, hence an ambiguity, but if they resolve
|
|
141
|
+
# to different minimum alternatives we have also identified a
|
|
142
|
+
# context sensitivity.
|
|
143
|
+
@decisions[@current_decision].context_sensitivities.add(ContextSensitivityInfo.new(@current_decision, configs, @_input, start_index, stop_index))
|
|
144
|
+
end
|
|
145
|
+
@decisions[@current_decision].ambiguities.add(AmbiguityInfo.new(@current_decision, configs, ambig_alts, @_input, start_index, stop_index, configs.full_ctx))
|
|
146
|
+
super.report_ambiguity(dfa, _d, start_index, stop_index, exact, ambig_alts, configs)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class ProxyErrorListener
|
|
3
|
+
def initialize(delegates)
|
|
4
|
+
raise StandardError, 'delegates is nil' if delegates.nil?
|
|
5
|
+
|
|
6
|
+
@delegates = delegates
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def syntax_error(recognizer, offending_symbol, line, char_position_in_line, msg, e)
|
|
10
|
+
@delegates.each do |listener|
|
|
11
|
+
listener.syntax_error(recognizer, offending_symbol, line, char_position_in_line, msg, e)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def report_ambiguity(recognizer, dfa, start_index, stop_index, exact, ambig_alts, configs)
|
|
16
|
+
@delegates.each do |listener|
|
|
17
|
+
listener.report_ambiguity(recognizer, dfa, start_index, stop_index, exact, ambig_alts, configs)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def report_attempting_full_context(recognizer, dfa, start_index, stop_index, conflicting_alts, configs)
|
|
22
|
+
@delegates.each do |listener|
|
|
23
|
+
listener.report_attempting_full_context(recognizer, dfa, start_index, stop_index, conflicting_alts, configs)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def report_context_sensitivity(recognizer, dfa, start_index, stop_index, prediction, configs)
|
|
28
|
+
@delegates.each do |listener|
|
|
29
|
+
listener.report_context_sensitivity(recognizer, dfa, start_index, stop_index, prediction, configs)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
|
|
3
|
+
class RangeTransition < Transition
|
|
4
|
+
attr_reader :from
|
|
5
|
+
attr_reader :to
|
|
6
|
+
|
|
7
|
+
def initialize(target, from, to)
|
|
8
|
+
super(target)
|
|
9
|
+
@from = from
|
|
10
|
+
@to = to
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def serialization_type
|
|
14
|
+
RANGE
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def label
|
|
18
|
+
IntervalSet.of(@from, @to)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def matches(symbol, _min_vocab_symbol, _max_vocab_symbol)
|
|
22
|
+
symbol >= @from && symbol <= @to
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def to_s
|
|
26
|
+
"'" << @from << "'..'" << @to << "'"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class RecognitionException < StandardError
|
|
3
|
+
attr_accessor :recognizer
|
|
4
|
+
attr_accessor :context
|
|
5
|
+
attr_accessor :offending_token
|
|
6
|
+
attr_accessor :offending_state
|
|
7
|
+
attr_accessor :input
|
|
8
|
+
|
|
9
|
+
def expected_tokens
|
|
10
|
+
unless @recognizer.nil?
|
|
11
|
+
return @recognizer._interp.atn.expected_tokens(@offending_state, @context)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
nil
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
require 'antlr4/runtime/console_error_listener'
|
|
2
|
+
require 'antlr4/runtime/proxy_error_listener'
|
|
3
|
+
|
|
4
|
+
module Antlr4::Runtime
|
|
5
|
+
|
|
6
|
+
class Recognizer
|
|
7
|
+
EOF = -1
|
|
8
|
+
|
|
9
|
+
attr_accessor :_interp
|
|
10
|
+
attr_accessor :_state_number
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@token_type_map_cache = []
|
|
14
|
+
@rule_index_map_cache = []
|
|
15
|
+
@_listeners = []
|
|
16
|
+
@_listeners << ConsoleErrorListener.instance
|
|
17
|
+
@_interp = nil
|
|
18
|
+
@_state_number = -1
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def get_vocabulary
|
|
22
|
+
VocabularyImpl.from_token_names(token_names)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def token_names
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def rule_names
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def get_token_type_map
|
|
34
|
+
vocab = get_vocabulary
|
|
35
|
+
result = @token_type_map_cache[vocab]
|
|
36
|
+
if result.nil?
|
|
37
|
+
result = {}
|
|
38
|
+
i = 0
|
|
39
|
+
while i <= getATN.max_token_type
|
|
40
|
+
literal_name = vocab.literal_name(i)
|
|
41
|
+
result[literal_name] = i unless literal_name.nil?
|
|
42
|
+
|
|
43
|
+
symbolic_name = vocab.symbolic_name(i)
|
|
44
|
+
result[symbolic_name] = i unless symbolic_name.nil?
|
|
45
|
+
i += 1
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
result['EOF'] = Token::EOF
|
|
49
|
+
@token_type_map_cache[vocab] = result
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
result
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def get_rule_index_map
|
|
56
|
+
if rule_names.nil?
|
|
57
|
+
raise UnsupportedOperationException, 'The current recognizer does not provide a list of rule names.'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
result = @rule_index_map_cache[rule_names]
|
|
61
|
+
if result.nil?
|
|
62
|
+
result = Utils.toMap(rule_names)
|
|
63
|
+
@rule_index_map_cache[rule_names] = result
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
result
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def get_token_type(token_name)
|
|
70
|
+
ttype = get_token_type_map[token_name]
|
|
71
|
+
return ttype unless ttype.nil?
|
|
72
|
+
|
|
73
|
+
Token::INVALID_TYPE
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def get_serialized_atn
|
|
77
|
+
raise UnsupportedOperationException, 'there is no serialized ATN'
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def parse_info
|
|
81
|
+
nil
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def error_header(e)
|
|
85
|
+
line = e.getOffendingToken.line
|
|
86
|
+
charPositionInLine = e.getOffendingToken.char_position_in_line
|
|
87
|
+
'line ' + line + ':' + charPositionInLine
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def token_error_display(t)
|
|
91
|
+
return '<no token>' if t.nil?
|
|
92
|
+
|
|
93
|
+
s = t.text
|
|
94
|
+
if s.nil?
|
|
95
|
+
s = if t.type == Token::EOF
|
|
96
|
+
'<EOF>'
|
|
97
|
+
else
|
|
98
|
+
'<' + t.type + '>'
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
s = s.tr_s!("\n", '\\n')
|
|
102
|
+
s = s.tr_s!("\r", '\\r')
|
|
103
|
+
s = s.tr_s!("\t", '\\t')
|
|
104
|
+
"'" + s + "'"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def add_error_listener(listener)
|
|
108
|
+
raise NullPointerException, 'listener cannot be nil.' if listener.nil?
|
|
109
|
+
|
|
110
|
+
@_listeners << listener
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def remove_error_listener(listener)
|
|
114
|
+
@_listeners.delete(listener)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def remove_error_listeners
|
|
118
|
+
@_listeners.clear
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def error_listener_dispatch
|
|
122
|
+
ProxyErrorListener.new(@_listeners)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def sempred(_localctx, _rule_index, _action_index)
|
|
126
|
+
true
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def precpred(_localctx, _precedence)
|
|
130
|
+
true
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def action(_localctx, _rule_index, _action_index)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|