MCPcopy Create free account
hub / github.com/SourceCode-AI/aura / ASTPattern

Class ASTPattern

aura/pattern_matching.py:205–377  ·  view source on GitHub ↗

Match AST tree against a defined source code pattern (compiled into AST tree pattern)

Source from the content-addressed store, hash-verified

203
204
205class ASTPattern:
206 """
207 Match AST tree against a defined source code pattern (compiled into AST tree pattern)
208 """
209 # Nested class defined here on purpose to have a namespace as this `Context` is related to ASTPattern matching
210 # making it easier to distinguish from other classes also named `Context`, especially the one from ASTVisitor/ASTNodes
211 class Context:
212 def __init__(self, pattern: ASTPattern):
213 self.pattern = pattern
214
215 def match(
216 self,
217 node: nodes.NodeType, # original AST node from the source code
218 other: nodes.NodeType # AST node pattern to match against
219 ) -> bool:
220 if type(other) == ASTPattern.AnyOf:
221 return self._match_any_of(node, other)
222 elif node is None:
223 return other is None
224 elif type(node) == bool:
225 if type(other) != bool:
226 return False
227 return node is other
228 elif type(node) == str:
229 if type(other) not in (str, nodes.String):
230 return False
231 return node == str(other)
232 elif type(node) == dict:
233 return self._match_dict(node, other)
234 elif type(node) in (list, tuple):
235 return self._match_list(node, other)
236 elif type(node) == int:
237 if type(other) not in (int, nodes.Number):
238 return False
239 return node == int(other)
240 elif type(node) == float:
241 if type(other) != float:
242 return False
243 return node == other
244
245 try:
246 return node.match(other, self)
247 except AttributeError:
248 return False
249
250 # Dispatch method for basic python types
251 def _match_dict(self, node: dict, other) -> bool:
252 if type(other) != dict:
253 return False
254 if set(other.keys()) - set(node.keys()):
255 return False
256
257 for k, v in node.items():
258 if not self.match(v, other[k]):
259 return False
260 return True
261
262 def _match_list(self, node: list, other) -> bool:

Callers 5

test_patternsFunction · 0.90
test_matching_triggersFunction · 0.90
test_any_ofFunction · 0.90
disabled_test_decoratorFunction · 0.90
process_taintFunction · 0.90

Calls

no outgoing calls

Tested by 5

test_patternsFunction · 0.72
test_matching_triggersFunction · 0.72
test_any_ofFunction · 0.72
disabled_test_decoratorFunction · 0.72
process_taintFunction · 0.72