Method: Antlr4::Runtime::ParserATNSimulator#adaptive_predict

Defined in:
lib/antlr4/runtime/parser_atn_simulator.rb

#adaptive_predict(input, decision, outer_ctx) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/antlr4/runtime/parser_atn_simulator.rb', line 41

def adaptive_predict(input, decision, outer_ctx)
  if @debug || @debug_list_atn_decisions
    puts('adaptivePredict decision ' << decision.to_s << ' exec la(1)==' << lookahead_name(input) << ' line ' << input.lt(1).line.to_s << ':' << input.lt(1).char_position_in_line.to_s)
  end

  @_input = input
  @_start_index = input.index
  @_outer_context = outer_ctx
  dfa = @decision_to_dfa[decision]
  @_dfa = dfa

  m = input.mark
  index = @_start_index

  # Now we are certain to have a specific decision's DFA
  # But, do we still need an initial state?
  begin
    dfa.precedence_dfa? ? s0 = dfa.precedence_start_state(parser.precedence) : s0 = dfa.s0

    if s0.nil?
      outer_ctx = ParserRuleContext::EMPTY if outer_ctx.nil?
      if @debug || @debug_list_atn_decisions
        puts('predictATN decision ' << dfa.decision.to_s << ' exec la(1)==' << lookahead_name(input) << ', outer_ctx=' << outer_ctx.to_s_recog(@parser))
      end

      full_ctx = false
      s0_closure = compute_start_state(dfa.atn_start_state, ParserRuleContext::EMPTY, full_ctx)

      if dfa.precedence_dfa?
        dfa.s0.configs = s0_closure # not used for prediction but useful to know start configs anyway
        s0_closure = apply_precedence_filter(s0_closure)
        s0 = add_dfa_state(dfa, DFAState.new(s0_closure))
        dfa.precedence_start_state(@parser.precedence, s0)
      else
        s0 = add_dfa_state(dfa, DFAState.new(s0_closure))
        dfa.s0 = s0
      end
    end

    alt = exec_atn(dfa, s0, input, index, outer_ctx)
    if @debug
      puts('DFA after predictATN: ' << dfa.to_s2(@parser.get_vocabulary))
    end
    return alt
  ensure
    @merge_cache = nil # wack cache after each prediction
    @_dfa = nil
    input.seek(index)
    input.release(m)
  end
end