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.
- omlish/__about__.py +2 -2
- omlish/antlr/__init__.py +0 -0
- omlish/antlr/_runtime/BufferedTokenStream.py +305 -0
- omlish/antlr/_runtime/CommonTokenFactory.py +64 -0
- omlish/antlr/_runtime/CommonTokenStream.py +90 -0
- omlish/antlr/_runtime/FileStream.py +30 -0
- omlish/antlr/_runtime/InputStream.py +90 -0
- omlish/antlr/_runtime/IntervalSet.py +183 -0
- omlish/antlr/_runtime/LL1Analyzer.py +176 -0
- omlish/antlr/_runtime/Lexer.py +332 -0
- omlish/antlr/_runtime/ListTokenSource.py +147 -0
- omlish/antlr/_runtime/Parser.py +583 -0
- omlish/antlr/_runtime/ParserInterpreter.py +173 -0
- omlish/antlr/_runtime/ParserRuleContext.py +189 -0
- omlish/antlr/_runtime/PredictionContext.py +632 -0
- omlish/antlr/_runtime/Recognizer.py +150 -0
- omlish/antlr/_runtime/RuleContext.py +230 -0
- omlish/antlr/_runtime/StdinStream.py +14 -0
- omlish/antlr/_runtime/Token.py +158 -0
- omlish/antlr/_runtime/TokenStreamRewriter.py +258 -0
- omlish/antlr/_runtime/Utils.py +36 -0
- omlish/antlr/_runtime/__init__.py +24 -0
- omlish/antlr/_runtime/_pygrun.py +174 -0
- omlish/antlr/_runtime/atn/ATN.py +135 -0
- omlish/antlr/_runtime/atn/ATNConfig.py +162 -0
- omlish/antlr/_runtime/atn/ATNConfigSet.py +215 -0
- omlish/antlr/_runtime/atn/ATNDeserializationOptions.py +27 -0
- omlish/antlr/_runtime/atn/ATNDeserializer.py +449 -0
- omlish/antlr/_runtime/atn/ATNSimulator.py +50 -0
- omlish/antlr/_runtime/atn/ATNState.py +267 -0
- omlish/antlr/_runtime/atn/ATNType.py +20 -0
- omlish/antlr/_runtime/atn/LexerATNSimulator.py +573 -0
- omlish/antlr/_runtime/atn/LexerAction.py +301 -0
- omlish/antlr/_runtime/atn/LexerActionExecutor.py +146 -0
- omlish/antlr/_runtime/atn/ParserATNSimulator.py +1664 -0
- omlish/antlr/_runtime/atn/PredictionMode.py +502 -0
- omlish/antlr/_runtime/atn/SemanticContext.py +333 -0
- omlish/antlr/_runtime/atn/Transition.py +271 -0
- omlish/antlr/_runtime/atn/__init__.py +4 -0
- omlish/antlr/_runtime/dfa/DFA.py +136 -0
- omlish/antlr/_runtime/dfa/DFASerializer.py +76 -0
- omlish/antlr/_runtime/dfa/DFAState.py +129 -0
- omlish/antlr/_runtime/dfa/__init__.py +4 -0
- omlish/antlr/_runtime/error/DiagnosticErrorListener.py +110 -0
- omlish/antlr/_runtime/error/ErrorListener.py +75 -0
- omlish/antlr/_runtime/error/ErrorStrategy.py +712 -0
- omlish/antlr/_runtime/error/Errors.py +176 -0
- omlish/antlr/_runtime/error/__init__.py +4 -0
- omlish/antlr/_runtime/tree/Chunk.py +33 -0
- omlish/antlr/_runtime/tree/ParseTreeMatch.py +121 -0
- omlish/antlr/_runtime/tree/ParseTreePattern.py +75 -0
- omlish/antlr/_runtime/tree/ParseTreePatternMatcher.py +377 -0
- omlish/antlr/_runtime/tree/RuleTagToken.py +53 -0
- omlish/antlr/_runtime/tree/TokenTagToken.py +50 -0
- omlish/antlr/_runtime/tree/Tree.py +194 -0
- omlish/antlr/_runtime/tree/Trees.py +114 -0
- omlish/antlr/_runtime/tree/__init__.py +2 -0
- omlish/antlr/_runtime/xpath/XPath.py +272 -0
- omlish/antlr/_runtime/xpath/XPathLexer.py +98 -0
- omlish/antlr/_runtime/xpath/__init__.py +4 -0
- omlish/formats/json/cli.py +76 -7
- omlish/formats/props.py +6 -2
- {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/RECORD +68 -9
- {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/LICENSE +0 -0
- {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev56.dist-info → omlish-0.0.0.dev58.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,449 @@
|
|
1
|
+
# type: ignore
|
2
|
+
# ruff: noqa
|
3
|
+
# flake8: noqa
|
4
|
+
# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
5
|
+
# Use of this file is governed by the BSD 3-clause license that
|
6
|
+
# can be found in the LICENSE.txt file in the project root.
|
7
|
+
#/
|
8
|
+
from io import StringIO
|
9
|
+
from typing import Callable
|
10
|
+
from ..Token import Token
|
11
|
+
from .ATN import ATN
|
12
|
+
from .ATNType import ATNType
|
13
|
+
from .ATNState import *
|
14
|
+
from .Transition import *
|
15
|
+
from .LexerAction import *
|
16
|
+
from .ATNDeserializationOptions import ATNDeserializationOptions
|
17
|
+
|
18
|
+
SERIALIZED_VERSION = 4
|
19
|
+
|
20
|
+
class ATNDeserializer (object):
|
21
|
+
__slots__ = ('deserializationOptions', 'data', 'pos')
|
22
|
+
|
23
|
+
def __init__(self, options : ATNDeserializationOptions = None):
|
24
|
+
if options is None:
|
25
|
+
options = ATNDeserializationOptions.defaultOptions
|
26
|
+
self.deserializationOptions = options
|
27
|
+
|
28
|
+
def deserialize(self, data : [int]):
|
29
|
+
self.data = data
|
30
|
+
self.pos = 0
|
31
|
+
self.checkVersion()
|
32
|
+
atn = self.readATN()
|
33
|
+
self.readStates(atn)
|
34
|
+
self.readRules(atn)
|
35
|
+
self.readModes(atn)
|
36
|
+
sets = []
|
37
|
+
self.readSets(atn, sets)
|
38
|
+
self.readEdges(atn, sets)
|
39
|
+
self.readDecisions(atn)
|
40
|
+
self.readLexerActions(atn)
|
41
|
+
self.markPrecedenceDecisions(atn)
|
42
|
+
self.verifyATN(atn)
|
43
|
+
if self.deserializationOptions.generateRuleBypassTransitions \
|
44
|
+
and atn.grammarType == ATNType.PARSER:
|
45
|
+
self.generateRuleBypassTransitions(atn)
|
46
|
+
# re-verify after modification
|
47
|
+
self.verifyATN(atn)
|
48
|
+
return atn
|
49
|
+
|
50
|
+
def checkVersion(self):
|
51
|
+
version = self.readInt()
|
52
|
+
if version != SERIALIZED_VERSION:
|
53
|
+
raise Exception("Could not deserialize ATN with version " + str(version) + " (expected " + str(SERIALIZED_VERSION) + ").")
|
54
|
+
|
55
|
+
def readATN(self):
|
56
|
+
idx = self.readInt()
|
57
|
+
grammarType = ATNType.fromOrdinal(idx)
|
58
|
+
maxTokenType = self.readInt()
|
59
|
+
return ATN(grammarType, maxTokenType)
|
60
|
+
|
61
|
+
def readStates(self, atn:ATN):
|
62
|
+
loopBackStateNumbers = []
|
63
|
+
endStateNumbers = []
|
64
|
+
nstates = self.readInt()
|
65
|
+
for i in range(0, nstates):
|
66
|
+
stype = self.readInt()
|
67
|
+
# ignore bad type of states
|
68
|
+
if stype==ATNState.INVALID_TYPE:
|
69
|
+
atn.addState(None)
|
70
|
+
continue
|
71
|
+
ruleIndex = self.readInt()
|
72
|
+
s = self.stateFactory(stype, ruleIndex)
|
73
|
+
if stype == ATNState.LOOP_END: # special case
|
74
|
+
loopBackStateNumber = self.readInt()
|
75
|
+
loopBackStateNumbers.append((s, loopBackStateNumber))
|
76
|
+
elif isinstance(s, BlockStartState):
|
77
|
+
endStateNumber = self.readInt()
|
78
|
+
endStateNumbers.append((s, endStateNumber))
|
79
|
+
|
80
|
+
atn.addState(s)
|
81
|
+
|
82
|
+
# delay the assignment of loop back and end states until we know all the state instances have been initialized
|
83
|
+
for pair in loopBackStateNumbers:
|
84
|
+
pair[0].loopBackState = atn.states[pair[1]]
|
85
|
+
|
86
|
+
for pair in endStateNumbers:
|
87
|
+
pair[0].endState = atn.states[pair[1]]
|
88
|
+
|
89
|
+
numNonGreedyStates = self.readInt()
|
90
|
+
for i in range(0, numNonGreedyStates):
|
91
|
+
stateNumber = self.readInt()
|
92
|
+
atn.states[stateNumber].nonGreedy = True
|
93
|
+
|
94
|
+
numPrecedenceStates = self.readInt()
|
95
|
+
for i in range(0, numPrecedenceStates):
|
96
|
+
stateNumber = self.readInt()
|
97
|
+
atn.states[stateNumber].isPrecedenceRule = True
|
98
|
+
|
99
|
+
def readRules(self, atn:ATN):
|
100
|
+
nrules = self.readInt()
|
101
|
+
if atn.grammarType == ATNType.LEXER:
|
102
|
+
atn.ruleToTokenType = [0] * nrules
|
103
|
+
|
104
|
+
atn.ruleToStartState = [0] * nrules
|
105
|
+
for i in range(0, nrules):
|
106
|
+
s = self.readInt()
|
107
|
+
startState = atn.states[s]
|
108
|
+
atn.ruleToStartState[i] = startState
|
109
|
+
if atn.grammarType == ATNType.LEXER:
|
110
|
+
tokenType = self.readInt()
|
111
|
+
atn.ruleToTokenType[i] = tokenType
|
112
|
+
|
113
|
+
atn.ruleToStopState = [0] * nrules
|
114
|
+
for state in atn.states:
|
115
|
+
if not isinstance(state, RuleStopState):
|
116
|
+
continue
|
117
|
+
atn.ruleToStopState[state.ruleIndex] = state
|
118
|
+
atn.ruleToStartState[state.ruleIndex].stopState = state
|
119
|
+
|
120
|
+
def readModes(self, atn:ATN):
|
121
|
+
nmodes = self.readInt()
|
122
|
+
for i in range(0, nmodes):
|
123
|
+
s = self.readInt()
|
124
|
+
atn.modeToStartState.append(atn.states[s])
|
125
|
+
|
126
|
+
def readSets(self, atn:ATN, sets:list):
|
127
|
+
m = self.readInt()
|
128
|
+
for i in range(0, m):
|
129
|
+
iset = IntervalSet()
|
130
|
+
sets.append(iset)
|
131
|
+
n = self.readInt()
|
132
|
+
containsEof = self.readInt()
|
133
|
+
if containsEof!=0:
|
134
|
+
iset.addOne(-1)
|
135
|
+
for j in range(0, n):
|
136
|
+
i1 = self.readInt()
|
137
|
+
i2 = self.readInt()
|
138
|
+
iset.addRange(range(i1, i2 + 1)) # range upper limit is exclusive
|
139
|
+
|
140
|
+
def readEdges(self, atn:ATN, sets:list):
|
141
|
+
nedges = self.readInt()
|
142
|
+
for i in range(0, nedges):
|
143
|
+
src = self.readInt()
|
144
|
+
trg = self.readInt()
|
145
|
+
ttype = self.readInt()
|
146
|
+
arg1 = self.readInt()
|
147
|
+
arg2 = self.readInt()
|
148
|
+
arg3 = self.readInt()
|
149
|
+
trans = self.edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets)
|
150
|
+
srcState = atn.states[src]
|
151
|
+
srcState.addTransition(trans)
|
152
|
+
|
153
|
+
# edges for rule stop states can be derived, so they aren't serialized
|
154
|
+
for state in atn.states:
|
155
|
+
for i in range(0, len(state.transitions)):
|
156
|
+
t = state.transitions[i]
|
157
|
+
if not isinstance(t, RuleTransition):
|
158
|
+
continue
|
159
|
+
outermostPrecedenceReturn = -1
|
160
|
+
if atn.ruleToStartState[t.target.ruleIndex].isPrecedenceRule:
|
161
|
+
if t.precedence == 0:
|
162
|
+
outermostPrecedenceReturn = t.target.ruleIndex
|
163
|
+
trans = EpsilonTransition(t.followState, outermostPrecedenceReturn)
|
164
|
+
atn.ruleToStopState[t.target.ruleIndex].addTransition(trans)
|
165
|
+
|
166
|
+
for state in atn.states:
|
167
|
+
if isinstance(state, BlockStartState):
|
168
|
+
# we need to know the end state to set its start state
|
169
|
+
if state.endState is None:
|
170
|
+
raise Exception("IllegalState")
|
171
|
+
# block end states can only be associated to a single block start state
|
172
|
+
if state.endState.startState is not None:
|
173
|
+
raise Exception("IllegalState")
|
174
|
+
state.endState.startState = state
|
175
|
+
|
176
|
+
if isinstance(state, PlusLoopbackState):
|
177
|
+
for i in range(0, len(state.transitions)):
|
178
|
+
target = state.transitions[i].target
|
179
|
+
if isinstance(target, PlusBlockStartState):
|
180
|
+
target.loopBackState = state
|
181
|
+
elif isinstance(state, StarLoopbackState):
|
182
|
+
for i in range(0, len(state.transitions)):
|
183
|
+
target = state.transitions[i].target
|
184
|
+
if isinstance(target, StarLoopEntryState):
|
185
|
+
target.loopBackState = state
|
186
|
+
|
187
|
+
def readDecisions(self, atn:ATN):
|
188
|
+
ndecisions = self.readInt()
|
189
|
+
for i in range(0, ndecisions):
|
190
|
+
s = self.readInt()
|
191
|
+
decState = atn.states[s]
|
192
|
+
atn.decisionToState.append(decState)
|
193
|
+
decState.decision = i
|
194
|
+
|
195
|
+
def readLexerActions(self, atn:ATN):
|
196
|
+
if atn.grammarType == ATNType.LEXER:
|
197
|
+
count = self.readInt()
|
198
|
+
atn.lexerActions = [ None ] * count
|
199
|
+
for i in range(0, count):
|
200
|
+
actionType = self.readInt()
|
201
|
+
data1 = self.readInt()
|
202
|
+
data2 = self.readInt()
|
203
|
+
lexerAction = self.lexerActionFactory(actionType, data1, data2)
|
204
|
+
atn.lexerActions[i] = lexerAction
|
205
|
+
|
206
|
+
def generateRuleBypassTransitions(self, atn:ATN):
|
207
|
+
|
208
|
+
count = len(atn.ruleToStartState)
|
209
|
+
atn.ruleToTokenType = [ 0 ] * count
|
210
|
+
for i in range(0, count):
|
211
|
+
atn.ruleToTokenType[i] = atn.maxTokenType + i + 1
|
212
|
+
|
213
|
+
for i in range(0, count):
|
214
|
+
self.generateRuleBypassTransition(atn, i)
|
215
|
+
|
216
|
+
def generateRuleBypassTransition(self, atn:ATN, idx:int):
|
217
|
+
|
218
|
+
bypassStart = BasicBlockStartState()
|
219
|
+
bypassStart.ruleIndex = idx
|
220
|
+
atn.addState(bypassStart)
|
221
|
+
|
222
|
+
bypassStop = BlockEndState()
|
223
|
+
bypassStop.ruleIndex = idx
|
224
|
+
atn.addState(bypassStop)
|
225
|
+
|
226
|
+
bypassStart.endState = bypassStop
|
227
|
+
atn.defineDecisionState(bypassStart)
|
228
|
+
|
229
|
+
bypassStop.startState = bypassStart
|
230
|
+
|
231
|
+
excludeTransition = None
|
232
|
+
|
233
|
+
if atn.ruleToStartState[idx].isPrecedenceRule:
|
234
|
+
# wrap from the beginning of the rule to the StarLoopEntryState
|
235
|
+
endState = None
|
236
|
+
for state in atn.states:
|
237
|
+
if self.stateIsEndStateFor(state, idx):
|
238
|
+
endState = state
|
239
|
+
excludeTransition = state.loopBackState.transitions[0]
|
240
|
+
break
|
241
|
+
|
242
|
+
if excludeTransition is None:
|
243
|
+
raise Exception("Couldn't identify final state of the precedence rule prefix section.")
|
244
|
+
|
245
|
+
else:
|
246
|
+
|
247
|
+
endState = atn.ruleToStopState[idx]
|
248
|
+
|
249
|
+
# all non-excluded transitions that currently target end state need to target blockEnd instead
|
250
|
+
for state in atn.states:
|
251
|
+
for transition in state.transitions:
|
252
|
+
if transition == excludeTransition:
|
253
|
+
continue
|
254
|
+
if transition.target == endState:
|
255
|
+
transition.target = bypassStop
|
256
|
+
|
257
|
+
# all transitions leaving the rule start state need to leave blockStart instead
|
258
|
+
ruleToStartState = atn.ruleToStartState[idx]
|
259
|
+
count = len(ruleToStartState.transitions)
|
260
|
+
while count > 0:
|
261
|
+
bypassStart.addTransition(ruleToStartState.transitions[count-1])
|
262
|
+
del ruleToStartState.transitions[-1]
|
263
|
+
|
264
|
+
# link the new states
|
265
|
+
atn.ruleToStartState[idx].addTransition(EpsilonTransition(bypassStart))
|
266
|
+
bypassStop.addTransition(EpsilonTransition(endState))
|
267
|
+
|
268
|
+
matchState = BasicState()
|
269
|
+
atn.addState(matchState)
|
270
|
+
matchState.addTransition(AtomTransition(bypassStop, atn.ruleToTokenType[idx]))
|
271
|
+
bypassStart.addTransition(EpsilonTransition(matchState))
|
272
|
+
|
273
|
+
|
274
|
+
def stateIsEndStateFor(self, state:ATNState, idx:int):
|
275
|
+
if state.ruleIndex != idx:
|
276
|
+
return None
|
277
|
+
if not isinstance(state, StarLoopEntryState):
|
278
|
+
return None
|
279
|
+
|
280
|
+
maybeLoopEndState = state.transitions[len(state.transitions) - 1].target
|
281
|
+
if not isinstance(maybeLoopEndState, LoopEndState):
|
282
|
+
return None
|
283
|
+
|
284
|
+
if maybeLoopEndState.epsilonOnlyTransitions and \
|
285
|
+
isinstance(maybeLoopEndState.transitions[0].target, RuleStopState):
|
286
|
+
return state
|
287
|
+
else:
|
288
|
+
return None
|
289
|
+
|
290
|
+
|
291
|
+
#
|
292
|
+
# Analyze the {@link StarLoopEntryState} states in the specified ATN to set
|
293
|
+
# the {@link StarLoopEntryState#isPrecedenceDecision} field to the
|
294
|
+
# correct value.
|
295
|
+
#
|
296
|
+
# @param atn The ATN.
|
297
|
+
#
|
298
|
+
def markPrecedenceDecisions(self, atn:ATN):
|
299
|
+
for state in atn.states:
|
300
|
+
if not isinstance(state, StarLoopEntryState):
|
301
|
+
continue
|
302
|
+
|
303
|
+
# We analyze the ATN to determine if this ATN decision state is the
|
304
|
+
# decision for the closure block that determines whether a
|
305
|
+
# precedence rule should continue or complete.
|
306
|
+
#
|
307
|
+
if atn.ruleToStartState[state.ruleIndex].isPrecedenceRule:
|
308
|
+
maybeLoopEndState = state.transitions[len(state.transitions) - 1].target
|
309
|
+
if isinstance(maybeLoopEndState, LoopEndState):
|
310
|
+
if maybeLoopEndState.epsilonOnlyTransitions and \
|
311
|
+
isinstance(maybeLoopEndState.transitions[0].target, RuleStopState):
|
312
|
+
state.isPrecedenceDecision = True
|
313
|
+
|
314
|
+
def verifyATN(self, atn:ATN):
|
315
|
+
if not self.deserializationOptions.verifyATN:
|
316
|
+
return
|
317
|
+
# verify assumptions
|
318
|
+
for state in atn.states:
|
319
|
+
if state is None:
|
320
|
+
continue
|
321
|
+
|
322
|
+
self.checkCondition(state.epsilonOnlyTransitions or len(state.transitions) <= 1)
|
323
|
+
|
324
|
+
if isinstance(state, PlusBlockStartState):
|
325
|
+
self.checkCondition(state.loopBackState is not None)
|
326
|
+
|
327
|
+
if isinstance(state, StarLoopEntryState):
|
328
|
+
self.checkCondition(state.loopBackState is not None)
|
329
|
+
self.checkCondition(len(state.transitions) == 2)
|
330
|
+
|
331
|
+
if isinstance(state.transitions[0].target, StarBlockStartState):
|
332
|
+
self.checkCondition(isinstance(state.transitions[1].target, LoopEndState))
|
333
|
+
self.checkCondition(not state.nonGreedy)
|
334
|
+
elif isinstance(state.transitions[0].target, LoopEndState):
|
335
|
+
self.checkCondition(isinstance(state.transitions[1].target, StarBlockStartState))
|
336
|
+
self.checkCondition(state.nonGreedy)
|
337
|
+
else:
|
338
|
+
raise Exception("IllegalState")
|
339
|
+
|
340
|
+
if isinstance(state, StarLoopbackState):
|
341
|
+
self.checkCondition(len(state.transitions) == 1)
|
342
|
+
self.checkCondition(isinstance(state.transitions[0].target, StarLoopEntryState))
|
343
|
+
|
344
|
+
if isinstance(state, LoopEndState):
|
345
|
+
self.checkCondition(state.loopBackState is not None)
|
346
|
+
|
347
|
+
if isinstance(state, RuleStartState):
|
348
|
+
self.checkCondition(state.stopState is not None)
|
349
|
+
|
350
|
+
if isinstance(state, BlockStartState):
|
351
|
+
self.checkCondition(state.endState is not None)
|
352
|
+
|
353
|
+
if isinstance(state, BlockEndState):
|
354
|
+
self.checkCondition(state.startState is not None)
|
355
|
+
|
356
|
+
if isinstance(state, DecisionState):
|
357
|
+
self.checkCondition(len(state.transitions) <= 1 or state.decision >= 0)
|
358
|
+
else:
|
359
|
+
self.checkCondition(len(state.transitions) <= 1 or isinstance(state, RuleStopState))
|
360
|
+
|
361
|
+
def checkCondition(self, condition:bool, message=None):
|
362
|
+
if not condition:
|
363
|
+
if message is None:
|
364
|
+
message = "IllegalState"
|
365
|
+
raise Exception(message)
|
366
|
+
|
367
|
+
def readInt(self):
|
368
|
+
i = self.data[self.pos]
|
369
|
+
self.pos += 1
|
370
|
+
return i
|
371
|
+
|
372
|
+
edgeFactories = [ lambda args : None,
|
373
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : EpsilonTransition(target),
|
374
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
375
|
+
RangeTransition(target, Token.EOF, arg2) if arg3 != 0 else RangeTransition(target, arg1, arg2),
|
376
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
377
|
+
RuleTransition(atn.states[arg1], arg2, arg3, target),
|
378
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
379
|
+
PredicateTransition(target, arg1, arg2, arg3 != 0),
|
380
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
381
|
+
AtomTransition(target, Token.EOF) if arg3 != 0 else AtomTransition(target, arg1),
|
382
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
383
|
+
ActionTransition(target, arg1, arg2, arg3 != 0),
|
384
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
385
|
+
SetTransition(target, sets[arg1]),
|
386
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
387
|
+
NotSetTransition(target, sets[arg1]),
|
388
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
389
|
+
WildcardTransition(target),
|
390
|
+
lambda atn, src, trg, arg1, arg2, arg3, sets, target : \
|
391
|
+
PrecedencePredicateTransition(target, arg1)
|
392
|
+
]
|
393
|
+
|
394
|
+
def edgeFactory(self, atn:ATN, type:int, src:int, trg:int, arg1:int, arg2:int, arg3:int, sets:list):
|
395
|
+
target = atn.states[trg]
|
396
|
+
if type > len(self.edgeFactories) or self.edgeFactories[type] is None:
|
397
|
+
raise Exception("The specified transition type: " + str(type) + " is not valid.")
|
398
|
+
else:
|
399
|
+
return self.edgeFactories[type](atn, src, trg, arg1, arg2, arg3, sets, target)
|
400
|
+
|
401
|
+
stateFactories = [ lambda : None,
|
402
|
+
lambda : BasicState(),
|
403
|
+
lambda : RuleStartState(),
|
404
|
+
lambda : BasicBlockStartState(),
|
405
|
+
lambda : PlusBlockStartState(),
|
406
|
+
lambda : StarBlockStartState(),
|
407
|
+
lambda : TokensStartState(),
|
408
|
+
lambda : RuleStopState(),
|
409
|
+
lambda : BlockEndState(),
|
410
|
+
lambda : StarLoopbackState(),
|
411
|
+
lambda : StarLoopEntryState(),
|
412
|
+
lambda : PlusLoopbackState(),
|
413
|
+
lambda : LoopEndState()
|
414
|
+
]
|
415
|
+
|
416
|
+
def stateFactory(self, type:int, ruleIndex:int):
|
417
|
+
if type> len(self.stateFactories) or self.stateFactories[type] is None:
|
418
|
+
raise Exception("The specified state type " + str(type) + " is not valid.")
|
419
|
+
else:
|
420
|
+
s = self.stateFactories[type]()
|
421
|
+
if s is not None:
|
422
|
+
s.ruleIndex = ruleIndex
|
423
|
+
return s
|
424
|
+
|
425
|
+
CHANNEL = 0 #The type of a {@link LexerChannelAction} action.
|
426
|
+
CUSTOM = 1 #The type of a {@link LexerCustomAction} action.
|
427
|
+
MODE = 2 #The type of a {@link LexerModeAction} action.
|
428
|
+
MORE = 3 #The type of a {@link LexerMoreAction} action.
|
429
|
+
POP_MODE = 4 #The type of a {@link LexerPopModeAction} action.
|
430
|
+
PUSH_MODE = 5 #The type of a {@link LexerPushModeAction} action.
|
431
|
+
SKIP = 6 #The type of a {@link LexerSkipAction} action.
|
432
|
+
TYPE = 7 #The type of a {@link LexerTypeAction} action.
|
433
|
+
|
434
|
+
actionFactories = [ lambda data1, data2: LexerChannelAction(data1),
|
435
|
+
lambda data1, data2: LexerCustomAction(data1, data2),
|
436
|
+
lambda data1, data2: LexerModeAction(data1),
|
437
|
+
lambda data1, data2: LexerMoreAction.INSTANCE,
|
438
|
+
lambda data1, data2: LexerPopModeAction.INSTANCE,
|
439
|
+
lambda data1, data2: LexerPushModeAction(data1),
|
440
|
+
lambda data1, data2: LexerSkipAction.INSTANCE,
|
441
|
+
lambda data1, data2: LexerTypeAction(data1)
|
442
|
+
]
|
443
|
+
|
444
|
+
def lexerActionFactory(self, type:int, data1:int, data2:int):
|
445
|
+
|
446
|
+
if type > len(self.actionFactories) or self.actionFactories[type] is None:
|
447
|
+
raise Exception("The specified lexer action type " + str(type) + " is not valid.")
|
448
|
+
else:
|
449
|
+
return self.actionFactories[type](data1, data2)
|
@@ -0,0 +1,50 @@
|
|
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
|
+
from ..PredictionContext import PredictionContextCache, PredictionContext, getCachedPredictionContext
|
10
|
+
from .ATN import ATN
|
11
|
+
from .ATNConfigSet import ATNConfigSet
|
12
|
+
from ..dfa.DFAState import DFAState
|
13
|
+
|
14
|
+
|
15
|
+
class ATNSimulator(object):
|
16
|
+
__slots__ = ('atn', 'sharedContextCache', '__dict__')
|
17
|
+
|
18
|
+
# Must distinguish between missing edge and edge we know leads nowhere#/
|
19
|
+
ERROR = DFAState(configs=ATNConfigSet())
|
20
|
+
ERROR.stateNumber = 0x7FFFFFFF
|
21
|
+
|
22
|
+
# The context cache maps all PredictionContext objects that are ==
|
23
|
+
# to a single cached copy. This cache is shared across all contexts
|
24
|
+
# in all ATNConfigs in all DFA states. We rebuild each ATNConfigSet
|
25
|
+
# to use only cached nodes/graphs in addDFAState(). We don't want to
|
26
|
+
# fill this during closure() since there are lots of contexts that
|
27
|
+
# pop up but are not used ever again. It also greatly slows down closure().
|
28
|
+
#
|
29
|
+
# <p>This cache makes a huge difference in memory and a little bit in speed.
|
30
|
+
# For the Java grammar on java.*, it dropped the memory requirements
|
31
|
+
# at the end from 25M to 16M. We don't store any of the full context
|
32
|
+
# graphs in the DFA because they are limited to local context only,
|
33
|
+
# but apparently there's a lot of repetition there as well. We optimize
|
34
|
+
# the config contexts before storing the config set in the DFA states
|
35
|
+
# by literally rebuilding them with cached subgraphs only.</p>
|
36
|
+
#
|
37
|
+
# <p>I tried a cache for use during closure operations, that was
|
38
|
+
# whacked after each adaptivePredict(). It cost a little bit
|
39
|
+
# more time I think and doesn't save on the overall footprint
|
40
|
+
# so it's not worth the complexity.</p>
|
41
|
+
#/
|
42
|
+
def __init__(self, atn:ATN, sharedContextCache:PredictionContextCache):
|
43
|
+
self.atn = atn
|
44
|
+
self.sharedContextCache = sharedContextCache
|
45
|
+
|
46
|
+
def getCachedContext(self, context:PredictionContext):
|
47
|
+
if self.sharedContextCache is None:
|
48
|
+
return context
|
49
|
+
visited = dict()
|
50
|
+
return getCachedPredictionContext(context, self.sharedContextCache, visited)
|