Skip to main content

Parse tree pattern matcher

We have a small domain-specific language that we use to define patterns to look for in tree-sitter parse trees. This DSL enables us to rapidly define new syntactic scope types and support new programming languages.

Format

GRAND_PARENT_TYPE.*.CHILD_TYPE[FIELD]!?

SyntaxDescription
TYPEMatch node type
node.type
Dot operator(.)Type hierarchy between parent and child
Wildcard op(*)Match any type
Negation op(~)Match any type other than what is specified after ~
Important op(!)Use this node as result instead of parent.
By default the leftmost/top node is used
Optional op(?)Node is optional. Will match if available.
Field op([field])Return child node at given field.
node.childForFieldName(field)
Index op([n])Return nth named child node.

Multiple patterns

When using multiple patterns evaluation will be performed top to bottom and the first pattern too match will be used.

["export_statement?.class_declaration", "export_statement.class"];