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.3"

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.3",
  "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.

Release files for tree-sitter-swift 0.7.3

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.3
File Size Uploaded
tree_sitter_swift-0.7.3.tar.gz 1.0 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for tree-sitter-swift 0.7.3
File
tree_sitter_swift-0.7.3-cp38-abi3-win_arm64.whl CPython 3.8 abi3 Windows ARM64 Details
tree_sitter_swift-0.7.3-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_x86_64.whl CPython 3.8 abi3 Linux musl 1.2+ x86-64 Details
tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_aarch64.whl CPython 3.8 abi3 Linux musl 1.2+ ARM64 Details
tree_sitter_swift-0.7.3-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.3-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.3-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details
tree_sitter_swift-0.7.3-cp38-abi3-macosx_10_9_x86_64.whl CPython 3.8 abi3 macOS 10.9+ x86-64 Details

Total release size: 4.0 MB

Release files / tree_sitter_swift-0.7.3.tar.gz

Download URL tree_sitter_swift-0.7.3.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
a87f1dba3050a346ee3442aad8d727afd74555dea258e31c71c7934d8c04af9b
BLAKE2b-256 checksum
How to use checksums
faaa8e7b789bb74ad7b9efb784bfb7d42bbcf064288d7716a72b68211ac6c3d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-win_arm64.whl
Size 359.2 kB
Tags CPython 3.8 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
af44acc50d16f284abb607ae0cf7f81011d5566283d6c62a045a549a9331a653
BLAKE2b-256 checksum
How to use checksums
707bfaf0fa8a99a217952b57aa43ed1b85ede798b3e8af51344cb5234766f718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-win_amd64.whl
Size 365.0 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
4b1de6122cbd82b2cea6d3a295f9f5f9297601b829061119e161da17a7ba7d17
BLAKE2b-256 checksum
How to use checksums
6bbbe4e12fa0523c1acb2f9c4cebc454cd5415e94c915ad7f0b4b151ad13bc30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_x86_64.whl
Size 385.6 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
01c1e812289a2f7f01f63627a5d94a0b57d69332e8b52624becfe79ee8061651
BLAKE2b-256 checksum
How to use checksums
891c7ed9e76f14918106a27c548efc64f123af4b8e6424fcae13481683bb09a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_aarch64.whl
Size 380.3 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
f30c30831f090ebe245f54ddcd280d2c5f7020ba17d6bbec1662bbfae140c467
BLAKE2b-256 checksum
How to use checksums
813448014e4cee1e2cf194675beeb435612a781f5cfa3c6f0e14b023b70c5cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 381.1 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
eee02fecb60a07267edd123148c583d6ec9efc5d7fcb25e53da4e56869fd4cf3
BLAKE2b-256 checksum
How to use checksums
ff380b7c4d195d03396c19a7968a13342c89cb8322d97c4882bb7c4240adf419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 387.0 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
f38feeb4f7350c8b30d567a0dc08bf1eeaa67c241b6888d72a45a8b1a4aa7187
BLAKE2b-256 checksum
How to use checksums
e19a55f6cc9aad9079facf166d616472fd8e05007cbee9c62b749e153bf0521d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-macosx_11_0_arm64.whl
Size 381.0 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ee627e027d0868c552beca13dcdfa9944662b126f642464c5038ee3204e68340
BLAKE2b-256 checksum
How to use checksums
5d3784e2bc7826eb9007c531f47e5557461c5a48fd14bd3ea82424afa3d06b5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

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

Download URL tree_sitter_swift-0.7.3-cp38-abi3-macosx_10_9_x86_64.whl
Size 358.0 kB
Tags CPython 3.8 abi3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
2531ec866c22ea52384e2786e07f3b2bb396c6446428a2df02cc74af3f7e6b6a
BLAKE2b-256 checksum
How to use checksums
9a9ddf190b08548dcfa67790d3197442989b3dd5e46d31ee61a1b9ecea35d57b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.7.3 This release

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