Swift wrappers for the tree-sitter incremental parsing system

Overview

SwiftTreeSitter

Swift wrappers for the tree-sitter incremental parsing system. Remember that tree-sitter has both runtime and per-language dependencies. They all have to be installed and build seperately.

Integration

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/ChimeHQ/SwiftTreeSitter")
]

Building Dependencies

SwiftTreeSitter needs the tree-sitter runtime libraries and headers. Your build configuration will, unfortunately, depend on how you want to package and distribute your final target. This is made even more complex because SPM currently does not allow you to select between a .dylib and .a when both are in the same directory. Static linking can simplify distribution, but SwiftTreeSitter should be compatible with both.

Ultimately, it could be that you cannot use this package without modification. I'd really prefer to make it more seamless, but I experimented with many different appraoches, and this was the only one that offered sufficient flexibility. If you have other ideas, please get in touch.

Note: These instructions assume a macOS target. Also, I've only tested tree-sitter down to 10.13. I suspect it will work with lower targets, but have not tried.

build

Check out and build tree-sitter from source.

CFLAGS="-arch arm64 -arch x86_64 -mmacosx-version-min=10.13" LDFLAGS="-mmacosx-version-min=10.13" make

install

Install it into /usr/local. This is where the Swift.package expects it to be.

sudo make install PREFIX=/usr/local

remove the dylib

This deletes the dylib, so SPM links statically. I really wish this under the control of consumer of the package, but as far as I can tell, SPM does not support that.

sudo rm /usr/local/lib/libtree-sitter*.dylib

Building Language Libraries

In addition to the runtime, tree-sitter you'll probably also want at least one language library. These are more complex to build than the runtime, because each lib requires a small amount of patching. It's a real pain.

check out the source

modify binding.gyp

An xcode_settings section needs to be added to binding.gyp. The -isysroot parameter could be unnecessary, depending on how your developer tools are installed/configured, and which SDK you want to build against.

And, again, I would imagine these libraries support macOS versions lower than 10.13, but I have not tried.

You should be able to use the template, replacing tree_sitter_language_binding with the language binding name.

build

You need npm to build a language binding. Earlier versions of tree-sitter required a specific NPM version, but more recent versions are less picky. A standard NPM install should work.

npm install

package and install

