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,29 @@
1
+ module Antlr4::Runtime
2
+
3
+ class SetTransition < Transition
4
+ attr_reader :set
5
+
6
+ def initialize(target, set)
7
+ super(target)
8
+ set = IntervalSet.of(Token::INVALID_TYPE) if set.nil?
9
+
10
+ @set = set
11
+ end
12
+
13
+ def serialization_type
14
+ SET
15
+ end
16
+
17
+ def label
18
+ @set
19
+ end
20
+
21
+ def matches(symbol, _min_vocab_symbol, _max_vocab_symbol)
22
+ @set.contains(symbol)
23
+ end
24
+
25
+ def to_s
26
+ @set.to_s
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ require 'antlr4/runtime/prediction_context'
2
+ require 'antlr4/runtime/prediction_context_utils'
3
+
4
+ module Antlr4::Runtime
5
+
6
+ class SingletonPredictionContext < PredictionContext
7
+ attr_accessor :parent
8
+ attr_accessor :return_state
9
+
10
+ def initialize(parent, return_state)
11
+ super(!parent.nil? ? PredictionContextUtils.calculate_hash_code1(parent, return_state) : PredictionContextUtils.calculate_empty_hash_code)
12
+ @parent = parent
13
+ @return_state = return_state
14
+ end
15
+
16
+ def get_parent(_i)
17
+ @parent
18
+ end
19
+
20
+ def size
21
+ 1
22
+ end
23
+
24
+ def empty?
25
+ @return_state == EMPTY_RETURN_STATE
26
+ end
27
+
28
+ def get_return_state(_index)
29
+ @return_state
30
+ end
31
+
32
+ def equals(other)
33
+ if self == other
34
+ return true
35
+ elsif !(other.is_a? SingletonPredictionContext)
36
+ return false
37
+ end
38
+
39
+ if hash != other.hash
40
+ return false # can't be same if hash is different
41
+ end
42
+
43
+ @return_state == other.return_state && (!@parent.nil? && @parent.eql?(other.parent))
44
+ end
45
+
46
+ def to_s
47
+ up = !@parent.nil? ? @parent.to_s : ''
48
+ if up.empty?
49
+ return '$' if @return_state == EMPTY_RETURN_STATE
50
+
51
+ return @return_state.to_s
52
+ end
53
+ @return_state.to_s + ' ' + up
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ module Antlr4::Runtime
2
+
3
+ class StarBlockStartState < BlockStartState
4
+ def initialize
5
+ super
6
+ end
7
+
8
+ def state_type
9
+ STAR_BLOCK_START
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Antlr4::Runtime
2
+
3
+ class StarLoopEntryState < DecisionState
4
+ attr_accessor :loopback_state
5
+ attr_accessor :is_precedence_pecision
6
+
7
+ def initialize
8
+ super
9
+ @loopback_state = nil
10
+ @is_precedence_pecision = false
11
+ end
12
+
13
+ def state_type
14
+ STAR_LOOP_ENTRY
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Antlr4::Runtime
2
+
3
+ class StarLoopbackState < ATNState
4
+ def initialize
5
+ super
6
+ end
7
+
8
+ def loop_entry_state
9
+ transition(0).target
10
+ end
11
+
12
+ def state_type
13
+ STAR_LOOP_BACK
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ require 'antlr4/runtime/tree'
2
+
3
+ module Antlr4::Runtime
4
+ class SyntaxTree < Tree
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ module Antlr4::Runtime
2
+
3
+ class TagChunk < Chunk
4
+ attr_reader :tag
5
+ attr_reader :label
6
+
7
+ def initialize(label, tag)
8
+ if tag.nil? || tag.empty?
9
+ raise IllegalArgumentException, 'tag cannot be nil or empty'
10
+ end
11
+
12
+ @label = label
13
+ @tag = tag
14
+ end
15
+
16
+ def to_s
17
+ return @label + ':' + @tag unless @label.nil?
18
+
19
+ @tag
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Antlr4::Runtime
2
+
3
+ class TerminalNode < ParseTree
4
+ end
5
+ end
@@ -0,0 +1,50 @@
1
+ require 'antlr4/runtime/terminal_node'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class TerminalNodeImpl < TerminalNode
6
+ attr_accessor :symbol
7
+ attr_accessor :parent
8
+
9
+ def initialize(symbol)
10
+ @symbol = symbol
11
+ end
12
+
13
+ def child(_i)
14
+ nil
15
+ end
16
+
17
+ def payload
18
+ @symbol
19
+ end
20
+
21
+ def source_interval
22
+ return Interval.invalid if @symbol.nil?
23
+
24
+ token_index = @symbol.token_index
25
+ Interval.new(token_index, token_index)
26
+ end
27
+
28
+ def child_count
29
+ 0
30
+ end
31
+
32
+ def accept(visitor)
33
+ visitor.visit_terminal(self)
34
+ end
35
+
36
+ def text
37
+ @symbol.text
38
+ end
39
+
40
+ def to_string_tree(_parser = nil)
41
+ to_s
42
+ end
43
+
44
+ def to_s
45
+ return '<EOF>' if @symbol.type == Token::EOF
46
+
47
+ @symbol.text
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ module Antlr4::Runtime
2
+
3
+ class TextChunk < Chunk
4
+ attr_reader :text
5
+
6
+ def initialize(text)
7
+ raise IllegalArgumentException, 'text cannot be null' if text.nil?
8
+
9
+ @text = text
10
+ end
11
+
12
+ def to_s
13
+ "'" + @text + "'"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require 'antlr4/runtime/int_stream'
2
+ module Antlr4::Runtime
3
+
4
+ class Token
5
+ INVALID_TYPE = 0
6
+ EPSILON = -2
7
+ MIN_USER_TOKEN_TYPE = 1
8
+ EOF = IntStream::EOF
9
+ DEFAULT_CHANNEL = 0
10
+ HIDDEN_CHANNEL = 1
11
+ MIN_USER_CHANNEL_VALUE = 2
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Antlr4::Runtime
2
+
3
+ class TokenStream < IntStream
4
+ def lt(k)
5
+ end
6
+
7
+ def get(index)
8
+ end
9
+
10
+ def token_source
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Antlr4::Runtime
2
+ class TokenTagToken < CommonToken
3
+ attr_reader :token_name
4
+ attr_reader :label
5
+
6
+ def initialize(token_name, type, label = nil)
7
+ super(type)
8
+ @token_name = token_name
9
+ @label = label
10
+ end
11
+
12
+ def text
13
+ return '<' + @label + ':' + @token_name + '>' unless @label.nil?
14
+
15
+ '<' + @token_name + '>'
16
+ end
17
+
18
+ def to_s
19
+ @token_name + ':' + @type
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ require 'antlr4/runtime/decision_state'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class TokensStartState < DecisionState
6
+ def initialize
7
+ super
8
+ end
9
+
10
+ def state_type
11
+ TOKEN_START
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,51 @@
1
+ module Antlr4::Runtime
2
+ class Transition
3
+ EPSILON = 1
4
+ RANGE = 2
5
+ RULE = 3
6
+ PREDICATE = 4
7
+ ATOM = 5
8
+ ACTION = 6
9
+ SET = 7
10
+ NOT_SET = 8
11
+ WILDCARD = 9
12
+ PRECEDENCE = 10
13
+
14
+ class << self
15
+
16
+ @@serialization_names = %w[INVALID EPSILON RANGE RULE PREDICATE ATOM ACTION SET NOT_SET WILDCARD PRECEDENCE]
17
+
18
+ @@serialization_types = {}
19
+
20
+ @@serialization_types[:EpsilonTransition] = EPSILON
21
+ @@serialization_types[:RangeTransition] = RANGE
22
+ @@serialization_types[:RuleTransition] = RULE
23
+ @@serialization_types[:PredicateTransition] = PREDICATE
24
+ @@serialization_types[:AtomTransition] = ATOM
25
+ @@serialization_types[:ActionTransition] = ACTION
26
+ @@serialization_types[:SetTransition] = SET
27
+ @@serialization_types[:NotSetTransition] = NOT_SET
28
+ @@serialization_types[:WildcardTransition] = WILDCARD
29
+ @@serialization_types[:PrecedencePredicateTransition] = PRECEDENCE
30
+ end
31
+
32
+ attr_accessor :target
33
+
34
+ def initialize(target)
35
+ raise 'target cannot be null.' if target.nil?
36
+
37
+ @target = target
38
+ end
39
+
40
+ def epsilon?
41
+ false
42
+ end
43
+
44
+ def label
45
+ nil
46
+ end
47
+
48
+ def matches(symbol, min_vocab_symbol, max_vocab_symbol)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,4 @@
1
+ module Antlr4::Runtime
2
+ class Tree
3
+ end
4
+ end
@@ -0,0 +1,195 @@
1
+ module Antlr4::Runtime
2
+ class Trees
3
+ def self.to_s_tree_recog(t, recog = nil)
4
+ rule_names = !recog.nil? ? recog.rule_names : nil
5
+ rule_names_list = !rule_names.nil? ? rule_names : nil
6
+ to_s_tree_rulenames(t, rule_names_list)
7
+ end
8
+
9
+ def self.to_s_tree_rulenames(t, rule_names)
10
+ s = Utils.escape_whitespace(getNodeText(t, rule_names), false)
11
+ return s if t.child_count == 0
12
+
13
+ buf = ''
14
+ buf << '('
15
+ s = Utils.escape_whitespace(getNodeText(t, rule_names), false)
16
+ buf << s
17
+ buf << ' '
18
+ i = 0
19
+ while i < t.child_count
20
+ buf << ' ' if i > 0
21
+ buf << to_string_tree(t.child(i), rule_names)
22
+ i += 1
23
+ end
24
+ buf << ')'
25
+ buf
26
+ end
27
+
28
+ def self.node_text_recog(t, recog)
29
+ rule_names = !recog.nil? ? recog.rule_names : nil
30
+ rule_names_list = !rule_names.nil? ? rule_names : nil
31
+ getNodeText(t, rule_names_list)
32
+ end
33
+
34
+ def self.node_text_rulenames(t, rule_names)
35
+ unless rule_names.nil?
36
+ if t.is_a? RuleContext
37
+ rule_index = t.rule_context.rule_index
38
+ rule_name = rule_names[rule_index]
39
+ alt_number = t.alt_number
40
+ if alt_number != ATN::INVALID_ALT_NUMBER
41
+ return rule_name + ':' + alt_number
42
+ end
43
+
44
+ return rule_name
45
+ elsif t.is_a? ErrorNode
46
+ return t.to_s
47
+ elsif t.is_a? TerminalNode
48
+ symbol = t.getSymbol
49
+ unless symbol.nil?
50
+ s = symbol.text
51
+ return s
52
+ end
53
+ end
54
+ end
55
+ # no recog for rule names
56
+ payload = t.payload
57
+ return payload.text if payload.is_a Token
58
+
59
+ t.payload.to_s
60
+ end
61
+
62
+ def self.children(t)
63
+ kids = []
64
+ i = 0
65
+ while i < t.child_count
66
+ kids << t.child(i)
67
+ i += 1
68
+ end
69
+
70
+ kids
71
+ end
72
+
73
+ def self.ancestors(t)
74
+ return [] if t.get_parent.nil?
75
+
76
+ ancestors = []
77
+ t = t.get_parent
78
+ until t.nil?
79
+ ancestors.unshift(t) # insert at start
80
+ t = t.get_parent
81
+ end
82
+ ancestors
83
+ end
84
+
85
+ def self.ancestor_of?(t, u)
86
+ return false if t.nil? || u.nil? || t.get_parent.nil?
87
+
88
+ p = u.get_parent
89
+ until p.nil?
90
+ return true if t == p
91
+
92
+ p = p.get_parent
93
+ end
94
+ false
95
+ end
96
+
97
+ def self.find_all_token_nodes(t, ttype)
98
+ find_all_nodes(t, ttype, true)
99
+ end
100
+
101
+ def self.find_all_rule_nodes(t, rule_index)
102
+ find_all_nodes(t, rule_index, false)
103
+ end
104
+
105
+ def self.find_all_nodes(t, index, find_tokens)
106
+ nodes = []
107
+ _find_all_nodes(t, index, find_tokens, nodes)
108
+ nodes
109
+ end
110
+
111
+ def self._find_all_nodes(t, index, find_tokens, nodes)
112
+ # check this node (the root) first
113
+ if find_tokens && t.is_a?(TerminalNode)
114
+ tnode = t
115
+ nodes.add(t) if tnode.getSymbol.type == index
116
+ elsif !find_tokens && t.is_a(ParserRuleContext)
117
+ ctx = t
118
+ nodes.push(t) if ctx.rule_index == index
119
+ end
120
+ # check children
121
+ i = 0
122
+ while i < t.child_count
123
+ _find_all_nodes(t.child(i), index, find_tokens, nodes)
124
+ i += 1
125
+ end
126
+ end
127
+
128
+ def self.descendants(t)
129
+ nodes = []
130
+ nodes.push(t)
131
+
132
+ n = t.child_count
133
+ i = 0
134
+ while i < n
135
+ nodes.add_all(descendants(t.child(i)))
136
+ i += 1
137
+ end
138
+ nodes
139
+ end
140
+
141
+ def self.root_of_subtree_enclosing_region(t, start_token_index, stop_token_index)
142
+ n = t.child_count
143
+ i = 0
144
+ while i < n
145
+ ParseTree child = t.child(i)
146
+ ParserRuleContext r = root_of_subtree_enclosing_region(child, start_token_index, stop_token_index)
147
+ return r unless r.nil?
148
+
149
+ i += 1
150
+ end
151
+ if t.is_a? ParserRuleContext
152
+ r = t
153
+ if start_token_index >= r.getStart.token_index && # is range fully contained in t?
154
+ (r.getStop.nil? || stop_token_index <= r.getStop.token_index)
155
+
156
+ # note: r.getStop()==nil likely implies that we bailed out of parser and there's nothing to the right
157
+ return r
158
+ end
159
+ end
160
+ nil
161
+ end
162
+
163
+ def self.strip_children_out_of_range(t, root, start_index, stop_index)
164
+ return if t.nil?
165
+
166
+ i = 0
167
+ while i < t.child_count
168
+ child = t.child(i)
169
+ range = child.source_interval
170
+ next unless child.is_a? ParserRuleContext && (range.b < start_index || range.a > stop_index)
171
+
172
+ if ancestor_of?(child, root) # replace only if subtree doesn't have displayed root
173
+ abbrev = CommonToken.new(Token::INVALID_TYPE)
174
+ t.children.set(i, TerminalNodeImpl.new(abbrev))
175
+ end
176
+ end
177
+ end
178
+
179
+ def self.find_node_such_that(t, pred)
180
+ return t if pred.test(t)
181
+
182
+ return nil if t.nil?
183
+
184
+ n = t.child_count
185
+ i = 0
186
+ while i < n
187
+ u = find_node_such_that(t.child(i), pred)
188
+ return u unless u.nil?
189
+
190
+ i += 1
191
+ end
192
+ nil
193
+ end
194
+ end
195
+ end