Class: Antlr4ruby::ParserRuleContext

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

Constant Summary collapse

EMPTY =
ParserRuleContext.new(nil, -1)

Instance Attribute Summary collapse

Attributes inherited from RuleContext

#invoking_state, #parent

Instance Method Summary collapse

Methods inherited from RuleContext

#accept, #depth, #get_alt_number, #get_payload, #get_rule_index, #get_text, #is_empty?, #set_alt_number, #set_parent, #to_s, #to_string_tree

Constructor Details

#initialize(parent = nil, invoking_state = -1)) ⇒ ParserRuleContext

Returns a new instance of ParserRuleContext.



11
12
13
# File 'lib/antlr4ruby/parser_rule_context.rb', line 11

def initialize(parent = nil, invoking_state = -1)
  super(parent, invoking_state)
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#exceptionObject

Returns the value of attribute exception.



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

def exception
  @exception
end

#startObject

Returns the value of attribute start.



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

def start
  @start
end

#stopObject

Returns the value of attribute stop.



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

def stop
  @stop
end

Instance Method Details

#add_any_child(t) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/antlr4ruby/parser_rule_context.rb', line 48

def add_any_child(t)
  unless @children
    @children = []
  end
  @children.push(t)
  t
end

#add_child(rule_invocation) ⇒ Object



36
37
38
39
40
41
# File 'lib/antlr4ruby/parser_rule_context.rb', line 36

def add_child(rule_invocation)
  if rule_invocation.instance_of?(TerminalNode)
    rule_invocation.set_parent(self)
  end
  add_any_child(rule_invocation)
end

#add_error_node(error_node) ⇒ Object



43
44
45
46
# File 'lib/antlr4ruby/parser_rule_context.rb', line 43

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

#copy_from(ctx) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/antlr4ruby/parser_rule_context.rb', line 15

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

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

  if ctx.children
    self.children = []
    ctx.children.each do |child|
      if child.instance_of?(ErrorNode)
        add_child(child)
      end
    end
  end
end

#enter_rule(listener) ⇒ Object



32
# File 'lib/antlr4ruby/parser_rule_context.rb', line 32

def enter_rule(listener) end

#exit_rule(listener) ⇒ Object



34
# File 'lib/antlr4ruby/parser_rule_context.rb', line 34

def exit_rule(listener) end

#get_child(i, context_type) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/antlr4ruby/parser_rule_context.rb', line 69

def get_child(i, context_type)
  @children[i] unless context_type
  arr = []
  children.each do |child|
    arr.push(child) if child.instance_of?(context_type)
  end
  arr[i]
end

#get_child_countObject



132
133
134
# File 'lib/antlr4ruby/parser_rule_context.rb', line 132

def get_child_count
  @children.length || 0
end

#get_parentObject



63
64
65
66
# File 'lib/antlr4ruby/parser_rule_context.rb', line 63

def get_parent
  # 这里会有一个警告,好像没什么办法强制类型转换

  super
end

#get_rule_context(context_type, i) ⇒ Object



78
79
80
# File 'lib/antlr4ruby/parser_rule_context.rb', line 78

def get_rule_context(context_type, i)
  get_child(i, context_type)
end

#get_rule_contexts(context_type) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/antlr4ruby/parser_rule_context.rb', line 82

def get_rule_contexts(context_type)
  result = []
  return result unless children

  children.each do |child|
    result.push(child) if child.instance_of?(context_type)
  end
  result
end

#get_source_intervalObject



137
138
139
140
141
142
143
144
145
# File 'lib/antlr4ruby/parser_rule_context.rb', line 137

def get_source_interval
  unless @start
    return (-1)..(-2)
  end
  if !@stop || stop.get_token_index < start.get_token_index
    return start.get_token_index...stop.get_token_index
  end
  start.get_token_index..stop.get_token_index
end

#get_startObject



147
148
149
# File 'lib/antlr4ruby/parser_rule_context.rb', line 147

def get_start
  @start
end

#get_stopObject



151
152
153
# File 'lib/antlr4ruby/parser_rule_context.rb', line 151

def get_stop
  @stop
end

#get_token(token_type, i) ⇒ Object



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

def get_token(token_type, i)
  if !@children || i < 0 || i >= @children.length
    return nil
  end

  j = -1
  @children.each do |o|
    if o.instance_of?(TerminalNode)
      node = o
      symbol = node.get_symbol
      if symbol.get_type == token_type
        j += 1
        if j == i
          return node
        end
      end
    end
  end

  nil
end

#get_tokens(token_type) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/antlr4ruby/parser_rule_context.rb', line 114

def get_tokens(token_type)
  unless children
    return []
  end
  tokens = []
  children.each do |child|
    if child.instance_of?(TerminalNode)
      node = child
      symbol = node.get_symbol
      if symbol.get_type == token_type
        tokens.push(node)
      end
    end
  end
  tokens
end

#remove_last_childObject



56
57
58
59
60
# File 'lib/antlr4ruby/parser_rule_context.rb', line 56

def remove_last_child
  if @children
    @children.pop
  end
end

#to_info_string(recognizer) ⇒ Object



155
156
157
158
159
# File 'lib/antlr4ruby/parser_rule_context.rb', line 155

def to_info_string(recognizer)
  rules = recognizer.get_rule_invocation_stack(self)
  rules.reverse!
  "parser_rule_context#{rules}{start=#{start}, stop=#{stop}}"
end