Skip to content

Update tree-sitter to 0.21.0 #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{json,toml,yml,gyp}]
indent_style = space
indent_size = 2

[*.js]
indent_style = space
indent_size = 2

[*.rs]
indent_style = space
indent_size = 4

[*.{c,cc,h}]
indent_style = space
indent_size = 4

[*.{py,pyi}]
indent_style = space
indent_size = 4

[*.swift]
indent_style = space
indent_size = 4

[*.go]
indent_style = tab
indent_size = 8

[Makefile]
indent_style = tab
indent_size = 8
4 changes: 4 additions & 0 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ jobs:
id: verify-changed-files
with:
files: |
bindings/c/tree-sitter-scala.h
bindings/c/tree-sitter-scala.pc.in
grammar.js
src/grammar.json
src/node-types.json
src/parser.c
src/tree_sitter/alloc.h
src/tree_sitter/array.h
src/tree_sitter/parser.h

- name: Commit changes if necessary
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "tree-sitter-scala"
description = "scala grammar for the tree-sitter parsing library"
version = "0.20.0"
version = "0.21.0"
keywords = ["incremental", "parsing", "scala"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-scala"
edition = "2018"
edition = "2021"
license = "MIT"

build = "bindings/rust/build.rs"
Expand All @@ -20,7 +20,7 @@ include = [
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = "0.20.7"
tree-sitter = "0.21.0"

[build-dependencies]
cc = "1.0"
14 changes: 8 additions & 6 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
"targets": [
{
"target_name": "tree_sitter_scala_binding",
"dependencies": [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
"src",
],
"sources": [
"src/parser.c",
"bindings/node/binding.cc",
"src/scanner.c"
"src/parser.c",
"src/scanner.c",
],
"cflags_c": [
"-std=c99",
]
"-std=c11",
],
}
]
}
36 changes: 14 additions & 22 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
#include <napi.h>

using namespace v8;
typedef struct TSLanguage TSLanguage;

extern "C" TSLanguage * tree_sitter_scala();
extern "C" TSLanguage *tree_sitter_scala();

namespace {
// "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};

NAN_METHOD(New) {}

void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_scala());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("scala").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["name"] = Napi::String::New(env, "scala");
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_scala());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}

NODE_MODULE(tree_sitter_scala_binding, Init)

} // namespace
NODE_API_MODULE(tree_sitter_scala_binding, Init)
28 changes: 28 additions & 0 deletions bindings/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type BaseNode = {
type: string;
named: boolean;
};

type ChildNode = {
multiple: boolean;
required: boolean;
types: BaseNode[];
};

type NodeInfo =
| (BaseNode & {
subtypes: BaseNode[];
})
| (BaseNode & {
fields: { [name: string]: ChildNode };
children: ChildNode[];
});

type Language = {
name: string;
language: unknown;
nodeTypeInfo: NodeInfo[];
};

declare const language: Language;
export = language;
18 changes: 3 additions & 15 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
try {
module.exports = require("../../build/Release/tree_sitter_scala_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_scala_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
const root = require("path").join(__dirname, "..", "..");

module.exports = require("node-gyp-build")(root);

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
Expand Down
4 changes: 2 additions & 2 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_scala::language()).expect("Error loading scala grammar");
//! parser.set_language(&tree_sitter_scala::language()).expect("Error loading scala grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
Expand Down Expand Up @@ -46,7 +46,7 @@ mod tests {
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.set_language(&super::language())
.expect("Error loading scala language");
}
}
29 changes: 25 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,44 @@
"version": "0.20.0",
"description": "Scala grammar for tree-sitter",
"main": "bindings/node",
"types": "bindings/node",
"keywords": [
"parser",
"scala"
],
"files": [
"grammar.js",
"binding.gyp",
"prebuilds/**",
"bindings/node/*",
"queries/*",
"src/**"
],
"author": "Max Brunsfeld",
"license": "MIT",
"dependencies": {
"nan": "^2.14.1"
"node-addon-api": "^7.1.0",
"node-gyp-build": "^4.8.0"
},
"peerDependencies": {
"tree-sitter": "0.21.0"
},
"peerDependenciesMeta": {
"tree_sitter": {
"optional": true
}
},
"devDependencies": {
"tree-sitter-cli": "0.20.7",
"prettier": "3.0.0-alpha.6"
"tree-sitter-cli": "0.21.0",
"prettier": "3.0.0-alpha.6",
"prebuildify": "^6.0.0"
},
"scripts": {
"build": "tree-sitter generate && node-gyp build",
"test": "tree-sitter test && tree-sitter parse examples/*.scala --quiet --time",
"format": "prettier --write --ignore-unknown grammar.js"
"format": "prettier --write --ignore-unknown grammar.js",
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip"
},
"tree-sitter": [
{
Expand Down
34 changes: 17 additions & 17 deletions queries/scala/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
; CREDITS @stumash ([email protected])

(field_expression field: (identifier) @property)
(field_expression value: (identifier) @type
(#match? @type "^[A-Z]"))

(type_identifier) @type

(class_definition
name: (identifier) @type)

Expand All @@ -20,7 +26,7 @@

;; variables

(class_parameter
(class_parameter
name: (identifier) @parameter)

(self_type (identifier) @parameter)
Expand All @@ -33,8 +39,6 @@
(type_definition
name: (type_identifier) @type.definition)

(type_identifier) @type

;; val/var definitions/declarations

(val_definition
Expand All @@ -49,14 +53,6 @@
(var_declaration
name: (identifier) @variable)

; method definition

(function_declaration
name: (identifier) @method)

(function_definition
name: (identifier) @method)

; imports/exports

(import_declaration
Expand Down Expand Up @@ -110,11 +106,15 @@
(binding
name: (identifier) @parameter)

; expressions
; method definition

(field_expression field: (identifier) @property)
(field_expression value: (identifier) @type
(#match? @type "^[A-Z]"))
(function_declaration
name: (identifier) @method)

(function_definition
name: (identifier) @method)

; expressions

(infix_expression operator: (identifier) @operator)
(infix_expression operator: (operator_identifier) @operator)
Expand Down Expand Up @@ -235,8 +235,8 @@

"return" @keyword.return

(comment) @comment @spell
(block_comment) @comment @spell
(comment) @spell @comment
(block_comment) @spell @comment

;; `case` is a conditional keyword in case_block

Expand Down
2 changes: 1 addition & 1 deletion script/smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ run_tree_sitter () {
echo "Report written to $report_file"
fi

actual=$(echo "$out" | grep 'success percentage:' | rev | cut -d' ' -f1 | rev | sed 's/%//g' )
actual=$(echo "$out" | grep 'success percentage:' | rev | cut -d' ' -f5 | rev | sed 's/;//g' | sed 's/%//g' )
echo "$actual"
if (( $(echo "$actual >= $expected" |bc -l) )); then
# See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-creating-an-annotation-for-an-error
Expand Down
2 changes: 1 addition & 1 deletion test/highlight/scala3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object A:
// ^type

::(123)
//^function.call
//^operator
// ^number

object bla:
Expand Down