omlish 0.0.0.dev57__py3-none-any.whl → 0.0.0.dev58__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. omlish/__about__.py +2 -2
  2. omlish/antlr/__init__.py +0 -0
  3. omlish/antlr/_runtime/BufferedTokenStream.py +305 -0
  4. omlish/antlr/_runtime/CommonTokenFactory.py +64 -0
  5. omlish/antlr/_runtime/CommonTokenStream.py +90 -0
  6. omlish/antlr/_runtime/FileStream.py +30 -0
  7. omlish/antlr/_runtime/InputStream.py +90 -0
  8. omlish/antlr/_runtime/IntervalSet.py +183 -0
  9. omlish/antlr/_runtime/LL1Analyzer.py +176 -0
  10. omlish/antlr/_runtime/Lexer.py +332 -0
  11. omlish/antlr/_runtime/ListTokenSource.py +147 -0
  12. omlish/antlr/_runtime/Parser.py +583 -0
  13. omlish/antlr/_runtime/ParserInterpreter.py +173 -0
  14. omlish/antlr/_runtime/ParserRuleContext.py +189 -0
  15. omlish/antlr/_runtime/PredictionContext.py +632 -0
  16. omlish/antlr/_runtime/Recognizer.py +150 -0
  17. omlish/antlr/_runtime/RuleContext.py +230 -0
  18. omlish/antlr/_runtime/StdinStream.py +14 -0
  19. omlish/antlr/_runtime/Token.py +158 -0
  20. omlish/antlr/_runtime/TokenStreamRewriter.py +258 -0
  21. omlish/antlr/_runtime/Utils.py +36 -0
  22. omlish/antlr/_runtime/__init__.py +24 -0
  23. omlish/antlr/_runtime/_pygrun.py +174 -0
  24. omlish/antlr/_runtime/atn/ATN.py +135 -0
  25. omlish/antlr/_runtime/atn/ATNConfig.py +162 -0
  26. omlish/antlr/_runtime/atn/ATNConfigSet.py +215 -0
  27. omlish/antlr/_runtime/atn/ATNDeserializationOptions.py +27 -0
  28. omlish/antlr/_runtime/atn/ATNDeserializer.py +449 -0
  29. omlish/antlr/_runtime/atn/ATNSimulator.py +50 -0
  30. omlish/antlr/_runtime/atn/ATNState.py +267 -0
  31. omlish/antlr/_runtime/atn/ATNType.py +20 -0
  32. omlish/antlr/_runtime/atn/LexerATNSimulator.py +573 -0
  33. omlish/antlr/_runtime/atn/LexerAction.py +301 -0
  34. omlish/antlr/_runtime/atn/LexerActionExecutor.py +146 -0
  35. omlish/antlr/_runtime/atn/ParserATNSimulator.py +1664 -0
  36. omlish/antlr/_runtime/atn/PredictionMode.py +502 -0
  37. omlish/antlr/_runtime/atn/SemanticContext.py +333 -0
  38. omlish/antlr/_runtime/atn/Transition.py +271 -0
  39. omlish/antlr/_runtime/atn/__init__.py +4 -0
  40. omlish/antlr/_runtime/dfa/DFA.py +136 -0
  41. omlish/antlr/_runtime/dfa/DFASerializer.py +76 -0
  42. omlish/antlr/_runtime/dfa/DFAState.py +129 -0
  43. omlish/antlr/_runtime/dfa/__init__.py +4 -0
  44. omlish/antlr/_runtime/error/DiagnosticErrorListener.py +110 -0
  45. omlish/antlr/_runtime/error/ErrorListener.py +75 -0
  46. omlish/antlr/_runtime/error/ErrorStrategy.py +712 -0
  47. omlish/antlr/_runtime/error/Errors.py +176 -0
  48. omlish/antlr/_runtime/error/__init__.py +4 -0
  49. omlish/antlr/_runtime/tree/Chunk.py +33 -0
  50. omlish/antlr/_runtime/tree/ParseTreeMatch.py +121 -0
  51. omlish/antlr/_runtime/tree/ParseTreePattern.py +75 -0
  52. omlish/antlr/_runtime/tree/ParseTreePatternMatcher.py +377 -0
  53. omlish/antlr/_runtime/tree/RuleTagToken.py +53 -0
  54. omlish/antlr/_runtime/tree/TokenTagToken.py +50 -0
  55. omlish/antlr/_runtime/tree/Tree.py +194 -0
  56. omlish/antlr/_runtime/tree/Trees.py +114 -0
  57. omlish/antlr/_runtime/tree/__init__.py +2 -0
  58. omlish/antlr/_runtime/xpath/XPath.py +272 -0
  59. omlish/antlr/_runtime/xpath/XPathLexer.py +98 -0
  60. omlish/antlr/_runtime/xpath/__init__.py +4 -0
  61. {omlish-0.0.0.dev57.dist-info → omlish-0.0.0.dev58.dist-info}/METADATA +1 -1
  62. {omlish-0.0.0.dev57.dist-info → omlish-0.0.0.dev58.dist-info}/RECORD +66 -7
  63. {omlish-0.0.0.dev57.dist-info → omlish-0.0.0.dev58.dist-info}/LICENSE +0 -0
  64. {omlish-0.0.0.dev57.dist-info → omlish-0.0.0.dev58.dist-info}/WHEEL +0 -0
  65. {omlish-0.0.0.dev57.dist-info → omlish-0.0.0.dev58.dist-info}/entry_points.txt +0 -0
  66. {omlish-0.0.0.dev57.dist-info → omlish-0.0.0.dev58.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,502 @@
1
+ # type: ignore
2
+ # ruff: noqa
3
+ # flake8: noqa
4
+ #
5
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
6
+ # Use of this file is governed by the BSD 3-clause license that
7
+ # can be found in the LICENSE.txt file in the project root.
8
+ #
9
+ #
10
+ # This enumeration defines the prediction modes available in ANTLR 4 along with
11
+ # utility methods for analyzing configuration sets for conflicts and/or
12
+ # ambiguities.
13
+
14
+
15
+ from enum import Enum
16
+ from .ATN import ATN
17
+ from .ATNConfig import ATNConfig
18
+ from .ATNConfigSet import ATNConfigSet
19
+ from .ATNState import RuleStopState
20
+ from .SemanticContext import SemanticContext
21
+
22
+ PredictionMode = None
23
+
24
+ class PredictionMode(Enum):
25
+ #
26
+ # The SLL(*) prediction mode. This prediction mode ignores the current
27
+ # parser context when making predictions. This is the fastest prediction
28
+ # mode, and provides correct results for many grammars. This prediction
29
+ # mode is more powerful than the prediction mode provided by ANTLR 3, but
30
+ # may result in syntax errors for grammar and input combinations which are
31
+ # not SLL.
32
+ #
33
+ # <p>
34
+ # When using this prediction mode, the parser will either return a correct
35
+ # parse tree (i.e. the same parse tree that would be returned with the
36
+ # {@link #LL} prediction mode), or it will report a syntax error. If a
37
+ # syntax error is encountered when using the {@link #SLL} prediction mode,
38
+ # it may be due to either an actual syntax error in the input or indicate
39
+ # that the particular combination of grammar and input requires the more
40
+ # powerful {@link #LL} prediction abilities to complete successfully.</p>
41
+ #
42
+ # <p>
43
+ # This prediction mode does not provide any guarantees for prediction
44
+ # behavior for syntactically-incorrect inputs.</p>
45
+ #
46
+ SLL = 0
47
+ #
48
+ # The LL(*) prediction mode. This prediction mode allows the current parser
49
+ # context to be used for resolving SLL conflicts that occur during
50
+ # prediction. This is the fastest prediction mode that guarantees correct
51
+ # parse results for all combinations of grammars with syntactically correct
52
+ # inputs.
53
+ #
54
+ # <p>
55
+ # When using this prediction mode, the parser will make correct decisions
56
+ # for all syntactically-correct grammar and input combinations. However, in
57
+ # cases where the grammar is truly ambiguous this prediction mode might not
58
+ # report a precise answer for <em>exactly which</em> alternatives are
59
+ # ambiguous.</p>
60
+ #
61
+ # <p>
62
+ # This prediction mode does not provide any guarantees for prediction
63
+ # behavior for syntactically-incorrect inputs.</p>
64
+ #
65
+ LL = 1
66
+ #
67
+ # The LL(*) prediction mode with exact ambiguity detection. In addition to
68
+ # the correctness guarantees provided by the {@link #LL} prediction mode,
69
+ # this prediction mode instructs the prediction algorithm to determine the
70
+ # complete and exact set of ambiguous alternatives for every ambiguous
71
+ # decision encountered while parsing.
72
+ #
73
+ # <p>
74
+ # This prediction mode may be used for diagnosing ambiguities during
75
+ # grammar development. Due to the performance overhead of calculating sets
76
+ # of ambiguous alternatives, this prediction mode should be avoided when
77
+ # the exact results are not necessary.</p>
78
+ #
79
+ # <p>
80
+ # This prediction mode does not provide any guarantees for prediction
81
+ # behavior for syntactically-incorrect inputs.</p>
82
+ #
83
+ LL_EXACT_AMBIG_DETECTION = 2
84
+
85
+
86
+ #
87
+ # Computes the SLL prediction termination condition.
88
+ #
89
+ # <p>
90
+ # This method computes the SLL prediction termination condition for both of
91
+ # the following cases.</p>
92
+ #
93
+ # <ul>
94
+ # <li>The usual SLL+LL fallback upon SLL conflict</li>
95
+ # <li>Pure SLL without LL fallback</li>
96
+ # </ul>
97
+ #
98
+ # <p><strong>COMBINED SLL+LL PARSING</strong></p>
99
+ #
100
+ # <p>When LL-fallback is enabled upon SLL conflict, correct predictions are
101
+ # ensured regardless of how the termination condition is computed by this
102
+ # method. Due to the substantially higher cost of LL prediction, the
103
+ # prediction should only fall back to LL when the additional lookahead
104
+ # cannot lead to a unique SLL prediction.</p>
105
+ #
106
+ # <p>Assuming combined SLL+LL parsing, an SLL configuration set with only
107
+ # conflicting subsets should fall back to full LL, even if the
108
+ # configuration sets don't resolve to the same alternative (e.g.
109
+ # {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting
110
+ # configuration, SLL could continue with the hopes that more lookahead will
111
+ # resolve via one of those non-conflicting configurations.</p>
112
+ #
113
+ # <p>Here's the prediction termination rule them: SLL (for SLL+LL parsing)
114
+ # stops when it sees only conflicting configuration subsets. In contrast,
115
+ # full LL keeps going when there is uncertainty.</p>
116
+ #
117
+ # <p><strong>HEURISTIC</strong></p>
118
+ #
119
+ # <p>As a heuristic, we stop prediction when we see any conflicting subset
120
+ # unless we see a state that only has one alternative associated with it.
121
+ # The single-alt-state thing lets prediction continue upon rules like
122
+ # (otherwise, it would admit defeat too soon):</p>
123
+ #
124
+ # <p>{@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}</p>
125
+ #
126
+ # <p>When the ATN simulation reaches the state before {@code ';'}, it has a
127
+ # DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally
128
+ # {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop
129
+ # processing this node because alternative to has another way to continue,
130
+ # via {@code [6|2|[]]}.</p>
131
+ #
132
+ # <p>It also let's us continue for this rule:</p>
133
+ #
134
+ # <p>{@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}</p>
135
+ #
136
+ # <p>After matching input A, we reach the stop state for rule A, state 1.
137
+ # State 8 is the state right before B. Clearly alternatives 1 and 2
138
+ # conflict and no amount of further lookahead will separate the two.
139
+ # However, alternative 3 will be able to continue and so we do not stop
140
+ # working on this state. In the previous example, we're concerned with
141
+ # states associated with the conflicting alternatives. Here alt 3 is not
142
+ # associated with the conflicting configs, but since we can continue
143
+ # looking for input reasonably, don't declare the state done.</p>
144
+ #
145
+ # <p><strong>PURE SLL PARSING</strong></p>
146
+ #
147
+ # <p>To handle pure SLL parsing, all we have to do is make sure that we
148
+ # combine stack contexts for configurations that differ only by semantic
149
+ # predicate. From there, we can do the usual SLL termination heuristic.</p>
150
+ #
151
+ # <p><strong>PREDICATES IN SLL+LL PARSING</strong></p>
152
+ #
153
+ # <p>SLL decisions don't evaluate predicates until after they reach DFA stop
154
+ # states because they need to create the DFA cache that works in all
155
+ # semantic situations. In contrast, full LL evaluates predicates collected
156
+ # during start state computation so it can ignore predicates thereafter.
157
+ # This means that SLL termination detection can totally ignore semantic
158
+ # predicates.</p>
159
+ #
160
+ # <p>Implementation-wise, {@link ATNConfigSet} combines stack contexts but not
161
+ # semantic predicate contexts so we might see two configurations like the
162
+ # following.</p>
163
+ #
164
+ # <p>{@code (s, 1, x, {}), (s, 1, x', {p})}</p>
165
+ #
166
+ # <p>Before testing these configurations against others, we have to merge
167
+ # {@code x} and {@code x'} (without modifying the existing configurations).
168
+ # For example, we test {@code (x+x')==x''} when looking for conflicts in
169
+ # the following configurations.</p>
170
+ #
171
+ # <p>{@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}</p>
172
+ #
173
+ # <p>If the configuration set has predicates (as indicated by
174
+ # {@link ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of
175
+ # the configurations to strip out all of the predicates so that a standard
176
+ # {@link ATNConfigSet} will merge everything ignoring predicates.</p>
177
+ #
178
+ @classmethod
179
+ def hasSLLConflictTerminatingPrediction(cls, mode:PredictionMode, configs:ATNConfigSet):
180
+ # Configs in rule stop states indicate reaching the end of the decision
181
+ # rule (local context) or end of start rule (full context). If all
182
+ # configs meet this condition, then none of the configurations is able
183
+ # to match additional input so we terminate prediction.
184
+ #
185
+ if cls.allConfigsInRuleStopStates(configs):
186
+ return True
187
+
188
+ # pure SLL mode parsing
189
+ if mode == PredictionMode.SLL:
190
+ # Don't bother with combining configs from different semantic
191
+ # contexts if we can fail over to full LL; costs more time
192
+ # since we'll often fail over anyway.
193
+ if configs.hasSemanticContext:
194
+ # dup configs, tossing out semantic predicates
195
+ dup = ATNConfigSet()
196
+ for c in configs:
197
+ c = ATNConfig(config=c, semantic=SemanticContext.NONE)
198
+ dup.add(c)
199
+ configs = dup
200
+ # now we have combined contexts for configs with dissimilar preds
201
+
202
+ # pure SLL or combined SLL+LL mode parsing
203
+ altsets = cls.getConflictingAltSubsets(configs)
204
+ return cls.hasConflictingAltSet(altsets) and not cls.hasStateAssociatedWithOneAlt(configs)
205
+
206
+ # Checks if any configuration in {@code configs} is in a
207
+ # {@link RuleStopState}. Configurations meeting this condition have reached
208
+ # the end of the decision rule (local context) or end of start rule (full
209
+ # context).
210
+ #
211
+ # @param configs the configuration set to test
212
+ # @return {@code true} if any configuration in {@code configs} is in a
213
+ # {@link RuleStopState}, otherwise {@code false}
214
+ @classmethod
215
+ def hasConfigInRuleStopState(cls, configs:ATNConfigSet):
216
+ return any(isinstance(cfg.state, RuleStopState) for cfg in configs)
217
+
218
+ # Checks if all configurations in {@code configs} are in a
219
+ # {@link RuleStopState}. Configurations meeting this condition have reached
220
+ # the end of the decision rule (local context) or end of start rule (full
221
+ # context).
222
+ #
223
+ # @param configs the configuration set to test
224
+ # @return {@code true} if all configurations in {@code configs} are in a
225
+ # {@link RuleStopState}, otherwise {@code false}
226
+ @classmethod
227
+ def allConfigsInRuleStopStates(cls, configs:ATNConfigSet):
228
+ return all(isinstance(cfg.state, RuleStopState) for cfg in configs)
229
+
230
+ #
231
+ # Full LL prediction termination.
232
+ #
233
+ # <p>Can we stop looking ahead during ATN simulation or is there some
234
+ # uncertainty as to which alternative we will ultimately pick, after
235
+ # consuming more input? Even if there are partial conflicts, we might know
236
+ # that everything is going to resolve to the same minimum alternative. That
237
+ # means we can stop since no more lookahead will change that fact. On the
238
+ # other hand, there might be multiple conflicts that resolve to different
239
+ # minimums. That means we need more look ahead to decide which of those
240
+ # alternatives we should predict.</p>
241
+ #
242
+ # <p>The basic idea is to split the set of configurations {@code C}, into
243
+ # conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with
244
+ # non-conflicting configurations. Two configurations conflict if they have
245
+ # identical {@link ATNConfig#state} and {@link ATNConfig#context} values
246
+ # but different {@link ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)}
247
+ # and {@code (s, j, ctx, _)} for {@code i!=j}.</p>
248
+ #
249
+ # <p>Reduce these configuration subsets to the set of possible alternatives.
250
+ # You can compute the alternative subsets in one pass as follows:</p>
251
+ #
252
+ # <p>{@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in
253
+ # {@code C} holding {@code s} and {@code ctx} fixed.</p>
254
+ #
255
+ # <p>Or in pseudo-code, for each configuration {@code c} in {@code C}:</p>
256
+ #
257
+ # <pre>
258
+ # map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
259
+ # alt and not pred
260
+ # </pre>
261
+ #
262
+ # <p>The values in {@code map} are the set of {@code A_s,ctx} sets.</p>
263
+ #
264
+ # <p>If {@code |A_s,ctx|=1} then there is no conflict associated with
265
+ # {@code s} and {@code ctx}.</p>
266
+ #
267
+ # <p>Reduce the subsets to singletons by choosing a minimum of each subset. If
268
+ # the union of these alternative subsets is a singleton, then no amount of
269
+ # more lookahead will help us. We will always pick that alternative. If,
270
+ # however, there is more than one alternative, then we are uncertain which
271
+ # alternative to predict and must continue looking for resolution. We may
272
+ # or may not discover an ambiguity in the future, even if there are no
273
+ # conflicting subsets this round.</p>
274
+ #
275
+ # <p>The biggest sin is to terminate early because it means we've made a
276
+ # decision but were uncertain as to the eventual outcome. We haven't used
277
+ # enough lookahead. On the other hand, announcing a conflict too late is no
278
+ # big deal; you will still have the conflict. It's just inefficient. It
279
+ # might even look until the end of file.</p>
280
+ #
281
+ # <p>No special consideration for semantic predicates is required because
282
+ # predicates are evaluated on-the-fly for full LL prediction, ensuring that
283
+ # no configuration contains a semantic context during the termination
284
+ # check.</p>
285
+ #
286
+ # <p><strong>CONFLICTING CONFIGS</strong></p>
287
+ #
288
+ # <p>Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict
289
+ # when {@code i!=j} but {@code x=x'}. Because we merge all
290
+ # {@code (s, i, _)} configurations together, that means that there are at
291
+ # most {@code n} configurations associated with state {@code s} for
292
+ # {@code n} possible alternatives in the decision. The merged stacks
293
+ # complicate the comparison of configuration contexts {@code x} and
294
+ # {@code x'}. Sam checks to see if one is a subset of the other by calling
295
+ # merge and checking to see if the merged result is either {@code x} or
296
+ # {@code x'}. If the {@code x} associated with lowest alternative {@code i}
297
+ # is the superset, then {@code i} is the only possible prediction since the
298
+ # others resolve to {@code min(i)} as well. However, if {@code x} is
299
+ # associated with {@code j>i} then at least one stack configuration for
300
+ # {@code j} is not in conflict with alternative {@code i}. The algorithm
301
+ # should keep going, looking for more lookahead due to the uncertainty.</p>
302
+ #
303
+ # <p>For simplicity, I'm doing a equality check between {@code x} and
304
+ # {@code x'} that lets the algorithm continue to consume lookahead longer
305
+ # than necessary. The reason I like the equality is of course the
306
+ # simplicity but also because that is the test you need to detect the
307
+ # alternatives that are actually in conflict.</p>
308
+ #
309
+ # <p><strong>CONTINUE/STOP RULE</strong></p>
310
+ #
311
+ # <p>Continue if union of resolved alternative sets from non-conflicting and
312
+ # conflicting alternative subsets has more than one alternative. We are
313
+ # uncertain about which alternative to predict.</p>
314
+ #
315
+ # <p>The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which
316
+ # alternatives are still in the running for the amount of input we've
317
+ # consumed at this point. The conflicting sets let us to strip away
318
+ # configurations that won't lead to more states because we resolve
319
+ # conflicts to the configuration with a minimum alternate for the
320
+ # conflicting set.</p>
321
+ #
322
+ # <p><strong>CASES</strong></p>
323
+ #
324
+ # <ul>
325
+ #
326
+ # <li>no conflicts and more than 1 alternative in set =&gt; continue</li>
327
+ #
328
+ # <li> {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)},
329
+ # {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set
330
+ # {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
331
+ # {@code {1,3}} =&gt; continue
332
+ # </li>
333
+ #
334
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
335
+ # {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set
336
+ # {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
337
+ # {@code {1}} =&gt; stop and predict 1</li>
338
+ #
339
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
340
+ # {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U
341
+ # {@code {1}} = {@code {1}} =&gt; stop and predict 1, can announce
342
+ # ambiguity {@code {1,2}}</li>
343
+ #
344
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)},
345
+ # {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U
346
+ # {@code {2}} = {@code {1,2}} =&gt; continue</li>
347
+ #
348
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)},
349
+ # {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U
350
+ # {@code {3}} = {@code {1,3}} =&gt; continue</li>
351
+ #
352
+ # </ul>
353
+ #
354
+ # <p><strong>EXACT AMBIGUITY DETECTION</strong></p>
355
+ #
356
+ # <p>If all states report the same conflicting set of alternatives, then we
357
+ # know we have the exact ambiguity set.</p>
358
+ #
359
+ # <p><code>|A_<em>i</em>|&gt;1</code> and
360
+ # <code>A_<em>i</em> = A_<em>j</em></code> for all <em>i</em>, <em>j</em>.</p>
361
+ #
362
+ # <p>In other words, we continue examining lookahead until all {@code A_i}
363
+ # have more than one alternative and all {@code A_i} are the same. If
364
+ # {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate
365
+ # because the resolved set is {@code {1}}. To determine what the real
366
+ # ambiguity is, we have to know whether the ambiguity is between one and
367
+ # two or one and three so we keep going. We can only stop prediction when
368
+ # we need exact ambiguity detection when the sets look like
369
+ # {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...</p>
370
+ #
371
+ @classmethod
372
+ def resolvesToJustOneViableAlt(cls, altsets:list):
373
+ return cls.getSingleViableAlt(altsets)
374
+
375
+ #
376
+ # Determines if every alternative subset in {@code altsets} contains more
377
+ # than one alternative.
378
+ #
379
+ # @param altsets a collection of alternative subsets
380
+ # @return {@code true} if every {@link BitSet} in {@code altsets} has
381
+ # {@link BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
382
+ #
383
+ @classmethod
384
+ def allSubsetsConflict(cls, altsets:list):
385
+ return not cls.hasNonConflictingAltSet(altsets)
386
+
387
+ #
388
+ # Determines if any single alternative subset in {@code altsets} contains
389
+ # exactly one alternative.
390
+ #
391
+ # @param altsets a collection of alternative subsets
392
+ # @return {@code true} if {@code altsets} contains a {@link BitSet} with
393
+ # {@link BitSet#cardinality cardinality} 1, otherwise {@code false}
394
+ #
395
+ @classmethod
396
+ def hasNonConflictingAltSet(cls, altsets:list):
397
+ return any(len(alts) == 1 for alts in altsets)
398
+
399
+ #
400
+ # Determines if any single alternative subset in {@code altsets} contains
401
+ # more than one alternative.
402
+ #
403
+ # @param altsets a collection of alternative subsets
404
+ # @return {@code true} if {@code altsets} contains a {@link BitSet} with
405
+ # {@link BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
406
+ #
407
+ @classmethod
408
+ def hasConflictingAltSet(cls, altsets:list):
409
+ return any(len(alts) > 1 for alts in altsets)
410
+
411
+ #
412
+ # Determines if every alternative subset in {@code altsets} is equivalent.
413
+ #
414
+ # @param altsets a collection of alternative subsets
415
+ # @return {@code true} if every member of {@code altsets} is equal to the
416
+ # others, otherwise {@code false}
417
+ #
418
+ @classmethod
419
+ def allSubsetsEqual(cls, altsets:list):
420
+ if not altsets:
421
+ return True
422
+ first = next(iter(altsets))
423
+ return all(alts == first for alts in iter(altsets))
424
+
425
+ #
426
+ # Returns the unique alternative predicted by all alternative subsets in
427
+ # {@code altsets}. If no such alternative exists, this method returns
428
+ # {@link ATN#INVALID_ALT_NUMBER}.
429
+ #
430
+ # @param altsets a collection of alternative subsets
431
+ #
432
+ @classmethod
433
+ def getUniqueAlt(cls, altsets:list):
434
+ all = cls.getAlts(altsets)
435
+ if len(all)==1:
436
+ return next(iter(all))
437
+ return ATN.INVALID_ALT_NUMBER
438
+
439
+ # Gets the complete set of represented alternatives for a collection of
440
+ # alternative subsets. This method returns the union of each {@link BitSet}
441
+ # in {@code altsets}.
442
+ #
443
+ # @param altsets a collection of alternative subsets
444
+ # @return the set of represented alternatives in {@code altsets}
445
+ #
446
+ @classmethod
447
+ def getAlts(cls, altsets:list):
448
+ return set.union(*altsets)
449
+
450
+ #
451
+ # This function gets the conflicting alt subsets from a configuration set.
452
+ # For each configuration {@code c} in {@code configs}:
453
+ #
454
+ # <pre>
455
+ # map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
456
+ # alt and not pred
457
+ # </pre>
458
+ #
459
+ @classmethod
460
+ def getConflictingAltSubsets(cls, configs:ATNConfigSet):
461
+ configToAlts = dict()
462
+ for c in configs:
463
+ h = hash((c.state.stateNumber, c.context))
464
+ alts = configToAlts.get(h, None)
465
+ if alts is None:
466
+ alts = set()
467
+ configToAlts[h] = alts
468
+ alts.add(c.alt)
469
+ return configToAlts.values()
470
+
471
+ #
472
+ # Get a map from state to alt subset from a configuration set. For each
473
+ # configuration {@code c} in {@code configs}:
474
+ #
475
+ # <pre>
476
+ # map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#alt alt}
477
+ # </pre>
478
+ #
479
+ @classmethod
480
+ def getStateToAltMap(cls, configs:ATNConfigSet):
481
+ m = dict()
482
+ for c in configs:
483
+ alts = m.get(c.state, None)
484
+ if alts is None:
485
+ alts = set()
486
+ m[c.state] = alts
487
+ alts.add(c.alt)
488
+ return m
489
+
490
+ @classmethod
491
+ def hasStateAssociatedWithOneAlt(cls, configs:ATNConfigSet):
492
+ return any(len(alts) == 1 for alts in cls.getStateToAltMap(configs).values())
493
+
494
+ @classmethod
495
+ def getSingleViableAlt(cls, altsets:list):
496
+ viableAlts = set()
497
+ for alts in altsets:
498
+ minAlt = min(alts)
499
+ viableAlts.add(minAlt)
500
+ if len(viableAlts)>1 : # more than 1 viable alt
501
+ return ATN.INVALID_ALT_NUMBER
502
+ return min(viableAlts)