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.

MISC

Scenario: Python parser backends

  • Python patterns match on PythonRstNode.
  • With a PythonFactory(PythonCstNode), the create* methods of PythonPatternFactory and PythonRefactoring.replace_stmt raise NotImplementedError instead of matching.

Scenario: Independent of layout (whitespaces)

Scenario: Independent of comments

Three cases

  1. absent in pattern, present in code
  2. present in pattern, absent in code
  3. 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] in def $f[$$tps]($$args) -> $r: or class $c[$$tps]($$bases): matches any number of type parameters (TypeVar, ParamSpec and TypeVarTuple).
  • [$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

  • **$$kws in class $c($$bases, **$$kws): or f($$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.
  • $$args alone, as in def $f($$args):, matches only positional-or-keyword parameters without a default value. What a lone $$args should 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.


  1. Match functionality

    1. Elementary matching - "symbols with symbols" - representations of integers, characters, etc.
    2. Basic matching - "code with code" - with(out) comment, with white spaces - note: python is sensitive for indentation
    3. Single placeholder - "pattern with code" - pattern has only one instance of a single placeholder
    4. Multi placeholder - "pattern with code" - pattern has only one instance of a multi placeholder
    5. different placeholders - "pattern with code" with different placeholders (each placeholder occurs only once & only single assignment)
    6. constraint / recurring placeholder - "pattern with code" with single placeholder that occurs multiple times
    7. patterns with multi placeholders that can have multiple assignments
    8. mixed