Class: Antlr4::Runtime::PredictionMode

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

Defined Under Namespace

Classes: AltAndContextConfigEqualityComparator, AltAndContextMap

Constant Summary collapse

SLL =
0
LL =
1
LL_EXACT_AMBIG_DETECTION =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_configs_in_rule_stop_states?(configs) ⇒ Boolean

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
80
# File 'lib/antlr4/runtime/prediction_mode.rb', line 72

def self.all_configs_in_rule_stop_states?(configs)
  i = 0
  while i < configs.configs.length
    return false unless configs.configs[i].state.is_a? RuleStopState
    i += 1
  end

  true
end

.all_subsets_conflict?(altsets) ⇒ Boolean

Returns:

  • (Boolean)

86
87
88
# File 'lib/antlr4/runtime/prediction_mode.rb', line 86

def self.all_subsets_conflict?(altsets)
  !has_non_conflicting_alt_set?(altsets)
end

.conflicting_alt_subsets(configs) ⇒ Object


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/antlr4/runtime/prediction_mode.rb', line 154

def self.conflicting_alt_subsets(configs)
  config_to_alts = AltAndContextMap.new
  i = 0
  while i < configs.configs.length
    c = configs.configs[i]
    alts = config_to_alts.get(c)
    if alts.nil?
      alts = BitSet.new
      config_to_alts.put(c, alts)
    end
    alts.set(c.alt)
    i += 1
  end
  config_to_alts.values
end

.get_alts1(altsets) ⇒ Object


132
133
134
135
136
137
138
139
140
141
# File 'lib/antlr4/runtime/prediction_mode.rb', line 132

def self.get_alts1(altsets)
  all = BitSet.new
  i = 0
  while i < altsets.length
    alts = altsets[i]
    all.or(alts)
    i += 1
  end
  all
end

.has_config_in_rule_stop_state?(configs) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
65
66
67
68
69
70
# File 'lib/antlr4/runtime/prediction_mode.rb', line 62

def self.has_config_in_rule_stop_state?(configs)
  i = 0
  while i < configs.configs.length
    return true if configs.configs[i].state.is_a? RuleStopState
    i += 1
  end

  false
end

.has_conflicting_alt_set?(altsets) ⇒ Boolean

Returns:

  • (Boolean)

100
101
102
103
104
105
106
107
108
# File 'lib/antlr4/runtime/prediction_mode.rb', line 100

def self.has_conflicting_alt_set?(altsets)
  i = 0
  while i < altsets.length
    alts = altsets[i]
    return true if alts.cardinality > 1
    i += 1
  end
  false
end

.has_non_conflicting_alt_set?(altsets) ⇒ Boolean

Returns:

  • (Boolean)

90
91
92
93
94
95
96
97
98
# File 'lib/antlr4/runtime/prediction_mode.rb', line 90

def self.has_non_conflicting_alt_set?(altsets)
  i = 0
  while i < altsets.length
    alts = altsets[i]
    return true if alts.cardinality == 1
    i += 1
  end
  false
end

.has_sll_conflict_terminating_prediction(mode, configs) ⇒ Object


29
30
31
32
33
34
35
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
# File 'lib/antlr4/runtime/prediction_mode.rb', line 29

def self.has_sll_conflict_terminating_prediction(mode, configs)
  return true if all_configs_in_rule_stop_states?(configs)

  # pure SLL mode parsing
  if mode == PredictionMode::SLL
    # Don't bother with combining configs from different semantic
    # contexts if we can fail over to full LL costs more time
    # since we'll often fail over anyway.
    if configs.has_semantic_context
      # dup configs, tossing out semantic predicates
      dup = ATNConfigSet.new
      i = 0
      while i < configs.configs.length
        cfg = configs.configs[i]
        c = ATNConfig.new
        c.atn_config5(cfg, SemanticContext::NONE)
        dup.add(c)

        i += 1
      end

      configs = dup
    end
    # now we have combined contexts for configs with dissimilar preds
  end

  # pure SLL or combined SLL+LL mode parsing

  alt_sets = conflicting_alt_subsets(configs)
  heuristic = has_conflicting_alt_set?(alt_sets) && !has_state_associated_with_one_alt?(configs)
  heuristic
end

.has_state_associated_with_one_alt?(configs) ⇒ Boolean

Returns:

  • (Boolean)

186
187
188
189
190
191
192
193
194
195
# File 'lib/antlr4/runtime/prediction_mode.rb', line 186

def self.has_state_associated_with_one_alt?(configs)
  x = state_to_alt_map(configs)
  i = 0
  while i < x.values.length
    alts = x.values[i]
    return true if alts.cardinality == 1
    i += 1
  end
  false
end

.resolves_to_just_one_viable_alt?(altsets) ⇒ Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/antlr4/runtime/prediction_mode.rb', line 82

def self.resolves_to_just_one_viable_alt?(altsets)
  single_viable_alt(altsets)
end

.single_viable_alt(altsets) ⇒ Object


197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/antlr4/runtime/prediction_mode.rb', line 197

def self.single_viable_alt(altsets)
  viable_alts = BitSet.new
  i = 0
  while i < altsets.length
    alts = altsets[i]
    min_alt = alts.next_set_bit(0)
    viable_alts.set(min_alt)
    return ATN::INVALID_ALT_NUMBER if viable_alts.cardinality > 1 # more than 1 viable alt
    i += 1
  end
  viable_alts.next_set_bit(0)
end

.state_to_alt_map(configs) ⇒ Object


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/antlr4/runtime/prediction_mode.rb', line 170

def self.state_to_alt_map(configs)
  m = {}
  i = 0
  while i < configs.configs.length
    c = configs.configs[i]
    alts = m[c.state]
    if alts.nil?
      alts = BitSet.new
      m[c.state] = alts
    end
    alts.set(c.alt)
    i += 1
  end
  m
end

.unique_alt(altsets) ⇒ Object


125
126
127
128
129
130
# File 'lib/antlr4/runtime/prediction_mode.rb', line 125

def self.unique_alt(altsets)
  all = get_alts1(altsets)
  return all.next_set_bit(0) if all.cardinality == 1

  ATN::INVALID_ALT_NUMBER
end

Instance Method Details

#all_subsets_equal?(altsets) ⇒ Boolean

Returns:

  • (Boolean)

110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/antlr4/runtime/prediction_mode.rb', line 110

def all_subsets_equal?(altsets)
  first = nil
  i = 0
  while i < altsets.length
    alt = altsets[i]
    if i == 0
      first = altsets[0]
    else
      return false unless alt.eql?(first)
    end
    i += 1
  end
  true
end

#get_alts2(configs) ⇒ Object


143
144
145
146
147
148
149
150
151
152
# File 'lib/antlr4/runtime/prediction_mode.rb', line 143

def get_alts2(configs)
  alts = BitSet.new
  i = 0
  while i < configs.length
    config = configs[i]
    alts.set(config.alt)
    i += 1
  end
  alts
end