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,133 @@
1
+ require 'antlr4/runtime/abstract_predicate_transition'
2
+ require 'antlr4/runtime/wildcard_transition'
3
+ require 'antlr4/runtime/not_set_transition'
4
+ require 'antlr4/runtime/rule_transition'
5
+ require 'antlr4/runtime/rule_stop_state'
6
+
7
+ module Antlr4::Runtime
8
+
9
+ class LL1Analyzer
10
+ @@hit_pred = Token::INVALID_TYPE
11
+
12
+ def initialize(atn)
13
+ @atn = atn
14
+ end
15
+
16
+ def decision_lookahead(s)
17
+ return nil if s.nil?
18
+
19
+ look = []
20
+ alt = 0
21
+ while alt < s.number_of_transitions
22
+ look[alt] = new IntervalSet
23
+ look_busy = Set.new
24
+ see_thru_preds = false # fail to get lookahead upon pred
25
+ _look(s.transition(alt).target, nil, EmptyPredictionContext::EMPTY, look[alt], look_busy, Set.new, see_thru_preds, false)
26
+ # Wipe out lookahead for this alternative if we found nothing
27
+ # or we had a predicate when we !see_thru_preds
28
+ look[alt] = nil if look[alt].empty? || look[alt].include?(HIT_PRED)
29
+ alt += 1
30
+ end
31
+ look
32
+ end
33
+
34
+ def look(s, stop_state, ctx)
35
+ r = IntervalSet.new
36
+ see_thru_preds = true # ignore preds get all lookahead
37
+ look_context = !ctx.nil? ? PredictionContextUtils.from_rule_context(s.atn, ctx) : nil
38
+ _look(s, stop_state, look_context, r, Set.new, Set.new, see_thru_preds, true)
39
+ r
40
+ end
41
+
42
+ def _look(s, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
43
+ # System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx)
44
+ c = ATNConfig.new
45
+ c.atn_config1(s, 0, ctx)
46
+ added = false
47
+ unless lookBusy.include? c
48
+ lookBusy.add(c)
49
+ added = true
50
+ end
51
+ return unless added
52
+
53
+ if s == stopState
54
+ if ctx.nil?
55
+ look.add(Token::EPSILON)
56
+ return
57
+ elsif ctx.empty? && addEOF
58
+ look.add(Token::EOF)
59
+ return
60
+ end
61
+ end
62
+
63
+ if s.is_a? RuleStopState
64
+ if ctx.nil?
65
+ look.add(Token::EPSILON)
66
+ return
67
+ elsif ctx.empty? && addEOF
68
+ look.add(Token::EOF)
69
+ return
70
+ end
71
+
72
+ if ctx != EmptyPredictionContext::EMPTY
73
+ # run thru all possible stack tops in ctx
74
+ removed = calledRuleStack.get(s.rule_index)
75
+ begin
76
+ calledRuleStack.clear(s.rule_index)
77
+ i = 0
78
+ while i < ctx.size
79
+ return_state = atn.states.get(ctx.get_return_state(i))
80
+
81
+ _look(return_state, stopState, ctx.get_parent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
82
+ i += 1
83
+ end
84
+ ensure
85
+ calledRuleStack.set(s.rule_index) if removed
86
+ end
87
+ return
88
+ end
89
+ end
90
+
91
+ n = s.number_of_transitions
92
+ i = 0
93
+ while i < n
94
+ t = s.transition(i)
95
+ if t.is_a? RuleTransition.class
96
+ if calledRuleStack.get(t.target.rule_index)
97
+ i += 1
98
+ next
99
+ end
100
+
101
+ new_ctx = SingletonPredictionContext.create(ctx, t.follow_state.state_number)
102
+
103
+ begin
104
+ calledRuleStack.set(t.target.rule_index)
105
+ _look(t.target, stopState, new_ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
106
+ ensure
107
+ calledRuleStack.clear(t.target.rule_index)
108
+ end
109
+ elsif t.is_a? AbstractPredicateTransition
110
+ if seeThruPreds
111
+ _look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
112
+ else
113
+ look.add(HIT_PRED)
114
+ end
115
+ elsif t.epsilon?
116
+ _look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
117
+ elsif t.is_a? WildcardTransition
118
+ look.add_all(IntervalSet.of(Token::MIN_USER_TOKEN_TYPE, atn.max_token_type))
119
+ else
120
+ set = t.label
121
+ unless set.nil?
122
+ if t.is_a? NotSetTransition
123
+ set = set.complement(IntervalSet.of(Token::MIN_USER_TOKEN_TYPE, atn.max_token_type))
124
+ end
125
+ look.add_all(set)
126
+ end
127
+ end
128
+
129
+ i += 1
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,10 @@
1
+ module Antlr4::Runtime
2
+ class LookaheadEventInfo < DecisionEventInfo
3
+ attr_reader :predicted_alt
4
+
5
+ def initialize(decision, configs, predicted_alt, input, start_index, stop_index, fullCtx)
6
+ super(decision, configs, input, start_index, stop_index, fullCtx)
7
+ @predicted_alt = predicted_alt
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module Antlr4::Runtime
2
+
3
+ class LoopEndState < ATNState
4
+ attr_accessor :loopback_state
5
+
6
+ def initialize
7
+ super
8
+ @loopback_state = nil
9
+ end
10
+
11
+ def state_type
12
+ LOOP_END
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,99 @@
1
+ require 'rumourhash/rumourhash'
2
+
3
+ include RumourHash
4
+
5
+ module Antlr4::Runtime
6
+ class MurmurHash
7
+ DEFAULT_SEED = 0
8
+ MASK_32 = 0xFFFFFFFF
9
+
10
+ def self.hash_int(num)
11
+ hash_code = 7
12
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num)
13
+ RumourHash.rumour_hash_finish(hash_code, 0)
14
+ end
15
+
16
+ def self.hash_int_obj(num, obj)
17
+ hash_code = 7
18
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num)
19
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, !obj.nil? ? obj.hash : 0)
20
+ RumourHash.rumour_hash_finish(hash_code, 2)
21
+ end
22
+
23
+ def self.hash_int_int_obj_obj(num1, num2, obj1, obj2)
24
+ hash_code = 7
25
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num1)
26
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num2)
27
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, !obj1.nil? ? obj1.hash : 0)
28
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, !obj2.nil? ? obj2.hash : 0)
29
+ RumourHash.rumour_hash_finish(hash_code, 4)
30
+ end
31
+
32
+ def self.hash_int_int(num1, num2)
33
+ hash_code = 7
34
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num1)
35
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num2)
36
+ RumourHash.rumour_hash_finish(hash_code, 2)
37
+ end
38
+
39
+ def self.hash_objs(objs)
40
+ hash_code = 7
41
+
42
+ i = 0
43
+ while i < objs.length
44
+ obj = objs[i]
45
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, !obj.nil? ? obj.hash : 0)
46
+ i += 1
47
+ end
48
+
49
+ RumourHash.rumour_hash_finish(hash_code, objs.length)
50
+ end
51
+
52
+ def self.hash_ints_objs(nums, objs)
53
+ hash_code = 7
54
+
55
+ i = 0
56
+ while i < objs.length
57
+ obj = objs[i]
58
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, !obj.nil? ? obj.hash : 0)
59
+ i += 1
60
+ end
61
+
62
+ i = 0
63
+ while i < nums.length
64
+ num = nums[i]
65
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num)
66
+ i += 1
67
+ end
68
+
69
+ RumourHash.rumour_hash_finish(hash_code, 2 * objs.length)
70
+ end
71
+
72
+ def self.hash_ints(nums)
73
+ hash_code = 7
74
+
75
+ i = 0
76
+ while i < nums.length
77
+ num = nums[i]
78
+ hash_code = RumourHash.rumour_hash_update_int(hash_code, num)
79
+ i += 1
80
+ end
81
+
82
+ RumourHash.rumour_hash_finish(hash_code, 2 * nums.length)
83
+ end
84
+
85
+ private
86
+
87
+ def self.hash(data, seed)
88
+ hash = seed
89
+ i = 0
90
+ while i < data.length
91
+ value = data[i]
92
+ hash = RumourHash.rumour_hash_update_int(hash, !value.nil? ? value.hash : 0)
93
+ i += 1
94
+ end
95
+
96
+ RumourHash.rumour_hash_finish(hash, data.length)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,7 @@
1
+ module Antlr4::Runtime
2
+
3
+ class NoViableAltException < RecognitionException
4
+ attr_accessor :dead_end_configs
5
+ attr_accessor :start_token
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ module Antlr4::Runtime
2
+
3
+ class NotSetTransition < SetTransition
4
+ def initialize(target, set)
5
+ super(target, set)
6
+ end
7
+
8
+ def serialization_type
9
+ NOT_SET
10
+ end
11
+
12
+ def matches(symbol, min_vocab_symbol, max_vocab_symbol)
13
+ (symbol >= min_vocab_symbol) && (symbol <= max_vocab_symbol) && !super
14
+ end
15
+
16
+ def to_s
17
+ '~' + super.to_s
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module Antlr4::Runtime
2
+
3
+ class ObjectEqualityComparator
4
+ include Singleton
5
+
6
+ def hash(obj)
7
+ return 0 if obj.nil?
8
+
9
+ obj.hash
10
+ end
11
+
12
+ def equals(a, b)
13
+ return b.nil? if a.nil?
14
+
15
+ a.eql?(b)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Antlr4::Runtime
2
+
3
+ class OrderedATNConfigSet < ATNConfigSet
4
+ class LexerConfigHashSet < AbstractConfigHashSet
5
+ def initialize
6
+ super(ObjectEqualityComparator.instance)
7
+ end
8
+ end
9
+
10
+ def initialize
11
+ super
12
+ @config_lookup = LexerConfigHashSet.new
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Antlr4::Runtime
2
+
3
+ class ParseCancellationException < StandardError
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'antlr4/runtime/syntax_tree'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class ParseTree < SyntaxTree
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module Antlr4::Runtime
2
+ class ParseTreeListener
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Antlr4::Runtime
2
+ class ParseTreeVisitor
3
+ end
4
+ end
@@ -0,0 +1,522 @@
1
+ require 'antlr4/runtime/parse_tree_listener'
2
+ require 'antlr4/runtime/default_error_strategy'
3
+ require 'antlr4/runtime/terminal_node_impl'
4
+ require 'antlr4/runtime/error_node_impl'
5
+
6
+ module Antlr4::Runtime
7
+
8
+ class Parser < Recognizer
9
+ class TraceListener < ParseTreeListener
10
+ def initialize(parser, input)
11
+ @parser = parser
12
+ @_input = input
13
+ end
14
+
15
+ def enter_every_rule(ctx)
16
+ puts('enter ' << @parser.rule_names[ctx.rule_index] << ', lt(1)=' << @_input.lt(1).text)
17
+ end
18
+
19
+ def visit_terminal(node, ctx)
20
+ puts('consume ' << node.symbol.to_s << ' rule ' << @parser.rule_names[ctx.rule_index].to_s)
21
+ end
22
+
23
+ def visit_error_node(_node)
24
+ end
25
+
26
+ def exit_every_rule(ctx)
27
+ puts('exit ' << @parser.rule_names[ctx.rule_index] << ', lt(1)=' << @_input.lt(1).text)
28
+ end
29
+ end
30
+
31
+ class TrimToSizeListener < ParseTreeListener
32
+ include Singleton
33
+
34
+ def enter_every_rule(_ctx)
35
+ end
36
+
37
+ def visit_terminal(_node, _ctx)
38
+ end
39
+
40
+ def visit_error_node(_node)
41
+ end
42
+
43
+ def exit_every_rule(ctx)
44
+ ctx.children.trimToSize if ctx.children.is_a? ArrayList
45
+ end
46
+ end
47
+
48
+ attr_accessor :_ctx
49
+ attr_reader :_input
50
+
51
+ @@bypass_alts_atn_cache = {}
52
+
53
+ def initialize(input)
54
+ super()
55
+ @_err_handler = DefaultErrorStrategy.new
56
+ @_input = nil
57
+ @_precedence_stack = []
58
+ @_precedence_stack.push(0)
59
+ @_ctx = nil
60
+ @_build_parse_trees = true
61
+ @_tracer = nil
62
+ @_parse_listeners = nil
63
+ @_syntax_errors = nil
64
+ @matched_eof = nil
65
+ set_token_stream(input)
66
+ end
67
+
68
+ def reset
69
+ @_input.seek(0) unless @_input.nil?
70
+ @_err_handler.reset(self)
71
+ @_ctx = nil
72
+ @_syntax_errors = 0
73
+ @matched_eof = false
74
+ set_trace(false)
75
+ @_precedence_stack.clear
76
+ @_precedence_stack.push(0)
77
+ interpreter = @_interp
78
+ interpreter.reset unless interpreter.nil?
79
+ end
80
+
81
+ def match(ttype)
82
+ t = current_token
83
+ if t.type == ttype
84
+ @matched_eof = true if ttype == Token::EOF
85
+ @_err_handler.report_match(self)
86
+ consume
87
+ else
88
+ t = @_err_handler.recover_in_line(self)
89
+ if @_build_parse_trees && t.index == -1
90
+ # we must have conjured up a new token during single token insertion
91
+ # if it's not the current symbol
92
+ @_ctx.add_error_node(create_error_node(@_ctx, t))
93
+ end
94
+ end
95
+
96
+ t
97
+ end
98
+
99
+ def match_wildcard
100
+ t = current_token
101
+ if t.type > 0
102
+ @_err_handler.report_match(this)
103
+ consume
104
+ else
105
+ t = @_err_handler.recover_in_line(this)
106
+ if @_build_parse_trees && t.token_index == -1
107
+ # we must have conjured up a new token during single token insertion
108
+ # if it's not the current symbol
109
+ @_ctx.add_error_node(create_error_node(@_ctx, t))
110
+ end
111
+ end
112
+
113
+ t
114
+ end
115
+
116
+ def set_trim_parse_tree(trimParseTrees)
117
+ if trimParseTrees
118
+ return if get_trim_parse_tree
119
+
120
+ add_parse_listener(TrimToSizeListener.INSTANCE)
121
+ else
122
+ remove_parse_listener(TrimToSizeListener.INSTANCE)
123
+ end
124
+ end
125
+
126
+ def get_trim_parse_tree
127
+ get_parse_listeners.contains(TrimToSizeListener.INSTANCE)
128
+ end
129
+
130
+ def get_parse_listeners
131
+ listeners = @_parse_listeners
132
+ return [] if listeners.nil?
133
+
134
+ listeners
135
+ end
136
+
137
+ def add_parse_listener(listener)
138
+ raise nilPointerException, 'listener' if listener.nil?
139
+
140
+ @_parse_listeners = [] if @_parse_listeners.nil?
141
+
142
+ @_parse_listeners << listener
143
+ end
144
+
145
+ def remove_parse_listener(listener)
146
+ unless @_parse_listeners.nil?
147
+ if @_parse_listeners.remove(listener)
148
+ @_parse_listeners = nil if @_parse_listeners.empty?
149
+ end
150
+ end
151
+ end
152
+
153
+ def remove_parse_listeners
154
+ @_parse_listeners = nil
155
+ end
156
+
157
+ def trigger_enter_rule_event
158
+ @_parse_listeners.each do |listener|
159
+ listener.enter_every_rule(@_ctx)
160
+ @_ctx.enter_rule(listener)
161
+ end
162
+ end
163
+
164
+ def trigger_exit_rule_event # reverse order walk of listeners
165
+ i = @_parse_listeners.length - 1
166
+ while i >= 0
167
+
168
+ listener = @_parse_listeners[i]
169
+ @_ctx.exit_rule(listener)
170
+ listener.exit_every_rule(@_ctx)
171
+ i -= 1
172
+ end
173
+ end
174
+
175
+ def token_factory
176
+ @_input.token_source.token_factory
177
+ end
178
+
179
+ def set_token_factory(factory)
180
+ @_input.token_source.set_token_factory(factory)
181
+ end
182
+
183
+ def get_atn_with_bypass_alts
184
+ serialized_atn = get_serialized_atn
185
+ if serialized_atn.nil?
186
+ raise UnsupportedOperationException, 'The current parser does not support an ATN with bypass alternatives.'
187
+ end
188
+
189
+ result = @@bypass_alts_atn_cache.get(serialized_atn)
190
+ if result.nil?
191
+ deserialization_options = ATNDeserializationOptions.new
192
+ deserialization_options.generate_rule_bypass_transitions(true)
193
+ result = ATNDeserializer.new(deserialization_options).deserialize(serialized_atn)
194
+ @@bypass_alts_atn_cache.put(serialized_atn, result)
195
+ end
196
+
197
+ result
198
+ end
199
+
200
+ def compile_parse_tree_pattern1(pattern, patter_rule_index)
201
+ unless getTokenStream.nil?
202
+ token_source = getTokenStream.token_source
203
+ if token_source.is_a? Lexer
204
+ lexer = token_source
205
+ return compile_parse_tree_pattern2(pattern, patter_rule_index, lexer)
206
+ end
207
+ end
208
+ raise UnsupportedOperationException, "Parser can't discover a lexer to use"
209
+ end
210
+
211
+ def compile_parse_tree_pattern2(pattern, patternRuleIndex, lexer)
212
+ m = ParseTreePatternMatcher.new(lexer, self)
213
+ m.compile(pattern, patternRuleIndex)
214
+ end
215
+
216
+ def set_token_stream(input)
217
+ @_input = nil
218
+ reset
219
+ @_input = input
220
+ end
221
+
222
+ def current_token
223
+ @_input.lt(1)
224
+ end
225
+
226
+ def notify_error_listeners_simple(msg)
227
+ notify_error_listeners(current_token, msg, nil)
228
+ end
229
+
230
+ def notify_error_listeners(offending_token, msg, e)
231
+ @_syntax_errors += 1
232
+ line = offending_token.line
233
+ char_position_in_line = offending_token.char_position_in_line
234
+
235
+ listener = error_listener_dispatch
236
+ listener.syntax_error(self, offending_token, line, char_position_in_line, msg, e)
237
+ end
238
+
239
+ def consume
240
+ o = current_token
241
+ @_input.consume if o.type != EOF
242
+ has_listener = !@_parse_listeners.nil? && !@_parse_listeners.empty?
243
+ if @_build_parse_trees || has_listener
244
+ if @_err_handler.in_error_recovery_mode(self)
245
+ node = @_ctx.add_error_node(create_error_node(@_ctx, o))
246
+ unless @_parse_listeners.nil?
247
+ @_parse_listeners.each do |listener|
248
+ listener.visit_error_node(node)
249
+ end
250
+ end
251
+ else
252
+ node = @_ctx.add_child_terminal_node(create_terminal_node(@_ctx, o))
253
+ unless @_parse_listeners.nil?
254
+ @_parse_listeners.each do |listener|
255
+ listener.visit_terminal(node, @_ctx)
256
+ end
257
+ end
258
+ end
259
+ end
260
+
261
+ o
262
+ end
263
+
264
+ def create_terminal_node(_parent, t)
265
+ TerminalNodeImpl.new(t)
266
+ end
267
+
268
+ def create_error_node(_parent, t)
269
+ ErrorNodeImpl.new(t)
270
+ end
271
+
272
+ def add_context_to_parse_tree
273
+ parent = @_ctx.parent
274
+ # add current context to parent if we have a parent
275
+ parent.add_child_rule_invocation(@_ctx) unless parent.nil?
276
+ end
277
+
278
+ def enter_rule(local_ctx, state, _rule_index)
279
+ @_state_number = state
280
+ @_ctx = local_ctx
281
+ @_ctx.start = @_input.lt(1)
282
+ add_context_to_parse_tree if @_build_parse_trees
283
+ trigger_enter_rule_event unless @_parse_listeners.nil?
284
+ end
285
+
286
+ def exit_rule
287
+ if @matched_eof
288
+ # if we have matched EOF, it cannot consume past EOF so we use lt(1) here
289
+ @_ctx.stop = @_input.lt(1) # lt(1) will be end of file
290
+ else
291
+ @_ctx.stop = @_input.lt(-1) # stop node is what we just matched
292
+ end
293
+
294
+ # trigger event on @_ctx, before it reverts to parent
295
+ trigger_exit_rule_event unless @_parse_listeners.nil?
296
+ @_state_number = @_ctx.invoking_state
297
+ @_ctx = @_ctx.parent
298
+ end
299
+
300
+ def enter_outer_alt(local_ctx, alt_num)
301
+ local_ctx.set_alt_number(alt_num)
302
+ # if we have new local_ctx, make sure we replace existing ctx
303
+ # that is previous child of parse tree
304
+ if @_build_parse_trees && @_ctx != local_ctx
305
+ parent = @_ctx.parent
306
+ unless parent.nil?
307
+ parent.remove_last_child
308
+ parent.addChild(local_ctx)
309
+ end
310
+ end
311
+ @_ctx = local_ctx
312
+ end
313
+
314
+ def precedence
315
+ return -1 if @_precedence_stack.empty?
316
+
317
+ @_precedence_stack.peek
318
+ end
319
+
320
+ def enter_recursion_rule(local_ctx, state, _rule_index, precedence)
321
+ setState(state)
322
+ @_precedence_stack.push(precedence)
323
+ @_ctx = local_ctx
324
+ @_ctx.start = @_input.lt(1)
325
+ unless @_parse_listeners.nil?
326
+ trigger_enter_rule_event # simulates rule entry for left-recursive rules
327
+ end
328
+ end
329
+
330
+ def push_new_recursion_context(local_ctx, state, _rule_index)
331
+ previous = @_ctx
332
+ previous.parent = local_ctx
333
+ previous.invoking_state = state
334
+ previous.stop = @_input.lt(-1)
335
+
336
+ @_ctx = local_ctx
337
+ @_ctx.start = previous.start
338
+ @_ctx.addChild(previous) if @_build_parse_trees
339
+
340
+ unless @_parse_listeners.nil?
341
+ trigger_enter_rule_event # simulates rule entry for left-recursive rules
342
+ end
343
+ end
344
+
345
+ def unroll_recursion_contexts(_parent_ctx)
346
+ @_precedence_stack.pop
347
+ @_ctx.stop = @_input.lt(-1)
348
+ retctx = @_ctx # save current ctx (return value)
349
+
350
+ # unroll so @_ctx is as it was before call to recursive method
351
+ if !@_parse_listeners.nil?
352
+ while @_ctx != _parent_ctx
353
+ trigger_exit_rule_event
354
+ @_ctx = @_ctx.parent
355
+ end
356
+
357
+ else
358
+ _ctx = _parent_ctx
359
+ end
360
+
361
+ # hook into tree
362
+ retctx.parent = _parent_ctx
363
+
364
+ if @_build_parse_trees && !_parent_ctx.nil?
365
+ # add return ctx into invoking rule's tree
366
+ _parent_ctx.addChild(retctx)
367
+ end
368
+ end
369
+
370
+ def invoking_context(rule_index)
371
+ p = @_ctx
372
+ until p.nil?
373
+ return p if p.rule_index == rule_index
374
+
375
+ p = p.parent
376
+ end
377
+ nil
378
+ end
379
+
380
+ def precpred(_localctx, precedence)
381
+ precedence >= @_precedence_stack.peek
382
+ end
383
+
384
+ def in_context?(_context)
385
+ false
386
+ end
387
+
388
+ def expected_token?(symbol)
389
+ atn = @_interp.atn
390
+ ctx = @_ctx
391
+ s = atn.states.get(getState)
392
+ following = atn.next_tokens(s)
393
+ return true if following.include?(symbol)
394
+
395
+ return false unless following.contains(Token::EPSILON)
396
+
397
+ while !ctx.nil? && ctx.invoking_state >= 0 && following.include?(Token::EPSILON)
398
+ invoking_state = atn.states.get(ctx.invoking_state)
399
+ rt = invoking_state.transition(0)
400
+ following = atn.next_tokens(rt.follow_state)
401
+ return true if following.include?(symbol)
402
+
403
+ ctx = ctx.parent
404
+ end
405
+
406
+ return true if following.include?(Token::EPSILON) && symbol == Token::EOF
407
+
408
+ false
409
+ end
410
+
411
+ def matched_eof?
412
+ @matched_eof
413
+ end
414
+
415
+ def expected_tokens
416
+ atn = @_interp.atn
417
+ atn.expected_tokens(@_state_number, @_ctx)
418
+ end
419
+
420
+ def expected_tokens_within_current_rule
421
+ atn = @_interp.atn
422
+ s = atn.states.get(getState)
423
+ atn.next_tokens(s)
424
+ end
425
+
426
+ def rule_index(rule_name)
427
+ index = get_rule_index_map.get(rule_name)
428
+ return index unless index.nil?
429
+
430
+ -1
431
+ end
432
+
433
+ def rule_invocation_stack1
434
+ rule_invocation_stack2(@_ctx)
435
+ end
436
+
437
+ def rule_invocation_stack2(p)
438
+ rule_names = rule_names
439
+ stack = []
440
+ until p.nil?
441
+ # compute what follows who invoked us
442
+ rule_index = p.rule_index
443
+ if rule_index < 0
444
+ stack.push('n/a')
445
+ else
446
+ stack.push(rule_names[rule_index])
447
+ end
448
+ p = p.parent
449
+ end
450
+ stack
451
+ end
452
+
453
+ def dfa_strings
454
+ s = []
455
+ d = 0
456
+ while d < @_interp.decision_to_dfa.length
457
+ dfa = @_interp.decision_to_dfa[d]
458
+ s.push(dfa.to_s(get_vocabulary))
459
+ d += 1
460
+ end
461
+ s
462
+ end
463
+
464
+ def dump_dfa
465
+ seen_one = false
466
+ d = 0
467
+ while d < @_interp.decision_to_dfa.length
468
+ dfa = @_interp.decision_to_dfa[d]
469
+ unless dfa.states.empty?
470
+ puts if seen_one
471
+ puts('Decision ' << dfa.decision << ':')
472
+ puts(dfa.to_s(get_vocabulary))
473
+ seen_one = true
474
+ end
475
+ d += 1
476
+ end
477
+ end
478
+
479
+ def source_name
480
+ @_input.get_source_name
481
+ end
482
+
483
+ def parse_info
484
+ interp = @_interp
485
+ return ParseInfo.new(interp) if interp.is_a? ProfilingATNSimulator
486
+
487
+ nil
488
+ end
489
+
490
+ def set_profile(profile)
491
+ interp = @_interp
492
+ save_mode = interp.getPredictionMode
493
+ if profile
494
+ unless interp.is_a? ProfilingATNSimulator
495
+ @_interp = ProfilingATNSimulator.new(self)
496
+ end
497
+ elsif @_interp.is_a? ProfilingATNSimulator
498
+ sim = ParserATNSimulator.new(self, atn, interp.decision_to_dfa, interp.shared_context_cache)
499
+ @_interp = sim
500
+ end
501
+ @_interp.setPredictionMode(save_mode)
502
+ end
503
+
504
+ def set_trace(trace)
505
+ if !trace
506
+ remove_parse_listener(@_tracer)
507
+ @_tracer = nil
508
+ else
509
+ if !@_tracer.nil?
510
+ remove_parse_listener(@_tracer)
511
+ else
512
+ @_tracer = new TraceListener
513
+ end
514
+ add_parse_listener(@_tracer)
515
+ end
516
+ end
517
+
518
+ def trace?
519
+ !@_tracer.nil?
520
+ end
521
+ end
522
+ end