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,76 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
|
|
3
|
+
class ArrayPredictionContext < PredictionContext
|
|
4
|
+
attr_accessor :parents
|
|
5
|
+
attr_accessor :return_states
|
|
6
|
+
|
|
7
|
+
def initialize(parents, return_states = nil)
|
|
8
|
+
if parents.is_a? SingletonPredictionContext
|
|
9
|
+
return_states = [parents.return_state]
|
|
10
|
+
parents = [parents.parent]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
super(PredictionContextUtils.calculate_hash_code2(parents, return_states))
|
|
14
|
+
@parents = parents
|
|
15
|
+
@return_states = return_states
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def empty? # since EMPTY_RETURN_STATE can only appear in the last position, we
|
|
19
|
+
# don't need to verify that size==1
|
|
20
|
+
@return_states[0] == EMPTY_RETURN_STATE
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def size
|
|
24
|
+
@return_states.length
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def get_parent(index)
|
|
28
|
+
@parents[index]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def get_return_state(index)
|
|
32
|
+
@return_states[index]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def equals(o)
|
|
36
|
+
if self == o
|
|
37
|
+
return true
|
|
38
|
+
elsif !(o.is_a? ArrayPredictionContext)
|
|
39
|
+
return false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
if hash != o.hash
|
|
43
|
+
return false # can't be same if hash is different
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
(@return_states.eql? o.return_states) && (@parents.eql? o.parents)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def to_s
|
|
50
|
+
return '[]' if empty?
|
|
51
|
+
|
|
52
|
+
buf = ''
|
|
53
|
+
buf << '['
|
|
54
|
+
i = 0
|
|
55
|
+
while i < @return_states.length
|
|
56
|
+
buf << ', ' if i > 0
|
|
57
|
+
if return_states[i] == EMPTY_RETURN_STATE
|
|
58
|
+
buf << '$'
|
|
59
|
+
i += 1
|
|
60
|
+
next
|
|
61
|
+
end
|
|
62
|
+
buf << @return_states[i]
|
|
63
|
+
if !@parents[i].nil?
|
|
64
|
+
buf << ' '
|
|
65
|
+
buf << @parents[i].to_s
|
|
66
|
+
else
|
|
67
|
+
buf << 'nil'
|
|
68
|
+
end
|
|
69
|
+
i += 1
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
buf << ']'
|
|
73
|
+
buf.to_s
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
require 'antlr4/runtime/ll1_analyzer'
|
|
2
|
+
|
|
3
|
+
module Antlr4::Runtime
|
|
4
|
+
|
|
5
|
+
class IllegalArgumentException < RuntimeError
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class ATN
|
|
9
|
+
INVALID_ALT_NUMBER = 0
|
|
10
|
+
|
|
11
|
+
attr_accessor :states
|
|
12
|
+
attr_accessor :grammar_type
|
|
13
|
+
attr_accessor :rule_to_token_type
|
|
14
|
+
attr_accessor :rule_to_start_state
|
|
15
|
+
attr_accessor :rule_to_stop_state
|
|
16
|
+
attr_accessor :mode_name_to_start_state
|
|
17
|
+
attr_accessor :mode_to_start_state
|
|
18
|
+
attr_accessor :decision_to_state
|
|
19
|
+
attr_accessor :_a
|
|
20
|
+
attr_accessor :max_token_type
|
|
21
|
+
|
|
22
|
+
def initialize(grammar_type, max_token_type)
|
|
23
|
+
@states = []
|
|
24
|
+
@decision_to_state = []
|
|
25
|
+
@rule_to_start_state = []
|
|
26
|
+
@rule_to_stop_state = []
|
|
27
|
+
@mode_name_to_start_state = {}
|
|
28
|
+
@grammar_type = grammar_type
|
|
29
|
+
@max_token_type = max_token_type
|
|
30
|
+
@rule_to_token_type = []
|
|
31
|
+
@_a = []
|
|
32
|
+
@mode_to_start_state = []
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def next_tokens_ctx(s, ctx)
|
|
36
|
+
LL1Analyzer.new(self).look(s, nil, ctx)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def next_tokens(s)
|
|
40
|
+
return s.next_token_within_rule unless s.next_token_within_rule.nil?
|
|
41
|
+
|
|
42
|
+
s.next_token_within_rule = next_tokens_ctx(s, nil)
|
|
43
|
+
s.next_token_within_rule.readonly(true)
|
|
44
|
+
s.next_token_within_rule
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def add_state(state)
|
|
48
|
+
unless state.nil?
|
|
49
|
+
state.atn = self
|
|
50
|
+
state.state_number = @states.length
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
@states << state
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def remove_state(state)
|
|
57
|
+
@states[state.state_number] = nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def define_decision_state(s)
|
|
61
|
+
@decision_to_state << s
|
|
62
|
+
s.decision = @decision_to_state.length - 1
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def decision_state(decision)
|
|
66
|
+
@decision_to_state[decision] unless @decision_to_state.empty?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def number_of_decisions
|
|
70
|
+
@decision_to_state.length
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def expected_tokens(state_number, context)
|
|
74
|
+
if state_number < 0 || state_number >= @states.length
|
|
75
|
+
raise IllegalArgumentException, 'Invalid state number.'
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
ctx = context
|
|
79
|
+
s = @states[state_number]
|
|
80
|
+
following = next_tokens(s)
|
|
81
|
+
return following unless following.contains(Token::EPSILON)
|
|
82
|
+
|
|
83
|
+
expected = IntervalSet.new
|
|
84
|
+
expected.add_all(following)
|
|
85
|
+
expected.remove(Token::EPSILON)
|
|
86
|
+
while !ctx.nil? && ctx.invoking_state >= 0 && following.contains(Token::EPSILON)
|
|
87
|
+
invoking_state = @states[ctx.invoking_state]
|
|
88
|
+
rt = invoking_state.transition(0)
|
|
89
|
+
following = next_tokens(rt.follow_state)
|
|
90
|
+
expected.add_all(following)
|
|
91
|
+
expected.remove(Token::EPSILON)
|
|
92
|
+
ctx = ctx.parent
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
expected << Token::EOF if following.contains(Token::EPSILON)
|
|
96
|
+
|
|
97
|
+
expected
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class ATNConfig
|
|
3
|
+
SUPPRESS_PRECEDENCE_FILTER = 0x40000000
|
|
4
|
+
|
|
5
|
+
attr_accessor :state
|
|
6
|
+
attr_accessor :alt
|
|
7
|
+
attr_accessor :context
|
|
8
|
+
attr_accessor :target
|
|
9
|
+
attr_accessor :reaches_into_outer_context
|
|
10
|
+
attr_accessor :semantic_context
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@reaches_into_outer_context = 0
|
|
14
|
+
@alt = 0
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def bucket_hash
|
|
18
|
+
return @_bucket_hash unless @_bucket_hash.nil?
|
|
19
|
+
|
|
20
|
+
if !@_bucket_hash.nil?
|
|
21
|
+
if hash_code == @_bucket_hash
|
|
22
|
+
puts 'Same hash_code for ATNConfig.bucket_hash'
|
|
23
|
+
else
|
|
24
|
+
puts 'Different hash_code for ATNConfig.bucket_hash'
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
@_bucket_hash = MurmurHash.hash_int_int(@state.state_number, @alt)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def atn_config_copy(old)
|
|
31
|
+
@state = old.state
|
|
32
|
+
@alt = old.alt
|
|
33
|
+
@context = old.context
|
|
34
|
+
@semantic_context = old.semantic_context
|
|
35
|
+
@reaches_into_outer_context = old.reaches_into_outer_context
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def atn_config1(state, alt, context)
|
|
39
|
+
atn_config2(state, alt, context, SemanticContext::NONE)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def atn_config2(state, alt, context, semantic_context)
|
|
43
|
+
@state = state
|
|
44
|
+
@alt = alt
|
|
45
|
+
@context = context
|
|
46
|
+
@semantic_context = semantic_context
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def atn_config3(c, state)
|
|
50
|
+
atn_config7(c, state, c.context, c.semantic_context)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def atn_config4(c, state, semantic_context)
|
|
54
|
+
atn_config7(c, state, c.context, semantic_context)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def atn_config5(c, semantic_context)
|
|
58
|
+
atn_config7(c, c.state, c.context, semantic_context)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def atn_config6(c, state, context)
|
|
62
|
+
atn_config7(c, state, context, c.semantic_context)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def atn_config7(c, state, context, semantic_context)
|
|
66
|
+
@state = state
|
|
67
|
+
@alt = c.alt
|
|
68
|
+
@context = context
|
|
69
|
+
@semantic_context = semantic_context
|
|
70
|
+
@reaches_into_outer_context = c.reaches_into_outer_context
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def outer_context_depth
|
|
74
|
+
(@reaches_into_outer_context & ~SUPPRESS_PRECEDENCE_FILTER)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def precedence_filter_suppressed?
|
|
78
|
+
(@reaches_into_outer_context & SUPPRESS_PRECEDENCE_FILTER) != 0
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def precedence_filter_suppressed(value)
|
|
82
|
+
if value
|
|
83
|
+
@reaches_into_outer_context |= 0x40000000
|
|
84
|
+
else
|
|
85
|
+
@reaches_into_outer_context &= ~SUPPRESS_PRECEDENCE_FILTER
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def to_s
|
|
90
|
+
to_s2(nil, true)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def to_s2(_recog = nil, show_alt = false)
|
|
94
|
+
buf = ''
|
|
95
|
+
buf << '('
|
|
96
|
+
buf << @state.to_s
|
|
97
|
+
if show_alt
|
|
98
|
+
buf << ','
|
|
99
|
+
buf << @alt.to_s
|
|
100
|
+
end
|
|
101
|
+
unless @context.nil?
|
|
102
|
+
buf << ',['
|
|
103
|
+
buf << @context.to_s
|
|
104
|
+
buf << ']'
|
|
105
|
+
end
|
|
106
|
+
if !@semantic_context.nil? && @semantic_context != SemanticContext::NONE
|
|
107
|
+
buf << ','
|
|
108
|
+
buf << @semantic_context.to_s
|
|
109
|
+
end
|
|
110
|
+
buf << ',up=' << outer_context_depth.to_s if outer_context_depth > 0
|
|
111
|
+
buf << ')'
|
|
112
|
+
buf
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def eql?(other)
|
|
116
|
+
if self == other
|
|
117
|
+
return true
|
|
118
|
+
elsif other.nil?
|
|
119
|
+
return false
|
|
120
|
+
end
|
|
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?
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def hash
|
|
126
|
+
return @_hash unless @_hash.nil?
|
|
127
|
+
|
|
128
|
+
hash_code = MurmurHash.hash_int_int_obj_obj(@state.state_number,@alt,@context,@semantic_context)
|
|
129
|
+
|
|
130
|
+
if !@_hash.nil?
|
|
131
|
+
if hash_code == @_hash
|
|
132
|
+
puts 'Same hash_code for ATNConfig'
|
|
133
|
+
else
|
|
134
|
+
puts 'Different hash_code for ATNConfig'
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
@_hash = hash_code
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
require 'antlr4/runtime/array_2d_hash_set'
|
|
2
|
+
require 'antlr4/runtime/atn'
|
|
3
|
+
|
|
4
|
+
module Antlr4::Runtime
|
|
5
|
+
|
|
6
|
+
class ATNConfigSet
|
|
7
|
+
attr_accessor :has_semantic_context
|
|
8
|
+
attr_accessor :readonly
|
|
9
|
+
attr_accessor :configs
|
|
10
|
+
attr_accessor :unique_alt
|
|
11
|
+
attr_accessor :dips_into_outer_context
|
|
12
|
+
attr_accessor :full_ctx
|
|
13
|
+
attr_accessor :conflictingAlts
|
|
14
|
+
|
|
15
|
+
def initialize(full_ctx = true)
|
|
16
|
+
@full_ctx = full_ctx
|
|
17
|
+
@has_semantic_context = false
|
|
18
|
+
@readonly = false
|
|
19
|
+
@config_lookup = ConfigHashSet.new
|
|
20
|
+
@configs = []
|
|
21
|
+
@dips_into_outer_context = false
|
|
22
|
+
@unique_alt = ATN::INVALID_ALT_NUMBER
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def alts
|
|
26
|
+
alts = BitSet.new
|
|
27
|
+
i = 0
|
|
28
|
+
while i < @configs.length
|
|
29
|
+
config = @configs[i]
|
|
30
|
+
alts.set(config.alt)
|
|
31
|
+
i += 1
|
|
32
|
+
end
|
|
33
|
+
alts
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def add(config, merge_cache = nil)
|
|
37
|
+
raise IllegalStateException, 'This set is readonly' if @readonly
|
|
38
|
+
|
|
39
|
+
if config.semantic_context != SemanticContext::NONE
|
|
40
|
+
@has_semantic_context = true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
@dips_into_outer_context = true if config.outer_context_depth > 0
|
|
44
|
+
|
|
45
|
+
existing = @config_lookup.get_or_add config
|
|
46
|
+
if existing == config
|
|
47
|
+
@cached_hash_code = -1
|
|
48
|
+
@configs << config
|
|
49
|
+
return true
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
root_is_wildcard = !@full_ctx
|
|
53
|
+
|
|
54
|
+
merged = PredictionContextUtils.merge(existing.context, config.context, root_is_wildcard, merge_cache)
|
|
55
|
+
|
|
56
|
+
existing.reaches_into_outer_context = [existing.reaches_into_outer_context, config.reaches_into_outer_context].max
|
|
57
|
+
|
|
58
|
+
if config.precedence_filter_suppressed?
|
|
59
|
+
existing.precedence_filter_suppressed(true)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
existing.context = merged
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def find_first_rule_stop_state
|
|
67
|
+
result = nil
|
|
68
|
+
|
|
69
|
+
i = 0
|
|
70
|
+
while i < @configs.length
|
|
71
|
+
config = @configs[i]
|
|
72
|
+
if config.state.is_a? RuleStopState
|
|
73
|
+
result = config
|
|
74
|
+
break
|
|
75
|
+
end
|
|
76
|
+
i += 1
|
|
77
|
+
end
|
|
78
|
+
result
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def empty?
|
|
82
|
+
@config_lookup.empty?
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def to_s
|
|
86
|
+
buf = ''
|
|
87
|
+
buf << '<'
|
|
88
|
+
i = 0
|
|
89
|
+
while i < @configs.length
|
|
90
|
+
c = @configs[i]
|
|
91
|
+
buf << c.to_s << ' '
|
|
92
|
+
i += 1
|
|
93
|
+
end
|
|
94
|
+
buf << '>'
|
|
95
|
+
|
|
96
|
+
if @has_semantic_context
|
|
97
|
+
buf << ',hasSemanticContext=' << @has_semantic_context.to_s
|
|
98
|
+
end
|
|
99
|
+
buf << ',uniqueAlt=' << @unique_alt if @unique_alt != ATN::INVALID_ALT_NUMBER
|
|
100
|
+
unless @conflictingAlts.nil?
|
|
101
|
+
buf << ',conflictingAlts=' << @conflictingAlts.to_s
|
|
102
|
+
end
|
|
103
|
+
buf << ',dipsIntoOuterContext' if @dips_into_outer_context
|
|
104
|
+
buf
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
class AbstractConfigHashSet < Array2DHashSet
|
|
108
|
+
def initialize(comparator)
|
|
109
|
+
super(comparator, 64, 64)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
class ConfigHashSet < AbstractConfigHashSet
|
|
114
|
+
def initialize
|
|
115
|
+
super(ConfigEqualityComparator.instance)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def optimize_configs(interpreter)
|
|
120
|
+
raise IllegalStateException, 'This set is readonly' if @readonly
|
|
121
|
+
return if @config_lookup.empty?
|
|
122
|
+
|
|
123
|
+
i = 0
|
|
124
|
+
while i < @configs.length
|
|
125
|
+
config = @configs[i]
|
|
126
|
+
config.context = interpreter.cached_context(config.context)
|
|
127
|
+
i += 1
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
class ConfigEqualityComparator
|
|
132
|
+
include Singleton
|
|
133
|
+
|
|
134
|
+
def hash(o)
|
|
135
|
+
o.bucket_hash
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def equals(a, b)
|
|
139
|
+
return true if a == b
|
|
140
|
+
return false if a.nil? || b.nil?
|
|
141
|
+
|
|
142
|
+
a.state.state_number == b.state.state_number && a.alt == b.alt && a.semantic_context.eql?(b.semantic_context)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def size
|
|
147
|
+
@configs.length
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|