- Notifications
You must be signed in to change notification settings - Fork68
A tree-sitter grammar for the Swift programming language.
License
alex-pinkus/tree-sitter-swift
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This contains atree-sitter grammar for the Swift programming language.
To use this parser to parse Swift code, you'll want to depend on either the Rust crate or the NPM package.
To use the Rust crate, you'll add this to yourCargo.toml:
tree-sitter = "0.23.0"tree-sitter-swift = "=0.7.0"Then you can use atree-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 */)?;To use this from NPM, you'll add similar dependencies topackage.json:
"dependencies: { "tree-sitter-swift": "0.7.0", "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);With this package checked out, a common workflow for editing the grammar will look something like:
- Make a change to
grammar.ts. - Run
npm install && npm testto see whether the change has had impact on existing parsing behavior. The defaultnpm testtarget requiresvalgrindto be installed; if you do not have it installed, and do not wish to, you cansubstitutetree-sitter testdirectly. - Run
tree-sitter parseon some real Swift codebase and see whether (or where) it fails. - Use any failures to create new corpus test cases.
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 theFAQ entry and thepriordiscussions andcompromises that have occurred already on this topic.
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 nodemodule.see. Follow the instructions below
- Install the node modules
npm install web-tree-sitter tree-sitter-swift - Run the tree-sitter cli to create the wasm bundle
$ npx tree-sitter build-asm ./node_modules/tree-sitter
- Boot tree-sitter wasm like this.
constParser=require("web-tree-sitter");asyncfunctionrun(){//needs to happen firstawaitParser.init();//wait for the load of swiftconstSwift=awaitParser.Language.load("./tree-sitter-swift.wasm");constparser=newParser();parser.setLanguage(Swift);//Parse your swift code here.consttree=parser.parse('print("Hello, World!")');}//if you want to run thisrun().then(console.log,console.error);
This repository currently omits most of the code that is autogenerated during a build. This means, for instance, thatgrammar.json andparser.c are both only available following a build. It also significantly reduces noise duringdiffs.
The side benefit of not checking inparser.c is that you can guarantee backwards compatibility. Parsers generated bythe tree-sitter CLI aren't always backwards compatible. If you need a parser, generate it yourself using the CLI; allthe information to do so is available in this package. By doing that, you'll also know for sure that your parser versionand your library version are compatible.
If you need aparser.c, and you don't care about the tree-sitter version, but you don't have a local setup that wouldallow you to obtain the parser, you can just download one from a recent workflow run in this package. To do so:
- Go to theGitHub actions page for thisrepository.
- Click on the "Publish
grammar.jsonandparser.c" action for the appropriate commit. - Go down to
Artifactsand click ongenerated-parser-src. All the relevant parser files will be available in yourdownload.
About
A tree-sitter grammar for the Swift programming language.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.