omlish 0.0.0.dev56__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 (68) 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/formats/json/cli.py +76 -7
  62. omlish/formats/props.py +6 -2
  63. {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/METADATA +1 -1
  64. {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/RECORD +68 -9
  65. {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/LICENSE +0 -0
  66. {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/WHEEL +0 -0
  67. {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/entry_points.txt +0 -0
  68. {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1664 @@
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
+ #
11
+ # The embodiment of the adaptive LL(*), ALL(*), parsing strategy.
12
+ #
13
+ # <p>
14
+ # The basic complexity of the adaptive strategy makes it harder to understand.
15
+ # We begin with ATN simulation to build paths in a DFA. Subsequent prediction
16
+ # requests go through the DFA first. If they reach a state without an edge for
17
+ # the current symbol, the algorithm fails over to the ATN simulation to
18
+ # complete the DFA path for the current input (until it finds a conflict state
19
+ # or uniquely predicting state).</p>
20
+ #
21
+ # <p>
22
+ # All of that is done without using the outer context because we want to create
23
+ # a DFA that is not dependent upon the rule invocation stack when we do a
24
+ # prediction. One DFA works in all contexts. We avoid using context not
25
+ # necessarily because it's slower, although it can be, but because of the DFA
26
+ # caching problem. The closure routine only considers the rule invocation stack
27
+ # created during prediction beginning in the decision rule. For example, if
28
+ # prediction occurs without invoking another rule's ATN, there are no context
29
+ # stacks in the configurations. When lack of context leads to a conflict, we
30
+ # don't know if it's an ambiguity or a weakness in the strong LL(*) parsing
31
+ # strategy (versus full LL(*)).</p>
32
+ #
33
+ # <p>
34
+ # When SLL yields a configuration set with conflict, we rewind the input and
35
+ # retry the ATN simulation, this time using full outer context without adding
36
+ # to the DFA. Configuration context stacks will be the full invocation stacks
37
+ # from the start rule. If we get a conflict using full context, then we can
38
+ # definitively say we have a true ambiguity for that input sequence. If we
39
+ # don't get a conflict, it implies that the decision is sensitive to the outer
40
+ # context. (It is not context-sensitive in the sense of context-sensitive
41
+ # grammars.)</p>
42
+ #
43
+ # <p>
44
+ # The next time we reach this DFA state with an SLL conflict, through DFA
45
+ # simulation, we will again retry the ATN simulation using full context mode.
46
+ # This is slow because we can't save the results and have to "interpret" the
47
+ # ATN each time we get that input.</p>
48
+ #
49
+ # <p>
50
+ # <strong>CACHING FULL CONTEXT PREDICTIONS</strong></p>
51
+ #
52
+ # <p>
53
+ # We could cache results from full context to predicted alternative easily and
54
+ # that saves a lot of time but doesn't work in presence of predicates. The set
55
+ # of visible predicates from the ATN start state changes depending on the
56
+ # context, because closure can fall off the end of a rule. I tried to cache
57
+ # tuples (stack context, semantic context, predicted alt) but it was slower
58
+ # than interpreting and much more complicated. Also required a huge amount of
59
+ # memory. The goal is not to create the world's fastest parser anyway. I'd like
60
+ # to keep this algorithm simple. By launching multiple threads, we can improve
61
+ # the speed of parsing across a large number of files.</p>
62
+ #
63
+ # <p>
64
+ # There is no strict ordering between the amount of input used by SLL vs LL,
65
+ # which makes it really hard to build a cache for full context. Let's say that
66
+ # we have input A B C that leads to an SLL conflict with full context X. That
67
+ # implies that using X we might only use A B but we could also use A B C D to
68
+ # resolve conflict. Input A B C D could predict alternative 1 in one position
69
+ # in the input and A B C E could predict alternative 2 in another position in
70
+ # input. The conflicting SLL configurations could still be non-unique in the
71
+ # full context prediction, which would lead us to requiring more input than the
72
+ # original A B C. To make a prediction cache work, we have to track the exact
73
+ # input used during the previous prediction. That amounts to a cache that maps
74
+ # X to a specific DFA for that context.</p>
75
+ #
76
+ # <p>
77
+ # Something should be done for left-recursive expression predictions. They are
78
+ # likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry
79
+ # with full LL thing Sam does.</p>
80
+ #
81
+ # <p>
82
+ # <strong>AVOIDING FULL CONTEXT PREDICTION</strong></p>
83
+ #
84
+ # <p>
85
+ # We avoid doing full context retry when the outer context is empty, we did not
86
+ # dip into the outer context by falling off the end of the decision state rule,
87
+ # or when we force SLL mode.</p>
88
+ #
89
+ # <p>
90
+ # As an example of the not dip into outer context case, consider as super
91
+ # constructor calls versus function calls. One grammar might look like
92
+ # this:</p>
93
+ #
94
+ # <pre>
95
+ # ctorBody
96
+ # : '{' superCall? stat* '}'
97
+ # ;
98
+ # </pre>
99
+ #
100
+ # <p>
101
+ # Or, you might see something like</p>
102
+ #
103
+ # <pre>
104
+ # stat
105
+ # : superCall ';'
106
+ # | expression ';'
107
+ # | ...
108
+ # ;
109
+ # </pre>
110
+ #
111
+ # <p>
112
+ # In both cases I believe that no closure operations will dip into the outer
113
+ # context. In the first case ctorBody in the worst case will stop at the '}'.
114
+ # In the 2nd case it should stop at the ';'. Both cases should stay within the
115
+ # entry rule and not dip into the outer context.</p>
116
+ #
117
+ # <p>
118
+ # <strong>PREDICATES</strong></p>
119
+ #
120
+ # <p>
121
+ # Predicates are always evaluated if present in either SLL or LL both. SLL and
122
+ # LL simulation deals with predicates differently. SLL collects predicates as
123
+ # it performs closure operations like ANTLR v3 did. It delays predicate
124
+ # evaluation until it reaches and accept state. This allows us to cache the SLL
125
+ # ATN simulation whereas, if we had evaluated predicates on-the-fly during
126
+ # closure, the DFA state configuration sets would be different and we couldn't
127
+ # build up a suitable DFA.</p>
128
+ #
129
+ # <p>
130
+ # When building a DFA accept state during ATN simulation, we evaluate any
131
+ # predicates and return the sole semantically valid alternative. If there is
132
+ # more than 1 alternative, we report an ambiguity. If there are 0 alternatives,
133
+ # we throw an exception. Alternatives without predicates act like they have
134
+ # true predicates. The simple way to think about it is to strip away all
135
+ # alternatives with false predicates and choose the minimum alternative that
136
+ # remains.</p>
137
+ #
138
+ # <p>
139
+ # When we start in the DFA and reach an accept state that's predicated, we test
140
+ # those and return the minimum semantically viable alternative. If no
141
+ # alternatives are viable, we throw an exception.</p>
142
+ #
143
+ # <p>
144
+ # During full LL ATN simulation, closure always evaluates predicates and
145
+ # on-the-fly. This is crucial to reducing the configuration set size during
146
+ # closure. It hits a landmine when parsing with the Java grammar, for example,
147
+ # without this on-the-fly evaluation.</p>
148
+ #
149
+ # <p>
150
+ # <strong>SHARING DFA</strong></p>
151
+ #
152
+ # <p>
153
+ # All instances of the same parser share the same decision DFAs through a
154
+ # static field. Each instance gets its own ATN simulator but they share the
155
+ # same {@link #decisionToDFA} field. They also share a
156
+ # {@link PredictionContextCache} object that makes sure that all
157
+ # {@link PredictionContext} objects are shared among the DFA states. This makes
158
+ # a big size difference.</p>
159
+ #
160
+ # <p>
161
+ # <strong>THREAD SAFETY</strong></p>
162
+ #
163
+ # <p>
164
+ # The {@link ParserATNSimulator} locks on the {@link #decisionToDFA} field when
165
+ # it adds a new DFA object to that array. {@link #addDFAEdge}
166
+ # locks on the DFA for the current decision when setting the
167
+ # {@link DFAState#edges} field. {@link #addDFAState} locks on
168
+ # the DFA for the current decision when looking up a DFA state to see if it
169
+ # already exists. We must make sure that all requests to add DFA states that
170
+ # are equivalent result in the same shared DFA object. This is because lots of
171
+ # threads will be trying to update the DFA at once. The
172
+ # {@link #addDFAState} method also locks inside the DFA lock
173
+ # but this time on the shared context cache when it rebuilds the
174
+ # configurations' {@link PredictionContext} objects using cached
175
+ # subgraphs/nodes. No other locking occurs, even during DFA simulation. This is
176
+ # safe as long as we can guarantee that all threads referencing
177
+ # {@code s.edge[t]} get the same physical target {@link DFAState}, or
178
+ # {@code null}. Once into the DFA, the DFA simulation does not reference the
179
+ # {@link DFA#states} map. It follows the {@link DFAState#edges} field to new
180
+ # targets. The DFA simulator will either find {@link DFAState#edges} to be
181
+ # {@code null}, to be non-{@code null} and {@code dfa.edges[t]} null, or
182
+ # {@code dfa.edges[t]} to be non-null. The
183
+ # {@link #addDFAEdge} method could be racing to set the field
184
+ # but in either case the DFA simulator works; if {@code null}, and requests ATN
185
+ # simulation. It could also race trying to get {@code dfa.edges[t]}, but either
186
+ # way it will work because it's not doing a test and set operation.</p>
187
+ #
188
+ # <p>
189
+ # <strong>Starting with SLL then failing to combined SLL/LL (Two-Stage
190
+ # Parsing)</strong></p>
191
+ #
192
+ # <p>
193
+ # Sam pointed out that if SLL does not give a syntax error, then there is no
194
+ # point in doing full LL, which is slower. We only have to try LL if we get a
195
+ # syntax error. For maximum speed, Sam starts the parser set to pure SLL
196
+ # mode with the {@link BailErrorStrategy}:</p>
197
+ #
198
+ # <pre>
199
+ # parser.{@link Parser#getInterpreter() getInterpreter()}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
200
+ # parser.{@link Parser#setErrorHandler setErrorHandler}(new {@link BailErrorStrategy}());
201
+ # </pre>
202
+ #
203
+ # <p>
204
+ # If it does not get a syntax error, then we're done. If it does get a syntax
205
+ # error, we need to retry with the combined SLL/LL strategy.</p>
206
+ #
207
+ # <p>
208
+ # The reason this works is as follows. If there are no SLL conflicts, then the
209
+ # grammar is SLL (at least for that input set). If there is an SLL conflict,
210
+ # the full LL analysis must yield a set of viable alternatives which is a
211
+ # subset of the alternatives reported by SLL. If the LL set is a singleton,
212
+ # then the grammar is LL but not SLL. If the LL set is the same size as the SLL
213
+ # set, the decision is SLL. If the LL set has size &gt; 1, then that decision
214
+ # is truly ambiguous on the current input. If the LL set is smaller, then the
215
+ # SLL conflict resolution might choose an alternative that the full LL would
216
+ # rule out as a possibility based upon better context information. If that's
217
+ # the case, then the SLL parse will definitely get an error because the full LL
218
+ # analysis says it's not viable. If SLL conflict resolution chooses an
219
+ # alternative within the LL set, them both SLL and LL would choose the same
220
+ # alternative because they both choose the minimum of multiple conflicting
221
+ # alternatives.</p>
222
+ #
223
+ # <p>
224
+ # Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and
225
+ # a smaller LL set called <em>s</em>. If <em>s</em> is {@code {2, 3}}, then SLL
226
+ # parsing will get an error because SLL will pursue alternative 1. If
227
+ # <em>s</em> is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will
228
+ # choose the same alternative because alternative one is the minimum of either
229
+ # set. If <em>s</em> is {@code {2}} or {@code {3}} then SLL will get a syntax
230
+ # error. If <em>s</em> is {@code {1}} then SLL will succeed.</p>
231
+ #
232
+ # <p>
233
+ # Of course, if the input is invalid, then we will get an error for sure in
234
+ # both SLL and LL parsing. Erroneous input will therefore require 2 passes over
235
+ # the input.</p>
236
+ #
237
+ import sys
238
+ from .. import DFA
239
+ from ..BufferedTokenStream import TokenStream
240
+ from ..Parser import Parser
241
+ from ..ParserRuleContext import ParserRuleContext
242
+ from ..PredictionContext import PredictionContextCache, PredictionContext, SingletonPredictionContext, \
243
+ PredictionContextFromRuleContext
244
+ from ..RuleContext import RuleContext
245
+ from ..Token import Token
246
+ from ..Utils import str_list
247
+ from .ATN import ATN
248
+ from .ATNConfig import ATNConfig
249
+ from .ATNConfigSet import ATNConfigSet
250
+ from .ATNSimulator import ATNSimulator
251
+ from .ATNState import DecisionState, RuleStopState, ATNState
252
+ from .PredictionMode import PredictionMode
253
+ from .SemanticContext import SemanticContext, andContext, orContext
254
+ from .Transition import Transition, RuleTransition, ActionTransition, PrecedencePredicateTransition, \
255
+ PredicateTransition, AtomTransition, SetTransition, NotSetTransition
256
+ from ..dfa.DFAState import DFAState, PredPrediction
257
+ from ..error.Errors import NoViableAltException
258
+
259
+
260
+ class ParserATNSimulator(ATNSimulator):
261
+ __slots__ = (
262
+ 'parser', 'decisionToDFA', 'predictionMode', '_input', '_startIndex',
263
+ '_outerContext', '_dfa', 'mergeCache'
264
+ )
265
+
266
+ debug = False
267
+ trace_atn_sim = False
268
+ dfa_debug = False
269
+ retry_debug = False
270
+
271
+
272
+ def __init__(self, parser:Parser, atn:ATN, decisionToDFA:list, sharedContextCache:PredictionContextCache):
273
+ super().__init__(atn, sharedContextCache)
274
+ self.parser = parser
275
+ self.decisionToDFA = decisionToDFA
276
+ # SLL, LL, or LL + exact ambig detection?#
277
+ self.predictionMode = PredictionMode.LL
278
+ # LAME globals to avoid parameters!!!!! I need these down deep in predTransition
279
+ self._input = None
280
+ self._startIndex = 0
281
+ self._outerContext = None
282
+ self._dfa = None
283
+ # Each prediction operation uses a cache for merge of prediction contexts.
284
+ # Don't keep around as it wastes huge amounts of memory. DoubleKeyMap
285
+ # isn't synchronized but we're ok since two threads shouldn't reuse same
286
+ # parser/atnsim object because it can only handle one input at a time.
287
+ # This maps graphs a and b to merged result c. (a,b)&rarr;c. We can avoid
288
+ # the merge if we ever see a and b again. Note that (b,a)&rarr;c should
289
+ # also be examined during cache lookup.
290
+ #
291
+ self.mergeCache = None
292
+
293
+
294
+ def reset(self):
295
+ pass
296
+
297
+ def adaptivePredict(self, input:TokenStream, decision:int, outerContext:ParserRuleContext):
298
+ if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim:
299
+ print("adaptivePredict decision " + str(decision) +
300
+ " exec LA(1)==" + self.getLookaheadName(input) +
301
+ " line " + str(input.LT(1).line) + ":" +
302
+ str(input.LT(1).column))
303
+ self._input = input
304
+ self._startIndex = input.index
305
+ self._outerContext = outerContext
306
+
307
+ dfa = self.decisionToDFA[decision]
308
+ self._dfa = dfa
309
+ m = input.mark()
310
+ index = input.index
311
+
312
+ # Now we are certain to have a specific decision's DFA
313
+ # But, do we still need an initial state?
314
+ try:
315
+ if dfa.precedenceDfa:
316
+ # the start state for a precedence DFA depends on the current
317
+ # parser precedence, and is provided by a DFA method.
318
+ s0 = dfa.getPrecedenceStartState(self.parser.getPrecedence())
319
+ else:
320
+ # the start state for a "regular" DFA is just s0
321
+ s0 = dfa.s0
322
+
323
+ if s0 is None:
324
+ if outerContext is None:
325
+ outerContext = ParserRuleContext.EMPTY
326
+ if ParserATNSimulator.debug:
327
+ print("predictATN decision " + str(dfa.decision) +
328
+ " exec LA(1)==" + self.getLookaheadName(input) +
329
+ ", outerContext=" + str(outerContext));#outerContext.toString(self.parser.literalNames, None))
330
+
331
+ fullCtx = False
332
+ s0_closure = self.computeStartState(dfa.atnStartState, ParserRuleContext.EMPTY, fullCtx)
333
+
334
+ if dfa.precedenceDfa:
335
+ # If this is a precedence DFA, we use applyPrecedenceFilter
336
+ # to convert the computed start state to a precedence start
337
+ # state. We then use DFA.setPrecedenceStartState to set the
338
+ # appropriate start state for the precedence level rather
339
+ # than simply setting DFA.s0.
340
+ #
341
+ dfa.s0.configs = s0_closure # not used for prediction but useful to know start configs anyway
342
+ s0_closure = self.applyPrecedenceFilter(s0_closure)
343
+ s0 = self.addDFAState(dfa, DFAState(configs=s0_closure))
344
+ dfa.setPrecedenceStartState(self.parser.getPrecedence(), s0)
345
+ else:
346
+ s0 = self.addDFAState(dfa, DFAState(configs=s0_closure))
347
+ dfa.s0 = s0
348
+
349
+ alt = self.execATN(dfa, s0, input, index, outerContext)
350
+ if ParserATNSimulator.debug:
351
+ print("DFA after predictATN: " + dfa.toString(self.parser.literalNames))
352
+ return alt
353
+ finally:
354
+ self._dfa = None
355
+ self.mergeCache = None # wack cache after each prediction
356
+ input.seek(index)
357
+ input.release(m)
358
+
359
+ # Performs ATN simulation to compute a predicted alternative based
360
+ # upon the remaining input, but also updates the DFA cache to avoid
361
+ # having to traverse the ATN again for the same input sequence.
362
+
363
+ # There are some key conditions we're looking for after computing a new
364
+ # set of ATN configs (proposed DFA state):
365
+ # if the set is empty, there is no viable alternative for current symbol
366
+ # does the state uniquely predict an alternative?
367
+ # does the state have a conflict that would prevent us from
368
+ # putting it on the work list?
369
+
370
+ # We also have some key operations to do:
371
+ # add an edge from previous DFA state to potentially new DFA state, D,
372
+ # upon current symbol but only if adding to work list, which means in all
373
+ # cases except no viable alternative (and possibly non-greedy decisions?)
374
+ # collecting predicates and adding semantic context to DFA accept states
375
+ # adding rule context to context-sensitive DFA accept states
376
+ # consuming an input symbol
377
+ # reporting a conflict
378
+ # reporting an ambiguity
379
+ # reporting a context sensitivity
380
+ # reporting insufficient predicates
381
+
382
+ # cover these cases:
383
+ # dead end
384
+ # single alt
385
+ # single alt + preds
386
+ # conflict
387
+ # conflict + preds
388
+ #
389
+ def execATN(self, dfa:DFA, s0:DFAState, input:TokenStream, startIndex:int, outerContext:ParserRuleContext ):
390
+ if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim:
391
+ print("execATN decision " + str(dfa.decision) +
392
+ ", DFA state " + str(s0) +
393
+ ", LA(1)==" + self.getLookaheadName(input) +
394
+ " line " + str(input.LT(1).line) + ":" + str(input.LT(1).column))
395
+
396
+ previousD = s0
397
+
398
+ t = input.LA(1)
399
+
400
+ while True: # while more work
401
+ D = self.getExistingTargetState(previousD, t)
402
+ if D is None:
403
+ D = self.computeTargetState(dfa, previousD, t)
404
+ if D is self.ERROR:
405
+ # if any configs in previous dipped into outer context, that
406
+ # means that input up to t actually finished entry rule
407
+ # at least for SLL decision. Full LL doesn't dip into outer
408
+ # so don't need special case.
409
+ # We will get an error no matter what so delay until after
410
+ # decision; better error message. Also, no reachable target
411
+ # ATN states in SLL implies LL will also get nowhere.
412
+ # If conflict in states that dip out, choose min since we
413
+ # will get error no matter what.
414
+ e = self.noViableAlt(input, outerContext, previousD.configs, startIndex)
415
+ input.seek(startIndex)
416
+ alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD.configs, outerContext)
417
+ if alt!=ATN.INVALID_ALT_NUMBER:
418
+ return alt
419
+ raise e
420
+
421
+ if D.requiresFullContext and self.predictionMode != PredictionMode.SLL:
422
+ # IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
423
+ conflictingAlts = D.configs.conflictingAlts
424
+ if D.predicates is not None:
425
+ if ParserATNSimulator.debug:
426
+ print("DFA state has preds in DFA sim LL failover")
427
+ conflictIndex = input.index
428
+ if conflictIndex != startIndex:
429
+ input.seek(startIndex)
430
+
431
+ conflictingAlts = self.evalSemanticContext(D.predicates, outerContext, True)
432
+ if len(conflictingAlts)==1:
433
+ if ParserATNSimulator.debug:
434
+ print("Full LL avoided")
435
+ return min(conflictingAlts)
436
+
437
+ if conflictIndex != startIndex:
438
+ # restore the index so reporting the fallback to full
439
+ # context occurs with the index at the correct spot
440
+ input.seek(conflictIndex)
441
+
442
+ if ParserATNSimulator.dfa_debug:
443
+ print("ctx sensitive state " + str(outerContext) +" in " + str(D))
444
+ fullCtx = True
445
+ s0_closure = self.computeStartState(dfa.atnStartState, outerContext, fullCtx)
446
+ self.reportAttemptingFullContext(dfa, conflictingAlts, D.configs, startIndex, input.index)
447
+ alt = self.execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext)
448
+ return alt
449
+
450
+ if D.isAcceptState:
451
+ if D.predicates is None:
452
+ return D.prediction
453
+
454
+ stopIndex = input.index
455
+ input.seek(startIndex)
456
+ alts = self.evalSemanticContext(D.predicates, outerContext, True)
457
+ if len(alts)==0:
458
+ raise self.noViableAlt(input, outerContext, D.configs, startIndex)
459
+ elif len(alts)==1:
460
+ return min(alts)
461
+ else:
462
+ # report ambiguity after predicate evaluation to make sure the correct
463
+ # set of ambig alts is reported.
464
+ self.reportAmbiguity(dfa, D, startIndex, stopIndex, False, alts, D.configs)
465
+ return min(alts)
466
+
467
+ previousD = D
468
+
469
+ if t != Token.EOF:
470
+ input.consume()
471
+ t = input.LA(1)
472
+
473
+ #
474
+ # Get an existing target state for an edge in the DFA. If the target state
475
+ # for the edge has not yet been computed or is otherwise not available,
476
+ # this method returns {@code null}.
477
+ #
478
+ # @param previousD The current DFA state
479
+ # @param t The next input symbol
480
+ # @return The existing target DFA state for the given input symbol
481
+ # {@code t}, or {@code null} if the target state for this edge is not
482
+ # already cached
483
+ #
484
+ def getExistingTargetState(self, previousD:DFAState, t:int):
485
+ edges = previousD.edges
486
+ if edges is None or t + 1 < 0 or t + 1 >= len(edges):
487
+ return None
488
+ else:
489
+ return edges[t + 1]
490
+
491
+ #
492
+ # Compute a target state for an edge in the DFA, and attempt to add the
493
+ # computed state and corresponding edge to the DFA.
494
+ #
495
+ # @param dfa The DFA
496
+ # @param previousD The current DFA state
497
+ # @param t The next input symbol
498
+ #
499
+ # @return The computed target DFA state for the given input symbol
500
+ # {@code t}. If {@code t} does not lead to a valid DFA state, this method
501
+ # returns {@link #ERROR}.
502
+ #
503
+ def computeTargetState(self, dfa:DFA, previousD:DFAState, t:int):
504
+ reach = self.computeReachSet(previousD.configs, t, False)
505
+ if reach is None:
506
+ self.addDFAEdge(dfa, previousD, t, self.ERROR)
507
+ return self.ERROR
508
+
509
+ # create new target state; we'll add to DFA after it's complete
510
+ D = DFAState(configs=reach)
511
+
512
+ predictedAlt = self.getUniqueAlt(reach)
513
+
514
+ if ParserATNSimulator.debug:
515
+ altSubSets = PredictionMode.getConflictingAltSubsets(reach)
516
+ print("SLL altSubSets=" + str(altSubSets) + ", configs=" + str(reach) +
517
+ ", predict=" + str(predictedAlt) + ", allSubsetsConflict=" +
518
+ str(PredictionMode.allSubsetsConflict(altSubSets)) + ", conflictingAlts=" +
519
+ str(self.getConflictingAlts(reach)))
520
+
521
+ if predictedAlt!=ATN.INVALID_ALT_NUMBER:
522
+ # NO CONFLICT, UNIQUELY PREDICTED ALT
523
+ D.isAcceptState = True
524
+ D.configs.uniqueAlt = predictedAlt
525
+ D.prediction = predictedAlt
526
+ elif PredictionMode.hasSLLConflictTerminatingPrediction(self.predictionMode, reach):
527
+ # MORE THAN ONE VIABLE ALTERNATIVE
528
+ D.configs.conflictingAlts = self.getConflictingAlts(reach)
529
+ D.requiresFullContext = True
530
+ # in SLL-only mode, we will stop at this state and return the minimum alt
531
+ D.isAcceptState = True
532
+ D.prediction = min(D.configs.conflictingAlts)
533
+
534
+ if D.isAcceptState and D.configs.hasSemanticContext:
535
+ self.predicateDFAState(D, self.atn.getDecisionState(dfa.decision))
536
+ if D.predicates is not None:
537
+ D.prediction = ATN.INVALID_ALT_NUMBER
538
+
539
+ # all adds to dfa are done after we've created full D state
540
+ D = self.addDFAEdge(dfa, previousD, t, D)
541
+ return D
542
+
543
+ def predicateDFAState(self, dfaState:DFAState, decisionState:DecisionState):
544
+ # We need to test all predicates, even in DFA states that
545
+ # uniquely predict alternative.
546
+ nalts = len(decisionState.transitions)
547
+ # Update DFA so reach becomes accept state with (predicate,alt)
548
+ # pairs if preds found for conflicting alts
549
+ altsToCollectPredsFrom = self.getConflictingAltsOrUniqueAlt(dfaState.configs)
550
+ altToPred = self.getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState.configs, nalts)
551
+ if altToPred is not None:
552
+ dfaState.predicates = self.getPredicatePredictions(altsToCollectPredsFrom, altToPred)
553
+ dfaState.prediction = ATN.INVALID_ALT_NUMBER # make sure we use preds
554
+ else:
555
+ # There are preds in configs but they might go away
556
+ # when OR'd together like {p}? || NONE == NONE. If neither
557
+ # alt has preds, resolve to min alt
558
+ dfaState.prediction = min(altsToCollectPredsFrom)
559
+
560
+ # comes back with reach.uniqueAlt set to a valid alt
561
+ def execATNWithFullContext(self, dfa:DFA, D:DFAState, # how far we got before failing over
562
+ s0:ATNConfigSet,
563
+ input:TokenStream,
564
+ startIndex:int,
565
+ outerContext:ParserRuleContext):
566
+ if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim:
567
+ print("execATNWithFullContext", str(s0))
568
+ fullCtx = True
569
+ foundExactAmbig = False
570
+ reach = None
571
+ previous = s0
572
+ input.seek(startIndex)
573
+ t = input.LA(1)
574
+ predictedAlt = -1
575
+ while (True): # while more work
576
+ reach = self.computeReachSet(previous, t, fullCtx)
577
+ if reach is None:
578
+ # if any configs in previous dipped into outer context, that
579
+ # means that input up to t actually finished entry rule
580
+ # at least for LL decision. Full LL doesn't dip into outer
581
+ # so don't need special case.
582
+ # We will get an error no matter what so delay until after
583
+ # decision; better error message. Also, no reachable target
584
+ # ATN states in SLL implies LL will also get nowhere.
585
+ # If conflict in states that dip out, choose min since we
586
+ # will get error no matter what.
587
+ e = self.noViableAlt(input, outerContext, previous, startIndex)
588
+ input.seek(startIndex)
589
+ alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext)
590
+ if alt!=ATN.INVALID_ALT_NUMBER:
591
+ return alt
592
+ else:
593
+ raise e
594
+
595
+ altSubSets = PredictionMode.getConflictingAltSubsets(reach)
596
+ if ParserATNSimulator.debug:
597
+ print("LL altSubSets=" + str(altSubSets) + ", predict=" +
598
+ str(PredictionMode.getUniqueAlt(altSubSets)) + ", resolvesToJustOneViableAlt=" +
599
+ str(PredictionMode.resolvesToJustOneViableAlt(altSubSets)))
600
+
601
+ reach.uniqueAlt = self.getUniqueAlt(reach)
602
+ # unique prediction?
603
+ if reach.uniqueAlt!=ATN.INVALID_ALT_NUMBER:
604
+ predictedAlt = reach.uniqueAlt
605
+ break
606
+ elif self.predictionMode is not PredictionMode.LL_EXACT_AMBIG_DETECTION:
607
+ predictedAlt = PredictionMode.resolvesToJustOneViableAlt(altSubSets)
608
+ if predictedAlt != ATN.INVALID_ALT_NUMBER:
609
+ break
610
+ else:
611
+ # In exact ambiguity mode, we never try to terminate early.
612
+ # Just keeps scarfing until we know what the conflict is
613
+ if PredictionMode.allSubsetsConflict(altSubSets) and PredictionMode.allSubsetsEqual(altSubSets):
614
+ foundExactAmbig = True
615
+ predictedAlt = PredictionMode.getSingleViableAlt(altSubSets)
616
+ break
617
+ # else there are multiple non-conflicting subsets or
618
+ # we're not sure what the ambiguity is yet.
619
+ # So, keep going.
620
+
621
+ previous = reach
622
+ if t != Token.EOF:
623
+ input.consume()
624
+ t = input.LA(1)
625
+
626
+ # If the configuration set uniquely predicts an alternative,
627
+ # without conflict, then we know that it's a full LL decision
628
+ # not SLL.
629
+ if reach.uniqueAlt != ATN.INVALID_ALT_NUMBER :
630
+ self.reportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.index)
631
+ return predictedAlt
632
+
633
+ # We do not check predicates here because we have checked them
634
+ # on-the-fly when doing full context prediction.
635
+
636
+ #
637
+ # In non-exact ambiguity detection mode, we might actually be able to
638
+ # detect an exact ambiguity, but I'm not going to spend the cycles
639
+ # needed to check. We only emit ambiguity warnings in exact ambiguity
640
+ # mode.
641
+ #
642
+ # For example, we might know that we have conflicting configurations.
643
+ # But, that does not mean that there is no way forward without a
644
+ # conflict. It's possible to have nonconflicting alt subsets as in:
645
+
646
+ # altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]
647
+
648
+ # from
649
+ #
650
+ # [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
651
+ # (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]
652
+ #
653
+ # In this case, (17,1,[5 $]) indicates there is some next sequence that
654
+ # would resolve this without conflict to alternative 1. Any other viable
655
+ # next sequence, however, is associated with a conflict. We stop
656
+ # looking for input because no amount of further lookahead will alter
657
+ # the fact that we should predict alternative 1. We just can't say for
658
+ # sure that there is an ambiguity without looking further.
659
+
660
+ self.reportAmbiguity(dfa, D, startIndex, input.index, foundExactAmbig, None, reach)
661
+
662
+ return predictedAlt
663
+
664
+ def computeReachSet(self, closure:ATNConfigSet, t:int, fullCtx:bool):
665
+ if ParserATNSimulator.debug:
666
+ print("in computeReachSet, starting closure: " + str(closure))
667
+
668
+ if self.mergeCache is None:
669
+ self.mergeCache = dict()
670
+
671
+ intermediate = ATNConfigSet(fullCtx)
672
+
673
+ # Configurations already in a rule stop state indicate reaching the end
674
+ # of the decision rule (local context) or end of the start rule (full
675
+ # context). Once reached, these configurations are never updated by a
676
+ # closure operation, so they are handled separately for the performance
677
+ # advantage of having a smaller intermediate set when calling closure.
678
+ #
679
+ # For full-context reach operations, separate handling is required to
680
+ # ensure that the alternative matching the longest overall sequence is
681
+ # chosen when multiple such configurations can match the input.
682
+
683
+ skippedStopStates = None
684
+
685
+ # First figure out where we can reach on input t
686
+ for c in closure:
687
+ if ParserATNSimulator.debug:
688
+ print("testing " + self.getTokenName(t) + " at " + str(c))
689
+
690
+ if isinstance(c.state, RuleStopState):
691
+ if fullCtx or t == Token.EOF:
692
+ if skippedStopStates is None:
693
+ skippedStopStates = list()
694
+ skippedStopStates.append(c)
695
+ continue
696
+
697
+ for trans in c.state.transitions:
698
+ target = self.getReachableTarget(trans, t)
699
+ if target is not None:
700
+ intermediate.add(ATNConfig(state=target, config=c), self.mergeCache)
701
+
702
+ # Now figure out where the reach operation can take us...
703
+
704
+ reach = None
705
+
706
+ # This block optimizes the reach operation for intermediate sets which
707
+ # trivially indicate a termination state for the overall
708
+ # adaptivePredict operation.
709
+ #
710
+ # The conditions assume that intermediate
711
+ # contains all configurations relevant to the reach set, but this
712
+ # condition is not true when one or more configurations have been
713
+ # withheld in skippedStopStates, or when the current symbol is EOF.
714
+ #
715
+ if skippedStopStates is None and t!=Token.EOF:
716
+ if len(intermediate)==1:
717
+ # Don't pursue the closure if there is just one state.
718
+ # It can only have one alternative; just add to result
719
+ # Also don't pursue the closure if there is unique alternative
720
+ # among the configurations.
721
+ reach = intermediate
722
+ elif self.getUniqueAlt(intermediate)!=ATN.INVALID_ALT_NUMBER:
723
+ # Also don't pursue the closure if there is unique alternative
724
+ # among the configurations.
725
+ reach = intermediate
726
+
727
+ # If the reach set could not be trivially determined, perform a closure
728
+ # operation on the intermediate set to compute its initial value.
729
+ #
730
+ if reach is None:
731
+ reach = ATNConfigSet(fullCtx)
732
+ closureBusy = set()
733
+ treatEofAsEpsilon = t == Token.EOF
734
+ for c in intermediate:
735
+ self.closure(c, reach, closureBusy, False, fullCtx, treatEofAsEpsilon)
736
+
737
+ if t == Token.EOF:
738
+ # After consuming EOF no additional input is possible, so we are
739
+ # only interested in configurations which reached the end of the
740
+ # decision rule (local context) or end of the start rule (full
741
+ # context). Update reach to contain only these configurations. This
742
+ # handles both explicit EOF transitions in the grammar and implicit
743
+ # EOF transitions following the end of the decision or start rule.
744
+ #
745
+ # When reach==intermediate, no closure operation was performed. In
746
+ # this case, removeAllConfigsNotInRuleStopState needs to check for
747
+ # reachable rule stop states as well as configurations already in
748
+ # a rule stop state.
749
+ #
750
+ # This is handled before the configurations in skippedStopStates,
751
+ # because any configurations potentially added from that list are
752
+ # already guaranteed to meet this condition whether or not it's
753
+ # required.
754
+ #
755
+ reach = self.removeAllConfigsNotInRuleStopState(reach, reach is intermediate)
756
+
757
+ # If skippedStopStates is not null, then it contains at least one
758
+ # configuration. For full-context reach operations, these
759
+ # configurations reached the end of the start rule, in which case we
760
+ # only add them back to reach if no configuration during the current
761
+ # closure operation reached such a state. This ensures adaptivePredict
762
+ # chooses an alternative matching the longest overall sequence when
763
+ # multiple alternatives are viable.
764
+ #
765
+ if skippedStopStates is not None and ( (not fullCtx) or (not PredictionMode.hasConfigInRuleStopState(reach))):
766
+ for c in skippedStopStates:
767
+ reach.add(c, self.mergeCache)
768
+
769
+ if ParserATNSimulator.trace_atn_sim:
770
+ print("computeReachSet", str(closure), "->", reach)
771
+
772
+ if len(reach)==0:
773
+ return None
774
+ else:
775
+ return reach
776
+
777
+ #
778
+ # Return a configuration set containing only the configurations from
779
+ # {@code configs} which are in a {@link RuleStopState}. If all
780
+ # configurations in {@code configs} are already in a rule stop state, this
781
+ # method simply returns {@code configs}.
782
+ #
783
+ # <p>When {@code lookToEndOfRule} is true, this method uses
784
+ # {@link ATN#nextTokens} for each configuration in {@code configs} which is
785
+ # not already in a rule stop state to see if a rule stop state is reachable
786
+ # from the configuration via epsilon-only transitions.</p>
787
+ #
788
+ # @param configs the configuration set to update
789
+ # @param lookToEndOfRule when true, this method checks for rule stop states
790
+ # reachable by epsilon-only transitions from each configuration in
791
+ # {@code configs}.
792
+ #
793
+ # @return {@code configs} if all configurations in {@code configs} are in a
794
+ # rule stop state, otherwise return a new configuration set containing only
795
+ # the configurations from {@code configs} which are in a rule stop state
796
+ #
797
+ def removeAllConfigsNotInRuleStopState(self, configs:ATNConfigSet, lookToEndOfRule:bool):
798
+ if PredictionMode.allConfigsInRuleStopStates(configs):
799
+ return configs
800
+ result = ATNConfigSet(configs.fullCtx)
801
+ for config in configs:
802
+ if isinstance(config.state, RuleStopState):
803
+ result.add(config, self.mergeCache)
804
+ continue
805
+ if lookToEndOfRule and config.state.epsilonOnlyTransitions:
806
+ nextTokens = self.atn.nextTokens(config.state)
807
+ if Token.EPSILON in nextTokens:
808
+ endOfRuleState = self.atn.ruleToStopState[config.state.ruleIndex]
809
+ result.add(ATNConfig(state=endOfRuleState, config=config), self.mergeCache)
810
+ return result
811
+
812
+ def computeStartState(self, p:ATNState, ctx:RuleContext, fullCtx:bool):
813
+ # always at least the implicit call to start rule
814
+ initialContext = PredictionContextFromRuleContext(self.atn, ctx)
815
+ configs = ATNConfigSet(fullCtx)
816
+
817
+ if ParserATNSimulator.trace_atn_sim:
818
+ print("computeStartState from ATN state "+str(p)+
819
+ " initialContext="+str(initialContext))
820
+
821
+ for i in range(0, len(p.transitions)):
822
+ target = p.transitions[i].target
823
+ c = ATNConfig(target, i+1, initialContext)
824
+ closureBusy = set()
825
+ self.closure(c, configs, closureBusy, True, fullCtx, False)
826
+ return configs
827
+
828
+ #
829
+ # This method transforms the start state computed by
830
+ # {@link #computeStartState} to the special start state used by a
831
+ # precedence DFA for a particular precedence value. The transformation
832
+ # process applies the following changes to the start state's configuration
833
+ # set.
834
+ #
835
+ # <ol>
836
+ # <li>Evaluate the precedence predicates for each configuration using
837
+ # {@link SemanticContext#evalPrecedence}.</li>
838
+ # <li>Remove all configurations which predict an alternative greater than
839
+ # 1, for which another configuration that predicts alternative 1 is in the
840
+ # same ATN state with the same prediction context. This transformation is
841
+ # valid for the following reasons:
842
+ # <ul>
843
+ # <li>The closure block cannot contain any epsilon transitions which bypass
844
+ # the body of the closure, so all states reachable via alternative 1 are
845
+ # part of the precedence alternatives of the transformed left-recursive
846
+ # rule.</li>
847
+ # <li>The "primary" portion of a left recursive rule cannot contain an
848
+ # epsilon transition, so the only way an alternative other than 1 can exist
849
+ # in a state that is also reachable via alternative 1 is by nesting calls
850
+ # to the left-recursive rule, with the outer calls not being at the
851
+ # preferred precedence level.</li>
852
+ # </ul>
853
+ # </li>
854
+ # </ol>
855
+ #
856
+ # <p>
857
+ # The prediction context must be considered by this filter to address
858
+ # situations like the following.
859
+ # </p>
860
+ # <code>
861
+ # <pre>
862
+ # grammar TA;
863
+ # prog: statement* EOF;
864
+ # statement: letterA | statement letterA 'b' ;
865
+ # letterA: 'a';
866
+ # </pre>
867
+ # </code>
868
+ # <p>
869
+ # If the above grammar, the ATN state immediately before the token
870
+ # reference {@code 'a'} in {@code letterA} is reachable from the left edge
871
+ # of both the primary and closure blocks of the left-recursive rule
872
+ # {@code statement}. The prediction context associated with each of these
873
+ # configurations distinguishes between them, and prevents the alternative
874
+ # which stepped out to {@code prog} (and then back in to {@code statement}
875
+ # from being eliminated by the filter.
876
+ # </p>
877
+ #
878
+ # @param configs The configuration set computed by
879
+ # {@link #computeStartState} as the start state for the DFA.
880
+ # @return The transformed configuration set representing the start state
881
+ # for a precedence DFA at a particular precedence level (determined by
882
+ # calling {@link Parser#getPrecedence}).
883
+ #
884
+ def applyPrecedenceFilter(self, configs:ATNConfigSet):
885
+ statesFromAlt1 = dict()
886
+ configSet = ATNConfigSet(configs.fullCtx)
887
+ for config in configs:
888
+ # handle alt 1 first
889
+ if config.alt != 1:
890
+ continue
891
+ updatedContext = config.semanticContext.evalPrecedence(self.parser, self._outerContext)
892
+ if updatedContext is None:
893
+ # the configuration was eliminated
894
+ continue
895
+
896
+ statesFromAlt1[config.state.stateNumber] = config.context
897
+ if updatedContext is not config.semanticContext:
898
+ configSet.add(ATNConfig(config=config, semantic=updatedContext), self.mergeCache)
899
+ else:
900
+ configSet.add(config, self.mergeCache)
901
+
902
+ for config in configs:
903
+ if config.alt == 1:
904
+ # already handled
905
+ continue
906
+
907
+ # In the future, this elimination step could be updated to also
908
+ # filter the prediction context for alternatives predicting alt>1
909
+ # (basically a graph subtraction algorithm).
910
+ #
911
+ if not config.precedenceFilterSuppressed:
912
+ context = statesFromAlt1.get(config.state.stateNumber, None)
913
+ if context==config.context:
914
+ # eliminated
915
+ continue
916
+
917
+ configSet.add(config, self.mergeCache)
918
+
919
+ return configSet
920
+
921
+ def getReachableTarget(self, trans:Transition, ttype:int):
922
+ if trans.matches(ttype, 0, self.atn.maxTokenType):
923
+ return trans.target
924
+ else:
925
+ return None
926
+
927
+ def getPredsForAmbigAlts(self, ambigAlts:set, configs:ATNConfigSet, nalts:int):
928
+ # REACH=[1|1|[]|0:0, 1|2|[]|0:1]
929
+ # altToPred starts as an array of all null contexts. The entry at index i
930
+ # corresponds to alternative i. altToPred[i] may have one of three values:
931
+ # 1. null: no ATNConfig c is found such that c.alt==i
932
+ # 2. SemanticContext.NONE: At least one ATNConfig c exists such that
933
+ # c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
934
+ # alt i has at least one unpredicated config.
935
+ # 3. Non-NONE Semantic Context: There exists at least one, and for all
936
+ # ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
937
+ #
938
+ # From this, it is clear that NONE||anything==NONE.
939
+ #
940
+ altToPred = [None] * (nalts + 1)
941
+ for c in configs:
942
+ if c.alt in ambigAlts:
943
+ altToPred[c.alt] = orContext(altToPred[c.alt], c.semanticContext)
944
+
945
+ nPredAlts = 0
946
+ for i in range(1, nalts+1):
947
+ if altToPred[i] is None:
948
+ altToPred[i] = SemanticContext.NONE
949
+ elif altToPred[i] is not SemanticContext.NONE:
950
+ nPredAlts += 1
951
+
952
+ # nonambig alts are null in altToPred
953
+ if nPredAlts==0:
954
+ altToPred = None
955
+ if ParserATNSimulator.debug:
956
+ print("getPredsForAmbigAlts result " + str_list(altToPred))
957
+ return altToPred
958
+
959
+ def getPredicatePredictions(self, ambigAlts:set, altToPred:list):
960
+ pairs = []
961
+ containsPredicate = False
962
+ for i in range(1, len(altToPred)):
963
+ pred = altToPred[i]
964
+ # unpredicated is indicated by SemanticContext.NONE
965
+ if ambigAlts is not None and i in ambigAlts:
966
+ pairs.append(PredPrediction(pred, i))
967
+ if pred is not SemanticContext.NONE:
968
+ containsPredicate = True
969
+
970
+ if not containsPredicate:
971
+ return None
972
+
973
+ return pairs
974
+
975
+ #
976
+ # This method is used to improve the localization of error messages by
977
+ # choosing an alternative rather than throwing a
978
+ # {@link NoViableAltException} in particular prediction scenarios where the
979
+ # {@link #ERROR} state was reached during ATN simulation.
980
+ #
981
+ # <p>
982
+ # The default implementation of this method uses the following
983
+ # algorithm to identify an ATN configuration which successfully parsed the
984
+ # decision entry rule. Choosing such an alternative ensures that the
985
+ # {@link ParserRuleContext} returned by the calling rule will be complete
986
+ # and valid, and the syntax error will be reported later at a more
987
+ # localized location.</p>
988
+ #
989
+ # <ul>
990
+ # <li>If a syntactically valid path or paths reach the end of the decision rule and
991
+ # they are semantically valid if predicated, return the min associated alt.</li>
992
+ # <li>Else, if a semantically invalid but syntactically valid path exist
993
+ # or paths exist, return the minimum associated alt.
994
+ # </li>
995
+ # <li>Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.</li>
996
+ # </ul>
997
+ #
998
+ # <p>
999
+ # In some scenarios, the algorithm described above could predict an
1000
+ # alternative which will result in a {@link FailedPredicateException} in
1001
+ # the parser. Specifically, this could occur if the <em>only</em> configuration
1002
+ # capable of successfully parsing to the end of the decision rule is
1003
+ # blocked by a semantic predicate. By choosing this alternative within
1004
+ # {@link #adaptivePredict} instead of throwing a
1005
+ # {@link NoViableAltException}, the resulting
1006
+ # {@link FailedPredicateException} in the parser will identify the specific
1007
+ # predicate which is preventing the parser from successfully parsing the
1008
+ # decision rule, which helps developers identify and correct logic errors
1009
+ # in semantic predicates.
1010
+ # </p>
1011
+ #
1012
+ # @param configs The ATN configurations which were valid immediately before
1013
+ # the {@link #ERROR} state was reached
1014
+ # @param outerContext The is the \gamma_0 initial parser context from the paper
1015
+ # or the parser stack at the instant before prediction commences.
1016
+ #
1017
+ # @return The value to return from {@link #adaptivePredict}, or
1018
+ # {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not
1019
+ # identified and {@link #adaptivePredict} should report an error instead.
1020
+ #
1021
+ def getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(self, configs:ATNConfigSet, outerContext:ParserRuleContext):
1022
+ semValidConfigs, semInvalidConfigs = self.splitAccordingToSemanticValidity(configs, outerContext)
1023
+ alt = self.getAltThatFinishedDecisionEntryRule(semValidConfigs)
1024
+ if alt!=ATN.INVALID_ALT_NUMBER: # semantically/syntactically viable path exists
1025
+ return alt
1026
+ # Is there a syntactically valid path with a failed pred?
1027
+ if len(semInvalidConfigs)>0:
1028
+ alt = self.getAltThatFinishedDecisionEntryRule(semInvalidConfigs)
1029
+ if alt!=ATN.INVALID_ALT_NUMBER: # syntactically viable path exists
1030
+ return alt
1031
+ return ATN.INVALID_ALT_NUMBER
1032
+
1033
+ def getAltThatFinishedDecisionEntryRule(self, configs:ATNConfigSet):
1034
+ alts = set()
1035
+ for c in configs:
1036
+ if c.reachesIntoOuterContext>0 or (isinstance(c.state, RuleStopState) and c.context.hasEmptyPath() ):
1037
+ alts.add(c.alt)
1038
+ if len(alts)==0:
1039
+ return ATN.INVALID_ALT_NUMBER
1040
+ else:
1041
+ return min(alts)
1042
+
1043
+ # Walk the list of configurations and split them according to
1044
+ # those that have preds evaluating to true/false. If no pred, assume
1045
+ # true pred and include in succeeded set. Returns Pair of sets.
1046
+ #
1047
+ # Create a new set so as not to alter the incoming parameter.
1048
+ #
1049
+ # Assumption: the input stream has been restored to the starting point
1050
+ # prediction, which is where predicates need to evaluate.
1051
+ #
1052
+ def splitAccordingToSemanticValidity(self, configs:ATNConfigSet, outerContext:ParserRuleContext):
1053
+ succeeded = ATNConfigSet(configs.fullCtx)
1054
+ failed = ATNConfigSet(configs.fullCtx)
1055
+ for c in configs:
1056
+ if c.semanticContext is not SemanticContext.NONE:
1057
+ predicateEvaluationResult = c.semanticContext.eval(self.parser, outerContext)
1058
+ if predicateEvaluationResult:
1059
+ succeeded.add(c)
1060
+ else:
1061
+ failed.add(c)
1062
+ else:
1063
+ succeeded.add(c)
1064
+ return (succeeded,failed)
1065
+
1066
+ # Look through a list of predicate/alt pairs, returning alts for the
1067
+ # pairs that win. A {@code NONE} predicate indicates an alt containing an
1068
+ # unpredicated config which behaves as "always true." If !complete
1069
+ # then we stop at the first predicate that evaluates to true. This
1070
+ # includes pairs with null predicates.
1071
+ #
1072
+ def evalSemanticContext(self, predPredictions:list, outerContext:ParserRuleContext, complete:bool):
1073
+ predictions = set()
1074
+ for pair in predPredictions:
1075
+ if pair.pred is SemanticContext.NONE:
1076
+ predictions.add(pair.alt)
1077
+ if not complete:
1078
+ break
1079
+ continue
1080
+ predicateEvaluationResult = pair.pred.eval(self.parser, outerContext)
1081
+ if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug:
1082
+ print("eval pred " + str(pair) + "=" + str(predicateEvaluationResult))
1083
+
1084
+ if predicateEvaluationResult:
1085
+ if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug:
1086
+ print("PREDICT " + str(pair.alt))
1087
+ predictions.add(pair.alt)
1088
+ if not complete:
1089
+ break
1090
+ return predictions
1091
+
1092
+
1093
+ # TODO: If we are doing predicates, there is no point in pursuing
1094
+ # closure operations if we reach a DFA state that uniquely predicts
1095
+ # alternative. We will not be caching that DFA state and it is a
1096
+ # waste to pursue the closure. Might have to advance when we do
1097
+ # ambig detection thought :(
1098
+ #
1099
+
1100
+ def closure(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, treatEofAsEpsilon:bool):
1101
+ initialDepth = 0
1102
+ self.closureCheckingStopState(config, configs, closureBusy, collectPredicates,
1103
+ fullCtx, initialDepth, treatEofAsEpsilon)
1104
+
1105
+
1106
+ def closureCheckingStopState(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, depth:int, treatEofAsEpsilon:bool):
1107
+ if ParserATNSimulator.trace_atn_sim:
1108
+ print("closure(" + str(config) + ")")
1109
+
1110
+ if isinstance(config.state, RuleStopState):
1111
+ # We hit rule end. If we have context info, use it
1112
+ # run thru all possible stack tops in ctx
1113
+ if not config.context.isEmpty():
1114
+ for i in range(0, len(config.context)):
1115
+ state = config.context.getReturnState(i)
1116
+ if state is PredictionContext.EMPTY_RETURN_STATE:
1117
+ if fullCtx:
1118
+ configs.add(ATNConfig(state=config.state, context=PredictionContext.EMPTY, config=config), self.mergeCache)
1119
+ continue
1120
+ else:
1121
+ # we have no context info, just chase follow links (if greedy)
1122
+ if ParserATNSimulator.debug:
1123
+ print("FALLING off rule " + self.getRuleName(config.state.ruleIndex))
1124
+ self.closure_(config, configs, closureBusy, collectPredicates,
1125
+ fullCtx, depth, treatEofAsEpsilon)
1126
+ continue
1127
+ returnState = self.atn.states[state]
1128
+ newContext = config.context.getParent(i) # "pop" return state
1129
+ c = ATNConfig(state=returnState, alt=config.alt, context=newContext, semantic=config.semanticContext)
1130
+ # While we have context to pop back from, we may have
1131
+ # gotten that context AFTER having falling off a rule.
1132
+ # Make sure we track that we are now out of context.
1133
+ c.reachesIntoOuterContext = config.reachesIntoOuterContext
1134
+ self.closureCheckingStopState(c, configs, closureBusy, collectPredicates, fullCtx, depth - 1, treatEofAsEpsilon)
1135
+ return
1136
+ elif fullCtx:
1137
+ # reached end of start rule
1138
+ configs.add(config, self.mergeCache)
1139
+ return
1140
+ else:
1141
+ # else if we have no context info, just chase follow links (if greedy)
1142
+ if ParserATNSimulator.debug:
1143
+ print("FALLING off rule " + self.getRuleName(config.state.ruleIndex))
1144
+
1145
+ self.closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEofAsEpsilon)
1146
+
1147
+ # Do the actual work of walking epsilon edges#
1148
+ def closure_(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, depth:int, treatEofAsEpsilon:bool):
1149
+ p = config.state
1150
+ # optimization
1151
+ if not p.epsilonOnlyTransitions:
1152
+ configs.add(config, self.mergeCache)
1153
+ # make sure to not return here, because EOF transitions can act as
1154
+ # both epsilon transitions and non-epsilon transitions.
1155
+
1156
+ first = True
1157
+ for t in p.transitions:
1158
+ if first:
1159
+ first = False
1160
+ if self.canDropLoopEntryEdgeInLeftRecursiveRule(config):
1161
+ continue
1162
+
1163
+ continueCollecting = collectPredicates and not isinstance(t, ActionTransition)
1164
+ c = self.getEpsilonTarget(config, t, continueCollecting, depth == 0, fullCtx, treatEofAsEpsilon)
1165
+ if c is not None:
1166
+ newDepth = depth
1167
+ if isinstance( config.state, RuleStopState):
1168
+ # target fell off end of rule; mark resulting c as having dipped into outer context
1169
+ # We can't get here if incoming config was rule stop and we had context
1170
+ # track how far we dip into outer context. Might
1171
+ # come in handy and we avoid evaluating context dependent
1172
+ # preds if this is > 0.
1173
+ if self._dfa is not None and self._dfa.precedenceDfa:
1174
+ if t.outermostPrecedenceReturn == self._dfa.atnStartState.ruleIndex:
1175
+ c.precedenceFilterSuppressed = True
1176
+ c.reachesIntoOuterContext += 1
1177
+ if c in closureBusy:
1178
+ # avoid infinite recursion for right-recursive rules
1179
+ continue
1180
+ closureBusy.add(c)
1181
+ configs.dipsIntoOuterContext = True # TODO: can remove? only care when we add to set per middle of this method
1182
+ newDepth -= 1
1183
+ if ParserATNSimulator.debug:
1184
+ print("dips into outer ctx: " + str(c))
1185
+ else:
1186
+ if not t.isEpsilon:
1187
+ if c in closureBusy:
1188
+ # avoid infinite recursion for EOF* and EOF+
1189
+ continue
1190
+ closureBusy.add(c)
1191
+ if isinstance(t, RuleTransition):
1192
+ # latch when newDepth goes negative - once we step out of the entry context we can't return
1193
+ if newDepth >= 0:
1194
+ newDepth += 1
1195
+
1196
+ self.closureCheckingStopState(c, configs, closureBusy, continueCollecting, fullCtx, newDepth, treatEofAsEpsilon)
1197
+
1198
+
1199
+
1200
+ # Implements first-edge (loop entry) elimination as an optimization
1201
+ # during closure operations. See antlr/antlr4#1398.
1202
+ #
1203
+ # The optimization is to avoid adding the loop entry config when
1204
+ # the exit path can only lead back to the same
1205
+ # StarLoopEntryState after popping context at the rule end state
1206
+ # (traversing only epsilon edges, so we're still in closure, in
1207
+ # this same rule).
1208
+ #
1209
+ # We need to detect any state that can reach loop entry on
1210
+ # epsilon w/o exiting rule. We don't have to look at FOLLOW
1211
+ # links, just ensure that all stack tops for config refer to key
1212
+ # states in LR rule.
1213
+ #
1214
+ # To verify we are in the right situation we must first check
1215
+ # closure is at a StarLoopEntryState generated during LR removal.
1216
+ # Then we check that each stack top of context is a return state
1217
+ # from one of these cases:
1218
+ #
1219
+ # 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state
1220
+ # 2. expr op expr. The return state is the block end of internal block of (...)*
1221
+ # 3. 'between' expr 'and' expr. The return state of 2nd expr reference.
1222
+ # That state points at block end of internal block of (...)*.
1223
+ # 4. expr '?' expr ':' expr. The return state points at block end,
1224
+ # which points at loop entry state.
1225
+ #
1226
+ # If any is true for each stack top, then closure does not add a
1227
+ # config to the current config set for edge[0], the loop entry branch.
1228
+ #
1229
+ # Conditions fail if any context for the current config is:
1230
+ #
1231
+ # a. empty (we'd fall out of expr to do a global FOLLOW which could
1232
+ # even be to some weird spot in expr) or,
1233
+ # b. lies outside of expr or,
1234
+ # c. lies within expr but at a state not the BlockEndState
1235
+ # generated during LR removal
1236
+ #
1237
+ # Do we need to evaluate predicates ever in closure for this case?
1238
+ #
1239
+ # No. Predicates, including precedence predicates, are only
1240
+ # evaluated when computing a DFA start state. I.e., only before
1241
+ # the lookahead (but not parser) consumes a token.
1242
+ #
1243
+ # There are no epsilon edges allowed in LR rule alt blocks or in
1244
+ # the "primary" part (ID here). If closure is in
1245
+ # StarLoopEntryState any lookahead operation will have consumed a
1246
+ # token as there are no epsilon-paths that lead to
1247
+ # StarLoopEntryState. We do not have to evaluate predicates
1248
+ # therefore if we are in the generated StarLoopEntryState of a LR
1249
+ # rule. Note that when making a prediction starting at that
1250
+ # decision point, decision d=2, compute-start-state performs
1251
+ # closure starting at edges[0], edges[1] emanating from
1252
+ # StarLoopEntryState. That means it is not performing closure on
1253
+ # StarLoopEntryState during compute-start-state.
1254
+ #
1255
+ # How do we know this always gives same prediction answer?
1256
+ #
1257
+ # Without predicates, loop entry and exit paths are ambiguous
1258
+ # upon remaining input +b (in, say, a+b). Either paths lead to
1259
+ # valid parses. Closure can lead to consuming + immediately or by
1260
+ # falling out of this call to expr back into expr and loop back
1261
+ # again to StarLoopEntryState to match +b. In this special case,
1262
+ # we choose the more efficient path, which is to take the bypass
1263
+ # path.
1264
+ #
1265
+ # The lookahead language has not changed because closure chooses
1266
+ # one path over the other. Both paths lead to consuming the same
1267
+ # remaining input during a lookahead operation. If the next token
1268
+ # is an operator, lookahead will enter the choice block with
1269
+ # operators. If it is not, lookahead will exit expr. Same as if
1270
+ # closure had chosen to enter the choice block immediately.
1271
+ #
1272
+ # Closure is examining one config (some loopentrystate, some alt,
1273
+ # context) which means it is considering exactly one alt. Closure
1274
+ # always copies the same alt to any derived configs.
1275
+ #
1276
+ # How do we know this optimization doesn't mess up precedence in
1277
+ # our parse trees?
1278
+ #
1279
+ # Looking through expr from left edge of stat only has to confirm
1280
+ # that an input, say, a+b+c; begins with any valid interpretation
1281
+ # of an expression. The precedence actually doesn't matter when
1282
+ # making a decision in stat seeing through expr. It is only when
1283
+ # parsing rule expr that we must use the precedence to get the
1284
+ # right interpretation and, hence, parse tree.
1285
+ #
1286
+ # @since 4.6
1287
+ #
1288
+ def canDropLoopEntryEdgeInLeftRecursiveRule(self, config):
1289
+ # return False
1290
+ p = config.state
1291
+ # First check to see if we are in StarLoopEntryState generated during
1292
+ # left-recursion elimination. For efficiency, also check if
1293
+ # the context has an empty stack case. If so, it would mean
1294
+ # global FOLLOW so we can't perform optimization
1295
+ # Are we the special loop entry/exit state? or SLL wildcard
1296
+ if p.stateType != ATNState.STAR_LOOP_ENTRY \
1297
+ or not p.isPrecedenceDecision \
1298
+ or config.context.isEmpty() \
1299
+ or config.context.hasEmptyPath():
1300
+ return False
1301
+
1302
+ # Require all return states to return back to the same rule
1303
+ # that p is in.
1304
+ numCtxs = len(config.context)
1305
+ for i in range(0, numCtxs): # for each stack context
1306
+ returnState = self.atn.states[config.context.getReturnState(i)]
1307
+ if returnState.ruleIndex != p.ruleIndex:
1308
+ return False
1309
+
1310
+ decisionStartState = p.transitions[0].target
1311
+ blockEndStateNum = decisionStartState.endState.stateNumber
1312
+ blockEndState = self.atn.states[blockEndStateNum]
1313
+
1314
+ # Verify that the top of each stack context leads to loop entry/exit
1315
+ # state through epsilon edges and w/o leaving rule.
1316
+ for i in range(0, numCtxs): # for each stack context
1317
+ returnStateNumber = config.context.getReturnState(i)
1318
+ returnState = self.atn.states[returnStateNumber]
1319
+ # all states must have single outgoing epsilon edge
1320
+ if len(returnState.transitions) != 1 or not returnState.transitions[0].isEpsilon:
1321
+ return False
1322
+
1323
+ # Look for prefix op case like 'not expr', (' type ')' expr
1324
+ returnStateTarget = returnState.transitions[0].target
1325
+ if returnState.stateType == ATNState.BLOCK_END and returnStateTarget is p:
1326
+ continue
1327
+
1328
+ # Look for 'expr op expr' or case where expr's return state is block end
1329
+ # of (...)* internal block; the block end points to loop back
1330
+ # which points to p but we don't need to check that
1331
+ if returnState is blockEndState:
1332
+ continue
1333
+
1334
+ # Look for ternary expr ? expr : expr. The return state points at block end,
1335
+ # which points at loop entry state
1336
+ if returnStateTarget is blockEndState:
1337
+ continue
1338
+
1339
+ # Look for complex prefix 'between expr and expr' case where 2nd expr's
1340
+ # return state points at block end state of (...)* internal block
1341
+ if returnStateTarget.stateType == ATNState.BLOCK_END \
1342
+ and len(returnStateTarget.transitions) == 1 \
1343
+ and returnStateTarget.transitions[0].isEpsilon \
1344
+ and returnStateTarget.transitions[0].target is p:
1345
+ continue
1346
+
1347
+ # anything else ain't conforming
1348
+ return False
1349
+
1350
+ return True
1351
+
1352
+
1353
+ def getRuleName(self, index:int):
1354
+ if self.parser is not None and index>=0:
1355
+ return self.parser.ruleNames[index]
1356
+ else:
1357
+ return "<rule " + str(index) + ">"
1358
+
1359
+ epsilonTargetMethods = dict()
1360
+ epsilonTargetMethods[Transition.RULE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1361
+ sim.ruleTransition(config, t)
1362
+ epsilonTargetMethods[Transition.PRECEDENCE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1363
+ sim.precedenceTransition(config, t, collectPredicates, inContext, fullCtx)
1364
+ epsilonTargetMethods[Transition.PREDICATE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1365
+ sim.predTransition(config, t, collectPredicates, inContext, fullCtx)
1366
+ epsilonTargetMethods[Transition.ACTION] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1367
+ sim.actionTransition(config, t)
1368
+ epsilonTargetMethods[Transition.EPSILON] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1369
+ ATNConfig(state=t.target, config=config)
1370
+ epsilonTargetMethods[Transition.ATOM] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1371
+ ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
1372
+ epsilonTargetMethods[Transition.RANGE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1373
+ ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
1374
+ epsilonTargetMethods[Transition.SET] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1375
+ ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
1376
+
1377
+ def getEpsilonTarget(self, config:ATNConfig, t:Transition, collectPredicates:bool, inContext:bool, fullCtx:bool, treatEofAsEpsilon:bool):
1378
+ m = self.epsilonTargetMethods.get(t.serializationType, None)
1379
+ if m is None:
1380
+ return None
1381
+ else:
1382
+ return m(self, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon)
1383
+
1384
+ def actionTransition(self, config:ATNConfig, t:ActionTransition):
1385
+ if ParserATNSimulator.debug:
1386
+ print("ACTION edge " + str(t.ruleIndex) + ":" + str(t.actionIndex))
1387
+ return ATNConfig(state=t.target, config=config)
1388
+
1389
+ def precedenceTransition(self, config:ATNConfig, pt:PrecedencePredicateTransition, collectPredicates:bool, inContext:bool, fullCtx:bool):
1390
+ if ParserATNSimulator.debug:
1391
+ print("PRED (collectPredicates=" + str(collectPredicates) + ") " +
1392
+ str(pt.precedence) + ">=_p, ctx dependent=true")
1393
+ if self.parser is not None:
1394
+ print("context surrounding pred is " + str(self.parser.getRuleInvocationStack()))
1395
+
1396
+ c = None
1397
+ if collectPredicates and inContext:
1398
+ if fullCtx:
1399
+ # In full context mode, we can evaluate predicates on-the-fly
1400
+ # during closure, which dramatically reduces the size of
1401
+ # the config sets. It also obviates the need to test predicates
1402
+ # later during conflict resolution.
1403
+ currentPosition = self._input.index
1404
+ self._input.seek(self._startIndex)
1405
+ predSucceeds = pt.getPredicate().eval(self.parser, self._outerContext)
1406
+ self._input.seek(currentPosition)
1407
+ if predSucceeds:
1408
+ c = ATNConfig(state=pt.target, config=config) # no pred context
1409
+ else:
1410
+ newSemCtx = andContext(config.semanticContext, pt.getPredicate())
1411
+ c = ATNConfig(state=pt.target, semantic=newSemCtx, config=config)
1412
+ else:
1413
+ c = ATNConfig(state=pt.target, config=config)
1414
+
1415
+ if ParserATNSimulator.debug:
1416
+ print("config from pred transition=" + str(c))
1417
+ return c
1418
+
1419
+ def predTransition(self, config:ATNConfig, pt:PredicateTransition, collectPredicates:bool, inContext:bool, fullCtx:bool):
1420
+ if ParserATNSimulator.debug:
1421
+ print("PRED (collectPredicates=" + str(collectPredicates) + ") " + str(pt.ruleIndex) +
1422
+ ":" + str(pt.predIndex) + ", ctx dependent=" + str(pt.isCtxDependent))
1423
+ if self.parser is not None:
1424
+ print("context surrounding pred is " + str(self.parser.getRuleInvocationStack()))
1425
+
1426
+ c = None
1427
+ if collectPredicates and (not pt.isCtxDependent or (pt.isCtxDependent and inContext)):
1428
+ if fullCtx:
1429
+ # In full context mode, we can evaluate predicates on-the-fly
1430
+ # during closure, which dramatically reduces the size of
1431
+ # the config sets. It also obviates the need to test predicates
1432
+ # later during conflict resolution.
1433
+ currentPosition = self._input.index
1434
+ self._input.seek(self._startIndex)
1435
+ predSucceeds = pt.getPredicate().eval(self.parser, self._outerContext)
1436
+ self._input.seek(currentPosition)
1437
+ if predSucceeds:
1438
+ c = ATNConfig(state=pt.target, config=config) # no pred context
1439
+ else:
1440
+ newSemCtx = andContext(config.semanticContext, pt.getPredicate())
1441
+ c = ATNConfig(state=pt.target, semantic=newSemCtx, config=config)
1442
+ else:
1443
+ c = ATNConfig(state=pt.target, config=config)
1444
+
1445
+ if ParserATNSimulator.debug:
1446
+ print("config from pred transition=" + str(c))
1447
+ return c
1448
+
1449
+ def ruleTransition(self, config:ATNConfig, t:RuleTransition):
1450
+ if ParserATNSimulator.debug:
1451
+ print("CALL rule " + self.getRuleName(t.target.ruleIndex) + ", ctx=" + str(config.context))
1452
+ returnState = t.followState
1453
+ newContext = SingletonPredictionContext.create(config.context, returnState.stateNumber)
1454
+ return ATNConfig(state=t.target, context=newContext, config=config )
1455
+
1456
+ def getConflictingAlts(self, configs:ATNConfigSet):
1457
+ altsets = PredictionMode.getConflictingAltSubsets(configs)
1458
+ return PredictionMode.getAlts(altsets)
1459
+
1460
+ # Sam pointed out a problem with the previous definition, v3, of
1461
+ # ambiguous states. If we have another state associated with conflicting
1462
+ # alternatives, we should keep going. For example, the following grammar
1463
+ #
1464
+ # s : (ID | ID ID?) ';' ;
1465
+ #
1466
+ # When the ATN simulation reaches the state before ';', it has a DFA
1467
+ # state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
1468
+ # 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
1469
+ # because alternative to has another way to continue, via [6|2|[]].
1470
+ # The key is that we have a single state that has config's only associated
1471
+ # with a single alternative, 2, and crucially the state transitions
1472
+ # among the configurations are all non-epsilon transitions. That means
1473
+ # we don't consider any conflicts that include alternative 2. So, we
1474
+ # ignore the conflict between alts 1 and 2. We ignore a set of
1475
+ # conflicting alts when there is an intersection with an alternative
1476
+ # associated with a single alt state in the state&rarr;config-list map.
1477
+ #
1478
+ # It's also the case that we might have two conflicting configurations but
1479
+ # also a 3rd nonconflicting configuration for a different alternative:
1480
+ # [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:
1481
+ #
1482
+ # a : A | A | A B ;
1483
+ #
1484
+ # After matching input A, we reach the stop state for rule A, state 1.
1485
+ # State 8 is the state right before B. Clearly alternatives 1 and 2
1486
+ # conflict and no amount of further lookahead will separate the two.
1487
+ # However, alternative 3 will be able to continue and so we do not
1488
+ # stop working on this state. In the previous example, we're concerned
1489
+ # with states associated with the conflicting alternatives. Here alt
1490
+ # 3 is not associated with the conflicting configs, but since we can continue
1491
+ # looking for input reasonably, I don't declare the state done. We
1492
+ # ignore a set of conflicting alts when we have an alternative
1493
+ # that we still need to pursue.
1494
+ #
1495
+
1496
+ def getConflictingAltsOrUniqueAlt(self, configs:ATNConfigSet):
1497
+ conflictingAlts = None
1498
+ if configs.uniqueAlt!= ATN.INVALID_ALT_NUMBER:
1499
+ conflictingAlts = set()
1500
+ conflictingAlts.add(configs.uniqueAlt)
1501
+ else:
1502
+ conflictingAlts = configs.conflictingAlts
1503
+ return conflictingAlts
1504
+
1505
+ def getTokenName(self, t:int):
1506
+ if t==Token.EOF:
1507
+ return "EOF"
1508
+ if self.parser is not None and \
1509
+ self.parser.literalNames is not None and \
1510
+ t < len(self.parser.literalNames):
1511
+ return self.parser.literalNames[t] + "<" + str(t) + ">"
1512
+ if self.parser is not None and \
1513
+ self.parser.symbolicNames is not None and \
1514
+ t < len(self.parser.symbolicNames):
1515
+ return self.parser.symbolicNames[t] + "<" + str(t) + ">"
1516
+ else:
1517
+ return str(t)
1518
+
1519
+ def getLookaheadName(self, input:TokenStream):
1520
+ return self.getTokenName(input.LA(1))
1521
+
1522
+ # Used for debugging in adaptivePredict around execATN but I cut
1523
+ # it out for clarity now that alg. works well. We can leave this
1524
+ # "dead" code for a bit.
1525
+ #
1526
+ def dumpDeadEndConfigs(self, nvae:NoViableAltException):
1527
+ print("dead end configs: ")
1528
+ for c in nvae.getDeadEndConfigs():
1529
+ trans = "no edges"
1530
+ if len(c.state.transitions)>0:
1531
+ t = c.state.transitions[0]
1532
+ if isinstance(t, AtomTransition):
1533
+ trans = "Atom "+ self.getTokenName(t.label)
1534
+ elif isinstance(t, SetTransition):
1535
+ neg = isinstance(t, NotSetTransition)
1536
+ trans = ("~" if neg else "")+"Set "+ str(t.set)
1537
+ print(c.toString(self.parser, True) + ":" + trans, file=sys.stderr)
1538
+
1539
+ def noViableAlt(self, input:TokenStream, outerContext:ParserRuleContext, configs:ATNConfigSet, startIndex:int):
1540
+ return NoViableAltException(self.parser, input, input.get(startIndex), input.LT(1), configs, outerContext)
1541
+
1542
+ def getUniqueAlt(self, configs:ATNConfigSet):
1543
+ alt = ATN.INVALID_ALT_NUMBER
1544
+ for c in configs:
1545
+ if alt == ATN.INVALID_ALT_NUMBER:
1546
+ alt = c.alt # found first alt
1547
+ elif c.alt!=alt:
1548
+ return ATN.INVALID_ALT_NUMBER
1549
+ return alt
1550
+
1551
+ #
1552
+ # Add an edge to the DFA, if possible. This method calls
1553
+ # {@link #addDFAState} to ensure the {@code to} state is present in the
1554
+ # DFA. If {@code from} is {@code null}, or if {@code t} is outside the
1555
+ # range of edges that can be represented in the DFA tables, this method
1556
+ # returns without adding the edge to the DFA.
1557
+ #
1558
+ # <p>If {@code to} is {@code null}, this method returns {@code null}.
1559
+ # Otherwise, this method returns the {@link DFAState} returned by calling
1560
+ # {@link #addDFAState} for the {@code to} state.</p>
1561
+ #
1562
+ # @param dfa The DFA
1563
+ # @param from The source state for the edge
1564
+ # @param t The input symbol
1565
+ # @param to The target state for the edge
1566
+ #
1567
+ # @return If {@code to} is {@code null}, this method returns {@code null};
1568
+ # otherwise this method returns the result of calling {@link #addDFAState}
1569
+ # on {@code to}
1570
+ #
1571
+ def addDFAEdge(self, dfa:DFA, from_:DFAState, t:int, to:DFAState):
1572
+ if ParserATNSimulator.debug:
1573
+ print("EDGE " + str(from_) + " -> " + str(to) + " upon " + self.getTokenName(t))
1574
+
1575
+ if to is None:
1576
+ return None
1577
+
1578
+ to = self.addDFAState(dfa, to) # used existing if possible not incoming
1579
+ if from_ is None or t < -1 or t > self.atn.maxTokenType:
1580
+ return to
1581
+
1582
+ if from_.edges is None:
1583
+ from_.edges = [None] * (self.atn.maxTokenType + 2)
1584
+ from_.edges[t+1] = to # connect
1585
+
1586
+ if ParserATNSimulator.debug:
1587
+ names = None if self.parser is None else self.parser.literalNames
1588
+ print("DFA=\n" + dfa.toString(names))
1589
+
1590
+ return to
1591
+
1592
+ #
1593
+ # Add state {@code D} to the DFA if it is not already present, and return
1594
+ # the actual instance stored in the DFA. If a state equivalent to {@code D}
1595
+ # is already in the DFA, the existing state is returned. Otherwise this
1596
+ # method returns {@code D} after adding it to the DFA.
1597
+ #
1598
+ # <p>If {@code D} is {@link #ERROR}, this method returns {@link #ERROR} and
1599
+ # does not change the DFA.</p>
1600
+ #
1601
+ # @param dfa The dfa
1602
+ # @param D The DFA state to add
1603
+ # @return The state stored in the DFA. This will be either the existing
1604
+ # state if {@code D} is already in the DFA, or {@code D} itself if the
1605
+ # state was not already present.
1606
+ #
1607
+ def addDFAState(self, dfa:DFA, D:DFAState):
1608
+ if D is self.ERROR:
1609
+ return D
1610
+
1611
+
1612
+ existing = dfa.states.get(D, None)
1613
+ if existing is not None:
1614
+ if ParserATNSimulator.trace_atn_sim: print("addDFAState", str(D), "exists")
1615
+ return existing
1616
+
1617
+ D.stateNumber = len(dfa.states)
1618
+ if not D.configs.readonly:
1619
+ D.configs.optimizeConfigs(self)
1620
+ D.configs.setReadonly(True)
1621
+
1622
+ if ParserATNSimulator.trace_atn_sim: print("addDFAState new", str(D))
1623
+
1624
+ dfa.states[D] = D
1625
+ return D
1626
+
1627
+ def reportAttemptingFullContext(self, dfa:DFA, conflictingAlts:set, configs:ATNConfigSet, startIndex:int, stopIndex:int):
1628
+ if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
1629
+ print("reportAttemptingFullContext decision=" + str(dfa.decision) + ":" + str(configs) +
1630
+ ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
1631
+ if self.parser is not None:
1632
+ self.parser.getErrorListenerDispatch().reportAttemptingFullContext(self.parser, dfa, startIndex, stopIndex, conflictingAlts, configs)
1633
+
1634
+ def reportContextSensitivity(self, dfa:DFA, prediction:int, configs:ATNConfigSet, startIndex:int, stopIndex:int):
1635
+ if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
1636
+ print("reportContextSensitivity decision=" + str(dfa.decision) + ":" + str(configs) +
1637
+ ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
1638
+ if self.parser is not None:
1639
+ self.parser.getErrorListenerDispatch().reportContextSensitivity(self.parser, dfa, startIndex, stopIndex, prediction, configs)
1640
+
1641
+ # If context sensitive parsing, we know it's ambiguity not conflict#
1642
+ def reportAmbiguity(self, dfa:DFA, D:DFAState, startIndex:int, stopIndex:int,
1643
+ exact:bool, ambigAlts:set, configs:ATNConfigSet ):
1644
+ if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
1645
+ # ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn);
1646
+ # int i = 1;
1647
+ # for (Transition t : dfa.atnStartState.transitions) {
1648
+ # print("ALT "+i+"=");
1649
+ # print(startIndex+".."+stopIndex+", len(input)="+parser.getInputStream().size());
1650
+ # TraceTree path = finder.trace(t.target, parser.getContext(), (TokenStream)parser.getInputStream(),
1651
+ # startIndex, stopIndex);
1652
+ # if ( path!=null ) {
1653
+ # print("path = "+path.toStringTree());
1654
+ # for (TraceTree leaf : path.leaves) {
1655
+ # List<ATNState> states = path.getPathToNode(leaf);
1656
+ # print("states="+states);
1657
+ # }
1658
+ # }
1659
+ # i++;
1660
+ # }
1661
+ print("reportAmbiguity " + str(ambigAlts) + ":" + str(configs) +
1662
+ ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
1663
+ if self.parser is not None:
1664
+ self.parser.getErrorListenerDispatch().reportAmbiguity(self.parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs)