docs: apply scheme marker for all query syntax snippets
This commit is contained in:
parent
e021d6e979
commit
5766b8a0a7
1 changed files with 20 additions and 20 deletions
|
|
@ -442,13 +442,13 @@ Many code analysis tasks involve searching for patterns in syntax trees. Tree-si
|
|||
|
||||
A _query_ consists of one or more _patterns_, where each pattern is an [S-expression](https://en.wikipedia.org/wiki/S-expression) that matches a certain set of nodes in a syntax tree. The expression to match a given node consists of a pair of parentheses containing two things: the node's type, and optionally, a series of other S-expressions that match the node's children. For example, this pattern would match any `binary_expression` node whose children are both `number_literal` nodes:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(binary_expression (number_literal) (number_literal))
|
||||
```
|
||||
|
||||
Children can also be omitted. For example, this would match any `binary_expression` where at least _one_ of child is a `string_literal` node:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(binary_expression (string_literal))
|
||||
```
|
||||
|
||||
|
|
@ -456,7 +456,7 @@ Children can also be omitted. For example, this would match any `binary_expressi
|
|||
|
||||
In general, it's a good idea to make patterns more specific by specifying [field names](#node-field-names) associated with child nodes. You do this by prefixing a child pattern with a field name followed by a colon. For example, this pattern would match an `assignment_expression` node where the `left` child is a `member_expression` whose `object` is a `call_expression`.
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(assignment_expression
|
||||
left: (member_expression
|
||||
object: (call_expression)))
|
||||
|
|
@ -466,7 +466,7 @@ In general, it's a good idea to make patterns more specific by specifying [field
|
|||
|
||||
You can also constrain a pattern so that it only matches nodes that *lack* a certain field. To do this, add a field name prefixed by a `!` within the parent pattern. For example, this pattern would match a class declaration with no type parameters:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(class_declaration
|
||||
name: (identifier) @class_name
|
||||
!type_parameters)
|
||||
|
|
@ -476,7 +476,7 @@ You can also constrain a pattern so that it only matches nodes that *lack* a cer
|
|||
|
||||
The parenthesized syntax for writing nodes only applies to [named nodes](#named-vs-anonymous-nodes). To match specific anonymous nodes, you write their name between double quotes. For example, this pattern would match any `binary_expression` where the operator is `!=` and the right side is `null`:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(binary_expression
|
||||
operator: "!="
|
||||
right: (null))
|
||||
|
|
@ -488,7 +488,7 @@ When matching patterns, you may want to process specific nodes within the patter
|
|||
|
||||
For example, this pattern would match any assignment of a `function` to an `identifier`, and it would associate the name `the-function-name` with the identifier:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(assignment_expression
|
||||
left: (identifier) @the-function-name
|
||||
right: (function))
|
||||
|
|
@ -496,7 +496,7 @@ For example, this pattern would match any assignment of a `function` to an `iden
|
|||
|
||||
And this pattern would match all method definitions, associating the name `the-method-name` with the method name, `the-class-name` with the containing class name:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(class_declaration
|
||||
name: (identifier) @the-class-name
|
||||
body: (class_body
|
||||
|
|
@ -510,13 +510,13 @@ You can match a repeating sequence of sibling nodes using the postfix `+` and `*
|
|||
|
||||
For example, this pattern would match a sequence of one or more comments:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(comment)+
|
||||
```
|
||||
|
||||
This pattern would match a class declaration, capturing all of the decorators if any were present:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(class_declaration
|
||||
(decorator)* @the-decorator
|
||||
name: (identifier) @the-name)
|
||||
|
|
@ -524,7 +524,7 @@ This pattern would match a class declaration, capturing all of the decorators if
|
|||
|
||||
You can also mark a node as optional using the `?` operator. For example, this pattern would match all function calls, capturing a string argument if one was present:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(call_expression
|
||||
function: (identifier) @the-function
|
||||
arguments: (arguments (string)? @the-string-arg))
|
||||
|
|
@ -534,7 +534,7 @@ You can also mark a node as optional using the `?` operator. For example, this p
|
|||
|
||||
You can also use parentheses for grouping a sequence of _sibling_ nodes. For example, this pattern would match a comment followed by a function declaration:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(
|
||||
(comment)
|
||||
(function_declaration)
|
||||
|
|
@ -543,7 +543,7 @@ You can also use parentheses for grouping a sequence of _sibling_ nodes. For exa
|
|||
|
||||
Any of the quantification operators mentioned above (`+`, `*`, and `?`) can also be applied to groups. For example, this pattern would match a comma-separated series of numbers:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(
|
||||
(number)
|
||||
("," (number))*
|
||||
|
|
@ -558,7 +558,7 @@ This is similar to _character classes_ from regular expressions (`[abc]` matches
|
|||
For example, this pattern would match a call to either a variable or an object property.
|
||||
In the case of a variable, capture it as `@function`, and in the case of a property, capture it as `@method`:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(call_expression
|
||||
function: [
|
||||
(identifier) @function
|
||||
|
|
@ -569,7 +569,7 @@ In the case of a variable, capture it as `@function`, and in the case of a prope
|
|||
|
||||
This pattern would match a set of possible keyword tokens, capturing them as `@keyword`:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
[
|
||||
"break"
|
||||
"delete"
|
||||
|
|
@ -592,7 +592,7 @@ and `_` will match any named or anonymous node.
|
|||
|
||||
For example, this pattern would match any node inside a call:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(call (_) @call.inner)
|
||||
```
|
||||
|
||||
|
|
@ -602,7 +602,7 @@ The anchor operator, `.`, is used to constrain the ways in which child patterns
|
|||
|
||||
When `.` is placed before the _first_ child within a parent pattern, the child will only match when it is the first named node in the parent. For example, the below pattern matches a given `array` node at most once, assigning the `@the-element` capture to the first `identifier` node in the parent `array`:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(array . (identifier) @the-element)
|
||||
```
|
||||
|
||||
|
|
@ -610,13 +610,13 @@ Without this anchor, the pattern would match once for every identifier in the ar
|
|||
|
||||
Similarly, an anchor placed after a pattern's _last_ child will cause that child pattern to only match nodes that are the last named child of their parent. The below pattern matches only nodes that are the last named child within a `block`.
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(block (_) @last-expression .)
|
||||
```
|
||||
|
||||
Finally, an anchor _between_ two child patterns will cause the patterns to only match nodes that are immediate siblings. The pattern below, given a long dotted name like `a.b.c.d`, will only match pairs of consecutive identifiers: `a, b`, `b, c`, and `c, d`.
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(dotted_name
|
||||
(identifier) @prev-id
|
||||
.
|
||||
|
|
@ -633,7 +633,7 @@ You can also specify arbitrary metadata and conditions associated with a pattern
|
|||
|
||||
For example, this pattern would match identifier whose names is written in `SCREAMING_SNAKE_CASE`:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(
|
||||
(identifier) @constant
|
||||
(#match? @constant "^[A-Z][A-Z_]+")
|
||||
|
|
@ -642,7 +642,7 @@ For example, this pattern would match identifier whose names is written in `SCRE
|
|||
|
||||
And this pattern would match key-value pairs where the `value` is an identifier with the same name as the key:
|
||||
|
||||
```
|
||||
``` scheme
|
||||
(
|
||||
(pair
|
||||
key: (property_identifier) @key-name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue