Pattern matching¶
{ #feature-pattern-matching }
Stable ID: FEATURE-PATTERN-MATCHING
User-facing summary¶
The system supports sequence-based, kind-based, and structural matching with placeholders and controllable matching criteria.
Related concepts¶
Related images¶
- Local image directory: pattern-matching-images/
MISC¶
Scenario: Python parser backends¶
- Python patterns match on
PythonRstNode. - With a
PythonFactory(PythonCstNode), thecreate*methods ofPythonPatternFactoryandPythonRefactoring.replace_stmtraiseNotImplementedErrorinstead of matching.
Scenario: Independent of layout (whitespaces)¶
Scenario: Independent of comments¶
Three cases
- absent in pattern, present in code
- present in pattern, absent in code
- present in pattern, present in code, yet different content
Scenario: Support of placeholders¶
- Placeholders are AST Nodes
- Support of explicit and implicit placeholders
Scenario: Placeholders for type parameters¶
[$$tps]indef $f[$$tps]($$args) -> $r:orclass $c[$$tps]($$bases):matches any number of type parameters (TypeVar,ParamSpecandTypeVarTuple).[$t]matches exactly one type parameter.- Each captured type parameter is an AST node.
- A type parameter written in the pattern, such as
[T], matches only that name.
Scenario: Placeholders for keyword arguments¶
**$$kwsinclass $c($$bases, **$$kws):orf($$args, **$$kws)matches any number of keyword arguments of a class or a call, named (metaclass=M) or unpacked (**options), and captures each one as an AST node.- A single keyword argument written with placeholders, such as
$kw=$v, matches exactly one keyword argument.
Scenario: Placeholders for the parameters of a function¶
- Python keeps each kind of parameter in its own list, so a pattern names one placeholder per kind:
def $f($$positional_only, /, $$params=$$defaults, *$$varargs, $$keyword_only=$$keyword_defaults, **$$kwargs):matches every function, whatever kinds of parameters it has. $$argsalone, as indef $f($$args):, matches only positional-or-keyword parameters without a default value. What a lone$$argsshould match is open in issue 22.
Scenario: Robustness for occurrence of implicit placeholders¶
- in strings, e.g.
"$X" - in comments, e.g.,
/* $X */.
Scenario: Multiple occurrences of placeholders¶
- Equivalent AST nodes
- Access to all occurrences
Scenario: Multiple assignments of placeholders¶
- E.g., in patterns like
$f($$before, $arg, $$after)
Legality of match-patterns¶
For exact matching, we currently accept all patterns.
In other words, no illegal match pattern exists.
-
Match functionality
- Elementary matching - "symbols with symbols" - representations of integers, characters, etc.
- Basic matching - "code with code" - with(out) comment, with white spaces - note: python is sensitive for indentation
- Single placeholder - "pattern with code" - pattern has only one instance of a single placeholder
- Multi placeholder - "pattern with code" - pattern has only one instance of a multi placeholder
- different placeholders - "pattern with code" with different placeholders (each placeholder occurs only once & only single assignment)
- constraint / recurring placeholder - "pattern with code" with single placeholder that occurs multiple times
- patterns with multi placeholders that can have multiple assignments
- mixed