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

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.2",
  "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.2.tar.gz (926.8 kB 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.2-cp38-abi3-win_arm64.whl (333.4 kB view details)

Uploaded CPython 3.8+Windows ARM64

tree_sitter_swift-0.7.2-cp38-abi3-win_amd64.whl (337.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

tree_sitter_swift-0.7.2-cp38-abi3-musllinux_1_2_x86_64.whl (356.8 kB view details)

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

tree_sitter_swift-0.7.2-cp38-abi3-musllinux_1_2_aarch64.whl (353.5 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tree_sitter_swift-0.7.2-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (354.7 kB view details)

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

tree_sitter_swift-0.7.2-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (358.2 kB view details)

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

tree_sitter_swift-0.7.2-cp38-abi3-macosx_11_0_arm64.whl (352.2 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tree_sitter_swift-0.7.2-cp38-abi3-macosx_10_9_x86_64.whl (330.3 kB view details)

Uploaded CPython 3.8+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_swift-0.7.2.tar.gz
  • Upload date:
  • Size: 926.8 kB
  • 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.2.tar.gz
Algorithm Hash digest
SHA256 67b9a3ba5ab8fff2c082a2c0c33c8b5a66539f8bfa5058385688b1aefc11cead
MD5 e2aad1137b6b7300cef51deb34932321
BLAKE2b-256 d3456986ace9ad2eb7a111b7c47c8900192bc4d6c9f3db236fde873b7f8579c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 c7d11ca989e1930a55a79bbea5964fa1b121d947fa25ec7c068364383c85e6c3
MD5 2c89c78f9e57ae576c6c04a7ee956b6a
BLAKE2b-256 7d0043b80f23c282cd0391442c1e3e5d9e6fb8c3fd62add900d6879522dc81de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 dec5aa6bc475ccd41685ce88dfde5894077bed6123b85e89e2c027f5ab6ab09e
MD5 59c9d54ee698b6963f2628003bccda92
BLAKE2b-256 cb0e5eb7a57346a287fa9bd7d5757a9fc1cbaef4dc043093a565e91384a7df18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e4de7c8a789c6fe01e0e0ba2a2792e9d4db905eb146ed9a321502a848826ba84
MD5 c91ea299301cfb90d5aa5a320cd1734f
BLAKE2b-256 1cfb9acab9dd78a2fcbd04c90a42bd8f313d9ae719f4e3388cd1345d03bbe0de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 cfbd96472e4841dbacf903088044f4a6a0fb4fa5ef7084a5bf55a804fefcc013
MD5 ea4e4c4236a1b1ea748be8d911ec0daf
BLAKE2b-256 8f56befd27fac44be001e0489cdeed8c5837ebba4e1a92d2155460f5a53c5fe1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4c8398f0b105293bbae375c7701256772b90996044f822e8e590297cc671e6e4
MD5 3e34c26147f128b9972411bbfe6fb6e3
BLAKE2b-256 3404e6ded10edc9ece2a5812058dace35bbae03685547d4bee03af843b7a9ca5

See more details on using hashes here.

File details

Details for the file tree_sitter_swift-0.7.2-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.2-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 600053b3ed763beaa5156ba1d70b22602ed88a6cff6cf3aab238133983426f9e
MD5 b56ac47b635d44003cba35d2de546c9e
BLAKE2b-256 c9740af5181a67c71f09af7a9f7942ba8f65e22a4f4d6eed426e6daf6253d3a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d5791dbec5e4070accc0e06d231e18879d67edab98369685a81a1f77e024727
MD5 b8ed7e1433984a28710c5e557e3869f1
BLAKE2b-256 ddddaee99d2ccf0deb48e84656fefdecf059392a6778d3f050bf33cfa1d6074c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_swift-0.7.2-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4664a5cbf20f0090ea2de540abc4f3392479a89db516f9774a62885c1b61aac7
MD5 be66bdab68e5d9c3a1a0c668690fb1fa
BLAKE2b-256 5b7f98abba4def5dca30ece6e3cd9fb09f0cddbdc250fd2d050d1cfdbe0c8924

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