Class: Antlr4::Runtime::LL1Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/runtime/ll1_analyzer.rb

Constant Summary collapse

@@hit_pred =
Token::INVALID_TYPE

Instance Method Summary collapse

Constructor Details

#initialize(atn) ⇒ LL1Analyzer

Returns a new instance of LL1Analyzer



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

def initialize(atn)
  @atn = atn
end

Instance Method Details

#_look(s, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF) ⇒ Object



36
37
38
39
40
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/antlr4/runtime/ll1_analyzer.rb', line 36

def _look(s, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
  #    System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx)
  c = ATNConfig.new
  c.atn_config1(s, 0, ctx)
  added = false
  unless lookBusy.include? c
    lookBusy.add(c)
    added = true
  end
  return unless added

  if s == stopState
    if ctx.nil?
      look.add(Token::EPSILON)
      return
    elsif ctx.empty? && addEOF
      look.add(Token::EOF)
      return
    end
  end

  if s.is_a? RuleStopState
    if ctx.nil?
      look.add(Token::EPSILON)
      return
    elsif ctx.empty? && addEOF
      look.add(Token::EOF)
      return
    end

    if ctx != EmptyPredictionContext::EMPTY
      # run thru all possible stack tops in ctx
      removed = calledRuleStack.get(s.rule_index)
      begin
        calledRuleStack.clear(s.rule_index)
        i = 0
        while i < ctx.size
          return_state = atn.states.get(ctx.get_return_state(i))

          _look(return_state, stopState, ctx.get_parent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
          i += 1
        end
      ensure
        calledRuleStack.set(s.rule_index) if removed
      end
      return
    end
  end

  n = s.number_of_transitions
  i = 0
  while i < n
    t = s.transition(i)
    if t.is_a? RuleTransition.class
      if calledRuleStack.get(t.target.rule_index)
        i += 1
        next
      end

      new_ctx = SingletonPredictionContext.create(ctx, t.follow_state.state_number)

      begin
        calledRuleStack.set(t.target.rule_index)
        _look(t.target, stopState, new_ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
      ensure
        calledRuleStack.clear(t.target.rule_index)
      end
    elsif t.is_a? AbstractPredicateTransition
      if seeThruPreds
        _look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
      else
        look.add(HIT_PRED)
      end
    elsif t.epsilon?
      _look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
    elsif t.is_a? WildcardTransition
      look.add_all(IntervalSet.of(Token::MIN_USER_TOKEN_TYPE, atn.max_token_type))
    else
      set = t.label
      unless set.nil?
        if t.is_a? NotSetTransition
          set = set.complement(IntervalSet.of(Token::MIN_USER_TOKEN_TYPE, atn.max_token_type))
        end
        look.add_all(set)
      end
    end

    i += 1
  end
end

#decision_lookahead(s) ⇒ Object



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

def decision_lookahead(s)
  return nil if s.nil?

  look = []
  alt = 0
  while alt < s.number_of_transitions
    look[alt] = new IntervalSet
    look_busy = Set.new
    see_thru_preds = false # fail to get lookahead upon pred
    _look(s.transition(alt).target, nil, EmptyPredictionContext::EMPTY, look[alt], look_busy, Set.new, see_thru_preds, false)
    # Wipe out lookahead for this alternative if we found nothing
    # or we had a predicate when we !see_thru_preds
    look[alt] = nil if look[alt].empty? || look[alt].include?(HIT_PRED)
    alt += 1
  end
  look
end

#look(s, stop_state, ctx) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/antlr4/runtime/ll1_analyzer.rb', line 28

def look(s, stop_state, ctx)
  r = IntervalSet.new
  see_thru_preds = true # ignore preds get all lookahead
  look_context = !ctx.nil? ? PredictionContextUtils.from_rule_context(s.atn, ctx) : nil
  _look(s, stop_state, look_context, r, Set.new, Set.new, see_thru_preds, true)
  r
end