tree-sitter-swift
A tree-sitter grammar for the Swift programming language
Repository Health
Technical Analysis
tree-sitter-swift is a tree-sitter grammar for the Swift programming language. It parses Swift source into a concrete syntax tree that updates incrementally as code changes, providing fast, error-tolerant parsing for editors, linters, and code-analysis tooling.
The project tracks a high parse rate against real-world Swift repositories and ships bindings for Rust, Node, and other ecosystems. The Rust crate and npm package are both published as tree-sitter-swift, making it straightforward to add Swift language support to tree-sitter-based tools.
What You Get
- A complete tree-sitter grammar for Swift with a generated C parser
- Language bindings including a Rust crate and an npm package
- Highlight and query files for editor integration
- A tracked parse-rate benchmark against real Swift codebases
Common Use Cases
- Syntax highlighting for Swift in tree-sitter-based editors
- Structural navigation, folding, and selection in IDEs
- Linting, formatting, and static analysis of Swift code
- Language-server and code-intelligence backends for Swift
Under The Hood
Architecture - The grammar is authored in grammar.js using tree-sitter’s DSL and compiled to a C parser with an external scanner that handles Swift’s whitespace- and context-sensitive constructs. Bindings expose a language() entry point for consumers to register with a tree-sitter runtime.
Tech Stack - The grammar definition is JavaScript; the generated parser is C. A Rust crate depends on the tree-sitter language crate with a cc build step, and an npm package is published for Node. GitHub Actions runs build and parse-rate workflows.
Code Quality - The repo maintains a parse-rate badge scored against top Swift repositories, CI workflows, and a corpus of parser tests, indicating an actively benchmarked and maintained grammar.
API Design - Consuming the grammar is a one-call affair: import the binding and pass its language object to a tree-sitter parser. Shipped queries minimize editor glue, and consistent crate/npm naming eases discovery.