Skip to main content

Swift grammar for tree-sitter

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_swift-0.7.3.tar.gz (1.0 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_swift-0.7.3-cp38-abi3-win_arm64.whl (359.2 kB view details)

Uploaded CPython 3.8+Windows ARM64

tree_sitter_swift-0.7.3-cp38-abi3-win_amd64.whl (365.0 kB view details)

Uploaded CPython 3.8+Windows x86-64

tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_x86_64.whl (385.6 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_aarch64.whl (380.3 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tree_sitter_swift-0.7.3-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (381.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_swift-0.7.3-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (387.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_swift-0.7.3-cp38-abi3-macosx_11_0_arm64.whl (381.0 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tree_sitter_swift-0.7.3-cp38-abi3-macosx_10_9_x86_64.whl (358.0 kB view details)

Uploaded CPython 3.8+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_swift-0.7.3.tar.gz.

File metadata

  • Download URL: tree_sitter_swift-0.7.3.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tree_sitter_swift-0.7.3.tar.gz
Algorithm Hash digest
SHA256 a87f1dba3050a346ee3442aad8d727afd74555dea258e31c71c7934d8c04af9b
MD5 adc25d6cb6e8d83ad955d7eb3c38c661
BLAKE2b-256 faaa8e7b789bb74ad7b9efb784bfb7d42bbcf064288d7716a72b68211ac6c3d4

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 af44acc50d16f284abb607ae0cf7f81011d5566283d6c62a045a549a9331a653
MD5 55a4808325bc3f5f18307eecaa71b73d
BLAKE2b-256 707bfaf0fa8a99a217952b57aa43ed1b85ede798b3e8af51344cb5234766f718

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4b1de6122cbd82b2cea6d3a295f9f5f9297601b829061119e161da17a7ba7d17
MD5 93651daf6c59f167635721535cb1bc8f
BLAKE2b-256 6bbbe4e12fa0523c1acb2f9c4cebc454cd5415e94c915ad7f0b4b151ad13bc30

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 01c1e812289a2f7f01f63627a5d94a0b57d69332e8b52624becfe79ee8061651
MD5 ab941032edc19274df635b0c8a7b224b
BLAKE2b-256 891c7ed9e76f14918106a27c548efc64f123af4b8e6424fcae13481683bb09a4

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f30c30831f090ebe245f54ddcd280d2c5f7020ba17d6bbec1662bbfae140c467
MD5 94b1ed9ab03a4bba58165717a309fbd4
BLAKE2b-256 813448014e4cee1e2cf194675beeb435612a781f5cfa3c6f0e14b023b70c5cd7

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 eee02fecb60a07267edd123148c583d6ec9efc5d7fcb25e53da4e56869fd4cf3
MD5 a66592b738f556a72e1872c3f145499c
BLAKE2b-256 ff380b7c4d195d03396c19a7968a13342c89cb8322d97c4882bb7c4240adf419

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 f38feeb4f7350c8b30d567a0dc08bf1eeaa67c241b6888d72a45a8b1a4aa7187
MD5 ca61725de815cd94737bbedc42fa0603
BLAKE2b-256 e19a55f6cc9aad9079facf166d616472fd8e05007cbee9c62b749e153bf0521d

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ee627e027d0868c552beca13dcdfa9944662b126f642464c5038ee3204e68340
MD5 b3b8f42669463fc29e33a174486f263d
BLAKE2b-256 5d3784e2bc7826eb9007c531f47e5557461c5a48fd14bd3ea82424afa3d06b5f

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.3-cp38-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.3-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2531ec866c22ea52384e2786e07f3b2bb396c6446428a2df02cc74af3f7e6b6a
MD5 f3f9fa57db15869f1b066f8cd868a85c
BLAKE2b-256 9a9ddf190b08548dcfa67790d3197442989b3dd5e46d31ee61a1b9ecea35d57b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page