class Antlr4::Runtime::Parser

Attributes

_ctx[RW]
_input[R]

Public Class Methods

new(input) click to toggle source
Calls superclass method
# File lib/antlr4/runtime/parser.rb, line 48
def initialize(input)
  super()
  @_err_handler = DefaultErrorStrategy.new
  @_input = nil
  @_precedence_stack = []
  @_precedence_stack.push(0)
  @_ctx = nil
  @_build_parse_trees = true
  @_tracer = nil
  @_parse_listeners = nil
  @_syntax_errors = nil
  @matched_eof = nil
  set_token_stream(input)
end

Public Instance Methods

add_context_to_parse_tree() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 267
def add_context_to_parse_tree
  parent = @_ctx.parent
  # add current context to parent if we have a parent
  parent.add_child_rule_invocation(@_ctx) unless parent.nil?
end
add_parse_listener(listener) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 132
def add_parse_listener(listener)
  raise nilPointerException, 'listener' if listener.nil?

  @_parse_listeners = [] if @_parse_listeners.nil?

  @_parse_listeners << listener
end
compile_parse_tree_pattern1(pattern, patter_rule_index) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 195
def compile_parse_tree_pattern1(pattern, patter_rule_index)
  unless getTokenStream.nil?
    token_source = getTokenStream.token_source
    if token_source.is_a? Lexer
      lexer = token_source
      return compile_parse_tree_pattern2(pattern, patter_rule_index, lexer)
    end
  end
  raise UnsupportedOperationException, "Parser can't discover a lexer to use"
end
compile_parse_tree_pattern2(pattern, patternRuleIndex, lexer) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 206
def compile_parse_tree_pattern2(pattern, patternRuleIndex, lexer)
  m = ParseTreePatternMatcher.new(lexer, self)
  m.compile(pattern, patternRuleIndex)
end
consume() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 234
def consume
  o = current_token
  @_input.consume if o.type != EOF
  has_listener = !@_parse_listeners.nil? && !@_parse_listeners.empty?
  if @_build_parse_trees || has_listener
    if @_err_handler.in_error_recovery_mode(self)
      node = @_ctx.add_error_node(create_error_node(@_ctx, o))
      unless @_parse_listeners.nil?
        @_parse_listeners.each do |listener|
          listener.visit_error_node(node)
        end
      end
    else
      node = @_ctx.add_child_terminal_node(create_terminal_node(@_ctx, o))
      unless @_parse_listeners.nil?
        @_parse_listeners.each do |listener|
          listener.visit_terminal(node, @_ctx)
        end
      end
    end
  end

  o
end
create_error_node(_parent, t) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 263
def create_error_node(_parent, t)
  ErrorNodeImpl.new(t)
end
create_terminal_node(_parent, t) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 259
def create_terminal_node(_parent, t)
  TerminalNodeImpl.new(t)
end
current_token() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 217
def current_token
  @_input.lt(1)
end
dfa_strings() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 448
def dfa_strings
  s = []
  d = 0
  while d < @_interp.decision_to_dfa.length
    dfa = @_interp.decision_to_dfa[d]
    s.push(dfa.to_s(get_vocabulary))
    d += 1
  end
  s
end
dump_dfa() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 459
def dump_dfa
  seen_one = false
  d = 0
  while d < @_interp.decision_to_dfa.length
    dfa = @_interp.decision_to_dfa[d]
    unless dfa.states.empty?
      puts if seen_one
      puts('Decision ' << dfa.decision << ':')
      puts(dfa.to_s(get_vocabulary))
      seen_one = true
    end
    d += 1
  end
end
enter_outer_alt(local_ctx, alt_num) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 295
def enter_outer_alt(local_ctx, alt_num)
  local_ctx.set_alt_number(alt_num)
  # if we have new local_ctx, make sure we replace existing ctx
  # that is previous child of parse tree
  if @_build_parse_trees && @_ctx != local_ctx
    parent = @_ctx.parent
    unless parent.nil?
      parent.remove_last_child
      parent.addChild(local_ctx)
    end
  end
  @_ctx = local_ctx
end
enter_recursion_rule(local_ctx, state, _rule_index, precedence) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 315
def enter_recursion_rule(local_ctx, state, _rule_index, precedence)
  setState(state)
  @_precedence_stack.push(precedence)
  @_ctx = local_ctx
  @_ctx.start = @_input.lt(1)
  unless @_parse_listeners.nil?
    trigger_enter_rule_event # simulates rule entry for left-recursive rules
  end
end
enter_rule(local_ctx, state, _rule_index) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 273
def enter_rule(local_ctx, state, _rule_index)
  @_state_number = state
  @_ctx = local_ctx
  @_ctx.start = @_input.lt(1)
  add_context_to_parse_tree if @_build_parse_trees
  trigger_enter_rule_event unless @_parse_listeners.nil?
