Skip to content

Commit

Permalink
Doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Jan 13, 2024
1 parent 1236955 commit 2599e95
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 65 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let query = try language.query(contentsOf: url!)

let cursor = query.execute(node: tree.rootNode!)

let resolvingCursor = ResolvingQueryCursor(cursor: cursor)
let resolvingSequence = cursor.resolve(with: Predicate.Context(string: source))

let typeCaptures = cursor
.map { $0.captures(named: "type") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ let query = try language.query(contentsOf: url!)

let cursor = query.execute(node: tree.rootNode!)

let resolvingCursor = ResolvingQueryCursor(cursor: cursor)
let resolvingSequence = cursor.resolve(with: Predicate.Context(string: source))

resolvingCursor.prepare(with: source.cursorTextProvider)

let typeCaptures = cursor
let typeCaptures = resolvingSequence
.map { $0.captures(named: "type") }
.flatMap({ $0 })

Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftTreeSitter/Documentation.docc/SwiftTreeSitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Swift API for the tree-sitter incremental parsing system.

## Overview

SwiftTreeSitter attempts to map the [tree-sitter](https://tree-sitter.github.io/) C API very closely. It differs in two significant ways. First, it tries to use Swift/Foundation types when possible. And, it offers a much more featureful version of the ``QueryCursor`` object, ``ResolvingQueryCursor``. In all other respects, its best to refer to the C API for details and documentation.
SwiftTreeSitter attempts to map the [tree-sitter](https://tree-sitter.github.io/) C API very closely. It differs in two significant ways. First, it tries to use Swift/Foundation types when possible. And, it offers a query resolution system based around ``ResolvingQueryMatchSequence``.

In all other respects, it's best to refer to the C API for details and documentation.

## Topics

Expand All @@ -26,7 +28,7 @@ SwiftTreeSitter attempts to map the [tree-sitter](https://tree-sitter.github.io/

- ``Query``
- ``QueryCursor``
- ``ResolvingQueryCursor``
- ``ResolvingQueryMatchSequence``
- ``QueryCapture``
- ``QueryMatch``
- ``QueryError``
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@Tutorial(time: 10) {
@Intro(title: "Using ResolvingQueryCursor") {
While powerful, tree-sitter's query language cannot describe all syntax tree states. To help expand its capabilities, it allows for embedding arbitrary statements. These provide an additional way to filter matches. Tree-sitter's built-in query system does not evaluate predicates. Not all queries use predicates, but if they are present, the built-in types ignore them. This will lead to incorrect matching.
@Intro(title: "Fully Resolving Queries") {
While powerful, tree-sitter's query language cannot describe all syntax tree states. To help expand its capabilities, it allows for embedding arbitrary statements. These provide an additional way to filter matches. However, tree-sitter's built-in query system does not actually evaluate these statements. Not all queries use them, but if predicates are present, the built-in types ignore them.

`ResolvingQueryCursor` is a wrapper around the `QueryCursor` type which can transparently evaluate and filter results using query predicates. You can read more about predicates in tree-sitter's [query documentation](https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries).
`ResolvingQueryMatchSequence` is a `Sequence` that wraps the `QueryCursor` type and can transparently evaluate and filter results using query predicates. You can read more about predicates in tree-sitter's [query documentation](https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries).
}

@Section(title: "Understanding Predicates") {
Expand Down Expand Up @@ -89,15 +89,15 @@
Because `Cursor` does not evaluate predicates, we've matched too many nodes.

@Code(name: "TreeSitterExample.swift", file: "using-queries-2-7-example.swift") {
@Image(source: "cursor-output.png", alt: "")
@Image(source: "cursor-output.png", alt: "Two ranges {21, 8} and {40, 8}")
}
}
}
}

@Section(title: "Enumerating Matches with ResolvingQueryCursor") {
@Section(title: "Enumerating Matches with a Resolved Sequence") {
@ContentAndMedia {
A `ResolvingQueryCursor` works just like a regular `Cursor`, but can evaluate predicates to further filter matches. To do this, it does need access to the text content at evaluation time.
A `Cursor` must be transformed into a `ResolvingQueryMatchSequence` to evaluate predicates and further filter matches. This evaluation requires access to the text content.
}

@Steps {
Expand All @@ -107,26 +107,20 @@
@Code(name: "TreeSitterExample.swift", file: "using-queries-2-7-example.swift")
}
@Step {
Create a `ResolvingQueryCursor`.
Create a `ResolvingQueryMatchSequence` from the cursor.

To actually resolve the results, the sequence requires some context. We're building one from the source string data. But, there are other ways to do, and those are required if you need to evaluate subqueries.

@Code(name: "TreeSitterExample.swift", file: "using-queries-2-8-example.swift")
}

@Step {
Prepare it, passing in a function that can return text content.

Here, we're using a a simple String extension helper. You may need more a more involved implementation.

@Code(name: "TreeSitterExample.swift", file: "using-queries-2-9-example.swift")
}

@Step {
Swap the `cursor` for our new `resolvingCursor`.
Swap the `cursor` for our new `resolvingSequence`.

We now get one match, just like we'd expect.

@Code(name: "TreeSitterExample.swift", file: "using-queries-2-10-example.swift") {
@Image(source: "resolving-cursor-output.png", alt: "")
@Code(name: "TreeSitterExample.swift", file: "using-queries-2-9-example.swift") {
@Image(source: "resolved-output.png", alt: "One range {21, 8}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
@Chapter(name: "Doing more with Queries") {
Learn how to take full advantage of the tree-sitter query system.

@TutorialReference(tutorial: "doc:Resolving-Query-Cursors")
@TutorialReference(tutorial: "doc:Resolving-Queries")
}
}
4 changes: 2 additions & 2 deletions Tests/SwiftTreeSitterLayerTests/LanguageLayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class LanguageLayerTests: XCTestCase {

let highlightQuery = try! Query(language: language, data: queryText.data(using: .utf8)!)

return LanguageConfiguration(language: language,
return LanguageConfiguration(language,
name: "Swift",
queries: [.highlights: highlightQuery])
}()
Expand All @@ -38,7 +38,7 @@ final class LanguageLayerTests: XCTestCase {

queries[.injections] = injectionQuery

return LanguageConfiguration(language: swiftConfig.language,
return LanguageConfiguration(swiftConfig.language,
name: swiftConfig.name,
queries: queries)
}()
Expand Down

0 comments on commit 2599e95

Please sign in to comment.