Skip to content

Commit 5766b8a

Browse files
committed
docs: apply scheme marker for all query syntax snippets
1 parent e021d6e commit 5766b8a

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

docs/section-2-using-parsers.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,21 +442,21 @@ Many code analysis tasks involve searching for patterns in syntax trees. Tree-si
442442
443443
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:
444444
445-
```
445+
``` scheme
446446
(binary_expression (number_literal) (number_literal))
447447
```
448448

449449
Children can also be omitted. For example, this would match any `binary_expression` where at least _one_ of child is a `string_literal` node:
450450

451-
```
451+
``` scheme
452452
(binary_expression (string_literal))
453453
```
454454

455455
#### Fields
456456

457457
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`.
458458

459-
```
459+
``` scheme
460460
(assignment_expression
461461
left: (member_expression
462462
object: (call_expression)))
@@ -466,7 +466,7 @@ In general, it's a good idea to make patterns more specific by specifying [field
466466

467467
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:
468468

469-
```
469+
``` scheme
470470
(class_declaration
471471
name: (identifier) @class_name
472472
!type_parameters)
@@ -476,7 +476,7 @@ You can also constrain a pattern so that it only matches nodes that *lack* a cer
476476

477477
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`:
478478

479-
```
479+
``` scheme
480480
(binary_expression
481481
operator: "!="
482482
right: (null))
@@ -488,15 +488,15 @@ When matching patterns, you may want to process specific nodes within the patter
488488

489489
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:
490490

491-
```
491+
``` scheme
492492
(assignment_expression
493493
left: (identifier) @the-function-name
494494
right: (function))
495495
```
496496

497497
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:
498498

499-
```
499+
``` scheme
500500
(class_declaration
501501
name: (identifier) @the-class-name
502502
body: (class_body
@@ -510,21 +510,21 @@ You can match a repeating sequence of sibling nodes using the postfix `+` and `*
510510

511511
For example, this pattern would match a sequence of one or more comments:
512512

513-
```
513+
``` scheme
514514
(comment)+
515515
```
516516

517517
This pattern would match a class declaration, capturing all of the decorators if any were present:
518518

519-
```
519+
``` scheme
520520
(class_declaration
521521
(decorator)* @the-decorator
522522
name: (identifier) @the-name)
523523
```
524524

525525
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:
526526

527-
```
527+
``` scheme
528528
(call_expression
529529
function: (identifier) @the-function
530530
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
534534

535535
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:
536536

537-
```
537+
``` scheme
538538
(
539539
(comment)
540540
(function_declaration)
@@ -543,7 +543,7 @@ You can also use parentheses for grouping a sequence of _sibling_ nodes. For exa
543543

544544
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:
545545

546-
```
546+
``` scheme
547547
(
548548
(number)
549549
("," (number))*
@@ -558,7 +558,7 @@ This is similar to _character classes_ from regular expressions (`[abc]` matches
558558
For example, this pattern would match a call to either a variable or an object property.
559559
In the case of a variable, capture it as `@function`, and in the case of a property, capture it as `@method`:
560560

561-
```
561+
``` scheme
562562
(call_expression
563563
function: [
564564
(identifier) @function
@@ -569,7 +569,7 @@ In the case of a variable, capture it as `@function`, and in the case of a prope
569569

570570
This pattern would match a set of possible keyword tokens, capturing them as `@keyword`:
571571

572-
```
572+
``` scheme
573573
[
574574
"break"
575575
"delete"
@@ -592,7 +592,7 @@ and `_` will match any named or anonymous node.
592592

593593
For example, this pattern would match any node inside a call:
594594

595-
```
595+
``` scheme
596596
(call (_) @call.inner)
597597
```
598598

@@ -602,21 +602,21 @@ The anchor operator, `.`, is used to constrain the ways in which child patterns
602602

603603
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`:
604604

605-
```
605+
``` scheme
606606
(array . (identifier) @the-element)
607607
```
608608

609609
Without this anchor, the pattern would match once for every identifier in the array, with `@the-element` bound to each matched identifier.
610610

611611
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`.
612612

613-
```
613+
``` scheme
614614
(block (_) @last-expression .)
615615
```
616616

617617
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`.
618618

619-
```
619+
``` scheme
620620
(dotted_name
621621
(identifier) @prev-id
622622
.
@@ -633,7 +633,7 @@ You can also specify arbitrary metadata and conditions associated with a pattern
633633

634634
For example, this pattern would match identifier whose names is written in `SCREAMING_SNAKE_CASE`:
635635

636-
```
636+
``` scheme
637637
(
638638
(identifier) @constant
639639
(#match? @constant "^[A-Z][A-Z_]+")
@@ -642,7 +642,7 @@ For example, this pattern would match identifier whose names is written in `SCRE
642642

643643
And this pattern would match key-value pairs where the `value` is an identifier with the same name as the key:
644644

645-
```
645+
``` scheme
646646
(
647647
(pair
648648
key: (property_identifier) @key-name

0 commit comments

Comments
 (0)