cursorless/docs/contributing/parse-tree-patterns.md
AndrewDant 358cf953cd
Update parse-tree-patterns.md (#665)
* Update parse-tree-patterns.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update docs/contributing/parse-tree-patterns.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com>
2022-05-31 14:59:03 +01:00

1.6 KiB

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]!?

Syntax Description
TYPE Match 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"];