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,131 @@
1
+ require 'antlr4/runtime/rule_node'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class RuleContext < RuleNode
6
+ attr_accessor :parent
7
+ attr_accessor :invoking_state
8
+
9
+ def initialize(parent = nil, invoking_state = nil)
10
+ @invoking_state = -1
11
+ @parent = parent
12
+ @invoking_state = invoking_state
13
+ end
14
+
15
+ def depth
16
+ n = 0
17
+ p = self
18
+ until p.nil?
19
+ p = p.parent
20
+ n += 1
21
+ end
22
+ n
23
+ end
24
+
25
+ def empty?
26
+ @invoking_state == -1
27
+ end
28
+
29
+ # satisfy the ParseTree / SyntaxTree interface
30
+
31
+ def source_interval
32
+ Interval.invalid
33
+ end
34
+
35
+ def rule_context
36
+ self
37
+ end
38
+
39
+ def payload
40
+ self
41
+ end
42
+
43
+ def set_alt_number(_num)
44
+ end
45
+
46
+ def text
47
+ return '' if child_count == 0
48
+
49
+ builder = ''
50
+ i = 0
51
+ while i < child_count
52
+ builder << child_at(i).text
53
+ i += 1
54
+ end
55
+
56
+ builder.to_s
57
+ end
58
+
59
+ def rule_index
60
+ -1
61
+ end
62
+
63
+ def alt_number
64
+ ATN::INVALID_ALT_NUMBER
65
+ end
66
+
67
+ def child(_i)
68
+ nil
69
+ end
70
+
71
+ def child_count
72
+ 0
73
+ end
74
+
75
+ def accept(visitor)
76
+ visitor.visit_children(self)
77
+ end
78
+
79
+ def to_string_tree_recog(recog)
80
+ Trees.to_sTree(self, recog)
81
+ end
82
+
83
+ def to_string_tree_rulenames(rule_names)
84
+ Trees.to_sTree(self, rule_names)
85
+ end
86
+
87
+ def to_string_tree
88
+ to_string_tree_rulenames(nil)
89
+ end
90
+
91
+ def to_s
92
+ to_s_recog_ctx(nil, nil)
93
+ end
94
+
95
+ def to_s_recog(recog)
96
+ to_s_recog_ctx(recog, ParserRuleContext::EMPTY)
97
+ end
98
+
99
+ def to_s_list(rule_names)
100
+ to_s_list_ctx(rule_names, nil)
101
+ end
102
+
103
+ def to_s_recog_ctx(recog, stop)
104
+ rule_names = !recog.nil? ? recog.rule_names : nil
105
+ rule_names_list = !rule_names.nil? ? rule_names : nil
106
+ to_s_list_ctx(rule_names_list, stop)
107
+ end
108
+
109
+ def to_s_list_ctx(rule_names, stop)
110
+ buf = ''
111
+ p = self
112
+ buf << '['
113
+ while !p.nil? && p != stop
114
+ if rule_names.nil?
115
+ buf << p.invoking_state unless p.empty?
116
+ else
117
+ rule_index = p.rule_index
118
+ rule_name = rule_index >= 0 && rule_index < rule_names.size ? rule_names[rule_index] : rule_index
119
+ buf << rule_name.to_s
120
+ end
121
+
122
+ buf << ' ' if !p.parent.nil? && (!rule_names.nil? || !p.parent.empty?)
123
+
124
+ p = p.parent
125
+ end
126
+
127
+ buf << ']'
128
+ buf
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,11 @@
1
+ module Antlr4::Runtime
2
+
3
+ class RuleContextWithAltNum < ParserRuleContext
4
+ attr_accessor :alt_num
5
+
6
+ def initialize(parent = nil, invoking_state_number = nil)
7
+ super(parent, invoking_state_number)
8
+ @alt_num = ATN::INVALID_ALT_NUMBER
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'antlr4/runtime/parse_tree'
2
+
3
+ module Antlr4::Runtime
4
+ class RuleNode < ParseTree
5
+ def rule_context
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Antlr4::Runtime
2
+
3
+ class RuleStartState < ATNState
4
+ attr_accessor :stop_state
5
+ attr_accessor :is_left_recursive_rule
6
+
7
+ def initialize
8
+ super
9
+ @is_left_recursive_rule = false
10
+ @stop_state = nil
11
+ end
12
+
13
+ def state_type
14
+ RULE_START
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Antlr4::Runtime
2
+
3
+ class RuleStopState < ATNState
4
+ def initialize
5
+ super
6
+ end
7
+
8
+ def state_type
9
+ RULE_STOP
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,64 @@
1
+ module Antlr4::Runtime
2
+
3
+ class RuleTagToken < Token
4
+ attr_reader :rule_name
5
+ attr_reader :bypass_token_type
6
+ attr_reader :label
7
+
8
+ def initialize(rule_name, bypass_token_type, label = nil)
9
+ if rule_name.nil? || rule_name.empty?
10
+ raise IllegalArgumentException, 'rule_name cannot be nil or empty.'
11
+ end
12
+
13
+ @rule_name = rule_name
14
+ @bypass_token_type = bypass_token_type
15
+ @label = label
16
+ end
17
+
18
+ def channel
19
+ DEFAULT_CHANNEL
20
+ end
21
+
22
+ def text
23
+ return '<' + @label + ':' + @rule_name + '>' unless @label.nil?
24
+
25
+ '<' + @rule_name + '>'
26
+ end
27
+
28
+ def type
29
+ @bypass_token_type
30
+ end
31
+
32
+ def line
33
+ 0
34
+ end
35
+
36
+ def char_position_in_line
37
+ -1
38
+ end
39
+
40
+ def token_index
41
+ -1
42
+ end
43
+
44
+ def start_index
45
+ -1
46
+ end
47
+
48
+ def stop_index
49
+ -1
50
+ end
51
+
52
+ def token_source
53
+ nil
54
+ end
55
+
56
+ def input_stream
57
+ nil
58
+ end
59
+
60
+ def to_s
61
+ @rule_name + ':' + @bypass_token_type
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,29 @@
1
+ module Antlr4::Runtime
2
+
3
+ class RuleTransition < Transition
4
+ attr_reader :rule_index # no Rule object at runtime
5
+
6
+ attr_reader :precedence
7
+
8
+ attr_reader :follow_state
9
+
10
+ def initialize(rule_start, rule_index, precedence, follow_state)
11
+ super(rule_start)
12
+ @rule_index = rule_index
13
+ @precedence = precedence
14
+ @follow_state = follow_state
15
+ end
16
+
17
+ def serialization_type
18
+ RULE
19
+ end
20
+
21
+ def epsilon?
22
+ true
23
+ end
24
+
25
+ def matches(_symbol, _min_vocab_symbol, _max_vocab_symbol)
26
+ false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,313 @@
1
+ module Antlr4::Runtime
2
+ class SemanticContext
3
+ def eval(parser, parser_call_stack)
4
+ end
5
+
6
+ def eval_precedence(_parser, _parser_call_stack)
7
+ self
8
+ end
9
+
10
+ class Predicate < SemanticContext
11
+ attr_accessor :rule_index
12
+ attr_accessor :pred_index
13
+ attr_accessor :is_ctx_dependent # e.g., $i ref in pred
14
+
15
+ def initialize(rule_index = -1, pred_index = -1, is_ctx_dependent = false)
16
+ @rule_index = rule_index
17
+ @pred_index = pred_index
18
+ @is_ctx_dependent = is_ctx_dependent
19
+ end
20
+
21
+ def eval(parser, parser_call_stack)
22
+ localctx = @is_ctx_dependent ? parser_call_stack : nil
23
+ parser.sempred(localctx, @rule_index, @pred_index)
24
+ end
25
+
26
+ def hash
27
+ return @_hash unless @_hash.nil?
28
+
29
+ hash_code = MurmurHash.hash_ints([@rule_index, @pred_index, @is_ctx_dependent ? 1 : 0])
30
+
31
+ if !@_hash.nil?
32
+ if hash_code == @_hash
33
+ puts 'Same hash_code for SemanticContext'
34
+ else
35
+ puts 'Different hash_code for SemanticContext'
36
+ end
37
+ end
38
+ @_hash = hash_code
39
+ end
40
+
41
+ def eql?(other)
42
+ return false unless other.is_a? Predicate
43
+ return true if self == other
44
+
45
+ @rule_index == other.rule_index && @pred_index == other.pred_index && @is_ctx_dependent == other.is_ctx_dependent
46
+ end
47
+
48
+ def to_s
49
+ '' + @rule_index + ':' + @pred_index + 'end?'
50
+ end
51
+ end
52
+
53
+ class PrecedencePredicate < SemanticContext
54
+ attr_accessor :precedence
55
+
56
+ def initialize(precedence = 0)
57
+ @precedence = precedence
58
+ end
59
+
60
+ def eval(parser, parser_call_stack)
61
+ parser.precpred(parser_call_stack, @precedence)
62
+ end
63
+
64
+ def eval_precedence(parser, parser_call_stack)
65
+ SemanticContext::NONE if parser.precpred(parser_call_stack, @precedence)
66
+ end
67
+
68
+ def compare_to(o)
69
+ @precedence - o.precedence
70
+ end
71
+
72
+ def hash
73
+ hash_code = 1
74
+ 31 * hash_code + @precedence
75
+ end
76
+
77
+ def eql?(other)
78
+ return false unless other.is_a? PrecedencePredicate
79
+
80
+ return true if self == other
81
+
82
+ @precedence == other.precedence
83
+ end
84
+
85
+ # precedence >= _precedenceStack.peek()
86
+ def to_s
87
+ '' + @precedence + '>=precend?'
88
+ end
89
+ end
90
+
91
+ class Operator < SemanticContext
92
+ def operands
93
+ end
94
+ end
95
+
96
+ class AND < Operator
97
+ attr_accessor :opnds
98
+
99
+ def initialize(a, b)
100
+ operands = Set.new
101
+ if a.is_a? AND
102
+ operands.add_all(a.opnds)
103
+ else
104
+ operands.add(a)
105
+ end
106
+ if b.is_a? AND
107
+ operands.add_all(b.opnds)
108
+ else
109
+ operands.add(b)
110
+ end
111
+ precedence_predicates = filter_precedence_predicates(operands)
112
+ unless precedence_predicates.empty?
113
+ # interested in the transition with the lowest precedence
114
+ reduced = precedence_predicates.min
115
+ operands.add(reduced)
116
+ end
117
+
118
+ @opnds = operands.to_a
119
+ end
120
+
121
+ def eql?(other)
122
+ return true if self == other
123
+ return false unless other.is_a? AND
124
+
125
+ @opnds.eql?(other.opnds)
126
+ end
127
+
128
+ def hash
129
+ hash_code = MurmurHash.hash(@opnds, AND.hash)
130
+ if !@_hash2.nil?
131
+ if hash_code == @_hash2
132
+ puts 'Same hash_code for SemanticContext_2'
133
+ else
134
+ puts 'Different hash_code for SemanticContext_2'
135
+ end
136
+ end
137
+ @_hash2 = hash_code
138
+ end
139
+
140
+ def eval(parser, parser_call_stack)
141
+ i = 0
142
+ while i < @opnds.length
143
+ opnd = @opnds[i]
144
+ return false unless opnd.eval(parser, parser_call_stack)
145
+ i += 1
146
+ end
147
+ true
148
+ end
149
+
150
+ def eval_precedence(parser, parser_call_stack)
151
+ differs = false
152
+ operands = []
153
+ i = 0
154
+ while i < @opnds.length
155
+ context = @opnds[i]
156
+ evaluated = context.eval_precedence(parser, parser_call_stack)
157
+ differs |= (evaluated != context)
158
+ if evaluated == null
159
+ # The AND context is false if any element is false
160
+ return nil
161
+ elsif evaluated != NONE
162
+ # Reduce the result by skipping true elements
163
+ operands.add(evaluated)
164
+ end
165
+ i += 1
166
+ end
167
+
168
+ return self unless differs
169
+
170
+ if operands.empty?
171
+ # all elements were true, so the AND context is true
172
+ return NONE
173
+ end
174
+
175
+ result = operands[0]
176
+ i = 1
177
+ while i < operands.length
178
+ result = SemanticContext.and(result, operands.get(i))
179
+ i += 1
180
+ end
181
+
182
+ result
183
+ end
184
+
185
+ def to_s
186
+ @opnds.join('&&')
187
+ end
188
+ end
189
+
190
+ class OR < Operator
191
+ attr_accessor :opnds
192
+
193
+ def initialize(a, b)
194
+ operands = Set.new
195
+ if a.is_a? OR
196
+ operands.add_all(a.opnds)
197
+ else
198
+ operands.add(a)
199
+ end
200
+ if b.is_a? OR
201
+ operands.add_all(b.opnds)
202
+ else
203
+ operands.add(b)
204
+ end
205
+
206
+ precedence_predicates = filter_precedence_predicates(operands)
207
+ unless precedence_predicates.empty?
208
+ # interested in the transition with the highest precedence
209
+ reduced = precedence_predicates.max
210
+ operands.add(reduced)
211
+ end
212
+
213
+ @opnds = operands.to_s
214
+ end
215
+
216
+ def eql?(other)
217
+ return true if self == other
218
+ return false unless other.is_a? OR
219
+
220
+ @opnds.eql?(other.opnds)
221
+ end
222
+
223
+ def hash
224
+ hash_code = MurmurHash.hash(@opnds, OR.hash)
225
+ if !@_hash3.nil?
226
+ if hash_code == @_hash3
227
+ puts 'Same hash_code for SemanticContext_2'
228
+ else
229
+ puts 'Different hash_code for SemanticContext_2'
230
+ end
231
+ end
232
+ @_hash3 = hash_code
233
+ end
234
+
235
+ def eval(parser, parser_call_stack)
236
+ i = 0
237
+ while i < @opnds.length
238
+ opnd = @opnds[i]
239
+ return true if opnd.eval(parser, parser_call_stack)
240
+ i += 1
241
+ end
242
+ false
243
+ end
244
+
245
+ def eval_precedence(parser, parser_call_stack)
246
+ differs = false
247
+ operands = []
248
+ i = 0
249
+ while i < @opnds.length
250
+ context = @opnds[i]
251
+ evaluated = context.eval_precedence(parser, parser_call_stack)
252
+ differs |= (evaluated != context)
253
+ if evaluated == NONE
254
+ # The OR context is true if any element is true
255
+ return NONE
256
+ elsif evaluated != null
257
+ # Reduce the result by skipping false elements
258
+ operands.add(evaluated)
259
+ end
260
+ i += 1
261
+ end
262
+ return self unless differs
263
+
264
+ if operands.empty?
265
+ # all elements were false, so the OR context is false
266
+ return nil
267
+ end
268
+
269
+ result = operands[0]
270
+ i = 1
271
+ while i < operands.size
272
+ result = SemanticContext.or(result, operands.get(i))
273
+ i += 1
274
+ end
275
+
276
+ result
277
+ end
278
+
279
+ def to_s
280
+ @opnds.join('||')
281
+ end
282
+ end
283
+
284
+ def self.and(a, b)
285
+ return b if a.nil? || a == NONE
286
+ return a if b.nil? || b == NONE
287
+
288
+ result = AND.new(a, b)
289
+ return result.opnds[0] if result.opnds.length == 1
290
+
291
+ result
292
+ end
293
+
294
+ def self.or(a, b)
295
+ return b if a.nil?
296
+ return a if b.nil?
297
+ return NONE if a == NONE || b == NONE
298
+
299
+ OR result = OR.new(a, b)
300
+ return result.opnds[0] if result.opnds.length == 1
301
+
302
+ result
303
+ end
304
+
305
+ def self.filter_precedence_predicates(collection)
306
+ result = collection.select {|item| item.is_a? PrecedencePredicate}
307
+ collection.reject! {|item| (item.is_a? PrecedencePredicate)}
308
+ result
309
+ end
310
+
311
+ NONE = Predicate.new
312
+ end
313
+ end