Skip to main content

Parse rate badge Crates.io badge NPM badge Build

tree-sitter-swift

This contains a tree-sitter grammar for the Swift programming language.

Getting started

To use this parser to parse Swift code, you'll want to depend on either the Rust crate or the NPM package.

Rust

To use the Rust crate, you'll add this to your Cargo.toml:

tree-sitter = "0.23.0"
tree-sitter-swift = "=0.7.4"

Then you can use a tree-sitter parser with the language declared here:

let mut parser = tree_sitter::Parser::new();
parser.set_language(tree_sitter_swift::language())?;

// ...

let tree = parser.parse(&my_source_code, None)
    .ok_or_else(|| /* error handling code */)?;

Javascript

To use this from NPM, you'll add similar dependencies to package.json:

"dependencies: {
  "tree-sitter-swift": "0.7.4",
  "tree-sitter": "^0.22.1"
}

Your usage of the parser will look like:

const Parser = require("tree-sitter");
const Swift = require("tree-sitter-swift");

const parser = new Parser();
parser.setLanguage(Swift);

// ...

const tree = parser.parse(mySourceCode);

Editing the grammar

With this package checked out, a common workflow for editing the grammar will look something like:

  1. Make a change to grammar.ts.
  2. Run npm install && npm test to see whether the change has had impact on existing parsing behavior. The default npm test target requires valgrind to be installed; if you do not have it installed, and do not wish to, you can substitute tree-sitter test directly.
  3. Run tree-sitter parse on some real Swift codebase and see whether (or where) it fails.
  4. Use any failures to create new corpus test cases.

Contributions

All contributions to this repository are welcome.

If said contribution is to check generated files (e.g., parser.c) into the repository, be aware that your contribution will not be accepted. Make sure to read the FAQ entry and the prior discussions and compromises that have occurred already on this topic.

Using tree-sitter-swift in Web Assembly

To use tree-sitter-swift as a language for the web bindings version tree-sitter, which will likely be a more modern version than the published node module. see. Follow the instructions below

  1. Install the node modules npm install web-tree-sitter tree-sitter-swift
  2. Run the tree-sitter cli to create the wasm bundle
    $ npx tree-sitter build-asm ./node_modules/tree-sitter
    
  3. Boot tree-sitter wasm like this.
const Parser = require("web-tree-sitter");
async function run() {
  //needs to happen first
  await Parser.init();
  //wait for the load of swift
  const Swift = await Parser.Language.load("./tree-sitter-swift.wasm");

  const parser = new Parser();
  parser.setLanguage(Swift);

  //Parse your swift code here.
  const tree = parser.parse('print("Hello, World!")');
}
//if you want to run this
run().then(console.log, console.error);

Frequently asked questions

Where is your parser.c?

This repository currently omits most of the code that is autogenerated during a build. This means, for instance, that grammar.json and parser.c are both only available following a build. It also significantly reduces noise during diffs.

The side benefit of not checking in parser.c is that you can guarantee backwards compatibility. Parsers generated by the tree-sitter CLI aren't always backwards compatible. If you need a parser, generate it yourself using the CLI; all the information to do so is available in this package. By doing that, you'll also know for sure that your parser version and your library version are compatible.

If you need a parser.c, and you don't care about the tree-sitter version, but you don't have a local setup that would allow you to obtain the parser, you can just download one from a recent workflow run in this package. To do so:

  • Go to the GitHub actions page for this repository.
  • Click on the "Publish grammar.json and parser.c" action for the appropriate commit.
  • Go down to Artifacts and click on generated-parser-src. All the relevant parser files will be available in your download.

Metadata

