jasonfan commited on
Commit
f31fe4e
·
verified ·
1 Parent(s): ae51889

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/LexerATNSimulator.py +570 -0
  2. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/LexerAction.py +298 -0
  3. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/LexerActionExecutor.py +143 -0
  4. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/ParserATNSimulator.py +1649 -0
  5. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/PredictionMode.py +499 -0
  6. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/SemanticContext.py +323 -0
  7. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/Transition.py +268 -0
  8. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/__init__.py +1 -0
  9. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/DFA.py +133 -0
  10. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/DFASerializer.py +73 -0
  11. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/DFAState.py +126 -0
  12. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/__init__.py +1 -0
  13. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/DiagnosticErrorListener.py +107 -0
  14. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/ErrorListener.py +72 -0
  15. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/ErrorStrategy.py +709 -0
  16. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/Errors.py +172 -0
  17. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/__init__.py +1 -0
  18. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/Chunk.py +30 -0
  19. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/ParseTreeMatch.py +118 -0
  20. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/ParseTreePattern.py +72 -0
  21. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/ParseTreePatternMatcher.py +374 -0
  22. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/RuleTagToken.py +50 -0
  23. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/TokenTagToken.py +47 -0
  24. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/Tree.py +191 -0
  25. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/Trees.py +111 -0
  26. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/__init__.py +0 -0
  27. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/xpath/XPath.py +352 -0
  28. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/xpath/__init__.py +1 -0
  29. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/INSTALLER +1 -0
  30. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/METADATA +15 -0
  31. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/RECORD +118 -0
  32. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/WHEEL +5 -0
  33. miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/top_level.txt +1 -0
  34. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/INSTALLER +1 -0
  35. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/METADATA +96 -0
  36. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/RECORD +92 -0
  37. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/WHEEL +5 -0
  38. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/entry_points.txt +2 -0
  39. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/licenses/LICENSE +20 -0
  40. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/top_level.txt +1 -0
  41. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/__init__.py +111 -0
  42. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_backends/__init__.py +0 -0
  43. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_backends/_asyncio.py +0 -0
  44. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_backends/_trio.py +1346 -0
  45. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/__init__.py +0 -0
  46. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_asyncio_selector_thread.py +167 -0
  47. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_contextmanagers.py +200 -0
  48. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_eventloop.py +234 -0
  49. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_exceptions.py +156 -0
  50. miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_fileio.py +797 -0
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/LexerATNSimulator.py ADDED
@@ -0,0 +1,570 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #/
6
+
7
+ # When we hit an accept state in either the DFA or the ATN, we
8
+ # have to notify the character stream to start buffering characters
9
+ # via {@link IntStream#mark} and record the current state. The current sim state
10
+ # includes the current index into the input, the current line,
11
+ # and current character position in that line. Note that the Lexer is
12
+ # tracking the starting line and characterization of the token. These
13
+ # variables track the "state" of the simulator when it hits an accept state.
14
+ #
15
+ # <p>We track these variables separately for the DFA and ATN simulation
16
+ # because the DFA simulation often has to fail over to the ATN
17
+ # simulation. If the ATN simulation fails, we need the DFA to fall
18
+ # back to its previously accepted state, if any. If the ATN succeeds,
19
+ # then the ATN does the accept and the DFA simulator that invoked it
20
+ # can simply return the predicted token type.</p>
21
+ #/
22
+
23
+ from antlr4.PredictionContext import PredictionContextCache, SingletonPredictionContext, PredictionContext
24
+ from antlr4.InputStream import InputStream
25
+ from antlr4.Token import Token
26
+ from antlr4.atn.ATN import ATN
27
+ from antlr4.atn.ATNConfig import LexerATNConfig
28
+ from antlr4.atn.ATNSimulator import ATNSimulator
29
+ from antlr4.atn.ATNConfigSet import ATNConfigSet, OrderedATNConfigSet
30
+ from antlr4.atn.ATNState import RuleStopState, ATNState
31
+ from antlr4.atn.LexerActionExecutor import LexerActionExecutor
32
+ from antlr4.atn.Transition import Transition
33
+ from antlr4.dfa.DFAState import DFAState
34
+ from antlr4.error.Errors import LexerNoViableAltException, UnsupportedOperationException
35
+
36
+ class SimState(object):
37
+ __slots__ = ('index', 'line', 'column', 'dfaState')
38
+
39
+ def __init__(self):
40
+ self.reset()
41
+
42
+ def reset(self):
43
+ self.index = -1
44
+ self.line = 0
45
+ self.column = -1
46
+ self.dfaState = None
47
+
48
+ # need forward declaration
49
+ Lexer = None
50
+ LexerATNSimulator = None
51
+
52
+ class LexerATNSimulator(ATNSimulator):
53
+ __slots__ = (
54
+ 'decisionToDFA', 'recog', 'startIndex', 'line', 'column', 'mode',
55
+ 'DEFAULT_MODE', 'MAX_CHAR_VALUE', 'prevAccept'
56
+ )
57
+
58
+ debug = False
59
+ dfa_debug = False
60
+
61
+ MIN_DFA_EDGE = 0
62
+ MAX_DFA_EDGE = 127 # forces unicode to stay in ATN
63
+
64
+ ERROR = None
65
+
66
+ def __init__(self, recog:Lexer, atn:ATN, decisionToDFA:list, sharedContextCache:PredictionContextCache):
67
+ super().__init__(atn, sharedContextCache)
68
+ self.decisionToDFA = decisionToDFA
69
+ self.recog = recog
70
+ # The current token's starting index into the character stream.
71
+ # Shared across DFA to ATN simulation in case the ATN fails and the
72
+ # DFA did not have a previous accept state. In this case, we use the
73
+ # ATN-generated exception object.
74
+ self.startIndex = -1
75
+ # line number 1..n within the input#/
76
+ self.line = 1
77
+ # The index of the character relative to the beginning of the line 0..n-1#/
78
+ self.column = 0
79
+ from antlr4.Lexer import Lexer
80
+ self.mode = Lexer.DEFAULT_MODE
81
+ # Cache Lexer properties to avoid further imports
82
+ self.DEFAULT_MODE = Lexer.DEFAULT_MODE
83
+ self.MAX_CHAR_VALUE = Lexer.MAX_CHAR_VALUE
84
+ # Used during DFA/ATN exec to record the most recent accept configuration info
85
+ self.prevAccept = SimState()
86
+
87
+
88
+ def copyState(self, simulator:LexerATNSimulator ):
89
+ self.column = simulator.column
90
+ self.line = simulator.line
91
+ self.mode = simulator.mode
92
+ self.startIndex = simulator.startIndex
93
+
94
+ def match(self, input:InputStream , mode:int):
95
+ self.mode = mode
96
+ mark = input.mark()
97
+ try:
98
+ self.startIndex = input.index
99
+ self.prevAccept.reset()
100
+ dfa = self.decisionToDFA[mode]
101
+ if dfa.s0 is None:
102
+ return self.matchATN(input)
103
+ else:
104
+ return self.execATN(input, dfa.s0)
105
+ finally:
106
+ input.release(mark)
107
+
108
+ def reset(self):
109
+ self.prevAccept.reset()
110
+ self.startIndex = -1
111
+ self.line = 1
112
+ self.column = 0
113
+ self.mode = self.DEFAULT_MODE
114
+
115
+ def matchATN(self, input:InputStream):
116
+ startState = self.atn.modeToStartState[self.mode]
117
+
118
+ if LexerATNSimulator.debug:
119
+ print("matchATN mode " + str(self.mode) + " start: " + str(startState))
120
+
121
+ old_mode = self.mode
122
+ s0_closure = self.computeStartState(input, startState)
123
+ suppressEdge = s0_closure.hasSemanticContext
124
+ s0_closure.hasSemanticContext = False
125
+
126
+ next = self.addDFAState(s0_closure)
127
+ if not suppressEdge:
128
+ self.decisionToDFA[self.mode].s0 = next
129
+
130
+ predict = self.execATN(input, next)
131
+
132
+ if LexerATNSimulator.debug:
133
+ print("DFA after matchATN: " + str(self.decisionToDFA[old_mode].toLexerString()))
134
+
135
+ return predict
136
+
137
+ def execATN(self, input:InputStream, ds0:DFAState):
138
+ if LexerATNSimulator.debug:
139
+ print("start state closure=" + str(ds0.configs))
140
+
141
+ if ds0.isAcceptState:
142
+ # allow zero-length tokens
143
+ self.captureSimState(self.prevAccept, input, ds0)
144
+
145
+ t = input.LA(1)
146
+ s = ds0 # s is current/from DFA state
147
+
148
+ while True: # while more work
149
+ if LexerATNSimulator.debug:
150
+ print("execATN loop starting closure:", str(s.configs))
151
+
152
+ # As we move src->trg, src->trg, we keep track of the previous trg to
153
+ # avoid looking up the DFA state again, which is expensive.
154
+ # If the previous target was already part of the DFA, we might
155
+ # be able to avoid doing a reach operation upon t. If s!=null,
156
+ # it means that semantic predicates didn't prevent us from
157
+ # creating a DFA state. Once we know s!=null, we check to see if
158
+ # the DFA state has an edge already for t. If so, we can just reuse
159
+ # it's configuration set; there's no point in re-computing it.
160
+ # This is kind of like doing DFA simulation within the ATN
161
+ # simulation because DFA simulation is really just a way to avoid
162
+ # computing reach/closure sets. Technically, once we know that
163
+ # we have a previously added DFA state, we could jump over to
164
+ # the DFA simulator. But, that would mean popping back and forth
165
+ # a lot and making things more complicated algorithmically.
166
+ # This optimization makes a lot of sense for loops within DFA.
167
+ # A character will take us back to an existing DFA state
168
+ # that already has lots of edges out of it. e.g., .* in comments.
169
+ # print("Target for:" + str(s) + " and:" + str(t))
170
+ target = self.getExistingTargetState(s, t)
171
+ # print("Existing:" + str(target))
172
+ if target is None:
173
+ target = self.computeTargetState(input, s, t)
174
+ # print("Computed:" + str(target))
175
+
176
+ if target == self.ERROR:
177
+ break
178
+
179
+ # If this is a consumable input element, make sure to consume before
180
+ # capturing the accept state so the input index, line, and char
181
+ # position accurately reflect the state of the interpreter at the
182
+ # end of the token.
183
+ if t != Token.EOF:
184
+ self.consume(input)
185
+
186
+ if target.isAcceptState:
187
+ self.captureSimState(self.prevAccept, input, target)
188
+ if t == Token.EOF:
189
+ break
190
+
191
+ t = input.LA(1)
192
+
193
+ s = target # flip; current DFA target becomes new src/from state
194
+
195
+ return self.failOrAccept(self.prevAccept, input, s.configs, t)
196
+
197
+ # Get an existing target state for an edge in the DFA. If the target state
198
+ # for the edge has not yet been computed or is otherwise not available,
199
+ # this method returns {@code null}.
200
+ #
201
+ # @param s The current DFA state
202
+ # @param t The next input symbol
203
+ # @return The existing target DFA state for the given input symbol
204
+ # {@code t}, or {@code null} if the target state for this edge is not
205
+ # already cached
206
+ def getExistingTargetState(self, s:DFAState, t:int):
207
+ if s.edges is None or t < self.MIN_DFA_EDGE or t > self.MAX_DFA_EDGE:
208
+ return None
209
+
210
+ target = s.edges[t - self.MIN_DFA_EDGE]
211
+ if LexerATNSimulator.debug and target is not None:
212
+ print("reuse state", str(s.stateNumber), "edge to", str(target.stateNumber))
213
+
214
+ return target
215
+
216
+ # Compute a target state for an edge in the DFA, and attempt to add the
217
+ # computed state and corresponding edge to the DFA.
218
+ #
219
+ # @param input The input stream
220
+ # @param s The current DFA state
221
+ # @param t The next input symbol
222
+ #
223
+ # @return The computed target DFA state for the given input symbol
224
+ # {@code t}. If {@code t} does not lead to a valid DFA state, this method
225
+ # returns {@link #ERROR}.
226
+ def computeTargetState(self, input:InputStream, s:DFAState, t:int):
227
+ reach = OrderedATNConfigSet()
228
+
229
+ # if we don't find an existing DFA state
230
+ # Fill reach starting from closure, following t transitions
231
+ self.getReachableConfigSet(input, s.configs, reach, t)
232
+
233
+ if len(reach)==0: # we got nowhere on t from s
234
+ if not reach.hasSemanticContext:
235
+ # we got nowhere on t, don't throw out this knowledge; it'd
236
+ # cause a failover from DFA later.
237
+ self. addDFAEdge(s, t, self.ERROR)
238
+
239
+ # stop when we can't match any more char
240
+ return self.ERROR
241
+
242
+ # Add an edge from s to target DFA found/created for reach
243
+ return self.addDFAEdge(s, t, cfgs=reach)
244
+
245
+ def failOrAccept(self, prevAccept:SimState , input:InputStream, reach:ATNConfigSet, t:int):
246
+ if self.prevAccept.dfaState is not None:
247
+ lexerActionExecutor = prevAccept.dfaState.lexerActionExecutor
248
+ self.accept(input, lexerActionExecutor, self.startIndex, prevAccept.index, prevAccept.line, prevAccept.column)
249
+ return prevAccept.dfaState.prediction
250
+ else:
251
+ # if no accept and EOF is first char, return EOF
252
+ if t==Token.EOF and input.index==self.startIndex:
253
+ return Token.EOF
254
+ raise LexerNoViableAltException(self.recog, input, self.startIndex, reach)
255
+
256
+ # Given a starting configuration set, figure out all ATN configurations
257
+ # we can reach upon input {@code t}. Parameter {@code reach} is a return
258
+ # parameter.
259
+ def getReachableConfigSet(self, input:InputStream, closure:ATNConfigSet, reach:ATNConfigSet, t:int):
260
+ # this is used to skip processing for configs which have a lower priority
261
+ # than a config that already reached an accept state for the same rule
262
+ skipAlt = ATN.INVALID_ALT_NUMBER
263
+ for cfg in closure:
264
+ currentAltReachedAcceptState = ( cfg.alt == skipAlt )
265
+ if currentAltReachedAcceptState and cfg.passedThroughNonGreedyDecision:
266
+ continue
267
+
268
+ if LexerATNSimulator.debug:
269
+ print("testing", self.getTokenName(t), "at", str(cfg))
270
+
271
+ for trans in cfg.state.transitions: # for each transition
272
+ target = self.getReachableTarget(trans, t)
273
+ if target is not None:
274
+ lexerActionExecutor = cfg.lexerActionExecutor
275
+ if lexerActionExecutor is not None:
276
+ lexerActionExecutor = lexerActionExecutor.fixOffsetBeforeMatch(input.index - self.startIndex)
277
+
278
+ treatEofAsEpsilon = (t == Token.EOF)
279
+ config = LexerATNConfig(state=target, lexerActionExecutor=lexerActionExecutor, config=cfg)
280
+ if self.closure(input, config, reach, currentAltReachedAcceptState, True, treatEofAsEpsilon):
281
+ # any remaining configs for this alt have a lower priority than
282
+ # the one that just reached an accept state.
283
+ skipAlt = cfg.alt
284
+
285
+ def accept(self, input:InputStream, lexerActionExecutor:LexerActionExecutor, startIndex:int, index:int, line:int, charPos:int):
286
+ if LexerATNSimulator.debug:
287
+ print("ACTION", lexerActionExecutor)
288
+
289
+ # seek to after last char in token
290
+ input.seek(index)
291
+ self.line = line
292
+ self.column = charPos
293
+
294
+ if lexerActionExecutor is not None and self.recog is not None:
295
+ lexerActionExecutor.execute(self.recog, input, startIndex)
296
+
297
+ def getReachableTarget(self, trans:Transition, t:int):
298
+ if trans.matches(t, 0, self.MAX_CHAR_VALUE):
299
+ return trans.target
300
+ else:
301
+ return None
302
+
303
+ def computeStartState(self, input:InputStream, p:ATNState):
304
+ initialContext = PredictionContext.EMPTY
305
+ configs = OrderedATNConfigSet()
306
+ for i in range(0,len(p.transitions)):
307
+ target = p.transitions[i].target
308
+ c = LexerATNConfig(state=target, alt=i+1, context=initialContext)
309
+ self.closure(input, c, configs, False, False, False)
310
+ return configs
311
+
312
+ # Since the alternatives within any lexer decision are ordered by
313
+ # preference, this method stops pursuing the closure as soon as an accept
314
+ # state is reached. After the first accept state is reached by depth-first
315
+ # search from {@code config}, all other (potentially reachable) states for
316
+ # this rule would have a lower priority.
317
+ #
318
+ # @return {@code true} if an accept state is reached, otherwise
319
+ # {@code false}.
320
+ def closure(self, input:InputStream, config:LexerATNConfig, configs:ATNConfigSet, currentAltReachedAcceptState:bool,
321
+ speculative:bool, treatEofAsEpsilon:bool):
322
+ if LexerATNSimulator.debug:
323
+ print("closure(" + str(config) + ")")
324
+
325
+ if isinstance( config.state, RuleStopState ):
326
+ if LexerATNSimulator.debug:
327
+ if self.recog is not None:
328
+ print("closure at", self.recog.symbolicNames[config.state.ruleIndex], "rule stop", str(config))
329
+ else:
330
+ print("closure at rule stop", str(config))
331
+
332
+ if config.context is None or config.context.hasEmptyPath():
333
+ if config.context is None or config.context.isEmpty():
334
+ configs.add(config)
335
+ return True
336
+ else:
337
+ configs.add(LexerATNConfig(state=config.state, config=config, context=PredictionContext.EMPTY))
338
+ currentAltReachedAcceptState = True
339
+
340
+ if config.context is not None and not config.context.isEmpty():
341
+ for i in range(0,len(config.context)):
342
+ if config.context.getReturnState(i) != PredictionContext.EMPTY_RETURN_STATE:
343
+ newContext = config.context.getParent(i) # "pop" return state
344
+ returnState = self.atn.states[config.context.getReturnState(i)]
345
+ c = LexerATNConfig(state=returnState, config=config, context=newContext)
346
+ currentAltReachedAcceptState = self.closure(input, c, configs,
347
+ currentAltReachedAcceptState, speculative, treatEofAsEpsilon)
348
+
349
+ return currentAltReachedAcceptState
350
+
351
+ # optimization
352
+ if not config.state.epsilonOnlyTransitions:
353
+ if not currentAltReachedAcceptState or not config.passedThroughNonGreedyDecision:
354
+ configs.add(config)
355
+
356
+ for t in config.state.transitions:
357
+ c = self.getEpsilonTarget(input, config, t, configs, speculative, treatEofAsEpsilon)
358
+ if c is not None:
359
+ currentAltReachedAcceptState = self.closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon)
360
+
361
+ return currentAltReachedAcceptState
362
+
363
+ # side-effect: can alter configs.hasSemanticContext
364
+ def getEpsilonTarget(self, input:InputStream, config:LexerATNConfig, t:Transition, configs:ATNConfigSet,
365
+ speculative:bool, treatEofAsEpsilon:bool):
366
+ c = None
367
+ if t.serializationType==Transition.RULE:
368
+ newContext = SingletonPredictionContext.create(config.context, t.followState.stateNumber)
369
+ c = LexerATNConfig(state=t.target, config=config, context=newContext)
370
+
371
+ elif t.serializationType==Transition.PRECEDENCE:
372
+ raise UnsupportedOperationException("Precedence predicates are not supported in lexers.")
373
+
374
+ elif t.serializationType==Transition.PREDICATE:
375
+ # Track traversing semantic predicates. If we traverse,
376
+ # we cannot add a DFA state for this "reach" computation
377
+ # because the DFA would not test the predicate again in the
378
+ # future. Rather than creating collections of semantic predicates
379
+ # like v3 and testing them on prediction, v4 will test them on the
380
+ # fly all the time using the ATN not the DFA. This is slower but
381
+ # semantically it's not used that often. One of the key elements to
382
+ # this predicate mechanism is not adding DFA states that see
383
+ # predicates immediately afterwards in the ATN. For example,
384
+
385
+ # a : ID {p1}? | ID {p2}? ;
386
+
387
+ # should create the start state for rule 'a' (to save start state
388
+ # competition), but should not create target of ID state. The
389
+ # collection of ATN states the following ID references includes
390
+ # states reached by traversing predicates. Since this is when we
391
+ # test them, we cannot cash the DFA state target of ID.
392
+
393
+ if LexerATNSimulator.debug:
394
+ print("EVAL rule "+ str(t.ruleIndex) + ":" + str(t.predIndex))
395
+ configs.hasSemanticContext = True
396
+ if self.evaluatePredicate(input, t.ruleIndex, t.predIndex, speculative):
397
+ c = LexerATNConfig(state=t.target, config=config)
398
+
399
+ elif t.serializationType==Transition.ACTION:
400
+ if config.context is None or config.context.hasEmptyPath():
401
+ # execute actions anywhere in the start rule for a token.
402
+ #
403
+ # TODO: if the entry rule is invoked recursively, some
404
+ # actions may be executed during the recursive call. The
405
+ # problem can appear when hasEmptyPath() is true but
406
+ # isEmpty() is false. In this case, the config needs to be
407
+ # split into two contexts - one with just the empty path
408
+ # and another with everything but the empty path.
409
+ # Unfortunately, the current algorithm does not allow
410
+ # getEpsilonTarget to return two configurations, so
411
+ # additional modifications are needed before we can support
412
+ # the split operation.
413
+ lexerActionExecutor = LexerActionExecutor.append(config.lexerActionExecutor,
414
+ self.atn.lexerActions[t.actionIndex])
415
+ c = LexerATNConfig(state=t.target, config=config, lexerActionExecutor=lexerActionExecutor)
416
+
417
+ else:
418
+ # ignore actions in referenced rules
419
+ c = LexerATNConfig(state=t.target, config=config)
420
+
421
+ elif t.serializationType==Transition.EPSILON:
422
+ c = LexerATNConfig(state=t.target, config=config)
423
+
424
+ elif t.serializationType in [ Transition.ATOM, Transition.RANGE, Transition.SET ]:
425
+ if treatEofAsEpsilon:
426
+ if t.matches(Token.EOF, 0, self.MAX_CHAR_VALUE):
427
+ c = LexerATNConfig(state=t.target, config=config)
428
+
429
+ return c
430
+
431
+ # Evaluate a predicate specified in the lexer.
432
+ #
433
+ # <p>If {@code speculative} is {@code true}, this method was called before
434
+ # {@link #consume} for the matched character. This method should call
435
+ # {@link #consume} before evaluating the predicate to ensure position
436
+ # sensitive values, including {@link Lexer#getText}, {@link Lexer#getLine},
437
+ # and {@link Lexer#getcolumn}, properly reflect the current
438
+ # lexer state. This method should restore {@code input} and the simulator
439
+ # to the original state before returning (i.e. undo the actions made by the
440
+ # call to {@link #consume}.</p>
441
+ #
442
+ # @param input The input stream.
443
+ # @param ruleIndex The rule containing the predicate.
444
+ # @param predIndex The index of the predicate within the rule.
445
+ # @param speculative {@code true} if the current index in {@code input} is
446
+ # one character before the predicate's location.
447
+ #
448
+ # @return {@code true} if the specified predicate evaluates to
449
+ # {@code true}.
450
+ #/
451
+ def evaluatePredicate(self, input:InputStream, ruleIndex:int, predIndex:int, speculative:bool):
452
+ # assume true if no recognizer was provided
453
+ if self.recog is None:
454
+ return True
455
+
456
+ if not speculative:
457
+ return self.recog.sempred(None, ruleIndex, predIndex)
458
+
459
+ savedcolumn = self.column
460
+ savedLine = self.line
461
+ index = input.index
462
+ marker = input.mark()
463
+ try:
464
+ self.consume(input)
465
+ return self.recog.sempred(None, ruleIndex, predIndex)
466
+ finally:
467
+ self.column = savedcolumn
468
+ self.line = savedLine
469
+ input.seek(index)
470
+ input.release(marker)
471
+
472
+ def captureSimState(self, settings:SimState, input:InputStream, dfaState:DFAState):
473
+ settings.index = input.index
474
+ settings.line = self.line
475
+ settings.column = self.column
476
+ settings.dfaState = dfaState
477
+
478
+ def addDFAEdge(self, from_:DFAState, tk:int, to:DFAState=None, cfgs:ATNConfigSet=None) -> DFAState:
479
+
480
+ if to is None and cfgs is not None:
481
+ # leading to this call, ATNConfigSet.hasSemanticContext is used as a
482
+ # marker indicating dynamic predicate evaluation makes this edge
483
+ # dependent on the specific input sequence, so the static edge in the
484
+ # DFA should be omitted. The target DFAState is still created since
485
+ # execATN has the ability to resynchronize with the DFA state cache
486
+ # following the predicate evaluation step.
487
+ #
488
+ # TJP notes: next time through the DFA, we see a pred again and eval.
489
+ # If that gets us to a previously created (but dangling) DFA
490
+ # state, we can continue in pure DFA mode from there.
491
+ #/
492
+ suppressEdge = cfgs.hasSemanticContext
493
+ cfgs.hasSemanticContext = False
494
+
495
+ to = self.addDFAState(cfgs)
496
+
497
+ if suppressEdge:
498
+ return to
499
+
500
+ # add the edge
501
+ if tk < self.MIN_DFA_EDGE or tk > self.MAX_DFA_EDGE:
502
+ # Only track edges within the DFA bounds
503
+ return to
504
+
505
+ if LexerATNSimulator.debug:
506
+ print("EDGE " + str(from_) + " -> " + str(to) + " upon "+ chr(tk))
507
+
508
+ if from_.edges is None:
509
+ # make room for tokens 1..n and -1 masquerading as index 0
510
+ from_.edges = [ None ] * (self.MAX_DFA_EDGE - self.MIN_DFA_EDGE + 1)
511
+
512
+ from_.edges[tk - self.MIN_DFA_EDGE] = to # connect
513
+
514
+ return to
515
+
516
+
517
+ # Add a new DFA state if there isn't one with this set of
518
+ # configurations already. This method also detects the first
519
+ # configuration containing an ATN rule stop state. Later, when
520
+ # traversing the DFA, we will know which rule to accept.
521
+ def addDFAState(self, configs:ATNConfigSet) -> DFAState:
522
+
523
+ proposed = DFAState(configs=configs)
524
+ firstConfigWithRuleStopState = next((cfg for cfg in configs if isinstance(cfg.state, RuleStopState)), None)
525
+
526
+ if firstConfigWithRuleStopState is not None:
527
+ proposed.isAcceptState = True
528
+ proposed.lexerActionExecutor = firstConfigWithRuleStopState.lexerActionExecutor
529
+ proposed.prediction = self.atn.ruleToTokenType[firstConfigWithRuleStopState.state.ruleIndex]
530
+
531
+ dfa = self.decisionToDFA[self.mode]
532
+ existing = dfa.states.get(proposed, None)
533
+ if existing is not None:
534
+ return existing
535
+
536
+ newState = proposed
537
+
538
+ newState.stateNumber = len(dfa.states)
539
+ configs.setReadonly(True)
540
+ newState.configs = configs
541
+ dfa.states[newState] = newState
542
+ return newState
543
+
544
+ def getDFA(self, mode:int):
545
+ return self.decisionToDFA[mode]
546
+
547
+ # Get the text matched so far for the current token.
548
+ def getText(self, input:InputStream):
549
+ # index is first lookahead char, don't include.
550
+ return input.getText(self.startIndex, input.index-1)
551
+
552
+ def consume(self, input:InputStream):
553
+ curChar = input.LA(1)
554
+ if curChar==ord('\n'):
555
+ self.line += 1
556
+ self.column = 0
557
+ else:
558
+ self.column += 1
559
+ input.consume()
560
+
561
+ def getTokenName(self, t:int):
562
+ if t==-1:
563
+ return "EOF"
564
+ else:
565
+ return "'" + chr(t) + "'"
566
+
567
+
568
+ LexerATNSimulator.ERROR = DFAState(0x7FFFFFFF, ATNConfigSet())
569
+
570
+ del Lexer
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/LexerAction.py ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ from enum import IntEnum
8
+
9
+ # need forward declaration
10
+ Lexer = None
11
+
12
+
13
+ class LexerActionType(IntEnum):
14
+
15
+ CHANNEL = 0 #The type of a {@link LexerChannelAction} action.
16
+ CUSTOM = 1 #The type of a {@link LexerCustomAction} action.
17
+ MODE = 2 #The type of a {@link LexerModeAction} action.
18
+ MORE = 3 #The type of a {@link LexerMoreAction} action.
19
+ POP_MODE = 4 #The type of a {@link LexerPopModeAction} action.
20
+ PUSH_MODE = 5 #The type of a {@link LexerPushModeAction} action.
21
+ SKIP = 6 #The type of a {@link LexerSkipAction} action.
22
+ TYPE = 7 #The type of a {@link LexerTypeAction} action.
23
+
24
+ class LexerAction(object):
25
+ __slots__ = ('actionType', 'isPositionDependent')
26
+
27
+ def __init__(self, action:LexerActionType):
28
+ self.actionType = action
29
+ self.isPositionDependent = False
30
+
31
+ def __hash__(self):
32
+ return hash(self.actionType)
33
+
34
+ def __eq__(self, other):
35
+ return self is other
36
+
37
+
38
+ #
39
+ # Implements the {@code skip} lexer action by calling {@link Lexer#skip}.
40
+ #
41
+ # <p>The {@code skip} command does not have any parameters, so this action is
42
+ # implemented as a singleton instance exposed by {@link #INSTANCE}.</p>
43
+ class LexerSkipAction(LexerAction):
44
+
45
+ # Provides a singleton instance of this parameterless lexer action.
46
+ INSTANCE = None
47
+
48
+ def __init__(self):
49
+ super().__init__(LexerActionType.SKIP)
50
+
51
+ def execute(self, lexer:Lexer):
52
+ lexer.skip()
53
+
54
+ def __str__(self):
55
+ return "skip"
56
+
57
+ LexerSkipAction.INSTANCE = LexerSkipAction()
58
+
59
+ # Implements the {@code type} lexer action by calling {@link Lexer#setType}
60
+ # with the assigned type.
61
+ class LexerTypeAction(LexerAction):
62
+ __slots__ = 'type'
63
+
64
+ def __init__(self, type:int):
65
+ super().__init__(LexerActionType.TYPE)
66
+ self.type = type
67
+
68
+ def execute(self, lexer:Lexer):
69
+ lexer.type = self.type
70
+
71
+ def __hash__(self):
72
+ return hash((self.actionType, self.type))
73
+
74
+ def __eq__(self, other):
75
+ if self is other:
76
+ return True
77
+ elif not isinstance(other, LexerTypeAction):
78
+ return False
79
+ else:
80
+ return self.type == other.type
81
+
82
+ def __str__(self):
83
+ return "type(" + str(self.type) + ")"
84
+
85
+
86
+ # Implements the {@code pushMode} lexer action by calling
87
+ # {@link Lexer#pushMode} with the assigned mode.
88
+ class LexerPushModeAction(LexerAction):
89
+ __slots__ = 'mode'
90
+
91
+ def __init__(self, mode:int):
92
+ super().__init__(LexerActionType.PUSH_MODE)
93
+ self.mode = mode
94
+
95
+ # <p>This action is implemented by calling {@link Lexer#pushMode} with the
96
+ # value provided by {@link #getMode}.</p>
97
+ def execute(self, lexer:Lexer):
98
+ lexer.pushMode(self.mode)
99
+
100
+ def __hash__(self):
101
+ return hash((self.actionType, self.mode))
102
+
103
+ def __eq__(self, other):
104
+ if self is other:
105
+ return True
106
+ elif not isinstance(other, LexerPushModeAction):
107
+ return False
108
+ else:
109
+ return self.mode == other.mode
110
+
111
+ def __str__(self):
112
+ return "pushMode(" + str(self.mode) + ")"
113
+
114
+
115
+ # Implements the {@code popMode} lexer action by calling {@link Lexer#popMode}.
116
+ #
117
+ # <p>The {@code popMode} command does not have any parameters, so this action is
118
+ # implemented as a singleton instance exposed by {@link #INSTANCE}.</p>
119
+ class LexerPopModeAction(LexerAction):
120
+
121
+ INSTANCE = None
122
+
123
+ def __init__(self):
124
+ super().__init__(LexerActionType.POP_MODE)
125
+
126
+ # <p>This action is implemented by calling {@link Lexer#popMode}.</p>
127
+ def execute(self, lexer:Lexer):
128
+ lexer.popMode()
129
+
130
+ def __str__(self):
131
+ return "popMode"
132
+
133
+ LexerPopModeAction.INSTANCE = LexerPopModeAction()
134
+
135
+ # Implements the {@code more} lexer action by calling {@link Lexer#more}.
136
+ #
137
+ # <p>The {@code more} command does not have any parameters, so this action is
138
+ # implemented as a singleton instance exposed by {@link #INSTANCE}.</p>
139
+ class LexerMoreAction(LexerAction):
140
+
141
+ INSTANCE = None
142
+
143
+ def __init__(self):
144
+ super().__init__(LexerActionType.MORE)
145
+
146
+ # <p>This action is implemented by calling {@link Lexer#popMode}.</p>
147
+ def execute(self, lexer:Lexer):
148
+ lexer.more()
149
+
150
+ def __str__(self):
151
+ return "more"
152
+
153
+ LexerMoreAction.INSTANCE = LexerMoreAction()
154
+
155
+ # Implements the {@code mode} lexer action by calling {@link Lexer#mode} with
156
+ # the assigned mode.
157
+ class LexerModeAction(LexerAction):
158
+ __slots__ = 'mode'
159
+
160
+ def __init__(self, mode:int):
161
+ super().__init__(LexerActionType.MODE)
162
+ self.mode = mode
163
+
164
+ # <p>This action is implemented by calling {@link Lexer#mode} with the
165
+ # value provided by {@link #getMode}.</p>
166
+ def execute(self, lexer:Lexer):
167
+ lexer.mode(self.mode)
168
+
169
+ def __hash__(self):
170
+ return hash((self.actionType, self.mode))
171
+
172
+ def __eq__(self, other):
173
+ if self is other:
174
+ return True
175
+ elif not isinstance(other, LexerModeAction):
176
+ return False
177
+ else:
178
+ return self.mode == other.mode
179
+
180
+ def __str__(self):
181
+ return "mode(" + str(self.mode) + ")"
182
+
183
+ # Executes a custom lexer action by calling {@link Recognizer#action} with the
184
+ # rule and action indexes assigned to the custom action. The implementation of
185
+ # a custom action is added to the generated code for the lexer in an override
186
+ # of {@link Recognizer#action} when the grammar is compiled.
187
+ #
188
+ # <p>This class may represent embedded actions created with the <code>{...}</code>
189
+ # syntax in ANTLR 4, as well as actions created for lexer commands where the
190
+ # command argument could not be evaluated when the grammar was compiled.</p>
191
+
192
+ class LexerCustomAction(LexerAction):
193
+ __slots__ = ('ruleIndex', 'actionIndex')
194
+
195
+ # Constructs a custom lexer action with the specified rule and action
196
+ # indexes.
197
+ #
198
+ # @param ruleIndex The rule index to use for calls to
199
+ # {@link Recognizer#action}.
200
+ # @param actionIndex The action index to use for calls to
201
+ # {@link Recognizer#action}.
202
+ #/
203
+ def __init__(self, ruleIndex:int, actionIndex:int):
204
+ super().__init__(LexerActionType.CUSTOM)
205
+ self.ruleIndex = ruleIndex
206
+ self.actionIndex = actionIndex
207
+ self.isPositionDependent = True
208
+
209
+ # <p>Custom actions are implemented by calling {@link Lexer#action} with the
210
+ # appropriate rule and action indexes.</p>
211
+ def execute(self, lexer:Lexer):
212
+ lexer.action(None, self.ruleIndex, self.actionIndex)
213
+
214
+ def __hash__(self):
215
+ return hash((self.actionType, self.ruleIndex, self.actionIndex))
216
+
217
+ def __eq__(self, other):
218
+ if self is other:
219
+ return True
220
+ elif not isinstance(other, LexerCustomAction):
221
+ return False
222
+ else:
223
+ return self.ruleIndex == other.ruleIndex and self.actionIndex == other.actionIndex
224
+
225
+ # Implements the {@code channel} lexer action by calling
226
+ # {@link Lexer#setChannel} with the assigned channel.
227
+ class LexerChannelAction(LexerAction):
228
+ __slots__ = 'channel'
229
+
230
+ # Constructs a new {@code channel} action with the specified channel value.
231
+ # @param channel The channel value to pass to {@link Lexer#setChannel}.
232
+ def __init__(self, channel:int):
233
+ super().__init__(LexerActionType.CHANNEL)
234
+ self.channel = channel
235
+
236
+ # <p>This action is implemented by calling {@link Lexer#setChannel} with the
237
+ # value provided by {@link #getChannel}.</p>
238
+ def execute(self, lexer:Lexer):
239
+ lexer._channel = self.channel
240
+
241
+ def __hash__(self):
242
+ return hash((self.actionType, self.channel))
243
+
244
+ def __eq__(self, other):
245
+ if self is other:
246
+ return True
247
+ elif not isinstance(other, LexerChannelAction):
248
+ return False
249
+ else:
250
+ return self.channel == other.channel
251
+
252
+ def __str__(self):
253
+ return "channel(" + str(self.channel) + ")"
254
+
255
+ # This implementation of {@link LexerAction} is used for tracking input offsets
256
+ # for position-dependent actions within a {@link LexerActionExecutor}.
257
+ #
258
+ # <p>This action is not serialized as part of the ATN, and is only required for
259
+ # position-dependent lexer actions which appear at a location other than the
260
+ # end of a rule. For more information about DFA optimizations employed for
261
+ # lexer actions, see {@link LexerActionExecutor#append} and
262
+ # {@link LexerActionExecutor#fixOffsetBeforeMatch}.</p>
263
+ class LexerIndexedCustomAction(LexerAction):
264
+ __slots__ = ('offset', 'action')
265
+
266
+ # Constructs a new indexed custom action by associating a character offset
267
+ # with a {@link LexerAction}.
268
+ #
269
+ # <p>Note: This class is only required for lexer actions for which
270
+ # {@link LexerAction#isPositionDependent} returns {@code true}.</p>
271
+ #
272
+ # @param offset The offset into the input {@link CharStream}, relative to
273
+ # the token start index, at which the specified lexer action should be
274
+ # executed.
275
+ # @param action The lexer action to execute at a particular offset in the
276
+ # input {@link CharStream}.
277
+ def __init__(self, offset:int, action:LexerAction):
278
+ super().__init__(action.actionType)
279
+ self.offset = offset
280
+ self.action = action
281
+ self.isPositionDependent = True
282
+
283
+ # <p>This method calls {@link #execute} on the result of {@link #getAction}
284
+ # using the provided {@code lexer}.</p>
285
+ def execute(self, lexer:Lexer):
286
+ # assume the input stream position was properly set by the calling code
287
+ self.action.execute(lexer)
288
+
289
+ def __hash__(self):
290
+ return hash((self.actionType, self.offset, self.action))
291
+
292
+ def __eq__(self, other):
293
+ if self is other:
294
+ return True
295
+ elif not isinstance(other, LexerIndexedCustomAction):
296
+ return False
297
+ else:
298
+ return self.offset == other.offset and self.action == other.action
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/LexerActionExecutor.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #/
6
+
7
+ # Represents an executor for a sequence of lexer actions which traversed during
8
+ # the matching operation of a lexer rule (token).
9
+ #
10
+ # <p>The executor tracks position information for position-dependent lexer actions
11
+ # efficiently, ensuring that actions appearing only at the end of the rule do
12
+ # not cause bloating of the {@link DFA} created for the lexer.</p>
13
+
14
+
15
+ from antlr4.InputStream import InputStream
16
+ from antlr4.atn.LexerAction import LexerAction, LexerIndexedCustomAction
17
+
18
+ # need a forward declaration
19
+ Lexer = None
20
+ LexerActionExecutor = None
21
+
22
+ class LexerActionExecutor(object):
23
+ __slots__ = ('lexerActions', 'hashCode')
24
+
25
+ def __init__(self, lexerActions:list=list()):
26
+ self.lexerActions = lexerActions
27
+ # Caches the result of {@link #hashCode} since the hash code is an element
28
+ # of the performance-critical {@link LexerATNConfig#hashCode} operation.
29
+ self.hashCode = hash("".join([str(la) for la in lexerActions]))
30
+
31
+
32
+ # Creates a {@link LexerActionExecutor} which executes the actions for
33
+ # the input {@code lexerActionExecutor} followed by a specified
34
+ # {@code lexerAction}.
35
+ #
36
+ # @param lexerActionExecutor The executor for actions already traversed by
37
+ # the lexer while matching a token within a particular
38
+ # {@link LexerATNConfig}. If this is {@code null}, the method behaves as
39
+ # though it were an empty executor.
40
+ # @param lexerAction The lexer action to execute after the actions
41
+ # specified in {@code lexerActionExecutor}.
42
+ #
43
+ # @return A {@link LexerActionExecutor} for executing the combine actions
44
+ # of {@code lexerActionExecutor} and {@code lexerAction}.
45
+ @staticmethod
46
+ def append(lexerActionExecutor:LexerActionExecutor , lexerAction:LexerAction ):
47
+ if lexerActionExecutor is None:
48
+ return LexerActionExecutor([ lexerAction ])
49
+
50
+ lexerActions = lexerActionExecutor.lexerActions + [ lexerAction ]
51
+ return LexerActionExecutor(lexerActions)
52
+
53
+ # Creates a {@link LexerActionExecutor} which encodes the current offset
54
+ # for position-dependent lexer actions.
55
+ #
56
+ # <p>Normally, when the executor encounters lexer actions where
57
+ # {@link LexerAction#isPositionDependent} returns {@code true}, it calls
58
+ # {@link IntStream#seek} on the input {@link CharStream} to set the input
59
+ # position to the <em>end</em> of the current token. This behavior provides
60
+ # for efficient DFA representation of lexer actions which appear at the end
61
+ # of a lexer rule, even when the lexer rule matches a variable number of
62
+ # characters.</p>
63
+ #
64
+ # <p>Prior to traversing a match transition in the ATN, the current offset
65
+ # from the token start index is assigned to all position-dependent lexer
66
+ # actions which have not already been assigned a fixed offset. By storing
67
+ # the offsets relative to the token start index, the DFA representation of
68
+ # lexer actions which appear in the middle of tokens remains efficient due
69
+ # to sharing among tokens of the same length, regardless of their absolute
70
+ # position in the input stream.</p>
71
+ #
72
+ # <p>If the current executor already has offsets assigned to all
73
+ # position-dependent lexer actions, the method returns {@code this}.</p>
74
+ #
75
+ # @param offset The current offset to assign to all position-dependent
76
+ # lexer actions which do not already have offsets assigned.
77
+ #
78
+ # @return A {@link LexerActionExecutor} which stores input stream offsets
79
+ # for all position-dependent lexer actions.
80
+ #/
81
+ def fixOffsetBeforeMatch(self, offset:int):
82
+ updatedLexerActions = None
83
+ for i in range(0, len(self.lexerActions)):
84
+ if self.lexerActions[i].isPositionDependent and not isinstance(self.lexerActions[i], LexerIndexedCustomAction):
85
+ if updatedLexerActions is None:
86
+ updatedLexerActions = [ la for la in self.lexerActions ]
87
+ updatedLexerActions[i] = LexerIndexedCustomAction(offset, self.lexerActions[i])
88
+
89
+ if updatedLexerActions is None:
90
+ return self
91
+ else:
92
+ return LexerActionExecutor(updatedLexerActions)
93
+
94
+
95
+ # Execute the actions encapsulated by this executor within the context of a
96
+ # particular {@link Lexer}.
97
+ #
98
+ # <p>This method calls {@link IntStream#seek} to set the position of the
99
+ # {@code input} {@link CharStream} prior to calling
100
+ # {@link LexerAction#execute} on a position-dependent action. Before the
101
+ # method returns, the input position will be restored to the same position
102
+ # it was in when the method was invoked.</p>
103
+ #
104
+ # @param lexer The lexer instance.
105
+ # @param input The input stream which is the source for the current token.
106
+ # When this method is called, the current {@link IntStream#index} for
107
+ # {@code input} should be the start of the following token, i.e. 1
108
+ # character past the end of the current token.
109
+ # @param startIndex The token start index. This value may be passed to
110
+ # {@link IntStream#seek} to set the {@code input} position to the beginning
111
+ # of the token.
112
+ #/
113
+ def execute(self, lexer:Lexer, input:InputStream, startIndex:int):
114
+ requiresSeek = False
115
+ stopIndex = input.index
116
+ try:
117
+ for lexerAction in self.lexerActions:
118
+ if isinstance(lexerAction, LexerIndexedCustomAction):
119
+ offset = lexerAction.offset
120
+ input.seek(startIndex + offset)
121
+ lexerAction = lexerAction.action
122
+ requiresSeek = (startIndex + offset) != stopIndex
123
+ elif lexerAction.isPositionDependent:
124
+ input.seek(stopIndex)
125
+ requiresSeek = False
126
+ lexerAction.execute(lexer)
127
+ finally:
128
+ if requiresSeek:
129
+ input.seek(stopIndex)
130
+
131
+ def __hash__(self):
132
+ return self.hashCode
133
+
134
+ def __eq__(self, other):
135
+ if self is other:
136
+ return True
137
+ elif not isinstance(other, LexerActionExecutor):
138
+ return False
139
+ else:
140
+ return self.hashCode == other.hashCode \
141
+ and self.lexerActions == other.lexerActions
142
+
143
+ del Lexer
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/ParserATNSimulator.py ADDED
@@ -0,0 +1,1649 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ #
8
+ # The embodiment of the adaptive LL(*), ALL(*), parsing strategy.
9
+ #
10
+ # <p>
11
+ # The basic complexity of the adaptive strategy makes it harder to understand.
12
+ # We begin with ATN simulation to build paths in a DFA. Subsequent prediction
13
+ # requests go through the DFA first. If they reach a state without an edge for
14
+ # the current symbol, the algorithm fails over to the ATN simulation to
15
+ # complete the DFA path for the current input (until it finds a conflict state
16
+ # or uniquely predicting state).</p>
17
+ #
18
+ # <p>
19
+ # All of that is done without using the outer context because we want to create
20
+ # a DFA that is not dependent upon the rule invocation stack when we do a
21
+ # prediction. One DFA works in all contexts. We avoid using context not
22
+ # necessarily because it's slower, although it can be, but because of the DFA
23
+ # caching problem. The closure routine only considers the rule invocation stack
24
+ # created during prediction beginning in the decision rule. For example, if
25
+ # prediction occurs without invoking another rule's ATN, there are no context
26
+ # stacks in the configurations. When lack of context leads to a conflict, we
27
+ # don't know if it's an ambiguity or a weakness in the strong LL(*) parsing
28
+ # strategy (versus full LL(*)).</p>
29
+ #
30
+ # <p>
31
+ # When SLL yields a configuration set with conflict, we rewind the input and
32
+ # retry the ATN simulation, this time using full outer context without adding
33
+ # to the DFA. Configuration context stacks will be the full invocation stacks
34
+ # from the start rule. If we get a conflict using full context, then we can
35
+ # definitively say we have a true ambiguity for that input sequence. If we
36
+ # don't get a conflict, it implies that the decision is sensitive to the outer
37
+ # context. (It is not context-sensitive in the sense of context-sensitive
38
+ # grammars.)</p>
39
+ #
40
+ # <p>
41
+ # The next time we reach this DFA state with an SLL conflict, through DFA
42
+ # simulation, we will again retry the ATN simulation using full context mode.
43
+ # This is slow because we can't save the results and have to "interpret" the
44
+ # ATN each time we get that input.</p>
45
+ #
46
+ # <p>
47
+ # <strong>CACHING FULL CONTEXT PREDICTIONS</strong></p>
48
+ #
49
+ # <p>
50
+ # We could cache results from full context to predicted alternative easily and
51
+ # that saves a lot of time but doesn't work in presence of predicates. The set
52
+ # of visible predicates from the ATN start state changes depending on the
53
+ # context, because closure can fall off the end of a rule. I tried to cache
54
+ # tuples (stack context, semantic context, predicted alt) but it was slower
55
+ # than interpreting and much more complicated. Also required a huge amount of
56
+ # memory. The goal is not to create the world's fastest parser anyway. I'd like
57
+ # to keep this algorithm simple. By launching multiple threads, we can improve
58
+ # the speed of parsing across a large number of files.</p>
59
+ #
60
+ # <p>
61
+ # There is no strict ordering between the amount of input used by SLL vs LL,
62
+ # which makes it really hard to build a cache for full context. Let's say that
63
+ # we have input A B C that leads to an SLL conflict with full context X. That
64
+ # implies that using X we might only use A B but we could also use A B C D to
65
+ # resolve conflict. Input A B C D could predict alternative 1 in one position
66
+ # in the input and A B C E could predict alternative 2 in another position in
67
+ # input. The conflicting SLL configurations could still be non-unique in the
68
+ # full context prediction, which would lead us to requiring more input than the
69
+ # original A B C. To make a prediction cache work, we have to track the exact
70
+ # input used during the previous prediction. That amounts to a cache that maps
71
+ # X to a specific DFA for that context.</p>
72
+ #
73
+ # <p>
74
+ # Something should be done for left-recursive expression predictions. They are
75
+ # likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry
76
+ # with full LL thing Sam does.</p>
77
+ #
78
+ # <p>
79
+ # <strong>AVOIDING FULL CONTEXT PREDICTION</strong></p>
80
+ #
81
+ # <p>
82
+ # We avoid doing full context retry when the outer context is empty, we did not
83
+ # dip into the outer context by falling off the end of the decision state rule,
84
+ # or when we force SLL mode.</p>
85
+ #
86
+ # <p>
87
+ # As an example of the not dip into outer context case, consider as super
88
+ # constructor calls versus function calls. One grammar might look like
89
+ # this:</p>
90
+ #
91
+ # <pre>
92
+ # ctorBody
93
+ # : '{' superCall? stat* '}'
94
+ # ;
95
+ # </pre>
96
+ #
97
+ # <p>
98
+ # Or, you might see something like</p>
99
+ #
100
+ # <pre>
101
+ # stat
102
+ # : superCall ';'
103
+ # | expression ';'
104
+ # | ...
105
+ # ;
106
+ # </pre>
107
+ #
108
+ # <p>
109
+ # In both cases I believe that no closure operations will dip into the outer
110
+ # context. In the first case ctorBody in the worst case will stop at the '}'.
111
+ # In the 2nd case it should stop at the ';'. Both cases should stay within the
112
+ # entry rule and not dip into the outer context.</p>
113
+ #
114
+ # <p>
115
+ # <strong>PREDICATES</strong></p>
116
+ #
117
+ # <p>
118
+ # Predicates are always evaluated if present in either SLL or LL both. SLL and
119
+ # LL simulation deals with predicates differently. SLL collects predicates as
120
+ # it performs closure operations like ANTLR v3 did. It delays predicate
121
+ # evaluation until it reaches and accept state. This allows us to cache the SLL
122
+ # ATN simulation whereas, if we had evaluated predicates on-the-fly during
123
+ # closure, the DFA state configuration sets would be different and we couldn't
124
+ # build up a suitable DFA.</p>
125
+ #
126
+ # <p>
127
+ # When building a DFA accept state during ATN simulation, we evaluate any
128
+ # predicates and return the sole semantically valid alternative. If there is
129
+ # more than 1 alternative, we report an ambiguity. If there are 0 alternatives,
130
+ # we throw an exception. Alternatives without predicates act like they have
131
+ # true predicates. The simple way to think about it is to strip away all
132
+ # alternatives with false predicates and choose the minimum alternative that
133
+ # remains.</p>
134
+ #
135
+ # <p>
136
+ # When we start in the DFA and reach an accept state that's predicated, we test
137
+ # those and return the minimum semantically viable alternative. If no
138
+ # alternatives are viable, we throw an exception.</p>
139
+ #
140
+ # <p>
141
+ # During full LL ATN simulation, closure always evaluates predicates and
142
+ # on-the-fly. This is crucial to reducing the configuration set size during
143
+ # closure. It hits a landmine when parsing with the Java grammar, for example,
144
+ # without this on-the-fly evaluation.</p>
145
+ #
146
+ # <p>
147
+ # <strong>SHARING DFA</strong></p>
148
+ #
149
+ # <p>
150
+ # All instances of the same parser share the same decision DFAs through a
151
+ # static field. Each instance gets its own ATN simulator but they share the
152
+ # same {@link #decisionToDFA} field. They also share a
153
+ # {@link PredictionContextCache} object that makes sure that all
154
+ # {@link PredictionContext} objects are shared among the DFA states. This makes
155
+ # a big size difference.</p>
156
+ #
157
+ # <p>
158
+ # <strong>THREAD SAFETY</strong></p>
159
+ #
160
+ # <p>
161
+ # The {@link ParserATNSimulator} locks on the {@link #decisionToDFA} field when
162
+ # it adds a new DFA object to that array. {@link #addDFAEdge}
163
+ # locks on the DFA for the current decision when setting the
164
+ # {@link DFAState#edges} field. {@link #addDFAState} locks on
165
+ # the DFA for the current decision when looking up a DFA state to see if it
166
+ # already exists. We must make sure that all requests to add DFA states that
167
+ # are equivalent result in the same shared DFA object. This is because lots of
168
+ # threads will be trying to update the DFA at once. The
169
+ # {@link #addDFAState} method also locks inside the DFA lock
170
+ # but this time on the shared context cache when it rebuilds the
171
+ # configurations' {@link PredictionContext} objects using cached
172
+ # subgraphs/nodes. No other locking occurs, even during DFA simulation. This is
173
+ # safe as long as we can guarantee that all threads referencing
174
+ # {@code s.edge[t]} get the same physical target {@link DFAState}, or
175
+ # {@code null}. Once into the DFA, the DFA simulation does not reference the
176
+ # {@link DFA#states} map. It follows the {@link DFAState#edges} field to new
177
+ # targets. The DFA simulator will either find {@link DFAState#edges} to be
178
+ # {@code null}, to be non-{@code null} and {@code dfa.edges[t]} null, or
179
+ # {@code dfa.edges[t]} to be non-null. The
180
+ # {@link #addDFAEdge} method could be racing to set the field
181
+ # but in either case the DFA simulator works; if {@code null}, and requests ATN
182
+ # simulation. It could also race trying to get {@code dfa.edges[t]}, but either
183
+ # way it will work because it's not doing a test and set operation.</p>
184
+ #
185
+ # <p>
186
+ # <strong>Starting with SLL then failing to combined SLL/LL (Two-Stage
187
+ # Parsing)</strong></p>
188
+ #
189
+ # <p>
190
+ # Sam pointed out that if SLL does not give a syntax error, then there is no
191
+ # point in doing full LL, which is slower. We only have to try LL if we get a
192
+ # syntax error. For maximum speed, Sam starts the parser set to pure SLL
193
+ # mode with the {@link BailErrorStrategy}:</p>
194
+ #
195
+ # <pre>
196
+ # parser.{@link Parser#getInterpreter() getInterpreter()}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
197
+ # parser.{@link Parser#setErrorHandler setErrorHandler}(new {@link BailErrorStrategy}());
198
+ # </pre>
199
+ #
200
+ # <p>
201
+ # If it does not get a syntax error, then we're done. If it does get a syntax
202
+ # error, we need to retry with the combined SLL/LL strategy.</p>
203
+ #
204
+ # <p>
205
+ # The reason this works is as follows. If there are no SLL conflicts, then the
206
+ # grammar is SLL (at least for that input set). If there is an SLL conflict,
207
+ # the full LL analysis must yield a set of viable alternatives which is a
208
+ # subset of the alternatives reported by SLL. If the LL set is a singleton,
209
+ # then the grammar is LL but not SLL. If the LL set is the same size as the SLL
210
+ # set, the decision is SLL. If the LL set has size &gt; 1, then that decision
211
+ # is truly ambiguous on the current input. If the LL set is smaller, then the
212
+ # SLL conflict resolution might choose an alternative that the full LL would
213
+ # rule out as a possibility based upon better context information. If that's
214
+ # the case, then the SLL parse will definitely get an error because the full LL
215
+ # analysis says it's not viable. If SLL conflict resolution chooses an
216
+ # alternative within the LL set, them both SLL and LL would choose the same
217
+ # alternative because they both choose the minimum of multiple conflicting
218
+ # alternatives.</p>
219
+ #
220
+ # <p>
221
+ # Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and
222
+ # a smaller LL set called <em>s</em>. If <em>s</em> is {@code {2, 3}}, then SLL
223
+ # parsing will get an error because SLL will pursue alternative 1. If
224
+ # <em>s</em> is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will
225
+ # choose the same alternative because alternative one is the minimum of either
226
+ # set. If <em>s</em> is {@code {2}} or {@code {3}} then SLL will get a syntax
227
+ # error. If <em>s</em> is {@code {1}} then SLL will succeed.</p>
228
+ #
229
+ # <p>
230
+ # Of course, if the input is invalid, then we will get an error for sure in
231
+ # both SLL and LL parsing. Erroneous input will therefore require 2 passes over
232
+ # the input.</p>
233
+ #
234
+ import sys
235
+ from antlr4 import DFA
236
+ from antlr4.PredictionContext import PredictionContextCache, PredictionContext, SingletonPredictionContext, \
237
+ PredictionContextFromRuleContext
238
+ from antlr4.BufferedTokenStream import TokenStream
239
+ from antlr4.Parser import Parser
240
+ from antlr4.ParserRuleContext import ParserRuleContext
241
+ from antlr4.RuleContext import RuleContext
242
+ from antlr4.Token import Token
243
+ from antlr4.Utils import str_list
244
+ from antlr4.atn.ATN import ATN
245
+ from antlr4.atn.ATNConfig import ATNConfig
246
+ from antlr4.atn.ATNConfigSet import ATNConfigSet
247
+ from antlr4.atn.ATNSimulator import ATNSimulator
248
+ from antlr4.atn.ATNState import StarLoopEntryState, DecisionState, RuleStopState, ATNState
249
+ from antlr4.atn.PredictionMode import PredictionMode
250
+ from antlr4.atn.SemanticContext import SemanticContext, AND, andContext, orContext
251
+ from antlr4.atn.Transition import Transition, RuleTransition, ActionTransition, PrecedencePredicateTransition, \
252
+ PredicateTransition, AtomTransition, SetTransition, NotSetTransition
253
+ from antlr4.dfa.DFAState import DFAState, PredPrediction
254
+ from antlr4.error.Errors import NoViableAltException
255
+
256
+
257
+ class ParserATNSimulator(ATNSimulator):
258
+ __slots__ = (
259
+ 'parser', 'decisionToDFA', 'predictionMode', '_input', '_startIndex',
260
+ '_outerContext', '_dfa', 'mergeCache'
261
+ )
262
+
263
+ debug = False
264
+ debug_list_atn_decisions = False
265
+ dfa_debug = False
266
+ retry_debug = False
267
+
268
+
269
+ def __init__(self, parser:Parser, atn:ATN, decisionToDFA:list, sharedContextCache:PredictionContextCache):
270
+ super().__init__(atn, sharedContextCache)
271
+ self.parser = parser
272
+ self.decisionToDFA = decisionToDFA
273
+ # SLL, LL, or LL + exact ambig detection?#
274
+ self.predictionMode = PredictionMode.LL
275
+ # LAME globals to avoid parameters!!!!! I need these down deep in predTransition
276
+ self._input = None
277
+ self._startIndex = 0
278
+ self._outerContext = None
279
+ self._dfa = None
280
+ # Each prediction operation uses a cache for merge of prediction contexts.
281
+ # Don't keep around as it wastes huge amounts of memory. DoubleKeyMap
282
+ # isn't synchronized but we're ok since two threads shouldn't reuse same
283
+ # parser/atnsim object because it can only handle one input at a time.
284
+ # This maps graphs a and b to merged result c. (a,b)&rarr;c. We can avoid
285
+ # the merge if we ever see a and b again. Note that (b,a)&rarr;c should
286
+ # also be examined during cache lookup.
287
+ #
288
+ self.mergeCache = None
289
+
290
+
291
+ def reset(self):
292
+ pass
293
+
294
+ def adaptivePredict(self, input:TokenStream, decision:int, outerContext:ParserRuleContext):
295
+ if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
296
+ print("adaptivePredict decision " + str(decision) +
297
+ " exec LA(1)==" + self.getLookaheadName(input) +
298
+ " line " + str(input.LT(1).line) + ":" +
299
+ str(input.LT(1).column))
300
+ self._input = input
301
+ self._startIndex = input.index
302
+ self._outerContext = outerContext
303
+
304
+ dfa = self.decisionToDFA[decision]
305
+ self._dfa = dfa
306
+ m = input.mark()
307
+ index = input.index
308
+
309
+ # Now we are certain to have a specific decision's DFA
310
+ # But, do we still need an initial state?
311
+ try:
312
+ if dfa.precedenceDfa:
313
+ # the start state for a precedence DFA depends on the current
314
+ # parser precedence, and is provided by a DFA method.
315
+ s0 = dfa.getPrecedenceStartState(self.parser.getPrecedence())
316
+ else:
317
+ # the start state for a "regular" DFA is just s0
318
+ s0 = dfa.s0
319
+
320
+ if s0 is None:
321
+ if outerContext is None:
322
+ outerContext = ParserRuleContext.EMPTY
323
+ if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
324
+ print("predictATN decision " + str(dfa.decision) +
325
+ " exec LA(1)==" + self.getLookaheadName(input) +
326
+ ", outerContext=" + outerContext.toString(self.parser.literalNames, None))
327
+
328
+ fullCtx = False
329
+ s0_closure = self.computeStartState(dfa.atnStartState, ParserRuleContext.EMPTY, fullCtx)
330
+
331
+ if dfa.precedenceDfa:
332
+ # If this is a precedence DFA, we use applyPrecedenceFilter
333
+ # to convert the computed start state to a precedence start
334
+ # state. We then use DFA.setPrecedenceStartState to set the
335
+ # appropriate start state for the precedence level rather
336
+ # than simply setting DFA.s0.
337
+ #
338
+ dfa.s0.configs = s0_closure # not used for prediction but useful to know start configs anyway
339
+ s0_closure = self.applyPrecedenceFilter(s0_closure)
340
+ s0 = self.addDFAState(dfa, DFAState(configs=s0_closure))
341
+ dfa.setPrecedenceStartState(self.parser.getPrecedence(), s0)
342
+ else:
343
+ s0 = self.addDFAState(dfa, DFAState(configs=s0_closure))
344
+ dfa.s0 = s0
345
+
346
+ alt = self.execATN(dfa, s0, input, index, outerContext)
347
+ if ParserATNSimulator.debug:
348
+ print("DFA after predictATN: " + dfa.toString(self.parser.literalNames))
349
+ return alt
350
+ finally:
351
+ self._dfa = None
352
+ self.mergeCache = None # wack cache after each prediction
353
+ input.seek(index)
354
+ input.release(m)
355
+
356
+ # Performs ATN simulation to compute a predicted alternative based
357
+ # upon the remaining input, but also updates the DFA cache to avoid
358
+ # having to traverse the ATN again for the same input sequence.
359
+
360
+ # There are some key conditions we're looking for after computing a new
361
+ # set of ATN configs (proposed DFA state):
362
+ # if the set is empty, there is no viable alternative for current symbol
363
+ # does the state uniquely predict an alternative?
364
+ # does the state have a conflict that would prevent us from
365
+ # putting it on the work list?
366
+
367
+ # We also have some key operations to do:
368
+ # add an edge from previous DFA state to potentially new DFA state, D,
369
+ # upon current symbol but only if adding to work list, which means in all
370
+ # cases except no viable alternative (and possibly non-greedy decisions?)
371
+ # collecting predicates and adding semantic context to DFA accept states
372
+ # adding rule context to context-sensitive DFA accept states
373
+ # consuming an input symbol
374
+ # reporting a conflict
375
+ # reporting an ambiguity
376
+ # reporting a context sensitivity
377
+ # reporting insufficient predicates
378
+
379
+ # cover these cases:
380
+ # dead end
381
+ # single alt
382
+ # single alt + preds
383
+ # conflict
384
+ # conflict + preds
385
+ #
386
+ def execATN(self, dfa:DFA, s0:DFAState, input:TokenStream, startIndex:int, outerContext:ParserRuleContext ):
387
+ if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
388
+ print("execATN decision " + str(dfa.decision) +
389
+ " exec LA(1)==" + self.getLookaheadName(input) +
390
+ " line " + str(input.LT(1).line) + ":" + str(input.LT(1).column))
391
+
392
+ previousD = s0
393
+
394
+ if ParserATNSimulator.debug:
395
+ print("s0 = " + str(s0))
396
+
397
+ t = input.LA(1)
398
+
399
+ while True: # while more work
400
+ D = self.getExistingTargetState(previousD, t)
401
+ if D is None:
402
+ D = self.computeTargetState(dfa, previousD, t)
403
+ if D is self.ERROR:
404
+ # if any configs in previous dipped into outer context, that
405
+ # means that input up to t actually finished entry rule
406
+ # at least for SLL decision. Full LL doesn't dip into outer
407
+ # so don't need special case.
408
+ # We will get an error no matter what so delay until after
409
+ # decision; better error message. Also, no reachable target
410
+ # ATN states in SLL implies LL will also get nowhere.
411
+ # If conflict in states that dip out, choose min since we
412
+ # will get error no matter what.
413
+ e = self.noViableAlt(input, outerContext, previousD.configs, startIndex)
414
+ input.seek(startIndex)
415
+ alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD.configs, outerContext)
416
+ if alt!=ATN.INVALID_ALT_NUMBER:
417
+ return alt
418
+ raise e
419
+
420
+ if D.requiresFullContext and self.predictionMode != PredictionMode.SLL:
421
+ # IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
422
+ conflictingAlts = D.configs.conflictingAlts
423
+ if D.predicates is not None:
424
+ if ParserATNSimulator.debug:
425
+ print("DFA state has preds in DFA sim LL failover")
426
+ conflictIndex = input.index
427
+ if conflictIndex != startIndex:
428
+ input.seek(startIndex)
429
+
430
+ conflictingAlts = self.evalSemanticContext(D.predicates, outerContext, True)
431
+ if len(conflictingAlts)==1:
432
+ if ParserATNSimulator.debug:
433
+ print("Full LL avoided")
434
+ return min(conflictingAlts)
435
+
436
+ if conflictIndex != startIndex:
437
+ # restore the index so reporting the fallback to full
438
+ # context occurs with the index at the correct spot
439
+ input.seek(conflictIndex)
440
+
441
+ if ParserATNSimulator.dfa_debug:
442
+ print("ctx sensitive state " + str(outerContext) +" in " + str(D))
443
+ fullCtx = True
444
+ s0_closure = self.computeStartState(dfa.atnStartState, outerContext, fullCtx)
445
+ self.reportAttemptingFullContext(dfa, conflictingAlts, D.configs, startIndex, input.index)
446
+ alt = self.execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext)
447
+ return alt
448
+
449
+ if D.isAcceptState:
450
+ if D.predicates is None:
451
+ return D.prediction
452
+
453
+ stopIndex = input.index
454
+ input.seek(startIndex)
455
+ alts = self.evalSemanticContext(D.predicates, outerContext, True)
456
+ if len(alts)==0:
457
+ raise self.noViableAlt(input, outerContext, D.configs, startIndex)
458
+ elif len(alts)==1:
459
+ return min(alts)
460
+ else:
461
+ # report ambiguity after predicate evaluation to make sure the correct
462
+ # set of ambig alts is reported.
463
+ self.reportAmbiguity(dfa, D, startIndex, stopIndex, False, alts, D.configs)
464
+ return min(alts)
465
+
466
+ previousD = D
467
+
468
+ if t != Token.EOF:
469
+ input.consume()
470
+ t = input.LA(1)
471
+
472
+ #
473
+ # Get an existing target state for an edge in the DFA. If the target state
474
+ # for the edge has not yet been computed or is otherwise not available,
475
+ # this method returns {@code null}.
476
+ #
477
+ # @param previousD The current DFA state
478
+ # @param t The next input symbol
479
+ # @return The existing target DFA state for the given input symbol
480
+ # {@code t}, or {@code null} if the target state for this edge is not
481
+ # already cached
482
+ #
483
+ def getExistingTargetState(self, previousD:DFAState, t:int):
484
+ edges = previousD.edges
485
+ if edges is None or t + 1 < 0 or t + 1 >= len(edges):
486
+ return None
487
+ else:
488
+ return edges[t + 1]
489
+
490
+ #
491
+ # Compute a target state for an edge in the DFA, and attempt to add the
492
+ # computed state and corresponding edge to the DFA.
493
+ #
494
+ # @param dfa The DFA
495
+ # @param previousD The current DFA state
496
+ # @param t The next input symbol
497
+ #
498
+ # @return The computed target DFA state for the given input symbol
499
+ # {@code t}. If {@code t} does not lead to a valid DFA state, this method
500
+ # returns {@link #ERROR}.
501
+ #
502
+ def computeTargetState(self, dfa:DFA, previousD:DFAState, t:int):
503
+ reach = self.computeReachSet(previousD.configs, t, False)
504
+ if reach is None:
505
+ self.addDFAEdge(dfa, previousD, t, self.ERROR)
506
+ return self.ERROR
507
+
508
+ # create new target state; we'll add to DFA after it's complete
509
+ D = DFAState(configs=reach)
510
+
511
+ predictedAlt = self.getUniqueAlt(reach)
512
+
513
+ if ParserATNSimulator.debug:
514
+ altSubSets = PredictionMode.getConflictingAltSubsets(reach)
515
+ print("SLL altSubSets=" + str(altSubSets) + ", configs=" + str(reach) +
516
+ ", predict=" + str(predictedAlt) + ", allSubsetsConflict=" +
517
+ str(PredictionMode.allSubsetsConflict(altSubSets)) + ", conflictingAlts=" +
518
+ str(self.getConflictingAlts(reach)))
519
+
520
+ if predictedAlt!=ATN.INVALID_ALT_NUMBER:
521
+ # NO CONFLICT, UNIQUELY PREDICTED ALT
522
+ D.isAcceptState = True
523
+ D.configs.uniqueAlt = predictedAlt
524
+ D.prediction = predictedAlt
525
+ elif PredictionMode.hasSLLConflictTerminatingPrediction(self.predictionMode, reach):
526
+ # MORE THAN ONE VIABLE ALTERNATIVE
527
+ D.configs.conflictingAlts = self.getConflictingAlts(reach)
528
+ D.requiresFullContext = True
529
+ # in SLL-only mode, we will stop at this state and return the minimum alt
530
+ D.isAcceptState = True
531
+ D.prediction = min(D.configs.conflictingAlts)
532
+
533
+ if D.isAcceptState and D.configs.hasSemanticContext:
534
+ self.predicateDFAState(D, self.atn.getDecisionState(dfa.decision))
535
+ if D.predicates is not None:
536
+ D.prediction = ATN.INVALID_ALT_NUMBER
537
+
538
+ # all adds to dfa are done after we've created full D state
539
+ D = self.addDFAEdge(dfa, previousD, t, D)
540
+ return D
541
+
542
+ def predicateDFAState(self, dfaState:DFAState, decisionState:DecisionState):
543
+ # We need to test all predicates, even in DFA states that
544
+ # uniquely predict alternative.
545
+ nalts = len(decisionState.transitions)
546
+ # Update DFA so reach becomes accept state with (predicate,alt)
547
+ # pairs if preds found for conflicting alts
548
+ altsToCollectPredsFrom = self.getConflictingAltsOrUniqueAlt(dfaState.configs)
549
+ altToPred = self.getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState.configs, nalts)
550
+ if altToPred is not None:
551
+ dfaState.predicates = self.getPredicatePredictions(altsToCollectPredsFrom, altToPred)
552
+ dfaState.prediction = ATN.INVALID_ALT_NUMBER # make sure we use preds
553
+ else:
554
+ # There are preds in configs but they might go away
555
+ # when OR'd together like {p}? || NONE == NONE. If neither
556
+ # alt has preds, resolve to min alt
557
+ dfaState.prediction = min(altsToCollectPredsFrom)
558
+
559
+ # comes back with reach.uniqueAlt set to a valid alt
560
+ def execATNWithFullContext(self, dfa:DFA, D:DFAState, # how far we got before failing over
561
+ s0:ATNConfigSet,
562
+ input:TokenStream,
563
+ startIndex:int,
564
+ outerContext:ParserRuleContext):
565
+ if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
566
+ print("execATNWithFullContext", str(s0))
567
+ fullCtx = True
568
+ foundExactAmbig = False
569
+ reach = None
570
+ previous = s0
571
+ input.seek(startIndex)
572
+ t = input.LA(1)
573
+ predictedAlt = -1
574
+ while (True): # while more work
575
+ reach = self.computeReachSet(previous, t, fullCtx)
576
+ if reach is None:
577
+ # if any configs in previous dipped into outer context, that
578
+ # means that input up to t actually finished entry rule
579
+ # at least for LL decision. Full LL doesn't dip into outer
580
+ # so don't need special case.
581
+ # We will get an error no matter what so delay until after
582
+ # decision; better error message. Also, no reachable target
583
+ # ATN states in SLL implies LL will also get nowhere.
584
+ # If conflict in states that dip out, choose min since we
585
+ # will get error no matter what.
586
+ e = self.noViableAlt(input, outerContext, previous, startIndex)
587
+ input.seek(startIndex)
588
+ alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext)
589
+ if alt!=ATN.INVALID_ALT_NUMBER:
590
+ return alt
591
+ else:
592
+ raise e
593
+
594
+ altSubSets = PredictionMode.getConflictingAltSubsets(reach)
595
+ if ParserATNSimulator.debug:
596
+ print("LL altSubSets=" + str(altSubSets) + ", predict=" +
597
+ str(PredictionMode.getUniqueAlt(altSubSets)) + ", resolvesToJustOneViableAlt=" +
598
+ str(PredictionMode.resolvesToJustOneViableAlt(altSubSets)))
599
+
600
+ reach.uniqueAlt = self.getUniqueAlt(reach)
601
+ # unique prediction?
602
+ if reach.uniqueAlt!=ATN.INVALID_ALT_NUMBER:
603
+ predictedAlt = reach.uniqueAlt
604
+ break
605
+ elif self.predictionMode is not PredictionMode.LL_EXACT_AMBIG_DETECTION:
606
+ predictedAlt = PredictionMode.resolvesToJustOneViableAlt(altSubSets)
607
+ if predictedAlt != ATN.INVALID_ALT_NUMBER:
608
+ break
609
+ else:
610
+ # In exact ambiguity mode, we never try to terminate early.
611
+ # Just keeps scarfing until we know what the conflict is
612
+ if PredictionMode.allSubsetsConflict(altSubSets) and PredictionMode.allSubsetsEqual(altSubSets):
613
+ foundExactAmbig = True
614
+ predictedAlt = PredictionMode.getSingleViableAlt(altSubSets)
615
+ break
616
+ # else there are multiple non-conflicting subsets or
617
+ # we're not sure what the ambiguity is yet.
618
+ # So, keep going.
619
+
620
+ previous = reach
621
+ if t != Token.EOF:
622
+ input.consume()
623
+ t = input.LA(1)
624
+
625
+ # If the configuration set uniquely predicts an alternative,
626
+ # without conflict, then we know that it's a full LL decision
627
+ # not SLL.
628
+ if reach.uniqueAlt != ATN.INVALID_ALT_NUMBER :
629
+ self.reportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.index)
630
+ return predictedAlt
631
+
632
+ # We do not check predicates here because we have checked them
633
+ # on-the-fly when doing full context prediction.
634
+
635
+ #
636
+ # In non-exact ambiguity detection mode, we might actually be able to
637
+ # detect an exact ambiguity, but I'm not going to spend the cycles
638
+ # needed to check. We only emit ambiguity warnings in exact ambiguity
639
+ # mode.
640
+ #
641
+ # For example, we might know that we have conflicting configurations.
642
+ # But, that does not mean that there is no way forward without a
643
+ # conflict. It's possible to have nonconflicting alt subsets as in:
644
+
645
+ # altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]
646
+
647
+ # from
648
+ #
649
+ # [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
650
+ # (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]
651
+ #
652
+ # In this case, (17,1,[5 $]) indicates there is some next sequence that
653
+ # would resolve this without conflict to alternative 1. Any other viable
654
+ # next sequence, however, is associated with a conflict. We stop
655
+ # looking for input because no amount of further lookahead will alter
656
+ # the fact that we should predict alternative 1. We just can't say for
657
+ # sure that there is an ambiguity without looking further.
658
+
659
+ self.reportAmbiguity(dfa, D, startIndex, input.index, foundExactAmbig, None, reach)
660
+
661
+ return predictedAlt
662
+
663
+ def computeReachSet(self, closure:ATNConfigSet, t:int, fullCtx:bool):
664
+ if ParserATNSimulator.debug:
665
+ print("in computeReachSet, starting closure: " + str(closure))
666
+
667
+ if self.mergeCache is None:
668
+ self.mergeCache = dict()
669
+
670
+ intermediate = ATNConfigSet(fullCtx)
671
+
672
+ # Configurations already in a rule stop state indicate reaching the end
673
+ # of the decision rule (local context) or end of the start rule (full
674
+ # context). Once reached, these configurations are never updated by a
675
+ # closure operation, so they are handled separately for the performance
676
+ # advantage of having a smaller intermediate set when calling closure.
677
+ #
678
+ # For full-context reach operations, separate handling is required to
679
+ # ensure that the alternative matching the longest overall sequence is
680
+ # chosen when multiple such configurations can match the input.
681
+
682
+ skippedStopStates = None
683
+
684
+ # First figure out where we can reach on input t
685
+ for c in closure:
686
+ if ParserATNSimulator.debug:
687
+ print("testing " + self.getTokenName(t) + " at " + str(c))
688
+
689
+ if isinstance(c.state, RuleStopState):
690
+ if fullCtx or t == Token.EOF:
691
+ if skippedStopStates is None:
692
+ skippedStopStates = list()
693
+ skippedStopStates.append(c)
694
+ continue
695
+
696
+ for trans in c.state.transitions:
697
+ target = self.getReachableTarget(trans, t)
698
+ if target is not None:
699
+ intermediate.add(ATNConfig(state=target, config=c), self.mergeCache)
700
+
701
+ # Now figure out where the reach operation can take us...
702
+
703
+ reach = None
704
+
705
+ # This block optimizes the reach operation for intermediate sets which
706
+ # trivially indicate a termination state for the overall
707
+ # adaptivePredict operation.
708
+ #
709
+ # The conditions assume that intermediate
710
+ # contains all configurations relevant to the reach set, but this
711
+ # condition is not true when one or more configurations have been
712
+ # withheld in skippedStopStates, or when the current symbol is EOF.
713
+ #
714
+ if skippedStopStates is None and t!=Token.EOF:
715
+ if len(intermediate)==1:
716
+ # Don't pursue the closure if there is just one state.
717
+ # It can only have one alternative; just add to result
718
+ # Also don't pursue the closure if there is unique alternative
719
+ # among the configurations.
720
+ reach = intermediate
721
+ elif self.getUniqueAlt(intermediate)!=ATN.INVALID_ALT_NUMBER:
722
+ # Also don't pursue the closure if there is unique alternative
723
+ # among the configurations.
724
+ reach = intermediate
725
+
726
+ # If the reach set could not be trivially determined, perform a closure
727
+ # operation on the intermediate set to compute its initial value.
728
+ #
729
+ if reach is None:
730
+ reach = ATNConfigSet(fullCtx)
731
+ closureBusy = set()
732
+ treatEofAsEpsilon = t == Token.EOF
733
+ for c in intermediate:
734
+ self.closure(c, reach, closureBusy, False, fullCtx, treatEofAsEpsilon)
735
+
736
+ if t == Token.EOF:
737
+ # After consuming EOF no additional input is possible, so we are
738
+ # only interested in configurations which reached the end of the
739
+ # decision rule (local context) or end of the start rule (full
740
+ # context). Update reach to contain only these configurations. This
741
+ # handles both explicit EOF transitions in the grammar and implicit
742
+ # EOF transitions following the end of the decision or start rule.
743
+ #
744
+ # When reach==intermediate, no closure operation was performed. In
745
+ # this case, removeAllConfigsNotInRuleStopState needs to check for
746
+ # reachable rule stop states as well as configurations already in
747
+ # a rule stop state.
748
+ #
749
+ # This is handled before the configurations in skippedStopStates,
750
+ # because any configurations potentially added from that list are
751
+ # already guaranteed to meet this condition whether or not it's
752
+ # required.
753
+ #
754
+ reach = self.removeAllConfigsNotInRuleStopState(reach, reach is intermediate)
755
+
756
+ # If skippedStopStates is not null, then it contains at least one
757
+ # configuration. For full-context reach operations, these
758
+ # configurations reached the end of the start rule, in which case we
759
+ # only add them back to reach if no configuration during the current
760
+ # closure operation reached such a state. This ensures adaptivePredict
761
+ # chooses an alternative matching the longest overall sequence when
762
+ # multiple alternatives are viable.
763
+ #
764
+ if skippedStopStates is not None and ( (not fullCtx) or (not PredictionMode.hasConfigInRuleStopState(reach))):
765
+ for c in skippedStopStates:
766
+ reach.add(c, self.mergeCache)
767
+ if len(reach)==0:
768
+ return None
769
+ else:
770
+ return reach
771
+
772
+ #
773
+ # Return a configuration set containing only the configurations from
774
+ # {@code configs} which are in a {@link RuleStopState}. If all
775
+ # configurations in {@code configs} are already in a rule stop state, this
776
+ # method simply returns {@code configs}.
777
+ #
778
+ # <p>When {@code lookToEndOfRule} is true, this method uses
779
+ # {@link ATN#nextTokens} for each configuration in {@code configs} which is
780
+ # not already in a rule stop state to see if a rule stop state is reachable
781
+ # from the configuration via epsilon-only transitions.</p>
782
+ #
783
+ # @param configs the configuration set to update
784
+ # @param lookToEndOfRule when true, this method checks for rule stop states
785
+ # reachable by epsilon-only transitions from each configuration in
786
+ # {@code configs}.
787
+ #
788
+ # @return {@code configs} if all configurations in {@code configs} are in a
789
+ # rule stop state, otherwise return a new configuration set containing only
790
+ # the configurations from {@code configs} which are in a rule stop state
791
+ #
792
+ def removeAllConfigsNotInRuleStopState(self, configs:ATNConfigSet, lookToEndOfRule:bool):
793
+ if PredictionMode.allConfigsInRuleStopStates(configs):
794
+ return configs
795
+ result = ATNConfigSet(configs.fullCtx)
796
+ for config in configs:
797
+ if isinstance(config.state, RuleStopState):
798
+ result.add(config, self.mergeCache)
799
+ continue
800
+ if lookToEndOfRule and config.state.epsilonOnlyTransitions:
801
+ nextTokens = self.atn.nextTokens(config.state)
802
+ if Token.EPSILON in nextTokens:
803
+ endOfRuleState = self.atn.ruleToStopState[config.state.ruleIndex]
804
+ result.add(ATNConfig(state=endOfRuleState, config=config), self.mergeCache)
805
+ return result
806
+
807
+ def computeStartState(self, p:ATNState, ctx:RuleContext, fullCtx:bool):
808
+ # always at least the implicit call to start rule
809
+ initialContext = PredictionContextFromRuleContext(self.atn, ctx)
810
+ configs = ATNConfigSet(fullCtx)
811
+
812
+ for i in range(0, len(p.transitions)):
813
+ target = p.transitions[i].target
814
+ c = ATNConfig(target, i+1, initialContext)
815
+ closureBusy = set()
816
+ self.closure(c, configs, closureBusy, True, fullCtx, False)
817
+ return configs
818
+
819
+ #
820
+ # This method transforms the start state computed by
821
+ # {@link #computeStartState} to the special start state used by a
822
+ # precedence DFA for a particular precedence value. The transformation
823
+ # process applies the following changes to the start state's configuration
824
+ # set.
825
+ #
826
+ # <ol>
827
+ # <li>Evaluate the precedence predicates for each configuration using
828
+ # {@link SemanticContext#evalPrecedence}.</li>
829
+ # <li>Remove all configurations which predict an alternative greater than
830
+ # 1, for which another configuration that predicts alternative 1 is in the
831
+ # same ATN state with the same prediction context. This transformation is
832
+ # valid for the following reasons:
833
+ # <ul>
834
+ # <li>The closure block cannot contain any epsilon transitions which bypass
835
+ # the body of the closure, so all states reachable via alternative 1 are
836
+ # part of the precedence alternatives of the transformed left-recursive
837
+ # rule.</li>
838
+ # <li>The "primary" portion of a left recursive rule cannot contain an
839
+ # epsilon transition, so the only way an alternative other than 1 can exist
840
+ # in a state that is also reachable via alternative 1 is by nesting calls
841
+ # to the left-recursive rule, with the outer calls not being at the
842
+ # preferred precedence level.</li>
843
+ # </ul>
844
+ # </li>
845
+ # </ol>
846
+ #
847
+ # <p>
848
+ # The prediction context must be considered by this filter to address
849
+ # situations like the following.
850
+ # </p>
851
+ # <code>
852
+ # <pre>
853
+ # grammar TA;
854
+ # prog: statement* EOF;
855
+ # statement: letterA | statement letterA 'b' ;
856
+ # letterA: 'a';
857
+ # </pre>
858
+ # </code>
859
+ # <p>
860
+ # If the above grammar, the ATN state immediately before the token
861
+ # reference {@code 'a'} in {@code letterA} is reachable from the left edge
862
+ # of both the primary and closure blocks of the left-recursive rule
863
+ # {@code statement}. The prediction context associated with each of these
864
+ # configurations distinguishes between them, and prevents the alternative
865
+ # which stepped out to {@code prog} (and then back in to {@code statement}
866
+ # from being eliminated by the filter.
867
+ # </p>
868
+ #
869
+ # @param configs The configuration set computed by
870
+ # {@link #computeStartState} as the start state for the DFA.
871
+ # @return The transformed configuration set representing the start state
872
+ # for a precedence DFA at a particular precedence level (determined by
873
+ # calling {@link Parser#getPrecedence}).
874
+ #
875
+ def applyPrecedenceFilter(self, configs:ATNConfigSet):
876
+ statesFromAlt1 = dict()
877
+ configSet = ATNConfigSet(configs.fullCtx)
878
+ for config in configs:
879
+ # handle alt 1 first
880
+ if config.alt != 1:
881
+ continue
882
+ updatedContext = config.semanticContext.evalPrecedence(self.parser, self._outerContext)
883
+ if updatedContext is None:
884
+ # the configuration was eliminated
885
+ continue
886
+
887
+ statesFromAlt1[config.state.stateNumber] = config.context
888
+ if updatedContext is not config.semanticContext:
889
+ configSet.add(ATNConfig(config=config, semantic=updatedContext), self.mergeCache)
890
+ else:
891
+ configSet.add(config, self.mergeCache)
892
+
893
+ for config in configs:
894
+ if config.alt == 1:
895
+ # already handled
896
+ continue
897
+
898
+ # In the future, this elimination step could be updated to also
899
+ # filter the prediction context for alternatives predicting alt>1
900
+ # (basically a graph subtraction algorithm).
901
+ #
902
+ if not config.precedenceFilterSuppressed:
903
+ context = statesFromAlt1.get(config.state.stateNumber, None)
904
+ if context==config.context:
905
+ # eliminated
906
+ continue
907
+
908
+ configSet.add(config, self.mergeCache)
909
+
910
+ return configSet
911
+
912
+ def getReachableTarget(self, trans:Transition, ttype:int):
913
+ if trans.matches(ttype, 0, self.atn.maxTokenType):
914
+ return trans.target
915
+ else:
916
+ return None
917
+
918
+ def getPredsForAmbigAlts(self, ambigAlts:set, configs:ATNConfigSet, nalts:int):
919
+ # REACH=[1|1|[]|0:0, 1|2|[]|0:1]
920
+ # altToPred starts as an array of all null contexts. The entry at index i
921
+ # corresponds to alternative i. altToPred[i] may have one of three values:
922
+ # 1. null: no ATNConfig c is found such that c.alt==i
923
+ # 2. SemanticContext.NONE: At least one ATNConfig c exists such that
924
+ # c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
925
+ # alt i has at least one unpredicated config.
926
+ # 3. Non-NONE Semantic Context: There exists at least one, and for all
927
+ # ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
928
+ #
929
+ # From this, it is clear that NONE||anything==NONE.
930
+ #
931
+ altToPred = [None] * (nalts + 1)
932
+ for c in configs:
933
+ if c.alt in ambigAlts:
934
+ altToPred[c.alt] = orContext(altToPred[c.alt], c.semanticContext)
935
+
936
+ nPredAlts = 0
937
+ for i in range(1, nalts+1):
938
+ if altToPred[i] is None:
939
+ altToPred[i] = SemanticContext.NONE
940
+ elif altToPred[i] is not SemanticContext.NONE:
941
+ nPredAlts += 1
942
+
943
+ # nonambig alts are null in altToPred
944
+ if nPredAlts==0:
945
+ altToPred = None
946
+ if ParserATNSimulator.debug:
947
+ print("getPredsForAmbigAlts result " + str_list(altToPred))
948
+ return altToPred
949
+
950
+ def getPredicatePredictions(self, ambigAlts:set, altToPred:list):
951
+ pairs = []
952
+ containsPredicate = False
953
+ for i in range(1, len(altToPred)):
954
+ pred = altToPred[i]
955
+ # unpredicated is indicated by SemanticContext.NONE
956
+ if ambigAlts is not None and i in ambigAlts:
957
+ pairs.append(PredPrediction(pred, i))
958
+ if pred is not SemanticContext.NONE:
959
+ containsPredicate = True
960
+
961
+ if not containsPredicate:
962
+ return None
963
+
964
+ return pairs
965
+
966
+ #
967
+ # This method is used to improve the localization of error messages by
968
+ # choosing an alternative rather than throwing a
969
+ # {@link NoViableAltException} in particular prediction scenarios where the
970
+ # {@link #ERROR} state was reached during ATN simulation.
971
+ #
972
+ # <p>
973
+ # The default implementation of this method uses the following
974
+ # algorithm to identify an ATN configuration which successfully parsed the
975
+ # decision entry rule. Choosing such an alternative ensures that the
976
+ # {@link ParserRuleContext} returned by the calling rule will be complete
977
+ # and valid, and the syntax error will be reported later at a more
978
+ # localized location.</p>
979
+ #
980
+ # <ul>
981
+ # <li>If a syntactically valid path or paths reach the end of the decision rule and
982
+ # they are semantically valid if predicated, return the min associated alt.</li>
983
+ # <li>Else, if a semantically invalid but syntactically valid path exist
984
+ # or paths exist, return the minimum associated alt.
985
+ # </li>
986
+ # <li>Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.</li>
987
+ # </ul>
988
+ #
989
+ # <p>
990
+ # In some scenarios, the algorithm described above could predict an
991
+ # alternative which will result in a {@link FailedPredicateException} in
992
+ # the parser. Specifically, this could occur if the <em>only</em> configuration
993
+ # capable of successfully parsing to the end of the decision rule is
994
+ # blocked by a semantic predicate. By choosing this alternative within
995
+ # {@link #adaptivePredict} instead of throwing a
996
+ # {@link NoViableAltException}, the resulting
997
+ # {@link FailedPredicateException} in the parser will identify the specific
998
+ # predicate which is preventing the parser from successfully parsing the
999
+ # decision rule, which helps developers identify and correct logic errors
1000
+ # in semantic predicates.
1001
+ # </p>
1002
+ #
1003
+ # @param configs The ATN configurations which were valid immediately before
1004
+ # the {@link #ERROR} state was reached
1005
+ # @param outerContext The is the \gamma_0 initial parser context from the paper
1006
+ # or the parser stack at the instant before prediction commences.
1007
+ #
1008
+ # @return The value to return from {@link #adaptivePredict}, or
1009
+ # {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not
1010
+ # identified and {@link #adaptivePredict} should report an error instead.
1011
+ #
1012
+ def getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(self, configs:ATNConfigSet, outerContext:ParserRuleContext):
1013
+ semValidConfigs, semInvalidConfigs = self.splitAccordingToSemanticValidity(configs, outerContext)
1014
+ alt = self.getAltThatFinishedDecisionEntryRule(semValidConfigs)
1015
+ if alt!=ATN.INVALID_ALT_NUMBER: # semantically/syntactically viable path exists
1016
+ return alt
1017
+ # Is there a syntactically valid path with a failed pred?
1018
+ if len(semInvalidConfigs)>0:
1019
+ alt = self.getAltThatFinishedDecisionEntryRule(semInvalidConfigs)
1020
+ if alt!=ATN.INVALID_ALT_NUMBER: # syntactically viable path exists
1021
+ return alt
1022
+ return ATN.INVALID_ALT_NUMBER
1023
+
1024
+ def getAltThatFinishedDecisionEntryRule(self, configs:ATNConfigSet):
1025
+ alts = set()
1026
+ for c in configs:
1027
+ if c.reachesIntoOuterContext>0 or (isinstance(c.state, RuleStopState) and c.context.hasEmptyPath() ):
1028
+ alts.add(c.alt)
1029
+ if len(alts)==0:
1030
+ return ATN.INVALID_ALT_NUMBER
1031
+ else:
1032
+ return min(alts)
1033
+
1034
+ # Walk the list of configurations and split them according to
1035
+ # those that have preds evaluating to true/false. If no pred, assume
1036
+ # true pred and include in succeeded set. Returns Pair of sets.
1037
+ #
1038
+ # Create a new set so as not to alter the incoming parameter.
1039
+ #
1040
+ # Assumption: the input stream has been restored to the starting point
1041
+ # prediction, which is where predicates need to evaluate.
1042
+ #
1043
+ def splitAccordingToSemanticValidity(self, configs:ATNConfigSet, outerContext:ParserRuleContext):
1044
+ succeeded = ATNConfigSet(configs.fullCtx)
1045
+ failed = ATNConfigSet(configs.fullCtx)
1046
+ for c in configs:
1047
+ if c.semanticContext is not SemanticContext.NONE:
1048
+ predicateEvaluationResult = c.semanticContext.eval(self.parser, outerContext)
1049
+ if predicateEvaluationResult:
1050
+ succeeded.add(c)
1051
+ else:
1052
+ failed.add(c)
1053
+ else:
1054
+ succeeded.add(c)
1055
+ return (succeeded,failed)
1056
+
1057
+ # Look through a list of predicate/alt pairs, returning alts for the
1058
+ # pairs that win. A {@code NONE} predicate indicates an alt containing an
1059
+ # unpredicated config which behaves as "always true." If !complete
1060
+ # then we stop at the first predicate that evaluates to true. This
1061
+ # includes pairs with null predicates.
1062
+ #
1063
+ def evalSemanticContext(self, predPredictions:list, outerContext:ParserRuleContext, complete:bool):
1064
+ predictions = set()
1065
+ for pair in predPredictions:
1066
+ if pair.pred is SemanticContext.NONE:
1067
+ predictions.add(pair.alt)
1068
+ if not complete:
1069
+ break
1070
+ continue
1071
+ predicateEvaluationResult = pair.pred.eval(self.parser, outerContext)
1072
+ if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug:
1073
+ print("eval pred " + str(pair) + "=" + str(predicateEvaluationResult))
1074
+
1075
+ if predicateEvaluationResult:
1076
+ if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug:
1077
+ print("PREDICT " + str(pair.alt))
1078
+ predictions.add(pair.alt)
1079
+ if not complete:
1080
+ break
1081
+ return predictions
1082
+
1083
+
1084
+ # TODO: If we are doing predicates, there is no point in pursuing
1085
+ # closure operations if we reach a DFA state that uniquely predicts
1086
+ # alternative. We will not be caching that DFA state and it is a
1087
+ # waste to pursue the closure. Might have to advance when we do
1088
+ # ambig detection thought :(
1089
+ #
1090
+
1091
+ def closure(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, treatEofAsEpsilon:bool):
1092
+ initialDepth = 0
1093
+ self.closureCheckingStopState(config, configs, closureBusy, collectPredicates,
1094
+ fullCtx, initialDepth, treatEofAsEpsilon)
1095
+
1096
+
1097
+ def closureCheckingStopState(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, depth:int, treatEofAsEpsilon:bool):
1098
+ if ParserATNSimulator.debug:
1099
+ print("closure(" + str(config) + ")")
1100
+
1101
+ if isinstance(config.state, RuleStopState):
1102
+ # We hit rule end. If we have context info, use it
1103
+ # run thru all possible stack tops in ctx
1104
+ if not config.context.isEmpty():
1105
+ for i in range(0, len(config.context)):
1106
+ state = config.context.getReturnState(i)
1107
+ if state is PredictionContext.EMPTY_RETURN_STATE:
1108
+ if fullCtx:
1109
+ configs.add(ATNConfig(state=config.state, context=PredictionContext.EMPTY, config=config), self.mergeCache)
1110
+ continue
1111
+ else:
1112
+ # we have no context info, just chase follow links (if greedy)
1113
+ if ParserATNSimulator.debug:
1114
+ print("FALLING off rule " + self.getRuleName(config.state.ruleIndex))
1115
+ self.closure_(config, configs, closureBusy, collectPredicates,
1116
+ fullCtx, depth, treatEofAsEpsilon)
1117
+ continue
1118
+ returnState = self.atn.states[state]
1119
+ newContext = config.context.getParent(i) # "pop" return state
1120
+ c = ATNConfig(state=returnState, alt=config.alt, context=newContext, semantic=config.semanticContext)
1121
+ # While we have context to pop back from, we may have
1122
+ # gotten that context AFTER having falling off a rule.
1123
+ # Make sure we track that we are now out of context.
1124
+ c.reachesIntoOuterContext = config.reachesIntoOuterContext
1125
+ self.closureCheckingStopState(c, configs, closureBusy, collectPredicates, fullCtx, depth - 1, treatEofAsEpsilon)
1126
+ return
1127
+ elif fullCtx:
1128
+ # reached end of start rule
1129
+ configs.add(config, self.mergeCache)
1130
+ return
1131
+ else:
1132
+ # else if we have no context info, just chase follow links (if greedy)
1133
+ if ParserATNSimulator.debug:
1134
+ print("FALLING off rule " + self.getRuleName(config.state.ruleIndex))
1135
+
1136
+ self.closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEofAsEpsilon)
1137
+
1138
+ # Do the actual work of walking epsilon edges#
1139
+ def closure_(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, depth:int, treatEofAsEpsilon:bool):
1140
+ p = config.state
1141
+ # optimization
1142
+ if not p.epsilonOnlyTransitions:
1143
+ configs.add(config, self.mergeCache)
1144
+ # make sure to not return here, because EOF transitions can act as
1145
+ # both epsilon transitions and non-epsilon transitions.
1146
+
1147
+ first = True
1148
+ for t in p.transitions:
1149
+ if first:
1150
+ first = False
1151
+ if self.canDropLoopEntryEdgeInLeftRecursiveRule(config):
1152
+ continue
1153
+
1154
+ continueCollecting = collectPredicates and not isinstance(t, ActionTransition)
1155
+ c = self.getEpsilonTarget(config, t, continueCollecting, depth == 0, fullCtx, treatEofAsEpsilon)
1156
+ if c is not None:
1157
+ newDepth = depth
1158
+ if isinstance( config.state, RuleStopState):
1159
+ # target fell off end of rule; mark resulting c as having dipped into outer context
1160
+ # We can't get here if incoming config was rule stop and we had context
1161
+ # track how far we dip into outer context. Might
1162
+ # come in handy and we avoid evaluating context dependent
1163
+ # preds if this is > 0.
1164
+ if self._dfa is not None and self._dfa.precedenceDfa:
1165
+ if t.outermostPrecedenceReturn == self._dfa.atnStartState.ruleIndex:
1166
+ c.precedenceFilterSuppressed = True
1167
+ c.reachesIntoOuterContext += 1
1168
+ if c in closureBusy:
1169
+ # avoid infinite recursion for right-recursive rules
1170
+ continue
1171
+ closureBusy.add(c)
1172
+ configs.dipsIntoOuterContext = True # TODO: can remove? only care when we add to set per middle of this method
1173
+ newDepth -= 1
1174
+ if ParserATNSimulator.debug:
1175
+ print("dips into outer ctx: " + str(c))
1176
+ else:
1177
+ if not t.isEpsilon:
1178
+ if c in closureBusy:
1179
+ # avoid infinite recursion for EOF* and EOF+
1180
+ continue
1181
+ closureBusy.add(c)
1182
+ if isinstance(t, RuleTransition):
1183
+ # latch when newDepth goes negative - once we step out of the entry context we can't return
1184
+ if newDepth >= 0:
1185
+ newDepth += 1
1186
+
1187
+ self.closureCheckingStopState(c, configs, closureBusy, continueCollecting, fullCtx, newDepth, treatEofAsEpsilon)
1188
+
1189
+
1190
+
1191
+ # Implements first-edge (loop entry) elimination as an optimization
1192
+ # during closure operations. See antlr/antlr4#1398.
1193
+ #
1194
+ # The optimization is to avoid adding the loop entry config when
1195
+ # the exit path can only lead back to the same
1196
+ # StarLoopEntryState after popping context at the rule end state
1197
+ # (traversing only epsilon edges, so we're still in closure, in
1198
+ # this same rule).
1199
+ #
1200
+ # We need to detect any state that can reach loop entry on
1201
+ # epsilon w/o exiting rule. We don't have to look at FOLLOW
1202
+ # links, just ensure that all stack tops for config refer to key
1203
+ # states in LR rule.
1204
+ #
1205
+ # To verify we are in the right situation we must first check
1206
+ # closure is at a StarLoopEntryState generated during LR removal.
1207
+ # Then we check that each stack top of context is a return state
1208
+ # from one of these cases:
1209
+ #
1210
+ # 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state
1211
+ # 2. expr op expr. The return state is the block end of internal block of (...)*
1212
+ # 3. 'between' expr 'and' expr. The return state of 2nd expr reference.
1213
+ # That state points at block end of internal block of (...)*.
1214
+ # 4. expr '?' expr ':' expr. The return state points at block end,
1215
+ # which points at loop entry state.
1216
+ #
1217
+ # If any is true for each stack top, then closure does not add a
1218
+ # config to the current config set for edge[0], the loop entry branch.
1219
+ #
1220
+ # Conditions fail if any context for the current config is:
1221
+ #
1222
+ # a. empty (we'd fall out of expr to do a global FOLLOW which could
1223
+ # even be to some weird spot in expr) or,
1224
+ # b. lies outside of expr or,
1225
+ # c. lies within expr but at a state not the BlockEndState
1226
+ # generated during LR removal
1227
+ #
1228
+ # Do we need to evaluate predicates ever in closure for this case?
1229
+ #
1230
+ # No. Predicates, including precedence predicates, are only
1231
+ # evaluated when computing a DFA start state. I.e., only before
1232
+ # the lookahead (but not parser) consumes a token.
1233
+ #
1234
+ # There are no epsilon edges allowed in LR rule alt blocks or in
1235
+ # the "primary" part (ID here). If closure is in
1236
+ # StarLoopEntryState any lookahead operation will have consumed a
1237
+ # token as there are no epsilon-paths that lead to
1238
+ # StarLoopEntryState. We do not have to evaluate predicates
1239
+ # therefore if we are in the generated StarLoopEntryState of a LR
1240
+ # rule. Note that when making a prediction starting at that
1241
+ # decision point, decision d=2, compute-start-state performs
1242
+ # closure starting at edges[0], edges[1] emanating from
1243
+ # StarLoopEntryState. That means it is not performing closure on
1244
+ # StarLoopEntryState during compute-start-state.
1245
+ #
1246
+ # How do we know this always gives same prediction answer?
1247
+ #
1248
+ # Without predicates, loop entry and exit paths are ambiguous
1249
+ # upon remaining input +b (in, say, a+b). Either paths lead to
1250
+ # valid parses. Closure can lead to consuming + immediately or by
1251
+ # falling out of this call to expr back into expr and loop back
1252
+ # again to StarLoopEntryState to match +b. In this special case,
1253
+ # we choose the more efficient path, which is to take the bypass
1254
+ # path.
1255
+ #
1256
+ # The lookahead language has not changed because closure chooses
1257
+ # one path over the other. Both paths lead to consuming the same
1258
+ # remaining input during a lookahead operation. If the next token
1259
+ # is an operator, lookahead will enter the choice block with
1260
+ # operators. If it is not, lookahead will exit expr. Same as if
1261
+ # closure had chosen to enter the choice block immediately.
1262
+ #
1263
+ # Closure is examining one config (some loopentrystate, some alt,
1264
+ # context) which means it is considering exactly one alt. Closure
1265
+ # always copies the same alt to any derived configs.
1266
+ #
1267
+ # How do we know this optimization doesn't mess up precedence in
1268
+ # our parse trees?
1269
+ #
1270
+ # Looking through expr from left edge of stat only has to confirm
1271
+ # that an input, say, a+b+c; begins with any valid interpretation
1272
+ # of an expression. The precedence actually doesn't matter when
1273
+ # making a decision in stat seeing through expr. It is only when
1274
+ # parsing rule expr that we must use the precedence to get the
1275
+ # right interpretation and, hence, parse tree.
1276
+ #
1277
+ # @since 4.6
1278
+ #
1279
+ def canDropLoopEntryEdgeInLeftRecursiveRule(self, config):
1280
+ # return False
1281
+ p = config.state
1282
+ # First check to see if we are in StarLoopEntryState generated during
1283
+ # left-recursion elimination. For efficiency, also check if
1284
+ # the context has an empty stack case. If so, it would mean
1285
+ # global FOLLOW so we can't perform optimization
1286
+ # Are we the special loop entry/exit state? or SLL wildcard
1287
+ if p.stateType != ATNState.STAR_LOOP_ENTRY \
1288
+ or not p.isPrecedenceDecision \
1289
+ or config.context.isEmpty() \
1290
+ or config.context.hasEmptyPath():
1291
+ return False
1292
+
1293
+ # Require all return states to return back to the same rule
1294
+ # that p is in.
1295
+ numCtxs = len(config.context)
1296
+ for i in range(0, numCtxs): # for each stack context
1297
+ returnState = self.atn.states[config.context.getReturnState(i)]
1298
+ if returnState.ruleIndex != p.ruleIndex:
1299
+ return False
1300
+
1301
+ decisionStartState = p.transitions[0].target
1302
+ blockEndStateNum = decisionStartState.endState.stateNumber
1303
+ blockEndState = self.atn.states[blockEndStateNum]
1304
+
1305
+ # Verify that the top of each stack context leads to loop entry/exit
1306
+ # state through epsilon edges and w/o leaving rule.
1307
+ for i in range(0, numCtxs): # for each stack context
1308
+ returnStateNumber = config.context.getReturnState(i)
1309
+ returnState = self.atn.states[returnStateNumber]
1310
+ # all states must have single outgoing epsilon edge
1311
+ if len(returnState.transitions) != 1 or not returnState.transitions[0].isEpsilon:
1312
+ return False
1313
+
1314
+ # Look for prefix op case like 'not expr', (' type ')' expr
1315
+ returnStateTarget = returnState.transitions[0].target
1316
+ if returnState.stateType == ATNState.BLOCK_END and returnStateTarget is p:
1317
+ continue
1318
+
1319
+ # Look for 'expr op expr' or case where expr's return state is block end
1320
+ # of (...)* internal block; the block end points to loop back
1321
+ # which points to p but we don't need to check that
1322
+ if returnState is blockEndState:
1323
+ continue
1324
+
1325
+ # Look for ternary expr ? expr : expr. The return state points at block end,
1326
+ # which points at loop entry state
1327
+ if returnStateTarget is blockEndState:
1328
+ continue
1329
+
1330
+ # Look for complex prefix 'between expr and expr' case where 2nd expr's
1331
+ # return state points at block end state of (...)* internal block
1332
+ if returnStateTarget.stateType == ATNState.BLOCK_END \
1333
+ and len(returnStateTarget.transitions) == 1 \
1334
+ and returnStateTarget.transitions[0].isEpsilon \
1335
+ and returnStateTarget.transitions[0].target is p:
1336
+ continue
1337
+
1338
+ # anything else ain't conforming
1339
+ return False
1340
+
1341
+ return True
1342
+
1343
+
1344
+ def getRuleName(self, index:int):
1345
+ if self.parser is not None and index>=0:
1346
+ return self.parser.ruleNames[index]
1347
+ else:
1348
+ return "<rule " + str(index) + ">"
1349
+
1350
+ epsilonTargetMethods = dict()
1351
+ epsilonTargetMethods[Transition.RULE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1352
+ sim.ruleTransition(config, t)
1353
+ epsilonTargetMethods[Transition.PRECEDENCE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1354
+ sim.precedenceTransition(config, t, collectPredicates, inContext, fullCtx)
1355
+ epsilonTargetMethods[Transition.PREDICATE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1356
+ sim.predTransition(config, t, collectPredicates, inContext, fullCtx)
1357
+ epsilonTargetMethods[Transition.ACTION] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1358
+ sim.actionTransition(config, t)
1359
+ epsilonTargetMethods[Transition.EPSILON] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1360
+ ATNConfig(state=t.target, config=config)
1361
+ epsilonTargetMethods[Transition.ATOM] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1362
+ ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
1363
+ epsilonTargetMethods[Transition.RANGE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1364
+ ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
1365
+ epsilonTargetMethods[Transition.SET] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
1366
+ ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
1367
+
1368
+ def getEpsilonTarget(self, config:ATNConfig, t:Transition, collectPredicates:bool, inContext:bool, fullCtx:bool, treatEofAsEpsilon:bool):
1369
+ m = self.epsilonTargetMethods.get(t.serializationType, None)
1370
+ if m is None:
1371
+ return None
1372
+ else:
1373
+ return m(self, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon)
1374
+
1375
+ def actionTransition(self, config:ATNConfig, t:ActionTransition):
1376
+ if ParserATNSimulator.debug:
1377
+ print("ACTION edge " + str(t.ruleIndex) + ":" + str(t.actionIndex))
1378
+ return ATNConfig(state=t.target, config=config)
1379
+
1380
+ def precedenceTransition(self, config:ATNConfig, pt:PrecedencePredicateTransition, collectPredicates:bool, inContext:bool, fullCtx:bool):
1381
+ if ParserATNSimulator.debug:
1382
+ print("PRED (collectPredicates=" + str(collectPredicates) + ") " +
1383
+ str(pt.precedence) + ">=_p, ctx dependent=true")
1384
+ if self.parser is not None:
1385
+ print("context surrounding pred is " + str(self.parser.getRuleInvocationStack()))
1386
+
1387
+ c = None
1388
+ if collectPredicates and inContext:
1389
+ if fullCtx:
1390
+ # In full context mode, we can evaluate predicates on-the-fly
1391
+ # during closure, which dramatically reduces the size of
1392
+ # the config sets. It also obviates the need to test predicates
1393
+ # later during conflict resolution.
1394
+ currentPosition = self._input.index
1395
+ self._input.seek(self._startIndex)
1396
+ predSucceeds = pt.getPredicate().eval(self.parser, self._outerContext)
1397
+ self._input.seek(currentPosition)
1398
+ if predSucceeds:
1399
+ c = ATNConfig(state=pt.target, config=config) # no pred context
1400
+ else:
1401
+ newSemCtx = andContext(config.semanticContext, pt.getPredicate())
1402
+ c = ATNConfig(state=pt.target, semantic=newSemCtx, config=config)
1403
+ else:
1404
+ c = ATNConfig(state=pt.target, config=config)
1405
+
1406
+ if ParserATNSimulator.debug:
1407
+ print("config from pred transition=" + str(c))
1408
+ return c
1409
+
1410
+ def predTransition(self, config:ATNConfig, pt:PredicateTransition, collectPredicates:bool, inContext:bool, fullCtx:bool):
1411
+ if ParserATNSimulator.debug:
1412
+ print("PRED (collectPredicates=" + str(collectPredicates) + ") " + str(pt.ruleIndex) +
1413
+ ":" + str(pt.predIndex) + ", ctx dependent=" + str(pt.isCtxDependent))
1414
+ if self.parser is not None:
1415
+ print("context surrounding pred is " + str(self.parser.getRuleInvocationStack()))
1416
+
1417
+ c = None
1418
+ if collectPredicates and (not pt.isCtxDependent or (pt.isCtxDependent and inContext)):
1419
+ if fullCtx:
1420
+ # In full context mode, we can evaluate predicates on-the-fly
1421
+ # during closure, which dramatically reduces the size of
1422
+ # the config sets. It also obviates the need to test predicates
1423
+ # later during conflict resolution.
1424
+ currentPosition = self._input.index
1425
+ self._input.seek(self._startIndex)
1426
+ predSucceeds = pt.getPredicate().eval(self.parser, self._outerContext)
1427
+ self._input.seek(currentPosition)
1428
+ if predSucceeds:
1429
+ c = ATNConfig(state=pt.target, config=config) # no pred context
1430
+ else:
1431
+ newSemCtx = andContext(config.semanticContext, pt.getPredicate())
1432
+ c = ATNConfig(state=pt.target, semantic=newSemCtx, config=config)
1433
+ else:
1434
+ c = ATNConfig(state=pt.target, config=config)
1435
+
1436
+ if ParserATNSimulator.debug:
1437
+ print("config from pred transition=" + str(c))
1438
+ return c
1439
+
1440
+ def ruleTransition(self, config:ATNConfig, t:RuleTransition):
1441
+ if ParserATNSimulator.debug:
1442
+ print("CALL rule " + self.getRuleName(t.target.ruleIndex) + ", ctx=" + str(config.context))
1443
+ returnState = t.followState
1444
+ newContext = SingletonPredictionContext.create(config.context, returnState.stateNumber)
1445
+ return ATNConfig(state=t.target, context=newContext, config=config )
1446
+
1447
+ def getConflictingAlts(self, configs:ATNConfigSet):
1448
+ altsets = PredictionMode.getConflictingAltSubsets(configs)
1449
+ return PredictionMode.getAlts(altsets)
1450
+
1451
+ # Sam pointed out a problem with the previous definition, v3, of
1452
+ # ambiguous states. If we have another state associated with conflicting
1453
+ # alternatives, we should keep going. For example, the following grammar
1454
+ #
1455
+ # s : (ID | ID ID?) ';' ;
1456
+ #
1457
+ # When the ATN simulation reaches the state before ';', it has a DFA
1458
+ # state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
1459
+ # 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
1460
+ # because alternative to has another way to continue, via [6|2|[]].
1461
+ # The key is that we have a single state that has config's only associated
1462
+ # with a single alternative, 2, and crucially the state transitions
1463
+ # among the configurations are all non-epsilon transitions. That means
1464
+ # we don't consider any conflicts that include alternative 2. So, we
1465
+ # ignore the conflict between alts 1 and 2. We ignore a set of
1466
+ # conflicting alts when there is an intersection with an alternative
1467
+ # associated with a single alt state in the state&rarr;config-list map.
1468
+ #
1469
+ # It's also the case that we might have two conflicting configurations but
1470
+ # also a 3rd nonconflicting configuration for a different alternative:
1471
+ # [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:
1472
+ #
1473
+ # a : A | A | A B ;
1474
+ #
1475
+ # After matching input A, we reach the stop state for rule A, state 1.
1476
+ # State 8 is the state right before B. Clearly alternatives 1 and 2
1477
+ # conflict and no amount of further lookahead will separate the two.
1478
+ # However, alternative 3 will be able to continue and so we do not
1479
+ # stop working on this state. In the previous example, we're concerned
1480
+ # with states associated with the conflicting alternatives. Here alt
1481
+ # 3 is not associated with the conflicting configs, but since we can continue
1482
+ # looking for input reasonably, I don't declare the state done. We
1483
+ # ignore a set of conflicting alts when we have an alternative
1484
+ # that we still need to pursue.
1485
+ #
1486
+
1487
+ def getConflictingAltsOrUniqueAlt(self, configs:ATNConfigSet):
1488
+ conflictingAlts = None
1489
+ if configs.uniqueAlt!= ATN.INVALID_ALT_NUMBER:
1490
+ conflictingAlts = set()
1491
+ conflictingAlts.add(configs.uniqueAlt)
1492
+ else:
1493
+ conflictingAlts = configs.conflictingAlts
1494
+ return conflictingAlts
1495
+
1496
+ def getTokenName(self, t:int):
1497
+ if t==Token.EOF:
1498
+ return "EOF"
1499
+ if self.parser is not None and \
1500
+ self.parser.literalNames is not None and \
1501
+ t < len(self.parser.literalNames):
1502
+ return self.parser.literalNames[t] + "<" + str(t) + ">"
1503
+ else:
1504
+ return str(t)
1505
+
1506
+ def getLookaheadName(self, input:TokenStream):
1507
+ return self.getTokenName(input.LA(1))
1508
+
1509
+ # Used for debugging in adaptivePredict around execATN but I cut
1510
+ # it out for clarity now that alg. works well. We can leave this
1511
+ # "dead" code for a bit.
1512
+ #
1513
+ def dumpDeadEndConfigs(self, nvae:NoViableAltException):
1514
+ print("dead end configs: ")
1515
+ for c in nvae.getDeadEndConfigs():
1516
+ trans = "no edges"
1517
+ if len(c.state.transitions)>0:
1518
+ t = c.state.transitions[0]
1519
+ if isinstance(t, AtomTransition):
1520
+ trans = "Atom "+ self.getTokenName(t.label)
1521
+ elif isinstance(t, SetTransition):
1522
+ neg = isinstance(t, NotSetTransition)
1523
+ trans = ("~" if neg else "")+"Set "+ str(t.set)
1524
+ print(c.toString(self.parser, True) + ":" + trans, file=sys.stderr)
1525
+
1526
+ def noViableAlt(self, input:TokenStream, outerContext:ParserRuleContext, configs:ATNConfigSet, startIndex:int):
1527
+ return NoViableAltException(self.parser, input, input.get(startIndex), input.LT(1), configs, outerContext)
1528
+
1529
+ def getUniqueAlt(self, configs:ATNConfigSet):
1530
+ alt = ATN.INVALID_ALT_NUMBER
1531
+ for c in configs:
1532
+ if alt == ATN.INVALID_ALT_NUMBER:
1533
+ alt = c.alt # found first alt
1534
+ elif c.alt!=alt:
1535
+ return ATN.INVALID_ALT_NUMBER
1536
+ return alt
1537
+
1538
+ #
1539
+ # Add an edge to the DFA, if possible. This method calls
1540
+ # {@link #addDFAState} to ensure the {@code to} state is present in the
1541
+ # DFA. If {@code from} is {@code null}, or if {@code t} is outside the
1542
+ # range of edges that can be represented in the DFA tables, this method
1543
+ # returns without adding the edge to the DFA.
1544
+ #
1545
+ # <p>If {@code to} is {@code null}, this method returns {@code null}.
1546
+ # Otherwise, this method returns the {@link DFAState} returned by calling
1547
+ # {@link #addDFAState} for the {@code to} state.</p>
1548
+ #
1549
+ # @param dfa The DFA
1550
+ # @param from The source state for the edge
1551
+ # @param t The input symbol
1552
+ # @param to The target state for the edge
1553
+ #
1554
+ # @return If {@code to} is {@code null}, this method returns {@code null};
1555
+ # otherwise this method returns the result of calling {@link #addDFAState}
1556
+ # on {@code to}
1557
+ #
1558
+ def addDFAEdge(self, dfa:DFA, from_:DFAState, t:int, to:DFAState):
1559
+ if ParserATNSimulator.debug:
1560
+ print("EDGE " + str(from_) + " -> " + str(to) + " upon " + self.getTokenName(t))
1561
+
1562
+ if to is None:
1563
+ return None
1564
+
1565
+ to = self.addDFAState(dfa, to) # used existing if possible not incoming
1566
+ if from_ is None or t < -1 or t > self.atn.maxTokenType:
1567
+ return to
1568
+
1569
+ if from_.edges is None:
1570
+ from_.edges = [None] * (self.atn.maxTokenType + 2)
1571
+ from_.edges[t+1] = to # connect
1572
+
1573
+ if ParserATNSimulator.debug:
1574
+ names = None if self.parser is None else self.parser.literalNames
1575
+ print("DFA=\n" + dfa.toString(names))
1576
+
1577
+ return to
1578
+
1579
+ #
1580
+ # Add state {@code D} to the DFA if it is not already present, and return
1581
+ # the actual instance stored in the DFA. If a state equivalent to {@code D}
1582
+ # is already in the DFA, the existing state is returned. Otherwise this
1583
+ # method returns {@code D} after adding it to the DFA.
1584
+ #
1585
+ # <p>If {@code D} is {@link #ERROR}, this method returns {@link #ERROR} and
1586
+ # does not change the DFA.</p>
1587
+ #
1588
+ # @param dfa The dfa
1589
+ # @param D The DFA state to add
1590
+ # @return The state stored in the DFA. This will be either the existing
1591
+ # state if {@code D} is already in the DFA, or {@code D} itself if the
1592
+ # state was not already present.
1593
+ #
1594
+ def addDFAState(self, dfa:DFA, D:DFAState):
1595
+ if D is self.ERROR:
1596
+ return D
1597
+
1598
+
1599
+ existing = dfa.states.get(D, None)
1600
+ if existing is not None:
1601
+ return existing
1602
+
1603
+ D.stateNumber = len(dfa.states)
1604
+ if not D.configs.readonly:
1605
+ D.configs.optimizeConfigs(self)
1606
+ D.configs.setReadonly(True)
1607
+ dfa.states[D] = D
1608
+ if ParserATNSimulator.debug:
1609
+ print("adding new DFA state: " + str(D))
1610
+ return D
1611
+
1612
+ def reportAttemptingFullContext(self, dfa:DFA, conflictingAlts:set, configs:ATNConfigSet, startIndex:int, stopIndex:int):
1613
+ if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
1614
+ print("reportAttemptingFullContext decision=" + str(dfa.decision) + ":" + str(configs) +
1615
+ ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
1616
+ if self.parser is not None:
1617
+ self.parser.getErrorListenerDispatch().reportAttemptingFullContext(self.parser, dfa, startIndex, stopIndex, conflictingAlts, configs)
1618
+
1619
+ def reportContextSensitivity(self, dfa:DFA, prediction:int, configs:ATNConfigSet, startIndex:int, stopIndex:int):
1620
+ if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
1621
+ print("reportContextSensitivity decision=" + str(dfa.decision) + ":" + str(configs) +
1622
+ ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
1623
+ if self.parser is not None:
1624
+ self.parser.getErrorListenerDispatch().reportContextSensitivity(self.parser, dfa, startIndex, stopIndex, prediction, configs)
1625
+
1626
+ # If context sensitive parsing, we know it's ambiguity not conflict#
1627
+ def reportAmbiguity(self, dfa:DFA, D:DFAState, startIndex:int, stopIndex:int,
1628
+ exact:bool, ambigAlts:set, configs:ATNConfigSet ):
1629
+ if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
1630
+ # ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn);
1631
+ # int i = 1;
1632
+ # for (Transition t : dfa.atnStartState.transitions) {
1633
+ # print("ALT "+i+"=");
1634
+ # print(startIndex+".."+stopIndex+", len(input)="+parser.getInputStream().size());
1635
+ # TraceTree path = finder.trace(t.target, parser.getContext(), (TokenStream)parser.getInputStream(),
1636
+ # startIndex, stopIndex);
1637
+ # if ( path!=null ) {
1638
+ # print("path = "+path.toStringTree());
1639
+ # for (TraceTree leaf : path.leaves) {
1640
+ # List<ATNState> states = path.getPathToNode(leaf);
1641
+ # print("states="+states);
1642
+ # }
1643
+ # }
1644
+ # i++;
1645
+ # }
1646
+ print("reportAmbiguity " + str(ambigAlts) + ":" + str(configs) +
1647
+ ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
1648
+ if self.parser is not None:
1649
+ self.parser.getErrorListenerDispatch().reportAmbiguity(self.parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/PredictionMode.py ADDED
@@ -0,0 +1,499 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+ #
7
+ # This enumeration defines the prediction modes available in ANTLR 4 along with
8
+ # utility methods for analyzing configuration sets for conflicts and/or
9
+ # ambiguities.
10
+
11
+
12
+ from enum import Enum
13
+ from antlr4.atn.ATN import ATN
14
+ from antlr4.atn.ATNConfig import ATNConfig
15
+ from antlr4.atn.ATNConfigSet import ATNConfigSet
16
+ from antlr4.atn.ATNState import RuleStopState
17
+ from antlr4.atn.SemanticContext import SemanticContext
18
+
19
+ PredictionMode = None
20
+
21
+ class PredictionMode(Enum):
22
+ #
23
+ # The SLL(*) prediction mode. This prediction mode ignores the current
24
+ # parser context when making predictions. This is the fastest prediction
25
+ # mode, and provides correct results for many grammars. This prediction
26
+ # mode is more powerful than the prediction mode provided by ANTLR 3, but
27
+ # may result in syntax errors for grammar and input combinations which are
28
+ # not SLL.
29
+ #
30
+ # <p>
31
+ # When using this prediction mode, the parser will either return a correct
32
+ # parse tree (i.e. the same parse tree that would be returned with the
33
+ # {@link #LL} prediction mode), or it will report a syntax error. If a
34
+ # syntax error is encountered when using the {@link #SLL} prediction mode,
35
+ # it may be due to either an actual syntax error in the input or indicate
36
+ # that the particular combination of grammar and input requires the more
37
+ # powerful {@link #LL} prediction abilities to complete successfully.</p>
38
+ #
39
+ # <p>
40
+ # This prediction mode does not provide any guarantees for prediction
41
+ # behavior for syntactically-incorrect inputs.</p>
42
+ #
43
+ SLL = 0
44
+ #
45
+ # The LL(*) prediction mode. This prediction mode allows the current parser
46
+ # context to be used for resolving SLL conflicts that occur during
47
+ # prediction. This is the fastest prediction mode that guarantees correct
48
+ # parse results for all combinations of grammars with syntactically correct
49
+ # inputs.
50
+ #
51
+ # <p>
52
+ # When using this prediction mode, the parser will make correct decisions
53
+ # for all syntactically-correct grammar and input combinations. However, in
54
+ # cases where the grammar is truly ambiguous this prediction mode might not
55
+ # report a precise answer for <em>exactly which</em> alternatives are
56
+ # ambiguous.</p>
57
+ #
58
+ # <p>
59
+ # This prediction mode does not provide any guarantees for prediction
60
+ # behavior for syntactically-incorrect inputs.</p>
61
+ #
62
+ LL = 1
63
+ #
64
+ # The LL(*) prediction mode with exact ambiguity detection. In addition to
65
+ # the correctness guarantees provided by the {@link #LL} prediction mode,
66
+ # this prediction mode instructs the prediction algorithm to determine the
67
+ # complete and exact set of ambiguous alternatives for every ambiguous
68
+ # decision encountered while parsing.
69
+ #
70
+ # <p>
71
+ # This prediction mode may be used for diagnosing ambiguities during
72
+ # grammar development. Due to the performance overhead of calculating sets
73
+ # of ambiguous alternatives, this prediction mode should be avoided when
74
+ # the exact results are not necessary.</p>
75
+ #
76
+ # <p>
77
+ # This prediction mode does not provide any guarantees for prediction
78
+ # behavior for syntactically-incorrect inputs.</p>
79
+ #
80
+ LL_EXACT_AMBIG_DETECTION = 2
81
+
82
+
83
+ #
84
+ # Computes the SLL prediction termination condition.
85
+ #
86
+ # <p>
87
+ # This method computes the SLL prediction termination condition for both of
88
+ # the following cases.</p>
89
+ #
90
+ # <ul>
91
+ # <li>The usual SLL+LL fallback upon SLL conflict</li>
92
+ # <li>Pure SLL without LL fallback</li>
93
+ # </ul>
94
+ #
95
+ # <p><strong>COMBINED SLL+LL PARSING</strong></p>
96
+ #
97
+ # <p>When LL-fallback is enabled upon SLL conflict, correct predictions are
98
+ # ensured regardless of how the termination condition is computed by this
99
+ # method. Due to the substantially higher cost of LL prediction, the
100
+ # prediction should only fall back to LL when the additional lookahead
101
+ # cannot lead to a unique SLL prediction.</p>
102
+ #
103
+ # <p>Assuming combined SLL+LL parsing, an SLL configuration set with only
104
+ # conflicting subsets should fall back to full LL, even if the
105
+ # configuration sets don't resolve to the same alternative (e.g.
106
+ # {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting
107
+ # configuration, SLL could continue with the hopes that more lookahead will
108
+ # resolve via one of those non-conflicting configurations.</p>
109
+ #
110
+ # <p>Here's the prediction termination rule them: SLL (for SLL+LL parsing)
111
+ # stops when it sees only conflicting configuration subsets. In contrast,
112
+ # full LL keeps going when there is uncertainty.</p>
113
+ #
114
+ # <p><strong>HEURISTIC</strong></p>
115
+ #
116
+ # <p>As a heuristic, we stop prediction when we see any conflicting subset
117
+ # unless we see a state that only has one alternative associated with it.
118
+ # The single-alt-state thing lets prediction continue upon rules like
119
+ # (otherwise, it would admit defeat too soon):</p>
120
+ #
121
+ # <p>{@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}</p>
122
+ #
123
+ # <p>When the ATN simulation reaches the state before {@code ';'}, it has a
124
+ # DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally
125
+ # {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop
126
+ # processing this node because alternative to has another way to continue,
127
+ # via {@code [6|2|[]]}.</p>
128
+ #
129
+ # <p>It also let's us continue for this rule:</p>
130
+ #
131
+ # <p>{@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}</p>
132
+ #
133
+ # <p>After matching input A, we reach the stop state for rule A, state 1.
134
+ # State 8 is the state right before B. Clearly alternatives 1 and 2
135
+ # conflict and no amount of further lookahead will separate the two.
136
+ # However, alternative 3 will be able to continue and so we do not stop
137
+ # working on this state. In the previous example, we're concerned with
138
+ # states associated with the conflicting alternatives. Here alt 3 is not
139
+ # associated with the conflicting configs, but since we can continue
140
+ # looking for input reasonably, don't declare the state done.</p>
141
+ #
142
+ # <p><strong>PURE SLL PARSING</strong></p>
143
+ #
144
+ # <p>To handle pure SLL parsing, all we have to do is make sure that we
145
+ # combine stack contexts for configurations that differ only by semantic
146
+ # predicate. From there, we can do the usual SLL termination heuristic.</p>
147
+ #
148
+ # <p><strong>PREDICATES IN SLL+LL PARSING</strong></p>
149
+ #
150
+ # <p>SLL decisions don't evaluate predicates until after they reach DFA stop
151
+ # states because they need to create the DFA cache that works in all
152
+ # semantic situations. In contrast, full LL evaluates predicates collected
153
+ # during start state computation so it can ignore predicates thereafter.
154
+ # This means that SLL termination detection can totally ignore semantic
155
+ # predicates.</p>
156
+ #
157
+ # <p>Implementation-wise, {@link ATNConfigSet} combines stack contexts but not
158
+ # semantic predicate contexts so we might see two configurations like the
159
+ # following.</p>
160
+ #
161
+ # <p>{@code (s, 1, x, {}), (s, 1, x', {p})}</p>
162
+ #
163
+ # <p>Before testing these configurations against others, we have to merge
164
+ # {@code x} and {@code x'} (without modifying the existing configurations).
165
+ # For example, we test {@code (x+x')==x''} when looking for conflicts in
166
+ # the following configurations.</p>
167
+ #
168
+ # <p>{@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}</p>
169
+ #
170
+ # <p>If the configuration set has predicates (as indicated by
171
+ # {@link ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of
172
+ # the configurations to strip out all of the predicates so that a standard
173
+ # {@link ATNConfigSet} will merge everything ignoring predicates.</p>
174
+ #
175
+ @classmethod
176
+ def hasSLLConflictTerminatingPrediction(cls, mode:PredictionMode, configs:ATNConfigSet):
177
+ # Configs in rule stop states indicate reaching the end of the decision
178
+ # rule (local context) or end of start rule (full context). If all
179
+ # configs meet this condition, then none of the configurations is able
180
+ # to match additional input so we terminate prediction.
181
+ #
182
+ if cls.allConfigsInRuleStopStates(configs):
183
+ return True
184
+
185
+ # pure SLL mode parsing
186
+ if mode == PredictionMode.SLL:
187
+ # Don't bother with combining configs from different semantic
188
+ # contexts if we can fail over to full LL; costs more time
189
+ # since we'll often fail over anyway.
190
+ if configs.hasSemanticContext:
191
+ # dup configs, tossing out semantic predicates
192
+ dup = ATNConfigSet()
193
+ for c in configs:
194
+ c = ATNConfig(config=c, semantic=SemanticContext.NONE)
195
+ dup.add(c)
196
+ configs = dup
197
+ # now we have combined contexts for configs with dissimilar preds
198
+
199
+ # pure SLL or combined SLL+LL mode parsing
200
+ altsets = cls.getConflictingAltSubsets(configs)
201
+ return cls.hasConflictingAltSet(altsets) and not cls.hasStateAssociatedWithOneAlt(configs)
202
+
203
+ # Checks if any configuration in {@code configs} is in a
204
+ # {@link RuleStopState}. Configurations meeting this condition have reached
205
+ # the end of the decision rule (local context) or end of start rule (full
206
+ # context).
207
+ #
208
+ # @param configs the configuration set to test
209
+ # @return {@code true} if any configuration in {@code configs} is in a
210
+ # {@link RuleStopState}, otherwise {@code false}
211
+ @classmethod
212
+ def hasConfigInRuleStopState(cls, configs:ATNConfigSet):
213
+ return any(isinstance(cfg.state, RuleStopState) for cfg in configs)
214
+
215
+ # Checks if all configurations in {@code configs} are in a
216
+ # {@link RuleStopState}. Configurations meeting this condition have reached
217
+ # the end of the decision rule (local context) or end of start rule (full
218
+ # context).
219
+ #
220
+ # @param configs the configuration set to test
221
+ # @return {@code true} if all configurations in {@code configs} are in a
222
+ # {@link RuleStopState}, otherwise {@code false}
223
+ @classmethod
224
+ def allConfigsInRuleStopStates(cls, configs:ATNConfigSet):
225
+ return all(isinstance(cfg.state, RuleStopState) for cfg in configs)
226
+
227
+ #
228
+ # Full LL prediction termination.
229
+ #
230
+ # <p>Can we stop looking ahead during ATN simulation or is there some
231
+ # uncertainty as to which alternative we will ultimately pick, after
232
+ # consuming more input? Even if there are partial conflicts, we might know
233
+ # that everything is going to resolve to the same minimum alternative. That
234
+ # means we can stop since no more lookahead will change that fact. On the
235
+ # other hand, there might be multiple conflicts that resolve to different
236
+ # minimums. That means we need more look ahead to decide which of those
237
+ # alternatives we should predict.</p>
238
+ #
239
+ # <p>The basic idea is to split the set of configurations {@code C}, into
240
+ # conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with
241
+ # non-conflicting configurations. Two configurations conflict if they have
242
+ # identical {@link ATNConfig#state} and {@link ATNConfig#context} values
243
+ # but different {@link ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)}
244
+ # and {@code (s, j, ctx, _)} for {@code i!=j}.</p>
245
+ #
246
+ # <p>Reduce these configuration subsets to the set of possible alternatives.
247
+ # You can compute the alternative subsets in one pass as follows:</p>
248
+ #
249
+ # <p>{@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in
250
+ # {@code C} holding {@code s} and {@code ctx} fixed.</p>
251
+ #
252
+ # <p>Or in pseudo-code, for each configuration {@code c} in {@code C}:</p>
253
+ #
254
+ # <pre>
255
+ # map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
256
+ # alt and not pred
257
+ # </pre>
258
+ #
259
+ # <p>The values in {@code map} are the set of {@code A_s,ctx} sets.</p>
260
+ #
261
+ # <p>If {@code |A_s,ctx|=1} then there is no conflict associated with
262
+ # {@code s} and {@code ctx}.</p>
263
+ #
264
+ # <p>Reduce the subsets to singletons by choosing a minimum of each subset. If
265
+ # the union of these alternative subsets is a singleton, then no amount of
266
+ # more lookahead will help us. We will always pick that alternative. If,
267
+ # however, there is more than one alternative, then we are uncertain which
268
+ # alternative to predict and must continue looking for resolution. We may
269
+ # or may not discover an ambiguity in the future, even if there are no
270
+ # conflicting subsets this round.</p>
271
+ #
272
+ # <p>The biggest sin is to terminate early because it means we've made a
273
+ # decision but were uncertain as to the eventual outcome. We haven't used
274
+ # enough lookahead. On the other hand, announcing a conflict too late is no
275
+ # big deal; you will still have the conflict. It's just inefficient. It
276
+ # might even look until the end of file.</p>
277
+ #
278
+ # <p>No special consideration for semantic predicates is required because
279
+ # predicates are evaluated on-the-fly for full LL prediction, ensuring that
280
+ # no configuration contains a semantic context during the termination
281
+ # check.</p>
282
+ #
283
+ # <p><strong>CONFLICTING CONFIGS</strong></p>
284
+ #
285
+ # <p>Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict
286
+ # when {@code i!=j} but {@code x=x'}. Because we merge all
287
+ # {@code (s, i, _)} configurations together, that means that there are at
288
+ # most {@code n} configurations associated with state {@code s} for
289
+ # {@code n} possible alternatives in the decision. The merged stacks
290
+ # complicate the comparison of configuration contexts {@code x} and
291
+ # {@code x'}. Sam checks to see if one is a subset of the other by calling
292
+ # merge and checking to see if the merged result is either {@code x} or
293
+ # {@code x'}. If the {@code x} associated with lowest alternative {@code i}
294
+ # is the superset, then {@code i} is the only possible prediction since the
295
+ # others resolve to {@code min(i)} as well. However, if {@code x} is
296
+ # associated with {@code j>i} then at least one stack configuration for
297
+ # {@code j} is not in conflict with alternative {@code i}. The algorithm
298
+ # should keep going, looking for more lookahead due to the uncertainty.</p>
299
+ #
300
+ # <p>For simplicity, I'm doing a equality check between {@code x} and
301
+ # {@code x'} that lets the algorithm continue to consume lookahead longer
302
+ # than necessary. The reason I like the equality is of course the
303
+ # simplicity but also because that is the test you need to detect the
304
+ # alternatives that are actually in conflict.</p>
305
+ #
306
+ # <p><strong>CONTINUE/STOP RULE</strong></p>
307
+ #
308
+ # <p>Continue if union of resolved alternative sets from non-conflicting and
309
+ # conflicting alternative subsets has more than one alternative. We are
310
+ # uncertain about which alternative to predict.</p>
311
+ #
312
+ # <p>The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which
313
+ # alternatives are still in the running for the amount of input we've
314
+ # consumed at this point. The conflicting sets let us to strip away
315
+ # configurations that won't lead to more states because we resolve
316
+ # conflicts to the configuration with a minimum alternate for the
317
+ # conflicting set.</p>
318
+ #
319
+ # <p><strong>CASES</strong></p>
320
+ #
321
+ # <ul>
322
+ #
323
+ # <li>no conflicts and more than 1 alternative in set =&gt; continue</li>
324
+ #
325
+ # <li> {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)},
326
+ # {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set
327
+ # {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
328
+ # {@code {1,3}} =&gt; continue
329
+ # </li>
330
+ #
331
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
332
+ # {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set
333
+ # {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
334
+ # {@code {1}} =&gt; stop and predict 1</li>
335
+ #
336
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
337
+ # {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U
338
+ # {@code {1}} = {@code {1}} =&gt; stop and predict 1, can announce
339
+ # ambiguity {@code {1,2}}</li>
340
+ #
341
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)},
342
+ # {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U
343
+ # {@code {2}} = {@code {1,2}} =&gt; continue</li>
344
+ #
345
+ # <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)},
346
+ # {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U
347
+ # {@code {3}} = {@code {1,3}} =&gt; continue</li>
348
+ #
349
+ # </ul>
350
+ #
351
+ # <p><strong>EXACT AMBIGUITY DETECTION</strong></p>
352
+ #
353
+ # <p>If all states report the same conflicting set of alternatives, then we
354
+ # know we have the exact ambiguity set.</p>
355
+ #
356
+ # <p><code>|A_<em>i</em>|&gt;1</code> and
357
+ # <code>A_<em>i</em> = A_<em>j</em></code> for all <em>i</em>, <em>j</em>.</p>
358
+ #
359
+ # <p>In other words, we continue examining lookahead until all {@code A_i}
360
+ # have more than one alternative and all {@code A_i} are the same. If
361
+ # {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate
362
+ # because the resolved set is {@code {1}}. To determine what the real
363
+ # ambiguity is, we have to know whether the ambiguity is between one and
364
+ # two or one and three so we keep going. We can only stop prediction when
365
+ # we need exact ambiguity detection when the sets look like
366
+ # {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...</p>
367
+ #
368
+ @classmethod
369
+ def resolvesToJustOneViableAlt(cls, altsets:list):
370
+ return cls.getSingleViableAlt(altsets)
371
+
372
+ #
373
+ # Determines if every alternative subset in {@code altsets} contains more
374
+ # than one alternative.
375
+ #
376
+ # @param altsets a collection of alternative subsets
377
+ # @return {@code true} if every {@link BitSet} in {@code altsets} has
378
+ # {@link BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
379
+ #
380
+ @classmethod
381
+ def allSubsetsConflict(cls, altsets:list):
382
+ return not cls.hasNonConflictingAltSet(altsets)
383
+
384
+ #
385
+ # Determines if any single alternative subset in {@code altsets} contains
386
+ # exactly one alternative.
387
+ #
388
+ # @param altsets a collection of alternative subsets
389
+ # @return {@code true} if {@code altsets} contains a {@link BitSet} with
390
+ # {@link BitSet#cardinality cardinality} 1, otherwise {@code false}
391
+ #
392
+ @classmethod
393
+ def hasNonConflictingAltSet(cls, altsets:list):
394
+ return any(len(alts) == 1 for alts in altsets)
395
+
396
+ #
397
+ # Determines if any single alternative subset in {@code altsets} contains
398
+ # more than one alternative.
399
+ #
400
+ # @param altsets a collection of alternative subsets
401
+ # @return {@code true} if {@code altsets} contains a {@link BitSet} with
402
+ # {@link BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
403
+ #
404
+ @classmethod
405
+ def hasConflictingAltSet(cls, altsets:list):
406
+ return any(len(alts) > 1 for alts in altsets)
407
+
408
+ #
409
+ # Determines if every alternative subset in {@code altsets} is equivalent.
410
+ #
411
+ # @param altsets a collection of alternative subsets
412
+ # @return {@code true} if every member of {@code altsets} is equal to the
413
+ # others, otherwise {@code false}
414
+ #
415
+ @classmethod
416
+ def allSubsetsEqual(cls, altsets:list):
417
+ if not altsets:
418
+ return True
419
+ first = next(iter(altsets))
420
+ return all(alts == first for alts in iter(altsets))
421
+
422
+ #
423
+ # Returns the unique alternative predicted by all alternative subsets in
424
+ # {@code altsets}. If no such alternative exists, this method returns
425
+ # {@link ATN#INVALID_ALT_NUMBER}.
426
+ #
427
+ # @param altsets a collection of alternative subsets
428
+ #
429
+ @classmethod
430
+ def getUniqueAlt(cls, altsets:list):
431
+ all = cls.getAlts(altsets)
432
+ if len(all)==1:
433
+ return next(iter(all))
434
+ return ATN.INVALID_ALT_NUMBER
435
+
436
+ # Gets the complete set of represented alternatives for a collection of
437
+ # alternative subsets. This method returns the union of each {@link BitSet}
438
+ # in {@code altsets}.
439
+ #
440
+ # @param altsets a collection of alternative subsets
441
+ # @return the set of represented alternatives in {@code altsets}
442
+ #
443
+ @classmethod
444
+ def getAlts(cls, altsets:list):
445
+ return set.union(*altsets)
446
+
447
+ #
448
+ # This function gets the conflicting alt subsets from a configuration set.
449
+ # For each configuration {@code c} in {@code configs}:
450
+ #
451
+ # <pre>
452
+ # map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
453
+ # alt and not pred
454
+ # </pre>
455
+ #
456
+ @classmethod
457
+ def getConflictingAltSubsets(cls, configs:ATNConfigSet):
458
+ configToAlts = dict()
459
+ for c in configs:
460
+ h = hash((c.state.stateNumber, c.context))
461
+ alts = configToAlts.get(h, None)
462
+ if alts is None:
463
+ alts = set()
464
+ configToAlts[h] = alts
465
+ alts.add(c.alt)
466
+ return configToAlts.values()
467
+
468
+ #
469
+ # Get a map from state to alt subset from a configuration set. For each
470
+ # configuration {@code c} in {@code configs}:
471
+ #
472
+ # <pre>
473
+ # map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#alt alt}
474
+ # </pre>
475
+ #
476
+ @classmethod
477
+ def getStateToAltMap(cls, configs:ATNConfigSet):
478
+ m = dict()
479
+ for c in configs:
480
+ alts = m.get(c.state, None)
481
+ if alts is None:
482
+ alts = set()
483
+ m[c.state] = alts
484
+ alts.add(c.alt)
485
+ return m
486
+
487
+ @classmethod
488
+ def hasStateAssociatedWithOneAlt(cls, configs:ATNConfigSet):
489
+ return any(len(alts) == 1 for alts in cls.getStateToAltMap(configs).values())
490
+
491
+ @classmethod
492
+ def getSingleViableAlt(cls, altsets:list):
493
+ viableAlts = set()
494
+ for alts in altsets:
495
+ minAlt = min(alts)
496
+ viableAlts.add(minAlt)
497
+ if len(viableAlts)>1 : # more than 1 viable alt
498
+ return ATN.INVALID_ALT_NUMBER
499
+ return min(viableAlts)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/SemanticContext.py ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ # A tree structure used to record the semantic context in which
8
+ # an ATN configuration is valid. It's either a single predicate,
9
+ # a conjunction {@code p1&&p2}, or a sum of products {@code p1||p2}.
10
+ #
11
+ # <p>I have scoped the {@link AND}, {@link OR}, and {@link Predicate} subclasses of
12
+ # {@link SemanticContext} within the scope of this outer class.</p>
13
+ #
14
+ from antlr4.Recognizer import Recognizer
15
+ from antlr4.RuleContext import RuleContext
16
+ from io import StringIO
17
+
18
+
19
+ class SemanticContext(object):
20
+ #
21
+ # The default {@link SemanticContext}, which is semantically equivalent to
22
+ # a predicate of the form {@code {true}?}.
23
+ #
24
+ NONE = None
25
+
26
+ #
27
+ # For context independent predicates, we evaluate them without a local
28
+ # context (i.e., null context). That way, we can evaluate them without
29
+ # having to create proper rule-specific context during prediction (as
30
+ # opposed to the parser, which creates them naturally). In a practical
31
+ # sense, this avoids a cast exception from RuleContext to myruleContext.
32
+ #
33
+ # <p>For context dependent predicates, we must pass in a local context so that
34
+ # references such as $arg evaluate properly as _localctx.arg. We only
35
+ # capture context dependent predicates in the context in which we begin
36
+ # prediction, so we passed in the outer context here in case of context
37
+ # dependent predicate evaluation.</p>
38
+ #
39
+ def eval(self, parser:Recognizer , outerContext:RuleContext ):
40
+ pass
41
+
42
+ #
43
+ # Evaluate the precedence predicates for the context and reduce the result.
44
+ #
45
+ # @param parser The parser instance.
46
+ # @param outerContext The current parser context object.
47
+ # @return The simplified semantic context after precedence predicates are
48
+ # evaluated, which will be one of the following values.
49
+ # <ul>
50
+ # <li>{@link #NONE}: if the predicate simplifies to {@code true} after
51
+ # precedence predicates are evaluated.</li>
52
+ # <li>{@code null}: if the predicate simplifies to {@code false} after
53
+ # precedence predicates are evaluated.</li>
54
+ # <li>{@code this}: if the semantic context is not changed as a result of
55
+ # precedence predicate evaluation.</li>
56
+ # <li>A non-{@code null} {@link SemanticContext}: the new simplified
57
+ # semantic context after precedence predicates are evaluated.</li>
58
+ # </ul>
59
+ #
60
+ def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
61
+ return self
62
+
63
+ # need forward declaration
64
+ AND = None
65
+
66
+ def andContext(a:SemanticContext, b:SemanticContext):
67
+ if a is None or a is SemanticContext.NONE:
68
+ return b
69
+ if b is None or b is SemanticContext.NONE:
70
+ return a
71
+ result = AND(a, b)
72
+ if len(result.opnds) == 1:
73
+ return result.opnds[0]
74
+ else:
75
+ return result
76
+
77
+ # need forward declaration
78
+ OR = None
79
+
80
+ def orContext(a:SemanticContext, b:SemanticContext):
81
+ if a is None:
82
+ return b
83
+ if b is None:
84
+ return a
85
+ if a is SemanticContext.NONE or b is SemanticContext.NONE:
86
+ return SemanticContext.NONE
87
+ result = OR(a, b)
88
+ if len(result.opnds) == 1:
89
+ return result.opnds[0]
90
+ else:
91
+ return result
92
+
93
+ def filterPrecedencePredicates(collection:set):
94
+ return [context for context in collection if isinstance(context, PrecedencePredicate)]
95
+
96
+
97
+ class Predicate(SemanticContext):
98
+ __slots__ = ('ruleIndex', 'predIndex', 'isCtxDependent')
99
+
100
+ def __init__(self, ruleIndex:int=-1, predIndex:int=-1, isCtxDependent:bool=False):
101
+ self.ruleIndex = ruleIndex
102
+ self.predIndex = predIndex
103
+ self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
104
+
105
+ def eval(self, parser:Recognizer , outerContext:RuleContext ):
106
+ localctx = outerContext if self.isCtxDependent else None
107
+ return parser.sempred(localctx, self.ruleIndex, self.predIndex)
108
+
109
+ def __hash__(self):
110
+ return hash((self.ruleIndex, self.predIndex, self.isCtxDependent))
111
+
112
+ def __eq__(self, other):
113
+ if self is other:
114
+ return True
115
+ elif not isinstance(other, Predicate):
116
+ return False
117
+ return self.ruleIndex == other.ruleIndex and \
118
+ self.predIndex == other.predIndex and \
119
+ self.isCtxDependent == other.isCtxDependent
120
+
121
+ def __str__(self):
122
+ return "{" + str(self.ruleIndex) + ":" + str(self.predIndex) + "}?"
123
+
124
+
125
+ class PrecedencePredicate(SemanticContext):
126
+
127
+ def __init__(self, precedence:int=0):
128
+ self.precedence = precedence
129
+
130
+ def eval(self, parser:Recognizer , outerContext:RuleContext ):
131
+ return parser.precpred(outerContext, self.precedence)
132
+
133
+ def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
134
+ if parser.precpred(outerContext, self.precedence):
135
+ return SemanticContext.NONE
136
+ else:
137
+ return None
138
+
139
+ def __lt__(self, other):
140
+ return self.precedence < other.precedence
141
+
142
+ def __hash__(self):
143
+ return 31
144
+
145
+ def __eq__(self, other):
146
+ if self is other:
147
+ return True
148
+ elif not isinstance(other, PrecedencePredicate):
149
+ return False
150
+ else:
151
+ return self.precedence == other.precedence
152
+
153
+ # A semantic context which is true whenever none of the contained contexts
154
+ # is false.
155
+ del AND
156
+ class AND(SemanticContext):
157
+ __slots__ = 'opnds'
158
+
159
+ def __init__(self, a:SemanticContext, b:SemanticContext):
160
+ operands = set()
161
+ if isinstance( a, AND ):
162
+ operands.update(a.opnds)
163
+ else:
164
+ operands.add(a)
165
+ if isinstance( b, AND ):
166
+ operands.update(b.opnds)
167
+ else:
168
+ operands.add(b)
169
+
170
+ precedencePredicates = filterPrecedencePredicates(operands)
171
+ if len(precedencePredicates)>0:
172
+ # interested in the transition with the lowest precedence
173
+ reduced = min(precedencePredicates)
174
+ operands.add(reduced)
175
+
176
+ self.opnds = list(operands)
177
+
178
+ def __eq__(self, other):
179
+ if self is other:
180
+ return True
181
+ elif not isinstance(other, AND):
182
+ return False
183
+ else:
184
+ return self.opnds == other.opnds
185
+
186
+ def __hash__(self):
187
+ h = 0
188
+ for o in self.opnds:
189
+ h = hash((h, o))
190
+ return hash((h, "AND"))
191
+
192
+ #
193
+ # {@inheritDoc}
194
+ #
195
+ # <p>
196
+ # The evaluation of predicates by this context is short-circuiting, but
197
+ # unordered.</p>
198
+ #
199
+ def eval(self, parser:Recognizer, outerContext:RuleContext):
200
+ return all(opnd.eval(parser, outerContext) for opnd in self.opnds)
201
+
202
+ def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
203
+ differs = False
204
+ operands = []
205
+ for context in self.opnds:
206
+ evaluated = context.evalPrecedence(parser, outerContext)
207
+ differs |= evaluated is not context
208
+ if evaluated is None:
209
+ # The AND context is false if any element is false
210
+ return None
211
+ elif evaluated is not SemanticContext.NONE:
212
+ # Reduce the result by skipping true elements
213
+ operands.append(evaluated)
214
+
215
+ if not differs:
216
+ return self
217
+
218
+ if len(operands)==0:
219
+ # all elements were true, so the AND context is true
220
+ return SemanticContext.NONE
221
+
222
+ result = None
223
+ for o in operands:
224
+ result = o if result is None else andContext(result, o)
225
+
226
+ return result
227
+
228
+ def __str__(self):
229
+ with StringIO() as buf:
230
+ first = True
231
+ for o in self.opnds:
232
+ if not first:
233
+ buf.write("&&")
234
+ buf.write(str(o))
235
+ first = False
236
+ return buf.getvalue()
237
+
238
+ #
239
+ # A semantic context which is true whenever at least one of the contained
240
+ # contexts is true.
241
+ del OR
242
+ class OR (SemanticContext):
243
+ __slots__ = 'opnds'
244
+
245
+ def __init__(self, a:SemanticContext, b:SemanticContext):
246
+ operands = set()
247
+ if isinstance( a, OR ):
248
+ operands.update(a.opnds)
249
+ else:
250
+ operands.add(a)
251
+ if isinstance( b, OR ):
252
+ operands.update(b.opnds)
253
+ else:
254
+ operands.add(b)
255
+
256
+ precedencePredicates = filterPrecedencePredicates(operands)
257
+ if len(precedencePredicates)>0:
258
+ # interested in the transition with the highest precedence
259
+ s = sorted(precedencePredicates)
260
+ reduced = s[-1]
261
+ operands.add(reduced)
262
+
263
+ self.opnds = list(operands)
264
+
265
+ def __eq__(self, other):
266
+ if self is other:
267
+ return True
268
+ elif not isinstance(other, OR):
269
+ return False
270
+ else:
271
+ return self.opnds == other.opnds
272
+
273
+ def __hash__(self):
274
+ h = 0
275
+ for o in self.opnds:
276
+ h = hash((h, o))
277
+ return hash((h, "OR"))
278
+
279
+ # <p>
280
+ # The evaluation of predicates by this context is short-circuiting, but
281
+ # unordered.</p>
282
+ #
283
+ def eval(self, parser:Recognizer, outerContext:RuleContext):
284
+ return any(opnd.eval(parser, outerContext) for opnd in self.opnds)
285
+
286
+ def evalPrecedence(self, parser:Recognizer, outerContext:RuleContext):
287
+ differs = False
288
+ operands = []
289
+ for context in self.opnds:
290
+ evaluated = context.evalPrecedence(parser, outerContext)
291
+ differs |= evaluated is not context
292
+ if evaluated is SemanticContext.NONE:
293
+ # The OR context is true if any element is true
294
+ return SemanticContext.NONE
295
+ elif evaluated is not None:
296
+ # Reduce the result by skipping false elements
297
+ operands.append(evaluated)
298
+
299
+ if not differs:
300
+ return self
301
+
302
+ if len(operands)==0:
303
+ # all elements were false, so the OR context is false
304
+ return None
305
+
306
+ result = None
307
+ for o in operands:
308
+ result = o if result is None else orContext(result, o)
309
+
310
+ return result
311
+
312
+ def __str__(self):
313
+ with StringIO() as buf:
314
+ first = True
315
+ for o in self.opnds:
316
+ if not first:
317
+ buf.write("||")
318
+ buf.write(str(o))
319
+ first = False
320
+ return buf.getvalue()
321
+
322
+
323
+ SemanticContext.NONE = Predicate()
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/Transition.py ADDED
@@ -0,0 +1,268 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ # An ATN transition between any two ATN states. Subclasses define
8
+ # atom, set, epsilon, action, predicate, rule transitions.
9
+ #
10
+ # <p>This is a one way link. It emanates from a state (usually via a list of
11
+ # transitions) and has a target state.</p>
12
+ #
13
+ # <p>Since we never have to change the ATN transitions once we construct it,
14
+ # we can fix these transitions as specific classes. The DFA transitions
15
+ # on the other hand need to update the labels as it adds transitions to
16
+ # the states. We'll use the term Edge for the DFA to distinguish them from
17
+ # ATN transitions.</p>
18
+ #
19
+ from antlr4.IntervalSet import IntervalSet
20
+ from antlr4.Token import Token
21
+
22
+ # need forward declarations
23
+ from antlr4.atn.SemanticContext import Predicate, PrecedencePredicate
24
+
25
+ ATNState = None
26
+ RuleStartState = None
27
+
28
+ class Transition (object):
29
+ __slots__ = ('target','isEpsilon','label')
30
+
31
+ # constants for serialization
32
+ EPSILON = 1
33
+ RANGE = 2
34
+ RULE = 3
35
+ PREDICATE = 4 # e.g., {isType(input.LT(1))}?
36
+ ATOM = 5
37
+ ACTION = 6
38
+ SET = 7 # ~(A|B) or ~atom, wildcard, which convert to next 2
39
+ NOT_SET = 8
40
+ WILDCARD = 9
41
+ PRECEDENCE = 10
42
+
43
+ serializationNames = [
44
+ "INVALID",
45
+ "EPSILON",
46
+ "RANGE",
47
+ "RULE",
48
+ "PREDICATE",
49
+ "ATOM",
50
+ "ACTION",
51
+ "SET",
52
+ "NOT_SET",
53
+ "WILDCARD",
54
+ "PRECEDENCE"
55
+ ]
56
+
57
+ serializationTypes = dict()
58
+
59
+ def __init__(self, target:ATNState):
60
+ # The target of this transition.
61
+ if target is None:
62
+ raise Exception("target cannot be null.")
63
+ self.target = target
64
+ # Are we epsilon, action, sempred?
65
+ self.isEpsilon = False
66
+ self.label = None
67
+
68
+
69
+ # TODO: make all transitions sets? no, should remove set edges
70
+ class AtomTransition(Transition):
71
+ __slots__ = ('label_', 'serializationType')
72
+
73
+ def __init__(self, target:ATNState, label:int):
74
+ super().__init__(target)
75
+ self.label_ = label # The token type or character value; or, signifies special label.
76
+ self.label = self.makeLabel()
77
+ self.serializationType = self.ATOM
78
+
79
+ def makeLabel(self):
80
+ s = IntervalSet()
81
+ s.addOne(self.label_)
82
+ return s
83
+
84
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
85
+ return self.label_ == symbol
86
+
87
+ def __str__(self):
88
+ return str(self.label_)
89
+
90
+ class RuleTransition(Transition):
91
+ __slots__ = ('ruleIndex', 'precedence', 'followState', 'serializationType')
92
+
93
+ def __init__(self, ruleStart:RuleStartState, ruleIndex:int, precedence:int, followState:ATNState):
94
+ super().__init__(ruleStart)
95
+ self.ruleIndex = ruleIndex # ptr to the rule definition object for this rule ref
96
+ self.precedence = precedence
97
+ self.followState = followState # what node to begin computations following ref to rule
98
+ self.serializationType = self.RULE
99
+ self.isEpsilon = True
100
+
101
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
102
+ return False
103
+
104
+
105
+ class EpsilonTransition(Transition):
106
+ __slots__ = ('serializationType', 'outermostPrecedenceReturn')
107
+
108
+ def __init__(self, target, outermostPrecedenceReturn=-1):
109
+ super(EpsilonTransition, self).__init__(target)
110
+ self.serializationType = self.EPSILON
111
+ self.isEpsilon = True
112
+ self.outermostPrecedenceReturn = outermostPrecedenceReturn
113
+
114
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
115
+ return False
116
+
117
+ def __str__(self):
118
+ return "epsilon"
119
+
120
+ class RangeTransition(Transition):
121
+ __slots__ = ('serializationType', 'start', 'stop')
122
+
123
+ def __init__(self, target:ATNState, start:int, stop:int):
124
+ super().__init__(target)
125
+ self.serializationType = self.RANGE
126
+ self.start = start
127
+ self.stop = stop
128
+ self.label = self.makeLabel()
129
+
130
+ def makeLabel(self):
131
+ s = IntervalSet()
132
+ s.addRange(range(self.start, self.stop + 1))
133
+ return s
134
+
135
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
136
+ return symbol >= self.start and symbol <= self.stop
137
+
138
+ def __str__(self):
139
+ return "'" + chr(self.start) + "'..'" + chr(self.stop) + "'"
140
+
141
+ class AbstractPredicateTransition(Transition):
142
+
143
+ def __init__(self, target:ATNState):
144
+ super().__init__(target)
145
+
146
+
147
+ class PredicateTransition(AbstractPredicateTransition):
148
+ __slots__ = ('serializationType', 'ruleIndex', 'predIndex', 'isCtxDependent')
149
+
150
+ def __init__(self, target:ATNState, ruleIndex:int, predIndex:int, isCtxDependent:bool):
151
+ super().__init__(target)
152
+ self.serializationType = self.PREDICATE
153
+ self.ruleIndex = ruleIndex
154
+ self.predIndex = predIndex
155
+ self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
156
+ self.isEpsilon = True
157
+
158
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
159
+ return False
160
+
161
+ def getPredicate(self):
162
+ return Predicate(self.ruleIndex, self.predIndex, self.isCtxDependent)
163
+
164
+ def __str__(self):
165
+ return "pred_" + str(self.ruleIndex) + ":" + str(self.predIndex)
166
+
167
+ class ActionTransition(Transition):
168
+ __slots__ = ('serializationType', 'ruleIndex', 'actionIndex', 'isCtxDependent')
169
+
170
+ def __init__(self, target:ATNState, ruleIndex:int, actionIndex:int=-1, isCtxDependent:bool=False):
171
+ super().__init__(target)
172
+ self.serializationType = self.ACTION
173
+ self.ruleIndex = ruleIndex
174
+ self.actionIndex = actionIndex
175
+ self.isCtxDependent = isCtxDependent # e.g., $i ref in pred
176
+ self.isEpsilon = True
177
+
178
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
179
+ return False
180
+
181
+ def __str__(self):
182
+ return "action_"+self.ruleIndex+":"+self.actionIndex
183
+
184
+ # A transition containing a set of values.
185
+ class SetTransition(Transition):
186
+ __slots__ = 'serializationType'
187
+
188
+ def __init__(self, target:ATNState, set:IntervalSet):
189
+ super().__init__(target)
190
+ self.serializationType = self.SET
191
+ if set is not None:
192
+ self.label = set
193
+ else:
194
+ self.label = IntervalSet()
195
+ self.label.addRange(range(Token.INVALID_TYPE, Token.INVALID_TYPE + 1))
196
+
197
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
198
+ return symbol in self.label
199
+
200
+ def __str__(self):
201
+ return str(self.label)
202
+
203
+ class NotSetTransition(SetTransition):
204
+
205
+ def __init__(self, target:ATNState, set:IntervalSet):
206
+ super().__init__(target, set)
207
+ self.serializationType = self.NOT_SET
208
+
209
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
210
+ return symbol >= minVocabSymbol \
211
+ and symbol <= maxVocabSymbol \
212
+ and not super(type(self), self).matches(symbol, minVocabSymbol, maxVocabSymbol)
213
+
214
+ def __str__(self):
215
+ return '~' + super(type(self), self).__str__()
216
+
217
+
218
+ class WildcardTransition(Transition):
219
+ __slots__ = 'serializationType'
220
+
221
+ def __init__(self, target:ATNState):
222
+ super().__init__(target)
223
+ self.serializationType = self.WILDCARD
224
+
225
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
226
+ return symbol >= minVocabSymbol and symbol <= maxVocabSymbol
227
+
228
+ def __str__(self):
229
+ return "."
230
+
231
+
232
+ class PrecedencePredicateTransition(AbstractPredicateTransition):
233
+ __slots__ = ('serializationType', 'precedence')
234
+
235
+ def __init__(self, target:ATNState, precedence:int):
236
+ super().__init__(target)
237
+ self.serializationType = self.PRECEDENCE
238
+ self.precedence = precedence
239
+ self.isEpsilon = True
240
+
241
+ def matches( self, symbol:int, minVocabSymbol:int, maxVocabSymbol:int):
242
+ return False
243
+
244
+
245
+ def getPredicate(self):
246
+ return PrecedencePredicate(self.precedence)
247
+
248
+ def __str__(self):
249
+ return self.precedence + " >= _p"
250
+
251
+
252
+ Transition.serializationTypes = {
253
+ EpsilonTransition: Transition.EPSILON,
254
+ RangeTransition: Transition.RANGE,
255
+ RuleTransition: Transition.RULE,
256
+ PredicateTransition: Transition.PREDICATE,
257
+ AtomTransition: Transition.ATOM,
258
+ ActionTransition: Transition.ACTION,
259
+ SetTransition: Transition.SET,
260
+ NotSetTransition: Transition.NOT_SET,
261
+ WildcardTransition: Transition.WILDCARD,
262
+ PrecedencePredicateTransition: Transition.PRECEDENCE
263
+ }
264
+
265
+ del ATNState
266
+ del RuleStartState
267
+
268
+ from antlr4.atn.ATNState import *
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/atn/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __author__ = 'ericvergnaud'
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/DFA.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ from antlr4.atn.ATNState import StarLoopEntryState
6
+
7
+ from antlr4.atn.ATNConfigSet import ATNConfigSet
8
+ from antlr4.atn.ATNState import DecisionState
9
+ from antlr4.dfa.DFAState import DFAState
10
+ from antlr4.error.Errors import IllegalStateException
11
+
12
+
13
+ class DFA(object):
14
+ __slots__ = ('atnStartState', 'decision', '_states', 's0', 'precedenceDfa')
15
+
16
+ def __init__(self, atnStartState:DecisionState, decision:int=0):
17
+ # From which ATN state did we create this DFA?
18
+ self.atnStartState = atnStartState
19
+ self.decision = decision
20
+ # A set of all DFA states. Use {@link Map} so we can get old state back
21
+ # ({@link Set} only allows you to see if it's there).
22
+ self._states = dict()
23
+ self.s0 = None
24
+ # {@code true} if this DFA is for a precedence decision; otherwise,
25
+ # {@code false}. This is the backing field for {@link #isPrecedenceDfa},
26
+ # {@link #setPrecedenceDfa}.
27
+ self.precedenceDfa = False
28
+
29
+ if isinstance(atnStartState, StarLoopEntryState):
30
+ if atnStartState.isPrecedenceDecision:
31
+ self.precedenceDfa = True
32
+ precedenceState = DFAState(configs=ATNConfigSet())
33
+ precedenceState.edges = []
34
+ precedenceState.isAcceptState = False
35
+ precedenceState.requiresFullContext = False
36
+ self.s0 = precedenceState
37
+
38
+
39
+ # Get the start state for a specific precedence value.
40
+ #
41
+ # @param precedence The current precedence.
42
+ # @return The start state corresponding to the specified precedence, or
43
+ # {@code null} if no start state exists for the specified precedence.
44
+ #
45
+ # @throws IllegalStateException if this is not a precedence DFA.
46
+ # @see #isPrecedenceDfa()
47
+
48
+ def getPrecedenceStartState(self, precedence:int):
49
+ if not self.precedenceDfa:
50
+ raise IllegalStateException("Only precedence DFAs may contain a precedence start state.")
51
+
52
+ # s0.edges is never null for a precedence DFA
53
+ if precedence < 0 or precedence >= len(self.s0.edges):
54
+ return None
55
+ return self.s0.edges[precedence]
56
+
57
+ # Set the start state for a specific precedence value.
58
+ #
59
+ # @param precedence The current precedence.
60
+ # @param startState The start state corresponding to the specified
61
+ # precedence.
62
+ #
63
+ # @throws IllegalStateException if this is not a precedence DFA.
64
+ # @see #isPrecedenceDfa()
65
+ #
66
+ def setPrecedenceStartState(self, precedence:int, startState:DFAState):
67
+ if not self.precedenceDfa:
68
+ raise IllegalStateException("Only precedence DFAs may contain a precedence start state.")
69
+
70
+ if precedence < 0:
71
+ return
72
+
73
+ # synchronization on s0 here is ok. when the DFA is turned into a
74
+ # precedence DFA, s0 will be initialized once and not updated again
75
+ # s0.edges is never null for a precedence DFA
76
+ if precedence >= len(self.s0.edges):
77
+ ext = [None] * (precedence + 1 - len(self.s0.edges))
78
+ self.s0.edges.extend(ext)
79
+ self.s0.edges[precedence] = startState
80
+ #
81
+ # Sets whether this is a precedence DFA. If the specified value differs
82
+ # from the current DFA configuration, the following actions are taken;
83
+ # otherwise no changes are made to the current DFA.
84
+ #
85
+ # <ul>
86
+ # <li>The {@link #states} map is cleared</li>
87
+ # <li>If {@code precedenceDfa} is {@code false}, the initial state
88
+ # {@link #s0} is set to {@code null}; otherwise, it is initialized to a new
89
+ # {@link DFAState} with an empty outgoing {@link DFAState#edges} array to
90
+ # store the start states for individual precedence values.</li>
91
+ # <li>The {@link #precedenceDfa} field is updated</li>
92
+ # </ul>
93
+ #
94
+ # @param precedenceDfa {@code true} if this is a precedence DFA; otherwise,
95
+ # {@code false}
96
+
97
+ def setPrecedenceDfa(self, precedenceDfa:bool):
98
+ if self.precedenceDfa != precedenceDfa:
99
+ self._states = dict()
100
+ if precedenceDfa:
101
+ precedenceState = DFAState(configs=ATNConfigSet())
102
+ precedenceState.edges = []
103
+ precedenceState.isAcceptState = False
104
+ precedenceState.requiresFullContext = False
105
+ self.s0 = precedenceState
106
+ else:
107
+ self.s0 = None
108
+ self.precedenceDfa = precedenceDfa
109
+
110
+ @property
111
+ def states(self):
112
+ return self._states
113
+
114
+ # Return a list of all states in this DFA, ordered by state number.
115
+ def sortedStates(self):
116
+ return sorted(self._states.keys(), key=lambda state: state.stateNumber)
117
+
118
+ def __str__(self):
119
+ return self.toString(None)
120
+
121
+ def toString(self, literalNames:list=None, symbolicNames:list=None):
122
+ if self.s0 is None:
123
+ return ""
124
+ from antlr4.dfa.DFASerializer import DFASerializer
125
+ serializer = DFASerializer(self,literalNames,symbolicNames)
126
+ return str(serializer)
127
+
128
+ def toLexerString(self):
129
+ if self.s0 is None:
130
+ return ""
131
+ from antlr4.dfa.DFASerializer import LexerDFASerializer
132
+ serializer = LexerDFASerializer(self)
133
+ return str(serializer)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/DFASerializer.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #/
6
+
7
+ # A DFA walker that knows how to dump them to serialized strings.#/
8
+ from io import StringIO
9
+ from antlr4 import DFA
10
+ from antlr4.Utils import str_list
11
+ from antlr4.dfa.DFAState import DFAState
12
+
13
+
14
+ class DFASerializer(object):
15
+ __slots__ = ('dfa', 'literalNames', 'symbolicNames')
16
+
17
+ def __init__(self, dfa:DFA, literalNames:list=None, symbolicNames:list=None):
18
+ self.dfa = dfa
19
+ self.literalNames = literalNames
20
+ self.symbolicNames = symbolicNames
21
+
22
+ def __str__(self):
23
+ if self.dfa.s0 is None:
24
+ return None
25
+ with StringIO() as buf:
26
+ for s in self.dfa.sortedStates():
27
+ n = 0
28
+ if s.edges is not None:
29
+ n = len(s.edges)
30
+ for i in range(0, n):
31
+ t = s.edges[i]
32
+ if t is not None and t.stateNumber != 0x7FFFFFFF:
33
+ buf.write(self.getStateString(s))
34
+ label = self.getEdgeLabel(i)
35
+ buf.write("-")
36
+ buf.write(label)
37
+ buf.write("->")
38
+ buf.write(self.getStateString(t))
39
+ buf.write('\n')
40
+ output = buf.getvalue()
41
+ if len(output)==0:
42
+ return None
43
+ else:
44
+ return output
45
+
46
+ def getEdgeLabel(self, i:int):
47
+ if i==0:
48
+ return "EOF"
49
+ if self.literalNames is not None and i<=len(self.literalNames):
50
+ return self.literalNames[i-1]
51
+ elif self.symbolicNames is not None and i<=len(self.symbolicNames):
52
+ return self.symbolicNames[i-1]
53
+ else:
54
+ return str(i-1)
55
+
56
+ def getStateString(self, s:DFAState):
57
+ n = s.stateNumber
58
+ baseStateStr = ( ":" if s.isAcceptState else "") + "s" + str(n) + ( "^" if s.requiresFullContext else "")
59
+ if s.isAcceptState:
60
+ if s.predicates is not None:
61
+ return baseStateStr + "=>" + str_list(s.predicates)
62
+ else:
63
+ return baseStateStr + "=>" + str(s.prediction)
64
+ else:
65
+ return baseStateStr
66
+
67
+ class LexerDFASerializer(DFASerializer):
68
+
69
+ def __init__(self, dfa:DFA):
70
+ super().__init__(dfa, None)
71
+
72
+ def getEdgeLabel(self, i:int):
73
+ return "'" + chr(i) + "'"
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/DFAState.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #/
6
+
7
+ # Map a predicate to a predicted alternative.#/
8
+ from io import StringIO
9
+ from antlr4.atn.ATNConfigSet import ATNConfigSet
10
+ from antlr4.atn.SemanticContext import SemanticContext
11
+
12
+
13
+ class PredPrediction(object):
14
+ __slots__ = ('alt', 'pred')
15
+
16
+ def __init__(self, pred:SemanticContext, alt:int):
17
+ self.alt = alt
18
+ self.pred = pred
19
+
20
+ def __str__(self):
21
+ return "(" + str(self.pred) + ", " + str(self.alt) + ")"
22
+
23
+ # A DFA state represents a set of possible ATN configurations.
24
+ # As Aho, Sethi, Ullman p. 117 says "The DFA uses its state
25
+ # to keep track of all possible states the ATN can be in after
26
+ # reading each input symbol. That is to say, after reading
27
+ # input a1a2..an, the DFA is in a state that represents the
28
+ # subset T of the states of the ATN that are reachable from the
29
+ # ATN's start state along some path labeled a1a2..an."
30
+ # In conventional NFA&rarr;DFA conversion, therefore, the subset T
31
+ # would be a bitset representing the set of states the
32
+ # ATN could be in. We need to track the alt predicted by each
33
+ # state as well, however. More importantly, we need to maintain
34
+ # a stack of states, tracking the closure operations as they
35
+ # jump from rule to rule, emulating rule invocations (method calls).
36
+ # I have to add a stack to simulate the proper lookahead sequences for
37
+ # the underlying LL grammar from which the ATN was derived.
38
+ #
39
+ # <p>I use a set of ATNConfig objects not simple states. An ATNConfig
40
+ # is both a state (ala normal conversion) and a RuleContext describing
41
+ # the chain of rules (if any) followed to arrive at that state.</p>
42
+ #
43
+ # <p>A DFA state may have multiple references to a particular state,
44
+ # but with different ATN contexts (with same or different alts)
45
+ # meaning that state was reached via a different set of rule invocations.</p>
46
+ #/
47
+ class DFAState(object):
48
+ __slots__ = (
49
+ 'stateNumber', 'configs', 'edges', 'isAcceptState', 'prediction',
50
+ 'lexerActionExecutor', 'requiresFullContext', 'predicates'
51
+ )
52
+
53
+ def __init__(self, stateNumber:int=-1, configs:ATNConfigSet=ATNConfigSet()):
54
+ self.stateNumber = stateNumber
55
+ self.configs = configs
56
+ # {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1)
57
+ # {@link Token#EOF} maps to {@code edges[0]}.
58
+ self.edges = None
59
+ self.isAcceptState = False
60
+ # if accept state, what ttype do we match or alt do we predict?
61
+ # This is set to {@link ATN#INVALID_ALT_NUMBER} when {@link #predicates}{@code !=null} or
62
+ # {@link #requiresFullContext}.
63
+ self.prediction = 0
64
+ self.lexerActionExecutor = None
65
+ # Indicates that this state was created during SLL prediction that
66
+ # discovered a conflict between the configurations in the state. Future
67
+ # {@link ParserATNSimulator#execATN} invocations immediately jumped doing
68
+ # full context prediction if this field is true.
69
+ self.requiresFullContext = False
70
+ # During SLL parsing, this is a list of predicates associated with the
71
+ # ATN configurations of the DFA state. When we have predicates,
72
+ # {@link #requiresFullContext} is {@code false} since full context prediction evaluates predicates
73
+ # on-the-fly. If this is not null, then {@link #prediction} is
74
+ # {@link ATN#INVALID_ALT_NUMBER}.
75
+ #
76
+ # <p>We only use these for non-{@link #requiresFullContext} but conflicting states. That
77
+ # means we know from the context (it's $ or we don't dip into outer
78
+ # context) that it's an ambiguity not a conflict.</p>
79
+ #
80
+ # <p>This list is computed by {@link ParserATNSimulator#predicateDFAState}.</p>
81
+ self.predicates = None
82
+
83
+
84
+
85
+ # Get the set of all alts mentioned by all ATN configurations in this
86
+ # DFA state.
87
+ def getAltSet(self):
88
+ if self.configs is not None:
89
+ return set(cfg.alt for cfg in self.configs) or None
90
+ return None
91
+
92
+ def __hash__(self):
93
+ return hash(self.configs)
94
+
95
+ # Two {@link DFAState} instances are equal if their ATN configuration sets
96
+ # are the same. This method is used to see if a state already exists.
97
+ #
98
+ # <p>Because the number of alternatives and number of ATN configurations are
99
+ # finite, there is a finite number of DFA states that can be processed.
100
+ # This is necessary to show that the algorithm terminates.</p>
101
+ #
102
+ # <p>Cannot test the DFA state numbers here because in
103
+ # {@link ParserATNSimulator#addDFAState} we need to know if any other state
104
+ # exists that has this exact set of ATN configurations. The
105
+ # {@link #stateNumber} is irrelevant.</p>
106
+ def __eq__(self, other):
107
+ # compare set of ATN configurations in this set with other
108
+ if self is other:
109
+ return True
110
+ elif not isinstance(other, DFAState):
111
+ return False
112
+ else:
113
+ return self.configs==other.configs
114
+
115
+ def __str__(self):
116
+ with StringIO() as buf:
117
+ buf.write(str(self.stateNumber))
118
+ buf.write(":")
119
+ buf.write(str(self.configs))
120
+ if self.isAcceptState:
121
+ buf.write("=>")
122
+ if self.predicates is not None:
123
+ buf.write(str(self.predicates))
124
+ else:
125
+ buf.write(str(self.prediction))
126
+ return buf.getvalue()
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/dfa/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __author__ = 'ericvergnaud'
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/DiagnosticErrorListener.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+
8
+ #
9
+ # This implementation of {@link ANTLRErrorListener} can be used to identify
10
+ # certain potential correctness and performance problems in grammars. "Reports"
11
+ # are made by calling {@link Parser#notifyErrorListeners} with the appropriate
12
+ # message.
13
+ #
14
+ # <ul>
15
+ # <li><b>Ambiguities</b>: These are cases where more than one path through the
16
+ # grammar can match the input.</li>
17
+ # <li><b>Weak context sensitivity</b>: These are cases where full-context
18
+ # prediction resolved an SLL conflict to a unique alternative which equaled the
19
+ # minimum alternative of the SLL conflict.</li>
20
+ # <li><b>Strong (forced) context sensitivity</b>: These are cases where the
21
+ # full-context prediction resolved an SLL conflict to a unique alternative,
22
+ # <em>and</em> the minimum alternative of the SLL conflict was found to not be
23
+ # a truly viable alternative. Two-stage parsing cannot be used for inputs where
24
+ # this situation occurs.</li>
25
+ # </ul>
26
+
27
+ from io import StringIO
28
+ from antlr4 import Parser, DFA
29
+ from antlr4.atn.ATNConfigSet import ATNConfigSet
30
+ from antlr4.error.ErrorListener import ErrorListener
31
+
32
+ class DiagnosticErrorListener(ErrorListener):
33
+
34
+ def __init__(self, exactOnly:bool=True):
35
+ # whether all ambiguities or only exact ambiguities are reported.
36
+ self.exactOnly = exactOnly
37
+
38
+ def reportAmbiguity(self, recognizer:Parser, dfa:DFA, startIndex:int,
39
+ stopIndex:int, exact:bool, ambigAlts:set, configs:ATNConfigSet):
40
+ if self.exactOnly and not exact:
41
+ return
42
+
43
+ with StringIO() as buf:
44
+ buf.write("reportAmbiguity d=")
45
+ buf.write(self.getDecisionDescription(recognizer, dfa))
46
+ buf.write(": ambigAlts=")
47
+ buf.write(str(self.getConflictingAlts(ambigAlts, configs)))
48
+ buf.write(", input='")
49
+ buf.write(recognizer.getTokenStream().getText(startIndex, stopIndex))
50
+ buf.write("'")
51
+ recognizer.notifyErrorListeners(buf.getvalue())
52
+
53
+
54
+ def reportAttemptingFullContext(self, recognizer:Parser, dfa:DFA, startIndex:int,
55
+ stopIndex:int, conflictingAlts:set, configs:ATNConfigSet):
56
+ with StringIO() as buf:
57
+ buf.write("reportAttemptingFullContext d=")
58
+ buf.write(self.getDecisionDescription(recognizer, dfa))
59
+ buf.write(", input='")
60
+ buf.write(recognizer.getTokenStream().getText(startIndex, stopIndex))
61
+ buf.write("'")
62
+ recognizer.notifyErrorListeners(buf.getvalue())
63
+
64
+ def reportContextSensitivity(self, recognizer:Parser, dfa:DFA, startIndex:int,
65
+ stopIndex:int, prediction:int, configs:ATNConfigSet):
66
+ with StringIO() as buf:
67
+ buf.write("reportContextSensitivity d=")
68
+ buf.write(self.getDecisionDescription(recognizer, dfa))
69
+ buf.write(", input='")
70
+ buf.write(recognizer.getTokenStream().getText(startIndex, stopIndex))
71
+ buf.write("'")
72
+ recognizer.notifyErrorListeners(buf.getvalue())
73
+
74
+ def getDecisionDescription(self, recognizer:Parser, dfa:DFA):
75
+ decision = dfa.decision
76
+ ruleIndex = dfa.atnStartState.ruleIndex
77
+
78
+ ruleNames = recognizer.ruleNames
79
+ if ruleIndex < 0 or ruleIndex >= len(ruleNames):
80
+ return str(decision)
81
+
82
+ ruleName = ruleNames[ruleIndex]
83
+ if ruleName is None or len(ruleName)==0:
84
+ return str(decision)
85
+
86
+ return str(decision) + " (" + ruleName + ")"
87
+
88
+ #
89
+ # Computes the set of conflicting or ambiguous alternatives from a
90
+ # configuration set, if that information was not already provided by the
91
+ # parser.
92
+ #
93
+ # @param reportedAlts The set of conflicting or ambiguous alternatives, as
94
+ # reported by the parser.
95
+ # @param configs The conflicting or ambiguous configuration set.
96
+ # @return Returns {@code reportedAlts} if it is not {@code null}, otherwise
97
+ # returns the set of alternatives represented in {@code configs}.
98
+ #
99
+ def getConflictingAlts(self, reportedAlts:set, configs:ATNConfigSet):
100
+ if reportedAlts is not None:
101
+ return reportedAlts
102
+
103
+ result = set()
104
+ for config in configs:
105
+ result.add(config.alt)
106
+
107
+ return result
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/ErrorListener.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+
6
+ # Provides an empty default implementation of {@link ANTLRErrorListener}. The
7
+ # default implementation of each method does nothing, but can be overridden as
8
+ # necessary.
9
+
10
+
11
+ import sys
12
+
13
+ class ErrorListener(object):
14
+
15
+ def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
16
+ pass
17
+
18
+ def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
19
+ pass
20
+
21
+ def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
22
+ pass
23
+
24
+ def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
25
+ pass
26
+
27
+ class ConsoleErrorListener(ErrorListener):
28
+ #
29
+ # Provides a default instance of {@link ConsoleErrorListener}.
30
+ #
31
+ INSTANCE = None
32
+
33
+ #
34
+ # {@inheritDoc}
35
+ #
36
+ # <p>
37
+ # This implementation prints messages to {@link System#err} containing the
38
+ # values of {@code line}, {@code charPositionInLine}, and {@code msg} using
39
+ # the following format.</p>
40
+ #
41
+ # <pre>
42
+ # line <em>line</em>:<em>charPositionInLine</em> <em>msg</em>
43
+ # </pre>
44
+ #
45
+ def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
46
+ print("line " + str(line) + ":" + str(column) + " " + msg, file=sys.stderr)
47
+
48
+ ConsoleErrorListener.INSTANCE = ConsoleErrorListener()
49
+
50
+ class ProxyErrorListener(ErrorListener):
51
+
52
+ def __init__(self, delegates):
53
+ super().__init__()
54
+ if delegates is None:
55
+ raise ReferenceError("delegates")
56
+ self.delegates = delegates
57
+
58
+ def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
59
+ for delegate in self.delegates:
60
+ delegate.syntaxError(recognizer, offendingSymbol, line, column, msg, e)
61
+
62
+ def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
63
+ for delegate in self.delegates:
64
+ delegate.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs)
65
+
66
+ def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
67
+ for delegate in self.delegates:
68
+ delegate.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs)
69
+
70
+ def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
71
+ for delegate in self.delegates:
72
+ delegate.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/ErrorStrategy.py ADDED
@@ -0,0 +1,709 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+ import sys
7
+ from antlr4.IntervalSet import IntervalSet
8
+
9
+ from antlr4.Token import Token
10
+ from antlr4.atn.ATNState import ATNState
11
+ from antlr4.error.Errors import RecognitionException, NoViableAltException, InputMismatchException, \
12
+ FailedPredicateException, ParseCancellationException
13
+
14
+ # need forward declaration
15
+ Parser = None
16
+
17
+ class ErrorStrategy(object):
18
+
19
+ def reset(self, recognizer:Parser):
20
+ pass
21
+
22
+ def recoverInline(self, recognizer:Parser):
23
+ pass
24
+
25
+ def recover(self, recognizer:Parser, e:RecognitionException):
26
+ pass
27
+
28
+ def sync(self, recognizer:Parser):
29
+ pass
30
+
31
+ def inErrorRecoveryMode(self, recognizer:Parser):
32
+ pass
33
+
34
+ def reportError(self, recognizer:Parser, e:RecognitionException):
35
+ pass
36
+
37
+
38
+ # This is the default implementation of {@link ANTLRErrorStrategy} used for
39
+ # error reporting and recovery in ANTLR parsers.
40
+ #
41
+ class DefaultErrorStrategy(ErrorStrategy):
42
+
43
+ def __init__(self):
44
+ super().__init__()
45
+ # Indicates whether the error strategy is currently "recovering from an
46
+ # error". This is used to suppress reporting multiple error messages while
47
+ # attempting to recover from a detected syntax error.
48
+ #
49
+ # @see #inErrorRecoveryMode
50
+ #
51
+ self.errorRecoveryMode = False
52
+
53
+ # The index into the input stream where the last error occurred.
54
+ # This is used to prevent infinite loops where an error is found
55
+ # but no token is consumed during recovery...another error is found,
56
+ # ad nauseum. This is a failsafe mechanism to guarantee that at least
57
+ # one token/tree node is consumed for two errors.
58
+ #
59
+ self.lastErrorIndex = -1
60
+ self.lastErrorStates = None
61
+ self.nextTokensContext = None
62
+ self.nextTokenState = 0
63
+
64
+ # <p>The default implementation simply calls {@link #endErrorCondition} to
65
+ # ensure that the handler is not in error recovery mode.</p>
66
+ def reset(self, recognizer:Parser):
67
+ self.endErrorCondition(recognizer)
68
+
69
+ #
70
+ # This method is called to enter error recovery mode when a recognition
71
+ # exception is reported.
72
+ #
73
+ # @param recognizer the parser instance
74
+ #
75
+ def beginErrorCondition(self, recognizer:Parser):
76
+ self.errorRecoveryMode = True
77
+
78
+ def inErrorRecoveryMode(self, recognizer:Parser):
79
+ return self.errorRecoveryMode
80
+
81
+ #
82
+ # This method is called to leave error recovery mode after recovering from
83
+ # a recognition exception.
84
+ #
85
+ # @param recognizer
86
+ #
87
+ def endErrorCondition(self, recognizer:Parser):
88
+ self.errorRecoveryMode = False
89
+ self.lastErrorStates = None
90
+ self.lastErrorIndex = -1
91
+
92
+ #
93
+ # {@inheritDoc}
94
+ #
95
+ # <p>The default implementation simply calls {@link #endErrorCondition}.</p>
96
+ #
97
+ def reportMatch(self, recognizer:Parser):
98
+ self.endErrorCondition(recognizer)
99
+
100
+ #
101
+ # {@inheritDoc}
102
+ #
103
+ # <p>The default implementation returns immediately if the handler is already
104
+ # in error recovery mode. Otherwise, it calls {@link #beginErrorCondition}
105
+ # and dispatches the reporting task based on the runtime type of {@code e}
106
+ # according to the following table.</p>
107
+ #
108
+ # <ul>
109
+ # <li>{@link NoViableAltException}: Dispatches the call to
110
+ # {@link #reportNoViableAlternative}</li>
111
+ # <li>{@link InputMismatchException}: Dispatches the call to
112
+ # {@link #reportInputMismatch}</li>
113
+ # <li>{@link FailedPredicateException}: Dispatches the call to
114
+ # {@link #reportFailedPredicate}</li>
115
+ # <li>All other types: calls {@link Parser#notifyErrorListeners} to report
116
+ # the exception</li>
117
+ # </ul>
118
+ #
119
+ def reportError(self, recognizer:Parser, e:RecognitionException):
120
+ # if we've already reported an error and have not matched a token
121
+ # yet successfully, don't report any errors.
122
+ if self.inErrorRecoveryMode(recognizer):
123
+ return # don't report spurious errors
124
+ self.beginErrorCondition(recognizer)
125
+ if isinstance( e, NoViableAltException ):
126
+ self.reportNoViableAlternative(recognizer, e)
127
+ elif isinstance( e, InputMismatchException ):
128
+ self.reportInputMismatch(recognizer, e)
129
+ elif isinstance( e, FailedPredicateException ):
130
+ self.reportFailedPredicate(recognizer, e)
131
+ else:
132
+ print("unknown recognition error type: " + type(e).__name__)
133
+ recognizer.notifyErrorListeners(e.message, e.offendingToken, e)
134
+
135
+ #
136
+ # {@inheritDoc}
137
+ #
138
+ # <p>The default implementation resynchronizes the parser by consuming tokens
139
+ # until we find one in the resynchronization set--loosely the set of tokens
140
+ # that can follow the current rule.</p>
141
+ #
142
+ def recover(self, recognizer:Parser, e:RecognitionException):
143
+ if self.lastErrorIndex==recognizer.getInputStream().index \
144
+ and self.lastErrorStates is not None \
145
+ and recognizer.state in self.lastErrorStates:
146
+ # uh oh, another error at same token index and previously-visited
147
+ # state in ATN; must be a case where LT(1) is in the recovery
148
+ # token set so nothing got consumed. Consume a single token
149
+ # at least to prevent an infinite loop; this is a failsafe.
150
+ recognizer.consume()
151
+
152
+ self.lastErrorIndex = recognizer._input.index
153
+ if self.lastErrorStates is None:
154
+ self.lastErrorStates = []
155
+ self.lastErrorStates.append(recognizer.state)
156
+ followSet = self.getErrorRecoverySet(recognizer)
157
+ self.consumeUntil(recognizer, followSet)
158
+
159
+ # The default implementation of {@link ANTLRErrorStrategy#sync} makes sure
160
+ # that the current lookahead symbol is consistent with what were expecting
161
+ # at this point in the ATN. You can call this anytime but ANTLR only
162
+ # generates code to check before subrules/loops and each iteration.
163
+ #
164
+ # <p>Implements Jim Idle's magic sync mechanism in closures and optional
165
+ # subrules. E.g.,</p>
166
+ #
167
+ # <pre>
168
+ # a : sync ( stuff sync )* ;
169
+ # sync : {consume to what can follow sync} ;
170
+ # </pre>
171
+ #
172
+ # At the start of a sub rule upon error, {@link #sync} performs single
173
+ # token deletion, if possible. If it can't do that, it bails on the current
174
+ # rule and uses the default error recovery, which consumes until the
175
+ # resynchronization set of the current rule.
176
+ #
177
+ # <p>If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block
178
+ # with an empty alternative), then the expected set includes what follows
179
+ # the subrule.</p>
180
+ #
181
+ # <p>During loop iteration, it consumes until it sees a token that can start a
182
+ # sub rule or what follows loop. Yes, that is pretty aggressive. We opt to
183
+ # stay in the loop as long as possible.</p>
184
+ #
185
+ # <p><strong>ORIGINS</strong></p>
186
+ #
187
+ # <p>Previous versions of ANTLR did a poor job of their recovery within loops.
188
+ # A single mismatch token or missing token would force the parser to bail
189
+ # out of the entire rules surrounding the loop. So, for rule</p>
190
+ #
191
+ # <pre>
192
+ # classDef : 'class' ID '{' member* '}'
193
+ # </pre>
194
+ #
195
+ # input with an extra token between members would force the parser to
196
+ # consume until it found the next class definition rather than the next
197
+ # member definition of the current class.
198
+ #
199
+ # <p>This functionality cost a little bit of effort because the parser has to
200
+ # compare token set at the start of the loop and at each iteration. If for
201
+ # some reason speed is suffering for you, you can turn off this
202
+ # functionality by simply overriding this method as a blank { }.</p>
203
+ #
204
+ def sync(self, recognizer:Parser):
205
+ # If already recovering, don't try to sync
206
+ if self.inErrorRecoveryMode(recognizer):
207
+ return
208
+
209
+ s = recognizer._interp.atn.states[recognizer.state]
210
+ la = recognizer.getTokenStream().LA(1)
211
+ # try cheaper subset first; might get lucky. seems to shave a wee bit off
212
+ nextTokens = recognizer.atn.nextTokens(s)
213
+ if la in nextTokens:
214
+ self.nextTokensContext = None
215
+ self.nextTokenState = ATNState.INVALID_STATE_NUMBER
216
+ return
217
+ elif Token.EPSILON in nextTokens:
218
+ if self.nextTokensContext is None:
219
+ # It's possible the next token won't match information tracked
220
+ # by sync is restricted for performance.
221
+ self.nextTokensContext = recognizer._ctx
222
+ self.nextTokensState = recognizer._stateNumber
223
+ return
224
+
225
+ if s.stateType in [ATNState.BLOCK_START, ATNState.STAR_BLOCK_START,
226
+ ATNState.PLUS_BLOCK_START, ATNState.STAR_LOOP_ENTRY]:
227
+ # report error and recover if possible
228
+ if self.singleTokenDeletion(recognizer)is not None:
229
+ return
230
+ else:
231
+ raise InputMismatchException(recognizer)
232
+
233
+ elif s.stateType in [ATNState.PLUS_LOOP_BACK, ATNState.STAR_LOOP_BACK]:
234
+ self.reportUnwantedToken(recognizer)
235
+ expecting = recognizer.getExpectedTokens()
236
+ whatFollowsLoopIterationOrRule = expecting.addSet(self.getErrorRecoverySet(recognizer))
237
+ self.consumeUntil(recognizer, whatFollowsLoopIterationOrRule)
238
+
239
+ else:
240
+ # do nothing if we can't identify the exact kind of ATN state
241
+ pass
242
+
243
+ # This is called by {@link #reportError} when the exception is a
244
+ # {@link NoViableAltException}.
245
+ #
246
+ # @see #reportError
247
+ #
248
+ # @param recognizer the parser instance
249
+ # @param e the recognition exception
250
+ #
251
+ def reportNoViableAlternative(self, recognizer:Parser, e:NoViableAltException):
252
+ tokens = recognizer.getTokenStream()
253
+ if tokens is not None:
254
+ if e.startToken.type==Token.EOF:
255
+ input = "<EOF>"
256
+ else:
257
+ input = tokens.getText(e.startToken, e.offendingToken)
258
+ else:
259
+ input = "<unknown input>"
260
+ msg = "no viable alternative at input " + self.escapeWSAndQuote(input)
261
+ recognizer.notifyErrorListeners(msg, e.offendingToken, e)
262
+
263
+ #
264
+ # This is called by {@link #reportError} when the exception is an
265
+ # {@link InputMismatchException}.
266
+ #
267
+ # @see #reportError
268
+ #
269
+ # @param recognizer the parser instance
270
+ # @param e the recognition exception
271
+ #
272
+ def reportInputMismatch(self, recognizer:Parser, e:InputMismatchException):
273
+ msg = "mismatched input " + self.getTokenErrorDisplay(e.offendingToken) \
274
+ + " expecting " + e.getExpectedTokens().toString(recognizer.literalNames, recognizer.symbolicNames)
275
+ recognizer.notifyErrorListeners(msg, e.offendingToken, e)
276
+
277
+ #
278
+ # This is called by {@link #reportError} when the exception is a
279
+ # {@link FailedPredicateException}.
280
+ #
281
+ # @see #reportError
282
+ #
283
+ # @param recognizer the parser instance
284
+ # @param e the recognition exception
285
+ #
286
+ def reportFailedPredicate(self, recognizer, e):
287
+ ruleName = recognizer.ruleNames[recognizer._ctx.getRuleIndex()]
288
+ msg = "rule " + ruleName + " " + e.message
289
+ recognizer.notifyErrorListeners(msg, e.offendingToken, e)
290
+
291
+ # This method is called to report a syntax error which requires the removal
292
+ # of a token from the input stream. At the time this method is called, the
293
+ # erroneous symbol is current {@code LT(1)} symbol and has not yet been
294
+ # removed from the input stream. When this method returns,
295
+ # {@code recognizer} is in error recovery mode.
296
+ #
297
+ # <p>This method is called when {@link #singleTokenDeletion} identifies
298
+ # single-token deletion as a viable recovery strategy for a mismatched
299
+ # input error.</p>
300
+ #
301
+ # <p>The default implementation simply returns if the handler is already in
302
+ # error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
303
+ # enter error recovery mode, followed by calling
304
+ # {@link Parser#notifyErrorListeners}.</p>
305
+ #
306
+ # @param recognizer the parser instance
307
+ #
308
+ def reportUnwantedToken(self, recognizer:Parser):
309
+ if self.inErrorRecoveryMode(recognizer):
310
+ return
311
+
312
+ self.beginErrorCondition(recognizer)
313
+ t = recognizer.getCurrentToken()
314
+ tokenName = self.getTokenErrorDisplay(t)
315
+ expecting = self.getExpectedTokens(recognizer)
316
+ msg = "extraneous input " + tokenName + " expecting " \
317
+ + expecting.toString(recognizer.literalNames, recognizer.symbolicNames)
318
+ recognizer.notifyErrorListeners(msg, t, None)
319
+
320
+ # This method is called to report a syntax error which requires the
321
+ # insertion of a missing token into the input stream. At the time this
322
+ # method is called, the missing token has not yet been inserted. When this
323
+ # method returns, {@code recognizer} is in error recovery mode.
324
+ #
325
+ # <p>This method is called when {@link #singleTokenInsertion} identifies
326
+ # single-token insertion as a viable recovery strategy for a mismatched
327
+ # input error.</p>
328
+ #
329
+ # <p>The default implementation simply returns if the handler is already in
330
+ # error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
331
+ # enter error recovery mode, followed by calling
332
+ # {@link Parser#notifyErrorListeners}.</p>
333
+ #
334
+ # @param recognizer the parser instance
335
+ #
336
+ def reportMissingToken(self, recognizer:Parser):
337
+ if self.inErrorRecoveryMode(recognizer):
338
+ return
339
+ self.beginErrorCondition(recognizer)
340
+ t = recognizer.getCurrentToken()
341
+ expecting = self.getExpectedTokens(recognizer)
342
+ msg = "missing " + expecting.toString(recognizer.literalNames, recognizer.symbolicNames) \
343
+ + " at " + self.getTokenErrorDisplay(t)
344
+ recognizer.notifyErrorListeners(msg, t, None)
345
+
346
+ # <p>The default implementation attempts to recover from the mismatched input
347
+ # by using single token insertion and deletion as described below. If the
348
+ # recovery attempt fails, this method throws an
349
+ # {@link InputMismatchException}.</p>
350
+ #
351
+ # <p><strong>EXTRA TOKEN</strong> (single token deletion)</p>
352
+ #
353
+ # <p>{@code LA(1)} is not what we are looking for. If {@code LA(2)} has the
354
+ # right token, however, then assume {@code LA(1)} is some extra spurious
355
+ # token and delete it. Then consume and return the next token (which was
356
+ # the {@code LA(2)} token) as the successful result of the match operation.</p>
357
+ #
358
+ # <p>This recovery strategy is implemented by {@link #singleTokenDeletion}.</p>
359
+ #
360
+ # <p><strong>MISSING TOKEN</strong> (single token insertion)</p>
361
+ #
362
+ # <p>If current token (at {@code LA(1)}) is consistent with what could come
363
+ # after the expected {@code LA(1)} token, then assume the token is missing
364
+ # and use the parser's {@link TokenFactory} to create it on the fly. The
365
+ # "insertion" is performed by returning the created token as the successful
366
+ # result of the match operation.</p>
367
+ #
368
+ # <p>This recovery strategy is implemented by {@link #singleTokenInsertion}.</p>
369
+ #
370
+ # <p><strong>EXAMPLE</strong></p>
371
+ #
372
+ # <p>For example, Input {@code i=(3;} is clearly missing the {@code ')'}. When
373
+ # the parser returns from the nested call to {@code expr}, it will have
374
+ # call chain:</p>
375
+ #
376
+ # <pre>
377
+ # stat &rarr; expr &rarr; atom
378
+ # </pre>
379
+ #
380
+ # and it will be trying to match the {@code ')'} at this point in the
381
+ # derivation:
382
+ #
383
+ # <pre>
384
+ # =&gt; ID '=' '(' INT ')' ('+' atom)* ';'
385
+ # ^
386
+ # </pre>
387
+ #
388
+ # The attempt to match {@code ')'} will fail when it sees {@code ';'} and
389
+ # call {@link #recoverInline}. To recover, it sees that {@code LA(1)==';'}
390
+ # is in the set of tokens that can follow the {@code ')'} token reference
391
+ # in rule {@code atom}. It can assume that you forgot the {@code ')'}.
392
+ #
393
+ def recoverInline(self, recognizer:Parser):
394
+ # SINGLE TOKEN DELETION
395
+ matchedSymbol = self.singleTokenDeletion(recognizer)
396
+ if matchedSymbol is not None:
397
+ # we have deleted the extra token.
398
+ # now, move past ttype token as if all were ok
399
+ recognizer.consume()
400
+ return matchedSymbol
401
+
402
+ # SINGLE TOKEN INSERTION
403
+ if self.singleTokenInsertion(recognizer):
404
+ return self.getMissingSymbol(recognizer)
405
+
406
+ # even that didn't work; must throw the exception
407
+ raise InputMismatchException(recognizer)
408
+
409
+ #
410
+ # This method implements the single-token insertion inline error recovery
411
+ # strategy. It is called by {@link #recoverInline} if the single-token
412
+ # deletion strategy fails to recover from the mismatched input. If this
413
+ # method returns {@code true}, {@code recognizer} will be in error recovery
414
+ # mode.
415
+ #
416
+ # <p>This method determines whether or not single-token insertion is viable by
417
+ # checking if the {@code LA(1)} input symbol could be successfully matched
418
+ # if it were instead the {@code LA(2)} symbol. If this method returns
419
+ # {@code true}, the caller is responsible for creating and inserting a
420
+ # token with the correct type to produce this behavior.</p>
421
+ #
422
+ # @param recognizer the parser instance
423
+ # @return {@code true} if single-token insertion is a viable recovery
424
+ # strategy for the current mismatched input, otherwise {@code false}
425
+ #
426
+ def singleTokenInsertion(self, recognizer:Parser):
427
+ currentSymbolType = recognizer.getTokenStream().LA(1)
428
+ # if current token is consistent with what could come after current
429
+ # ATN state, then we know we're missing a token; error recovery
430
+ # is free to conjure up and insert the missing token
431
+ atn = recognizer._interp.atn
432
+ currentState = atn.states[recognizer.state]
433
+ next = currentState.transitions[0].target
434
+ expectingAtLL2 = atn.nextTokens(next, recognizer._ctx)
435
+ if currentSymbolType in expectingAtLL2:
436
+ self.reportMissingToken(recognizer)
437
+ return True
438
+ else:
439
+ return False
440
+
441
+ # This method implements the single-token deletion inline error recovery
442
+ # strategy. It is called by {@link #recoverInline} to attempt to recover
443
+ # from mismatched input. If this method returns null, the parser and error
444
+ # handler state will not have changed. If this method returns non-null,
445
+ # {@code recognizer} will <em>not</em> be in error recovery mode since the
446
+ # returned token was a successful match.
447
+ #
448
+ # <p>If the single-token deletion is successful, this method calls
449
+ # {@link #reportUnwantedToken} to report the error, followed by
450
+ # {@link Parser#consume} to actually "delete" the extraneous token. Then,
451
+ # before returning {@link #reportMatch} is called to signal a successful
452
+ # match.</p>
453
+ #
454
+ # @param recognizer the parser instance
455
+ # @return the successfully matched {@link Token} instance if single-token
456
+ # deletion successfully recovers from the mismatched input, otherwise
457
+ # {@code null}
458
+ #
459
+ def singleTokenDeletion(self, recognizer:Parser):
460
+ nextTokenType = recognizer.getTokenStream().LA(2)
461
+ expecting = self.getExpectedTokens(recognizer)
462
+ if nextTokenType in expecting:
463
+ self.reportUnwantedToken(recognizer)
464
+ # print("recoverFromMismatchedToken deleting " \
465
+ # + str(recognizer.getTokenStream().LT(1)) \
466
+ # + " since " + str(recognizer.getTokenStream().LT(2)) \
467
+ # + " is what we want", file=sys.stderr)
468
+ recognizer.consume() # simply delete extra token
469
+ # we want to return the token we're actually matching
470
+ matchedSymbol = recognizer.getCurrentToken()
471
+ self.reportMatch(recognizer) # we know current token is correct
472
+ return matchedSymbol
473
+ else:
474
+ return None
475
+
476
+ # Conjure up a missing token during error recovery.
477
+ #
478
+ # The recognizer attempts to recover from single missing
479
+ # symbols. But, actions might refer to that missing symbol.
480
+ # For example, x=ID {f($x);}. The action clearly assumes
481
+ # that there has been an identifier matched previously and that
482
+ # $x points at that token. If that token is missing, but
483
+ # the next token in the stream is what we want we assume that
484
+ # this token is missing and we keep going. Because we
485
+ # have to return some token to replace the missing token,
486
+ # we have to conjure one up. This method gives the user control
487
+ # over the tokens returned for missing tokens. Mostly,
488
+ # you will want to create something special for identifier
489
+ # tokens. For literals such as '{' and ',', the default
490
+ # action in the parser or tree parser works. It simply creates
491
+ # a CommonToken of the appropriate type. The text will be the token.
492
+ # If you change what tokens must be created by the lexer,
493
+ # override this method to create the appropriate tokens.
494
+ #
495
+ def getMissingSymbol(self, recognizer:Parser):
496
+ currentSymbol = recognizer.getCurrentToken()
497
+ expecting = self.getExpectedTokens(recognizer)
498
+ expectedTokenType = expecting[0] # get any element
499
+ if expectedTokenType==Token.EOF:
500
+ tokenText = "<missing EOF>"
501
+ else:
502
+ name = None
503
+ if expectedTokenType < len(recognizer.literalNames):
504
+ name = recognizer.literalNames[expectedTokenType]
505
+ if name is None and expectedTokenType < len(recognizer.symbolicNames):
506
+ name = recognizer.symbolicNames[expectedTokenType]
507
+ tokenText = "<missing " + str(name) + ">"
508
+ current = currentSymbol
509
+ lookback = recognizer.getTokenStream().LT(-1)
510
+ if current.type==Token.EOF and lookback is not None:
511
+ current = lookback
512
+ return recognizer.getTokenFactory().create(current.source,
513
+ expectedTokenType, tokenText, Token.DEFAULT_CHANNEL,
514
+ -1, -1, current.line, current.column)
515
+
516
+ def getExpectedTokens(self, recognizer:Parser):
517
+ return recognizer.getExpectedTokens()
518
+
519
+ # How should a token be displayed in an error message? The default
520
+ # is to display just the text, but during development you might
521
+ # want to have a lot of information spit out. Override in that case
522
+ # to use t.toString() (which, for CommonToken, dumps everything about
523
+ # the token). This is better than forcing you to override a method in
524
+ # your token objects because you don't have to go modify your lexer
525
+ # so that it creates a new Java type.
526
+ #
527
+ def getTokenErrorDisplay(self, t:Token):
528
+ if t is None:
529
+ return "<no token>"
530
+ s = t.text
531
+ if s is None:
532
+ if t.type==Token.EOF:
533
+ s = "<EOF>"
534
+ else:
535
+ s = "<" + str(t.type) + ">"
536
+ return self.escapeWSAndQuote(s)
537
+
538
+ def escapeWSAndQuote(self, s:str):
539
+ s = s.replace("\n","\\n")
540
+ s = s.replace("\r","\\r")
541
+ s = s.replace("\t","\\t")
542
+ return "'" + s + "'"
543
+
544
+ # Compute the error recovery set for the current rule. During
545
+ # rule invocation, the parser pushes the set of tokens that can
546
+ # follow that rule reference on the stack; this amounts to
547
+ # computing FIRST of what follows the rule reference in the
548
+ # enclosing rule. See LinearApproximator.FIRST().
549
+ # This local follow set only includes tokens
550
+ # from within the rule; i.e., the FIRST computation done by
551
+ # ANTLR stops at the end of a rule.
552
+ #
553
+ # EXAMPLE
554
+ #
555
+ # When you find a "no viable alt exception", the input is not
556
+ # consistent with any of the alternatives for rule r. The best
557
+ # thing to do is to consume tokens until you see something that
558
+ # can legally follow a call to r#or* any rule that called r.
559
+ # You don't want the exact set of viable next tokens because the
560
+ # input might just be missing a token--you might consume the
561
+ # rest of the input looking for one of the missing tokens.
562
+ #
563
+ # Consider grammar:
564
+ #
565
+ # a : '[' b ']'
566
+ # | '(' b ')'
567
+ # ;
568
+ # b : c '^' INT ;
569
+ # c : ID
570
+ # | INT
571
+ # ;
572
+ #
573
+ # At each rule invocation, the set of tokens that could follow
574
+ # that rule is pushed on a stack. Here are the various
575
+ # context-sensitive follow sets:
576
+ #
577
+ # FOLLOW(b1_in_a) = FIRST(']') = ']'
578
+ # FOLLOW(b2_in_a) = FIRST(')') = ')'
579
+ # FOLLOW(c_in_b) = FIRST('^') = '^'
580
+ #
581
+ # Upon erroneous input "[]", the call chain is
582
+ #
583
+ # a -> b -> c
584
+ #
585
+ # and, hence, the follow context stack is:
586
+ #
587
+ # depth follow set start of rule execution
588
+ # 0 <EOF> a (from main())
589
+ # 1 ']' b
590
+ # 2 '^' c
591
+ #
592
+ # Notice that ')' is not included, because b would have to have
593
+ # been called from a different context in rule a for ')' to be
594
+ # included.
595
+ #
596
+ # For error recovery, we cannot consider FOLLOW(c)
597
+ # (context-sensitive or otherwise). We need the combined set of
598
+ # all context-sensitive FOLLOW sets--the set of all tokens that
599
+ # could follow any reference in the call chain. We need to
600
+ # resync to one of those tokens. Note that FOLLOW(c)='^' and if
601
+ # we resync'd to that token, we'd consume until EOF. We need to
602
+ # sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}.
603
+ # In this case, for input "[]", LA(1) is ']' and in the set, so we would
604
+ # not consume anything. After printing an error, rule c would
605
+ # return normally. Rule b would not find the required '^' though.
606
+ # At this point, it gets a mismatched token error and throws an
607
+ # exception (since LA(1) is not in the viable following token
608
+ # set). The rule exception handler tries to recover, but finds
609
+ # the same recovery set and doesn't consume anything. Rule b
610
+ # exits normally returning to rule a. Now it finds the ']' (and
611
+ # with the successful match exits errorRecovery mode).
612
+ #
613
+ # So, you can see that the parser walks up the call chain looking
614
+ # for the token that was a member of the recovery set.
615
+ #
616
+ # Errors are not generated in errorRecovery mode.
617
+ #
618
+ # ANTLR's error recovery mechanism is based upon original ideas:
619
+ #
620
+ # "Algorithms + Data Structures = Programs" by Niklaus Wirth
621
+ #
622
+ # and
623
+ #
624
+ # "A note on error recovery in recursive descent parsers":
625
+ # http:#portal.acm.org/citation.cfm?id=947902.947905
626
+ #
627
+ # Later, Josef Grosch had some good ideas:
628
+ #
629
+ # "Efficient and Comfortable Error Recovery in Recursive Descent
630
+ # Parsers":
631
+ # ftp:#www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip
632
+ #
633
+ # Like Grosch I implement context-sensitive FOLLOW sets that are combined
634
+ # at run-time upon error to avoid overhead during parsing.
635
+ #
636
+ def getErrorRecoverySet(self, recognizer:Parser):
637
+ atn = recognizer._interp.atn
638
+ ctx = recognizer._ctx
639
+ recoverSet = IntervalSet()
640
+ while ctx is not None and ctx.invokingState>=0:
641
+ # compute what follows who invoked us
642
+ invokingState = atn.states[ctx.invokingState]
643
+ rt = invokingState.transitions[0]
644
+ follow = atn.nextTokens(rt.followState)
645
+ recoverSet.addSet(follow)
646
+ ctx = ctx.parentCtx
647
+ recoverSet.removeOne(Token.EPSILON)
648
+ return recoverSet
649
+
650
+ # Consume tokens until one matches the given token set.#
651
+ def consumeUntil(self, recognizer:Parser, set_:set):
652
+ ttype = recognizer.getTokenStream().LA(1)
653
+ while ttype != Token.EOF and not ttype in set_:
654
+ recognizer.consume()
655
+ ttype = recognizer.getTokenStream().LA(1)
656
+
657
+
658
+ #
659
+ # This implementation of {@link ANTLRErrorStrategy} responds to syntax errors
660
+ # by immediately canceling the parse operation with a
661
+ # {@link ParseCancellationException}. The implementation ensures that the
662
+ # {@link ParserRuleContext#exception} field is set for all parse tree nodes
663
+ # that were not completed prior to encountering the error.
664
+ #
665
+ # <p>
666
+ # This error strategy is useful in the following scenarios.</p>
667
+ #
668
+ # <ul>
669
+ # <li><strong>Two-stage parsing:</strong> This error strategy allows the first
670
+ # stage of two-stage parsing to immediately terminate if an error is
671
+ # encountered, and immediately fall back to the second stage. In addition to
672
+ # avoiding wasted work by attempting to recover from errors here, the empty
673
+ # implementation of {@link BailErrorStrategy#sync} improves the performance of
674
+ # the first stage.</li>
675
+ # <li><strong>Silent validation:</strong> When syntax errors are not being
676
+ # reported or logged, and the parse result is simply ignored if errors occur,
677
+ # the {@link BailErrorStrategy} avoids wasting work on recovering from errors
678
+ # when the result will be ignored either way.</li>
679
+ # </ul>
680
+ #
681
+ # <p>
682
+ # {@code myparser.setErrorHandler(new BailErrorStrategy());}</p>
683
+ #
684
+ # @see Parser#setErrorHandler(ANTLRErrorStrategy)
685
+ #
686
+ class BailErrorStrategy(DefaultErrorStrategy):
687
+ # Instead of recovering from exception {@code e}, re-throw it wrapped
688
+ # in a {@link ParseCancellationException} so it is not caught by the
689
+ # rule function catches. Use {@link Exception#getCause()} to get the
690
+ # original {@link RecognitionException}.
691
+ #
692
+ def recover(self, recognizer:Parser, e:RecognitionException):
693
+ context = recognizer._ctx
694
+ while context is not None:
695
+ context.exception = e
696
+ context = context.parentCtx
697
+ raise ParseCancellationException(e)
698
+
699
+ # Make sure we don't attempt to recover inline; if the parser
700
+ # successfully recovers, it won't throw an exception.
701
+ #
702
+ def recoverInline(self, recognizer:Parser):
703
+ self.recover(recognizer, InputMismatchException(recognizer))
704
+
705
+ # Make sure we don't attempt to recover from problems in subrules.#
706
+ def sync(self, recognizer:Parser):
707
+ pass
708
+
709
+ del Parser
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/Errors.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2
+ # Use of this file is governed by the BSD 3-clause license that
3
+ # can be found in the LICENSE.txt file in the project root.
4
+ #
5
+
6
+ # need forward declaration
7
+ Token = None
8
+ Lexer = None
9
+ Parser = None
10
+ TokenStream = None
11
+ ATNConfigSet = None
12
+ ParserRulecontext = None
13
+ PredicateTransition = None
14
+ BufferedTokenStream = None
15
+
16
+ class UnsupportedOperationException(Exception):
17
+
18
+ def __init__(self, msg:str):
19
+ super().__init__(msg)
20
+
21
+ class IllegalStateException(Exception):
22
+
23
+ def __init__(self, msg:str):
24
+ super().__init__(msg)
25
+
26
+ class CancellationException(IllegalStateException):
27
+
28
+ def __init__(self, msg:str):
29
+ super().__init__(msg)
30
+
31
+ # The root of the ANTLR exception hierarchy. In general, ANTLR tracks just
32
+ # 3 kinds of errors: prediction errors, failed predicate errors, and
33
+ # mismatched input errors. In each case, the parser knows where it is
34
+ # in the input, where it is in the ATN, the rule invocation stack,
35
+ # and what kind of problem occurred.
36
+
37
+ from antlr4.InputStream import InputStream
38
+ from antlr4.ParserRuleContext import ParserRuleContext
39
+ from antlr4.Recognizer import Recognizer
40
+
41
+ class RecognitionException(Exception):
42
+
43
+
44
+ def __init__(self, message:str=None, recognizer:Recognizer=None, input:InputStream=None, ctx:ParserRulecontext=None):
45
+ super().__init__(message)
46
+ self.message = message
47
+ self.recognizer = recognizer
48
+ self.input = input
49
+ self.ctx = ctx
50
+ # The current {@link Token} when an error occurred. Since not all streams
51
+ # support accessing symbols by index, we have to track the {@link Token}
52
+ # instance itself.
53
+ self.offendingToken = None
54
+ # Get the ATN state number the parser was in at the time the error
55
+ # occurred. For {@link NoViableAltException} and
56
+ # {@link LexerNoViableAltException} exceptions, this is the
57
+ # {@link DecisionState} number. For others, it is the state whose outgoing
58
+ # edge we couldn't match.
59
+ self.offendingState = -1
60
+ if recognizer is not None:
61
+ self.offendingState = recognizer.state
62
+
63
+ # <p>If the state number is not known, this method returns -1.</p>
64
+
65
+ #
66
+ # Gets the set of input symbols which could potentially follow the
67
+ # previously matched symbol at the time this exception was thrown.
68
+ #
69
+ # <p>If the set of expected tokens is not known and could not be computed,
70
+ # this method returns {@code null}.</p>
71
+ #
72
+ # @return The set of token types that could potentially follow the current
73
+ # state in the ATN, or {@code null} if the information is not available.
74
+ #/
75
+ def getExpectedTokens(self):
76
+ if self.recognizer is not None:
77
+ return self.recognizer.atn.getExpectedTokens(self.offendingState, self.ctx)
78
+ else:
79
+ return None
80
+
81
+
82
+ class LexerNoViableAltException(RecognitionException):
83
+
84
+ def __init__(self, lexer:Lexer, input:InputStream, startIndex:int, deadEndConfigs:ATNConfigSet):
85
+ super().__init__(message=None, recognizer=lexer, input=input, ctx=None)
86
+ self.startIndex = startIndex
87
+ self.deadEndConfigs = deadEndConfigs
88
+
89
+ def __str__(self):
90
+ symbol = ""
91
+ if self.startIndex >= 0 and self.startIndex < self.input.size:
92
+ symbol = self.input.getText(self.startIndex, self.startIndex)
93
+ # TODO symbol = Utils.escapeWhitespace(symbol, false);
94
+ return "LexerNoViableAltException('" + symbol + "')"
95
+
96
+ # Indicates that the parser could not decide which of two or more paths
97
+ # to take based upon the remaining input. It tracks the starting token
98
+ # of the offending input and also knows where the parser was
99
+ # in the various paths when the error. Reported by reportNoViableAlternative()
100
+ #
101
+ class NoViableAltException(RecognitionException):
102
+
103
+ def __init__(self, recognizer:Parser, input:TokenStream=None, startToken:Token=None,
104
+ offendingToken:Token=None, deadEndConfigs:ATNConfigSet=None, ctx:ParserRuleContext=None):
105
+ if ctx is None:
106
+ ctx = recognizer._ctx
107
+ if offendingToken is None:
108
+ offendingToken = recognizer.getCurrentToken()
109
+ if startToken is None:
110
+ startToken = recognizer.getCurrentToken()
111
+ if input is None:
112
+ input = recognizer.getInputStream()
113
+ super().__init__(recognizer=recognizer, input=input, ctx=ctx)
114
+ # Which configurations did we try at input.index() that couldn't match input.LT(1)?#
115
+ self.deadEndConfigs = deadEndConfigs
116
+ # The token object at the start index; the input stream might
117
+ # not be buffering tokens so get a reference to it. (At the
118
+ # time the error occurred, of course the stream needs to keep a
119
+ # buffer all of the tokens but later we might not have access to those.)
120
+ self.startToken = startToken
121
+ self.offendingToken = offendingToken
122
+
123
+ # This signifies any kind of mismatched input exceptions such as
124
+ # when the current input does not match the expected token.
125
+ #
126
+ class InputMismatchException(RecognitionException):
127
+
128
+ def __init__(self, recognizer:Parser):
129
+ super().__init__(recognizer=recognizer, input=recognizer.getInputStream(), ctx=recognizer._ctx)
130
+ self.offendingToken = recognizer.getCurrentToken()
131
+
132
+
133
+ # A semantic predicate failed during validation. Validation of predicates
134
+ # occurs when normally parsing the alternative just like matching a token.
135
+ # Disambiguating predicate evaluation occurs when we test a predicate during
136
+ # prediction.
137
+
138
+ class FailedPredicateException(RecognitionException):
139
+
140
+ def __init__(self, recognizer:Parser, predicate:str=None, message:str=None):
141
+ super().__init__(message=self.formatMessage(predicate,message), recognizer=recognizer,
142
+ input=recognizer.getInputStream(), ctx=recognizer._ctx)
143
+ s = recognizer._interp.atn.states[recognizer.state]
144
+ trans = s.transitions[0]
145
+ from antlr4.atn.Transition import PredicateTransition
146
+ if isinstance(trans, PredicateTransition):
147
+ self.ruleIndex = trans.ruleIndex
148
+ self.predicateIndex = trans.predIndex
149
+ else:
150
+ self.ruleIndex = 0
151
+ self.predicateIndex = 0
152
+ self.predicate = predicate
153
+ self.offendingToken = recognizer.getCurrentToken()
154
+
155
+ def formatMessage(self, predicate:str, message:str):
156
+ if message is not None:
157
+ return message
158
+ else:
159
+ return "failed predicate: {" + predicate + "}?"
160
+
161
+ class ParseCancellationException(CancellationException):
162
+
163
+ pass
164
+
165
+ del Token
166
+ del Lexer
167
+ del Parser
168
+ del TokenStream
169
+ del ATNConfigSet
170
+ del ParserRulecontext
171
+ del PredicateTransition
172
+ del BufferedTokenStream
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/error/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __author__ = 'ericvergnaud'
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/Chunk.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ class Chunk(object):
8
+ pass
9
+
10
+ class TagChunk(Chunk):
11
+ __slots__ = ('tag', 'label')
12
+
13
+ def __init__(self, tag:str, label:str=None):
14
+ self.tag = tag
15
+ self.label = label
16
+
17
+ def __str__(self):
18
+ if self.label is None:
19
+ return self.tag
20
+ else:
21
+ return self.label + ":" + self.tag
22
+
23
+ class TextChunk(Chunk):
24
+ __slots__ = 'text'
25
+
26
+ def __init__(self, text:str):
27
+ self.text = text
28
+
29
+ def __str__(self):
30
+ return "'" + self.text + "'"
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/ParseTreeMatch.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+
8
+ #
9
+ # Represents the result of matching a {@link ParseTree} against a tree pattern.
10
+ #
11
+ from io import StringIO
12
+ from antlr4.tree.ParseTreePattern import ParseTreePattern
13
+ from antlr4.tree.Tree import ParseTree
14
+
15
+
16
+ class ParseTreeMatch(object):
17
+ __slots__ = ('tree', 'pattern', 'labels', 'mismatchedNode')
18
+ #
19
+ # Constructs a new instance of {@link ParseTreeMatch} from the specified
20
+ # parse tree and pattern.
21
+ #
22
+ # @param tree The parse tree to match against the pattern.
23
+ # @param pattern The parse tree pattern.
24
+ # @param labels A mapping from label names to collections of
25
+ # {@link ParseTree} objects located by the tree pattern matching process.
26
+ # @param mismatchedNode The first node which failed to match the tree
27
+ # pattern during the matching process.
28
+ #
29
+ # @exception IllegalArgumentException if {@code tree} is {@code null}
30
+ # @exception IllegalArgumentException if {@code pattern} is {@code null}
31
+ # @exception IllegalArgumentException if {@code labels} is {@code null}
32
+ #
33
+ def __init__(self, tree:ParseTree, pattern:ParseTreePattern, labels:dict, mismatchedNode:ParseTree):
34
+ if tree is None:
35
+ raise Exception("tree cannot be null")
36
+ if pattern is None:
37
+ raise Exception("pattern cannot be null")
38
+ if labels is None:
39
+ raise Exception("labels cannot be null")
40
+ self.tree = tree
41
+ self.pattern = pattern
42
+ self.labels = labels
43
+ self.mismatchedNode = mismatchedNode
44
+
45
+ #
46
+ # Get the last node associated with a specific {@code label}.
47
+ #
48
+ # <p>For example, for pattern {@code <id:ID>}, {@code get("id")} returns the
49
+ # node matched for that {@code ID}. If more than one node
50
+ # matched the specified label, only the last is returned. If there is
51
+ # no node associated with the label, this returns {@code null}.</p>
52
+ #
53
+ # <p>Pattern tags like {@code <ID>} and {@code <expr>} without labels are
54
+ # considered to be labeled with {@code ID} and {@code expr}, respectively.</p>
55
+ #
56
+ # @param label The label to check.
57
+ #
58
+ # @return The last {@link ParseTree} to match a tag with the specified
59
+ # label, or {@code null} if no parse tree matched a tag with the label.
60
+ #
61
+ def get(self, label:str):
62
+ parseTrees = self.labels.get(label, None)
63
+ if parseTrees is None or len(parseTrees)==0:
64
+ return None
65
+ else:
66
+ return parseTrees[len(parseTrees)-1]
67
+
68
+ #
69
+ # Return all nodes matching a rule or token tag with the specified label.
70
+ #
71
+ # <p>If the {@code label} is the name of a parser rule or token in the
72
+ # grammar, the resulting list will contain both the parse trees matching
73
+ # rule or tags explicitly labeled with the label and the complete set of
74
+ # parse trees matching the labeled and unlabeled tags in the pattern for
75
+ # the parser rule or token. For example, if {@code label} is {@code "foo"},
76
+ # the result will contain <em>all</em> of the following.</p>
77
+ #
78
+ # <ul>
79
+ # <li>Parse tree nodes matching tags of the form {@code <foo:anyRuleName>} and
80
+ # {@code <foo:AnyTokenName>}.</li>
81
+ # <li>Parse tree nodes matching tags of the form {@code <anyLabel:foo>}.</li>
82
+ # <li>Parse tree nodes matching tags of the form {@code <foo>}.</li>
83
+ # </ul>
84
+ #
85
+ # @param label The label.
86
+ #
87
+ # @return A collection of all {@link ParseTree} nodes matching tags with
88
+ # the specified {@code label}. If no nodes matched the label, an empty list
89
+ # is returned.
90
+ #
91
+ def getAll(self, label:str):
92
+ nodes = self.labels.get(label, None)
93
+ if nodes is None:
94
+ return list()
95
+ else:
96
+ return nodes
97
+
98
+
99
+ #
100
+ # Gets a value indicating whether the match operation succeeded.
101
+ #
102
+ # @return {@code true} if the match operation succeeded; otherwise,
103
+ # {@code false}.
104
+ #
105
+ def succeeded(self):
106
+ return self.mismatchedNode is None
107
+
108
+ #
109
+ # {@inheritDoc}
110
+ #
111
+ def __str__(self):
112
+ with StringIO() as buf:
113
+ buf.write("Match ")
114
+ buf.write("succeeded" if self.succeeded() else "failed")
115
+ buf.write("; found ")
116
+ buf.write(str(len(self.labels)))
117
+ buf.write(" labels")
118
+ return buf.getvalue()
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/ParseTreePattern.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ #
8
+ # A pattern like {@code <ID> = <expr>;} converted to a {@link ParseTree} by
9
+ # {@link ParseTreePatternMatcher#compile(String, int)}.
10
+ #
11
+ from antlr4.tree.ParseTreePatternMatcher import ParseTreePatternMatcher
12
+ from antlr4.tree.Tree import ParseTree
13
+ from antlr4.xpath.XPath import XPath
14
+
15
+
16
+ class ParseTreePattern(object):
17
+ __slots__ = ('matcher', 'patternRuleIndex', 'pattern', 'patternTree')
18
+
19
+ # Construct a new instance of the {@link ParseTreePattern} class.
20
+ #
21
+ # @param matcher The {@link ParseTreePatternMatcher} which created this
22
+ # tree pattern.
23
+ # @param pattern The tree pattern in concrete syntax form.
24
+ # @param patternRuleIndex The parser rule which serves as the root of the
25
+ # tree pattern.
26
+ # @param patternTree The tree pattern in {@link ParseTree} form.
27
+ #
28
+ def __init__(self, matcher:ParseTreePatternMatcher, pattern:str, patternRuleIndex:int , patternTree:ParseTree):
29
+ self.matcher = matcher
30
+ self.patternRuleIndex = patternRuleIndex
31
+ self.pattern = pattern
32
+ self.patternTree = patternTree
33
+
34
+ #
35
+ # Match a specific parse tree against this tree pattern.
36
+ #
37
+ # @param tree The parse tree to match against this tree pattern.
38
+ # @return A {@link ParseTreeMatch} object describing the result of the
39
+ # match operation. The {@link ParseTreeMatch#succeeded()} method can be
40
+ # used to determine whether or not the match was successful.
41
+ #
42
+ def match(self, tree:ParseTree):
43
+ return self.matcher.match(tree, self)
44
+
45
+ #
46
+ # Determine whether or not a parse tree matches this tree pattern.
47
+ #
48
+ # @param tree The parse tree to match against this tree pattern.
49
+ # @return {@code true} if {@code tree} is a match for the current tree
50
+ # pattern; otherwise, {@code false}.
51
+ #
52
+ def matches(self, tree:ParseTree):
53
+ return self.matcher.match(tree, self).succeeded()
54
+
55
+ # Find all nodes using XPath and then try to match those subtrees against
56
+ # this tree pattern.
57
+ #
58
+ # @param tree The {@link ParseTree} to match against this pattern.
59
+ # @param xpath An expression matching the nodes
60
+ #
61
+ # @return A collection of {@link ParseTreeMatch} objects describing the
62
+ # successful matches. Unsuccessful matches are omitted from the result,
63
+ # regardless of the reason for the failure.
64
+ #
65
+ def findAll(self, tree:ParseTree, xpath:str):
66
+ subtrees = XPath.findAll(tree, xpath, self.matcher.parser)
67
+ matches = list()
68
+ for t in subtrees:
69
+ match = self.match(t)
70
+ if match.succeeded():
71
+ matches.append(match)
72
+ return matches
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/ParseTreePatternMatcher.py ADDED
@@ -0,0 +1,374 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ #
8
+ # A tree pattern matching mechanism for ANTLR {@link ParseTree}s.
9
+ #
10
+ # <p>Patterns are strings of source input text with special tags representing
11
+ # token or rule references such as:</p>
12
+ #
13
+ # <p>{@code <ID> = <expr>;}</p>
14
+ #
15
+ # <p>Given a pattern start rule such as {@code statement}, this object constructs
16
+ # a {@link ParseTree} with placeholders for the {@code ID} and {@code expr}
17
+ # subtree. Then the {@link #match} routines can compare an actual
18
+ # {@link ParseTree} from a parse with this pattern. Tag {@code <ID>} matches
19
+ # any {@code ID} token and tag {@code <expr>} references the result of the
20
+ # {@code expr} rule (generally an instance of {@code ExprContext}.</p>
21
+ #
22
+ # <p>Pattern {@code x = 0;} is a similar pattern that matches the same pattern
23
+ # except that it requires the identifier to be {@code x} and the expression to
24
+ # be {@code 0}.</p>
25
+ #
26
+ # <p>The {@link #matches} routines return {@code true} or {@code false} based
27
+ # upon a match for the tree rooted at the parameter sent in. The
28
+ # {@link #match} routines return a {@link ParseTreeMatch} object that
29
+ # contains the parse tree, the parse tree pattern, and a map from tag name to
30
+ # matched nodes (more below). A subtree that fails to match, returns with
31
+ # {@link ParseTreeMatch#mismatchedNode} set to the first tree node that did not
32
+ # match.</p>
33
+ #
34
+ # <p>For efficiency, you can compile a tree pattern in string form to a
35
+ # {@link ParseTreePattern} object.</p>
36
+ #
37
+ # <p>See {@code TestParseTreeMatcher} for lots of examples.
38
+ # {@link ParseTreePattern} has two static helper methods:
39
+ # {@link ParseTreePattern#findAll} and {@link ParseTreePattern#match} that
40
+ # are easy to use but not super efficient because they create new
41
+ # {@link ParseTreePatternMatcher} objects each time and have to compile the
42
+ # pattern in string form before using it.</p>
43
+ #
44
+ # <p>The lexer and parser that you pass into the {@link ParseTreePatternMatcher}
45
+ # constructor are used to parse the pattern in string form. The lexer converts
46
+ # the {@code <ID> = <expr>;} into a sequence of four tokens (assuming lexer
47
+ # throws out whitespace or puts it on a hidden channel). Be aware that the
48
+ # input stream is reset for the lexer (but not the parser; a
49
+ # {@link ParserInterpreter} is created to parse the input.). Any user-defined
50
+ # fields you have put into the lexer might get changed when this mechanism asks
51
+ # it to scan the pattern string.</p>
52
+ #
53
+ # <p>Normally a parser does not accept token {@code <expr>} as a valid
54
+ # {@code expr} but, from the parser passed in, we create a special version of
55
+ # the underlying grammar representation (an {@link ATN}) that allows imaginary
56
+ # tokens representing rules ({@code <expr>}) to match entire rules. We call
57
+ # these <em>bypass alternatives</em>.</p>
58
+ #
59
+ # <p>Delimiters are {@code <} and {@code >}, with {@code \} as the escape string
60
+ # by default, but you can set them to whatever you want using
61
+ # {@link #setDelimiters}. You must escape both start and stop strings
62
+ # {@code \<} and {@code \>}.</p>
63
+ #
64
+ from antlr4.CommonTokenStream import CommonTokenStream
65
+ from antlr4.InputStream import InputStream
66
+ from antlr4.ParserRuleContext import ParserRuleContext
67
+ from antlr4.Lexer import Lexer
68
+ from antlr4.ListTokenSource import ListTokenSource
69
+ from antlr4.Token import Token
70
+ from antlr4.error.ErrorStrategy import BailErrorStrategy
71
+ from antlr4.error.Errors import RecognitionException, ParseCancellationException
72
+ from antlr4.tree.Chunk import TagChunk, TextChunk
73
+ from antlr4.tree.RuleTagToken import RuleTagToken
74
+ from antlr4.tree.TokenTagToken import TokenTagToken
75
+ from antlr4.tree.Tree import ParseTree, TerminalNode, RuleNode
76
+
77
+ # need forward declaration
78
+ Parser = None
79
+ ParseTreePattern = None
80
+
81
+ class CannotInvokeStartRule(Exception):
82
+
83
+ def __init__(self, e:Exception):
84
+ super().__init__(e)
85
+
86
+ class StartRuleDoesNotConsumeFullPattern(Exception):
87
+
88
+ pass
89
+
90
+
91
+ class ParseTreePatternMatcher(object):
92
+ __slots__ = ('lexer', 'parser', 'start', 'stop', 'escape')
93
+
94
+ # Constructs a {@link ParseTreePatternMatcher} or from a {@link Lexer} and
95
+ # {@link Parser} object. The lexer input stream is altered for tokenizing
96
+ # the tree patterns. The parser is used as a convenient mechanism to get
97
+ # the grammar name, plus token, rule names.
98
+ def __init__(self, lexer:Lexer, parser:Parser):
99
+ self.lexer = lexer
100
+ self.parser = parser
101
+ self.start = "<"
102
+ self.stop = ">"
103
+ self.escape = "\\" # e.g., \< and \> must escape BOTH!
104
+
105
+ # Set the delimiters used for marking rule and token tags within concrete
106
+ # syntax used by the tree pattern parser.
107
+ #
108
+ # @param start The start delimiter.
109
+ # @param stop The stop delimiter.
110
+ # @param escapeLeft The escape sequence to use for escaping a start or stop delimiter.
111
+ #
112
+ # @exception IllegalArgumentException if {@code start} is {@code null} or empty.
113
+ # @exception IllegalArgumentException if {@code stop} is {@code null} or empty.
114
+ #
115
+ def setDelimiters(self, start:str, stop:str, escapeLeft:str):
116
+ if start is None or len(start)==0:
117
+ raise Exception("start cannot be null or empty")
118
+ if stop is None or len(stop)==0:
119
+ raise Exception("stop cannot be null or empty")
120
+ self.start = start
121
+ self.stop = stop
122
+ self.escape = escapeLeft
123
+
124
+ # Does {@code pattern} matched as rule {@code patternRuleIndex} match {@code tree}?#
125
+ def matchesRuleIndex(self, tree:ParseTree, pattern:str, patternRuleIndex:int):
126
+ p = self.compileTreePattern(pattern, patternRuleIndex)
127
+ return self.matches(tree, p)
128
+
129
+ # Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a
130
+ # compiled pattern instead of a string representation of a tree pattern.
131
+ #
132
+ def matchesPattern(self, tree:ParseTree, pattern:ParseTreePattern):
133
+ mismatchedNode = self.matchImpl(tree, pattern.patternTree, dict())
134
+ return mismatchedNode is None
135
+
136
+ #
137
+ # Compare {@code pattern} matched as rule {@code patternRuleIndex} against
138
+ # {@code tree} and return a {@link ParseTreeMatch} object that contains the
139
+ # matched elements, or the node at which the match failed.
140
+ #
141
+ def matchRuleIndex(self, tree:ParseTree, pattern:str, patternRuleIndex:int):
142
+ p = self.compileTreePattern(pattern, patternRuleIndex)
143
+ return self.matchPattern(tree, p)
144
+
145
+ #
146
+ # Compare {@code pattern} matched against {@code tree} and return a
147
+ # {@link ParseTreeMatch} object that contains the matched elements, or the
148
+ # node at which the match failed. Pass in a compiled pattern instead of a
149
+ # string representation of a tree pattern.
150
+ #
151
+ def matchPattern(self, tree:ParseTree, pattern:ParseTreePattern):
152
+ labels = dict()
153
+ mismatchedNode = self.matchImpl(tree, pattern.patternTree, labels)
154
+ from antlr4.tree.ParseTreeMatch import ParseTreeMatch
155
+ return ParseTreeMatch(tree, pattern, labels, mismatchedNode)
156
+
157
+ #
158
+ # For repeated use of a tree pattern, compile it to a
159
+ # {@link ParseTreePattern} using this method.
160
+ #
161
+ def compileTreePattern(self, pattern:str, patternRuleIndex:int):
162
+ tokenList = self.tokenize(pattern)
163
+ tokenSrc = ListTokenSource(tokenList)
164
+ tokens = CommonTokenStream(tokenSrc)
165
+ from antlr4.ParserInterpreter import ParserInterpreter
166
+ parserInterp = ParserInterpreter(self.parser.grammarFileName, self.parser.tokenNames,
167
+ self.parser.ruleNames, self.parser.getATNWithBypassAlts(),tokens)
168
+ tree = None
169
+ try:
170
+ parserInterp.setErrorHandler(BailErrorStrategy())
171
+ tree = parserInterp.parse(patternRuleIndex)
172
+ except ParseCancellationException as e:
173
+ raise e.cause
174
+ except RecognitionException as e:
175
+ raise e
176
+ except Exception as e:
177
+ raise CannotInvokeStartRule(e)
178
+
179
+ # Make sure tree pattern compilation checks for a complete parse
180
+ if tokens.LA(1)!=Token.EOF:
181
+ raise StartRuleDoesNotConsumeFullPattern()
182
+
183
+ from antlr4.tree.ParseTreePattern import ParseTreePattern
184
+ return ParseTreePattern(self, pattern, patternRuleIndex, tree)
185
+
186
+ #
187
+ # Recursively walk {@code tree} against {@code patternTree}, filling
188
+ # {@code match.}{@link ParseTreeMatch#labels labels}.
189
+ #
190
+ # @return the first node encountered in {@code tree} which does not match
191
+ # a corresponding node in {@code patternTree}, or {@code null} if the match
192
+ # was successful. The specific node returned depends on the matching
193
+ # algorithm used by the implementation, and may be overridden.
194
+ #
195
+ def matchImpl(self, tree:ParseTree, patternTree:ParseTree, labels:dict):
196
+ if tree is None:
197
+ raise Exception("tree cannot be null")
198
+ if patternTree is None:
199
+ raise Exception("patternTree cannot be null")
200
+
201
+ # x and <ID>, x and y, or x and x; or could be mismatched types
202
+ if isinstance(tree, TerminalNode) and isinstance(patternTree, TerminalNode ):
203
+ mismatchedNode = None
204
+ # both are tokens and they have same type
205
+ if tree.symbol.type == patternTree.symbol.type:
206
+ if isinstance( patternTree.symbol, TokenTagToken ): # x and <ID>
207
+ tokenTagToken = patternTree.symbol
208
+ # track label->list-of-nodes for both token name and label (if any)
209
+ self.map(labels, tokenTagToken.tokenName, tree)
210
+ if tokenTagToken.label is not None:
211
+ self.map(labels, tokenTagToken.label, tree)
212
+ elif tree.getText()==patternTree.getText():
213
+ # x and x
214
+ pass
215
+ else:
216
+ # x and y
217
+ if mismatchedNode is None:
218
+ mismatchedNode = tree
219
+ else:
220
+ if mismatchedNode is None:
221
+ mismatchedNode = tree
222
+
223
+ return mismatchedNode
224
+
225
+ if isinstance(tree, ParserRuleContext) and isinstance(patternTree, ParserRuleContext):
226
+ mismatchedNode = None
227
+ # (expr ...) and <expr>
228
+ ruleTagToken = self.getRuleTagToken(patternTree)
229
+ if ruleTagToken is not None:
230
+ m = None
231
+ if tree.ruleContext.ruleIndex == patternTree.ruleContext.ruleIndex:
232
+ # track label->list-of-nodes for both rule name and label (if any)
233
+ self.map(labels, ruleTagToken.ruleName, tree)
234
+ if ruleTagToken.label is not None:
235
+ self.map(labels, ruleTagToken.label, tree)
236
+ else:
237
+ if mismatchedNode is None:
238
+ mismatchedNode = tree
239
+
240
+ return mismatchedNode
241
+
242
+ # (expr ...) and (expr ...)
243
+ if tree.getChildCount()!=patternTree.getChildCount():
244
+ if mismatchedNode is None:
245
+ mismatchedNode = tree
246
+ return mismatchedNode
247
+
248
+ n = tree.getChildCount()
249
+ for i in range(0, n):
250
+ childMatch = self.matchImpl(tree.getChild(i), patternTree.getChild(i), labels)
251
+ if childMatch is not None:
252
+ return childMatch
253
+
254
+ return mismatchedNode
255
+
256
+ # if nodes aren't both tokens or both rule nodes, can't match
257
+ return tree
258
+
259
+ def map(self, labels, label, tree):
260
+ v = labels.get(label, None)
261
+ if v is None:
262
+ v = list()
263
+ labels[label] = v
264
+ v.append(tree)
265
+
266
+ # Is {@code t} {@code (expr <expr>)} subtree?#
267
+ def getRuleTagToken(self, tree:ParseTree):
268
+ if isinstance( tree, RuleNode ):
269
+ if tree.getChildCount()==1 and isinstance(tree.getChild(0), TerminalNode ):
270
+ c = tree.getChild(0)
271
+ if isinstance( c.symbol, RuleTagToken ):
272
+ return c.symbol
273
+ return None
274
+
275
+ def tokenize(self, pattern:str):
276
+ # split pattern into chunks: sea (raw input) and islands (<ID>, <expr>)
277
+ chunks = self.split(pattern)
278
+
279
+ # create token stream from text and tags
280
+ tokens = list()
281
+ for chunk in chunks:
282
+ if isinstance( chunk, TagChunk ):
283
+ # add special rule token or conjure up new token from name
284
+ if chunk.tag[0].isupper():
285
+ ttype = self.parser.getTokenType(chunk.tag)
286
+ if ttype==Token.INVALID_TYPE:
287
+ raise Exception("Unknown token " + str(chunk.tag) + " in pattern: " + pattern)
288
+ tokens.append(TokenTagToken(chunk.tag, ttype, chunk.label))
289
+ elif chunk.tag[0].islower():
290
+ ruleIndex = self.parser.getRuleIndex(chunk.tag)
291
+ if ruleIndex==-1:
292
+ raise Exception("Unknown rule " + str(chunk.tag) + " in pattern: " + pattern)
293
+ ruleImaginaryTokenType = self.parser.getATNWithBypassAlts().ruleToTokenType[ruleIndex]
294
+ tokens.append(RuleTagToken(chunk.tag, ruleImaginaryTokenType, chunk.label))
295
+ else:
296
+ raise Exception("invalid tag: " + str(chunk.tag) + " in pattern: " + pattern)
297
+ else:
298
+ self.lexer.setInputStream(InputStream(chunk.text))
299
+ t = self.lexer.nextToken()
300
+ while t.type!=Token.EOF:
301
+ tokens.append(t)
302
+ t = self.lexer.nextToken()
303
+ return tokens
304
+
305
+ # Split {@code <ID> = <e:expr> ;} into 4 chunks for tokenizing by {@link #tokenize}.#
306
+ def split(self, pattern:str):
307
+ p = 0
308
+ n = len(pattern)
309
+ chunks = list()
310
+ # find all start and stop indexes first, then collect
311
+ starts = list()
312
+ stops = list()
313
+ while p < n :
314
+ if p == pattern.find(self.escape + self.start, p):
315
+ p += len(self.escape) + len(self.start)
316
+ elif p == pattern.find(self.escape + self.stop, p):
317
+ p += len(self.escape) + len(self.stop)
318
+ elif p == pattern.find(self.start, p):
319
+ starts.append(p)
320
+ p += len(self.start)
321
+ elif p == pattern.find(self.stop, p):
322
+ stops.append(p)
323
+ p += len(self.stop)
324
+ else:
325
+ p += 1
326
+
327
+ nt = len(starts)
328
+
329
+ if nt > len(stops):
330
+ raise Exception("unterminated tag in pattern: " + pattern)
331
+ if nt < len(stops):
332
+ raise Exception("missing start tag in pattern: " + pattern)
333
+
334
+ for i in range(0, nt):
335
+ if starts[i] >= stops[i]:
336
+ raise Exception("tag delimiters out of order in pattern: " + pattern)
337
+
338
+ # collect into chunks now
339
+ if nt==0:
340
+ chunks.append(TextChunk(pattern))
341
+
342
+ if nt>0 and starts[0]>0: # copy text up to first tag into chunks
343
+ text = pattern[0:starts[0]]
344
+ chunks.add(TextChunk(text))
345
+
346
+ for i in range(0, nt):
347
+ # copy inside of <tag>
348
+ tag = pattern[starts[i] + len(self.start) : stops[i]]
349
+ ruleOrToken = tag
350
+ label = None
351
+ colon = tag.find(':')
352
+ if colon >= 0:
353
+ label = tag[0:colon]
354
+ ruleOrToken = tag[colon+1 : len(tag)]
355
+ chunks.append(TagChunk(label, ruleOrToken))
356
+ if i+1 < len(starts):
357
+ # copy from end of <tag> to start of next
358
+ text = pattern[stops[i] + len(self.stop) : starts[i + 1]]
359
+ chunks.append(TextChunk(text))
360
+
361
+ if nt > 0 :
362
+ afterLastTag = stops[nt - 1] + len(self.stop)
363
+ if afterLastTag < n : # copy text from end of last tag to end
364
+ text = pattern[afterLastTag : n]
365
+ chunks.append(TextChunk(text))
366
+
367
+ # strip out the escape sequences from text chunks but not tags
368
+ for i in range(0, len(chunks)):
369
+ c = chunks[i]
370
+ if isinstance( c, TextChunk ):
371
+ unescaped = c.text.replace(self.escape, "")
372
+ if len(unescaped) < len(c.text):
373
+ chunks[i] = TextChunk(unescaped)
374
+ return chunks
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/RuleTagToken.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ #
8
+ # A {@link Token} object representing an entire subtree matched by a parser
9
+ # rule; e.g., {@code <expr>}. These tokens are created for {@link TagChunk}
10
+ # chunks where the tag corresponds to a parser rule.
11
+ #
12
+ from antlr4.Token import Token
13
+
14
+
15
+ class RuleTagToken(Token):
16
+ __slots__ = ('label', 'ruleName')
17
+ #
18
+ # Constructs a new instance of {@link RuleTagToken} with the specified rule
19
+ # name, bypass token type, and label.
20
+ #
21
+ # @param ruleName The name of the parser rule this rule tag matches.
22
+ # @param bypassTokenType The bypass token type assigned to the parser rule.
23
+ # @param label The label associated with the rule tag, or {@code null} if
24
+ # the rule tag is unlabeled.
25
+ #
26
+ # @exception IllegalArgumentException if {@code ruleName} is {@code null}
27
+ # or empty.
28
+
29
+ def __init__(self, ruleName:str, bypassTokenType:int, label:str=None):
30
+ if ruleName is None or len(ruleName)==0:
31
+ raise Exception("ruleName cannot be null or empty.")
32
+ self.source = None
33
+ self.type = bypassTokenType # token type of the token
34
+ self.channel = Token.DEFAULT_CHANNEL # The parser ignores everything not on DEFAULT_CHANNEL
35
+ self.start = -1 # optional; return -1 if not implemented.
36
+ self.stop = -1 # optional; return -1 if not implemented.
37
+ self.tokenIndex = -1 # from 0..n-1 of the token object in the input stream
38
+ self.line = 0 # line=1..n of the 1st character
39
+ self.column = -1 # beginning of the line at which it occurs, 0..n-1
40
+ self.label = label
41
+ self._text = self.getText() # text of the token.
42
+
43
+ self.ruleName = ruleName
44
+
45
+
46
+ def getText(self):
47
+ if self.label is None:
48
+ return "<" + self.ruleName + ">"
49
+ else:
50
+ return "<" + self.label + ":" + self.ruleName + ">"
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/TokenTagToken.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ #
8
+ # A {@link Token} object representing a token of a particular type; e.g.,
9
+ # {@code <ID>}. These tokens are created for {@link TagChunk} chunks where the
10
+ # tag corresponds to a lexer rule or token type.
11
+ #
12
+ from antlr4.Token import CommonToken
13
+
14
+
15
+ class TokenTagToken(CommonToken):
16
+ __slots__ = ('tokenName', 'label')
17
+ # Constructs a new instance of {@link TokenTagToken} with the specified
18
+ # token name, type, and label.
19
+ #
20
+ # @param tokenName The token name.
21
+ # @param type The token type.
22
+ # @param label The label associated with the token tag, or {@code null} if
23
+ # the token tag is unlabeled.
24
+ #
25
+ def __init__(self, tokenName:str, type:int, label:str=None):
26
+ super().__init__(type=type)
27
+ self.tokenName = tokenName
28
+ self.label = label
29
+ self._text = self.getText()
30
+
31
+ #
32
+ # {@inheritDoc}
33
+ #
34
+ # <p>The implementation for {@link TokenTagToken} returns the token tag
35
+ # formatted with {@code <} and {@code >} delimiters.</p>
36
+ #
37
+ def getText(self):
38
+ if self.label is None:
39
+ return "<" + self.tokenName + ">"
40
+ else:
41
+ return "<" + self.label + ":" + self.tokenName + ">"
42
+
43
+ # <p>The implementation for {@link TokenTagToken} returns a string of the form
44
+ # {@code tokenName:type}.</p>
45
+ #
46
+ def __str__(self):
47
+ return self.tokenName + ":" + str(self.type)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/Tree.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2
+ # Use of this file is governed by the BSD 3-clause license that
3
+ # can be found in the LICENSE.txt file in the project root.
4
+ #/
5
+
6
+
7
+ # The basic notion of a tree has a parent, a payload, and a list of children.
8
+ # It is the most abstract interface for all the trees used by ANTLR.
9
+ #/
10
+ from antlr4.Token import Token
11
+
12
+ INVALID_INTERVAL = (-1, -2)
13
+
14
+ class Tree(object):
15
+ pass
16
+
17
+ class SyntaxTree(Tree):
18
+ pass
19
+
20
+ class ParseTree(SyntaxTree):
21
+ pass
22
+
23
+ class RuleNode(ParseTree):
24
+ pass
25
+
26
+ class TerminalNode(ParseTree):
27
+ pass
28
+
29
+ class ErrorNode(TerminalNode):
30
+ pass
31
+
32
+ class ParseTreeVisitor(object):
33
+ def visit(self, tree):
34
+ return tree.accept(self)
35
+
36
+ def visitChildren(self, node):
37
+ result = self.defaultResult()
38
+ n = node.getChildCount()
39
+ for i in range(n):
40
+ if not self.shouldVisitNextChild(node, result):
41
+ return result
42
+
43
+ c = node.getChild(i)
44
+ childResult = c.accept(self)
45
+ result = self.aggregateResult(result, childResult)
46
+
47
+ return result
48
+
49
+ def visitTerminal(self, node):
50
+ return self.defaultResult()
51
+
52
+ def visitErrorNode(self, node):
53
+ return self.defaultResult()
54
+
55
+ def defaultResult(self):
56
+ return None
57
+
58
+ def aggregateResult(self, aggregate, nextResult):
59
+ return nextResult
60
+
61
+ def shouldVisitNextChild(self, node, currentResult):
62
+ return True
63
+
64
+ ParserRuleContext = None
65
+
66
+ class ParseTreeListener(object):
67
+
68
+ def visitTerminal(self, node:TerminalNode):
69
+ pass
70
+
71
+ def visitErrorNode(self, node:ErrorNode):
72
+ pass
73
+
74
+ def enterEveryRule(self, ctx:ParserRuleContext):
75
+ pass
76
+
77
+ def exitEveryRule(self, ctx:ParserRuleContext):
78
+ pass
79
+
80
+ del ParserRuleContext
81
+
82
+ class TerminalNodeImpl(TerminalNode):
83
+ __slots__ = ('parentCtx', 'symbol')
84
+
85
+ def __init__(self, symbol:Token):
86
+ self.parentCtx = None
87
+ self.symbol = symbol
88
+ def __setattr__(self, key, value):
89
+ super().__setattr__(key, value)
90
+
91
+ def getChild(self, i:int):
92
+ return None
93
+
94
+ def getSymbol(self):
95
+ return self.symbol
96
+
97
+ def getParent(self):
98
+ return self.parentCtx
99
+
100
+ def getPayload(self):
101
+ return self.symbol
102
+
103
+ def getSourceInterval(self):
104
+ if self.symbol is None:
105
+ return INVALID_INTERVAL
106
+ tokenIndex = self.symbol.tokenIndex
107
+ return (tokenIndex, tokenIndex)
108
+
109
+ def getChildCount(self):
110
+ return 0
111
+
112
+ def accept(self, visitor:ParseTreeVisitor):
113
+ return visitor.visitTerminal(self)
114
+
115
+ def getText(self):
116
+ return self.symbol.text
117
+
118
+ def __str__(self):
119
+ if self.symbol.type == Token.EOF:
120
+ return "<EOF>"
121
+ else:
122
+ return self.symbol.text
123
+
124
+ # Represents a token that was consumed during resynchronization
125
+ # rather than during a valid match operation. For example,
126
+ # we will create this kind of a node during single token insertion
127
+ # and deletion as well as during "consume until error recovery set"
128
+ # upon no viable alternative exceptions.
129
+
130
+ class ErrorNodeImpl(TerminalNodeImpl,ErrorNode):
131
+
132
+ def __init__(self, token:Token):
133
+ super().__init__(token)
134
+
135
+ def accept(self, visitor:ParseTreeVisitor):
136
+ return visitor.visitErrorNode(self)
137
+
138
+
139
+ class ParseTreeWalker(object):
140
+
141
+ DEFAULT = None
142
+
143
+ def walk(self, listener:ParseTreeListener, t:ParseTree):
144
+ """
145
+ Performs a walk on the given parse tree starting at the root and going down recursively
146
+ with depth-first search. On each node, {@link ParseTreeWalker#enterRule} is called before
147
+ recursively walking down into child nodes, then
148
+ {@link ParseTreeWalker#exitRule} is called after the recursive call to wind up.
149
+ @param listener The listener used by the walker to process grammar rules
150
+ @param t The parse tree to be walked on
151
+ """
152
+ if isinstance(t, ErrorNode):
153
+ listener.visitErrorNode(t)
154
+ return
155
+ elif isinstance(t, TerminalNode):
156
+ listener.visitTerminal(t)
157
+ return
158
+ self.enterRule(listener, t)
159
+ for child in t.getChildren():
160
+ self.walk(listener, child)
161
+ self.exitRule(listener, t)
162
+
163
+ #
164
+ # The discovery of a rule node, involves sending two events: the generic
165
+ # {@link ParseTreeListener#enterEveryRule} and a
166
+ # {@link RuleContext}-specific event. First we trigger the generic and then
167
+ # the rule specific. We to them in reverse order upon finishing the node.
168
+ #
169
+ def enterRule(self, listener:ParseTreeListener, r:RuleNode):
170
+ """
171
+ Enters a grammar rule by first triggering the generic event {@link ParseTreeListener#enterEveryRule}
172
+ then by triggering the event specific to the given parse tree node
173
+ @param listener The listener responding to the trigger events
174
+ @param r The grammar rule containing the rule context
175
+ """
176
+ ctx = r.getRuleContext()
177
+ listener.enterEveryRule(ctx)
178
+ ctx.enterRule(listener)
179
+
180
+ def exitRule(self, listener:ParseTreeListener, r:RuleNode):
181
+ """
182
+ Exits a grammar rule by first triggering the event specific to the given parse tree node
183
+ then by triggering the generic event {@link ParseTreeListener#exitEveryRule}
184
+ @param listener The listener responding to the trigger events
185
+ @param r The grammar rule containing the rule context
186
+ """
187
+ ctx = r.getRuleContext()
188
+ ctx.exitRule(listener)
189
+ listener.exitEveryRule(ctx)
190
+
191
+ ParseTreeWalker.DEFAULT = ParseTreeWalker()
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/Trees.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+
8
+ # A set of utility routines useful for all kinds of ANTLR trees.#
9
+ from io import StringIO
10
+ from antlr4.Token import Token
11
+ from antlr4.Utils import escapeWhitespace
12
+ from antlr4.tree.Tree import RuleNode, ErrorNode, TerminalNode, Tree, ParseTree
13
+
14
+ # need forward declaration
15
+ Parser = None
16
+
17
+ class Trees(object):
18
+
19
+ # Print out a whole tree in LISP form. {@link #getNodeText} is used on the
20
+ # node payloads to get the text for the nodes. Detect
21
+ # parse trees and extract data appropriately.
22
+ @classmethod
23
+ def toStringTree(cls, t:Tree, ruleNames:list=None, recog:Parser=None):
24
+ if recog is not None:
25
+ ruleNames = recog.ruleNames
26
+ s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
27
+ if t.getChildCount()==0:
28
+ return s
29
+ with StringIO() as buf:
30
+ buf.write("(")
31
+ buf.write(s)
32
+ buf.write(' ')
33
+ for i in range(0, t.getChildCount()):
34
+ if i > 0:
35
+ buf.write(' ')
36
+ buf.write(cls.toStringTree(t.getChild(i), ruleNames))
37
+ buf.write(")")
38
+ return buf.getvalue()
39
+
40
+ @classmethod
41
+ def getNodeText(cls, t:Tree, ruleNames:list=None, recog:Parser=None):
42
+ if recog is not None:
43
+ ruleNames = recog.ruleNames
44
+ if ruleNames is not None:
45
+ if isinstance(t, RuleNode):
46
+ if t.getAltNumber()!=0: # should use ATN.INVALID_ALT_NUMBER but won't compile
47
+ return ruleNames[t.getRuleIndex()]+":"+str(t.getAltNumber())
48
+ return ruleNames[t.getRuleIndex()]
49
+ elif isinstance( t, ErrorNode):
50
+ return str(t)
51
+ elif isinstance(t, TerminalNode):
52
+ if t.symbol is not None:
53
+ return t.symbol.text
54
+ # no recog for rule names
55
+ payload = t.getPayload()
56
+ if isinstance(payload, Token ):
57
+ return payload.text
58
+ return str(t.getPayload())
59
+
60
+
61
+ # Return ordered list of all children of this node
62
+ @classmethod
63
+ def getChildren(cls, t:Tree):
64
+ return [ t.getChild(i) for i in range(0, t.getChildCount()) ]
65
+
66
+ # Return a list of all ancestors of this node. The first node of
67
+ # list is the root and the last is the parent of this node.
68
+ #
69
+ @classmethod
70
+ def getAncestors(cls, t:Tree):
71
+ ancestors = []
72
+ t = t.getParent()
73
+ while t is not None:
74
+ ancestors.insert(0, t) # insert at start
75
+ t = t.getParent()
76
+ return ancestors
77
+
78
+ @classmethod
79
+ def findAllTokenNodes(cls, t:ParseTree, ttype:int):
80
+ return cls.findAllNodes(t, ttype, True)
81
+
82
+ @classmethod
83
+ def findAllRuleNodes(cls, t:ParseTree, ruleIndex:int):
84
+ return cls.findAllNodes(t, ruleIndex, False)
85
+
86
+ @classmethod
87
+ def findAllNodes(cls, t:ParseTree, index:int, findTokens:bool):
88
+ nodes = []
89
+ cls._findAllNodes(t, index, findTokens, nodes)
90
+ return nodes
91
+
92
+ @classmethod
93
+ def _findAllNodes(cls, t:ParseTree, index:int, findTokens:bool, nodes:list):
94
+ from antlr4.ParserRuleContext import ParserRuleContext
95
+ # check this node (the root) first
96
+ if findTokens and isinstance(t, TerminalNode):
97
+ if t.symbol.type==index:
98
+ nodes.append(t)
99
+ elif not findTokens and isinstance(t, ParserRuleContext):
100
+ if t.ruleIndex == index:
101
+ nodes.append(t)
102
+ # check children
103
+ for i in range(0, t.getChildCount()):
104
+ cls._findAllNodes(t.getChild(i), index, findTokens, nodes)
105
+
106
+ @classmethod
107
+ def descendants(cls, t:ParseTree):
108
+ nodes = [t]
109
+ for i in range(0, t.getChildCount()):
110
+ nodes.extend(cls.descendants(t.getChild(i)))
111
+ return nodes
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/tree/__init__.py ADDED
File without changes
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/xpath/XPath.py ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
+ # Use of this file is governed by the BSD 3-clause license that
4
+ # can be found in the LICENSE.txt file in the project root.
5
+ #
6
+
7
+ #
8
+ # Represent a subset of XPath XML path syntax for use in identifying nodes in
9
+ # parse trees.
10
+ #
11
+ # <p>
12
+ # Split path into words and separators {@code /} and {@code //} via ANTLR
13
+ # itself then walk path elements from left to right. At each separator-word
14
+ # pair, find set of nodes. Next stage uses those as work list.</p>
15
+ #
16
+ # <p>
17
+ # The basic interface is
18
+ # {@link XPath#findAll ParseTree.findAll}{@code (tree, pathString, parser)}.
19
+ # But that is just shorthand for:</p>
20
+ #
21
+ # <pre>
22
+ # {@link XPath} p = new {@link XPath#XPath XPath}(parser, pathString);
23
+ # return p.{@link #evaluate evaluate}(tree);
24
+ # </pre>
25
+ #
26
+ # <p>
27
+ # See {@code org.antlr.v4.test.TestXPath} for descriptions. In short, this
28
+ # allows operators:</p>
29
+ #
30
+ # <dl>
31
+ # <dt>/</dt> <dd>root</dd>
32
+ # <dt>//</dt> <dd>anywhere</dd>
33
+ # <dt>!</dt> <dd>invert; this must appear directly after root or anywhere
34
+ # operator</dd>
35
+ # </dl>
36
+ #
37
+ # <p>
38
+ # and path elements:</p>
39
+ #
40
+ # <dl>
41
+ # <dt>ID</dt> <dd>token name</dd>
42
+ # <dt>'string'</dt> <dd>any string literal token from the grammar</dd>
43
+ # <dt>expr</dt> <dd>rule name</dd>
44
+ # <dt>*</dt> <dd>wildcard matching any node</dd>
45
+ # </dl>
46
+ #
47
+ # <p>
48
+ # Whitespace is not allowed.</p>
49
+ #
50
+ from antlr4 import CommonTokenStream, DFA, PredictionContextCache, Lexer, LexerATNSimulator, ParserRuleContext, TerminalNode
51
+ from antlr4.InputStream import InputStream
52
+ from antlr4.Parser import Parser
53
+ from antlr4.RuleContext import RuleContext
54
+ from antlr4.Token import Token
55
+ from antlr4.atn.ATNDeserializer import ATNDeserializer
56
+ from antlr4.error.ErrorListener import ErrorListener
57
+ from antlr4.error.Errors import LexerNoViableAltException
58
+ from antlr4.tree.Tree import ParseTree
59
+ from antlr4.tree.Trees import Trees
60
+ from io import StringIO
61
+
62
+
63
+ def serializedATN():
64
+ with StringIO() as buf:
65
+ buf.write("\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2\n")
66
+ buf.write("\64\b\1\4\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t")
67
+ buf.write("\7\4\b\t\b\4\t\t\t\3\2\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5")
68
+ buf.write("\3\6\3\6\7\6\37\n\6\f\6\16\6\"\13\6\3\6\3\6\3\7\3\7\5")
69
+ buf.write("\7(\n\7\3\b\3\b\3\t\3\t\7\t.\n\t\f\t\16\t\61\13\t\3\t")
70
+ buf.write("\3\t\3/\2\n\3\5\5\6\7\7\t\b\13\t\r\2\17\2\21\n\3\2\4\7")
71
+ buf.write("\2\62;aa\u00b9\u00b9\u0302\u0371\u2041\u2042\17\2C\\c")
72
+ buf.write("|\u00c2\u00d8\u00da\u00f8\u00fa\u0301\u0372\u037f\u0381")
73
+ buf.write("\u2001\u200e\u200f\u2072\u2191\u2c02\u2ff1\u3003\ud801")
74
+ buf.write("\uf902\ufdd1\ufdf2\uffff\64\2\3\3\2\2\2\2\5\3\2\2\2\2")
75
+ buf.write("\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\21\3\2\2\2\3\23")
76
+ buf.write("\3\2\2\2\5\26\3\2\2\2\7\30\3\2\2\2\t\32\3\2\2\2\13\34")
77
+ buf.write("\3\2\2\2\r\'\3\2\2\2\17)\3\2\2\2\21+\3\2\2\2\23\24\7\61")
78
+ buf.write("\2\2\24\25\7\61\2\2\25\4\3\2\2\2\26\27\7\61\2\2\27\6\3")
79
+ buf.write("\2\2\2\30\31\7,\2\2\31\b\3\2\2\2\32\33\7#\2\2\33\n\3\2")
80
+ buf.write("\2\2\34 \5\17\b\2\35\37\5\r\7\2\36\35\3\2\2\2\37\"\3\2")
81
+ buf.write("\2\2 \36\3\2\2\2 !\3\2\2\2!#\3\2\2\2\" \3\2\2\2#$\b\6")
82
+ buf.write("\2\2$\f\3\2\2\2%(\5\17\b\2&(\t\2\2\2\'%\3\2\2\2\'&\3\2")
83
+ buf.write("\2\2(\16\3\2\2\2)*\t\3\2\2*\20\3\2\2\2+/\7)\2\2,.\13\2")
84
+ buf.write("\2\2-,\3\2\2\2.\61\3\2\2\2/\60\3\2\2\2/-\3\2\2\2\60\62")
85
+ buf.write("\3\2\2\2\61/\3\2\2\2\62\63\7)\2\2\63\22\3\2\2\2\6\2 \'")
86
+ buf.write("/\3\3\6\2")
87
+ return buf.getvalue()
88
+
89
+
90
+ class XPathLexer(Lexer):
91
+
92
+ atn = ATNDeserializer().deserialize(serializedATN())
93
+
94
+ decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]
95
+
96
+
97
+ TOKEN_REF = 1
98
+ RULE_REF = 2
99
+ ANYWHERE = 3
100
+ ROOT = 4
101
+ WILDCARD = 5
102
+ BANG = 6
103
+ ID = 7
104
+ STRING = 8
105
+
106
+ modeNames = [ "DEFAULT_MODE" ]
107
+
108
+ literalNames = [ "<INVALID>",
109
+ "'//'", "'/'", "'*'", "'!'" ]
110
+
111
+ symbolicNames = [ "<INVALID>",
112
+ "TOKEN_REF", "RULE_REF", "ANYWHERE", "ROOT", "WILDCARD", "BANG",
113
+ "ID", "STRING" ]
114
+
115
+ ruleNames = [ "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar",
116
+ "NameStartChar", "STRING" ]
117
+
118
+ grammarFileName = "XPathLexer.g4"
119
+
120
+ def __init__(self, input=None):
121
+ super().__init__(input)
122
+ self.checkVersion("4.9.1")
123
+ self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
124
+ self._actions = None
125
+ self._predicates = None
126
+
127
+
128
+ def action(self, localctx:RuleContext, ruleIndex:int, actionIndex:int):
129
+ if self._actions is None:
130
+ actions = dict()
131
+ actions[4] = self.ID_action
132
+ self._actions = actions
133
+ _action = self._actions.get(ruleIndex, None)
134
+ if _action is not None:
135
+ _action(localctx, actionIndex)
136
+ else:
137
+ raise Exception("No registered action for: %d" % ruleIndex)
138
+
139
+ def ID_action(self, localctx:RuleContext , actionIndex:int):
140
+ if actionIndex == 0:
141
+ char = self.text[0]
142
+ if char.isupper():
143
+ self.type = XPathLexer.TOKEN_REF
144
+ else:
145
+ self.type = XPathLexer.RULE_REF
146
+
147
+ class XPath(object):
148
+
149
+ WILDCARD = "*" # word not operator/separator
150
+ NOT = "!" # word for invert operator
151
+
152
+ def __init__(self, parser:Parser, path:str):
153
+ self.parser = parser
154
+ self.path = path
155
+ self.elements = self.split(path)
156
+
157
+ def split(self, path:str):
158
+ input = InputStream(path)
159
+ lexer = XPathLexer(input)
160
+ def recover(self, e):
161
+ raise e
162
+ lexer.recover = recover
163
+ lexer.removeErrorListeners()
164
+ lexer.addErrorListener(ErrorListener()) # XPathErrorListener does no more
165
+ tokenStream = CommonTokenStream(lexer)
166
+ try:
167
+ tokenStream.fill()
168
+ except LexerNoViableAltException as e:
169
+ pos = lexer.column
170
+ msg = "Invalid tokens or characters at index %d in path '%s'" % (pos, path)
171
+ raise Exception(msg, e)
172
+
173
+ tokens = iter(tokenStream.tokens)
174
+ elements = list()
175
+ for el in tokens:
176
+ invert = False
177
+ anywhere = False
178
+ # Check for path separators, if none assume root
179
+ if el.type in [XPathLexer.ROOT, XPathLexer.ANYWHERE]:
180
+ anywhere = el.type == XPathLexer.ANYWHERE
181
+ next_el = next(tokens, None)
182
+ if not next_el:
183
+ raise Exception('Missing element after %s' % el.getText())
184
+ else:
185
+ el = next_el
186
+ # Check for bangs
187
+ if el.type == XPathLexer.BANG:
188
+ invert = True
189
+ next_el = next(tokens, None)
190
+ if not next_el:
191
+ raise Exception('Missing element after %s' % el.getText())
192
+ else:
193
+ el = next_el
194
+ # Add searched element
195
+ if el.type in [XPathLexer.TOKEN_REF, XPathLexer.RULE_REF, XPathLexer.WILDCARD, XPathLexer.STRING]:
196
+ element = self.getXPathElement(el, anywhere)
197
+ element.invert = invert
198
+ elements.append(element)
199
+ elif el.type==Token.EOF:
200
+ break
201
+ else:
202
+ raise Exception("Unknown path element %s" % lexer.symbolicNames[el.type])
203
+ return elements
204
+
205
+ #
206
+ # Convert word like {@code#} or {@code ID} or {@code expr} to a path
207
+ # element. {@code anywhere} is {@code true} if {@code //} precedes the
208
+ # word.
209
+ #
210
+ def getXPathElement(self, wordToken:Token, anywhere:bool):
211
+ if wordToken.type==Token.EOF:
212
+ raise Exception("Missing path element at end of path")
213
+
214
+ word = wordToken.text
215
+ if wordToken.type==XPathLexer.WILDCARD :
216
+ return XPathWildcardAnywhereElement() if anywhere else XPathWildcardElement()
217
+
218
+ elif wordToken.type in [XPathLexer.TOKEN_REF, XPathLexer.STRING]:
219
+ tsource = self.parser.getTokenStream().tokenSource
220
+
221
+ ttype = Token.INVALID_TYPE
222
+ if wordToken.type == XPathLexer.TOKEN_REF:
223
+ if word in tsource.ruleNames:
224
+ ttype = tsource.ruleNames.index(word) + 1
225
+ else:
226
+ if word in tsource.literalNames:
227
+ ttype = tsource.literalNames.index(word)
228
+
229
+ if ttype == Token.INVALID_TYPE:
230
+ raise Exception("%s at index %d isn't a valid token name" % (word, wordToken.tokenIndex))
231
+ return XPathTokenAnywhereElement(word, ttype) if anywhere else XPathTokenElement(word, ttype)
232
+
233
+ else:
234
+ ruleIndex = self.parser.ruleNames.index(word) if word in self.parser.ruleNames else -1
235
+
236
+ if ruleIndex == -1:
237
+ raise Exception("%s at index %d isn't a valid rule name" % (word, wordToken.tokenIndex))
238
+ return XPathRuleAnywhereElement(word, ruleIndex) if anywhere else XPathRuleElement(word, ruleIndex)
239
+
240
+
241
+ @staticmethod
242
+ def findAll(tree:ParseTree, xpath:str, parser:Parser):
243
+ p = XPath(parser, xpath)
244
+ return p.evaluate(tree)
245
+
246
+ #
247
+ # Return a list of all nodes starting at {@code t} as root that satisfy the
248
+ # path. The root {@code /} is relative to the node passed to
249
+ # {@link #evaluate}.
250
+ #
251
+ def evaluate(self, t:ParseTree):
252
+ dummyRoot = ParserRuleContext()
253
+ dummyRoot.children = [t] # don't set t's parent.
254
+
255
+ work = [dummyRoot]
256
+ for element in self.elements:
257
+ work_next = list()
258
+ for node in work:
259
+ if not isinstance(node, TerminalNode) and node.children:
260
+ # only try to match next element if it has children
261
+ # e.g., //func/*/stat might have a token node for which
262
+ # we can't go looking for stat nodes.
263
+ matching = element.evaluate(node)
264
+
265
+ # See issue antlr#370 - Prevents XPath from returning the
266
+ # same node multiple times
267
+ matching = filter(lambda m: m not in work_next, matching)
268
+
269
+ work_next.extend(matching)
270
+ work = work_next
271
+
272
+ return work
273
+
274
+
275
+ class XPathElement(object):
276
+
277
+ def __init__(self, nodeName:str):
278
+ self.nodeName = nodeName
279
+ self.invert = False
280
+
281
+ def __str__(self):
282
+ return type(self).__name__ + "[" + ("!" if self.invert else "") + self.nodeName + "]"
283
+
284
+
285
+
286
+ #
287
+ # Either {@code ID} at start of path or {@code ...//ID} in middle of path.
288
+ #
289
+ class XPathRuleAnywhereElement(XPathElement):
290
+
291
+ def __init__(self, ruleName:str, ruleIndex:int):
292
+ super().__init__(ruleName)
293
+ self.ruleIndex = ruleIndex
294
+
295
+ def evaluate(self, t:ParseTree):
296
+ # return all ParserRuleContext descendants of t that match ruleIndex (or do not match if inverted)
297
+ return filter(lambda c: isinstance(c, ParserRuleContext) and (self.invert ^ (c.getRuleIndex() == self.ruleIndex)), Trees.descendants(t))
298
+
299
+ class XPathRuleElement(XPathElement):
300
+
301
+ def __init__(self, ruleName:str, ruleIndex:int):
302
+ super().__init__(ruleName)
303
+ self.ruleIndex = ruleIndex
304
+
305
+ def evaluate(self, t:ParseTree):
306
+ # return all ParserRuleContext children of t that match ruleIndex (or do not match if inverted)
307
+ return filter(lambda c: isinstance(c, ParserRuleContext) and (self.invert ^ (c.getRuleIndex() == self.ruleIndex)), Trees.getChildren(t))
308
+
309
+ class XPathTokenAnywhereElement(XPathElement):
310
+
311
+ def __init__(self, ruleName:str, tokenType:int):
312
+ super().__init__(ruleName)
313
+ self.tokenType = tokenType
314
+
315
+ def evaluate(self, t:ParseTree):
316
+ # return all TerminalNode descendants of t that match tokenType (or do not match if inverted)
317
+ return filter(lambda c: isinstance(c, TerminalNode) and (self.invert ^ (c.symbol.type == self.tokenType)), Trees.descendants(t))
318
+
319
+ class XPathTokenElement(XPathElement):
320
+
321
+ def __init__(self, ruleName:str, tokenType:int):
322
+ super().__init__(ruleName)
323
+ self.tokenType = tokenType
324
+
325
+ def evaluate(self, t:ParseTree):
326
+ # return all TerminalNode children of t that match tokenType (or do not match if inverted)
327
+ return filter(lambda c: isinstance(c, TerminalNode) and (self.invert ^ (c.symbol.type == self.tokenType)), Trees.getChildren(t))
328
+
329
+
330
+ class XPathWildcardAnywhereElement(XPathElement):
331
+
332
+ def __init__(self):
333
+ super().__init__(XPath.WILDCARD)
334
+
335
+ def evaluate(self, t:ParseTree):
336
+ if self.invert:
337
+ return list() # !* is weird but valid (empty)
338
+ else:
339
+ return Trees.descendants(t)
340
+
341
+
342
+ class XPathWildcardElement(XPathElement):
343
+
344
+ def __init__(self):
345
+ super().__init__(XPath.WILDCARD)
346
+
347
+
348
+ def evaluate(self, t:ParseTree):
349
+ if self.invert:
350
+ return list() # !* is weird but valid (empty)
351
+ else:
352
+ return Trees.getChildren(t)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4/xpath/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __author__ = 'ericvergnaud'
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/INSTALLER ADDED
@@ -0,0 +1 @@
 
 
1
+ pip
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/METADATA ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.4
2
+ Name: antlr4-python3-runtime
3
+ Version: 4.9.3
4
+ Summary: ANTLR 4.9.3 runtime for Python 3.7
5
+ Home-page: http://www.antlr.org
6
+ Author: Eric Vergnaud, Terence Parr, Sam Harwell
7
+ Author-email: eric.vergnaud@wanadoo.fr
8
+ License: BSD
9
+ Requires-Dist: typing; python_version < "3.5"
10
+ Dynamic: author
11
+ Dynamic: author-email
12
+ Dynamic: home-page
13
+ Dynamic: license
14
+ Dynamic: requires-dist
15
+ Dynamic: summary
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/RECORD ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ../../../bin/pygrun,sha256=fyqneSOA38hfCV9dBVscsKTIt9XVKA4-Iy3FFEQsVVM,6182
2
+ antlr4/BufferedTokenStream.py,sha256=_BwmzOH1TO6yL2yC_ZaUzkghq8wzc0UPHfI3UpnZUwM,10780
3
+ antlr4/CommonTokenFactory.py,sha256=Tv16zg_pWD1Dv3IphsxFu8nwWdLeXYcqJ8CC5yHwjH8,2110
4
+ antlr4/CommonTokenStream.py,sha256=NNJHXwRg2_Zn46ZhJyDxZtvZzsPWhb6JjXa7BjM45eg,2770
5
+ antlr4/FileStream.py,sha256=-ZR_-jl_If9IIBYLINIwlQrlTSmu5k1VUKDc3ie7WR4,868
6
+ antlr4/InputStream.py,sha256=sggjE2jEGvSgQmxFvqeeuT3aOVgcH5tS7mMybW8wKS4,2334
7
+ antlr4/IntervalSet.py,sha256=Cd0WKhd_kYbiLYKkDNncgSM19GAuS7OaTOC4-5Yubs4,5965
8
+ antlr4/LL1Analyzer.py,sha256=oJBvO7_S8cAlb_D4qWNxd2IlK0qP4ka-oeoDxx16CZ4,7752
9
+ antlr4/Lexer.py,sha256=C72hqayfkympxb46AcSnhPD9kVZ0quWgboGxa6gcIcg,11542
10
+ antlr4/ListTokenSource.py,sha256=IffLMo7YQnD_CjKryrrgNWSk0q5QSYd7puZyyUk7vOk,5356
11
+ antlr4/Parser.py,sha256=F2Q25z0-__KHfa354KQhDu3ZOVzLFfag3s2ixJ4dl_o,22883
12
+ antlr4/ParserInterpreter.py,sha256=-QU9kn4x3WCQ-LSA99R231HoicTqakiHZ5KM72l-hIo,7206
13
+ antlr4/ParserRuleContext.py,sha256=wHAVdOxMAO5jkUqloTXVzn_xYnJhiHbvvuhZpth0ZF8,6762
14
+ antlr4/PredictionContext.py,sha256=cb4KI6EGpS7sRzJ8UvPEkxphINZuWhyiZ95752g3prI,22977
15
+ antlr4/Recognizer.py,sha256=vmKAtSjIgR9LQr5YzuK5OmPZWMJ3x69OuVZQ_FTzQHE,5383
16
+ antlr4/RuleContext.py,sha256=GiviRv2k_al1IBgdJOEEoD0ohJaVd-_h5T_CPG_Bsmg,8099
17
+ antlr4/StdinStream.py,sha256=MMSH4zN8T6i_nu-3_TlN-3E4nPM4b5KgK4GT6n_FUQA,303
18
+ antlr4/Token.py,sha256=OtWCab4Ut52X_nLLAA-8x4Zl6xaF6TEN-0033uaoaEo,5206
19
+ antlr4/TokenStreamRewriter.py,sha256=cuErQTrXwC_0kqVv3MsTWGZSm-E1Vy1yzA-3SOhKd_s,10324
20
+ antlr4/Utils.py,sha256=Oyg8CJCRL1TrF_QSB_LLlVdWOB4loVcKOgFNT-icO7c,931
21
+ antlr4/__init__.py,sha256=g8UGpflnlMWcAyLtihejzrgAP1Uo3b9GhwfI8QnZjtw,1125
22
+ antlr4/__pycache__/BufferedTokenStream.cpython-310.pyc,,
23
+ antlr4/__pycache__/CommonTokenFactory.cpython-310.pyc,,
24
+ antlr4/__pycache__/CommonTokenStream.cpython-310.pyc,,
25
+ antlr4/__pycache__/FileStream.cpython-310.pyc,,
26
+ antlr4/__pycache__/InputStream.cpython-310.pyc,,
27
+ antlr4/__pycache__/IntervalSet.cpython-310.pyc,,
28
+ antlr4/__pycache__/LL1Analyzer.cpython-310.pyc,,
29
+ antlr4/__pycache__/Lexer.cpython-310.pyc,,
30
+ antlr4/__pycache__/ListTokenSource.cpython-310.pyc,,
31
+ antlr4/__pycache__/Parser.cpython-310.pyc,,
32
+ antlr4/__pycache__/ParserInterpreter.cpython-310.pyc,,
33
+ antlr4/__pycache__/ParserRuleContext.cpython-310.pyc,,
34
+ antlr4/__pycache__/PredictionContext.cpython-310.pyc,,
35
+ antlr4/__pycache__/Recognizer.cpython-310.pyc,,
36
+ antlr4/__pycache__/RuleContext.cpython-310.pyc,,
37
+ antlr4/__pycache__/StdinStream.cpython-310.pyc,,
38
+ antlr4/__pycache__/Token.cpython-310.pyc,,
39
+ antlr4/__pycache__/TokenStreamRewriter.cpython-310.pyc,,
40
+ antlr4/__pycache__/Utils.cpython-310.pyc,,
41
+ antlr4/__pycache__/__init__.cpython-310.pyc,,
42
+ antlr4/atn/ATN.py,sha256=LYE8kT-D8FpUd5fpOtyOLqvXLFkUSa83TVFowhCWAiY,5789
43
+ antlr4/atn/ATNConfig.py,sha256=tNdIC6_GrxXllHBx3npAWyDh6KrohLZDV_XyPrydRMY,6565
44
+ antlr4/atn/ATNConfigSet.py,sha256=qRzVsBeMqk2txjG3DrGptwF6Vb2hHC5w3umkSL0GNJw,8312
45
+ antlr4/atn/ATNDeserializationOptions.py,sha256=lUV_bGW6mxj7t20esda5Yv-X9m-U_x1-0xaLifhXIPo,1010
46
+ antlr4/atn/ATNDeserializer.py,sha256=aYLDDtQ-wyo3gId6A-wD1E3QmpfrPZlXxj4_IDm-mUY,22252
47
+ antlr4/atn/ATNSimulator.py,sha256=mDc-G3GF3kSeqpfGDabUOLJ0WLVTqibxZlkvXQYmBRk,2298
48
+ antlr4/atn/ATNState.py,sha256=NbndISWUwFDF_vuBfbTiZZ8GPHoQa6UXdqbD-yjJE7c,7663
49
+ antlr4/atn/ATNType.py,sha256=xgv8AMVU7tc07U73_hRTm1AiZ7MvGhoaP5fTiOrrCGg,422
50
+ antlr4/atn/LexerATNSimulator.py,sha256=kYXRwUvHptSRU8T_K9pSrGlCk9YypWeHlAcjgry1VVo,25465
51
+ antlr4/atn/LexerAction.py,sha256=KUeJwKekBch0m1poSPskHIh-15dcKAG4lR7zlq98tzc,10014
52
+ antlr4/atn/LexerActionExecutor.py,sha256=7rlg17THcwLsuTmh7NsLrTbRH4DTrm8qIdW9_235CEc,6420
53
+ antlr4/atn/ParserATNSimulator.py,sha256=IKCzsDLcznROSVojU-daAygKr3svl0DmK5DhkUllASY,80365
54
+ antlr4/atn/PredictionMode.py,sha256=i8B7MULA7v-qbXeCY_xp6sgi21kHM6kybqIrG6rSrro,22486
55
+ antlr4/atn/SemanticContext.py,sha256=ds0TmM4qenb0LN-rl2Fp_N_xB959abN67I19EF6rs8o,10495
56
+ antlr4/atn/Transition.py,sha256=ZAsEFpa5I_n-zxD6U-DauM5_33jFK65x3PWu6-NW0RA,8762
57
+ antlr4/atn/__init__.py,sha256=gsnQdtTH8IUgCiVUpQfzhxx2pFRvksW76SjwIk3fYSk,28
58
+ antlr4/atn/__pycache__/ATN.cpython-310.pyc,,
59
+ antlr4/atn/__pycache__/ATNConfig.cpython-310.pyc,,
60
+ antlr4/atn/__pycache__/ATNConfigSet.cpython-310.pyc,,
61
+ antlr4/atn/__pycache__/ATNDeserializationOptions.cpython-310.pyc,,
62
+ antlr4/atn/__pycache__/ATNDeserializer.cpython-310.pyc,,
63
+ antlr4/atn/__pycache__/ATNSimulator.cpython-310.pyc,,
64
+ antlr4/atn/__pycache__/ATNState.cpython-310.pyc,,
65
+ antlr4/atn/__pycache__/ATNType.cpython-310.pyc,,
66
+ antlr4/atn/__pycache__/LexerATNSimulator.cpython-310.pyc,,
67
+ antlr4/atn/__pycache__/LexerAction.cpython-310.pyc,,
68
+ antlr4/atn/__pycache__/LexerActionExecutor.cpython-310.pyc,,
69
+ antlr4/atn/__pycache__/ParserATNSimulator.cpython-310.pyc,,
70
+ antlr4/atn/__pycache__/PredictionMode.cpython-310.pyc,,
71
+ antlr4/atn/__pycache__/SemanticContext.cpython-310.pyc,,
72
+ antlr4/atn/__pycache__/Transition.cpython-310.pyc,,
73
+ antlr4/atn/__pycache__/__init__.cpython-310.pyc,,
74
+ antlr4/dfa/DFA.py,sha256=weIh0uaRfakP12mFvHo7U0tqO3GONV3-nHFkc2xk-ZE,5388
75
+ antlr4/dfa/DFASerializer.py,sha256=1st_HO85yXLYy7gInTEnkztgA6am4CT-yReh-mazp9E,2518
76
+ antlr4/dfa/DFAState.py,sha256=R7JwKf0GtAEs9J_MD_Y0WKcuzdt0BVX1sow-uv9yFYc,5583
77
+ antlr4/dfa/__init__.py,sha256=gsnQdtTH8IUgCiVUpQfzhxx2pFRvksW76SjwIk3fYSk,28
78
+ antlr4/dfa/__pycache__/DFA.cpython-310.pyc,,
79
+ antlr4/dfa/__pycache__/DFASerializer.cpython-310.pyc,,
80
+ antlr4/dfa/__pycache__/DFAState.cpython-310.pyc,,
81
+ antlr4/dfa/__pycache__/__init__.cpython-310.pyc,,
82
+ antlr4/error/DiagnosticErrorListener.py,sha256=EwS2D_Ox6CmvCa16NPJ9ud4QYPHmlPXt6-Wdn1h5Kg8,4430
83
+ antlr4/error/ErrorListener.py,sha256=yP_MDguol4Cj0_pEPyNzeH3v4ZvUjW5iwDjhYTVAHbE,2722
84
+ antlr4/error/ErrorStrategy.py,sha256=0mhzFL57ZVnjKkGrtadta93Zm3NXdF-HW10DVD07VXs,30391
85
+ antlr4/error/Errors.py,sha256=hlKngclBfXdkDiAymhYsvh2OCXlvmHM2kTl_A1vgp-w,6759
86
+ antlr4/error/__init__.py,sha256=gsnQdtTH8IUgCiVUpQfzhxx2pFRvksW76SjwIk3fYSk,28
87
+ antlr4/error/__pycache__/DiagnosticErrorListener.cpython-310.pyc,,
88
+ antlr4/error/__pycache__/ErrorListener.cpython-310.pyc,,
89
+ antlr4/error/__pycache__/ErrorStrategy.cpython-310.pyc,,
90
+ antlr4/error/__pycache__/Errors.cpython-310.pyc,,
91
+ antlr4/error/__pycache__/__init__.cpython-310.pyc,,
92
+ antlr4/tree/Chunk.py,sha256=oCIZjolLq9xkxtVDROEDxfUGgndcEnsDW0eUmLM7Gpk,695
93
+ antlr4/tree/ParseTreeMatch.py,sha256=Dc6GVWSUqoIAFXUaUZqUwCUlZfTcgUbGLGzNf6QxQvE,4485
94
+ antlr4/tree/ParseTreePattern.py,sha256=ASBNaQORh3f7f8KnFeZJC2yWFFx4uQlxvC2Y55ifhY0,2825
95
+ antlr4/tree/ParseTreePatternMatcher.py,sha256=HtE9yi1Urr2QPLGLJDBvr0lxv6bjuj9CHl-4clahSe8,16388
96
+ antlr4/tree/RuleTagToken.py,sha256=n4zXcmrrfsGyl91pj5ZYcc_CeKMhPrvYkUdppgMBpbY,2022
97
+ antlr4/tree/TokenTagToken.py,sha256=S3o3DJhfzL5kpClxsKyI-Il-xvuuZQiBAIsLCKFjRHo,1576
98
+ antlr4/tree/Tree.py,sha256=ZI7U_5IxBLm_IrnfJOtb12BCPIWyzfeZtLnhHKVVZIw,5572
99
+ antlr4/tree/Trees.py,sha256=JtQ7cYWmKwI9TIBP6y9XIgjlNS4mYjv3ARwOfwWc5Vg,3968
100
+ antlr4/tree/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
101
+ antlr4/tree/__pycache__/Chunk.cpython-310.pyc,,
102
+ antlr4/tree/__pycache__/ParseTreeMatch.cpython-310.pyc,,
103
+ antlr4/tree/__pycache__/ParseTreePattern.cpython-310.pyc,,
104
+ antlr4/tree/__pycache__/ParseTreePatternMatcher.cpython-310.pyc,,
105
+ antlr4/tree/__pycache__/RuleTagToken.cpython-310.pyc,,
106
+ antlr4/tree/__pycache__/TokenTagToken.cpython-310.pyc,,
107
+ antlr4/tree/__pycache__/Tree.cpython-310.pyc,,
108
+ antlr4/tree/__pycache__/Trees.cpython-310.pyc,,
109
+ antlr4/tree/__pycache__/__init__.cpython-310.pyc,,
110
+ antlr4/xpath/XPath.py,sha256=O9s4-EDvLbAbYytP_bae9Z2khLl0iAtRzPAtVbuWUM4,13015
111
+ antlr4/xpath/__init__.py,sha256=gsnQdtTH8IUgCiVUpQfzhxx2pFRvksW76SjwIk3fYSk,28
112
+ antlr4/xpath/__pycache__/XPath.cpython-310.pyc,,
113
+ antlr4/xpath/__pycache__/__init__.cpython-310.pyc,,
114
+ antlr4_python3_runtime-4.9.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
115
+ antlr4_python3_runtime-4.9.3.dist-info/METADATA,sha256=vzJeUVUxg4Wi_MRoB0LacQu63xOj0cCXmpizSHmA8mE,403
116
+ antlr4_python3_runtime-4.9.3.dist-info/RECORD,,
117
+ antlr4_python3_runtime-4.9.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
118
+ antlr4_python3_runtime-4.9.3.dist-info/top_level.txt,sha256=OsoZsh9bb30wgXb2zBUjdDwYg46MfV-RVZA6Pk8pcB0,7
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/WHEEL ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
miniconda3/envs/active_proaction/lib/python3.10/site-packages/antlr4_python3_runtime-4.9.3.dist-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ antlr4
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/INSTALLER ADDED
@@ -0,0 +1 @@
 
 
1
+ pip
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/METADATA ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.4
2
+ Name: anyio
3
+ Version: 4.12.1
4
+ Summary: High-level concurrency and networking framework on top of asyncio or Trio
5
+ Author-email: Alex Grönholm <alex.gronholm@nextday.fi>
6
+ License-Expression: MIT
7
+ Project-URL: Documentation, https://anyio.readthedocs.io/en/latest/
8
+ Project-URL: Changelog, https://anyio.readthedocs.io/en/stable/versionhistory.html
9
+ Project-URL: Source code, https://github.com/agronholm/anyio
10
+ Project-URL: Issue tracker, https://github.com/agronholm/anyio/issues
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Framework :: AnyIO
14
+ Classifier: Typing :: Typed
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Requires-Python: >=3.9
24
+ Description-Content-Type: text/x-rst
25
+ License-File: LICENSE
26
+ Requires-Dist: exceptiongroup>=1.0.2; python_version < "3.11"
27
+ Requires-Dist: idna>=2.8
28
+ Requires-Dist: typing_extensions>=4.5; python_version < "3.13"
29
+ Provides-Extra: trio
30
+ Requires-Dist: trio>=0.32.0; python_version >= "3.10" and extra == "trio"
31
+ Requires-Dist: trio>=0.31.0; python_version < "3.10" and extra == "trio"
32
+ Dynamic: license-file
33
+
34
+ .. image:: https://github.com/agronholm/anyio/actions/workflows/test.yml/badge.svg
35
+ :target: https://github.com/agronholm/anyio/actions/workflows/test.yml
36
+ :alt: Build Status
37
+ .. image:: https://coveralls.io/repos/github/agronholm/anyio/badge.svg?branch=master
38
+ :target: https://coveralls.io/github/agronholm/anyio?branch=master
39
+ :alt: Code Coverage
40
+ .. image:: https://readthedocs.org/projects/anyio/badge/?version=latest
41
+ :target: https://anyio.readthedocs.io/en/latest/?badge=latest
42
+ :alt: Documentation
43
+ .. image:: https://badges.gitter.im/gitterHQ/gitter.svg
44
+ :target: https://gitter.im/python-trio/AnyIO
45
+ :alt: Gitter chat
46
+
47
+ AnyIO is an asynchronous networking and concurrency library that works on top of either asyncio_ or
48
+ Trio_. It implements Trio-like `structured concurrency`_ (SC) on top of asyncio and works in harmony
49
+ with the native SC of Trio itself.
50
+
51
+ Applications and libraries written against AnyIO's API will run unmodified on either asyncio_ or
52
+ Trio_. AnyIO can also be adopted into a library or application incrementally – bit by bit, no full
53
+ refactoring necessary. It will blend in with the native libraries of your chosen backend.
54
+
55
+ To find out why you might want to use AnyIO's APIs instead of asyncio's, you can read about it
56
+ `here <https://anyio.readthedocs.io/en/stable/why.html>`_.
57
+
58
+ Documentation
59
+ -------------
60
+
61
+ View full documentation at: https://anyio.readthedocs.io/
62
+
63
+ Features
64
+ --------
65
+
66
+ AnyIO offers the following functionality:
67
+
68
+ * Task groups (nurseries_ in trio terminology)
69
+ * High-level networking (TCP, UDP and UNIX sockets)
70
+
71
+ * `Happy eyeballs`_ algorithm for TCP connections (more robust than that of asyncio on Python
72
+ 3.8)
73
+ * async/await style UDP sockets (unlike asyncio where you still have to use Transports and
74
+ Protocols)
75
+
76
+ * A versatile API for byte streams and object streams
77
+ * Inter-task synchronization and communication (locks, conditions, events, semaphores, object
78
+ streams)
79
+ * Worker threads
80
+ * Subprocesses
81
+ * Subinterpreter support for code parallelization (on Python 3.13 and later)
82
+ * Asynchronous file I/O (using worker threads)
83
+ * Signal handling
84
+ * Asynchronous version of the functools_ module
85
+
86
+ AnyIO also comes with its own pytest_ plugin which also supports asynchronous fixtures.
87
+ It even works with the popular Hypothesis_ library.
88
+
89
+ .. _asyncio: https://docs.python.org/3/library/asyncio.html
90
+ .. _Trio: https://github.com/python-trio/trio
91
+ .. _structured concurrency: https://en.wikipedia.org/wiki/Structured_concurrency
92
+ .. _nurseries: https://trio.readthedocs.io/en/stable/reference-core.html#nurseries-and-spawning
93
+ .. _Happy eyeballs: https://en.wikipedia.org/wiki/Happy_Eyeballs
94
+ .. _pytest: https://docs.pytest.org/en/latest/
95
+ .. _functools: https://docs.python.org/3/library/functools.html
96
+ .. _Hypothesis: https://hypothesis.works/
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/RECORD ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ anyio-4.12.1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
2
+ anyio-4.12.1.dist-info/METADATA,sha256=DfiDab9Tmmcfy802lOLTMEHJQShkOSbopCwqCYbLuJk,4277
3
+ anyio-4.12.1.dist-info/RECORD,,
4
+ anyio-4.12.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ anyio-4.12.1.dist-info/entry_points.txt,sha256=_d6Yu6uiaZmNe0CydowirE9Cmg7zUL2g08tQpoS3Qvc,39
6
+ anyio-4.12.1.dist-info/licenses/LICENSE,sha256=U2GsncWPLvX9LpsJxoKXwX8ElQkJu8gCO9uC6s8iwrA,1081
7
+ anyio-4.12.1.dist-info/top_level.txt,sha256=QglSMiWX8_5dpoVAEIHdEYzvqFMdSYWmCj6tYw2ITkQ,6
8
+ anyio/__init__.py,sha256=7iDVqMUprUuKNY91FuoKqayAhR-OY136YDPI6P78HHk,6170
9
+ anyio/__pycache__/__init__.cpython-310.pyc,,
10
+ anyio/__pycache__/from_thread.cpython-310.pyc,,
11
+ anyio/__pycache__/functools.cpython-310.pyc,,
12
+ anyio/__pycache__/lowlevel.cpython-310.pyc,,
13
+ anyio/__pycache__/pytest_plugin.cpython-310.pyc,,
14
+ anyio/__pycache__/to_interpreter.cpython-310.pyc,,
15
+ anyio/__pycache__/to_process.cpython-310.pyc,,
16
+ anyio/__pycache__/to_thread.cpython-310.pyc,,
17
+ anyio/_backends/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
+ anyio/_backends/__pycache__/__init__.cpython-310.pyc,,
19
+ anyio/_backends/__pycache__/_asyncio.cpython-310.pyc,,
20
+ anyio/_backends/__pycache__/_trio.cpython-310.pyc,,
21
+ anyio/_backends/_asyncio.py,sha256=xG6qv60mgGnL0mK82dxjH2b8hlkMlJ-x2BqIq3qv70Y,98863
22
+ anyio/_backends/_trio.py,sha256=30Rctb7lm8g63ZHljVPVnj5aH-uK6oQvphjwUBoAzuI,41456
23
+ anyio/_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
+ anyio/_core/__pycache__/__init__.cpython-310.pyc,,
25
+ anyio/_core/__pycache__/_asyncio_selector_thread.cpython-310.pyc,,
26
+ anyio/_core/__pycache__/_contextmanagers.cpython-310.pyc,,
27
+ anyio/_core/__pycache__/_eventloop.cpython-310.pyc,,
28
+ anyio/_core/__pycache__/_exceptions.cpython-310.pyc,,
29
+ anyio/_core/__pycache__/_fileio.cpython-310.pyc,,
30
+ anyio/_core/__pycache__/_resources.cpython-310.pyc,,
31
+ anyio/_core/__pycache__/_signals.cpython-310.pyc,,
32
+ anyio/_core/__pycache__/_sockets.cpython-310.pyc,,
33
+ anyio/_core/__pycache__/_streams.cpython-310.pyc,,
34
+ anyio/_core/__pycache__/_subprocesses.cpython-310.pyc,,
35
+ anyio/_core/__pycache__/_synchronization.cpython-310.pyc,,
36
+ anyio/_core/__pycache__/_tasks.cpython-310.pyc,,
37
+ anyio/_core/__pycache__/_tempfile.cpython-310.pyc,,
38
+ anyio/_core/__pycache__/_testing.cpython-310.pyc,,
39
+ anyio/_core/__pycache__/_typedattr.cpython-310.pyc,,
40
+ anyio/_core/_asyncio_selector_thread.py,sha256=2PdxFM3cs02Kp6BSppbvmRT7q7asreTW5FgBxEsflBo,5626
41
+ anyio/_core/_contextmanagers.py,sha256=YInBCabiEeS-UaP_Jdxa1CaFC71ETPW8HZTHIM8Rsc8,7215
42
+ anyio/_core/_eventloop.py,sha256=c2EdcBX-xnKwxPcC4Pjn3_qG9I-x4IWFO2R9RqCGjM4,6448
43
+ anyio/_core/_exceptions.py,sha256=Y3aq-Wxd7Q2HqwSg7nZPvRsHEuGazv_qeet6gqEBdPk,4407
44
+ anyio/_core/_fileio.py,sha256=uc7t10Vb-If7GbdWM_zFf-ajUe6uek63fSt7IBLlZW0,25731
45
+ anyio/_core/_resources.py,sha256=NbmU5O5UX3xEyACnkmYX28Fmwdl-f-ny0tHym26e0w0,435
46
+ anyio/_core/_signals.py,sha256=mjTBB2hTKNPRlU0IhnijeQedpWOGERDiMjSlJQsFrug,1016
47
+ anyio/_core/_sockets.py,sha256=RBXHcUqZt5gg_-OOfgHVv8uq2FSKk1uVUzTdpjBoI1o,34977
48
+ anyio/_core/_streams.py,sha256=FczFwIgDpnkK0bODWJXMpsUJYdvAD04kaUaGzJU8DK0,1806
49
+ anyio/_core/_subprocesses.py,sha256=EXm5igL7dj55iYkPlbYVAqtbqxJxjU-6OndSTIx9SRg,8047
50
+ anyio/_core/_synchronization.py,sha256=MgVVqFzvt580tHC31LiOcq1G6aryut--xRG4Ff8KwxQ,20869
51
+ anyio/_core/_tasks.py,sha256=pVB7K6AAulzUM8YgXAeqNZG44nSyZ1bYJjH8GznC00I,5435
52
+ anyio/_core/_tempfile.py,sha256=lHb7CW4FyIlpkf5ADAf4VmLHCKwEHF9nxqNyBCFFUiA,19697
53
+ anyio/_core/_testing.py,sha256=u7MPqGXwpTxqI7hclSdNA30z2GH1Nw258uwKvy_RfBg,2340
54
+ anyio/_core/_typedattr.py,sha256=P4ozZikn3-DbpoYcvyghS_FOYAgbmUxeoU8-L_07pZM,2508
55
+ anyio/abc/__init__.py,sha256=6mWhcl_pGXhrgZVHP_TCfMvIXIOp9mroEFM90fYCU_U,2869
56
+ anyio/abc/__pycache__/__init__.cpython-310.pyc,,
57
+ anyio/abc/__pycache__/_eventloop.cpython-310.pyc,,
58
+ anyio/abc/__pycache__/_resources.cpython-310.pyc,,
59
+ anyio/abc/__pycache__/_sockets.cpython-310.pyc,,
60
+ anyio/abc/__pycache__/_streams.cpython-310.pyc,,
61
+ anyio/abc/__pycache__/_subprocesses.cpython-310.pyc,,
62
+ anyio/abc/__pycache__/_tasks.cpython-310.pyc,,
63
+ anyio/abc/__pycache__/_testing.cpython-310.pyc,,
64
+ anyio/abc/_eventloop.py,sha256=GlzgB3UJGgG6Kr7olpjOZ-o00PghecXuofVDQ_5611Q,10749
65
+ anyio/abc/_resources.py,sha256=DrYvkNN1hH6Uvv5_5uKySvDsnknGVDe8FCKfko0VtN8,783
66
+ anyio/abc/_sockets.py,sha256=ECTY0jLEF18gryANHR3vFzXzGdZ-xPwELq1QdgOb0Jo,13258
67
+ anyio/abc/_streams.py,sha256=005GKSCXGprxnhucILboSqc2JFovECZk9m3p-qqxXVc,7640
68
+ anyio/abc/_subprocesses.py,sha256=cumAPJTktOQtw63IqG0lDpyZqu_l1EElvQHMiwJgL08,2067
69
+ anyio/abc/_tasks.py,sha256=KC7wrciE48AINOI-AhPutnFhe1ewfP7QnamFlDzqesQ,3721
70
+ anyio/abc/_testing.py,sha256=tBJUzkSfOXJw23fe8qSJ03kJlShOYjjaEyFB6k6MYT8,1821
71
+ anyio/from_thread.py,sha256=L-0w1HxJ6BSb-KuVi57k5Tkc3yzQrx3QK5tAxMPcY-0,19141
72
+ anyio/functools.py,sha256=HWj7GBEmc0Z-mZg3uok7Z7ZJn0rEC_0Pzbt0nYUDaTQ,10973
73
+ anyio/lowlevel.py,sha256=AyKLVK3LaWSoK39LkCKxE4_GDMLKZBNqTrLUgk63y80,5158
74
+ anyio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
75
+ anyio/pytest_plugin.py,sha256=3jAFQn0jv_pyoWE2GBBlHaj9sqXj4e8vob0_hgrsXE8,10244
76
+ anyio/streams/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
77
+ anyio/streams/__pycache__/__init__.cpython-310.pyc,,
78
+ anyio/streams/__pycache__/buffered.cpython-310.pyc,,
79
+ anyio/streams/__pycache__/file.cpython-310.pyc,,
80
+ anyio/streams/__pycache__/memory.cpython-310.pyc,,
81
+ anyio/streams/__pycache__/stapled.cpython-310.pyc,,
82
+ anyio/streams/__pycache__/text.cpython-310.pyc,,
83
+ anyio/streams/__pycache__/tls.cpython-310.pyc,,
84
+ anyio/streams/buffered.py,sha256=2R3PeJhe4EXrdYqz44Y6-Eg9R6DrmlsYrP36Ir43-po,6263
85
+ anyio/streams/file.py,sha256=4WZ7XGz5WNu39FQHvqbe__TQ0HDP9OOhgO1mk9iVpVU,4470
86
+ anyio/streams/memory.py,sha256=F0zwzvFJKAhX_LRZGoKzzqDC2oMM-f-yyTBrEYEGOaU,10740
87
+ anyio/streams/stapled.py,sha256=T8Xqwf8K6EgURPxbt1N4i7A8BAk-gScv-GRhjLXIf_o,4390
88
+ anyio/streams/text.py,sha256=BcVAGJw1VRvtIqnv-o0Rb0pwH7p8vwlvl21xHq522ag,5765
89
+ anyio/streams/tls.py,sha256=Jpxy0Mfbcp1BxHCwE-YjSSFaLnIBbnnwur-excYThs4,15368
90
+ anyio/to_interpreter.py,sha256=_mLngrMy97TMR6VbW4Y6YzDUk9ZuPcQMPlkuyRh3C9k,7100
91
+ anyio/to_process.py,sha256=J7gAA_YOuoHqnpDAf5fm1Qu6kOmTzdFbiDNvnV755vk,9798
92
+ anyio/to_thread.py,sha256=menEgXYmUV7Fjg_9WqCV95P9MAtQS8BzPGGcWB_QnfQ,2687
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/WHEEL ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/entry_points.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [pytest11]
2
+ anyio = anyio.pytest_plugin
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/licenses/LICENSE ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alex Grönholm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio-4.12.1.dist-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ anyio
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/__init__.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from ._core._contextmanagers import AsyncContextManagerMixin as AsyncContextManagerMixin
4
+ from ._core._contextmanagers import ContextManagerMixin as ContextManagerMixin
5
+ from ._core._eventloop import current_time as current_time
6
+ from ._core._eventloop import get_all_backends as get_all_backends
7
+ from ._core._eventloop import get_available_backends as get_available_backends
8
+ from ._core._eventloop import get_cancelled_exc_class as get_cancelled_exc_class
9
+ from ._core._eventloop import run as run
10
+ from ._core._eventloop import sleep as sleep
11
+ from ._core._eventloop import sleep_forever as sleep_forever
12
+ from ._core._eventloop import sleep_until as sleep_until
13
+ from ._core._exceptions import BrokenResourceError as BrokenResourceError
14
+ from ._core._exceptions import BrokenWorkerInterpreter as BrokenWorkerInterpreter
15
+ from ._core._exceptions import BrokenWorkerProcess as BrokenWorkerProcess
16
+ from ._core._exceptions import BusyResourceError as BusyResourceError
17
+ from ._core._exceptions import ClosedResourceError as ClosedResourceError
18
+ from ._core._exceptions import ConnectionFailed as ConnectionFailed
19
+ from ._core._exceptions import DelimiterNotFound as DelimiterNotFound
20
+ from ._core._exceptions import EndOfStream as EndOfStream
21
+ from ._core._exceptions import IncompleteRead as IncompleteRead
22
+ from ._core._exceptions import NoEventLoopError as NoEventLoopError
23
+ from ._core._exceptions import RunFinishedError as RunFinishedError
24
+ from ._core._exceptions import TypedAttributeLookupError as TypedAttributeLookupError
25
+ from ._core._exceptions import WouldBlock as WouldBlock
26
+ from ._core._fileio import AsyncFile as AsyncFile
27
+ from ._core._fileio import Path as Path
28
+ from ._core._fileio import open_file as open_file
29
+ from ._core._fileio import wrap_file as wrap_file
30
+ from ._core._resources import aclose_forcefully as aclose_forcefully
31
+ from ._core._signals import open_signal_receiver as open_signal_receiver
32
+ from ._core._sockets import TCPConnectable as TCPConnectable
33
+ from ._core._sockets import UNIXConnectable as UNIXConnectable
34
+ from ._core._sockets import as_connectable as as_connectable
35
+ from ._core._sockets import connect_tcp as connect_tcp
36
+ from ._core._sockets import connect_unix as connect_unix
37
+ from ._core._sockets import create_connected_udp_socket as create_connected_udp_socket
38
+ from ._core._sockets import (
39
+ create_connected_unix_datagram_socket as create_connected_unix_datagram_socket,
40
+ )
41
+ from ._core._sockets import create_tcp_listener as create_tcp_listener
42
+ from ._core._sockets import create_udp_socket as create_udp_socket
43
+ from ._core._sockets import create_unix_datagram_socket as create_unix_datagram_socket
44
+ from ._core._sockets import create_unix_listener as create_unix_listener
45
+ from ._core._sockets import getaddrinfo as getaddrinfo
46
+ from ._core._sockets import getnameinfo as getnameinfo
47
+ from ._core._sockets import notify_closing as notify_closing
48
+ from ._core._sockets import wait_readable as wait_readable
49
+ from ._core._sockets import wait_socket_readable as wait_socket_readable
50
+ from ._core._sockets import wait_socket_writable as wait_socket_writable
51
+ from ._core._sockets import wait_writable as wait_writable
52
+ from ._core._streams import create_memory_object_stream as create_memory_object_stream
53
+ from ._core._subprocesses import open_process as open_process
54
+ from ._core._subprocesses import run_process as run_process
55
+ from ._core._synchronization import CapacityLimiter as CapacityLimiter
56
+ from ._core._synchronization import (
57
+ CapacityLimiterStatistics as CapacityLimiterStatistics,
58
+ )
59
+ from ._core._synchronization import Condition as Condition
60
+ from ._core._synchronization import ConditionStatistics as ConditionStatistics
61
+ from ._core._synchronization import Event as Event
62
+ from ._core._synchronization import EventStatistics as EventStatistics
63
+ from ._core._synchronization import Lock as Lock
64
+ from ._core._synchronization import LockStatistics as LockStatistics
65
+ from ._core._synchronization import ResourceGuard as ResourceGuard
66
+ from ._core._synchronization import Semaphore as Semaphore
67
+ from ._core._synchronization import SemaphoreStatistics as SemaphoreStatistics
68
+ from ._core._tasks import TASK_STATUS_IGNORED as TASK_STATUS_IGNORED
69
+ from ._core._tasks import CancelScope as CancelScope
70
+ from ._core._tasks import create_task_group as create_task_group
71
+ from ._core._tasks import current_effective_deadline as current_effective_deadline
72
+ from ._core._tasks import fail_after as fail_after
73
+ from ._core._tasks import move_on_after as move_on_after
74
+ from ._core._tempfile import NamedTemporaryFile as NamedTemporaryFile
75
+ from ._core._tempfile import SpooledTemporaryFile as SpooledTemporaryFile
76
+ from ._core._tempfile import TemporaryDirectory as TemporaryDirectory
77
+ from ._core._tempfile import TemporaryFile as TemporaryFile
78
+ from ._core._tempfile import gettempdir as gettempdir
79
+ from ._core._tempfile import gettempdirb as gettempdirb
80
+ from ._core._tempfile import mkdtemp as mkdtemp
81
+ from ._core._tempfile import mkstemp as mkstemp
82
+ from ._core._testing import TaskInfo as TaskInfo
83
+ from ._core._testing import get_current_task as get_current_task
84
+ from ._core._testing import get_running_tasks as get_running_tasks
85
+ from ._core._testing import wait_all_tasks_blocked as wait_all_tasks_blocked
86
+ from ._core._typedattr import TypedAttributeProvider as TypedAttributeProvider
87
+ from ._core._typedattr import TypedAttributeSet as TypedAttributeSet
88
+ from ._core._typedattr import typed_attribute as typed_attribute
89
+
90
+ # Re-export imports so they look like they live directly in this package
91
+ for __value in list(locals().values()):
92
+ if getattr(__value, "__module__", "").startswith("anyio."):
93
+ __value.__module__ = __name__
94
+
95
+
96
+ del __value
97
+
98
+
99
+ def __getattr__(attr: str) -> type[BrokenWorkerInterpreter]:
100
+ """Support deprecated aliases."""
101
+ if attr == "BrokenWorkerIntepreter":
102
+ import warnings
103
+
104
+ warnings.warn(
105
+ "The 'BrokenWorkerIntepreter' alias is deprecated, use 'BrokenWorkerInterpreter' instead.",
106
+ DeprecationWarning,
107
+ stacklevel=2,
108
+ )
109
+ return BrokenWorkerInterpreter
110
+
111
+ raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_backends/__init__.py ADDED
File without changes
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_backends/_asyncio.py ADDED
The diff for this file is too large to render. See raw diff
 
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_backends/_trio.py ADDED
@@ -0,0 +1,1346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import array
4
+ import math
5
+ import os
6
+ import socket
7
+ import sys
8
+ import types
9
+ import weakref
10
+ from collections.abc import (
11
+ AsyncGenerator,
12
+ AsyncIterator,
13
+ Awaitable,
14
+ Callable,
15
+ Collection,
16
+ Coroutine,
17
+ Iterable,
18
+ Sequence,
19
+ )
20
+ from contextlib import AbstractContextManager
21
+ from dataclasses import dataclass
22
+ from io import IOBase
23
+ from os import PathLike
24
+ from signal import Signals
25
+ from socket import AddressFamily, SocketKind
26
+ from types import TracebackType
27
+ from typing import (
28
+ IO,
29
+ TYPE_CHECKING,
30
+ Any,
31
+ Generic,
32
+ NoReturn,
33
+ TypeVar,
34
+ cast,
35
+ overload,
36
+ )
37
+
38
+ import trio.from_thread
39
+ import trio.lowlevel
40
+ from outcome import Error, Outcome, Value
41
+ from trio.lowlevel import (
42
+ current_root_task,
43
+ current_task,
44
+ notify_closing,
45
+ wait_readable,
46
+ wait_writable,
47
+ )
48
+ from trio.socket import SocketType as TrioSocketType
49
+ from trio.to_thread import run_sync
50
+
51
+ from .. import (
52
+ CapacityLimiterStatistics,
53
+ EventStatistics,
54
+ LockStatistics,
55
+ RunFinishedError,
56
+ TaskInfo,
57
+ WouldBlock,
58
+ abc,
59
+ )
60
+ from .._core._eventloop import claim_worker_thread
61
+ from .._core._exceptions import (
62
+ BrokenResourceError,
63
+ BusyResourceError,
64
+ ClosedResourceError,
65
+ EndOfStream,
66
+ )
67
+ from .._core._sockets import convert_ipv6_sockaddr
68
+ from .._core._streams import create_memory_object_stream
69
+ from .._core._synchronization import (
70
+ CapacityLimiter as BaseCapacityLimiter,
71
+ )
72
+ from .._core._synchronization import Event as BaseEvent
73
+ from .._core._synchronization import Lock as BaseLock
74
+ from .._core._synchronization import (
75
+ ResourceGuard,
76
+ SemaphoreStatistics,
77
+ )
78
+ from .._core._synchronization import Semaphore as BaseSemaphore
79
+ from .._core._tasks import CancelScope as BaseCancelScope
80
+ from ..abc import IPSockAddrType, UDPPacketType, UNIXDatagramPacketType
81
+ from ..abc._eventloop import AsyncBackend, StrOrBytesPath
82
+ from ..streams.memory import MemoryObjectSendStream
83
+
84
+ if TYPE_CHECKING:
85
+ from _typeshed import FileDescriptorLike
86
+
87
+ if sys.version_info >= (3, 10):
88
+ from typing import ParamSpec
89
+ else:
90
+ from typing_extensions import ParamSpec
91
+
92
+ if sys.version_info >= (3, 11):
93
+ from typing import TypeVarTuple, Unpack
94
+ else:
95
+ from exceptiongroup import BaseExceptionGroup
96
+ from typing_extensions import TypeVarTuple, Unpack
97
+
98
+ T = TypeVar("T")
99
+ T_Retval = TypeVar("T_Retval")
100
+ T_SockAddr = TypeVar("T_SockAddr", str, IPSockAddrType)
101
+ PosArgsT = TypeVarTuple("PosArgsT")
102
+ P = ParamSpec("P")
103
+
104
+
105
+ #
106
+ # Event loop
107
+ #
108
+
109
+ RunVar = trio.lowlevel.RunVar
110
+
111
+
112
+ #
113
+ # Timeouts and cancellation
114
+ #
115
+
116
+
117
+ class CancelScope(BaseCancelScope):
118
+ def __new__(
119
+ cls, original: trio.CancelScope | None = None, **kwargs: object
120
+ ) -> CancelScope:
121
+ return object.__new__(cls)
122
+
123
+ def __init__(self, original: trio.CancelScope | None = None, **kwargs: Any) -> None:
124
+ self.__original = original or trio.CancelScope(**kwargs)
125
+
126
+ def __enter__(self) -> CancelScope:
127
+ self.__original.__enter__()
128
+ return self
129
+
130
+ def __exit__(
131
+ self,
132
+ exc_type: type[BaseException] | None,
133
+ exc_val: BaseException | None,
134
+ exc_tb: TracebackType | None,
135
+ ) -> bool:
136
+ return self.__original.__exit__(exc_type, exc_val, exc_tb)
137
+
138
+ def cancel(self, reason: str | None = None) -> None:
139
+ self.__original.cancel(reason)
140
+
141
+ @property
142
+ def deadline(self) -> float:
143
+ return self.__original.deadline
144
+
145
+ @deadline.setter
146
+ def deadline(self, value: float) -> None:
147
+ self.__original.deadline = value
148
+
149
+ @property
150
+ def cancel_called(self) -> bool:
151
+ return self.__original.cancel_called
152
+
153
+ @property
154
+ def cancelled_caught(self) -> bool:
155
+ return self.__original.cancelled_caught
156
+
157
+ @property
158
+ def shield(self) -> bool:
159
+ return self.__original.shield
160
+
161
+ @shield.setter
162
+ def shield(self, value: bool) -> None:
163
+ self.__original.shield = value
164
+
165
+
166
+ #
167
+ # Task groups
168
+ #
169
+
170
+
171
+ class TaskGroup(abc.TaskGroup):
172
+ def __init__(self) -> None:
173
+ self._active = False
174
+ self._nursery_manager = trio.open_nursery(strict_exception_groups=True)
175
+ self.cancel_scope = None # type: ignore[assignment]
176
+
177
+ async def __aenter__(self) -> TaskGroup:
178
+ self._active = True
179
+ self._nursery = await self._nursery_manager.__aenter__()
180
+ self.cancel_scope = CancelScope(self._nursery.cancel_scope)
181
+ return self
182
+
183
+ async def __aexit__(
184
+ self,
185
+ exc_type: type[BaseException] | None,
186
+ exc_val: BaseException | None,
187
+ exc_tb: TracebackType | None,
188
+ ) -> bool:
189
+ try:
190
+ # trio.Nursery.__exit__ returns bool; .open_nursery has wrong type
191
+ return await self._nursery_manager.__aexit__(exc_type, exc_val, exc_tb) # type: ignore[return-value]
192
+ except BaseExceptionGroup as exc:
193
+ if not exc.split(trio.Cancelled)[1]:
194
+ raise trio.Cancelled._create() from exc
195
+
196
+ raise
197
+ finally:
198
+ del exc_val, exc_tb
199
+ self._active = False
200
+
201
+ def start_soon(
202
+ self,
203
+ func: Callable[[Unpack[PosArgsT]], Awaitable[Any]],
204
+ *args: Unpack[PosArgsT],
205
+ name: object = None,
206
+ ) -> None:
207
+ if not self._active:
208
+ raise RuntimeError(
209
+ "This task group is not active; no new tasks can be started."
210
+ )
211
+
212
+ self._nursery.start_soon(func, *args, name=name)
213
+
214
+ async def start(
215
+ self, func: Callable[..., Awaitable[Any]], *args: object, name: object = None
216
+ ) -> Any:
217
+ if not self._active:
218
+ raise RuntimeError(
219
+ "This task group is not active; no new tasks can be started."
220
+ )
221
+
222
+ return await self._nursery.start(func, *args, name=name)
223
+
224
+
225
+ #
226
+ # Subprocesses
227
+ #
228
+
229
+
230
+ @dataclass(eq=False)
231
+ class ReceiveStreamWrapper(abc.ByteReceiveStream):
232
+ _stream: trio.abc.ReceiveStream
233
+
234
+ async def receive(self, max_bytes: int | None = None) -> bytes:
235
+ try:
236
+ data = await self._stream.receive_some(max_bytes)
237
+ except trio.ClosedResourceError as exc:
238
+ raise ClosedResourceError from exc.__cause__
239
+ except trio.BrokenResourceError as exc:
240
+ raise BrokenResourceError from exc.__cause__
241
+
242
+ if data:
243
+ return bytes(data)
244
+ else:
245
+ raise EndOfStream
246
+
247
+ async def aclose(self) -> None:
248
+ await self._stream.aclose()
249
+
250
+
251
+ @dataclass(eq=False)
252
+ class SendStreamWrapper(abc.ByteSendStream):
253
+ _stream: trio.abc.SendStream
254
+
255
+ async def send(self, item: bytes) -> None:
256
+ try:
257
+ await self._stream.send_all(item)
258
+ except trio.ClosedResourceError as exc:
259
+ raise ClosedResourceError from exc.__cause__
260
+ except trio.BrokenResourceError as exc:
261
+ raise BrokenResourceError from exc.__cause__
262
+
263
+ async def aclose(self) -> None:
264
+ await self._stream.aclose()
265
+
266
+
267
+ @dataclass(eq=False)
268
+ class Process(abc.Process):
269
+ _process: trio.Process
270
+ _stdin: abc.ByteSendStream | None
271
+ _stdout: abc.ByteReceiveStream | None
272
+ _stderr: abc.ByteReceiveStream | None
273
+
274
+ async def aclose(self) -> None:
275
+ with CancelScope(shield=True):
276
+ if self._stdin:
277
+ await self._stdin.aclose()
278
+ if self._stdout:
279
+ await self._stdout.aclose()
280
+ if self._stderr:
281
+ await self._stderr.aclose()
282
+
283
+ try:
284
+ await self.wait()
285
+ except BaseException:
286
+ self.kill()
287
+ with CancelScope(shield=True):
288
+ await self.wait()
289
+ raise
290
+
291
+ async def wait(self) -> int:
292
+ return await self._process.wait()
293
+
294
+ def terminate(self) -> None:
295
+ self._process.terminate()
296
+
297
+ def kill(self) -> None:
298
+ self._process.kill()
299
+
300
+ def send_signal(self, signal: Signals) -> None:
301
+ self._process.send_signal(signal)
302
+
303
+ @property
304
+ def pid(self) -> int:
305
+ return self._process.pid
306
+
307
+ @property
308
+ def returncode(self) -> int | None:
309
+ return self._process.returncode
310
+
311
+ @property
312
+ def stdin(self) -> abc.ByteSendStream | None:
313
+ return self._stdin
314
+
315
+ @property
316
+ def stdout(self) -> abc.ByteReceiveStream | None:
317
+ return self._stdout
318
+
319
+ @property
320
+ def stderr(self) -> abc.ByteReceiveStream | None:
321
+ return self._stderr
322
+
323
+
324
+ class _ProcessPoolShutdownInstrument(trio.abc.Instrument):
325
+ def after_run(self) -> None:
326
+ super().after_run()
327
+
328
+
329
+ current_default_worker_process_limiter: trio.lowlevel.RunVar = RunVar(
330
+ "current_default_worker_process_limiter"
331
+ )
332
+
333
+
334
+ async def _shutdown_process_pool(workers: set[abc.Process]) -> None:
335
+ try:
336
+ await trio.sleep(math.inf)
337
+ except trio.Cancelled:
338
+ for process in workers:
339
+ if process.returncode is None:
340
+ process.kill()
341
+
342
+ with CancelScope(shield=True):
343
+ for process in workers:
344
+ await process.aclose()
345
+
346
+
347
+ #
348
+ # Sockets and networking
349
+ #
350
+
351
+
352
+ class _TrioSocketMixin(Generic[T_SockAddr]):
353
+ def __init__(self, trio_socket: TrioSocketType) -> None:
354
+ self._trio_socket = trio_socket
355
+ self._closed = False
356
+
357
+ def _check_closed(self) -> None:
358
+ if self._closed:
359
+ raise ClosedResourceError
360
+ if self._trio_socket.fileno() < 0:
361
+ raise BrokenResourceError
362
+
363
+ @property
364
+ def _raw_socket(self) -> socket.socket:
365
+ return self._trio_socket._sock # type: ignore[attr-defined]
366
+
367
+ async def aclose(self) -> None:
368
+ if self._trio_socket.fileno() >= 0:
369
+ self._closed = True
370
+ self._trio_socket.close()
371
+
372
+ def _convert_socket_error(self, exc: BaseException) -> NoReturn:
373
+ if isinstance(exc, trio.ClosedResourceError):
374
+ raise ClosedResourceError from exc
375
+ elif self._trio_socket.fileno() < 0 and self._closed:
376
+ raise ClosedResourceError from None
377
+ elif isinstance(exc, OSError):
378
+ raise BrokenResourceError from exc
379
+ else:
380
+ raise exc
381
+
382
+
383
+ class SocketStream(_TrioSocketMixin, abc.SocketStream):
384
+ def __init__(self, trio_socket: TrioSocketType) -> None:
385
+ super().__init__(trio_socket)
386
+ self._receive_guard = ResourceGuard("reading from")
387
+ self._send_guard = ResourceGuard("writing to")
388
+
389
+ async def receive(self, max_bytes: int = 65536) -> bytes:
390
+ with self._receive_guard:
391
+ try:
392
+ data = await self._trio_socket.recv(max_bytes)
393
+ except BaseException as exc:
394
+ self._convert_socket_error(exc)
395
+
396
+ if data:
397
+ return data
398
+ else:
399
+ raise EndOfStream
400
+
401
+ async def send(self, item: bytes) -> None:
402
+ with self._send_guard:
403
+ view = memoryview(item)
404
+ while view:
405
+ try:
406
+ bytes_sent = await self._trio_socket.send(view)
407
+ except BaseException as exc:
408
+ self._convert_socket_error(exc)
409
+
410
+ view = view[bytes_sent:]
411
+
412
+ async def send_eof(self) -> None:
413
+ self._trio_socket.shutdown(socket.SHUT_WR)
414
+
415
+
416
+ class UNIXSocketStream(SocketStream, abc.UNIXSocketStream):
417
+ async def receive_fds(self, msglen: int, maxfds: int) -> tuple[bytes, list[int]]:
418
+ if not isinstance(msglen, int) or msglen < 0:
419
+ raise ValueError("msglen must be a non-negative integer")
420
+ if not isinstance(maxfds, int) or maxfds < 1:
421
+ raise ValueError("maxfds must be a positive integer")
422
+
423
+ fds = array.array("i")
424
+ await trio.lowlevel.checkpoint()
425
+ with self._receive_guard:
426
+ while True:
427
+ try:
428
+ message, ancdata, flags, addr = await self._trio_socket.recvmsg(
429
+ msglen, socket.CMSG_LEN(maxfds * fds.itemsize)
430
+ )
431
+ except BaseException as exc:
432
+ self._convert_socket_error(exc)
433
+ else:
434
+ if not message and not ancdata:
435
+ raise EndOfStream
436
+
437
+ break
438
+
439
+ for cmsg_level, cmsg_type, cmsg_data in ancdata:
440
+ if cmsg_level != socket.SOL_SOCKET or cmsg_type != socket.SCM_RIGHTS:
441
+ raise RuntimeError(
442
+ f"Received unexpected ancillary data; message = {message!r}, "
443
+ f"cmsg_level = {cmsg_level}, cmsg_type = {cmsg_type}"
444
+ )
445
+
446
+ fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
447
+
448
+ return message, list(fds)
449
+
450
+ async def send_fds(self, message: bytes, fds: Collection[int | IOBase]) -> None:
451
+ if not message:
452
+ raise ValueError("message must not be empty")
453
+ if not fds:
454
+ raise ValueError("fds must not be empty")
455
+
456
+ filenos: list[int] = []
457
+ for fd in fds:
458
+ if isinstance(fd, int):
459
+ filenos.append(fd)
460
+ elif isinstance(fd, IOBase):
461
+ filenos.append(fd.fileno())
462
+
463
+ fdarray = array.array("i", filenos)
464
+ await trio.lowlevel.checkpoint()
465
+ with self._send_guard:
466
+ while True:
467
+ try:
468
+ await self._trio_socket.sendmsg(
469
+ [message],
470
+ [
471
+ (
472
+ socket.SOL_SOCKET,
473
+ socket.SCM_RIGHTS,
474
+ fdarray,
475
+ )
476
+ ],
477
+ )
478
+ break
479
+ except BaseException as exc:
480
+ self._convert_socket_error(exc)
481
+
482
+
483
+ class TCPSocketListener(_TrioSocketMixin, abc.SocketListener):
484
+ def __init__(self, raw_socket: socket.socket):
485
+ super().__init__(trio.socket.from_stdlib_socket(raw_socket))
486
+ self._accept_guard = ResourceGuard("accepting connections from")
487
+
488
+ async def accept(self) -> SocketStream:
489
+ with self._accept_guard:
490
+ try:
491
+ trio_socket, _addr = await self._trio_socket.accept()
492
+ except BaseException as exc:
493
+ self._convert_socket_error(exc)
494
+
495
+ trio_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
496
+ return SocketStream(trio_socket)
497
+
498
+
499
+ class UNIXSocketListener(_TrioSocketMixin, abc.SocketListener):
500
+ def __init__(self, raw_socket: socket.socket):
501
+ super().__init__(trio.socket.from_stdlib_socket(raw_socket))
502
+ self._accept_guard = ResourceGuard("accepting connections from")
503
+
504
+ async def accept(self) -> UNIXSocketStream:
505
+ with self._accept_guard:
506
+ try:
507
+ trio_socket, _addr = await self._trio_socket.accept()
508
+ except BaseException as exc:
509
+ self._convert_socket_error(exc)
510
+
511
+ return UNIXSocketStream(trio_socket)
512
+
513
+
514
+ class UDPSocket(_TrioSocketMixin[IPSockAddrType], abc.UDPSocket):
515
+ def __init__(self, trio_socket: TrioSocketType) -> None:
516
+ super().__init__(trio_socket)
517
+ self._receive_guard = ResourceGuard("reading from")
518
+ self._send_guard = ResourceGuard("writing to")
519
+
520
+ async def receive(self) -> tuple[bytes, IPSockAddrType]:
521
+ with self._receive_guard:
522
+ try:
523
+ data, addr = await self._trio_socket.recvfrom(65536)
524
+ return data, convert_ipv6_sockaddr(addr)
525
+ except BaseException as exc:
526
+ self._convert_socket_error(exc)
527
+
528
+ async def send(self, item: UDPPacketType) -> None:
529
+ with self._send_guard:
530
+ try:
531
+ await self._trio_socket.sendto(*item)
532
+ except BaseException as exc:
533
+ self._convert_socket_error(exc)
534
+
535
+
536
+ class ConnectedUDPSocket(_TrioSocketMixin[IPSockAddrType], abc.ConnectedUDPSocket):
537
+ def __init__(self, trio_socket: TrioSocketType) -> None:
538
+ super().__init__(trio_socket)
539
+ self._receive_guard = ResourceGuard("reading from")
540
+ self._send_guard = ResourceGuard("writing to")
541
+
542
+ async def receive(self) -> bytes:
543
+ with self._receive_guard:
544
+ try:
545
+ return await self._trio_socket.recv(65536)
546
+ except BaseException as exc:
547
+ self._convert_socket_error(exc)
548
+
549
+ async def send(self, item: bytes) -> None:
550
+ with self._send_guard:
551
+ try:
552
+ await self._trio_socket.send(item)
553
+ except BaseException as exc:
554
+ self._convert_socket_error(exc)
555
+
556
+
557
+ class UNIXDatagramSocket(_TrioSocketMixin[str], abc.UNIXDatagramSocket):
558
+ def __init__(self, trio_socket: TrioSocketType) -> None:
559
+ super().__init__(trio_socket)
560
+ self._receive_guard = ResourceGuard("reading from")
561
+ self._send_guard = ResourceGuard("writing to")
562
+
563
+ async def receive(self) -> UNIXDatagramPacketType:
564
+ with self._receive_guard:
565
+ try:
566
+ data, addr = await self._trio_socket.recvfrom(65536)
567
+ return data, addr
568
+ except BaseException as exc:
569
+ self._convert_socket_error(exc)
570
+
571
+ async def send(self, item: UNIXDatagramPacketType) -> None:
572
+ with self._send_guard:
573
+ try:
574
+ await self._trio_socket.sendto(*item)
575
+ except BaseException as exc:
576
+ self._convert_socket_error(exc)
577
+
578
+
579
+ class ConnectedUNIXDatagramSocket(
580
+ _TrioSocketMixin[str], abc.ConnectedUNIXDatagramSocket
581
+ ):
582
+ def __init__(self, trio_socket: TrioSocketType) -> None:
583
+ super().__init__(trio_socket)
584
+ self._receive_guard = ResourceGuard("reading from")
585
+ self._send_guard = ResourceGuard("writing to")
586
+
587
+ async def receive(self) -> bytes:
588
+ with self._receive_guard:
589
+ try:
590
+ return await self._trio_socket.recv(65536)
591
+ except BaseException as exc:
592
+ self._convert_socket_error(exc)
593
+
594
+ async def send(self, item: bytes) -> None:
595
+ with self._send_guard:
596
+ try:
597
+ await self._trio_socket.send(item)
598
+ except BaseException as exc:
599
+ self._convert_socket_error(exc)
600
+
601
+
602
+ #
603
+ # Synchronization
604
+ #
605
+
606
+
607
+ class Event(BaseEvent):
608
+ def __new__(cls) -> Event:
609
+ return object.__new__(cls)
610
+
611
+ def __init__(self) -> None:
612
+ self.__original = trio.Event()
613
+
614
+ def is_set(self) -> bool:
615
+ return self.__original.is_set()
616
+
617
+ async def wait(self) -> None:
618
+ return await self.__original.wait()
619
+
620
+ def statistics(self) -> EventStatistics:
621
+ orig_statistics = self.__original.statistics()
622
+ return EventStatistics(tasks_waiting=orig_statistics.tasks_waiting)
623
+
624
+ def set(self) -> None:
625
+ self.__original.set()
626
+
627
+
628
+ class Lock(BaseLock):
629
+ def __new__(cls, *, fast_acquire: bool = False) -> Lock:
630
+ return object.__new__(cls)
631
+
632
+ def __init__(self, *, fast_acquire: bool = False) -> None:
633
+ self._fast_acquire = fast_acquire
634
+ self.__original = trio.Lock()
635
+
636
+ @staticmethod
637
+ def _convert_runtime_error_msg(exc: RuntimeError) -> None:
638
+ if exc.args == ("attempt to re-acquire an already held Lock",):
639
+ exc.args = ("Attempted to acquire an already held Lock",)
640
+
641
+ async def acquire(self) -> None:
642
+ if not self._fast_acquire:
643
+ try:
644
+ await self.__original.acquire()
645
+ except RuntimeError as exc:
646
+ self._convert_runtime_error_msg(exc)
647
+ raise
648
+
649
+ return
650
+
651
+ # This is the "fast path" where we don't let other tasks run
652
+ await trio.lowlevel.checkpoint_if_cancelled()
653
+ try:
654
+ self.__original.acquire_nowait()
655
+ except trio.WouldBlock:
656
+ await self.__original._lot.park()
657
+ except RuntimeError as exc:
658
+ self._convert_runtime_error_msg(exc)
659
+ raise
660
+
661
+ def acquire_nowait(self) -> None:
662
+ try:
663
+ self.__original.acquire_nowait()
664
+ except trio.WouldBlock:
665
+ raise WouldBlock from None
666
+ except RuntimeError as exc:
667
+ self._convert_runtime_error_msg(exc)
668
+ raise
669
+
670
+ def locked(self) -> bool:
671
+ return self.__original.locked()
672
+
673
+ def release(self) -> None:
674
+ self.__original.release()
675
+
676
+ def statistics(self) -> LockStatistics:
677
+ orig_statistics = self.__original.statistics()
678
+ owner = TrioTaskInfo(orig_statistics.owner) if orig_statistics.owner else None
679
+ return LockStatistics(
680
+ orig_statistics.locked, owner, orig_statistics.tasks_waiting
681
+ )
682
+
683
+
684
+ class Semaphore(BaseSemaphore):
685
+ def __new__(
686
+ cls,
687
+ initial_value: int,
688
+ *,
689
+ max_value: int | None = None,
690
+ fast_acquire: bool = False,
691
+ ) -> Semaphore:
692
+ return object.__new__(cls)
693
+
694
+ def __init__(
695
+ self,
696
+ initial_value: int,
697
+ *,
698
+ max_value: int | None = None,
699
+ fast_acquire: bool = False,
700
+ ) -> None:
701
+ super().__init__(initial_value, max_value=max_value, fast_acquire=fast_acquire)
702
+ self.__original = trio.Semaphore(initial_value, max_value=max_value)
703
+
704
+ async def acquire(self) -> None:
705
+ if not self._fast_acquire:
706
+ await self.__original.acquire()
707
+ return
708
+
709
+ # This is the "fast path" where we don't let other tasks run
710
+ await trio.lowlevel.checkpoint_if_cancelled()
711
+ try:
712
+ self.__original.acquire_nowait()
713
+ except trio.WouldBlock:
714
+ await self.__original._lot.park()
715
+
716
+ def acquire_nowait(self) -> None:
717
+ try:
718
+ self.__original.acquire_nowait()
719
+ except trio.WouldBlock:
720
+ raise WouldBlock from None
721
+
722
+ @property
723
+ def max_value(self) -> int | None:
724
+ return self.__original.max_value
725
+
726
+ @property
727
+ def value(self) -> int:
728
+ return self.__original.value
729
+
730
+ def release(self) -> None:
731
+ self.__original.release()
732
+
733
+ def statistics(self) -> SemaphoreStatistics:
734
+ orig_statistics = self.__original.statistics()
735
+ return SemaphoreStatistics(orig_statistics.tasks_waiting)
736
+
737
+
738
+ class CapacityLimiter(BaseCapacityLimiter):
739
+ def __new__(
740
+ cls,
741
+ total_tokens: float | None = None,
742
+ *,
743
+ original: trio.CapacityLimiter | None = None,
744
+ ) -> CapacityLimiter:
745
+ return object.__new__(cls)
746
+
747
+ def __init__(
748
+ self,
749
+ total_tokens: float | None = None,
750
+ *,
751
+ original: trio.CapacityLimiter | None = None,
752
+ ) -> None:
753
+ if original is not None:
754
+ self.__original = original
755
+ else:
756
+ assert total_tokens is not None
757
+ self.__original = trio.CapacityLimiter(total_tokens)
758
+
759
+ async def __aenter__(self) -> None:
760
+ return await self.__original.__aenter__()
761
+
762
+ async def __aexit__(
763
+ self,
764
+ exc_type: type[BaseException] | None,
765
+ exc_val: BaseException | None,
766
+ exc_tb: TracebackType | None,
767
+ ) -> None:
768
+ await self.__original.__aexit__(exc_type, exc_val, exc_tb)
769
+
770
+ @property
771
+ def total_tokens(self) -> float:
772
+ return self.__original.total_tokens
773
+
774
+ @total_tokens.setter
775
+ def total_tokens(self, value: float) -> None:
776
+ self.__original.total_tokens = value
777
+
778
+ @property
779
+ def borrowed_tokens(self) -> int:
780
+ return self.__original.borrowed_tokens
781
+
782
+ @property
783
+ def available_tokens(self) -> float:
784
+ return self.__original.available_tokens
785
+
786
+ def acquire_nowait(self) -> None:
787
+ self.__original.acquire_nowait()
788
+
789
+ def acquire_on_behalf_of_nowait(self, borrower: object) -> None:
790
+ self.__original.acquire_on_behalf_of_nowait(borrower)
791
+
792
+ async def acquire(self) -> None:
793
+ await self.__original.acquire()
794
+
795
+ async def acquire_on_behalf_of(self, borrower: object) -> None:
796
+ await self.__original.acquire_on_behalf_of(borrower)
797
+
798
+ def release(self) -> None:
799
+ return self.__original.release()
800
+
801
+ def release_on_behalf_of(self, borrower: object) -> None:
802
+ return self.__original.release_on_behalf_of(borrower)
803
+
804
+ def statistics(self) -> CapacityLimiterStatistics:
805
+ orig = self.__original.statistics()
806
+ return CapacityLimiterStatistics(
807
+ borrowed_tokens=orig.borrowed_tokens,
808
+ total_tokens=orig.total_tokens,
809
+ borrowers=tuple(orig.borrowers),
810
+ tasks_waiting=orig.tasks_waiting,
811
+ )
812
+
813
+
814
+ _capacity_limiter_wrapper: trio.lowlevel.RunVar = RunVar("_capacity_limiter_wrapper")
815
+
816
+
817
+ #
818
+ # Signal handling
819
+ #
820
+
821
+
822
+ class _SignalReceiver:
823
+ _iterator: AsyncIterator[int]
824
+
825
+ def __init__(self, signals: tuple[Signals, ...]):
826
+ self._signals = signals
827
+
828
+ def __enter__(self) -> _SignalReceiver:
829
+ self._cm = trio.open_signal_receiver(*self._signals)
830
+ self._iterator = self._cm.__enter__()
831
+ return self
832
+
833
+ def __exit__(
834
+ self,
835
+ exc_type: type[BaseException] | None,
836
+ exc_val: BaseException | None,
837
+ exc_tb: TracebackType | None,
838
+ ) -> bool | None:
839
+ return self._cm.__exit__(exc_type, exc_val, exc_tb)
840
+
841
+ def __aiter__(self) -> _SignalReceiver:
842
+ return self
843
+
844
+ async def __anext__(self) -> Signals:
845
+ signum = await self._iterator.__anext__()
846
+ return Signals(signum)
847
+
848
+
849
+ #
850
+ # Testing and debugging
851
+ #
852
+
853
+
854
+ class TestRunner(abc.TestRunner):
855
+ def __init__(self, **options: Any) -> None:
856
+ from queue import Queue
857
+
858
+ self._call_queue: Queue[Callable[[], object]] = Queue()
859
+ self._send_stream: MemoryObjectSendStream | None = None
860
+ self._options = options
861
+
862
+ def __exit__(
863
+ self,
864
+ exc_type: type[BaseException] | None,
865
+ exc_val: BaseException | None,
866
+ exc_tb: types.TracebackType | None,
867
+ ) -> None:
868
+ if self._send_stream:
869
+ self._send_stream.close()
870
+ while self._send_stream is not None:
871
+ self._call_queue.get()()
872
+
873
+ async def _run_tests_and_fixtures(self) -> None:
874
+ self._send_stream, receive_stream = create_memory_object_stream(1)
875
+ with receive_stream:
876
+ async for coro, outcome_holder in receive_stream:
877
+ try:
878
+ retval = await coro
879
+ except BaseException as exc:
880
+ outcome_holder.append(Error(exc))
881
+ else:
882
+ outcome_holder.append(Value(retval))
883
+
884
+ def _main_task_finished(self, outcome: object) -> None:
885
+ self._send_stream = None
886
+
887
+ def _call_in_runner_task(
888
+ self,
889
+ func: Callable[P, Awaitable[T_Retval]],
890
+ *args: P.args,
891
+ **kwargs: P.kwargs,
892
+ ) -> T_Retval:
893
+ if self._send_stream is None:
894
+ trio.lowlevel.start_guest_run(
895
+ self._run_tests_and_fixtures,
896
+ run_sync_soon_threadsafe=self._call_queue.put,
897
+ done_callback=self._main_task_finished,
898
+ **self._options,
899
+ )
900
+ while self._send_stream is None:
901
+ self._call_queue.get()()
902
+
903
+ outcome_holder: list[Outcome] = []
904
+ self._send_stream.send_nowait((func(*args, **kwargs), outcome_holder))
905
+ while not outcome_holder:
906
+ self._call_queue.get()()
907
+
908
+ return outcome_holder[0].unwrap()
909
+
910
+ def run_asyncgen_fixture(
911
+ self,
912
+ fixture_func: Callable[..., AsyncGenerator[T_Retval, Any]],
913
+ kwargs: dict[str, Any],
914
+ ) -> Iterable[T_Retval]:
915
+ asyncgen = fixture_func(**kwargs)
916
+ fixturevalue: T_Retval = self._call_in_runner_task(asyncgen.asend, None)
917
+
918
+ yield fixturevalue
919
+
920
+ try:
921
+ self._call_in_runner_task(asyncgen.asend, None)
922
+ except StopAsyncIteration:
923
+ pass
924
+ else:
925
+ self._call_in_runner_task(asyncgen.aclose)
926
+ raise RuntimeError("Async generator fixture did not stop")
927
+
928
+ def run_fixture(
929
+ self,
930
+ fixture_func: Callable[..., Coroutine[Any, Any, T_Retval]],
931
+ kwargs: dict[str, Any],
932
+ ) -> T_Retval:
933
+ return self._call_in_runner_task(fixture_func, **kwargs)
934
+
935
+ def run_test(
936
+ self, test_func: Callable[..., Coroutine[Any, Any, Any]], kwargs: dict[str, Any]
937
+ ) -> None:
938
+ self._call_in_runner_task(test_func, **kwargs)
939
+
940
+
941
+ class TrioTaskInfo(TaskInfo):
942
+ def __init__(self, task: trio.lowlevel.Task):
943
+ parent_id = None
944
+ if task.parent_nursery and task.parent_nursery.parent_task:
945
+ parent_id = id(task.parent_nursery.parent_task)
946
+
947
+ super().__init__(id(task), parent_id, task.name, task.coro)
948
+ self._task = weakref.proxy(task)
949
+
950
+ def has_pending_cancellation(self) -> bool:
951
+ try:
952
+ return self._task._cancel_status.effectively_cancelled
953
+ except ReferenceError:
954
+ # If the task is no longer around, it surely doesn't have a cancellation
955
+ # pending
956
+ return False
957
+
958
+
959
+ class TrioBackend(AsyncBackend):
960
+ @classmethod
961
+ def run(
962
+ cls,
963
+ func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]],
964
+ args: tuple[Unpack[PosArgsT]],
965
+ kwargs: dict[str, Any],
966
+ options: dict[str, Any],
967
+ ) -> T_Retval:
968
+ return trio.run(func, *args)
969
+
970
+ @classmethod
971
+ def current_token(cls) -> object:
972
+ return trio.lowlevel.current_trio_token()
973
+
974
+ @classmethod
975
+ def current_time(cls) -> float:
976
+ return trio.current_time()
977
+
978
+ @classmethod
979
+ def cancelled_exception_class(cls) -> type[BaseException]:
980
+ return trio.Cancelled
981
+
982
+ @classmethod
983
+ async def checkpoint(cls) -> None:
984
+ await trio.lowlevel.checkpoint()
985
+
986
+ @classmethod
987
+ async def checkpoint_if_cancelled(cls) -> None:
988
+ await trio.lowlevel.checkpoint_if_cancelled()
989
+
990
+ @classmethod
991
+ async def cancel_shielded_checkpoint(cls) -> None:
992
+ await trio.lowlevel.cancel_shielded_checkpoint()
993
+
994
+ @classmethod
995
+ async def sleep(cls, delay: float) -> None:
996
+ await trio.sleep(delay)
997
+
998
+ @classmethod
999
+ def create_cancel_scope(
1000
+ cls, *, deadline: float = math.inf, shield: bool = False
1001
+ ) -> abc.CancelScope:
1002
+ return CancelScope(deadline=deadline, shield=shield)
1003
+
1004
+ @classmethod
1005
+ def current_effective_deadline(cls) -> float:
1006
+ return trio.current_effective_deadline()
1007
+
1008
+ @classmethod
1009
+ def create_task_group(cls) -> abc.TaskGroup:
1010
+ return TaskGroup()
1011
+
1012
+ @classmethod
1013
+ def create_event(cls) -> abc.Event:
1014
+ return Event()
1015
+
1016
+ @classmethod
1017
+ def create_lock(cls, *, fast_acquire: bool) -> Lock:
1018
+ return Lock(fast_acquire=fast_acquire)
1019
+
1020
+ @classmethod
1021
+ def create_semaphore(
1022
+ cls,
1023
+ initial_value: int,
1024
+ *,
1025
+ max_value: int | None = None,
1026
+ fast_acquire: bool = False,
1027
+ ) -> abc.Semaphore:
1028
+ return Semaphore(initial_value, max_value=max_value, fast_acquire=fast_acquire)
1029
+
1030
+ @classmethod
1031
+ def create_capacity_limiter(cls, total_tokens: float) -> CapacityLimiter:
1032
+ return CapacityLimiter(total_tokens)
1033
+
1034
+ @classmethod
1035
+ async def run_sync_in_worker_thread(
1036
+ cls,
1037
+ func: Callable[[Unpack[PosArgsT]], T_Retval],
1038
+ args: tuple[Unpack[PosArgsT]],
1039
+ abandon_on_cancel: bool = False,
1040
+ limiter: abc.CapacityLimiter | None = None,
1041
+ ) -> T_Retval:
1042
+ def wrapper() -> T_Retval:
1043
+ with claim_worker_thread(TrioBackend, token):
1044
+ return func(*args)
1045
+
1046
+ token = TrioBackend.current_token()
1047
+ return await run_sync(
1048
+ wrapper,
1049
+ abandon_on_cancel=abandon_on_cancel,
1050
+ limiter=cast(trio.CapacityLimiter, limiter),
1051
+ )
1052
+
1053
+ @classmethod
1054
+ def check_cancelled(cls) -> None:
1055
+ trio.from_thread.check_cancelled()
1056
+
1057
+ @classmethod
1058
+ def run_async_from_thread(
1059
+ cls,
1060
+ func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]],
1061
+ args: tuple[Unpack[PosArgsT]],
1062
+ token: object,
1063
+ ) -> T_Retval:
1064
+ trio_token = cast("trio.lowlevel.TrioToken | None", token)
1065
+ try:
1066
+ return trio.from_thread.run(func, *args, trio_token=trio_token)
1067
+ except trio.RunFinishedError:
1068
+ raise RunFinishedError from None
1069
+
1070
+ @classmethod
1071
+ def run_sync_from_thread(
1072
+ cls,
1073
+ func: Callable[[Unpack[PosArgsT]], T_Retval],
1074
+ args: tuple[Unpack[PosArgsT]],
1075
+ token: object,
1076
+ ) -> T_Retval:
1077
+ trio_token = cast("trio.lowlevel.TrioToken | None", token)
1078
+ try:
1079
+ return trio.from_thread.run_sync(func, *args, trio_token=trio_token)
1080
+ except trio.RunFinishedError:
1081
+ raise RunFinishedError from None
1082
+
1083
+ @classmethod
1084
+ async def open_process(
1085
+ cls,
1086
+ command: StrOrBytesPath | Sequence[StrOrBytesPath],
1087
+ *,
1088
+ stdin: int | IO[Any] | None,
1089
+ stdout: int | IO[Any] | None,
1090
+ stderr: int | IO[Any] | None,
1091
+ **kwargs: Any,
1092
+ ) -> Process:
1093
+ def convert_item(item: StrOrBytesPath) -> str:
1094
+ str_or_bytes = os.fspath(item)
1095
+ if isinstance(str_or_bytes, str):
1096
+ return str_or_bytes
1097
+ else:
1098
+ return os.fsdecode(str_or_bytes)
1099
+
1100
+ if isinstance(command, (str, bytes, PathLike)):
1101
+ process = await trio.lowlevel.open_process(
1102
+ convert_item(command),
1103
+ stdin=stdin,
1104
+ stdout=stdout,
1105
+ stderr=stderr,
1106
+ shell=True,
1107
+ **kwargs,
1108
+ )
1109
+ else:
1110
+ process = await trio.lowlevel.open_process(
1111
+ [convert_item(item) for item in command],
1112
+ stdin=stdin,
1113
+ stdout=stdout,
1114
+ stderr=stderr,
1115
+ shell=False,
1116
+ **kwargs,
1117
+ )
1118
+
1119
+ stdin_stream = SendStreamWrapper(process.stdin) if process.stdin else None
1120
+ stdout_stream = ReceiveStreamWrapper(process.stdout) if process.stdout else None
1121
+ stderr_stream = ReceiveStreamWrapper(process.stderr) if process.stderr else None
1122
+ return Process(process, stdin_stream, stdout_stream, stderr_stream)
1123
+
1124
+ @classmethod
1125
+ def setup_process_pool_exit_at_shutdown(cls, workers: set[abc.Process]) -> None:
1126
+ trio.lowlevel.spawn_system_task(_shutdown_process_pool, workers)
1127
+
1128
+ @classmethod
1129
+ async def connect_tcp(
1130
+ cls, host: str, port: int, local_address: IPSockAddrType | None = None
1131
+ ) -> SocketStream:
1132
+ family = socket.AF_INET6 if ":" in host else socket.AF_INET
1133
+ trio_socket = trio.socket.socket(family)
1134
+ trio_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
1135
+ if local_address:
1136
+ await trio_socket.bind(local_address)
1137
+
1138
+ try:
1139
+ await trio_socket.connect((host, port))
1140
+ except BaseException:
1141
+ trio_socket.close()
1142
+ raise
1143
+
1144
+ return SocketStream(trio_socket)
1145
+
1146
+ @classmethod
1147
+ async def connect_unix(cls, path: str | bytes) -> abc.UNIXSocketStream:
1148
+ trio_socket = trio.socket.socket(socket.AF_UNIX)
1149
+ try:
1150
+ await trio_socket.connect(path)
1151
+ except BaseException:
1152
+ trio_socket.close()
1153
+ raise
1154
+
1155
+ return UNIXSocketStream(trio_socket)
1156
+
1157
+ @classmethod
1158
+ def create_tcp_listener(cls, sock: socket.socket) -> abc.SocketListener:
1159
+ return TCPSocketListener(sock)
1160
+
1161
+ @classmethod
1162
+ def create_unix_listener(cls, sock: socket.socket) -> abc.SocketListener:
1163
+ return UNIXSocketListener(sock)
1164
+
1165
+ @classmethod
1166
+ async def create_udp_socket(
1167
+ cls,
1168
+ family: socket.AddressFamily,
1169
+ local_address: IPSockAddrType | None,
1170
+ remote_address: IPSockAddrType | None,
1171
+ reuse_port: bool,
1172
+ ) -> UDPSocket | ConnectedUDPSocket:
1173
+ trio_socket = trio.socket.socket(family=family, type=socket.SOCK_DGRAM)
1174
+
1175
+ if reuse_port:
1176
+ trio_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
1177
+
1178
+ if local_address:
1179
+ await trio_socket.bind(local_address)
1180
+
1181
+ if remote_address:
1182
+ await trio_socket.connect(remote_address)
1183
+ return ConnectedUDPSocket(trio_socket)
1184
+ else:
1185
+ return UDPSocket(trio_socket)
1186
+
1187
+ @classmethod
1188
+ @overload
1189
+ async def create_unix_datagram_socket(
1190
+ cls, raw_socket: socket.socket, remote_path: None
1191
+ ) -> abc.UNIXDatagramSocket: ...
1192
+
1193
+ @classmethod
1194
+ @overload
1195
+ async def create_unix_datagram_socket(
1196
+ cls, raw_socket: socket.socket, remote_path: str | bytes
1197
+ ) -> abc.ConnectedUNIXDatagramSocket: ...
1198
+
1199
+ @classmethod
1200
+ async def create_unix_datagram_socket(
1201
+ cls, raw_socket: socket.socket, remote_path: str | bytes | None
1202
+ ) -> abc.UNIXDatagramSocket | abc.ConnectedUNIXDatagramSocket:
1203
+ trio_socket = trio.socket.from_stdlib_socket(raw_socket)
1204
+
1205
+ if remote_path:
1206
+ await trio_socket.connect(remote_path)
1207
+ return ConnectedUNIXDatagramSocket(trio_socket)
1208
+ else:
1209
+ return UNIXDatagramSocket(trio_socket)
1210
+
1211
+ @classmethod
1212
+ async def getaddrinfo(
1213
+ cls,
1214
+ host: bytes | str | None,
1215
+ port: str | int | None,
1216
+ *,
1217
+ family: int | AddressFamily = 0,
1218
+ type: int | SocketKind = 0,
1219
+ proto: int = 0,
1220
+ flags: int = 0,
1221
+ ) -> Sequence[
1222
+ tuple[
1223
+ AddressFamily,
1224
+ SocketKind,
1225
+ int,
1226
+ str,
1227
+ tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes],
1228
+ ]
1229
+ ]:
1230
+ return await trio.socket.getaddrinfo(host, port, family, type, proto, flags)
1231
+
1232
+ @classmethod
1233
+ async def getnameinfo(
1234
+ cls, sockaddr: IPSockAddrType, flags: int = 0
1235
+ ) -> tuple[str, str]:
1236
+ return await trio.socket.getnameinfo(sockaddr, flags)
1237
+
1238
+ @classmethod
1239
+ async def wait_readable(cls, obj: FileDescriptorLike) -> None:
1240
+ try:
1241
+ await wait_readable(obj)
1242
+ except trio.ClosedResourceError as exc:
1243
+ raise ClosedResourceError().with_traceback(exc.__traceback__) from None
1244
+ except trio.BusyResourceError:
1245
+ raise BusyResourceError("reading from") from None
1246
+
1247
+ @classmethod
1248
+ async def wait_writable(cls, obj: FileDescriptorLike) -> None:
1249
+ try:
1250
+ await wait_writable(obj)
1251
+ except trio.ClosedResourceError as exc:
1252
+ raise ClosedResourceError().with_traceback(exc.__traceback__) from None
1253
+ except trio.BusyResourceError:
1254
+ raise BusyResourceError("writing to") from None
1255
+
1256
+ @classmethod
1257
+ def notify_closing(cls, obj: FileDescriptorLike) -> None:
1258
+ notify_closing(obj)
1259
+
1260
+ @classmethod
1261
+ async def wrap_listener_socket(cls, sock: socket.socket) -> abc.SocketListener:
1262
+ return TCPSocketListener(sock)
1263
+
1264
+ @classmethod
1265
+ async def wrap_stream_socket(cls, sock: socket.socket) -> SocketStream:
1266
+ trio_sock = trio.socket.from_stdlib_socket(sock)
1267
+ return SocketStream(trio_sock)
1268
+
1269
+ @classmethod
1270
+ async def wrap_unix_stream_socket(cls, sock: socket.socket) -> UNIXSocketStream:
1271
+ trio_sock = trio.socket.from_stdlib_socket(sock)
1272
+ return UNIXSocketStream(trio_sock)
1273
+
1274
+ @classmethod
1275
+ async def wrap_udp_socket(cls, sock: socket.socket) -> UDPSocket:
1276
+ trio_sock = trio.socket.from_stdlib_socket(sock)
1277
+ return UDPSocket(trio_sock)
1278
+
1279
+ @classmethod
1280
+ async def wrap_connected_udp_socket(cls, sock: socket.socket) -> ConnectedUDPSocket:
1281
+ trio_sock = trio.socket.from_stdlib_socket(sock)
1282
+ return ConnectedUDPSocket(trio_sock)
1283
+
1284
+ @classmethod
1285
+ async def wrap_unix_datagram_socket(cls, sock: socket.socket) -> UNIXDatagramSocket:
1286
+ trio_sock = trio.socket.from_stdlib_socket(sock)
1287
+ return UNIXDatagramSocket(trio_sock)
1288
+
1289
+ @classmethod
1290
+ async def wrap_connected_unix_datagram_socket(
1291
+ cls, sock: socket.socket
1292
+ ) -> ConnectedUNIXDatagramSocket:
1293
+ trio_sock = trio.socket.from_stdlib_socket(sock)
1294
+ return ConnectedUNIXDatagramSocket(trio_sock)
1295
+
1296
+ @classmethod
1297
+ def current_default_thread_limiter(cls) -> CapacityLimiter:
1298
+ try:
1299
+ return _capacity_limiter_wrapper.get()
1300
+ except LookupError:
1301
+ limiter = CapacityLimiter(
1302
+ original=trio.to_thread.current_default_thread_limiter()
1303
+ )
1304
+ _capacity_limiter_wrapper.set(limiter)
1305
+ return limiter
1306
+
1307
+ @classmethod
1308
+ def open_signal_receiver(
1309
+ cls, *signals: Signals
1310
+ ) -> AbstractContextManager[AsyncIterator[Signals]]:
1311
+ return _SignalReceiver(signals)
1312
+
1313
+ @classmethod
1314
+ def get_current_task(cls) -> TaskInfo:
1315
+ task = current_task()
1316
+ return TrioTaskInfo(task)
1317
+
1318
+ @classmethod
1319
+ def get_running_tasks(cls) -> Sequence[TaskInfo]:
1320
+ root_task = current_root_task()
1321
+ assert root_task
1322
+ task_infos = [TrioTaskInfo(root_task)]
1323
+ nurseries = root_task.child_nurseries
1324
+ while nurseries:
1325
+ new_nurseries: list[trio.Nursery] = []
1326
+ for nursery in nurseries:
1327
+ for task in nursery.child_tasks:
1328
+ task_infos.append(TrioTaskInfo(task))
1329
+ new_nurseries.extend(task.child_nurseries)
1330
+
1331
+ nurseries = new_nurseries
1332
+
1333
+ return task_infos
1334
+
1335
+ @classmethod
1336
+ async def wait_all_tasks_blocked(cls) -> None:
1337
+ from trio.testing import wait_all_tasks_blocked
1338
+
1339
+ await wait_all_tasks_blocked()
1340
+
1341
+ @classmethod
1342
+ def create_test_runner(cls, options: dict[str, Any]) -> TestRunner:
1343
+ return TestRunner(**options)
1344
+
1345
+
1346
+ backend_class = TrioBackend
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/__init__.py ADDED
File without changes
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_asyncio_selector_thread.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import socket
5
+ import threading
6
+ from collections.abc import Callable
7
+ from selectors import EVENT_READ, EVENT_WRITE, DefaultSelector
8
+ from typing import TYPE_CHECKING, Any
9
+
10
+ if TYPE_CHECKING:
11
+ from _typeshed import FileDescriptorLike
12
+
13
+ _selector_lock = threading.Lock()
14
+ _selector: Selector | None = None
15
+
16
+
17
+ class Selector:
18
+ def __init__(self) -> None:
19
+ self._thread = threading.Thread(target=self.run, name="AnyIO socket selector")
20
+ self._selector = DefaultSelector()
21
+ self._send, self._receive = socket.socketpair()
22
+ self._send.setblocking(False)
23
+ self._receive.setblocking(False)
24
+ # This somewhat reduces the amount of memory wasted queueing up data
25
+ # for wakeups. With these settings, maximum number of 1-byte sends
26
+ # before getting BlockingIOError:
27
+ # Linux 4.8: 6
28
+ # macOS (darwin 15.5): 1
29
+ # Windows 10: 525347
30
+ # Windows you're weird. (And on Windows setting SNDBUF to 0 makes send
31
+ # blocking, even on non-blocking sockets, so don't do that.)
32
+ self._receive.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1)
33
+ self._send.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1)
34
+ # On Windows this is a TCP socket so this might matter. On other
35
+ # platforms this fails b/c AF_UNIX sockets aren't actually TCP.
36
+ try:
37
+ self._send.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
38
+ except OSError:
39
+ pass
40
+
41
+ self._selector.register(self._receive, EVENT_READ)
42
+ self._closed = False
43
+
44
+ def start(self) -> None:
45
+ self._thread.start()
46
+ threading._register_atexit(self._stop) # type: ignore[attr-defined]
47
+
48
+ def _stop(self) -> None:
49
+ global _selector
50
+ self._closed = True
51
+ self._notify_self()
52
+ self._send.close()
53
+ self._thread.join()
54
+ self._selector.unregister(self._receive)
55
+ self._receive.close()
56
+ self._selector.close()
57
+ _selector = None
58
+ assert not self._selector.get_map(), (
59
+ "selector still has registered file descriptors after shutdown"
60
+ )
61
+
62
+ def _notify_self(self) -> None:
63
+ try:
64
+ self._send.send(b"\x00")
65
+ except BlockingIOError:
66
+ pass
67
+
68
+ def add_reader(self, fd: FileDescriptorLike, callback: Callable[[], Any]) -> None:
69
+ loop = asyncio.get_running_loop()
70
+ try:
71
+ key = self._selector.get_key(fd)
72
+ except KeyError:
73
+ self._selector.register(fd, EVENT_READ, {EVENT_READ: (loop, callback)})
74
+ else:
75
+ if EVENT_READ in key.data:
76
+ raise ValueError(
77
+ "this file descriptor is already registered for reading"
78
+ )
79
+
80
+ key.data[EVENT_READ] = loop, callback
81
+ self._selector.modify(fd, key.events | EVENT_READ, key.data)
82
+
83
+ self._notify_self()
84
+
85
+ def add_writer(self, fd: FileDescriptorLike, callback: Callable[[], Any]) -> None:
86
+ loop = asyncio.get_running_loop()
87
+ try:
88
+ key = self._selector.get_key(fd)
89
+ except KeyError:
90
+ self._selector.register(fd, EVENT_WRITE, {EVENT_WRITE: (loop, callback)})
91
+ else:
92
+ if EVENT_WRITE in key.data:
93
+ raise ValueError(
94
+ "this file descriptor is already registered for writing"
95
+ )
96
+
97
+ key.data[EVENT_WRITE] = loop, callback
98
+ self._selector.modify(fd, key.events | EVENT_WRITE, key.data)
99
+
100
+ self._notify_self()
101
+
102
+ def remove_reader(self, fd: FileDescriptorLike) -> bool:
103
+ try:
104
+ key = self._selector.get_key(fd)
105
+ except KeyError:
106
+ return False
107
+
108
+ if new_events := key.events ^ EVENT_READ:
109
+ del key.data[EVENT_READ]
110
+ self._selector.modify(fd, new_events, key.data)
111
+ else:
112
+ self._selector.unregister(fd)
113
+
114
+ return True
115
+
116
+ def remove_writer(self, fd: FileDescriptorLike) -> bool:
117
+ try:
118
+ key = self._selector.get_key(fd)
119
+ except KeyError:
120
+ return False
121
+
122
+ if new_events := key.events ^ EVENT_WRITE:
123
+ del key.data[EVENT_WRITE]
124
+ self._selector.modify(fd, new_events, key.data)
125
+ else:
126
+ self._selector.unregister(fd)
127
+
128
+ return True
129
+
130
+ def run(self) -> None:
131
+ while not self._closed:
132
+ for key, events in self._selector.select():
133
+ if key.fileobj is self._receive:
134
+ try:
135
+ while self._receive.recv(4096):
136
+ pass
137
+ except BlockingIOError:
138
+ pass
139
+
140
+ continue
141
+
142
+ if events & EVENT_READ:
143
+ loop, callback = key.data[EVENT_READ]
144
+ self.remove_reader(key.fd)
145
+ try:
146
+ loop.call_soon_threadsafe(callback)
147
+ except RuntimeError:
148
+ pass # the loop was already closed
149
+
150
+ if events & EVENT_WRITE:
151
+ loop, callback = key.data[EVENT_WRITE]
152
+ self.remove_writer(key.fd)
153
+ try:
154
+ loop.call_soon_threadsafe(callback)
155
+ except RuntimeError:
156
+ pass # the loop was already closed
157
+
158
+
159
+ def get_selector() -> Selector:
160
+ global _selector
161
+
162
+ with _selector_lock:
163
+ if _selector is None:
164
+ _selector = Selector()
165
+ _selector.start()
166
+
167
+ return _selector
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_contextmanagers.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from abc import abstractmethod
4
+ from contextlib import AbstractAsyncContextManager, AbstractContextManager
5
+ from inspect import isasyncgen, iscoroutine, isgenerator
6
+ from types import TracebackType
7
+ from typing import Protocol, TypeVar, cast, final
8
+
9
+ _T_co = TypeVar("_T_co", covariant=True)
10
+ _ExitT_co = TypeVar("_ExitT_co", covariant=True, bound="bool | None")
11
+
12
+
13
+ class _SupportsCtxMgr(Protocol[_T_co, _ExitT_co]):
14
+ def __contextmanager__(self) -> AbstractContextManager[_T_co, _ExitT_co]: ...
15
+
16
+
17
+ class _SupportsAsyncCtxMgr(Protocol[_T_co, _ExitT_co]):
18
+ def __asynccontextmanager__(
19
+ self,
20
+ ) -> AbstractAsyncContextManager[_T_co, _ExitT_co]: ...
21
+
22
+
23
+ class ContextManagerMixin:
24
+ """
25
+ Mixin class providing context manager functionality via a generator-based
26
+ implementation.
27
+
28
+ This class allows you to implement a context manager via :meth:`__contextmanager__`
29
+ which should return a generator. The mechanics are meant to mirror those of
30
+ :func:`@contextmanager <contextlib.contextmanager>`.
31
+
32
+ .. note:: Classes using this mix-in are not reentrant as context managers, meaning
33
+ that once you enter it, you can't re-enter before first exiting it.
34
+
35
+ .. seealso:: :doc:`contextmanagers`
36
+ """
37
+
38
+ __cm: AbstractContextManager[object, bool | None] | None = None
39
+
40
+ @final
41
+ def __enter__(self: _SupportsCtxMgr[_T_co, bool | None]) -> _T_co:
42
+ # Needed for mypy to assume self still has the __cm member
43
+ assert isinstance(self, ContextManagerMixin)
44
+ if self.__cm is not None:
45
+ raise RuntimeError(
46
+ f"this {self.__class__.__qualname__} has already been entered"
47
+ )
48
+
49
+ cm = self.__contextmanager__()
50
+ if not isinstance(cm, AbstractContextManager):
51
+ if isgenerator(cm):
52
+ raise TypeError(
53
+ "__contextmanager__() returned a generator object instead of "
54
+ "a context manager. Did you forget to add the @contextmanager "
55
+ "decorator?"
56
+ )
57
+
58
+ raise TypeError(
59
+ f"__contextmanager__() did not return a context manager object, "
60
+ f"but {cm.__class__!r}"
61
+ )
62
+
63
+ if cm is self:
64
+ raise TypeError(
65
+ f"{self.__class__.__qualname__}.__contextmanager__() returned "
66
+ f"self. Did you forget to add the @contextmanager decorator and a "
67
+ f"'yield' statement?"
68
+ )
69
+
70
+ value = cm.__enter__()
71
+ self.__cm = cm
72
+ return value
73
+
74
+ @final
75
+ def __exit__(
76
+ self: _SupportsCtxMgr[object, _ExitT_co],
77
+ exc_type: type[BaseException] | None,
78
+ exc_val: BaseException | None,
79
+ exc_tb: TracebackType | None,
80
+ ) -> _ExitT_co:
81
+ # Needed for mypy to assume self still has the __cm member
82
+ assert isinstance(self, ContextManagerMixin)
83
+ if self.__cm is None:
84
+ raise RuntimeError(
85
+ f"this {self.__class__.__qualname__} has not been entered yet"
86
+ )
87
+
88
+ # Prevent circular references
89
+ cm = self.__cm
90
+ del self.__cm
91
+
92
+ return cast(_ExitT_co, cm.__exit__(exc_type, exc_val, exc_tb))
93
+
94
+ @abstractmethod
95
+ def __contextmanager__(self) -> AbstractContextManager[object, bool | None]:
96
+ """
97
+ Implement your context manager logic here.
98
+
99
+ This method **must** be decorated with
100
+ :func:`@contextmanager <contextlib.contextmanager>`.
101
+
102
+ .. note:: Remember that the ``yield`` will raise any exception raised in the
103
+ enclosed context block, so use a ``finally:`` block to clean up resources!
104
+
105
+ :return: a context manager object
106
+ """
107
+
108
+
109
+ class AsyncContextManagerMixin:
110
+ """
111
+ Mixin class providing async context manager functionality via a generator-based
112
+ implementation.
113
+
114
+ This class allows you to implement a context manager via
115
+ :meth:`__asynccontextmanager__`. The mechanics are meant to mirror those of
116
+ :func:`@asynccontextmanager <contextlib.asynccontextmanager>`.
117
+
118
+ .. note:: Classes using this mix-in are not reentrant as context managers, meaning
119
+ that once you enter it, you can't re-enter before first exiting it.
120
+
121
+ .. seealso:: :doc:`contextmanagers`
122
+ """
123
+
124
+ __cm: AbstractAsyncContextManager[object, bool | None] | None = None
125
+
126
+ @final
127
+ async def __aenter__(self: _SupportsAsyncCtxMgr[_T_co, bool | None]) -> _T_co:
128
+ # Needed for mypy to assume self still has the __cm member
129
+ assert isinstance(self, AsyncContextManagerMixin)
130
+ if self.__cm is not None:
131
+ raise RuntimeError(
132
+ f"this {self.__class__.__qualname__} has already been entered"
133
+ )
134
+
135
+ cm = self.__asynccontextmanager__()
136
+ if not isinstance(cm, AbstractAsyncContextManager):
137
+ if isasyncgen(cm):
138
+ raise TypeError(
139
+ "__asynccontextmanager__() returned an async generator instead of "
140
+ "an async context manager. Did you forget to add the "
141
+ "@asynccontextmanager decorator?"
142
+ )
143
+ elif iscoroutine(cm):
144
+ cm.close()
145
+ raise TypeError(
146
+ "__asynccontextmanager__() returned a coroutine object instead of "
147
+ "an async context manager. Did you forget to add the "
148
+ "@asynccontextmanager decorator and a 'yield' statement?"
149
+ )
150
+
151
+ raise TypeError(
152
+ f"__asynccontextmanager__() did not return an async context manager, "
153
+ f"but {cm.__class__!r}"
154
+ )
155
+
156
+ if cm is self:
157
+ raise TypeError(
158
+ f"{self.__class__.__qualname__}.__asynccontextmanager__() returned "
159
+ f"self. Did you forget to add the @asynccontextmanager decorator and a "
160
+ f"'yield' statement?"
161
+ )
162
+
163
+ value = await cm.__aenter__()
164
+ self.__cm = cm
165
+ return value
166
+
167
+ @final
168
+ async def __aexit__(
169
+ self: _SupportsAsyncCtxMgr[object, _ExitT_co],
170
+ exc_type: type[BaseException] | None,
171
+ exc_val: BaseException | None,
172
+ exc_tb: TracebackType | None,
173
+ ) -> _ExitT_co:
174
+ assert isinstance(self, AsyncContextManagerMixin)
175
+ if self.__cm is None:
176
+ raise RuntimeError(
177
+ f"this {self.__class__.__qualname__} has not been entered yet"
178
+ )
179
+
180
+ # Prevent circular references
181
+ cm = self.__cm
182
+ del self.__cm
183
+
184
+ return cast(_ExitT_co, await cm.__aexit__(exc_type, exc_val, exc_tb))
185
+
186
+ @abstractmethod
187
+ def __asynccontextmanager__(
188
+ self,
189
+ ) -> AbstractAsyncContextManager[object, bool | None]:
190
+ """
191
+ Implement your async context manager logic here.
192
+
193
+ This method **must** be decorated with
194
+ :func:`@asynccontextmanager <contextlib.asynccontextmanager>`.
195
+
196
+ .. note:: Remember that the ``yield`` will raise any exception raised in the
197
+ enclosed context block, so use a ``finally:`` block to clean up resources!
198
+
199
+ :return: an async context manager object
200
+ """
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_eventloop.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import sys
5
+ import threading
6
+ from collections.abc import Awaitable, Callable, Generator
7
+ from contextlib import contextmanager
8
+ from contextvars import Token
9
+ from importlib import import_module
10
+ from typing import TYPE_CHECKING, Any, TypeVar
11
+
12
+ from ._exceptions import NoEventLoopError
13
+
14
+ if sys.version_info >= (3, 11):
15
+ from typing import TypeVarTuple, Unpack
16
+ else:
17
+ from typing_extensions import TypeVarTuple, Unpack
18
+
19
+ sniffio: Any
20
+ try:
21
+ import sniffio
22
+ except ModuleNotFoundError:
23
+ sniffio = None
24
+
25
+ if TYPE_CHECKING:
26
+ from ..abc import AsyncBackend
27
+
28
+ # This must be updated when new backends are introduced
29
+ BACKENDS = "asyncio", "trio"
30
+
31
+ T_Retval = TypeVar("T_Retval")
32
+ PosArgsT = TypeVarTuple("PosArgsT")
33
+
34
+ threadlocals = threading.local()
35
+ loaded_backends: dict[str, type[AsyncBackend]] = {}
36
+
37
+
38
+ def run(
39
+ func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]],
40
+ *args: Unpack[PosArgsT],
41
+ backend: str = "asyncio",
42
+ backend_options: dict[str, Any] | None = None,
43
+ ) -> T_Retval:
44
+ """
45
+ Run the given coroutine function in an asynchronous event loop.
46
+
47
+ The current thread must not be already running an event loop.
48
+
49
+ :param func: a coroutine function
50
+ :param args: positional arguments to ``func``
51
+ :param backend: name of the asynchronous event loop implementation – currently
52
+ either ``asyncio`` or ``trio``
53
+ :param backend_options: keyword arguments to call the backend ``run()``
54
+ implementation with (documented :ref:`here <backend options>`)
55
+ :return: the return value of the coroutine function
56
+ :raises RuntimeError: if an asynchronous event loop is already running in this
57
+ thread
58
+ :raises LookupError: if the named backend is not found
59
+
60
+ """
61
+ if asynclib_name := current_async_library():
62
+ raise RuntimeError(f"Already running {asynclib_name} in this thread")
63
+
64
+ try:
65
+ async_backend = get_async_backend(backend)
66
+ except ImportError as exc:
67
+ raise LookupError(f"No such backend: {backend}") from exc
68
+
69
+ token = None
70
+ if asynclib_name is None:
71
+ # Since we're in control of the event loop, we can cache the name of the async
72
+ # library
73
+ token = set_current_async_library(backend)
74
+
75
+ try:
76
+ backend_options = backend_options or {}
77
+ return async_backend.run(func, args, {}, backend_options)
78
+ finally:
79
+ reset_current_async_library(token)
80
+
81
+
82
+ async def sleep(delay: float) -> None:
83
+ """
84
+ Pause the current task for the specified duration.
85
+
86
+ :param delay: the duration, in seconds
87
+
88
+ """
89
+ return await get_async_backend().sleep(delay)
90
+
91
+
92
+ async def sleep_forever() -> None:
93
+ """
94
+ Pause the current task until it's cancelled.
95
+
96
+ This is a shortcut for ``sleep(math.inf)``.
97
+
98
+ .. versionadded:: 3.1
99
+
100
+ """
101
+ await sleep(math.inf)
102
+
103
+
104
+ async def sleep_until(deadline: float) -> None:
105
+ """
106
+ Pause the current task until the given time.
107
+
108
+ :param deadline: the absolute time to wake up at (according to the internal
109
+ monotonic clock of the event loop)
110
+
111
+ .. versionadded:: 3.1
112
+
113
+ """
114
+ now = current_time()
115
+ await sleep(max(deadline - now, 0))
116
+
117
+
118
+ def current_time() -> float:
119
+ """
120
+ Return the current value of the event loop's internal clock.
121
+
122
+ :return: the clock value (seconds)
123
+ :raises NoEventLoopError: if no supported asynchronous event loop is running in the
124
+ current thread
125
+
126
+ """
127
+ return get_async_backend().current_time()
128
+
129
+
130
+ def get_all_backends() -> tuple[str, ...]:
131
+ """Return a tuple of the names of all built-in backends."""
132
+ return BACKENDS
133
+
134
+
135
+ def get_available_backends() -> tuple[str, ...]:
136
+ """
137
+ Test for the availability of built-in backends.
138
+
139
+ :return a tuple of the built-in backend names that were successfully imported
140
+
141
+ .. versionadded:: 4.12
142
+
143
+ """
144
+ available_backends: list[str] = []
145
+ for backend_name in get_all_backends():
146
+ try:
147
+ get_async_backend(backend_name)
148
+ except ImportError:
149
+ continue
150
+
151
+ available_backends.append(backend_name)
152
+
153
+ return tuple(available_backends)
154
+
155
+
156
+ def get_cancelled_exc_class() -> type[BaseException]:
157
+ """
158
+ Return the current async library's cancellation exception class.
159
+
160
+ :raises NoEventLoopError: if no supported asynchronous event loop is running in the
161
+ current thread
162
+
163
+ """
164
+ return get_async_backend().cancelled_exception_class()
165
+
166
+
167
+ #
168
+ # Private API
169
+ #
170
+
171
+
172
+ @contextmanager
173
+ def claim_worker_thread(
174
+ backend_class: type[AsyncBackend], token: object
175
+ ) -> Generator[Any, None, None]:
176
+ from ..lowlevel import EventLoopToken
177
+
178
+ threadlocals.current_token = EventLoopToken(backend_class, token)
179
+ try:
180
+ yield
181
+ finally:
182
+ del threadlocals.current_token
183
+
184
+
185
+ def get_async_backend(asynclib_name: str | None = None) -> type[AsyncBackend]:
186
+ if asynclib_name is None:
187
+ asynclib_name = current_async_library()
188
+ if not asynclib_name:
189
+ raise NoEventLoopError(
190
+ f"Not currently running on any asynchronous event loop. "
191
+ f"Available async backends: {', '.join(get_all_backends())}"
192
+ )
193
+
194
+ # We use our own dict instead of sys.modules to get the already imported back-end
195
+ # class because the appropriate modules in sys.modules could potentially be only
196
+ # partially initialized
197
+ try:
198
+ return loaded_backends[asynclib_name]
199
+ except KeyError:
200
+ module = import_module(f"anyio._backends._{asynclib_name}")
201
+ loaded_backends[asynclib_name] = module.backend_class
202
+ return module.backend_class
203
+
204
+
205
+ def current_async_library() -> str | None:
206
+ if sniffio is None:
207
+ # If sniffio is not installed, we assume we're either running asyncio or nothing
208
+ import asyncio
209
+
210
+ try:
211
+ asyncio.get_running_loop()
212
+ return "asyncio"
213
+ except RuntimeError:
214
+ pass
215
+ else:
216
+ try:
217
+ return sniffio.current_async_library()
218
+ except sniffio.AsyncLibraryNotFoundError:
219
+ pass
220
+
221
+ return None
222
+
223
+
224
+ def set_current_async_library(asynclib_name: str | None) -> Token | None:
225
+ # no-op if sniffio is not installed
226
+ if sniffio is None:
227
+ return None
228
+
229
+ return sniffio.current_async_library_cvar.set(asynclib_name)
230
+
231
+
232
+ def reset_current_async_library(token: Token | None) -> None:
233
+ if token is not None:
234
+ sniffio.current_async_library_cvar.reset(token)
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_exceptions.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+ from collections.abc import Generator
5
+ from textwrap import dedent
6
+ from typing import Any
7
+
8
+ if sys.version_info < (3, 11):
9
+ from exceptiongroup import BaseExceptionGroup
10
+
11
+
12
+ class BrokenResourceError(Exception):
13
+ """
14
+ Raised when trying to use a resource that has been rendered unusable due to external
15
+ causes (e.g. a send stream whose peer has disconnected).
16
+ """
17
+
18
+
19
+ class BrokenWorkerProcess(Exception):
20
+ """
21
+ Raised by :meth:`~anyio.to_process.run_sync` if the worker process terminates abruptly or
22
+ otherwise misbehaves.
23
+ """
24
+
25
+
26
+ class BrokenWorkerInterpreter(Exception):
27
+ """
28
+ Raised by :meth:`~anyio.to_interpreter.run_sync` if an unexpected exception is
29
+ raised in the subinterpreter.
30
+ """
31
+
32
+ def __init__(self, excinfo: Any):
33
+ # This was adapted from concurrent.futures.interpreter.ExecutionFailed
34
+ msg = excinfo.formatted
35
+ if not msg:
36
+ if excinfo.type and excinfo.msg:
37
+ msg = f"{excinfo.type.__name__}: {excinfo.msg}"
38
+ else:
39
+ msg = excinfo.type.__name__ or excinfo.msg
40
+
41
+ super().__init__(msg)
42
+ self.excinfo = excinfo
43
+
44
+ def __str__(self) -> str:
45
+ try:
46
+ formatted = self.excinfo.errdisplay
47
+ except Exception:
48
+ return super().__str__()
49
+ else:
50
+ return dedent(
51
+ f"""
52
+ {super().__str__()}
53
+
54
+ Uncaught in the interpreter:
55
+
56
+ {formatted}
57
+ """.strip()
58
+ )
59
+
60
+
61
+ class BusyResourceError(Exception):
62
+ """
63
+ Raised when two tasks are trying to read from or write to the same resource
64
+ concurrently.
65
+ """
66
+
67
+ def __init__(self, action: str):
68
+ super().__init__(f"Another task is already {action} this resource")
69
+
70
+
71
+ class ClosedResourceError(Exception):
72
+ """Raised when trying to use a resource that has been closed."""
73
+
74
+
75
+ class ConnectionFailed(OSError):
76
+ """
77
+ Raised when a connection attempt fails.
78
+
79
+ .. note:: This class inherits from :exc:`OSError` for backwards compatibility.
80
+ """
81
+
82
+
83
+ def iterate_exceptions(
84
+ exception: BaseException,
85
+ ) -> Generator[BaseException, None, None]:
86
+ if isinstance(exception, BaseExceptionGroup):
87
+ for exc in exception.exceptions:
88
+ yield from iterate_exceptions(exc)
89
+ else:
90
+ yield exception
91
+
92
+
93
+ class DelimiterNotFound(Exception):
94
+ """
95
+ Raised during
96
+ :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_until` if the
97
+ maximum number of bytes has been read without the delimiter being found.
98
+ """
99
+
100
+ def __init__(self, max_bytes: int) -> None:
101
+ super().__init__(
102
+ f"The delimiter was not found among the first {max_bytes} bytes"
103
+ )
104
+
105
+
106
+ class EndOfStream(Exception):
107
+ """
108
+ Raised when trying to read from a stream that has been closed from the other end.
109
+ """
110
+
111
+
112
+ class IncompleteRead(Exception):
113
+ """
114
+ Raised during
115
+ :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_exactly` or
116
+ :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_until` if the
117
+ connection is closed before the requested amount of bytes has been read.
118
+ """
119
+
120
+ def __init__(self) -> None:
121
+ super().__init__(
122
+ "The stream was closed before the read operation could be completed"
123
+ )
124
+
125
+
126
+ class TypedAttributeLookupError(LookupError):
127
+ """
128
+ Raised by :meth:`~anyio.TypedAttributeProvider.extra` when the given typed attribute
129
+ is not found and no default value has been given.
130
+ """
131
+
132
+
133
+ class WouldBlock(Exception):
134
+ """Raised by ``X_nowait`` functions if ``X()`` would block."""
135
+
136
+
137
+ class NoEventLoopError(RuntimeError):
138
+ """
139
+ Raised by several functions that require an event loop to be running in the current
140
+ thread when there is no running event loop.
141
+
142
+ This is also raised by :func:`.from_thread.run` and :func:`.from_thread.run_sync`
143
+ if not calling from an AnyIO worker thread, and no ``token`` was passed.
144
+ """
145
+
146
+
147
+ class RunFinishedError(RuntimeError):
148
+ """
149
+ Raised by :func:`.from_thread.run` and :func:`.from_thread.run_sync` if the event
150
+ loop associated with the explicitly passed token has already finished.
151
+ """
152
+
153
+ def __init__(self) -> None:
154
+ super().__init__(
155
+ "The event loop associated with the given token has already finished"
156
+ )
miniconda3/envs/active_proaction/lib/python3.10/site-packages/anyio/_core/_fileio.py ADDED
@@ -0,0 +1,797 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import pathlib
5
+ import sys
6
+ from collections.abc import (
7
+ AsyncIterator,
8
+ Callable,
9
+ Iterable,
10
+ Iterator,
11
+ Sequence,
12
+ )
13
+ from dataclasses import dataclass
14
+ from functools import partial
15
+ from os import PathLike
16
+ from typing import (
17
+ IO,
18
+ TYPE_CHECKING,
19
+ Any,
20
+ AnyStr,
21
+ ClassVar,
22
+ Final,
23
+ Generic,
24
+ overload,
25
+ )
26
+
27
+ from .. import to_thread
28
+ from ..abc import AsyncResource
29
+
30
+ if TYPE_CHECKING:
31
+ from types import ModuleType
32
+
33
+ from _typeshed import OpenBinaryMode, OpenTextMode, ReadableBuffer, WriteableBuffer
34
+ else:
35
+ ReadableBuffer = OpenBinaryMode = OpenTextMode = WriteableBuffer = object
36
+
37
+
38
+ class AsyncFile(AsyncResource, Generic[AnyStr]):
39
+ """
40
+ An asynchronous file object.
41
+
42
+ This class wraps a standard file object and provides async friendly versions of the
43
+ following blocking methods (where available on the original file object):
44
+
45
+ * read
46
+ * read1
47
+ * readline
48
+ * readlines
49
+ * readinto
50
+ * readinto1
51
+ * write
52
+ * writelines
53
+ * truncate
54
+ * seek
55
+ * tell
56
+ * flush
57
+
58
+ All other methods are directly passed through.
59
+
60
+ This class supports the asynchronous context manager protocol which closes the
61
+ underlying file at the end of the context block.
62
+
63
+ This class also supports asynchronous iteration::
64
+
65
+ async with await open_file(...) as f:
66
+ async for line in f:
67
+ print(line)
68
+ """
69
+
70
+ def __init__(self, fp: IO[AnyStr]) -> None:
71
+ self._fp: Any = fp
72
+
73
+ def __getattr__(self, name: str) -> object:
74
+ return getattr(self._fp, name)
75
+
76
+ @property
77
+ def wrapped(self) -> IO[AnyStr]:
78
+ """The wrapped file object."""
79
+ return self._fp
80
+
81
+ async def __aiter__(self) -> AsyncIterator[AnyStr]:
82
+ while True:
83
+ line = await self.readline()
84
+ if line:
85
+ yield line
86
+ else:
87
+ break
88
+
89
+ async def aclose(self) -> None:
90
+ return await to_thread.run_sync(self._fp.close)
91
+
92
+ async def read(self, size: int = -1) -> AnyStr:
93
+ return await to_thread.run_sync(self._fp.read, size)
94
+
95
+ async def read1(self: AsyncFile[bytes], size: int = -1) -> bytes:
96
+ return await to_thread.run_sync(self._fp.read1, size)
97
+
98
+ async def readline(self) -> AnyStr:
99
+ return await to_thread.run_sync(self._fp.readline)
100
+
101
+ async def readlines(self) -> list[AnyStr]:
102
+ return await to_thread.run_sync(self._fp.readlines)
103
+
104
+ async def readinto(self: AsyncFile[bytes], b: WriteableBuffer) -> int:
105
+ return await to_thread.run_sync(self._fp.readinto, b)
106
+
107
+ async def readinto1(self: AsyncFile[bytes], b: WriteableBuffer) -> int:
108
+ return await to_thread.run_sync(self._fp.readinto1, b)
109
+
110
+ @overload
111
+ async def write(self: AsyncFile[bytes], b: ReadableBuffer) -> int: ...
112
+
113
+ @overload
114
+ async def write(self: AsyncFile[str], b: str) -> int: ...
115
+
116
+ async def write(self, b: ReadableBuffer | str) -> int:
117
+ return await to_thread.run_sync(self._fp.write, b)
118
+
119
+ @overload
120
+ async def writelines(
121
+ self: AsyncFile[bytes], lines: Iterable[ReadableBuffer]
122
+ ) -> None: ...
123
+
124
+ @overload
125
+ async def writelines(self: AsyncFile[str], lines: Iterable[str]) -> None: ...
126
+
127
+ async def writelines(self, lines: Iterable[ReadableBuffer] | Iterable[str]) -> None:
128
+ return await to_thread.run_sync(self._fp.writelines, lines)
129
+
130
+ async def truncate(self, size: int | None = None) -> int:
131
+ return await to_thread.run_sync(self._fp.truncate, size)
132
+
133
+ async def seek(self, offset: int, whence: int | None = os.SEEK_SET) -> int:
134
+ return await to_thread.run_sync(self._fp.seek, offset, whence)
135
+
136
+ async def tell(self) -> int:
137
+ return await to_thread.run_sync(self._fp.tell)
138
+
139
+ async def flush(self) -> None:
140
+ return await to_thread.run_sync(self._fp.flush)
141
+
142
+
143
+ @overload
144
+ async def open_file(
145
+ file: str | PathLike[str] | int,
146
+ mode: OpenBinaryMode,
147
+ buffering: int = ...,
148
+ encoding: str | None = ...,
149
+ errors: str | None = ...,
150
+ newline: str | None = ...,
151
+ closefd: bool = ...,
152
+ opener: Callable[[str, int], int] | None = ...,
153
+ ) -> AsyncFile[bytes]: ...
154
+
155
+
156
+ @overload
157
+ async def open_file(
158
+ file: str | PathLike[str] | int,
159
+ mode: OpenTextMode = ...,
160
+ buffering: int = ...,
161
+ encoding: str | None = ...,
162
+ errors: str | None = ...,
163
+ newline: str | None = ...,
164
+ closefd: bool = ...,
165
+ opener: Callable[[str, int], int] | None = ...,
166
+ ) -> AsyncFile[str]: ...
167
+
168
+
169
+ async def open_file(
170
+ file: str | PathLike[str] | int,
171
+ mode: str = "r",
172
+ buffering: int = -1,
173
+ encoding: str | None = None,
174
+ errors: str | None = None,
175
+ newline: str | None = None,
176
+ closefd: bool = True,
177
+ opener: Callable[[str, int], int] | None = None,
178
+ ) -> AsyncFile[Any]:
179
+ """
180
+ Open a file asynchronously.
181
+
182
+ The arguments are exactly the same as for the builtin :func:`open`.
183
+
184
+ :return: an asynchronous file object
185
+
186
+ """
187
+ fp = await to_thread.run_sync(
188
+ open, file, mode, buffering, encoding, errors, newline, closefd, opener
189
+ )
190
+ return AsyncFile(fp)
191
+
192
+
193
+ def wrap_file(file: IO[AnyStr]) -> AsyncFile[AnyStr]:
194
+ """
195
+ Wrap an existing file as an asynchronous file.
196
+
197
+ :param file: an existing file-like object
198
+ :return: an asynchronous file object
199
+
200
+ """
201
+ return AsyncFile(file)
202
+
203
+
204
+ @dataclass(eq=False)
205
+ class _PathIterator(AsyncIterator["Path"]):
206
+ iterator: Iterator[PathLike[str]]
207
+
208
+ async def __anext__(self) -> Path:
209
+ nextval = await to_thread.run_sync(
210
+ next, self.iterator, None, abandon_on_cancel=True
211
+ )
212
+ if nextval is None:
213
+ raise StopAsyncIteration from None
214
+
215
+ return Path(nextval)
216
+
217
+
218
+ class Path:
219
+ """
220
+ An asynchronous version of :class:`pathlib.Path`.
221
+
222
+ This class cannot be substituted for :class:`pathlib.Path` or
223
+ :class:`pathlib.PurePath`, but it is compatible with the :class:`os.PathLike`
224
+ interface.
225
+
226
+ It implements the Python 3.10 version of :class:`pathlib.Path` interface, except for
227
+ the deprecated :meth:`~pathlib.Path.link_to` method.
228
+
229
+ Some methods may be unavailable or have limited functionality, based on the Python
230
+ version:
231
+
232
+ * :meth:`~pathlib.Path.copy` (available on Python 3.14 or later)
233
+ * :meth:`~pathlib.Path.copy_into` (available on Python 3.14 or later)
234
+ * :meth:`~pathlib.Path.from_uri` (available on Python 3.13 or later)
235
+ * :meth:`~pathlib.PurePath.full_match` (available on Python 3.13 or later)
236
+ * :attr:`~pathlib.Path.info` (available on Python 3.14 or later)
237
+ * :meth:`~pathlib.Path.is_junction` (available on Python 3.12 or later)
238
+ * :meth:`~pathlib.PurePath.match` (the ``case_sensitive`` parameter is only
239
+ available on Python 3.13 or later)
240
+ * :meth:`~pathlib.Path.move` (available on Python 3.14 or later)
241
+ * :meth:`~pathlib.Path.move_into` (available on Python 3.14 or later)
242
+ * :meth:`~pathlib.PurePath.relative_to` (the ``walk_up`` parameter is only available
243
+ on Python 3.12 or later)
244
+ * :meth:`~pathlib.Path.walk` (available on Python 3.12 or later)
245
+
246
+ Any methods that do disk I/O need to be awaited on. These methods are:
247
+
248
+ * :meth:`~pathlib.Path.absolute`
249
+ * :meth:`~pathlib.Path.chmod`
250
+ * :meth:`~pathlib.Path.cwd`
251
+ * :meth:`~pathlib.Path.exists`
252
+ * :meth:`~pathlib.Path.expanduser`
253
+ * :meth:`~pathlib.Path.group`
254
+ * :meth:`~pathlib.Path.hardlink_to`
255
+ * :meth:`~pathlib.Path.home`
256
+ * :meth:`~pathlib.Path.is_block_device`
257
+ * :meth:`~pathlib.Path.is_char_device`
258
+ * :meth:`~pathlib.Path.is_dir`
259
+ * :meth:`~pathlib.Path.is_fifo`
260
+ * :meth:`~pathlib.Path.is_file`
261
+ * :meth:`~pathlib.Path.is_junction`
262
+ * :meth:`~pathlib.Path.is_mount`
263
+ * :meth:`~pathlib.Path.is_socket`
264
+ * :meth:`~pathlib.Path.is_symlink`
265
+ * :meth:`~pathlib.Path.lchmod`
266
+ * :meth:`~pathlib.Path.lstat`
267
+ * :meth:`~pathlib.Path.mkdir`
268
+ * :meth:`~pathlib.Path.open`
269
+ * :meth:`~pathlib.Path.owner`
270
+ * :meth:`~pathlib.Path.read_bytes`
271
+ * :meth:`~pathlib.Path.read_text`
272
+ * :meth:`~pathlib.Path.readlink`
273
+ * :meth:`~pathlib.Path.rename`
274
+ * :meth:`~pathlib.Path.replace`
275
+ * :meth:`~pathlib.Path.resolve`
276
+ * :meth:`~pathlib.Path.rmdir`
277
+ * :meth:`~pathlib.Path.samefile`
278
+ * :meth:`~pathlib.Path.stat`
279
+ * :meth:`~pathlib.Path.symlink_to`
280
+ * :meth:`~pathlib.Path.touch`
281
+ * :meth:`~pathlib.Path.unlink`
282
+ * :meth:`~pathlib.Path.walk`
283
+ * :meth:`~pathlib.Path.write_bytes`
284
+ * :meth:`~pathlib.Path.write_text`
285
+
286
+ Additionally, the following methods return an async iterator yielding
287
+ :class:`~.Path` objects:
288
+
289
+ * :meth:`~pathlib.Path.glob`
290
+ * :meth:`~pathlib.Path.iterdir`
291
+ * :meth:`~pathlib.Path.rglob`
292
+ """
293
+
294
+ __slots__ = "_path", "__weakref__"
295
+
296
+ __weakref__: Any
297
+
298
+ def __init__(self, *args: str | PathLike[str]) -> None:
299
+ self._path: Final[pathlib.Path] = pathlib.Path(*args)
300
+
301
+ def __fspath__(self) -> str:
302
+ return self._path.__fspath__()
303
+
304
+ def __str__(self) -> str:
305
+ return self._path.__str__()
306
+
307
+ def __repr__(self) -> str:
308
+ return f"{self.__class__.__name__}({self.as_posix()!r})"
309
+
310
+ def __bytes__(self) -> bytes:
311
+ return self._path.__bytes__()
312
+
313
+ def __hash__(self) -> int:
314
+ return self._path.__hash__()
315
+
316
+ def __eq__(self, other: object) -> bool:
317
+ target = other._path if isinstance(other, Path) else other
318
+ return self._path.__eq__(target)
319
+
320
+ def __lt__(self, other: pathlib.PurePath | Path) -> bool:
321
+ target = other._path if isinstance(other, Path) else other
322
+ return self._path.__lt__(target)
323
+
324
+ def __le__(self, other: pathlib.PurePath | Path) -> bool:
325
+ target = other._path if isinstance(other, Path) else other
326
+ return self._path.__le__(target)
327
+
328
+ def __gt__(self, other: pathlib.PurePath | Path) -> bool:
329
+ target = other._path if isinstance(other, Path) else other
330
+ return self._path.__gt__(target)
331
+
332
+ def __ge__(self, other: pathlib.PurePath | Path) -> bool:
333
+ target = other._path if isinstance(other, Path) else other
334
+ return self._path.__ge__(target)
335
+
336
+ def __truediv__(self, other: str | PathLike[str]) -> Path:
337
+ return Path(self._path / other)
338
+
339
+ def __rtruediv__(self, other: str | PathLike[str]) -> Path:
340
+ return Path(other) / self
341
+
342
+ @property
343
+ def parts(self) -> tuple[str, ...]:
344
+ return self._path.parts
345
+
346
+ @property
347
+ def drive(self) -> str:
348
+ return self._path.drive
349
+
350
+ @property
351
+ def root(self) -> str:
352
+ return self._path.root
353
+
354
+ @property
355
+ def anchor(self) -> str:
356
+ return self._path.anchor
357
+
358
+ @property
359
+ def parents(self) -> Sequence[Path]:
360
+ return tuple(Path(p) for p in self._path.parents)
361
+
362
+ @property
363
+ def parent(self) -> Path:
364
+ return Path(self._path.parent)
365
+
366
+ @property
367
+ def name(self) -> str:
368
+ return self._path.name
369
+
370
+ @property
371
+ def suffix(self) -> str:
372
+ return self._path.suffix
373
+
374
+ @property
375
+ def suffixes(self) -> list[str]:
376
+ return self._path.suffixes
377
+
378
+ @property
379
+ def stem(self) -> str:
380
+ return self._path.stem
381
+
382
+ async def absolute(self) -> Path:
383
+ path = await to_thread.run_sync(self._path.absolute)
384
+ return Path(path)
385
+
386
+ def as_posix(self) -> str:
387
+ return self._path.as_posix()
388
+
389
+ def as_uri(self) -> str:
390
+ return self._path.as_uri()
391
+
392
+ if sys.version_info >= (3, 13):
393
+ parser: ClassVar[ModuleType] = pathlib.Path.parser
394
+
395
+ @classmethod
396
+ def from_uri(cls, uri: str) -> Path:
397
+ return Path(pathlib.Path.from_uri(uri))
398
+
399
+ def full_match(
400
+ self, path_pattern: str, *, case_sensitive: bool | None = None
401
+ ) -> bool:
402
+ return self._path.full_match(path_pattern, case_sensitive=case_sensitive)
403
+
404
+ def match(
405
+ self, path_pattern: str, *, case_sensitive: bool | None = None
406
+ ) -> bool:
407
+ return self._path.match(path_pattern, case_sensitive=case_sensitive)
408
+ else:
409
+
410
+ def match(self, path_pattern: str) -> bool:
411
+ return self._path.match(path_pattern)
412
+
413
+ if sys.version_info >= (3, 14):
414
+
415
+ @property
416
+ def info(self) -> Any: # TODO: add return type annotation when Typeshed gets it
417
+ return self._path.info
418
+
419
+ async def copy(
420
+ self,
421
+ target: str | os.PathLike[str],
422
+ *,
423
+ follow_symlinks: bool = True,
424
+ preserve_metadata: bool = False,
425
+ ) -> Path:
426
+ func = partial(
427
+ self._path.copy,
428
+ follow_symlinks=follow_symlinks,
429
+ preserve_metadata=preserve_metadata,
430
+ )
431
+ return Path(await to_thread.run_sync(func, pathlib.Path(target)))
432
+
433
+ async def copy_into(
434
+ self,
435
+ target_dir: str | os.PathLike[str],
436
+ *,
437
+ follow_symlinks: bool = True,
438
+ preserve_metadata: bool = False,
439
+ ) -> Path:
440
+ func = partial(
441
+ self._path.copy_into,
442
+ follow_symlinks=follow_symlinks,
443
+ preserve_metadata=preserve_metadata,
444
+ )
445
+ return Path(await to_thread.run_sync(func, pathlib.Path(target_dir)))
446
+
447
+ async def move(self, target: str | os.PathLike[str]) -> Path:
448
+ # Upstream does not handle anyio.Path properly as a PathLike
449
+ target = pathlib.Path(target)
450
+ return Path(await to_thread.run_sync(self._path.move, target))
451
+
452
+ async def move_into(
453
+ self,
454
+ target_dir: str | os.PathLike[str],
455
+ ) -> Path:
456
+ return Path(await to_thread.run_sync(self._path.move_into, target_dir))
457
+
458
+ def is_relative_to(self, other: str | PathLike[str]) -> bool:
459
+ try:
460
+ self.relative_to(other)
461
+ return True
462
+ except ValueError:
463
+ return False
464
+
465
+ async def chmod(self, mode: int, *, follow_symlinks: bool = True) -> None:
466
+ func = partial(os.chmod, follow_symlinks=follow_symlinks)
467
+ return await to_thread.run_sync(func, self._path, mode)
468
+
469
+ @classmethod
470
+ async def cwd(cls) -> Path:
471
+ path = await to_thread.run_sync(pathlib.Path.cwd)
472
+ return cls(path)
473
+
474
+ async def exists(self) -> bool:
475
+ return await to_thread.run_sync(self._path.exists, abandon_on_cancel=True)
476
+
477
+ async def expanduser(self) -> Path:
478
+ return Path(
479
+ await to_thread.run_sync(self._path.expanduser, abandon_on_cancel=True)
480
+ )
481
+
482
+ if sys.version_info < (3, 12):
483
+ # Python 3.11 and earlier
484
+ def glob(self, pattern: str) -> AsyncIterator[Path]:
485
+ gen = self._path.glob(pattern)
486
+ return _PathIterator(gen)
487
+ elif (3, 12) <= sys.version_info < (3, 13):
488
+ # changed in Python 3.12:
489
+ # - The case_sensitive parameter was added.
490
+ def glob(
491
+ self,
492
+ pattern: str,
493
+ *,
494
+ case_sensitive: bool | None = None,
495
+ ) -> AsyncIterator[Path]:
496
+ gen = self._path.glob(pattern, case_sensitive=case_sensitive)
497
+ return _PathIterator(gen)
498
+ elif sys.version_info >= (3, 13):
499
+ # Changed in Python 3.13:
500
+ # - The recurse_symlinks parameter was added.
501
+ # - The pattern parameter accepts a path-like object.
502
+ def glob( # type: ignore[misc] # mypy doesn't allow for differing signatures in a conditional block
503
+ self,
504
+ pattern: str | PathLike[str],
505
+ *,
506
+ case_sensitive: bool | None = None,
507
+ recurse_symlinks: bool = False,
508
+ ) -> AsyncIterator[Path]:
509
+ gen = self._path.glob(
510
+ pattern, # type: ignore[arg-type]
511
+ case_sensitive=case_sensitive,
512
+ recurse_symlinks=recurse_symlinks,
513
+ )
514
+ return _PathIterator(gen)
515
+
516
+ async def group(self) -> str:
517
+ return await to_thread.run_sync(self._path.group, abandon_on_cancel=True)
518
+
519
+ async def hardlink_to(
520
+ self, target: str | bytes | PathLike[str] | PathLike[bytes]
521
+ ) -> None:
522
+ if isinstance(target, Path):
523
+ target = target._path
524
+
525
+ await to_thread.run_sync(os.link, target, self)
526
+
527
+ @classmethod
528
+ async def home(cls) -> Path:
529
+ home_path = await to_thread.run_sync(pathlib.Path.home)
530
+ return cls(home_path)
531
+
532
+ def is_absolute(self) -> bool:
533
+ return self._path.is_absolute()
534
+
535
+ async def is_block_device(self) -> bool:
536
+ return await to_thread.run_sync(
537
+ self._path.is_block_device, abandon_on_cancel=True
538
+ )
539
+
540
+ async def is_char_device(self) -> bool:
541
+ return await to_thread.run_sync(
542
+ self._path.is_char_device, abandon_on_cancel=True
543
+ )
544
+
545
+ async def is_dir(self) -> bool:
546
+ return await to_thread.run_sync(self._path.is_dir, abandon_on_cancel=True)
547
+
548
+ async def is_fifo(self) -> bool:
549
+ return await to_thread.run_sync(self._path.is_fifo, abandon_on_cancel=True)
550
+
551
+ async def is_file(self) -> bool:
552
+ return await to_thread.run_sync(self._path.is_file, abandon_on_cancel=True)
553
+
554
+ if sys.version_info >= (3, 12):
555
+
556
+ async def is_junction(self) -> bool:
557
+ return await to_thread.run_sync(self._path.is_junction)
558
+
559
+ async def is_mount(self) -> bool:
560
+ return await to_thread.run_sync(
561
+ os.path.ismount, self._path, abandon_on_cancel=True
562
+ )
563
+
564
+ def is_reserved(self) -> bool:
565
+ return self._path.is_reserved()
566
+
567
+ async def is_socket(self) -> bool:
568
+ return await to_thread.run_sync(self._path.is_socket, abandon_on_cancel=True)
569
+
570
+ async def is_symlink(self) -> bool:
571
+ return await to_thread.run_sync(self._path.is_symlink, abandon_on_cancel=True)
572
+
573
+ async def iterdir(self) -> AsyncIterator[Path]:
574
+ gen = (
575
+ self._path.iterdir()
576
+ if sys.version_info < (3, 13)
577
+ else await to_thread.run_sync(self._path.iterdir, abandon_on_cancel=True)
578
+ )
579
+ async for path in _PathIterator(gen):
580
+ yield path
581
+
582
+ def joinpath(self, *args: str | PathLike[str]) -> Path:
583
+ return Path(self._path.joinpath(*args))
584
+
585
+ async def lchmod(self, mode: int) -> None:
586
+ await to_thread.run_sync(self._path.lchmod, mode)
587
+
588
+ async def lstat(self) -> os.stat_result:
589
+ return await to_thread.run_sync(self._path.lstat, abandon_on_cancel=True)
590
+
591
+ async def mkdir(
592
+ self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False
593
+ ) -> None:
594
+ await to_thread.run_sync(self._path.mkdir, mode, parents, exist_ok)
595
+
596
+ @overload
597
+ async def open(
598
+ self,
599
+ mode: OpenBinaryMode,
600
+ buffering: int = ...,
601
+ encoding: str | None = ...,
602
+ errors: str | None = ...,
603
+ newline: str | None = ...,
604
+ ) -> AsyncFile[bytes]: ...
605
+
606
+ @overload
607
+ async def open(
608
+ self,
609
+ mode: OpenTextMode = ...,
610
+ buffering: int = ...,
611
+ encoding: str | None = ...,
612
+ errors: str | None = ...,
613
+ newline: str | None = ...,
614
+ ) -> AsyncFile[str]: ...
615
+
616
+ async def open(
617
+ self,
618
+ mode: str = "r",
619
+ buffering: int = -1,
620
+ encoding: str | None = None,
621
+ errors: str | None = None,
622
+ newline: str | None = None,
623
+ ) -> AsyncFile[Any]:
624
+ fp = await to_thread.run_sync(
625
+ self._path.open, mode, buffering, encoding, errors, newline
626
+ )
627
+ return AsyncFile(fp)
628
+
629
+ async def owner(self) -> str:
630
+ return await to_thread.run_sync(self._path.owner, abandon_on_cancel=True)
631
+
632
+ async def read_bytes(self) -> bytes:
633
+ return await to_thread.run_sync(self._path.read_bytes)
634
+
635
+ async def read_text(
636
+ self, encoding: str | None = None, errors: str | None = None
637
+ ) -> str:
638
+ return await to_thread.run_sync(self._path.read_text, encoding, errors)
639
+
640
+ if sys.version_info >= (3, 12):
641
+
642
+ def relative_to(
643
+ self, *other: str | PathLike[str], walk_up: bool = False
644
+ ) -> Path:
645
+ # relative_to() should work with any PathLike but it doesn't
646
+ others = [pathlib.Path(other) for other in other]
647
+ return Path(self._path.relative_to(*others, walk_up=walk_up))
648
+
649
+ else:
650
+
651
+ def relative_to(self, *other: str | PathLike[str]) -> Path:
652
+ return Path(self._path.relative_to(*other))
653
+
654
+ async def readlink(self) -> Path:
655
+ target = await to_thread.run_sync(os.readlink, self._path)
656
+ return Path(target)
657
+
658
+ async def rename(self, target: str | pathlib.PurePath | Path) -> Path:
659
+ if isinstance(target, Path):
660
+ target = target._path
661
+
662
+ await to_thread.run_sync(self._path.rename, target)
663
+ return Path(target)
664
+
665
+ async def replace(self, target: str | pathlib.PurePath | Path) -> Path:
666
+ if isinstance(target, Path):
667
+ target = target._path
668
+
669
+ await to_thread.run_sync(self._path.replace, target)
670
+ return Path(target)
671
+
672
+ async def resolve(self, strict: bool = False) -> Path:
673
+ func = partial(self._path.resolve, strict=strict)
674
+ return Path(await to_thread.run_sync(func, abandon_on_cancel=True))
675
+
676
+ if sys.version_info < (3, 12):
677
+ # Pre Python 3.12
678
+ def rglob(self, pattern: str) -> AsyncIterator[Path]:
679
+ gen = self._path.rglob(pattern)
680
+ return _PathIterator(gen)
681
+ elif (3, 12) <= sys.version_info < (3, 13):
682
+ # Changed in Python 3.12:
683
+ # - The case_sensitive parameter was added.
684
+ def rglob(
685
+ self, pattern: str, *, case_sensitive: bool | None = None
686
+ ) -> AsyncIterator[Path]:
687
+ gen = self._path.rglob(pattern, case_sensitive=case_sensitive)
688
+ return _PathIterator(gen)
689
+ elif sys.version_info >= (3, 13):
690
+ # Changed in Python 3.13:
691
+ # - The recurse_symlinks parameter was added.
692
+ # - The pattern parameter accepts a path-like object.
693
+ def rglob( # type: ignore[misc] # mypy doesn't allow for differing signatures in a conditional block
694
+ self,
695
+ pattern: str | PathLike[str],
696
+ *,
697
+ case_sensitive: bool | None = None,
698
+ recurse_symlinks: bool = False,
699
+ ) -> AsyncIterator[Path]:
700
+ gen = self._path.rglob(
701
+ pattern, # type: ignore[arg-type]
702
+ case_sensitive=case_sensitive,
703
+ recurse_symlinks=recurse_symlinks,
704
+ )
705
+ return _PathIterator(gen)
706
+
707
+ async def rmdir(self) -> None:
708
+ await to_thread.run_sync(self._path.rmdir)
709
+
710
+ async def samefile(self, other_path: str | PathLike[str]) -> bool:
711
+ if isinstance(other_path, Path):
712
+ other_path = other_path._path
713
+
714
+ return await to_thread.run_sync(
715
+ self._path.samefile, other_path, abandon_on_cancel=True
716
+ )
717
+
718
+ async def stat(self, *, follow_symlinks: bool = True) -> os.stat_result:
719
+ func = partial(os.stat, follow_symlinks=follow_symlinks)
720
+ return await to_thread.run_sync(func, self._path, abandon_on_cancel=True)
721
+
722
+ async def symlink_to(
723
+ self,
724
+ target: str | bytes | PathLike[str] | PathLike[bytes],
725
+ target_is_directory: bool = False,
726
+ ) -> None:
727
+ if isinstance(target, Path):
728
+ target = target._path
729
+
730
+ await to_thread.run_sync(self._path.symlink_to, target, target_is_directory)
731
+
732
+ async def touch(self, mode: int = 0o666, exist_ok: bool = True) -> None:
733
+ await to_thread.run_sync(self._path.touch, mode, exist_ok)
734
+
735
+ async def unlink(self, missing_ok: bool = False) -> None:
736
+ try:
737
+ await to_thread.run_sync(self._path.unlink)
738
+ except FileNotFoundError:
739
+ if not missing_ok:
740
+ raise
741
+
742
+ if sys.version_info >= (3, 12):
743
+
744
+ async def walk(
745
+ self,
746
+ top_down: bool = True,
747
+ on_error: Callable[[OSError], object] | None = None,
748
+ follow_symlinks: bool = False,
749
+ ) -> AsyncIterator[tuple[Path, list[str], list[str]]]:
750
+ def get_next_value() -> tuple[pathlib.Path, list[str], list[str]] | None:
751
+ try:
752
+ return next(gen)
753
+ except StopIteration:
754
+ return None
755
+
756
+ gen = self._path.walk(top_down, on_error, follow_symlinks)
757
+ while True:
758
+ value = await to_thread.run_sync(get_next_value)
759
+ if value is None:
760
+ return
761
+
762
+ root, dirs, paths = value
763
+ yield Path(root), dirs, paths
764
+
765
+ def with_name(self, name: str) -> Path:
766
+ return Path(self._path.with_name(name))
767
+
768
+ def with_stem(self, stem: str) -> Path:
769
+ return Path(self._path.with_name(stem + self._path.suffix))
770
+
771
+ def with_suffix(self, suffix: str) -> Path:
772
+ return Path(self._path.with_suffix(suffix))
773
+
774
+ def with_segments(self, *pathsegments: str | PathLike[str]) -> Path:
775
+ return Path(*pathsegments)
776
+
777
+ async def write_bytes(self, data: bytes) -> int:
778
+ return await to_thread.run_sync(self._path.write_bytes, data)
779
+
780
+ async def write_text(
781
+ self,
782
+ data: str,
783
+ encoding: str | None = None,
784
+ errors: str | None = None,
785
+ newline: str | None = None,
786
+ ) -> int:
787
+ # Path.write_text() does not support the "newline" parameter before Python 3.10
788
+ def sync_write_text() -> int:
789
+ with self._path.open(
790
+ "w", encoding=encoding, errors=errors, newline=newline
791
+ ) as fp:
792
+ return fp.write(data)
793
+
794
+ return await to_thread.run_sync(sync_write_text)
795
+
796
+
797
+ PathLike.register(Path)