tree-sitter/swift-tree-sitter — dự án di động — đang có 410 sao GitHub trong nhóm Mobile. Swift API for the tree-sitter incremental parsing system
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Swift API for the tree-sitter incremental parsing system.
Close to full coverage of the C API
Swift/Foundation types where possible
Standard query result mapping for highlights and injections
Query predicate/directive support via ResolvingQueryMatchSequence
Nested language support
Swift concurrency support where possible
Structure
This project is actually split into two parts: SwiftTreeSitter and SwiftTreeSitterLayer.
The SwiftTreeSitter target is a close match to the C runtime API. It adds only a few additional types to help support querying. It is fairly low-level, and there will be significant work to use it in a real project.
SwiftTreeSitterLayer is an abstraction built on top of SwiftTreeSitter. It supports documents with nested languages and transparent querying across those nestings. It also supports asynchronous language resolution. While still low-level, SwiftTreeSitterLayer is easier to work with while also supporting more features.
And yet there's more! If you are looking a higher-level system for syntax highlighting and other syntactic operations, you might want to have a look at Neon. It is much easier to integrate with a text system, and has lots of additional performance-related features.
The tree-sitter runtime operates on raw string data. This means it works with bytes, and is string-encoding-sensitive. Swift's String type is an abstraction on top of raw data and cannot be used directly. To overcome this, you also have to be aware of the types of indexes you are using and how string data is translated back and forth.
To help, SwiftTreeSitter supports the base tree-sitter encoding facilities. You can control this via Parser.parse(tree:encoding:readBlock:). But, by default this will assume UTF-16-encoded data. This is done to offer direct compatibility with Foundation strings and NSRange, which both use UTF-16.
Also, to help with all the back and forth, SwiftTreeSitter includes some accessors that are NSRange-based, as well as extension on NSRange. These must be used when working with the native tree-sitter types unless you take care to handle encoding yourself.
To keep things clear, consistent naming and types are used. Node.byteRange returns a Range<UInt32>, which is an encoding-dependent value. Node.range is an NSRange which is defined to use UTF-16.
let node = tree.rootNode!
// this is encoding-dependent and cannot be used with your storage
node.byteRange
// this is a UTF-16-assumed translation of the byte ranges
node.range
// converting UTF-16-based changed ranges on re-parse
let ranges: [NSRange] = newtree.changedRanges(from: oldTree)
.map{ $0.bytes.range }
Query Conflicts
SwiftTreeSitter does its best to resolve poor/incorrect query constructs, which are surprisingly common.
When using injections, child query ranges are automatically expanded using parent matches. This handles cases where a parent has queries that overlap with children in conflicting ways. Without expansion, it is possible to construct queries that fall within children ranges but produce on parent matches.
All matches are sorted by:
depth
location in content
specificity of match label (more components => more specific)
occurrence in the query source
Even with these, it is possible to produce queries that will result in "incorrect" behavior that are either ambiguous or undefined in the query definition.
Highlighting
A very common use of tree-sitter is to do syntax highlighting. It is possible to use this library directly, especially if your source text does not change. Here's a little example that sets everything up with a SPM-bundled language.
First, check out how it works with SwiftTreeSitterLayer. It's complex, but does a lot for you.
// LanguageConfiguration takes care of finding and loading queries in SPM-created bundles.
let markdownConfig = try LanguageConfiguration(tree_sitter_markdown(), name: "Markdown")
let markdownInlineConfig = try LanguageConfiguration(
tree_sitter_markdown_inline(),
name: "MarkdownInline",
bundleName: "TreeSitterMarkdown_TreeSitterMarkdownInline"
)
let swiftConfig = try LanguageConfiguration(tree_sitter_swift(), name: "Swift")
// Unfortunately, injections do not use standardized language names, and can even be content-dependent. Your system must do this mapping.
let config = LanguageLayer.Configuration(
languageProvider: {
name in
switch name {
case "markdown":
return markdownConfig
case "markdown_inline":
return markdownInlineConfig
case "swift":
return swiftConfig
default:
return nil
}
}
)
let rootLayer = try LanguageLayer(languageConfig: markdownConfig, configuration: config)
let source = """
# this is markdown
```swift
func main(a: Int) {
}
```
## also markdown
```swift
let value = "abc"
```
"""
rootLayer.replaceContent(with: source)
let fullRange = NSRange(source.startIndex..<source.endIndex, in: source)
let textProvider = source.predicateTextProvider
let highlights = try rootLayer.highlights(in: fullRange, provider: textProvider)
for namedRange in highlights {
print("\(namedRange.name): \(namedRange.range)")
}
You can also use SwiftTreeSitter directly:
let swiftConfig = try LanguageConfiguration(tree_sitter_swift(), name: "Swift")
let parser = Parser()
try parser.setLanguage(swiftConfig.language)
let source = """
func main() {}
"""
let tree = parser.parse(source)!
let query = swiftConfig.queries[.highlights]!
let cursor = query.execute(in: tree)
let highlights = cursor
.resolve(with: .init(string: source))
.highlights()
for namedRange in highlights {
print("range: ", namedRange)
}
Language Parsers
Tree-sitter language parsers are separate projects, and you'll probably need at least one. More details are available in the documentation. How they can be installed an incorporated varies.
Here's a list of parsers that support SPM. Since you're here, you might find that convenient. And the LanguageConfiguration type supports loading bundled queries directly.
Parser
Make
SPM
Official Repo
Bash
✅
✅
C
✅
✅
C++
✅
✅
C#
✅
✅
Clojure
✅
CSS
✅
✅
✅
Dockerfile
✅
✅
✅
Diff
✅
✅
Elixir
✅
✅
✅
Elm
✅
✅
Go
✅
✅
✅
GoMod
✅
✅
✅
GoWork
✅
Haskell
✅
✅
HCL
✅
✅
HTML
✅
✅
Java
✅
✅
✅
Javascript
✅
✅
JSON
✅
✅
✅
JSDoc
✅
✅
Julia
✅
✅
Kotlin
✅
Latex
✅
✅
✅
Lua
✅
✅
Markdown
✅
✅
OCaml
✅
✅
Perl
✅
✅
PHP
✅
✅
✅
Pkl
✅
✅
Python
✅
✅
Ruby
✅
✅
✅
Rust
✅
✅
Scala
✅
✅
SQL
✅
✅
SSH
✅
✅
Swift
✅
✅
✅
TOML
✅
Tree-sitter query language
✅
✅
Typescript
✅
✅
Verilog
✅
✅
YAML
✅
Zig
✅
✅
✅
Contributing and Collaboration
I would love to hear from you! Issues or pull requests work great. A Discord server is also available for live help, but I have a strong bias towards answering in the form of documentation.
I prefer collaboration, and would love to find ways to work together if you have a similar project.
I prefer indentation with tabs for improved accessibility. But, I'd rather you use the system you want and make a PR than hesitate because of whitespace.
By participating in this project you agree to abide by the Contributor Code of Conduct.
tree-sitter/swift-tree-sitter thuộc nhóm Mobile trên TopGit, cùng 6 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về tree-sitter/swift-tree-sitter ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/tree-sitter/swift-tree-sitter là nguồn chính thức.
tree-sitter/swift-tree-sitter có bao nhiêu sao?
tree-sitter/swift-tree-sitter có 410 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/tree-sitter/swift-tree-sitter. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
tree-sitter/swift-tree-sitter có những chủ đề gì?
GitHub topics của tree-sitter/swift-tree-sitter: "ios", "macos", "parser", "parsing", "swift", "tree-sitter". TopGit xếp repo vào nhóm Mobile.
tree-sitter/swift-tree-sitter còn đang phát triển không?
Commit gần nhất trên tree-sitter/swift-tree-sitter là 5 ngày trước (theo timestamp GitHub). Repo có 53 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
tree-sitter/swift-tree-sitter viết bằng ngôn ngữ gì?
tree-sitter/swift-tree-sitter chủ yếu viết bằng Swift. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao tree-sitter/swift-tree-sitter được xếp vào nhóm Mobile?
TopGit xếp tree-sitter/swift-tree-sitter vào nhóm Mobile dựa trên GitHub topics và mô tả của repo (gắn thẻ: "ios", "macos", "parser"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về swift-tree-sitter?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về swift-tree-sitter.