-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
visitor.py
376 lines (314 loc) · 13 KB
/
visitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from copy import copy
from enum import Enum
from typing import (
Any,
Callable,
Collection,
Dict,
List,
NamedTuple,
Optional,
Tuple,
Union,
)
from ..pyutils import inspect, snake_to_camel
from . import ast
from .ast import Node, QUERY_DOCUMENT_KEYS
__all__ = [
"Visitor",
"ParallelVisitor",
"VisitorAction",
"visit",
"BREAK",
"SKIP",
"REMOVE",
"IDLE",
]
class VisitorActionEnum(Enum):
"""Special return values for the visitor methods.
You can also use the values of this enum directly.
"""
BREAK = True
SKIP = False
REMOVE = Ellipsis
VisitorAction = Optional[VisitorActionEnum]
# Note that in GraphQL.js these are defined differently:
# BREAK = {}, SKIP = false, REMOVE = null, IDLE = undefined
BREAK = VisitorActionEnum.BREAK
SKIP = VisitorActionEnum.SKIP
REMOVE = VisitorActionEnum.REMOVE
IDLE = None
VisitorKeyMap = Dict[str, Tuple[str, ...]]
class EnterLeaveVisitor(NamedTuple):
"""Visitor with functions for entering and leaving."""
enter: Optional[Callable[..., Optional[VisitorAction]]]
leave: Optional[Callable[..., Optional[VisitorAction]]]
class Visitor:
"""Visitor that walks through an AST.
Visitors can define two generic methods "enter" and "leave". The former will be
called when a node is entered in the traversal, the latter is called after visiting
the node and its child nodes. These methods have the following signature::
def enter(self, node, key, parent, path, ancestors):
# The return value has the following meaning:
# IDLE (None): no action
# SKIP: skip visiting this node
# BREAK: stop visiting altogether
# REMOVE: delete this node
# any other value: replace this node with the returned value
return
def leave(self, node, key, parent, path, ancestors):
# The return value has the following meaning:
# IDLE (None) or SKIP: no action
# BREAK: stop visiting altogether
# REMOVE: delete this node
# any other value: replace this node with the returned value
return
The parameters have the following meaning:
:arg node: The current node being visiting.
:arg key: The index or key to this node from the parent node or Array.
:arg parent: the parent immediately above this node, which may be an Array.
:arg path: The key path to get to this node from the root node.
:arg ancestors: All nodes and Arrays visited before reaching parent
of this node. These correspond to array indices in ``path``.
Note: ancestors includes arrays which contain the parent of visited node.
You can also define node kind specific methods by suffixing them with an underscore
followed by the kind of the node to be visited. For instance, to visit ``field``
nodes, you would defined the methods ``enter_field()`` and/or ``leave_field()``,
with the same signature as above. If no kind specific method has been defined
for a given node, the generic method is called.
"""
# Provide special return values as attributes
BREAK, SKIP, REMOVE, IDLE = BREAK, SKIP, REMOVE, IDLE
enter_leave_map: Dict[str, EnterLeaveVisitor]
def __init_subclass__(cls) -> None:
"""Verify that all defined handlers are valid."""
super().__init_subclass__()
for attr, val in cls.__dict__.items():
if attr.startswith("_"):
continue
attr_kind = attr.split("_", 1)
if len(attr_kind) < 2:
kind: Optional[str] = None
else:
attr, kind = attr_kind
if attr in ("enter", "leave") and kind:
name = snake_to_camel(kind) + "Node"
node_cls = getattr(ast, name, None)
if (
not node_cls
or not isinstance(node_cls, type)
or not issubclass(node_cls, Node)
):
raise TypeError(f"Invalid AST node kind: {kind}.")
def __init__(self) -> None:
self.enter_leave_map = {}
def get_enter_leave_for_kind(self, kind: str) -> EnterLeaveVisitor:
"""Given a node kind, return the EnterLeaveVisitor for that kind."""
try:
return self.enter_leave_map[kind]
except KeyError:
enter_fn = getattr(self, f"enter_{kind}", None)
if not enter_fn:
enter_fn = getattr(self, "enter", None)
leave_fn = getattr(self, f"leave_{kind}", None)
if not leave_fn:
leave_fn = getattr(self, "leave", None)
enter_leave = EnterLeaveVisitor(enter_fn, leave_fn)
self.enter_leave_map[kind] = enter_leave
return enter_leave
def get_visit_fn(
self, kind: str, is_leaving: bool = False
) -> Optional[Callable[..., Optional[VisitorAction]]]:
"""Get the visit function for the given node kind and direction.
.. deprecated:: 3.2
Please use ``get_enter_leave_for_kind`` instead. Will be removed in v3.3.
"""
enter_leave = self.get_enter_leave_for_kind(kind)
return enter_leave.leave if is_leaving else enter_leave.enter
class Stack(NamedTuple):
"""A stack for the visit function."""
in_array: bool
idx: int
keys: Tuple[Node, ...]
edits: List[Tuple[Union[int, str], Node]]
prev: Any # 'Stack' (python/mypy/issues/731)
def visit(
root: Node, visitor: Visitor, visitor_keys: Optional[VisitorKeyMap] = None
) -> Any:
"""Visit each node in an AST.
:func:`~.visit` will walk through an AST using a depth-first traversal, calling the
visitor's enter methods at each node in the traversal, and calling the leave methods
after visiting that node and all of its child nodes.
By returning different values from the enter and leave methods, the behavior of the
visitor can be altered, including skipping over a sub-tree of the AST (by returning
False), editing the AST by returning a value or None to remove the value, or to stop
the whole traversal by returning :data:`~.BREAK`.
When using :func:`~.visit` to edit an AST, the original AST will not be modified,
and a new version of the AST with the changes applied will be returned from the
visit function.
To customize the node attributes to be used for traversal, you can provide a
dictionary visitor_keys mapping node kinds to node attributes.
"""
if not isinstance(root, Node):
raise TypeError(f"Not an AST Node: {inspect(root)}.")
if not isinstance(visitor, Visitor):
raise TypeError(f"Not an AST Visitor: {inspect(visitor)}.")
if visitor_keys is None:
visitor_keys = QUERY_DOCUMENT_KEYS
stack: Any = None
in_array = False
keys: Tuple[Node, ...] = (root,)
idx = -1
edits: List[Any] = []
node: Any = root
key: Any = None
parent: Any = None
path: List[Any] = []
path_append = path.append
path_pop = path.pop
ancestors: List[Any] = []
ancestors_append = ancestors.append
ancestors_pop = ancestors.pop
while True:
idx += 1
is_leaving = idx == len(keys)
is_edited = is_leaving and edits
if is_leaving:
key = path[-1] if ancestors else None
node = parent
parent = ancestors_pop() if ancestors else None
if is_edited:
if in_array:
node = list(node)
edit_offset = 0
for edit_key, edit_value in edits:
array_key = edit_key - edit_offset
if edit_value is REMOVE or edit_value is Ellipsis:
node.pop(array_key)
edit_offset += 1
else:
node[array_key] = edit_value
node = tuple(node)
else:
node = copy(node)
for edit_key, edit_value in edits:
setattr(node, edit_key, edit_value)
idx = stack.idx
keys = stack.keys
edits = stack.edits
in_array = stack.in_array
stack = stack.prev
elif parent:
if in_array:
key = idx
node = parent[key]
else:
key = keys[idx]
node = getattr(parent, key, None)
if node is None:
continue
path_append(key)
if isinstance(node, tuple):
result = None
else:
if not isinstance(node, Node):
raise TypeError(f"Invalid AST Node: {inspect(node)}.")
enter_leave = visitor.get_enter_leave_for_kind(node.kind)
visit_fn = enter_leave.leave if is_leaving else enter_leave.enter
if visit_fn:
result = visit_fn(node, key, parent, path, ancestors)
if result is BREAK or result is True:
break
if result is SKIP or result is False:
if not is_leaving:
path_pop()
continue
elif result is not None:
edits.append((key, result))
if not is_leaving:
if isinstance(result, Node):
node = result
else:
path_pop()
continue
else:
result = None
if result is None and is_edited:
edits.append((key, node))
if is_leaving:
if path:
path_pop()
else:
stack = Stack(in_array, idx, keys, edits, stack)
in_array = isinstance(node, tuple)
keys = node if in_array else visitor_keys.get(node.kind, ())
idx = -1
edits = []
if parent:
ancestors_append(parent)
parent = node
if not stack:
break
if edits:
return edits[-1][1]
return root
class ParallelVisitor(Visitor):
"""A Visitor which delegates to many visitors to run in parallel.
Each visitor will be visited for each node before moving on.
If a prior visitor edits a node, no following visitors will see that node.
"""
def __init__(self, visitors: Collection[Visitor]):
"""Create a new visitor from the given list of parallel visitors."""
super().__init__()
self.visitors = visitors
self.skipping: List[Any] = [None] * len(visitors)
def get_enter_leave_for_kind(self, kind: str) -> EnterLeaveVisitor:
"""Given a node kind, return the EnterLeaveVisitor for that kind."""
try:
return self.enter_leave_map[kind]
except KeyError:
has_visitor = False
enter_list: List[Optional[Callable[..., Optional[VisitorAction]]]] = []
leave_list: List[Optional[Callable[..., Optional[VisitorAction]]]] = []
for visitor in self.visitors:
enter, leave = visitor.get_enter_leave_for_kind(kind)
if not has_visitor and (enter or leave):
has_visitor = True
enter_list.append(enter)
leave_list.append(leave)
if has_visitor:
def enter(node: Node, *args: Any) -> Optional[VisitorAction]:
skipping = self.skipping
for i, fn in enumerate(enter_list):
if not skipping[i]:
if fn:
result = fn(node, *args)
if result is SKIP or result is False:
skipping[i] = node
elif result is BREAK or result is True:
skipping[i] = BREAK
elif result is not None:
return result
return None
def leave(node: Node, *args: Any) -> Optional[VisitorAction]:
skipping = self.skipping
for i, fn in enumerate(leave_list):
if not skipping[i]:
if fn:
result = fn(node, *args)
if result is BREAK or result is True:
skipping[i] = BREAK
elif (
result is not None
and result is not SKIP
and result is not False
):
return result
elif skipping[i] is node:
skipping[i] = None
return None
else:
enter = leave = None
enter_leave = EnterLeaveVisitor(enter, leave)
self.enter_leave_map[kind] = enter_leave
return enter_leave