Swiftpack.co - simonbs/TreeSitterLanguages as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by simonbs.
simonbs/TreeSitterLanguages 0.1.1
Languages for Tree-sitter wrapped in Swift packages
⭐️ 13
🕓 2 weeks ago
.package(url: "https://github.com/simonbs/TreeSitterLanguages.git", from: "0.1.1")


Languages for the Tree-sitter parser generator wrapped in Swift packages.


There are two reasons this package exists:

  1. As an easy way to add several Tree-sitter languages to an app and particularly to an app that uses the Runestone framework.
  2. 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.


Stars: 13
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics