Skip to content

Commit

Permalink
feat!(deps): Update tree-sitter bindings to v0.23.0
Browse files Browse the repository at this point in the history
Update to tree-sitter v0.23.0.  The change contains breaking changes,
which are explained in the links below.

What the update does is: it removes the dependency on tree-sitter from
the bindings.  Now clients can chose their own tree-sitter version to
use the bindings with.

Most changes are form the auto-updated bindings.

This commit also fixes the Go build.

See
- alex-pinkus/tree-sitter-swift#435
- tree-sitter/tree-sitter#3069
  • Loading branch information
bugwelle committed Sep 9, 2024
1 parent 6c25109 commit 3c51620
Show file tree
Hide file tree
Showing 25 changed files with 316 additions and 204 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.rs]
indent_style = space
indent_size = 4
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
show-progress: false
- uses: actions/setup-node@v4
with:
node-version: 20
Expand All @@ -32,8 +34,12 @@ jobs:
# TODO: Abort if there are diffs; see #18
- name: Ensure generated files are up-to-date
run: |
# package.json may change due to custom scripts
git checkout HEAD -- package.json
git diff --diff-filter=M --color | head -n 100
git diff --diff-filter=M --quiet || (echo 'Generated grammar is not up-to-date: run `npm run build` and commit all changes')
git reset --hard HEAD
- run: npm run test:all
- run: npm run lint
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ log.html
target/

*.so

# via Python bindings
tree_sitter_cds.egg-info/
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Upcoming

### Changed

- [__BREAKING__] Generated bindings via tree-sitter-cli v0.23.0
With tree-sitter v0.23.0, Rust bindings have changed.


## Version 1.0.0 - 2024-06-25

Expand Down
23 changes: 16 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tree-sitter-cds"
description = "CAP CDS grammar for the tree-sitter parsing library"
version = "1.0.0"
version = "2.0.0"
authors = [ "Andre Meyering <[email protected]>" ]
license = "Apache-2.0"
keywords = ["incremental", "parsing", "cds"]
Expand All @@ -23,7 +23,10 @@ include = [
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = "~0.22.5"
tree-sitter-language = "0.1.0"

[dev-dependencies]
tree-sitter = "0.23.0"

[build-dependencies]
cc = "1.0"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := 1.0.0
VERSION := 2.0.0

LANGUAGE_NAME := tree-sitter-cds

Expand Down
4 changes: 2 additions & 2 deletions bindings/go/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tree_sitter_cds_test
import (
"testing"

tree_sitter "github.com/smacker/go-tree-sitter"
"github.com/cap-js-community/tree-sitter-cds"
tree_sitter "github.com/tree-sitter/go-tree-sitter"
tree_sitter_cds "github.com/cap-js-community/tree-sitter-cds/bindings/go"
)

func TestCanLoadGrammar(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions bindings/go/go.mod

This file was deleted.

9 changes: 9 additions & 0 deletions bindings/node/binding_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="node" />

const assert = require("node:assert");
const { test } = require("node:test");

test("can load grammar", () => {
const parser = new (require("tree-sitter"))();
assert.doesNotThrow(() => parser.setLanguage(require(".")));
});
11 changes: 11 additions & 0 deletions bindings/python/tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from unittest import TestCase

import tree_sitter, tree_sitter_cds


class TestLanguage(TestCase):
def test_can_load_grammar(self):
try:
tree_sitter.Language(tree_sitter_cds.language())
except Exception:
self.fail("Error loading CDS grammar")
4 changes: 2 additions & 2 deletions bindings/python/tree_sitter_cds/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ typedef struct TSLanguage TSLanguage;

TSLanguage *tree_sitter_cds(void);

static PyObject* _binding_language(PyObject *self, PyObject *args) {
return PyLong_FromVoidPtr(tree_sitter_cds());
static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
return PyCapsule_New(tree_sitter_cds(), "tree_sitter.Language", NULL);
}

static PyMethodDef methods[] = {
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To use this crate, add it to the `[dependencies]` section of your
``` toml
[dependencies]
tree-sitter = "~0.20"
tree-sitter-cds = "~1.0.0"
tree-sitter-cds = "~2.0.0"
```

The below example demonstrates a simple program that parses a CDS
Expand Down
59 changes: 37 additions & 22 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,67 @@
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = "entity MyEntity { key id : String; };";
//! let code = r#"
//! "#;
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(&tree_sitter_cds::language()).expect("Error loading cds grammar");
//! let language = tree_sitter_cds::LANGUAGE;
//! parser
//! .set_language(&language.into())
//! .expect("Error loading CDS parser");
//! let tree = parser.parse(code, None).unwrap();
//! let root = tree.root_node();
//! assert_eq!(root.kind(), "cds");
//! let entity = root.child(0).unwrap();
//! assert_eq!(entity.kind(), "entity_definition");
//! assert_eq!(&code[entity.child_by_field_name("name").unwrap().byte_range()], "MyEntity");
//! assert!(!tree.root_node().has_error());
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/

use tree_sitter::Language;
use tree_sitter_language::LanguageFn;

extern "C" {
fn tree_sitter_cds() -> Language;
fn tree_sitter_cds() -> *const ();
}

/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_cds() }
}
/// The tree-sitter [`LanguageFn`] for this grammar.
pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_cds) };

/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");

pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");

#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&super::language())
.expect("Error loading cds language");
.set_language(&super::LANGUAGE.into())
.expect("Error loading CDS parser");
}

#[test]
fn test_can_parse_basic_file() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&super::LANGUAGE.into())
.expect("Error loading CDS parser");


let tree = parser
.parse("entity E { key id : String; };", None)
.expect("Unable to parse simple CDS file!");


assert_eq!(
"(cds (entity_definition name: (name (identifier)) (element_definitions (element_definition name: (name) type: (simple_path (identifier))))))",
tree.root_node().to_sexp(),
);
}
}
12 changes: 12 additions & 0 deletions bindings/swift/TreeSitterCdsTests/TreeSitterCdsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import XCTest
import SwiftTreeSitter
import TreeSitterCds

final class TreeSitterCdsTests: XCTestCase {
func testCanLoadGrammar() throws {
let parser = Parser()
let language = Language(language: tree_sitter_cds())
XCTAssertNoThrow(try parser.setLanguage(language),
"Error loading CDS grammar")
}
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/cap-js-community/tree-sitter-cds

go 1.23

require github.com/tree-sitter/go-tree-sitter v0.23.1

require github.com/mattn/go-pointer v0.0.1 // indirect
Loading

0 comments on commit 3c51620

Please sign in to comment.