end
exit_rule() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 281
def exit_rule
  if @matched_eof
    # if we have matched EOF, it cannot consume past EOF so we use lt(1) here
    @_ctx.stop = @_input.lt(1) # lt(1) will be end of file
  else
    @_ctx.stop = @_input.lt(-1) # stop node is what we just matched
  end

  # trigger event on @_ctx, before it reverts to parent
  trigger_exit_rule_event unless @_parse_listeners.nil?
  @_state_number = @_ctx.invoking_state
  @_ctx = @_ctx.parent
end
expected_token?(symbol) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 383
def expected_token?(symbol)
  atn = @_interp.atn
  ctx = @_ctx
  s = atn.states.get(getState)
  following = atn.next_tokens(s)
  return true if following.include?(symbol)

  return false unless following.contains(Token::EPSILON)

  while !ctx.nil? && ctx.invoking_state >= 0 && following.include?(Token::EPSILON)
    invoking_state = atn.states.get(ctx.invoking_state)
    rt = invoking_state.transition(0)
    following = atn.next_tokens(rt.follow_state)
    return true if following.include?(symbol)

    ctx = ctx.parent
  end

  return true if following.include?(Token::EPSILON) && symbol == Token::EOF

  false
end
expected_tokens() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 410
def expected_tokens
  atn = @_interp.atn
  atn.expected_tokens(@_state_number, @_ctx)
end
expected_tokens_within_current_rule() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 415
def expected_tokens_within_current_rule
  atn = @_interp.atn
  s = atn.states.get(getState)
  atn.next_tokens(s)
end
get_atn_with_bypass_alts() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 178
def get_atn_with_bypass_alts
  serialized_atn = get_serialized_atn
  if serialized_atn.nil?
    raise UnsupportedOperationException, 'The current parser does not support an ATN with bypass alternatives.'
  end

  result = @@bypass_alts_atn_cache.get(serialized_atn)
  if result.nil?
    deserialization_options = ATNDeserializationOptions.new
    deserialization_options.generate_rule_bypass_transitions(true)
    result = ATNDeserializer.new(deserialization_options).deserialize(serialized_atn)
    @@bypass_alts_atn_cache.put(serialized_atn, result)
  end

  result
end
get_parse_listeners() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 125
def get_parse_listeners
  listeners = @_parse_listeners
  return [] if listeners.nil?

  listeners
end
get_trim_parse_tree() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 121
def get_trim_parse_tree
  get_parse_listeners.contains(TrimToSizeListener.INSTANCE)
end
in_context?(_context) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 379
def in_context?(_context)
  false
end
invoking_context(rule_index) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 365
def invoking_context(rule_index)
  p = @_ctx
  until p.nil?
    return p if p.rule_index == rule_index

    p = p.parent
  end
  nil
end
match(ttype) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 76
def match(ttype)
  t = current_token
  if t.type == ttype
    @matched_eof = true if ttype == Token::EOF
    @_err_handler.report_match(self)
    consume
  else
    t = @_err_handler.recover_in_line(self)
    if @_build_parse_trees && t.index == -1
      # we must have conjured up a new token during single token insertion
      # if it's not the current symbol
      @_ctx.add_error_node(create_error_node(@_ctx, t))
    end
  end

  t
end
match_wildcard() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 94
def match_wildcard
  t = current_token
  if t.type > 0
    @_err_handler.report_match(this)
    consume
  else
    t = @_err_handler.recover_in_line(this)
    if @_build_parse_trees && t.token_index == -1
      # we must have conjured up a new token during single token insertion
      # if it's not the current symbol
      @_ctx.add_error_node(create_error_node(@_ctx, t))
    end
  end

  t
end
matched_eof?() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 406
def matched_eof?
  @matched_eof
end
notify_error_listeners(offending_token, msg, e) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 225
def notify_error_listeners(offending_token, msg, e)
  @_syntax_errors += 1
  line = offending_token.line
  char_position_in_line = offending_token.char_position_in_line

  listener = error_listener_dispatch
  listener.syntax_error(self, offending_token, line, char_position_in_line, msg, e)
end
notify_error_listeners_simple(msg) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 221
def notify_error_listeners_simple(msg)
  notify_error_listeners(current_token, msg, nil)
end
parse_info() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 478
def parse_info
  interp = @_interp
  return ParseInfo.new(interp) if interp.is_a? ProfilingATNSimulator

  nil
end
precedence() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 309
def precedence
  return -1 if @_precedence_stack.empty?

  @_precedence_stack.peek
end
precpred(_localctx, precedence) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 375
def precpred(_localctx, precedence)
  precedence >= @_precedence_stack.peek
