Basic pattern matcher for AST. The pattern may contain wildcards represented by the symbol '_'. A node matches a pattern if for every node in the tree, either there is a node of the same type in pattern, or a Name node with id='_'. Args: node: ast.AST pattern: ast.AST Returns:
(node, pattern)
| 191 | |
| 192 | |
| 193 | def matches(node, pattern): |
| 194 | """Basic pattern matcher for AST. |
| 195 | |
| 196 | The pattern may contain wildcards represented by the symbol '_'. A node |
| 197 | matches a pattern if for every node in the tree, either there is a node of |
| 198 | the same type in pattern, or a Name node with id='_'. |
| 199 | |
| 200 | Args: |
| 201 | node: ast.AST |
| 202 | pattern: ast.AST |
| 203 | Returns: |
| 204 | bool |
| 205 | """ |
| 206 | if isinstance(pattern, str): |
| 207 | pattern = parser.parse_str(pattern) |
| 208 | |
| 209 | matcher = PatternMatcher(pattern) |
| 210 | matcher.visit(node) |
| 211 | return matcher.matches |
| 212 | |
| 213 | |
| 214 | # TODO(mdan): Once we have error tracing, we may be able to just go to SSA. |
nothing calls this directly
no test coverage detected