Release files for tree-sitter-swift 0.7.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tree-sitter-swift 0.7.4
File Size Uploaded
tree_sitter_swift-0.7.4.tar.gz 1.2 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for tree-sitter-swift 0.7.4
File
tree_sitter_swift-0.7.4-cp38-abi3-win_arm64.whl CPython 3.8 abi3 Windows ARM64 Details
tree_sitter_swift-0.7.4-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
tree_sitter_swift-0.7.4-cp38-abi3-musllinux_1_2_x86_64.whl CPython 3.8 abi3 Linux musl 1.2+ x86-64 Details
tree_sitter_swift-0.7.4-cp38-abi3-musllinux_1_2_aarch64.whl CPython 3.8 abi3 Linux musl 1.2+ ARM64 Details
tree_sitter_swift-0.7.4-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
tree_sitter_swift-0.7.4-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.8 abi3 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
tree_sitter_swift-0.7.4-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details
tree_sitter_swift-0.7.4-cp38-abi3-macosx_10_9_x86_64.whl CPython 3.8 abi3 macOS 10.9+ x86-64 Details

Total release size: 4.5 MB

Release files / tree_sitter_swift-0.7.4.tar.gz

Download URL tree_sitter_swift-0.7.4.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
1676a8ee04442993b84c239291d5976b823d22d25b02cfc5161a1bd644b390f5
BLAKE2b-256 checksum
How to use checksums
f2988265b5ee5f590654935742d89c187460deb867411307680e1ed7cb39979a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-win_arm64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-win_arm64.whl
Size 500.0 kB
Tags CPython 3.8 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
3d532f3f735e40c0cb84c101dcb43e04e25ab39e5bf6803ea2344a7e45b0cff3
BLAKE2b-256 checksum
How to use checksums
70b07582b73528301e4aad3a5c786bf1d10622ec45f8d9676cf0c95c4a699b6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-win_amd64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-win_amd64.whl
Size 396.4 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6edfdeff86d842e7ee54f2a5d77dad9b6d9cc8b9f1ae0342e1c4349c44348803
BLAKE2b-256 checksum
How to use checksums
229fcdc6712032be81a590f275603936a325943424824fab6956bf7e3382f20a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-musllinux_1_2_x86_64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-musllinux_1_2_x86_64.whl
Size 423.8 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
32bfed7a3a41f6657a836684e47a876affd180e28ee0bd104fe67904be12e74c
BLAKE2b-256 checksum
How to use checksums
ec7f6e21e938ad20bb6fd1f14723c99584e3663fd3182daf3666baec551f3cd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-musllinux_1_2_aarch64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-musllinux_1_2_aarch64.whl
Size 413.9 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
6d66607643e65e059b57ffbfcbb8b68db168b1a8709449611cb92646825b86e5
BLAKE2b-256 checksum
How to use checksums
260c21964e32a543fbe4ac8dc6c138a2cd03f40aef9782c4c0b836dd058e08a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 415.6 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
f54606c229c32ba130fbb005cbfb99e50ccfa0023ab9d1ffd45850515c893a7b
BLAKE2b-256 checksum
How to use checksums
e811b5a3c0a3bf1bdf64dbaa0ebd71c1cfeff22aa13899443674175c76fdb500
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 425.9 kB
Tags CPython 3.8 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 abi3
SHA-256 checksum
How to use checksums
d11d15a479d7d3b14c7fc526b4daab107995feca5752936fec5c60cc7fa6fe4d
BLAKE2b-256 checksum
How to use checksums
9cd105bcef72968411fefb7d6a0a2ccfb5bb3d248600949fe01fb80c6c3db57a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-macosx_11_0_arm64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-macosx_11_0_arm64.whl
Size 417.1 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
62ce592607fa532687a2d4656a90db2f04cbc197e34077038bca2e47073b6db7
BLAKE2b-256 checksum
How to use checksums
3d94ae2a4abb3f240b613635507e83893ad192bbc58074f811eeec65dba38ab6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tree_sitter_swift-0.7.4-cp38-abi3-macosx_10_9_x86_64.whl

Download URL tree_sitter_swift-0.7.4-cp38-abi3-macosx_10_9_x86_64.whl
Size 388.2 kB
Tags CPython 3.8 abi3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
e76bc562815bbeb4a6504a9fac6699ffc2b779ef55e12ea863f6a3f8fa0dfee6
BLAKE2b-256 checksum
How to use checksums
5d9913ce6e8b5ed496fff03a8d04e34d7dd75f60166bea50c435da4a84ddb0f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.7.4 This release

9 release files

0.7.3

9 release files

0.7.2

9 release files

0.0.1

8 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page