Skip to content

Commit

Permalink
Standardize on tree-query-language
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Apr 11, 2024
1 parent 3c2f501 commit 5eed383
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod query_language;
mod tree_query_language;
mod using_queries;
mod using_the_cursor;
mod using_the_parser;
2 changes: 1 addition & 1 deletion documentation/public/user-guide/NAV.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- [Introduction](./introduction.md)
- [Concepts](./concepts.md)
- [Tree Query Language](./query-language.md)
- [Tree Query Language](./tree-query-language.md)
- [Rust Crate](./rust-crate/)
- [NPM Package](./npm-package/)
2 changes: 1 addition & 1 deletion documentation/public/user-guide/concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ allows you to reuse the same query in multiple places. Queries can largely
replace the need for both internal (cursor), and external (visitor) iterator
patterns.

The [query language](./query-language.md) is based on pattern matching, and the
The [query language](./tree-query-language.md) is based on pattern matching, and the
execution semantics are closer to unification than to regular expression
matching i.e. a query returns all possible matches, not just the
longest/shortest/first/last match. There is no concept of a 'greedy' operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

It's often more convenient to use the declarative `Query` API to traverse the CST, as they allow you to express your intent more concisely and can largely replace the need for both internal (cursor), and external (visitor) iterator patterns.

The [query language](./../query-language.md) is based on pattern matching, and the execution semantics are closer to unification than to regular expression matching. A query returns all possible matches, not just the longest/shortest/first/last match.
The [query language](./../tree-query-language.md) is based on pattern matching, and the execution semantics are closer to unification than to regular expression matching. A query returns all possible matches, not just the longest/shortest/first/last match.

If not specified otherwise, let's assume we already parsed a Solidity source and have a `cursor` pointing to the root node of the CST (created with `createTreeCursor`, see [Using the Cursor](./using-the-cursor.md)).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

It's often more convenient to use the declarative `Query` API to traverse the CST, as they allow you to express your intent more concisely and can largely replace the need for both internal (cursor), and external (visitor) iterator patterns.

The [query language](./../query-language.md) is based on pattern matching, and the execution semantics are closer to unification than to regular expression matching. A query returns all possible matches, not just the longest/shortest/first/last match.
The [query language](./../tree-query-language.md) is based on pattern matching, and the execution semantics are closer to unification than to regular expression matching. A query returns all possible matches, not just the longest/shortest/first/last match.

If not specified otherwise, let's assume we already parsed a Solidity source and have a `cursor` pointing to the root node of the CST (created with `create_tree_cursor`, see [Using the Cursor](./using-the-cursor.md)).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ example, this pattern would match any `MultiplicativeExpression` node whose chil
are exactly two `Expression` nodes, with an `Asterisk` node in between (no whitespace):

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:query-syntax-1"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:query-syntax-1"
```

The children of a node can optionally be named. The name is a property of the edge from
the node to the child, and is not a property of the child. For example, this pattern will match
a `MultiplicativeExpression` node with the two `Expression` children named `left_operand` and `right_operand`:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:query-syntax-2"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:query-syntax-2"
```

You can also match a node's textual content using a string literal. For example, this pattern would match a
`MultiplicativeExpression` with a `*` operator (for clarity):

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:query-syntax-3"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:query-syntax-3"
```

If you don't care about the kind of a node, you can use an underscore `_`, which matches any kind.
For example, this pattern will match a `MultiplicativeExpression`
node with two children, one of any kind named `left_operand` and one of any kind:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:query-syntax-4"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:query-syntax-4"
```

Children can also be elided. For example, this would produce multiple matches for a
`MultiplicativeExpression` where at least _one_ of the children is an expression of a `StringExpression` variant, where each match
is associated with each of the `StringExpression` children:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:query-syntax-5"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:query-syntax-5"
```

### Capturing Nodes
Expand All @@ -55,14 +55,14 @@ For example, this pattern would match any struct definition and it would associa
the name `struct_name` with the identifier:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:capturing-nodes-1"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:capturing-nodes-1"
```

And this pattern would match all event definitions for a contract, associating the name
`event_name` with the event name, `contract_name` with the containing contract name:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:capturing-nodes-2"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:capturing-nodes-2"
```

### Quantification
Expand All @@ -75,20 +75,20 @@ matches _one or more_.
For example, this pattern would match a sequence of one or more comments at the top of the file:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:quantification-1"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:quantification-1"
```

This pattern would match a contract definition with at least one doc comment, capturing them:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:quantification-2"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:quantification-2"
```

This pattern would match all function calls, capturing a string argument if one was
present:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:quantification-3"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:quantification-3"
```

### Alternations
Expand All @@ -99,11 +99,11 @@ For example, this pattern would match a call to either a variable or an object p
In the case of a variable, capture it as `@function`, and in the case of a property, capture it as `@method`:

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:alternations-1"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:alternations-1"
```

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

```{ .scheme }
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/query_language.rs:alternations-2"
--8<-- "crates/solidity/outputs/cargo/tests/src/doc_examples/tree_query_language.rs:alternations-2"
```

0 comments on commit 5eed383

Please sign in to comment.