Languages for the Tree-sitter parser generator wrapped in Swift packages.
There are two reasons this package exists:
- As an easy way to add several Tree-sitter languages to an app and particularly to an app that uses the Runestone framework.
- To show how a Tree-sitter language can be wrapped in a Swift package.
It is not recommended to use this package if you only need to add a few Tree-sitter languages to your app. Instead you should add the languages manually.
The languages are distributed using the Swift Package Manager as separated libraries in a single Swift package. Install the package in your project by adding it as a dependency in your Package.swift manifest or through "Package Dependencies" in your project settings.
let package = Package(
dependencies: [
.package(url: "git@github.com:simonbs/TreeSitterLanguages.git", from: "0.1.0")
]
)
The Swift package contains multiple libraries. Each language is wrapped in its own Swift package so you don't need to import a whole suite of languages in case you only need a few.
The package contains the following three libraries for each language.
Name | Purpose |
---|---|
TreeSitter{Language} | The C code for the generated Tree-sitter parser. Each package exposes a function named like tree_sitter_{language} . |
TreeSitter{Language}Queries | The queries to be used with the language. For more information on queries, please refer to Tree-sitter's documentation. The only reason this is a separate library and not part of the TreeSitter{Language} library is that C code and Swift code cannot be mixed in a library using Swift Package Manager. |
TreeSitter{Language}Runestone | Exposes the language to be used with Runestone. This library depends on the TreeSitter{Language} and TreeSitter{Language}Queries libraries and it is the only library you need to import when using Runestone. The library exposes the language as an extension on TreeSitterLanguage (e.g. TreeSitterLanguage.javaScript ) that's ready to be used in Runestone. It also includes the indentation specification for select languages. |
Furthermore the package contains the TreeSitterLanguagesCommon library which exposes base types from Tree-sitter like TSLanguage
. This is useful when you need to refer to these types in your source code, for example if you return a TSLanguage
from a function.
The parser.c file of a language is generated using tree-sitter-cli and a language generated with one version of the CLI may not be compatible with future versions of Tree-sitter. All languages in this repository are kept compatible with the version of Tree-sitter used by my Runestone framework.
The TreeSitterLanguages repository contains files copied from the following repositories. I had originally added these repositories as submodules but it turned out to be very impractical when the package is used with Swift Package Manager because checking out the repository and all the submodules would take a long time and doing so on a slow connection (for example when commuting) would be unbearable.
Thanks to all the contributors to these repositories for their great effort in ensuring Tree-sitter supports a wide array of languages.
- tree-sitter-bash
- tree-sitter-c
- tree-sitter-comment
- tree-sitter-c-sharp
- tree-sitter-cpp
- tree-sitter-css
- tree-sitter-elixir
- tree-sitter-elm
- tree-sitter-go
- tree-sitter-html
- tree-sitter-java
- tree-sitter-javascript
- tree-sitter-jsdoc
- tree-sitter-json
- tree-sitter-json5
- tree-sitter-markdown
- tree-sitter-ocaml
- tree-sitter-perl
- tree-sitter-php
- tree-sitter-python
- tree-sitter-regex
- tree-sitter-ruby
- tree-sitter-rust
- tree-sitter-scss
- tree-sitter-svelte
- tree-sitter-swift
- tree-sitter-toml
- tree-sitter-typescript
- tree-sitter-yaml
- nvim-treesitter