ar rcs libtree-sitter-LANGUAGE.a build/Release/obj.target/tree_sitter_LANGUAGE_binding/src/*
sudo cp libtree-sitter-LANGUAGE.a /usr/local/lib/

make a .h

To build against a language library, you'll need an .h file.

You can use the template, replacing TREE_SITTER_LANGUAGE_H_, tree_sitter_LANGUAGE, and the file name as appropriate.

sudo cp LANGUAGE.h /usr/local/include/tree_sitter/

make a .pc

This is useful when using SPM, but could be skipped if you are building/linking with another mechansim.

There's a template file for that as well. Remember to fill in VERSION and language as needed.

sudo cp tree-sitter-changes/tree-sitter-LANGUAGE.pc /usr/local/lib/pkgconfig/

Suggestions or Feedback

We'd love to hear from you! Get in touch via twitter, an issue, or a pull request.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Comments
  • Add tree-sitter-xcframework dependency

    Add tree-sitter-xcframework dependency

    Use https://github.com/krzyzanowskim/tree-sitter-xcframework as a SPM dependency for seamless integration without building & installing tree-sitter locally

    README needs an update, but I leave it to you.

    opened by krzyzanowskim 2
  • Workaround to make it compile in WASM

    Workaround to make it compile in WASM

    The generated code for tree-sitter-swift is that long that is making the WASM generation to exceed the local limits. Using wasm-opt -O is improving the generated code avoiding the issue.

    https://github.com/ChimeHQ/SwiftTreeSitter/issues/8

    This is a workaround, I have already notified the Swift WASM toolchain guys to try to solve that issue.

    Cheers

    opened by fjtrujy 1
  • Adding Swift WebAssembly Support

    Adding Swift WebAssembly Support

    Description

    This PR makes the project to be WASM-friendly.

    You can find more information about Swift WASM here

    The PR basically does:

    • Force to use headers from the project instead of from the machine/OS.
    • Disable not supported WASM functionality
    • Adding specific CI for WASM

    Cheers

    opened by fjtrujy 1
  • Investigate issues with emoji

    Investigate issues with emoji

    @kaunteya reported that there are issues with handling emoji. I think these are all typically multi-point characters in UTF16, which is on the rare side. But, it would mean there are encoding/decoding issues and need to be investigated.

    opened by mattmassicotte 0
  • WASM cannot use tree-sitter-swift for tests

    WASM cannot use tree-sitter-swift for tests

    I'm not 100% sure what the issue is, but something in CI is unhappy with the submodule I added to get access to the swift parser. @fjtrujy can I enlist some help?

    opened by mattmassicotte 8
Releases(0.7.1)
Owner
Chime
Chime
An RSS, Atom and JSON Feed parser written in Swift

Features Atom RSS 0.90, 0.91, 1.00, 2.00 JSON Namespaces Dublin Core Syndication Content Media RSS iTunes Podcasting Tags Documentation Unit Test Cove

Nuno Dias 1k Jan 7, 2023
A Powerful , Extensible CSS Parser written in pure Swift.

A Powerful , Extensible CSS Parser written in pure Swift. Basic Usage From CSS: #View { "width" : 118; "height" : 120.5; "color1" : "#888888"; "co

null 273 Sep 9, 2022
Recursive Length Prefix encoding written in Swift

RLPSwift This is a basic Swift implementation of Recursive Length Prefix Encoding, a serialisation method for encoding arbitrarily structured binary d

bitfwd community 22 Oct 6, 2020
Excel spreadsheet (XLSX) format parser written in pure Swift

CoreXLSX Excel spreadsheet (XLSX) format parser written in pure Swift CoreXLSX is a library focused on representing the low-level structure of the XML

null 684 Dec 21, 2022
CodeEditTextView - An Xcode-inspired code editor view written in Swift powered by tree-sitter for CodeEdit

An Xcode-inspired code editor view written in Swift powered by tree-sitter for CodeEdit. Features include syntax highlighting (based

CodeEdit 243 Jan 8, 2023
A Collection of Tree-Sitter Parsers for Syntax Highlighting

CodeEditLanguages A collection of tree-sitter languages for syntax highlighting. Overview This package includes a binary framework CodeLanguagesContai

CodeEdit 16 Dec 30, 2022
Profile-Tree - Profile in Tree structure developed in UIKit

Profile-Tree Profile in Tree structure developed in UIKit Screenshot Video Profi

null 1 Oct 7, 2022
Incremental update tool to UITableView and UICollectionView

EditDistance is one of the incremental update tool for UITableView and UICollectionView. The followings show how this library update UI. They generate

Kazuhiro Hayashi 90 Jun 9, 2022
Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Nike Inc. 611 Oct 23, 2022
CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS

CoreML-Face-Parsing The simple sample how to use face-parsing CoreML model in iO

MLBoy 6 Oct 25, 2022
SwiftUI library to easily render diagrams given a tree of objects. Similar to ring chart, sunburst chart, multilevel pie chart.

Swift Sunburst Diagram Sunburst diagram is a library written with SwiftUI to easily render diagrams given a tree of objects. Similar to ring chart, su

Ludovic Landry 494 Dec 19, 2022
List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model and doesn't depend on UI framework

SwiftListTreeDataSource List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model, so can

Dzmitry Antonenka 26 Nov 26, 2022
Profile in Tree structure developed in UIKit

Profile-Tree Profile in Tree structure developed in UIKit Screenshot Video Profi

Stebin Alex 3 Dec 31, 2021
šŸ Draw Xcode targets and dependencies tree

?? Bee Draw Xcode targets and dependencies tree.

Vyacheslav Khorkov 20 Dec 19, 2022
A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern

Locatable Context Locatable is a Swift micro framework that leverages Property Wrappers to implement the Service Locator pattern, through a custom att

Vincent Pradeilles 116 Jan 9, 2022
Helps you define secure storages for your properties using Swift property wrappers.

?? Secure Property Storage Helps you define secure storages for your properties using Swift property wrappers. ?? Features All keys are hashed using S

Alex RupƩrez 443 Jan 4, 2023
CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc

RNCryptor Cross-language AES Encryptor/Decryptor data format. The primary targets are Swift and Objective-C, but implementations are available in C, C

null 3.3k Dec 30, 2022
Swift Property Wrappers, but in Objective-C. And done horribly.

TOPropertyAccessor is an open source, Objective-C abstract class. Similar to Realm's Cocoa API, it uses the dynamic nature of the Objective-C runtime to access the properties of any of its subclasses, and routes calling them through overridable access points.

Tim Oliver 3 May 23, 2021
CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc

RNCryptor Cross-language AES Encryptor/Decryptor data format. The primary targets are Swift and Objective-C, but implementations are available in C, C

null 3.3k Jan 7, 2023
A collection of Swift Property Wrappers (formerly "Property Delegates")

?? ?? Burritos A collection of well tested Swift Property Wrappers. @AtomicWrite @Clamping @Copying @DefaultValue @DynamicUIColor @EnvironmentVariable

Guillermo Muntaner 1.3k Dec 26, 2022