npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tree-sitter-parser-swift

v0.3.9

Published

Swift grammar for tree-sitter

Downloads

1

Readme

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.20.4"
tree-sitter-swift = "=0.3.6"

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.3.6",
  "tree-sitter": "^0.20.0"
}

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

If you have a change to make to this parser, and the change is a net positive, please submit a pull request. I mostly started this parser to teach myself how tree-sitter works, and how to write a grammar, so I welcome improvements. If you have an issue with the parser, please file a bug and include a test case to put in the corpus. I can't promise any level of support, but having the test case makes it more likely that I want to tinker with it.

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.