tree-sitter-swift

raw JSON →
0.7.2 verified Sat May 09 auth: no python

Swift grammar for tree-sitter, providing incremental parsing, syntax highlighting, and code analysis for the Swift programming language. Current version: 0.7.2. Released irregularly, with updates following Swift language evolution and tree-sitter releases.

pip install tree-sitter-swift
error AttributeError: module 'tree_sitter_swift' has no attribute 'Language'
cause Importing a non-existent attribute `Language`; the correct import is the function `language` without capital L.
fix
Change import to: from tree_sitter_swift import language
error TypeError: Parser() takes no arguments
cause Passing the language object directly to `Parser()` in older tree-sitter versions (<0.23) or using the wrong API pattern.
fix
Use the newer API: parser = ts.Parser(language())
error tree_sitter_swift does not provide bindings for version x.y.z
cause Attempting to install a version of `tree-sitter-swift` that does not have a prebuilt wheel for the current platform or Python version.
fix
Ensure you have Rust installed to compile from source, or use a version that supports your platform: pip install 'tree-sitter-swift>=0.7'
breaking In version 0.7.0, the grammar was updated to tree-sitter 0.23, introducing breaking changes in the tree-sitter API. Ensure you are using tree-sitter >= 0.23.0.
fix Update tree-sitter to >=0.23.0: pip install 'tree-sitter>=0.23'
deprecated The import path from tree_sitter_swift import Language is deprecated. Use from tree_sitter_swift import language instead.
fix Replace `from tree_sitter_swift import Language` with `from tree_sitter_swift import language` and call `language()`.
gotcha Grammar functions may be pickled incorrectly if you try to serialize the parser object with the grammar loaded.
fix Re-create the parser and load the grammar after deserialization instead of pickling the parser directly.

Create a parser with the Swift language and parse a simple Swift snippet.

import tree_sitter as ts
from tree_sitter_swift import language

parser = ts.Parser(language())
tree = parser.parse(b"let x = 42")
print(tree.root_node.type)