Class: Antlr4::Runtime::ProfilingATNSimulator

Inherits:
ParserATNSimulator show all
Defined in:
lib/antlr4/runtime/profiling_atn_simulator.rb

Constant Summary

Constants inherited from ParserATNSimulator

Antlr4::Runtime::ParserATNSimulator::TURN_OFF_LR_LOOP_ENTRY_BRANCH_OPT

Constants inherited from ATNSimulator

ATNSimulator::SERIALIZED_UUID, ATNSimulator::SERIALIZED_VERSION

Instance Attribute Summary

Attributes inherited from ParserATNSimulator

#debug, #debug_list_atn_decisions, #dfa_debug, #retry_debug

Attributes inherited from ATNSimulator

#atn, #shared_context_cache

Instance Method Summary collapse

Methods inherited from ParserATNSimulator

#action_transition, #add_dfa_edge, #add_dfa_state, #alt_that_finished_decision_entry_rule, #apply_precedence_filter, #can_drop_loop_entry_edge_in_left_recursive_rule?, #clear_dfa, #closure, #closure_, #closure_checking_stop_state, #compute_start_state, #conflicting_alts, #conflicting_alts_or_unique_alt, #dump_dead_end_configs, #epsilon_target, #eval_semantic_context1, #eval_semantic_context2, #exec_atn, #exec_atn_with_full_context, get_safe_env, #lookahead_name, #precedence_transition, #pred_transition, #predicate_dfa_state, #predicate_predictions, #preds_for_ambig_alts, #reachable_target, #remove_all_configs_not_in_rule_stop_state, #rule_name, #rule_transition, #split_according_to_semantic_validity, #syn_valid_or_sem_invalid_alt_that_finished_decision_entry_rule, #token_name, #unique_alt

Methods inherited from ATNSimulator

#cached_context, check_condition, #clear_dfa, deserialize, #edge_factory, #state_factory, to_int, to_int32, to_long, to_uuid

Constructor Details

#initialize(parser) ⇒ ProfilingATNSimulator

Returns a new instance of ProfilingATNSimulator



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 3

def initialize(parser)
  super(parser, parser._interp.atn, parser._interp.decision_to_dfa, parser._interp.shared_context_cache)

  @num_decisions = @atn.decision_to_state.size
  @_sll_stop_index = 0
  @_ll_stop_index = 0
  @current_decision = 0
  @current_state = nil
  @conflicting_alt_resolved_by_sll = 0

  @decisions = Array.new(@numDecisions)
  i = 0
  while i < @num_decisions
    @decisions[i] = DecisionInfo.new(i)
    i += 1
  end
end

Instance Method Details

#adaptive_predict(input, decision, outer_ctx) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 21

def adaptive_predict(input, decision, outer_ctx)
  @_sll_stop_index = -1
  @_ll_stop_index = -1
  @current_decision = decision
  start = Time.now # expensive but useful info
  alt = super.adaptive_predict(input, decision, outer_ctx)
  stop = Time.now
  @decisions[decision].timeInPrediction += (stop - start)
  @decisions[decision].invocations += 1

  _s_ll_k = @_sll_stop_index - @_start_index + 1
  @decisions[decision].SLL_TotalLook += _s_ll_k
  @decisions[decision].SLL_MinLook = @decisions[decision].SLL_MinLook == 0 ? _s_ll_k : Math.min(@decisions[decision].SLL_MinLook, _s_ll_k)
  if _s_ll_k > @decisions[decision].SLL_MaxLook
    @decisions[decision].SLL_MaxLook = _s_ll_k
    @decisions[decision].SLL_MaxLookEvent = LookaheadEventInfo.new(decision, nil, alt, input, @_start_index, @_sll_stop_index, false)
  end

  if @_ll_stop_index >= 0
    _ll_k = @_ll_stop_index - @_start_index + 1
    @decisions[decision].LL_TotalLook += _ll_k
    @decisions[decision].LL_MinLook = @decisions[decision].LL_MinLook == 0 ? _ll_k : Math.min(@decisions[decision].LL_MinLook, _ll_k)
    if _ll_k > @decisions[decision].LL_MaxLook
      @decisions[decision].LL_MaxLook = _ll_k
      @decisions[decision].LL_MaxLookEvent = LookaheadEventInfo.new(decision, nil, alt, input, @_start_index, @_ll_stop_index, true)
    end
  end

  alt
ensure
  @current_decision = -1
end

#compute_reach_set(closure, t, full_ctx) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 76

