Class: Antlr4::Runtime::Trees

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/runtime/trees.rb

Class Method Summary collapse

Class Method Details

._find_all_nodes(t, index, find_tokens, nodes) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/antlr4/runtime/trees.rb', line 111

def self._find_all_nodes(t, index, find_tokens, nodes)
  # check this node (the root) first
  if find_tokens && t.is_a?(TerminalNode)
    tnode = t
    nodes.add(t) if tnode.getSymbol.type == index
  elsif !find_tokens && t.is_a(ParserRuleContext)
    ctx = t
    nodes.push(t) if ctx.rule_index == index
  end
  # check children
  i = 0
  while i < t.child_count
    _find_all_nodes(t.child(i), index, find_tokens, nodes)
    i += 1
  end
end

.ancestor_of?(t, u) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/antlr4/runtime/trees.rb', line 85

def self.ancestor_of?(t, u)
  return false if t.nil? || u.nil? || t.get_parent.nil?

  p = u.get_parent
  until p.nil?
    return true if t == p

    p = p.get_parent
  end
  false
end

.ancestors(t) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/antlr4/runtime/trees.rb', line 73

def self.ancestors(t)
  return [] if t.get_parent.nil?

  ancestors = []
  t = t.get_parent
  until t.nil?
    ancestors.unshift(t) # insert at start
    t = t.get_parent
  end
  ancestors
end

.children(t) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/antlr4/runtime/trees.rb', line 62

def self.children(t)
  kids = []
  i = 0
  while i < t.child_count
    kids << t.child(i)
    i += 1
  end

  kids
end

.descendants(t) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/antlr4/runtime/trees.rb', line 128

def self.descendants(t)
  nodes = []
  nodes.push(t)

  n = t.child_count
  i = 0
  while i < n
    nodes.add_all(descendants(t.child(i)))
    i += 1
  end
  nodes
end

.find_all_nodes(t, index, find_tokens) ⇒ Object



105
106
107
108
109
# File 'lib/antlr4/runtime/trees.rb', line 105

def self.find_all_nodes(t, index, find_tokens)
  nodes = []
  _find_all_nodes(t, index, find_tokens, nodes)
  nodes
end

.find_all_rule_nodes(t, rule_index) ⇒ Object



101
102
103
# File 'lib/antlr4/runtime/trees.rb', line 101

def self.find_all_rule_nodes(t, rule_index)
  find_all_nodes(t, rule_index, false)
end

.find_all_token_nodes(t, ttype) ⇒ Object



97
98
99
# File 'lib/antlr4/runtime/trees.rb', line 97

def self.find_all_token_nodes(t, ttype)
  find_all_nodes(t, ttype, true)
end

.find_node_such_that(t, pred) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/antlr4/runtime/trees.rb', line 179

def self.find_node_such_that(t, pred)
  return t if pred.test(t)

  return nil if t.nil?

  n = t.child_count
  i = 0
  while i < n
    u = find_node_such_that(t.child(i), pred)
    return u unless u.nil?

    i += 1
  end
  nil
end

.node_text_recog(t, recog) ⇒ Object



28
29
30
31
32
# File 'lib/antlr4/runtime/trees.rb', line 28

def self.node_text_recog(t, recog)
  rule_names = !recog.nil? ? recog.rule_names : nil
  rule_names_list = !rule_names.nil? ? rule_names : nil
  getNodeText(t, rule_names_list)
end

.node_text_rulenames(t, rule_names) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/antlr4/runtime/trees.rb', line 34

def self.node_text_rulenames(t, rule_names)
  unless rule_names.nil?
    if t.is_a? RuleContext
      rule_index = t.rule_context.rule_index
      rule_name = rule_names[rule_index]
      alt_number = t.alt_number
      if alt_number != ATN::INVALID_ALT_NUMBER
        return rule_name + ':' + alt_number
      end

      return rule_name
    elsif t.is_a? ErrorNode
      return t.to_s
    elsif t.is_a? TerminalNode
      symbol = t.getSymbol
      unless symbol.nil?
        s = symbol.text
        return s
      end
    end
  end
  # no recog for rule names
  payload = t.payload
  return payload.text if payload.is_a Token

  t.payload.to_s
end

.root_of_subtree_enclosing_region(t, start_token_index, stop_token_index) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/antlr4/runtime/trees.rb', line 141

def self.root_of_subtree_enclosing_region(t, start_token_index, stop_token_index)
  n = t.child_count
  i = 0
  while i < n
    ParseTree child = t.child(i)
    ParserRuleContext r = root_of_subtree_enclosing_region(child, start_token_index, stop_token_index)
    return r unless r.nil?

    i += 1
  end
  if t.is_a? ParserRuleContext
    r = t
    if start_token_index >= r.getStart.token_index && # is range fully contained in t?
        (r.getStop.nil? || stop_token_index <= r.getStop.token_index)

      # note: r.getStop()==nil likely implies that we bailed out of parser and there's nothing to the right
      return r
    end
  end
  nil
end

.strip_children_out_of_range(t, root, start_index, stop_index) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/antlr4/runtime/trees.rb', line 163

def self.strip_children_out_of_range(t, root, start_index, stop_index)
  return if t.nil?

  i = 0
  while i < t.child_count
    child = t.child(i)
    range = child.source_interval
    next unless child.is_a? ParserRuleContext && (range.b < start_index || range.a > stop_index)

    if ancestor_of?(child, root) # replace only if subtree doesn't have displayed root
      abbrev = CommonToken.new(Token::INVALID_TYPE)
      t.children.set(i, TerminalNodeImpl.new(abbrev))
    end
  end
end

.to_s_tree_recog(t, recog = nil) ⇒ Object



3
4
5
6
7
# File 'lib/antlr4/runtime/trees.rb', line 3

def self.to_s_tree_recog(t, recog = nil)
  rule_names = !recog.nil? ? recog.rule_names : nil
  rule_names_list = !rule_names.nil? ? rule_names : nil
  to_s_tree_rulenames(t, rule_names_list)
end

.to_s_tree_rulenames(t, rule_names) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/antlr4/runtime/trees.rb', line 9

def self.to_s_tree_rulenames(t, rule_names)
  s = Utils.escape_whitespace(getNodeText(t, rule_names), false)
  return s if t.child_count == 0

  buf = ''
  buf << '('
  s = Utils.escape_whitespace(getNodeText(t, rule_names), false)
  buf << s
  buf << ' '
  i = 0
  while i < t.child_count
    buf << ' ' if i > 0
    buf << to_string_tree(t.child(i), rule_names)
    i += 1
  end
  buf << ')'
  buf
end