-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitors.py
196 lines (159 loc) · 5.86 KB
/
visitors.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
from collections import (
deque,
)
from typing import (
Iterable,
Optional,
Sequence,
)
from asty.nodes import (
BaseNode,
MatchRuleNode,
Node,
)
def iter_fields(node: BaseNode):
if node is not None:
yield from node.iterate_children()
def iter_child_nodes(node):
for name, field in iter_fields(node):
if isinstance(field, Sequence):
for item in field:
yield item
else:
yield field
def walk(node):
todo = deque([node])
while todo:
node = todo.popleft()
todo.extend(iter_child_nodes(node))
yield node
class NodeVisitor:
def visit(self, node):
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method, self.generic_visit)
return visitor(node)
def generic_visit(self, node):
for field, value in iter_fields(node):
if isinstance(value, list):
for item in value:
self.visit(item)
else:
self.visit(value)
class NodeTransformer(NodeVisitor):
def generic_visit(self, node):
for field, old_value in iter_fields(node):
if isinstance(old_value, Sequence):
new_values = []
for value in old_value:
value = self.visit(value)
if value is None:
continue
if isinstance(value, Sequence):
new_values.extend(value)
continue
new_values.append(value)
old_value[:] = new_values
elif isinstance(old_value, BaseNode):
new_node = self.visit(old_value)
if new_node is None:
delattr(node, field)
else:
setattr(node, field, new_node)
return node
class MatchingResult:
def __init__(self, pattern: BaseNode, node: BaseNode, context: str = None):
self.pattern = pattern
self.node = node
self.context = context
self.matches: list['MatchingResult'] = []
def __getitem__(self, key: str):
for match in self.matches:
if match.context == key:
yield match
def keys(self):
return [match.context for match in self.matches]
def attach(self, *sub_match: 'MatchingResult'):
self.matches.extend(sub_match)
return self
def __pretty__(self, fmt, **_kwargs):
from devtools import sformat
yield sformat('MatchingResult', sformat.bold) + '('
yield 1
yield 'context='
yield sformat(repr(self.context), sformat.green)
yield ','
yield 0
yield 'pattern='
yield sformat(repr(self.pattern.node_type), sformat.blue, sformat.italic)
yield ','
yield 0
yield 'node='
yield sformat(repr(self.node.node_type), sformat.yellow, sformat.italic)
yield ','
yield 0
if self.matches:
yield 'matchers='
yield fmt(self.matches)
yield ','
yield 0
yield -1
yield ')'
Result = Iterable[MatchingResult]
class Matcher:
def __init__(self, pattern: BaseNode, context: Optional[str] = None):
self.pattern = pattern
self.context = context
def make_matcher(self, pattern: BaseNode, context: str) -> 'Matcher':
return Matcher(pattern, context)
def make_result(self, node: BaseNode) -> MatchingResult:
return MatchingResult(self.pattern, node, self.context)
def match(self, node: BaseNode) -> Result:
method = 'match_' + self.pattern.__class__.__name__
matcher = getattr(self, method, self.generic_match)
return list(matcher(node))
def _single_match(self, node: BaseNode) -> Result:
assert isinstance(self.pattern, MatchRuleNode)
# TODO: add operator usage
for rule in self.pattern.rules:
sub_matcher = self.make_matcher(rule, self.pattern.name)
if sub_match := sub_matcher.match(node):
yield self.make_result(node).attach(*sub_match)
def _search_match(self, tree: BaseNode) -> Result:
for node in walk(tree):
yield from self._single_match(node)
def match_MatchRuleNode(self, node: BaseNode) -> Result:
assert isinstance(self.pattern, MatchRuleNode)
if self.pattern.exact:
yield from self._single_match(node)
else:
yield from self._search_match(node)
def generic_match(self, node: BaseNode) -> Result:
complex_fields: dict[str, Node] = {
name: value
for name, value in iter_fields(self.pattern)
}
for name in self.pattern.__fields_set__:
if name in complex_fields:
continue
pattern_value = getattr(self.pattern, name)
node_value = getattr(node, name, None)
if pattern_value != node_value:
return
sub_matches: list[MatchingResult] = []
for name, pattern_value in complex_fields.items():
items = pattern_value if isinstance(pattern_value, Sequence) else [pattern_value]
for item in items:
sub_matcher = self.make_matcher(item, name)
node_value = getattr(node, name, None)
if node_value is None:
continue
sub_match = []
if isinstance(node_value, Sequence):
for node_item in node_value:
sub_match.extend(sub_matcher.match(node_item))
if isinstance(node_value, BaseNode):
sub_match.extend(sub_matcher.match(node_value))
if not sub_match:
return
sub_matches.extend(sub_match)
yield self.make_result(node).attach(*sub_matches)