def compute_reach_set(closure, t, full_ctx)
  if full_ctx
    # this method is called after each time the input position advances
    # during full context prediction
    @_ll_stop_index = @_input.index
  end

  reach_configs = super.compute_reach_set(closure, t, full_ctx)
  if full_ctx
    @decisions[@current_decision].LL_ATNTransitions += 1 # count computation even if error
    if !reach_configs.nil?
    else # no reach on current lookahead symbol. ERROR.
      # TODO: does not handle delayed errors per getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule()
      @decisions[@current_decision].errors.add(ErrorInfo.new(@current_decision, closure, @_input, @_start_index, @_ll_stop_index, true))
    end
  else
    @decisions[@current_decision].SLL_ATNTransitions += 1
    if !reach_configs.nil?
    else # no reach on current lookahead symbol. ERROR.
      @decisions[@current_decision].errors.add(ErrorInfo.new(@current_decision, closure, @_input, @_start_index, @_sll_stop_index, false))
    end
  end

  reach_configs
end

#compute_target_state(dfa, prev_d, t) ⇒ Object



70
71
72
73
74
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 70

def compute_target_state(dfa, prev_d, t)
  state = super.compute_target_state(dfa, prev_d, t)
  @current_state = state
  state
end

#eval_semantic_context(pred, parser_call_stack, alt, full_ctx) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 102

def eval_semantic_context(pred, parser_call_stack, alt, full_ctx)
  result = super.eval_semantic_context(pred, parser_call_stack, alt, full_ctx)
  unless pred.is_a? SemanticContext.PrecedencePredicate
    full_context = (@_ll_stop_index >= 0)
    stop_index = full_context ? @_ll_stop_index : @_sll_stop_index
    @decisions[@current_decision].predicate_evals.add(PredicateEvalInfo.new(@current_decision, @_input, @_start_index, stop_index, pred, result, alt, full_ctx))
  end

  result
end

#existing_target_state(prev_d, t) ⇒ Object

this method is called after each time the input position advances



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 54

def existing_target_state(prev_d, t) # this method is called after each time the input position advances
  # during SLL prediction
  @_sll_stop_index = @_input.index

  existing_tgt_state = super.existing_target_state(prev_d, t)
  unless existing_tgt_state.nil?
    @decisions[@current_decision].SLL_DFATransitions += 1 # count only if we transition over a DFA state
    if existing_tgt_state == @@error
      @decisions[@current_decision].errors.add(ErrorInfo.new(@current_decision, prev_d.configs, @_input, @_start_index, @_sll_stop_index, false))
    end
  end

  @current_state = existing_tgt_state
  existing_tgt_state
end

#report_ambiguity(dfa, _d, start_index, stop_index, exact, ambig_alts, configs) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 130

def report_ambiguity(dfa, _d, start_index, stop_index, exact, ambig_alts, configs)
  prediction = if !ambig_alts.nil?
                 ambig_alts.next_set_bit(0)
               else
                 configs.alts.next_set_bit(0)
               end

  if configs.full_ctx && prediction != @conflicting_alt_resolved_by_sll
    # Even though this is an ambiguity we are reporting, we can
    # still detect some context sensitivities.  Both SLL and LL
    # are showing a conflict, hence an ambiguity, but if they resolve
    # to different minimum alternatives we have also identified a
    # context sensitivity.
    @decisions[@current_decision].context_sensitivities.add(ContextSensitivityInfo.new(@current_decision, configs, @_input, start_index, stop_index))
  end
  @decisions[@current_decision].ambiguities.add(AmbiguityInfo.new(@current_decision, configs, ambig_alts, @_input, start_index, stop_index, configs.full_ctx))
  super.report_ambiguity(dfa, _d, start_index, stop_index, exact, ambig_alts, configs)
end

#report_attempting_full_context(dfa, conflicting_alts, configs, start_index, stop_index) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 113

def report_attempting_full_context(dfa, conflicting_alts, configs, start_index, stop_index)
  if !conflicting_alts.nil?
    @conflicting_alt_resolved_by_sll = conflicting_alts.next_set_bit(0)
  else
    @conflicting_alt_resolved_by_sll = configs.alts.next_set_bit(0)
  end
  @decisions[@current_decision].LL_Fallback += 1
  super.report_attempting_full_context(dfa, conflicting_alts, configs, start_index, stop_index)
end

#report_context_sensitivity(dfa, prediction, configs, start_index, stop_index) ⇒ Object



123
124
125
126
127
128
# File 'lib/antlr4/runtime/profiling_atn_simulator.rb', line 123

def report_context_sensitivity(dfa, prediction, configs, start_index, stop_index)
  if prediction != @conflicting_alt_resolved_by_sll
    @decisions[@current_decision].context_sensitivities.add(ContextSensitivityInfo.new(@current_decision, configs, @_input, start_index, stop_index))
  end
  super.report_context_sensitivity(dfa, prediction, configs, start_index, stop_index)
end