end
push_new_recursion_context(local_ctx, state, _rule_index) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 325
def push_new_recursion_context(local_ctx, state, _rule_index)
  previous = @_ctx
  previous.parent = local_ctx
  previous.invoking_state = state
  previous.stop = @_input.lt(-1)

  @_ctx = local_ctx
  @_ctx.start = previous.start
  @_ctx.addChild(previous) if @_build_parse_trees

  unless @_parse_listeners.nil?
    trigger_enter_rule_event # simulates rule entry for left-recursive rules
  end
end
remove_parse_listener(listener) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 140
def remove_parse_listener(listener)
  unless @_parse_listeners.nil?
    if @_parse_listeners.remove(listener)
      @_parse_listeners = nil if @_parse_listeners.empty?
    end
  end
end
remove_parse_listeners() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 148
def remove_parse_listeners
  @_parse_listeners = nil
end
reset() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 63
def reset
  @_input.seek(0) unless @_input.nil?
  @_err_handler.reset(self)
  @_ctx = nil
  @_syntax_errors = 0
  @matched_eof = false
  set_trace(false)
  @_precedence_stack.clear
  @_precedence_stack.push(0)
  interpreter = @_interp
  interpreter.reset unless interpreter.nil?
end
rule_index(rule_name) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 421
def rule_index(rule_name)
  index = get_rule_index_map.get(rule_name)
  return index unless index.nil?

  -1
end
rule_invocation_stack1() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 428
def rule_invocation_stack1
  rule_invocation_stack2(@_ctx)
end
rule_invocation_stack2(p) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 432
def rule_invocation_stack2(p)
  rule_names = rule_names
  stack = []
  until p.nil?
    # compute what follows who invoked us
    rule_index = p.rule_index
    if rule_index < 0
      stack.push('n/a')
    else
      stack.push(rule_names[rule_index])
    end
    p = p.parent
  end
  stack
end
set_profile(profile) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 485
def set_profile(profile)
  interp = @_interp
  save_mode = interp.getPredictionMode
  if profile
    unless interp.is_a? ProfilingATNSimulator
      @_interp = ProfilingATNSimulator.new(self)
    end
  elsif @_interp.is_a? ProfilingATNSimulator
    sim = ParserATNSimulator.new(self, atn, interp.decision_to_dfa, interp.shared_context_cache)
    @_interp = sim
  end
  @_interp.setPredictionMode(save_mode)
end
set_token_stream(input) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 211
def set_token_stream(input)
  @_input = nil
  reset
  @_input = input
end
set_trace(trace) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 499
def set_trace(trace)
  if !trace
    remove_parse_listener(@_tracer)
    @_tracer = nil
  else
    if !@_tracer.nil?
      remove_parse_listener(@_tracer)
    else
      @_tracer = new TraceListener
    end
    add_parse_listener(@_tracer)
  end
end
set_trim_parse_tree(trimParseTrees) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 111
def set_trim_parse_tree(trimParseTrees)
  if trimParseTrees
    return if get_trim_parse_tree

    add_parse_listener(TrimToSizeListener.INSTANCE)
  else
    remove_parse_listener(TrimToSizeListener.INSTANCE)
  end
end
source_name() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 474
def source_name
  @_input.get_source_name
end
token_factory() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 170
def token_factory
  @_input.token_source.token_factory
end
token_factory=(factory) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 174
def token_factory=(factory)
  @_input.token_source.token_factory = factory
end
trace?() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 513
def trace?
  !@_tracer.nil?
end
trigger_enter_rule_event() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 152
def trigger_enter_rule_event
  @_parse_listeners.each do |listener|
    listener.enter_every_rule(@_ctx)
    @_ctx.enter_rule(listener)
  end
end
trigger_exit_rule_event() click to toggle source
# File lib/antlr4/runtime/parser.rb, line 159
def trigger_exit_rule_event # reverse order walk of listeners
  i = @_parse_listeners.length - 1
  while i >= 0

    listener = @_parse_listeners[i]
    @_ctx.exit_rule(listener)
    listener.exit_every_rule(@_ctx)
    i -= 1
  end
end
unroll_recursion_contexts(_parent_ctx) click to toggle source
# File lib/antlr4/runtime/parser.rb, line 340
def unroll_recursion_contexts(_parent_ctx)
  @_precedence_stack.pop
  @_ctx.stop = @_input.lt(-1)
  retctx = @_ctx # save current ctx (return value)

  # unroll so @_ctx is as it was before call to recursive method
  if !@_parse_listeners.nil?
    while @_ctx != _parent_ctx
      trigger_exit_rule_event
      @_ctx = @_ctx.parent
    end

  else
    _ctx = _parent_ctx
  end

  # hook into tree
  retctx.parent = _parent_ctx

  if @_build_parse_trees && !_parent_ctx.nil?
    # add return ctx into invoking rule's tree
    _parent_ctx.addChild(retctx)
  end
end