-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1eceac
commit 0e413d6
Showing
10 changed files
with
190 additions
and
67 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
Sources/SwiftTreeSitter/Documentation.docc/Code/using-essentials-4-1-example.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import SwiftTreeSitter | ||
import TreeSitterSwift | ||
|
||
let language = Language(language: tree_sitter_swift()) | ||
|
||
let parser = Parser() | ||
try parser.setLanguage(language) | ||
|
||
let source = """ | ||
func hello() { | ||
print("hello from tree-sitter") | ||
} | ||
""" | ||
|
||
let tree = parser.parse(source) | ||
|
||
print("tree: ", tree) |
23 changes: 0 additions & 23 deletions
23
Sources/SwiftTreeSitter/Documentation.docc/Code/using-essentials-4-1-queries.swift
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
Sources/SwiftTreeSitter/Documentation.docc/Code/using-essentials-4-2-example.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import SwiftTreeSitter | ||
import TreeSitterSwift | ||
|
||
let language = Language(language: tree_sitter_swift()) | ||
|
||
let parser = Parser() | ||
try parser.setLanguage(language) | ||
|
||
let source = """ | ||
func hello() { | ||
print("hello from tree-sitter") | ||
} | ||
""" | ||
|
||
let tree = parser.parse(source) | ||
|
||
print("tree: ", tree) | ||
|
||
let url = Bundle.main | ||
.resourceURL | ||
.appendingPathComponent("TreeSitterSwift_TreeSitterSwift.bundle") | ||
.appendingPathComponent("queries/highlights.scm") |
24 changes: 24 additions & 0 deletions
24
Sources/SwiftTreeSitter/Documentation.docc/Code/using-essentials-4-3-example.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import SwiftTreeSitter | ||
import TreeSitterSwift | ||
|
||
let language = Language(language: tree_sitter_swift()) | ||
|
||
let parser = Parser() | ||
try parser.setLanguage(language) | ||
|
||
let source = """ | ||
func hello() { | ||
print("hello from tree-sitter") | ||
} | ||
""" | ||
|
||
let tree = parser.parse(source) | ||
|
||
print("tree: ", tree) | ||
|
||
let url = Bundle.main | ||
.resourceURL | ||
.appendingPathComponent("TreeSitterSwift_TreeSitterSwift.bundle") | ||
.appendingPathComponent("queries/highlights.scm") | ||
|
||
let query = try language.query(contentsOf: url!) |
26 changes: 26 additions & 0 deletions
26
Sources/SwiftTreeSitter/Documentation.docc/Code/using-essentials-4-4-example.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import SwiftTreeSitter | ||
import TreeSitterSwift | ||
|
||
let language = Language(language: tree_sitter_swift()) | ||
|
||
let parser = Parser() | ||
try parser.setLanguage(language) | ||
|
||
let source = """ | ||
func hello() { | ||
print("hello from tree-sitter") | ||
} | ||
""" | ||
|
||
let tree = parser.parse(source) | ||
|
||
print("tree: ", tree) | ||
|
||
let url = Bundle.main | ||
.resourceURL | ||
.appendingPathComponent("TreeSitterSwift_TreeSitterSwift.bundle") | ||
.appendingPathComponent("queries/highlights.scm") | ||
|
||
let query = try language.query(contentsOf: url!) | ||
|
||
let cursor = query.execute(node: tree.rootNode!) |
30 changes: 30 additions & 0 deletions
30
Sources/SwiftTreeSitter/Documentation.docc/Code/using-essentials-4-5-example.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import SwiftTreeSitter | ||
import TreeSitterSwift | ||
|
||
let language = Language(language: tree_sitter_swift()) | ||
|
||
let parser = Parser() | ||
try parser.setLanguage(language) | ||
|
||
let source = """ | ||
func hello() { | ||
print("hello from tree-sitter") | ||
} | ||
""" | ||
|
||
let tree = parser.parse(source) | ||
|
||
print("tree: ", tree) | ||
|
||
let url = Bundle.main | ||
.resourceURL | ||
.appendingPathComponent("TreeSitterSwift_TreeSitterSwift.bundle") | ||
.appendingPathComponent("queries/highlights.scm") | ||
|
||
let query = try language.query(contentsOf: url!) | ||
|
||
let cursor = query.execute(node: tree.rootNode!) | ||
|
||
for match in cursor { | ||
print("match: ", match) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 39 additions & 34 deletions
73
Sources/SwiftTreeSitter/Documentation.docc/Tutorials/Queries.tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,52 @@ | ||
@Tutorial(time: 10) { | ||
@Intro(title: "Queries") { | ||
Use the tree-sitter query system to inspect the syntax tree. | ||
@Intro(title: "Running Queries") { | ||
Building and maintaing a parse tree on its own isn't very useful. You'll probably want to inspect the tree using language-specific patterns and matches. The tree-sitter query system is built to do exactly that. | ||
} | ||
|
||
@Section(title: "Getting Query Definitions") { | ||
@ContentAndMedia { | ||
text | ||
Let's build on our original example by running queries against our tree. You can write your own queries. Here, we'll use `highlights.scm`, a file many parser include for synatx highlighting. | ||
} | ||
|
||
@Steps { | ||
@Step { | ||
one | ||
Here we've parsed some text and have tree object set up. | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-1-queries.swift") | ||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-1-example.swift") | ||
} | ||
|
||
@Step { | ||
Create a `URL` to the query definition file. | ||
|
||
These are copied into the Swift packages. Their locations differ for macOS and iOS. | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-2-example.swift") | ||
} | ||
|
||
@Step { | ||
Initialize the `query` object. | ||
|
||
This can be expensive, depending on the language grammar/queries. | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-3-example.swift") | ||
} | ||
|
||
@Step { | ||
Execute the query. | ||
|
||
Queries must be run against a tree | ||
This produces a `QueryCursor`, which can be used to iterate over the results. | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-4-example.swift") | ||
} | ||
|
||
@Step { | ||
Use a loop to print all of the matches. | ||
|
||
`QueryCursor` conforms to `Sequence`. | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-5-example.swift") | ||
} | ||
} | ||
} | ||
|
||
@Section(title: "Executing Queries") { | ||
@ContentAndMedia { | ||
text | ||
} | ||
|
||
@Steps { | ||
@Step { | ||
one | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-1-queries.swift") | ||
} | ||
} | ||
} | ||
|
||
@Section(title: "Using ResolvingQueryCursor") { | ||
@ContentAndMedia { | ||
text | ||
} | ||
|
||
@Steps { | ||
@Step { | ||
one | ||
|
||
@Code(name: "TreeSitterExample.swift", file: "using-essentials-4-1-queries.swift") | ||
} | ||
} | ||
} | ||
} | ||
} |
18 changes: 12 additions & 6 deletions
18
Sources/SwiftTreeSitter/Documentation.docc/Tutorials/Using-Tree-Sitter.tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
@Tutorials(name: "Using SwiftTreeSitter") { | ||
@Intro(title: "Using SwiftTreeSitter") { | ||
SwiftTreeSitter provides a Swift interface to the tree-sitter incremental parsing system. You can use it to parse language text and get information about the syntatic elements contained. | ||
SwiftTreeSitter provides a Swift interface to the tree-sitter incremental parsing system. You can use it to parse language text, reparse it as it changes, and query the syntax tree. | ||
} | ||
|
||
@Chapter(name: "Essentials") { | ||
@Chapter(name: "Tree-Sitter Essentials") { | ||
Learn how to use parse text, process edits, and perform queries with tree-sitter. | ||
|
||
@TutorialReference(tutorial: "doc:Parsing") | ||
@TutorialReference(tutorial: "doc:Processing-Edits") | ||
@Comment { | ||
@TutorialReference(tutorial: "doc:Queries") | ||
} | ||
} | ||
@TutorialReference(tutorial: "doc:Queries") | ||
} | ||
|
||
@Comment { | ||
@Chapter(name: "SwiftTreeSitter Features") { | ||
Learn about APIs are that specific to SwiftTreeSitter. | ||
|
||
@TutorialReference(tutorial: "doc:ResolvingCursors") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters