Class: Antlr4::Runtime::ParserRuleContext

Inherits:
RuleContext show all
Defined in:
lib/antlr4/runtime/parser_rule_context.rb

Direct Known Subclasses

RuleContextWithAltNum

Constant Summary collapse

EMPTY =
ParserRuleContext.new

Instance Attribute Summary collapse

Attributes inherited from RuleContext

#invoking_state, #parent

Instance Method Summary collapse

Methods inherited from RuleContext

#accept, #alt_number, #depth, #empty?, #payload, #rule_index, #set_alt_number, #text, #to_s, #to_s_list, #to_s_list_ctx, #to_s_recog, #to_s_recog_ctx, #to_string_tree, #to_string_tree_recog, #to_string_tree_rulenames

Constructor Details

#initialize(parent = nil, invoking_state_number = nil) ⇒ ParserRuleContext

Returns a new instance of ParserRuleContext



31
32
33
34
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 31

def initialize(parent = nil, invoking_state_number = nil)
  super(parent, invoking_state_number)
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children



6
7
8
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 6

def children
  @children
end

#exceptionObject

Returns the value of attribute exception



9
10
11
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 9

def exception
  @exception
end

#startObject

Returns the value of attribute start



7
8
9
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 7

def start
  @start
end

#stopObject

Returns the value of attribute stop



8
9
10
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 8

def stop
  @stop
end

Instance Method Details

#add_any_child(t) ⇒ Object



42
43
44
45
46
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 42

def add_any_child(t)
  @children = [] if @children.nil?
  @children << t
  t
end

#add_child_rule_invocation(rule_invocation) ⇒ Object



48
49
50
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 48

def add_child_rule_invocation(rule_invocation)
  add_any_child(rule_invocation)
end

#add_child_terminal_node(t) ⇒ Object



52
53
54
55
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 52

def add_child_terminal_node(t)
  t.parent = self
  add_any_child(t)
end

#add_error_node(error_node) ⇒ Object



57
58
59
60
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 57

def add_error_node(error_node)
  error_node.setParent(self)
  add_any_child(error_node)
end

#child(ctxType, i) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 70

def child(ctxType, i)
  return nil if @children.nil? || i < 0 || i >= @children.length

  j = -1 # what element have we found with ctx_type?
  k = 0
  while k < @children.length
    o = @children[k]
    unless o.class.name.end_with? ctxType
      k += 1
      next
    end

    j += 1
    return o if j == i
    k += 1
  end
  nil
end

#child_at(i) ⇒ Object



66
67
68
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 66

def child_at(i)
  !@children.nil? && i >= 0 && i < @children.length ? @children[i] : nil
end

#child_countObject



165
166
167
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 165

def child_count
  !@children.nil? ? @children.length : 0
end

#copy_from(ctx) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 11

def copy_from(ctx)
  @parent = ctx.parent
  @invoking_state = ctx.invoking_state

  @start = ctx.start
  @stop = ctx.stop

  # copy any error nodes to alt label node
  unless ctx.children.nil?
    @children = []
    # reset parent pointer for any error nodes
    i = 0
    while i < ctx.children.length
      child = ctx.children[i]
      add_child_terminal_node(child) if child.is_a? ErrorNode
      i += 1
    end
  end
end

#enter_rule(_listener) ⇒ Object



36
37
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 36

def enter_rule(_listener)
end

#exit_rule(_listener) ⇒ Object



39
40
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 39

def exit_rule(_listener)
end

#remove_last_childObject



62
63
64
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 62

def remove_last_child
  @children.delete_at(-1) unless @children.nil?
end

#rule_context(ctx_type, i) ⇒ Object



139
140
141
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 139

def rule_context(ctx_type, i)
  child(ctx_type, i)
end

#rule_contexts(ctxType) ⇒ Object



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

def rule_contexts(ctxType)
  return [] if @children.nil?

  contexts = nil
  i = 0
  while i < @children.length
    o = @children[i]
    unless o.class.name.end_with? ctxType
      i += 1
      next
    end

    contexts = [] if contexts.nil?
    contexts << o
    i += 1
  end

  return [] if contexts.nil?

  contexts
end

#source_intervalObject



169
170
171
172
173
174
175
176
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 169

def source_interval
  return Interval.invalid if @start.nil?
  if @stop.nil? || @stop.token_index < @start.token_index
    return Interval.of(@start.token_index, @start.token_index - 1) # empty
  end

  Interval.of(@start.token_index, @stop.token_index)
end

#to_info_string(recognizer) ⇒ Object



178
179
180
181
182
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 178

def to_info_string(recognizer)
  rules = recognizer.rule_invocation_stack2(self)
  rules.reverse!
  'ParserRuleContext' + rules + '' + 'start=' + @start + ', stop=' + @stop + 'end'
end

#token(ttype, i) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 89

def token(ttype, i)
  return nil if @children.nil? || i < 0 || i >= @children.length

  j = -1 # what token with ttype have we found?
  k = 0
  while k < @children.length
    o = @children[k]
    unless o.is_a? TerminalNode
      k += 1
      next
    end

    tnode = o
    symbol = tnode.symbol
    if !symbol.nil? && symbol.type == ttype
      j += 1
      return tnode if j == i
    end
    k += 1
  end

  nil
end

#tokens(ttype) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/antlr4/runtime/parser_rule_context.rb', line 113

def tokens(ttype)
  return [] if @children.nil?

  tokens = nil
  i = 0
  while i < @children.length
    o = @children[i]
    unless o.is_a? TerminalNode
      i += 1
      next
    end

    tnode = o
    symbol = tnode.symbol
    if symbol.type == ttype
      tokens = [] if tokens.nil?
      tokens << tnode
    end
    i += 1
  end

  return [] if tokens.nil?

  tokens
end