Pattern Nodes
Pattern nodes are the basic building blocks to construct matching logic over an AST.
Basic Nodes
Basic nodes allow matching structural properties, capturing values, and simple boolean combinations.
ast_pattern_engine.nodes.basic
AllOf
Bases: Pattern
Matches if all patterns in the sequence match the node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Sequence[Pattern]
|
Sequence of patterns that must all match the node. |
required |
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
AllOf node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Sequence[Pattern]
|
Sequence of patterns that must all match the node. |
required |
AnyOf
Bases: Pattern
Match any of the patterns in the sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Sequence[Pattern]
|
Sequence of patterns where at least one must match. |
required |
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
AnyOf node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Sequence[Pattern]
|
Sequence of patterns where at least one must match. |
required |
Bind
Bases: Pattern
Bind the current node to key.
This is syntactic sugar for:
Collect(WildCard(), "x")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to bind the node(s) or value(s) to. |
required |
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
Bind node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to bind the node(s) or value(s) to. |
required |
Collect
Bases: Pattern
Collect the matched node under key and merge sub-bindings into current scope.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern to match. |
required |
key
|
str
|
The key to bind the pattern result to. |
required |
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
Collect node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern to match. |
required |
key
|
str
|
The key to bind the pattern result to. |
required |
Contains
Bases: Pattern
Matches a pattern that is contained anywhere within the node's sub-tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Sequence[Pattern]
|
The pattern or sequence of patterns to search for in the sub-tree. |
required |
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
Contains node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Sequence[Pattern]
|
The pattern or sequence of patterns to search for in the sub-tree. |
required |
Filter
Bases: Pattern
Match nodes where predicate(node) returns True and optionally bind node to key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[Any], bool]
|
A callable that returns True if the node matches. |
required |
key
|
str | None
|
Optional key to bind the matched node to. |
None
|
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
Filter node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[Any], bool]
|
A callable that returns True if the node matches. |
required |
key
|
str | None
|
Optional key to bind the matched node to. |
None
|
Source code in src/ast_pattern_engine/nodes/basic.py
NodePattern
Bases: Pattern
Match an AST node of node_type with constraints on its fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
type[AST]
|
The AST node class to match (e.g., ast.Assign). |
required |
**field_patterns
|
Pattern | Any
|
Patterns or exact values to match against the node's fields. |
{}
|
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
NodePattern node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
type[AST]
|
The AST node class to match (e.g., ast.Assign). |
required |
**field_patterns
|
Pattern | Any
|
Patterns or exact values to match against the node's fields. |
{}
|
Source code in src/ast_pattern_engine/nodes/basic.py
Not
Bases: Pattern
Match any node that is not matched by pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern that must fail for this to match. |
required |
Source code in src/ast_pattern_engine/nodes/basic.py
__init__
Not node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern that must fail for this to match. |
required |
WildCard
Sequence Nodes
Sequence nodes allow matching groups of adjacent nodes, similar to regex patterns.
ast_pattern_engine.nodes.sequences
OneOf
Bases: SequencePattern
Matches one of several patterns.
Can be set to be strict and only match if exactly one pattern matches. If not set to be strict, the first successful match is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Sequence[Pattern]
|
The patterns to match |
required |
strict
|
bool
|
Whether to be strict and only match if exactly one pattern matches |
False
|
key
|
str | None
|
Optional key to bind the matched pattern to |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
__init__
OneOf node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Sequence[Pattern]
|
The patterns to match |
required |
strict
|
bool
|
Whether to be strict and only match if exactly one pattern matches |
False
|
key
|
str | None
|
Optional key to bind the matched pattern to |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
Optional
Bases: SequencePattern
Matches a pattern zero or one times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern to match. |
required |
key
|
str | None
|
Optional key to bind the matched pattern to. |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
__init__
Optional node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern to match. |
required |
key
|
str | None
|
Optional key to bind the matched pattern to. |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
PatternGroup
Bases: SequencePattern
Matches a compound pattern/pattern group to an AST node sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Sequence[Pattern]
|
The compound pattern to match. |
required |
key
|
str | None
|
Optional key to bind the matched pattern to. |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
__init__
PatternGroup node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Sequence[Pattern]
|
The compound pattern to match. |
required |
key
|
str | None
|
Optional key to bind the matched pattern to. |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
Repetition
Bases: SequencePattern
Matches a single pattern zero or more times.
Also supports specifying min and max match count thresholds
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern to match. |
required |
min_matches
|
int
|
Minimum number of matches required. Default is 1. |
1
|
max_matches
|
int | None
|
Maximum number of allowed matches. Defaults to None. |
None
|
Source code in src/ast_pattern_engine/nodes/sequences.py
__init__
Repetition node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Pattern
|
The pattern to match. |
required |
min_matches
|
int
|
Minimum number of matches required. Default is 1. |
1
|
max_matches
|
int | None
|
Maximum number of allowed matches. Defaults to None. |
None
|