Merge pull request #659 from hugo-vrijswijk/update-scala-treesitter

update scala treesitter
pull/731/head
Wilfred Hughes 2024-07-23 07:08:49 +07:00 committed by GitHub
commit 954979b19f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
50 changed files with 516829 additions and 490638 deletions

@ -218,7 +218,7 @@ sample_files/ruby_1.rb sample_files/ruby_2.rb
d4d591902030355656f5c18c78f965a6 -
sample_files/scala_1.scala sample_files/scala_2.scala
f19e0f0ab3cabb1b5c863b78a5a6b2d2 -
446702806bab430dc50aad493d53f64f -
sample_files/scheme_1.scm sample_files/scheme_2.scm
09f8683219a8491c22d9a2fa1ce98c16 -

@ -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

@ -6,14 +6,14 @@ on:
- master
jobs:
changedfiles:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
outputs:
all: ${{ steps.changes.outputs.all}}
c: ${{ steps.changes.outputs.c }}
gen: ${{ steps.changes.outputs.gen }}
steps:
- name: checkout tree-sitter-scala
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 10
@ -30,42 +30,47 @@ jobs:
strategy:
fail-fast: true
matrix:
os: [ubuntu-20.04, macos-latest, windows-2019]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: checkout tree-sitter-scala
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 10
- name: checkout scala/scala
if: ${{ runner.os == 'Linux' }}
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: scala/scala
ref: v2.13.10
ref: v2.13.14
path: scala_scala
- name: checkout lampepfl/dotty
- name: checkout scala/scala3
if: ${{ runner.os == 'Linux' }}
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: lampepfl/dotty
ref: 3.3.0
repository: scala/scala3
ref: 3.5.0-RC2
path: dotty
- name: checkout lichess-org/lila
if: ${{ runner.os == 'Linux' }}
uses: actions/checkout@v4
with:
repository: lichess-org/lila
ref: 83e61b9ef617164fe1d3a5112fcc611d0e5a7ea9
path: lila
- name: checkout nvim-treesitter/nvim-treesitter
if: ${{ runner.os == 'Linux' }}
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: nvim-treesitter/nvim-treesitter
path: nvim_treesitter
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Test C stack code
run: gcc test/test-stack.c -o a.out && ./a.out
node-version: 20
- name: Generate parser from scratch and test it
if: ${{ runner.os == 'Linux' || needs.changedfiles.outputs.c }}
@ -93,6 +98,7 @@ jobs:
env:
SCALA_SCALA_DIR: scala_scala
DOTTY_DIR: dotty
LILA_DIR: lila
run: script/smoke_test.sh
- name: copy nvim-treesitter queries
@ -102,7 +108,7 @@ jobs:
- name: Check if queries are out of sync with nvim-treesitter
if: ${{ runner.os == 'Linux' }}
uses: tj-actions/verify-changed-files@v13
uses: tj-actions/verify-changed-files@v19
id: verify-changed-files
with:
files: |

@ -17,7 +17,7 @@ jobs:
name: Parser fuzzing
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: vigoux/tree-sitter-fuzz-action@v1
with:
language: scala

@ -6,13 +6,13 @@ on:
jobs:
check-and-sync:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
outputs:
all: ${{ steps.changes.outputs.all}}
c: ${{ steps.changes.outputs.c }}
steps:
- name: checkout tree-sitter-scala
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 10
@ -27,14 +27,18 @@ jobs:
npm run format
- name: Check for changes
uses: tj-actions/verify-changed-files@v13
uses: tj-actions/verify-changed-files@v19
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

@ -1,11 +1,11 @@
[package]
name = "tree-sitter-scala"
description = "scala grammar for the tree-sitter parsing library"
version = "0.20.0"
version = "0.22.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"
@ -20,7 +20,7 @@ include = [
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = "0.20.7"
tree-sitter = "0.22.6"
[build-dependencies]
cc = "1.0"

@ -0,0 +1,112 @@
VERSION := 0.0.1
LANGUAGE_NAME := tree-sitter-scala
# repository
SRC_DIR := src
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null)
ifeq ($(PARSER_URL),)
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),)
PARSER_URL := $(subst :,/,$(PARSER_URL))
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
endif
endif
TS ?= tree-sitter
# ABI versioning
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
SONAME_MINOR := $(word 2,$(subst ., ,$(VERSION)))
# install directory layout
PREFIX ?= /usr/local
INCLUDEDIR ?= $(PREFIX)/include
LIBDIR ?= $(PREFIX)/lib
PCLIBDIR ?= $(LIBDIR)/pkgconfig
# source/object files
PARSER := $(SRC_DIR)/parser.c
EXTRAS := $(filter-out $(PARSER),$(wildcard $(SRC_DIR)/*.c))
OBJS := $(patsubst %.c,%.o,$(PARSER) $(EXTRAS))
# flags
ARFLAGS ?= rcs
override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC
# OS-specific bits
ifeq ($(OS),Windows_NT)
$(error "Windows is not supported")
else ifeq ($(shell uname),Darwin)
SOEXT = dylib
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
ifneq ($(ADDITIONAL_LIBS),)
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
endif
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
else
SOEXT = so
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
LINKSHARED := $(LINKSHARED)-shared -Wl,
ifneq ($(ADDITIONAL_LIBS),)
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
endif
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
endif
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
endif
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
lib$(LANGUAGE_NAME).a: $(OBJS)
$(AR) $(ARFLAGS) $@ $^
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
ifneq ($(STRIP),)
$(STRIP) $@
endif
$(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in
sed -e 's|@URL@|$(PARSER_URL)|' \
-e 's|@VERSION@|$(VERSION)|' \
-e 's|@LIBDIR@|$(LIBDIR)|' \
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \
-e 's|@REQUIRES@|$(REQUIRES)|' \
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|' \
-e 's|=$(PREFIX)|=$${prefix}|' \
-e 's|@PREFIX@|$(PREFIX)|' $< > $@
$(PARSER): $(SRC_DIR)/grammar.json
$(TS) generate --no-bindings $^
install: all
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
install -m755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
uninstall:
$(RM) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a \
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER) \
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) \
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) \
'$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h \
'$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
clean:
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
test:
$(TS) test
.PHONY: all install uninstall clean test

@ -1,6 +1,10 @@
# tree-sitter-scala
[![Test the grammar](https://github.com/tree-sitter/tree-sitter-scala/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-scala/actions/workflows/ci.yml)
[![CI][ci]](https://github.com/tree-sitter/tree-sitter-scala/actions/workflows/ci.yml)
[![discord][discord]](https://discord.gg/w7nTvsVJhm)
[![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
[![crates][crates]](https://crates.io/crates/tree-sitter-scala)
[![npm][npm]](https://www.npmjs.com/package/tree-sitter-scala)
Scala grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter)
covering both Scala 2 and 3.
@ -8,6 +12,7 @@ covering both Scala 2 and 3.
## References
_Scala 2_
- [The Scala 2 Language Specification](https://www.scala-lang.org/files/archive/spec/2.13/)
- [Scala 2 Syntax Summary](https://www.scala-lang.org/files/archive/spec/2.13/13-syntax-summary.html)
@ -19,3 +24,9 @@ _Scala 3_
Please refer to the [CONTRIBUTING.md](./CONTRIBUTING.md) for instructions on
getting set up.
[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/tree-sitter-scala/ci.yml?logo=github&label=CI
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
[matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
[npm]: https://img.shields.io/npm/v/tree-sitter-scala?logo=npm
[crates]: https://img.shields.io/crates/v/tree-sitter-scala?logo=rust

@ -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",
],
}
]
}

@ -0,0 +1,16 @@
#ifndef TREE_SITTER_SCALA_H_
#define TREE_SITTER_SCALA_H_
typedef struct TSLanguage TSLanguage;
#ifdef __cplusplus
extern "C" {
#endif
const TSLanguage *tree_sitter_scala(void);
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_SCALA_H_

@ -0,0 +1,11 @@
prefix=@PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@
Name: tree-sitter-scala
Description: Scala grammar for tree-sitter
URL: @URL@
Version: @VERSION@
Requires: @REQUIRES@
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-scala
Cflags: -I${includedir}

@ -0,0 +1,13 @@
package tree_sitter_scala
// #cgo CFLAGS: -std=c11 -fPIC
// #include "../../src/parser.c"
// // NOTE: if your language has an external scanner, add it here.
import "C"
import "unsafe"
// Get the tree-sitter Language for this grammar.
func Language() unsafe.Pointer {
return unsafe.Pointer(C.tree_sitter_scala())
}

@ -0,0 +1,15 @@
package tree_sitter_scala_test
import (
"testing"
tree_sitter "github.com/smacker/go-tree-sitter"
"github.com/tree-sitter/tree-sitter-scala"
)
func TestCanLoadGrammar(t *testing.T) {
language := tree_sitter.NewLanguage(tree_sitter_scala.Language())
if language == nil {
t.Errorf("Error loading Scala grammar")
}
}

@ -0,0 +1,5 @@
module github.com/tree-sitter/tree-sitter-scala
go 1.22
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8

@ -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)

@ -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;

@ -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");

@ -0,0 +1,5 @@
"Scala grammar for tree-sitter"
from ._binding import language
__all__ = ["language"]

@ -0,0 +1,27 @@
#include <Python.h>
typedef struct TSLanguage TSLanguage;
TSLanguage *tree_sitter_scala(void);
static PyObject* _binding_language(PyObject *self, PyObject *args) {
return PyLong_FromVoidPtr(tree_sitter_scala());
}
static PyMethodDef methods[] = {
{"language", _binding_language, METH_NOARGS,
"Get the tree-sitter language for this grammar."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef module = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_binding",
.m_doc = NULL,
.m_size = -1,
.m_methods = methods
};
PyMODINIT_FUNC PyInit__binding(void) {
return PyModule_Create(&module);
}

@ -6,6 +6,9 @@ fn main() {
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
#[cfg(target_env = "msvc")]
c_config.flag("-utf-8");
let parser_path = src_dir.join("parser.c");
let scanner_path = src_dir.join("scanner.c");
c_config.file(&parser_path);

@ -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();
//! ```
//!
@ -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");
}
}

@ -1,206 +0,0 @@
=========================
Alternative patterns
=========================
val x = y match {
case 1 | a => b
case "c" | "d" | "e" => f
}
---
(compilation_unit
(val_definition
(identifier)
(match_expression (identifier) (case_block
(case_clause
(alternative_pattern (integer_literal) (identifier))
(identifier))
(case_clause
(alternative_pattern
(alternative_pattern (string) (string))
(string))
(identifier))))))
=========================
Typed patterns
=========================
val x = y match {
case 1 : Int => 2
case a : B with C => d
case _: B | _: C => 3
case Object.Constant => 3
}
---
(compilation_unit
(val_definition
(identifier)
(match_expression (identifier) (case_block
(case_clause
(typed_pattern (integer_literal) (type_identifier)) (integer_literal))
(case_clause
(typed_pattern (identifier) (compound_type (type_identifier) (type_identifier)))
(identifier))
(case_clause
(alternative_pattern
(typed_pattern (wildcard) (type_identifier))
(typed_pattern (wildcard) (type_identifier)))
(integer_literal))
(case_clause
(stable_identifier (identifier) (identifier))
(integer_literal))))))
============================
Tuple patterns
============================
val (a, b) = if (c) (d, e) else (f, g)
val x = y match {
case (A, B) => X
}
---
(compilation_unit
(val_definition
(tuple_pattern (identifier) (identifier))
(if_expression
(parenthesized_expression (identifier))
(tuple_expression (identifier) (identifier))
(tuple_expression (identifier) (identifier))))
(val_definition (identifier)
(match_expression (identifier)
(case_block
(case_clause
(tuple_pattern (identifier) (identifier)) (identifier))))))
============================
Case class patterns
============================
def showNotification(notification: Notification): String = {
notification match {
case Email(email, title, _) =>
s"You got an email from $email with title: $title"
case SMS(number, message) =>
s"You got an SMS from $number! Message: $message"
case VoiceRecording(name, link) =>
s"you received a Voice Recording from $name! Click the link to hear it: $link"
}
}
---
(compilation_unit
(function_definition
(identifier)
(parameters (parameter (identifier) (type_identifier)))
(type_identifier)
(block
(match_expression (identifier) (case_block
(case_clause
(case_class_pattern (type_identifier) (identifier) (identifier) (wildcard))
(interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier)))))
(case_clause
(case_class_pattern (type_identifier) (identifier) (identifier))
(interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier)))))
(case_clause
(case_class_pattern (type_identifier) (identifier) (identifier))
(interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier))))))))))
============================
Infix patterns
============================
def first(x: Seq[Int]) = x match {
case e :+ _ => Some(e)
case _ => None
}
---
(compilation_unit
(function_definition (identifier)
(parameters (parameter (identifier) (generic_type (type_identifier) (type_arguments (type_identifier)))))
(match_expression (identifier)
(case_block
(case_clause (infix_pattern (identifier) (operator_identifier) (wildcard))
(call_expression (identifier) (arguments (identifier))))
(case_clause (wildcard)
(identifier))))))
============================
Capture patterns
============================
val x = y match {
case a @ B(1) => a
case b @ C(d @ (e @ X, _: Y)) => e
case req @ (POST | GET) -> Root / "test" => 5
case Array(a: Type, _@_*) => y
}
---
(compilation_unit
(val_definition
(identifier)
(match_expression
(identifier)
(case_block
(case_clause
(capture_pattern (identifier) (case_class_pattern (type_identifier) (integer_literal)))
(identifier))
(case_clause
(capture_pattern (identifier)
(case_class_pattern (type_identifier)
(capture_pattern (identifier)
(tuple_pattern
(capture_pattern (identifier) (identifier))
(typed_pattern (wildcard) (type_identifier))))))
(identifier))
(case_clause
(infix_pattern
(infix_pattern
(capture_pattern (identifier)
(tuple_pattern (alternative_pattern (identifier) (identifier))))
(operator_identifier) (identifier)) (operator_identifier) (string))
(integer_literal))
(case_clause
(case_class_pattern
(type_identifier)
(typed_pattern
(identifier)
(type_identifier))
(repeat_pattern
(capture_pattern
(wildcard)
(wildcard))))
(identifier))))))
============================
Quoted patterns (Scala 3 syntax)
============================
def foo =
x match
case '{ $boolExpr } => Some(true)
case _ => None
---
(compilation_unit
(function_definition (identifier)
(indented_block
(match_expression (identifier)
(indented_cases
(case_clause
(quote_expression (identifier))
(call_expression (identifier) (arguments (boolean_literal))))
(case_clause (wildcard)
(identifier)))))))

File diff suppressed because it is too large Load Diff

@ -42,6 +42,7 @@ module.exports = grammar({
"catch",
"finally",
"extends",
"derives",
"with",
],
@ -70,6 +71,7 @@ module.exports = grammar({
[$.if_expression],
[$.match_expression],
[$._function_constructor, $._type_identifier],
[$._given_constructor, $._type_identifier],
[$.instance_expression],
// In case of: 'extension' _indent '{' 'case' operator_identifier 'if' operator_identifier • '=>' …
// we treat `operator_identifier` as `simple_expression`
@ -90,6 +92,11 @@ module.exports = grammar({
[$.class_parameters],
// 'for' operator_identifier ':' _annotated_type • ':' …
[$._type, $.compound_type],
[$.lambda_expression, $.modifiers],
// 'if' parenthesized_expression • '{' …
[$._if_condition, $._simple_expression],
// _postfix_expression_choice ':' '(' wildcard • ':' …
[$.binding, $._simple_type],
],
word: $ => $._alpha_identifier,
@ -97,7 +104,10 @@ module.exports = grammar({
rules: {
// TopStats ::= TopStat {semi TopStat}
compilation_unit: $ =>
optional(trailingSep1($._semicolon, $._top_level_definition)),
seq(
optional($._shebang),
optional(trailingSep1($._semicolon, $._top_level_definition)),
),
_top_level_definition: $ =>
choice(
@ -295,7 +305,7 @@ module.exports = grammar({
$._class_constructor,
field("extend", optional($.extends_clause)),
field("derive", optional($.derives_clause)),
optional($._definition_body),
field("body", optional($._definition_body)),
),
_definition_body: $ =>
@ -323,9 +333,7 @@ module.exports = grammar({
repeat($.annotation),
optional($.modifiers),
"trait",
$._class_constructor,
field("extend", optional($.extends_clause)),
field("body", optional($._definition_body)),
$._class_definition,
),
),
@ -343,6 +351,7 @@ module.exports = grammar({
$.covariant_type_parameter,
$.contravariant_type_parameter,
$._type_parameter, // invariant type parameter
$.type_lambda,
),
),
@ -548,6 +557,7 @@ module.exports = grammar({
"parameters",
repeat(seq(optional($._automatic_semicolon), $.parameters)),
),
optional($._automatic_semicolon),
),
),
@ -565,7 +575,11 @@ module.exports = grammar({
field("parameters", repeat($.parameters)),
field(
"body",
choice($._extension_template_body, $.function_definition),
choice(
$._extension_template_body,
$.function_definition,
$.function_declaration,
),
),
),
),
@ -580,18 +594,31 @@ module.exports = grammar({
repeat($.annotation),
optional($.modifiers),
"given",
optional(seq($._function_constructor, ":")),
optional($._given_constructor),
choice(
field("return_type", $._structural_instance),
seq(
field("return_type", $._annotated_type),
"=",
field("body", $.expression),
optional(seq("=", field("body", $._indentable_expression))),
),
),
),
),
_given_constructor: $ =>
prec.right(
seq(
field("name", optional($._identifier)),
field("type_parameters", optional($.type_parameters)),
field(
"parameters",
repeat(seq(optional($._automatic_semicolon), $.parameters)),
),
optional($._automatic_semicolon),
":",
),
),
/**
* StructuralInstance ::= ConstrApp {'with' ConstrApp} ['with' WithTemplateBody]
*/
@ -638,18 +665,20 @@ module.exports = grammar({
modifiers: $ =>
prec.left(
repeat1(
choice(
"abstract",
"final",
"sealed",
"implicit",
"lazy",
"override",
$.access_modifier,
$.inline_modifier,
$.infix_modifier,
$.open_modifier,
$.transparent_modifier,
prec.left(
choice(
"abstract",
"final",
"sealed",
"implicit",
"lazy",
"override",
$.access_modifier,
$.inline_modifier,
$.infix_modifier,
$.open_modifier,
$.transparent_modifier,
),
),
),
),
@ -679,7 +708,14 @@ module.exports = grammar({
),
derives_clause: $ =>
prec.left(seq("derives", commaSep1(field("type", $._type_identifier)))),
prec.left(
seq(
"derives",
commaSep1(
field("type", choice($._type_identifier, $.stable_type_identifier)),
),
),
),
class_parameters: $ =>
prec(
@ -752,14 +788,14 @@ module.exports = grammar({
seq(
sep1(
$._semicolon,
choice($.expression, $._definition, $._end_marker),
choice($.expression, $._definition, $._end_marker, ";"),
),
optional($._semicolon),
),
),
_indentable_expression: $ =>
choice($.indented_block, $.indented_cases, $.expression),
prec.right(choice($.indented_block, $.indented_cases, $.expression)),
block: $ => seq("{", optional($._block), "}"),
@ -787,6 +823,7 @@ module.exports = grammar({
$._annotated_type,
$.literal_type,
$._structural_type,
$.type_lambda,
),
_annotated_type: $ => prec.right(choice($.annotated_type, $._simple_type)),
@ -928,6 +965,15 @@ module.exports = grammar({
_type_identifier: $ => alias($._identifier, $.type_identifier),
type_lambda: $ =>
seq(
"[",
trailingCommaSep1($._type_parameter),
"]",
"=>>",
field("return_type", $._type),
),
// ---------------------------------------------------------------
// Patterns
@ -942,6 +988,7 @@ module.exports = grammar({
$.infix_pattern,
$.alternative_pattern,
$.typed_pattern,
$.given_pattern,
$.quote_expression,
$.literal,
$.wildcard,
@ -983,6 +1030,8 @@ module.exports = grammar({
seq(field("pattern", $._pattern), ":", field("type", $._type)),
),
given_pattern: $ => seq("given", field("type", $._type)),
// TODO: Flatten this.
alternative_pattern: $ => prec.left(-1, seq($._pattern, "|", $._pattern)),
@ -1007,6 +1056,7 @@ module.exports = grammar({
$.while_expression,
$.do_while_expression,
$.for_expression,
$.macro_body,
$._simple_expression,
),
@ -1051,27 +1101,48 @@ module.exports = grammar({
lambda_expression: $ =>
prec.right(
seq(
field("parameters", choice($.bindings, $._identifier, $.wildcard)),
"=>",
field(
"parameters",
choice(
$.bindings,
seq(optional("implicit"), $._identifier),
$.wildcard,
),
),
choice("=>", "?=>"),
$._indentable_expression,
),
),
/*
* ::= [inline] if ( Expr ) {nl} Expr [[semi] else Expr]
* | [inline] if Expr then Expr [[semi] else Expr]
*/
if_expression: $ =>
prec.right(
PREC.control,
seq(
optional($.inline_modifier),
"if",
field(
"condition",
choice(
$.parenthesized_expression,
seq($._indentable_expression, "then"),
),
seq(
optional($.inline_modifier),
"if",
field("condition", $._if_condition),
field("consequence", $._indentable_expression),
optional(
seq(
optional(";"),
"else",
field("alternative", $._indentable_expression),
),
field("consequence", $._indentable_expression),
optional(seq("else", field("alternative", $._indentable_expression))),
),
),
// NOTE(susliko): _if_condition and its magic dynamic precedence were introduced as a fix to
// https://github.com/tree-sitter/tree-sitter-scala/issues/263 and
// https://github.com/tree-sitter/tree-sitter-scala/issues/342
// Neither do I understand why this works, nor have I found a better solution
_if_condition: $ =>
prec.dynamic(
4,
choice(
$.parenthesized_expression,
seq($._indentable_expression, "then"),
),
),
@ -1079,14 +1150,11 @@ module.exports = grammar({
* MatchClause ::= 'match' <<< CaseClauses >>>
*/
match_expression: $ =>
prec.left(
PREC.postfix,
seq(
optional($.inline_modifier),
field("value", $.expression),
"match",
field("body", choice($.case_block, $.indented_cases)),
),
seq(
optional($.inline_modifier),
field("value", $.expression),
"match",
field("body", choice($.case_block, $.indented_cases)),
),
try_expression: $ =>
@ -1113,11 +1181,14 @@ module.exports = grammar({
finally_clause: $ => prec.right(seq("finally", $._indentable_expression)),
/*
* Binding ::= (id | _) [: Type]
*/
binding: $ =>
prec.dynamic(
PREC.binding,
seq(
field("name", $._identifier),
choice(field("name", $._identifier), $.wildcard),
optional(seq(":", field("type", $._param_type))),
),
),
@ -1287,6 +1358,15 @@ module.exports = grammar({
),
),
macro_body: $ =>
prec.left(
PREC.macro,
seq(
"macro",
choice($.infix_expression, $.prefix_expression, $._simple_expression),
),
),
/**
* PrefixExpr ::= [PrefixOperator] SimpleExpr
*/
@ -1369,6 +1449,26 @@ module.exports = grammar({
_alpha_identifier: $ =>
/[\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\$\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F\$][\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\$\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F0-9\$_\p{Ll}]*(_[\-!#%&*+\/\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]+)?/,
/**
* Despite what the lexical syntax suggests, the alphaid rule doesn't apply
* to identifiers that aren't in blocks in interpolated strings (e.g. $foo).
* A more accurate description is given in
* https://www.scala-lang.org/files/archive/spec/2.13/01-lexical-syntax.html
* where it states (regarding dollar sign escapes in interpolated strings) that
* """
* The simpler form consists of a $-sign followed by an identifier starting
* with a letter and followed only by letters, digits, and underscore characters
* """
* where "letters" does not include the $ character.
*
* This rule is similar to the _alpha_identifier rule, with the differences
* being that the $ character is excluded, along with the _(operator_chars)
* suffix and can be approximated as
* /[A-Za-z_][A-Z_a-z0-9]/;
*/
_interpolation_identifier: $ =>
/[\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F][\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F0-9_\p{Ll}]*/,
_backquoted_id: $ => /`[^\n`]+`/,
_identifier: $ => choice($.identifier, $.operator_identifier),
@ -1384,8 +1484,11 @@ module.exports = grammar({
operator_identifier: $ =>
token(
choice(
// single opchar
/[\-!#%&*+\/\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]/,
// opchar minus colon, equal, at
// Technically speaking, Sm (Math symbols https://www.compart.com/en/unicode/category/Sm)
// should be allowed as a single-characeter opchar, however, it includes `=`,
// so we should to avoid that to prevent bad parsing of `=` as infix term or type.
/[\-!#%&*+\/\\<>?\u005e\u007c~\u00ac\u00b1\u00d7\u00f7\u2190-\u2194\p{So}]/,
seq(
// opchar minus slash
/[\-!#%&*+\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]/,
@ -1452,12 +1555,7 @@ module.exports = grammar({
choice(
seq(
"\\",
choice(
/[^xu]/,
/u[0-9a-fA-F]{4}/,
/u{[0-9a-fA-F]+}/,
/x[0-9a-fA-F]{2}/,
),
choice(/[^xu]/, /uu?[0-9a-fA-F]{4}/, /x[0-9a-fA-F]{2}/),
),
/[^\\'\n]/,
),
@ -1473,18 +1571,34 @@ module.exports = grammar({
_interpolated_multiline_string_start: $ => '"""',
interpolation: $ => seq("$", choice($.identifier, $.block)),
_dollar_escape: $ => seq("$", choice("$", '"')),
_aliased_interpolation_identifier: $ =>
alias($._interpolation_identifier, $.identifier),
interpolation: $ =>
seq("$", choice($._aliased_interpolation_identifier, $.block)),
interpolated_string: $ =>
choice(
seq(
$._interpolated_string_start,
repeat(seq($._interpolated_string_middle, $.interpolation)),
repeat(
seq(
$._interpolated_string_middle,
choice($._dollar_escape, $.interpolation),
),
),
$._interpolated_string_end,
),
seq(
$._interpolated_multiline_string_start,
repeat(seq($._interpolated_multiline_string_middle, $.interpolation)),
repeat(
seq(
$._interpolated_multiline_string_middle,
choice($._dollar_escape, $.interpolation),
),
),
$._interpolated_multiline_string_end,
),
),
@ -1601,6 +1715,8 @@ module.exports = grammar({
repeat1($.guard),
),
_shebang: $ => alias(token(seq("#!", /.*/)), $.comment),
comment: $ => seq(token("//"), choice($.using_directive, $._comment_text)),
_comment_text: $ => token(prec(PREC.comment, /.*/)),

@ -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.22.6",
"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": [
{
@ -32,4 +53,4 @@
"locals": "queries/scala/locals.scm"
}
]
}
}

@ -0,0 +1,29 @@
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "tree-sitter-scala"
description = "Scala grammar for tree-sitter"
version = "0.0.1"
keywords = ["incremental", "parsing", "tree-sitter", "scala"]
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Topic :: Software Development :: Compilers",
"Topic :: Text Processing :: Linguistic",
"Typing :: Typed"
]
requires-python = ">=3.8"
license.text = "MIT"
readme = "README.md"
[project.urls]
Homepage = "https://github.com/tree-sitter/tree-sitter-scala"
[project.optional-dependencies]
core = ["tree-sitter~=0.21"]
[tool.cibuildwheel]
build = "cp38-*"
build-frontend = "build"

@ -1,5 +1,11 @@
; CREDITS @stumash (stuart.mashaal@gmail.com)
(field_expression field: (identifier) @property)
(field_expression value: (identifier) @type
(#match? @type "^[A-Z]"))
(type_identifier) @type
(class_definition
name: (identifier) @type)
@ -20,7 +26,7 @@
;; variables
(class_parameter
(class_parameter
name: (identifier) @parameter)
(self_type (identifier) @parameter)
@ -33,8 +39,6 @@
(type_definition
name: (type_identifier) @type.definition)
(type_identifier) @type
;; val/var definitions/declarations
(val_definition
@ -49,33 +53,25 @@
(var_declaration
name: (identifier) @variable)
; method definition
(function_declaration
name: (identifier) @method)
(function_definition
name: (identifier) @method)
; imports/exports
(import_declaration
path: (identifier) @namespace)
((stable_identifier (identifier) @namespace))
;; ((import_declaration
;; path: (identifier) @type) (#lua-match? @type "^[A-Z]"))
;; ((stable_identifier (identifier) @type) (#lua-match? @type "^[A-Z]"))
((import_declaration
path: (identifier) @type) (#match? @type "^[A-Z]"))
((stable_identifier (identifier) @type) (#match? @type "^[A-Z]"))
(export_declaration
path: (identifier) @namespace)
((stable_identifier (identifier) @namespace))
((export_declaration
path: (identifier) @type) (#lua-match? @type "^[A-Z]"))
;; ((stable_identifier (identifier) @type) (#lua-match? @type "^[A-Z]"))
path: (identifier) @type) (#match? @type "^[A-Z]"))
((stable_identifier (identifier) @type) (#match? @type "^[A-Z]"))
;; ((namespace_selectors (identifier) @type) (#lua-match? @type "^[A-Z]"))
((namespace_selectors (identifier) @type) (#match? @type "^[A-Z]"))
; method invocation
@ -89,9 +85,9 @@
function: (field_expression
field: (identifier) @method.call))
;; ((call_expression
;; function: (identifier) @constructor)
;; (#lua-match? @constructor "^[A-Z]"))
((call_expression
function: (identifier) @constructor)
(#match? @constructor "^[A-Z]"))
(generic_function
function: (identifier) @function.call)
@ -110,11 +106,15 @@
(binding
name: (identifier) @parameter)
; expressions
; method definition
(field_expression field: (identifier) @property)
;; (field_expression value: (identifier) @type
;; (#lua-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)
@ -235,24 +235,26 @@
"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
(case_block
(case_clause ("case") @conditional))
(indented_cases
(case_clause ("case") @conditional))
(operator_identifier) @operator
;; ((identifier) @type (#lua-match? @type "^[A-Z]"))
;; ((identifier) @variable.builtin
;; (#lua-match? @variable.builtin "^this$"))
((identifier) @type (#match? @type "^[A-Z]"))
((identifier) @variable.builtin
(#match? @variable.builtin "^this$"))
;; (
;; (identifier) @function.builtin
;; (#lua-match? @function.builtin "^super$")
;; )
(
(identifier) @function.builtin
(#match? @function.builtin "^super$")
)
;; Scala CLI using directives
(using_directive_key) @parameter

@ -27,3 +27,4 @@
name: (identifier) @local.definition)
(identifier) @local.reference

@ -3,9 +3,10 @@
# This is an integration test to generally check the quality of parsing.
SCALA_SCALA_LIBRARY_EXPECTED=100
SCALA_SCALA_COMPILER_EXPECTED=95
SCALA_SCALA_COMPILER_EXPECTED=96
DOTTY_COMPILER_EXPECTED=83
SYNTAX_COMPLEXITY_CEILING=1300
LILA_MODULES_EXPECTED=84
SYNTAX_COMPLEXITY_CEILING=1400
if [ ! -d "$SCALA_SCALA_DIR" ]; then
echo "\$SCALA_SCALA_DIR must be set"
@ -35,7 +36,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
@ -77,6 +78,7 @@ check_complexity () {
run_tree_sitter "$SCALA_SCALA_DIR/src/library/" $SCALA_SCALA_LIBRARY_EXPECTED scala2-library
run_tree_sitter "$SCALA_SCALA_DIR/src/compiler/" $SCALA_SCALA_COMPILER_EXPECTED scala2-compiler
run_tree_sitter "$DOTTY_DIR/compiler/" $DOTTY_COMPILER_EXPECTED dotty-compiler
run_tree_sitter "$LILA_DIR/modules/" $LILA_MODULES_EXPECTED lila-modules
check_complexity $SYNTAX_COMPLEXITY_CEILING

@ -0,0 +1,60 @@
from os.path import isdir, join
from platform import system
from setuptools import Extension, find_packages, setup
from setuptools.command.build import build
from wheel.bdist_wheel import bdist_wheel
class Build(build):
def run(self):
if isdir("queries"):
dest = join(self.build_lib, "tree_sitter_scala", "queries")
self.copy_tree("queries", dest)
super().run()
class BdistWheel(bdist_wheel):
def get_tag(self):
python, abi, platform = super().get_tag()
if python.startswith("cp"):
python, abi = "cp38", "abi3"
return python, abi, platform
setup(
packages=find_packages("bindings/python"),
package_dir={"": "bindings/python"},
package_data={
"tree_sitter_scala": ["*.pyi", "py.typed"],
"tree_sitter_scala.queries": ["*.scm"],
},
ext_package="tree_sitter_scala",
ext_modules=[
Extension(
name="_binding",
sources=[
"bindings/python/tree_sitter_scala/binding.c",
"src/parser.c",
# NOTE: if your language uses an external scanner, add it here.
],
extra_compile_args=[
"-std=c11",
] if system() != "Windows" else [
"/std:c11",
"/utf-8",
],
define_macros=[
("Py_LIMITED_API", "0x03080000"),
("PY_SSIZE_T_CLEAN", None)
],
include_dirs=["src"],
py_limited_api=True,
)
],
cmdclass={
"build": Build,
"bdist_wheel": BdistWheel
},
zip_safe=False
)

File diff suppressed because it is too large Load Diff

@ -93,6 +93,10 @@
"type": "floating_point_literal",
"named": true
},
{
"type": "given_pattern",
"named": true
},
{
"type": "identifier",
"named": true
@ -227,6 +231,10 @@
"type": "lambda_expression",
"named": true
},
{
"type": "macro_body",
"named": true
},
{
"type": "match_expression",
"named": true
@ -686,6 +694,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "unit",
"named": true
@ -813,7 +825,7 @@
"fields": {
"name": {
"multiple": false,
"required": true,
"required": false,
"types": [
{
"type": "identifier",
@ -889,12 +901,26 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
}
]
}
},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "wildcard",
"named": true
}
]
}
},
{
@ -1299,7 +1325,7 @@
"named": true,
"fields": {
"body": {
"multiple": false,
"multiple": true,
"required": false,
"types": [
{
@ -1474,6 +1500,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -1584,6 +1614,10 @@
"type": "_definition",
"named": true
},
{
"type": "comment",
"named": true
},
{
"type": "expression",
"named": true
@ -1751,6 +1785,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -1879,6 +1917,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "stable_type_identifier",
"named": true
},
{
"type": "type_identifier",
"named": true
@ -2624,6 +2666,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -2758,6 +2804,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -2860,6 +2910,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -2960,12 +3014,20 @@
},
"body": {
"multiple": false,
"required": true,
"required": false,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "indented_block",
"named": true
},
{
"type": "indented_cases",
"named": true
},
{
"type": "with_template_body",
"named": true
@ -3080,6 +3142,78 @@
]
}
},
{
"type": "given_pattern",
"named": true,
"fields": {
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "annotated_type",
"named": true
},
{
"type": "compound_type",
"named": true
},
{
"type": "function_type",
"named": true
},
{
"type": "generic_type",
"named": true
},
{
"type": "infix_type",
"named": true
},
{
"type": "literal_type",
"named": true
},
{
"type": "match_type",
"named": true
},
{
"type": "projected_type",
"named": true
},
{
"type": "singleton_type",
"named": true
},
{
"type": "stable_type_identifier",
"named": true
},
{
"type": "structural_type",
"named": true
},
{
"type": "tuple_type",
"named": true
},
{
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
}
]
}
}
},
{
"type": "guard",
"named": true,
@ -3404,6 +3538,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -3958,7 +4096,7 @@
"named": true,
"fields": {
"parameters": {
"multiple": false,
"multiple": true,
"required": true,
"types": [
{
@ -3969,6 +4107,10 @@
"type": "identifier",
"named": true
},
{
"type": "implicit",
"named": false
},
{
"type": "operator_identifier",
"named": true
@ -4059,6 +4201,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -4158,6 +4304,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -4166,6 +4316,109 @@
}
}
},
{
"type": "macro_body",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "call_expression",
"named": true
},
{
"type": "case_block",
"named": true
},
{
"type": "character_literal",
"named": true
},
{
"type": "field_expression",
"named": true
},
{
"type": "floating_point_literal",
"named": true
},
{
"type": "generic_function",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "infix_expression",
"named": true
},
{
"type": "instance_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "interpolated_string_expression",
"named": true
},
{
"type": "null_literal",
"named": true
},
{
"type": "operator_identifier",
"named": true
},
{
"type": "parenthesized_expression",
"named": true
},
{
"type": "prefix_expression",
"named": true
},
{
"type": "quote_expression",
"named": true
},
{
"type": "splice_expression",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple_expression",
"named": true
},
{
"type": "unit",
"named": true
},
{
"type": "wildcard",
"named": true
}
]
}
},
{
"type": "match_expression",
"named": true,
@ -4372,6 +4625,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -4645,6 +4902,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -4735,6 +4996,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -4814,6 +5079,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -5151,6 +5420,10 @@
{
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
}
]
}
@ -5254,6 +5527,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -5345,6 +5622,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -5472,6 +5753,10 @@
{
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
}
]
}
@ -5616,6 +5901,16 @@
}
]
},
"derive": {
"multiple": false,
"required": false,
"types": [
{
"type": "derives_clause",
"named": true
}
]
},
"extend": {
"multiple": false,
"required": false,
@ -5803,6 +6098,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -5870,6 +6169,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -5945,6 +6248,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6007,6 +6314,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6145,6 +6456,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6181,6 +6496,128 @@
]
}
},
{
"type": "type_lambda",
"named": true,
"fields": {
"bound": {
"multiple": true,
"required": false,
"types": [
{
"type": "context_bound",
"named": true
},
{
"type": "lower_bound",
"named": true
},
{
"type": "upper_bound",
"named": true
},
{
"type": "view_bound",
"named": true
}
]
},
"name": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "operator_identifier",
"named": true
},
{
"type": "wildcard",
"named": true
}
]
},
"return_type": {
"multiple": false,
"required": true,
"types": [
{
"type": "annotated_type",
"named": true
},
{
"type": "compound_type",
"named": true
},
{
"type": "function_type",
"named": true
},
{
"type": "generic_type",
"named": true
},
{
"type": "infix_type",
"named": true
},
{
"type": "literal_type",
"named": true
},
{
"type": "match_type",
"named": true
},
{
"type": "projected_type",
"named": true
},
{
"type": "singleton_type",
"named": true
},
{
"type": "stable_type_identifier",
"named": true
},
{
"type": "structural_type",
"named": true
},
{
"type": "tuple_type",
"named": true
},
{
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
}
]
},
"type_parameters": {
"multiple": true,
"required": false,
"types": [
{
"type": "type_parameters",
"named": true
}
]
}
}
},
{
"type": "type_parameters",
"named": true,
@ -6251,6 +6688,10 @@
{
"type": "covariant_type_parameter",
"named": true
},
{
"type": "type_lambda",
"named": true
}
]
}
@ -6325,6 +6766,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6398,6 +6843,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6499,6 +6948,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6595,6 +7048,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6709,6 +7166,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6805,6 +7266,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6905,6 +7370,10 @@
"type": "type_identifier",
"named": true
},
{
"type": "type_lambda",
"named": true
},
{
"type": "wildcard",
"named": true
@ -6991,6 +7460,10 @@
"type": "!",
"named": false
},
{
"type": "\"",
"named": false
},
{
"type": "#",
"named": false
@ -7071,6 +7544,10 @@
"type": "=>",
"named": false
},
{
"type": "=>>",
"named": false
},
{
"type": ">",
"named": false
@ -7211,6 +7688,10 @@
"type": "lazy",
"named": false
},
{
"type": "macro",
"named": false
},
{
"type": "match",
"named": false

File diff suppressed because it is too large Load Diff

@ -1,9 +1,17 @@
#include "stack.h"
#include "tree_sitter/alloc.h"
#include "tree_sitter/array.h"
#include "tree_sitter/parser.h"
#include <stdio.h>
#include <string.h>
#include <wctype.h>
// #define DEBUG
#ifdef DEBUG
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#else
#define LOG(...)
#endif
enum TokenType {
AUTOMATIC_SEMICOLON,
INDENT,
@ -18,32 +26,86 @@ enum TokenType {
CATCH,
FINALLY,
EXTENDS,
DERIVES,
WITH,
};
typedef struct {
Array(int16_t) indents;
int16_t last_indentation_size;
int16_t last_newline_count;
int16_t last_column;
} Scanner;
void *tree_sitter_scala_external_scanner_create() {
return createStack();
Scanner *scanner = ts_calloc(1, sizeof(Scanner));
array_init(&scanner->indents);
scanner->last_indentation_size = -1;
scanner->last_column = -1;
return scanner;
}
void tree_sitter_scala_external_scanner_destroy(void *p) {
free(p);
void tree_sitter_scala_external_scanner_destroy(void *payload) {
Scanner *scanner = payload;
array_delete(&scanner->indents);
ts_free(scanner);
}
void tree_sitter_scala_external_scanner_reset(void *p) {
resetStack(p);
}
unsigned tree_sitter_scala_external_scanner_serialize(void *payload, char *buffer) {
Scanner *scanner = (Scanner*)payload;
if ((scanner->indents.size + 3) * sizeof(int16_t) > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return 0;
}
unsigned tree_sitter_scala_external_scanner_serialize(void *p, char *buffer) {
return serialiseStack(p, buffer);
size_t size = 0;
*(int16_t *)&buffer[size] = scanner->last_indentation_size;
size += sizeof(int16_t);
*(int16_t *)&buffer[size] = scanner->last_newline_count;
size += sizeof(int16_t);
*(int16_t *)&buffer[size] = scanner->last_column;
size += sizeof(int16_t);
for (unsigned i = 0; i < scanner->indents.size; i++) {
*(int16_t *)&buffer[size] = scanner->indents.contents[i];
size += sizeof(int16_t);
}
return size;
}
void tree_sitter_scala_external_scanner_deserialize(void *p, const char *b,
unsigned n) {
deserialiseStack(p, b, n);
void tree_sitter_scala_external_scanner_deserialize(void *payload, const char *buffer,
unsigned length) {
Scanner *scanner = (Scanner*)payload;
array_clear(&scanner->indents);
scanner->last_indentation_size = -1;
scanner->last_column = -1;
scanner->last_newline_count = 0;
if (length == 0) {
return;
}
size_t size = 0;
scanner->last_indentation_size = *(int16_t *)&buffer[size];
size += sizeof(int16_t);
scanner->last_newline_count = *(int16_t *)&buffer[size];
size += sizeof(int16_t);
scanner->last_column = *(int16_t *)&buffer[size];
size += sizeof(int16_t);
while (size < length) {
array_push(&scanner->indents, *(int16_t *)&buffer[size]);
size += sizeof(int16_t);
}
assert(size == length);
}
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static bool scan_string_content(TSLexer *lexer, bool is_multiline, bool has_interpolation) {
unsigned closing_quote_count = 0;
@ -63,24 +125,26 @@ static bool scan_string_content(TSLexer *lexer, bool is_multiline, bool has_inte
if (is_multiline && has_interpolation) {
lexer->result_symbol = INTERPOLATED_MULTILINE_STRING_MIDDLE;
return true;
} else if (has_interpolation){
}
if (has_interpolation) {
lexer->result_symbol = INTERPOLATED_STRING_MIDDLE;
return true;
} else {
advance(lexer);
}
advance(lexer);
} else {
closing_quote_count = 0;
if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead != 0) advance(lexer);
if (!lexer->eof(lexer)) {
advance(lexer);
}
} else if (lexer->lookahead == '\n') {
if (is_multiline) {
advance(lexer);
} else {
return false;
}
} else if (lexer->lookahead == 0) {
} else if (lexer->eof(lexer)) {
return false;
} else {
advance(lexer);
@ -101,54 +165,84 @@ static bool detect_comment_start(TSLexer *lexer) {
return false;
}
static bool scan_word(TSLexer *lexer, const char* const word) {
for (uint8_t i = 0; word[i] != '\0'; i++) {
if (lexer->lookahead != word[i]) {
return false;
}
advance(lexer);
}
return !iswalnum(lexer->lookahead);
}
static inline void debug_indents(Scanner *scanner) {
LOG(" indents(%d): ", scanner->indents.size);
for (unsigned i = 0; i < scanner->indents.size; i++) {
LOG("%d ", scanner->indents.contents[i]);
}
LOG("\n");
}
bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
const bool *valid_symbols) {
ScannerStack *stack = (ScannerStack *)payload;
int prev = peekStack(stack);
int newline_count = 0;
int indentation_size = 0;
LOG("scanner was called at column: %d\n", lexer->get_column(lexer));
Scanner *scanner = (Scanner *)payload;
int16_t prev = scanner->indents.size > 0 ? *array_back(&scanner->indents) : -1;
int16_t newline_count = 0;
int16_t indentation_size = 0;
while (iswspace(lexer->lookahead)) {
if (lexer->lookahead == '\n') {
newline_count++;
indentation_size = 0;
}
else
else {
indentation_size++;
}
skip(lexer);
}
// Before advancing the lexer, check if we can double outdent
if (valid_symbols[OUTDENT] &&
(lexer->lookahead == 0 ||
if (
valid_symbols[OUTDENT] &&
(
(prev != -1) &&
lexer->lookahead == ')' ||
lexer->lookahead == ']' ||
lexer->lookahead == '}'
) || (
stack->last_indentation_size != -1 &&
prev != -1 &&
stack->last_indentation_size < prev))) {
popStack(stack);
lexer->lookahead == 0 ||
(
prev != -1 &&
(
lexer->lookahead == ')' ||
lexer->lookahead == ']' ||
lexer->lookahead == '}'
)
) ||
(
scanner->last_indentation_size != -1 &&
prev != -1 &&
scanner->last_indentation_size < prev
)
)
) {
if (scanner->indents.size > 0) {
array_pop(&scanner->indents);
}
LOG(" pop\n");
LOG(" OUTDENT\n");
lexer->result_symbol = OUTDENT;
return true;
}
stack->last_indentation_size = -1;
printStack(stack, " before");
scanner->last_indentation_size = -1;
if (valid_symbols[INDENT] &&
if (
valid_symbols[INDENT] &&
newline_count > 0 &&
(isEmptyStack(stack) ||
indentation_size > peekStack(stack))) {
(
scanner->indents.size == 0 ||
indentation_size > *array_back(&scanner->indents)
)
) {
if (detect_comment_start(lexer)) {
return false;
}
pushStack(stack, indentation_size);
array_push(&scanner->indents, indentation_size);
lexer->result_symbol = INDENT;
LOG(" INDENT\n");
return true;
@ -157,11 +251,17 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
// This saves the indentation_size and newline_count so it can be used
// in subsequent calls for multiple outdent or autosemicolon.
if (valid_symbols[OUTDENT] &&
(lexer->lookahead == 0 || (
(lexer->lookahead == 0 ||
(
newline_count > 0 &&
prev != -1 &&
indentation_size < prev))) {
popStack(stack);
indentation_size < prev
)
)
) {
if (scanner->indents.size > 0) {
array_pop(&scanner->indents);
}
LOG(" pop\n");
LOG(" OUTDENT\n");
lexer->result_symbol = OUTDENT;
@ -169,28 +269,26 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
if (detect_comment_start(lexer)) {
return false;
}
stack->last_indentation_size = indentation_size;
stack->last_newline_count = newline_count;
scanner->last_indentation_size = indentation_size;
scanner->last_newline_count = newline_count;
if (lexer->eof(lexer)) {
stack->last_column = -1;
scanner->last_column = -1;
} else {
stack->last_column = lexer->get_column(lexer);
scanner->last_column = (int16_t)lexer->get_column(lexer);
}
return true;
}
// Recover newline_count from the outdent reset
if (stack->last_newline_count > 0 &&
((lexer->eof(lexer) && stack->last_column == -1)
|| lexer->get_column(lexer) == stack->last_column)) {
newline_count += stack->last_newline_count;
bool is_eof = lexer->eof(lexer);
if (
scanner->last_newline_count > 0 &&
(is_eof && scanner->last_column == -1) ||
(!is_eof && lexer->get_column(lexer) == (uint32_t)scanner->last_column)
) {
newline_count += scanner->last_newline_count;
}
stack->last_newline_count = 0;
printStack(stack, " after");
LOG(" indentation_size: %d, newline_count: %d, column: %d, indent_is_valid: %d, dedent_is_valid: %d\n", indentation_size,
newline_count, lexer->get_column(lexer), valid_symbols[INDENT], valid_symbols[OUTDENT]);
scanner->last_newline_count = 0;
if (valid_symbols[AUTOMATIC_SEMICOLON] && newline_count > 0) {
// AUTOMATIC_SEMICOLON should not be issued in the middle of expressions
@ -203,14 +301,17 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
// a
// .b
// .c
if (lexer->lookahead == '.') return false;
if (lexer->lookahead == '.') {
return false;
}
// Single-line and multi-line comments
if (lexer->lookahead == '/') {
advance(lexer);
if (lexer->lookahead == '/') {
return false;
} else if (lexer->lookahead == '*') {
}
if (lexer->lookahead == '*') {
advance(lexer);
while (!lexer->eof(lexer)) {
if (lexer->lookahead == '*') {
@ -229,7 +330,7 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
}
skip(lexer);
}
// If some code is present at the same line after comment end,
// If some code is present at the same line after comment end,
// we should still produce AUTOMATIC_SEMICOLON, e.g. in
// val a = 1
// /* comment */ val b = 2
@ -238,108 +339,50 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
}
if (valid_symbols[ELSE]) {
if (lexer->lookahead != 'e') return true;
advance(lexer);
if (lexer->lookahead != 'l') return true;
advance(lexer);
if (lexer->lookahead != 's') return true;
advance(lexer);
if (lexer->lookahead != 'e') return true;
advance(lexer);
if (iswalpha(lexer->lookahead)) return true;
return false;
return !scan_word(lexer, "else");
}
if (valid_symbols[CATCH]) {
if (lexer->lookahead != 'c' && lexer->lookahead != 'f') return true;
advance(lexer);
if (lexer->lookahead == 'a') {
advance(lexer);
if (lexer->lookahead != 't') return true;
advance(lexer);
if (lexer->lookahead != 'c') return true;
advance(lexer);
if (lexer->lookahead != 'h') return true;
advance(lexer);
if (iswalpha(lexer->lookahead)) return true;
return false;
} else if (lexer->lookahead == 'i') {
advance(lexer);
if (lexer->lookahead != 'n') return true;
advance(lexer);
if (lexer->lookahead != 'a') return true;
advance(lexer);
if (lexer->lookahead != 'l') return true;
advance(lexer);
if (lexer->lookahead != 'l') return true;
advance(lexer);
if (lexer->lookahead != 'y') return true;
advance(lexer);
if (iswalpha(lexer->lookahead)) return true;
if (scan_word(lexer, "catch")) {
return false;
} else {
return true;
}
}
if (valid_symbols[FINALLY]) {
if (lexer->lookahead != 'f') return true;
advance(lexer);
if (lexer->lookahead != 'i') return true;
advance(lexer);
if (lexer->lookahead != 'n') return true;
advance(lexer);
if (lexer->lookahead != 'a') return true;
advance(lexer);
if (lexer->lookahead != 'l') return true;
advance(lexer);
if (lexer->lookahead != 'l') return true;
advance(lexer);
if (lexer->lookahead != 'y') return true;
advance(lexer);
if (iswalpha(lexer->lookahead)) return true;
return false;
if (scan_word(lexer, "finally")) {
return false;
}
}
if (valid_symbols[EXTENDS]) {
if (lexer->lookahead != 'e') return true;
advance(lexer);
if (lexer->lookahead != 'x') return true;
advance(lexer);
if (lexer->lookahead != 't') return true;
advance(lexer);
if (lexer->lookahead != 'e') return true;
advance(lexer);
if (lexer->lookahead != 'n') return true;
advance(lexer);
if (lexer->lookahead != 'd') return true;
advance(lexer);
if (lexer->lookahead != 's') return true;
advance(lexer);
if (iswalpha(lexer->lookahead)) return true;
return false;
if (scan_word(lexer, "extends")) {
return false;
}
}
if (valid_symbols[WITH]) {
if (lexer->lookahead != 'w') return true;
advance(lexer);
if (lexer->lookahead != 'i') return true;
advance(lexer);
if (lexer->lookahead != 't') return true;
advance(lexer);
if (lexer->lookahead != 'h') return true;
advance(lexer);
if (iswalpha(lexer->lookahead)) return true;
return false;
if (scan_word(lexer, "with")) {
return false;
}
}
if (valid_symbols[DERIVES]) {
if (scan_word(lexer, "derives")) {
return false;
}
}
if (newline_count > 1) return true;
if (newline_count > 1) {
return true;
}
return true;
}
while (iswspace(lexer->lookahead)) {
if (lexer->lookahead == '\n') newline_count++;
if (lexer->lookahead == '\n') {
newline_count++;
}
skip(lexer);
}

@ -1,99 +0,0 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifdef DEBUG
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#else
#define LOG(...)
#endif
// Total payload size is 1024 bytes max
#define STACK_SIZE 100
typedef struct ScannerStack {
int stack[STACK_SIZE];
int top;
int last_indentation_size;
int last_newline_count;
int last_column;
} ScannerStack;
ScannerStack* createStack() {
ScannerStack* ptr = (ScannerStack*) malloc(sizeof(ScannerStack));
ptr -> top = 0;
ptr -> last_indentation_size = -1;
ptr -> last_newline_count = 0;
ptr -> last_column = -1;
memset(ptr -> stack, STACK_SIZE, (0));
return ptr;
}
bool isEmptyStack(ScannerStack *stack) { return stack->top == 0; }
int peekStack(ScannerStack *stack) {
return isEmptyStack(stack) ? -1 : stack->stack[stack->top - 1];
}
void pushStack(ScannerStack *stack, unsigned int value) {
stack->top++;
stack->stack[stack->top - 1] = value;
}
int popStack(ScannerStack *stack) {
if (isEmptyStack(stack))
return -1;
else {
int result = peekStack(stack);
stack->top--;
return result;
}
}
void printStack(ScannerStack *stack, char *msg) {
LOG("%s Stack[top = %d; ", msg, stack->top);
for (int i = 0; i < stack->top; i++) {
LOG("%d | ", stack->stack[i]);
}
LOG("]\n");
}
unsigned serialiseStack(ScannerStack *stack, char *buf) {
int elements = isEmptyStack(stack) ? 0 : stack->top;
if (elements < 0) {
elements = 0;
}
unsigned result_length = (elements + 3) * sizeof(int);
int *placement = (int *)buf;
memcpy(placement, stack->stack, elements * sizeof(int));
placement[elements] = stack->last_indentation_size;
placement[elements + 1] = stack->last_newline_count;
placement[elements + 2] = stack->last_column;
return result_length;
}
void deserialiseStack(ScannerStack* stack, const char* buf, unsigned n) {
if (n != 0) {
int *intBuf = (int *)buf;
unsigned elements = n / sizeof(int) - 3;
stack->top = elements;
memcpy(stack->stack, intBuf, elements * sizeof(int));
stack->last_indentation_size = intBuf[elements];
stack->last_newline_count = intBuf[elements + 1];
stack->last_column = intBuf[elements + 2];
}
}
void resetStack(ScannerStack *p) {
p->top = 0;
p->last_indentation_size = -1;
p->last_newline_count = 0;
p->last_column = -1;
}

@ -0,0 +1,54 @@
#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void *(*ts_current_malloc)(size_t);
extern void *(*ts_current_calloc)(size_t, size_t);
extern void *(*ts_current_realloc)(void *, size_t);
extern void (*ts_current_free)(void *);
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif
#else
#ifndef ts_malloc
#define ts_malloc malloc
#endif
#ifndef ts_calloc
#define ts_calloc calloc
#endif
#ifndef ts_realloc
#define ts_realloc realloc
#endif
#ifndef ts_free
#define ts_free free
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ALLOC_H_

@ -0,0 +1,290 @@
#ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "./alloc.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#define Array(T) \
struct { \
T *contents; \
uint32_t size; \
uint32_t capacity; \
}
/// Initialize an array.
#define array_init(self) \
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
/// Create an empty array.
#define array_new() \
{ NULL, 0, 0 }
/// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0)
/// Get a pointer to the last element in the array.
#define array_back(self) array_get(self, (self)->size - 1)
/// Clear the array, setting its size to zero. Note that this does not free any
/// memory allocated for the array's contents.
#define array_clear(self) ((self)->size = 0)
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
/// less than the array's current capacity, this function has no effect.
#define array_reserve(self, new_capacity) \
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
/// Free any memory allocated for this array. Note that this does not free any
/// memory allocated for the array's contents.
#define array_delete(self) _array__delete((Array *)(self))
/// Push a new `element` onto the end of the array.
#define array_push(self, element) \
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
(self)->contents[(self)->size++] = (element))
/// Increase the array's size by `count` elements.
/// New elements are zero-initialized.
#define array_grow_by(self, count) \
do { \
if ((count) == 0) break; \
_array__grow((Array *)(self), count, array_elem_size(self)); \
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
(self)->size += (count); \
} while (0)
/// Append all elements from one array to the end of another.
#define array_push_all(self, other) \
array_extend((self), (other)->size, (other)->contents)
/// Append `count` elements to the end of the array, reading their values from the
/// `contents` pointer.
#define array_extend(self, count, contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), (self)->size, \
0, count, contents \
)
/// Remove `old_count` elements from the array starting at the given `index`. At
/// the same index, insert `new_count` new elements, reading their values from the
/// `new_contents` pointer.
#define array_splice(self, _index, old_count, new_count, new_contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), _index, \
old_count, new_count, new_contents \
)
/// Insert one `element` into the array at the given `index`.
#define array_insert(self, _index, element) \
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
/// Remove one element from the array at the given `index`.
#define array_erase(self, _index) \
_array__erase((Array *)(self), array_elem_size(self), _index)
/// Pop the last element off the array, returning the element by value.
#define array_pop(self) ((self)->contents[--(self)->size])
/// Assign the contents of one array to another, reallocating if necessary.
#define array_assign(self, other) \
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
/// Swap one array with another
#define array_swap(self, other) \
_array__swap((Array *)(self), (Array *)(other))
/// Get the size of the array contents
#define array_elem_size(self) (sizeof *(self)->contents)
/// Search a sorted array for a given `needle` value, using the given `compare`
/// callback to determine the order.
///
/// If an existing element is found to be equal to `needle`, then the `index`
/// out-parameter is set to the existing value's index, and the `exists`
/// out-parameter is set to true. Otherwise, `index` is set to an index where
/// `needle` should be inserted in order to preserve the sorting, and `exists`
/// is set to false.
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
/// Search a sorted array for a given `needle` value, using integer comparisons
/// of a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, _index, _exists) \
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
/// Insert a given `value` into a sorted array, using the given `compare`
/// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
/// Insert a given `value` into a sorted array, using integer comparisons of
/// a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
// Private
typedef Array(void) Array;
/// This is not what you're looking for, see `array_delete`.
static inline void _array__delete(Array *self) {
if (self->contents) {
ts_free(self->contents);
self->contents = NULL;
self->size = 0;
self->capacity = 0;
}
}
/// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array *self, size_t element_size,
uint32_t index) {
assert(index < self->size);
char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size,
(self->size - index - 1) * element_size);
self->size--;
}
/// This is not what you're looking for, see `array_reserve`.
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
if (new_capacity > self->capacity) {
if (self->contents) {
self->contents = ts_realloc(self->contents, new_capacity * element_size);
} else {
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
}
}
/// This is not what you're looking for, see `array_assign`.
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
_array__reserve(self, element_size, other->size);
self->size = other->size;
memcpy(self->contents, other->contents, self->size * element_size);
}
/// This is not what you're looking for, see `array_swap`.
static inline void _array__swap(Array *self, Array *other) {
Array swap = *other;
*other = *self;
*self = swap;
}
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
uint32_t new_size = self->size + count;
if (new_size > self->capacity) {
uint32_t new_capacity = self->capacity * 2;
if (new_capacity < 8) new_capacity = 8;
if (new_capacity < new_size) new_capacity = new_size;
_array__reserve(self, element_size, new_capacity);
}
}
/// This is not what you're looking for, see `array_splice`.
static inline void _array__splice(Array *self, size_t element_size,
uint32_t index, uint32_t old_count,
uint32_t new_count, const void *elements) {
uint32_t new_size = self->size + new_count - old_count;
uint32_t old_end = index + old_count;
uint32_t new_end = index + new_count;
assert(old_end <= self->size);
_array__reserve(self, element_size, new_size);
char *contents = (char *)self->contents;
if (self->size > old_end) {
memmove(
contents + new_end * element_size,
contents + old_end * element_size,
(self->size - old_end) * element_size
);
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
}
}
self->size += new_count - old_count;
}
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
do { \
*(_index) = start; \
*(_exists) = false; \
uint32_t size = (self)->size - *(_index); \
if (size == 0) break; \
int comparison; \
while (size > 1) { \
uint32_t half_size = size / 2; \
uint32_t mid_index = *(_index) + half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
if (comparison <= 0) *(_index) = mid_index; \
size -= half_size; \
} \
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
if (comparison == 0) *(_exists) = true; \
else if (comparison < 0) *(_index) += 1; \
} while (0)
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
/// parameter by reference in order to work with the generic sorting function above.
#define _compare_int(a, b) ((int)*(a) - (int)(b))
#ifdef _MSC_VER
#pragma warning(default : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ARRAY_H_

@ -13,9 +13,8 @@ extern "C" {
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
@ -87,6 +86,11 @@ typedef union {
} entry;
} TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
@ -126,13 +130,38 @@ struct TSLanguage {
const TSStateId *primary_state_ids;
};
static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
uint32_t index = 0;
uint32_t size = len - index;
while (size > 1) {
uint32_t half_size = size / 2;
uint32_t mid_index = index + half_size;
TSCharacterRange *range = &ranges[mid_index];
if (lookahead >= range->start && lookahead <= range->end) {
return true;
} else if (lookahead > range->end) {
index = mid_index;
}
size -= half_size;
}
TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
}
/*
* Lexer Macros
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
int32_t lookahead; \
goto start; \
@ -148,6 +177,17 @@ struct TSLanguage {
goto next_state; \
}
#define ADVANCE_MAP(...) \
{ \
static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
if (map[i] == lookahead) { \
state = map[i + 1]; \
goto next_state; \
} \
} \
}
#define SKIP(state_value) \
{ \
skip = true; \
@ -166,7 +206,7 @@ struct TSLanguage {
* Parse Table Macros
*/
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id
@ -176,7 +216,7 @@ struct TSLanguage {
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value \
.state = (state_value) \
} \
}}
@ -184,7 +224,7 @@ struct TSLanguage {
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value, \
.state = (state_value), \
.repetition = true \
} \
}}
@ -197,14 +237,15 @@ struct TSLanguage {
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
#define REDUCE(symbol_name, children, precedence, prod_id) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_name, \
.child_count = children, \
.dynamic_precedence = precedence, \
.production_id = prod_id \
}, \
}}
#define RECOVER() \

@ -64,7 +64,7 @@ Using directives
//> using jvm graalvm:21
//> using scala 3.3.0
//> using dep foo:bar:1,2,3,url=https://github.com
//> using exclude "examples/*" "*/resources/*"
//> using exclude "examples/*" "*/resources/*"
// > just a comment
--------------------------------------------------------------------------------
@ -87,3 +87,16 @@ Using directives
(using_directive_key)
(using_directive_value)))
(comment))
================================================================================
Shebang
================================================================================
#!/usr/bin/env -S scala-cli shebang -S 3
"shebang"
--------------------------------------------------------------------------------
(compilation_unit
(comment)
(string))

@ -418,6 +418,12 @@ object O4 extends {
val a: A = b
}
object O5 {
;;
;
;
}
--------------------------------------------------------------------------------
(compilation_unit
@ -440,7 +446,10 @@ object O4 extends {
(val_definition
(identifier)
(type_identifier)
(identifier))))))
(identifier)))))
(object_definition
(identifier)
(template_body)))
================================================================================
Object definitions
@ -502,7 +511,9 @@ class A
================================================================================
Class definitions (Scala 3)
================================================================================
final case class C() extends A derives B, C
final case class C()
extends A
derives B, C.D
--------------------------------------------------------------------------------
(compilation_unit
@ -514,7 +525,9 @@ final case class C() extends A derives B, C
(type_identifier))
(derives_clause
(type_identifier)
(type_identifier))))
(stable_type_identifier
(identifier)
(type_identifier)))))
================================================================================
Subclass definitions
@ -784,7 +797,7 @@ Trait definitions
trait A extends B
trait A extends B with C
trait A extends B with C derives D
trait T[U] {
}
@ -803,6 +816,8 @@ trait T[U] extends V.W[U] {
(identifier)
(extends_clause
(type_identifier)
(type_identifier))
(derives_clause
(type_identifier)))
(trait_definition
(identifier)
@ -1076,6 +1091,9 @@ class A {
def n(using a: A)
(using B <:< B, C =:= C) = ()
def o()
: Int = 42
}
--------------------------------------------------------------------------------
@ -1117,7 +1135,12 @@ class A {
(type_identifier)
(operator_identifier)
(type_identifier)))
(unit)))))
(unit))
(function_definition
(identifier)
(parameters)
(type_identifier)
(integer_literal)))))
================================================================================
Function definitions (Scala 3 syntax)
@ -1164,6 +1187,9 @@ object A:
extension [A1](d: D) def foo = "foo"
trait B:
extension (x: Int) def bar: String
--------------------------------------------------------------------------------
(compilation_unit
@ -1188,7 +1214,18 @@ object A:
(type_identifier)))
(function_definition
(identifier)
(string))))))
(string)))))
(trait_definition
(identifier)
(template_body
(extension_definition
(parameters
(parameter
(identifier)
(type_identifier)))
(function_declaration
(identifier)
(type_identifier))))))
================================================================================
Given instance definitions (Scala 3 syntax)
@ -1206,13 +1243,26 @@ object A:
private given listFoo[A1](using ev: CanFoo[A1]): CanFoo[List[A1]] with
def foo(xs: List[A1]): Int = 0
given [T: Ordering]: Ordering[List[T]] with
def x = ()
given foo(using ev: Foo)
: Foo = ev
given Context = ctx
given Context[T] = ctx
given Foo[Int] =
case n => "123"
given (using a: Int): Int = a
given [A: Foo]: Foo[List[A]] with
def x = ()
given [A](using Foo[A]): Foo[List[A]] with
def x = ()
trait B:
given c: Context[T]
--------------------------------------------------------------------------------
@ -1286,8 +1336,39 @@ object A:
(type_identifier)
(integer_literal))))
(given_definition
(identifier
(MISSING _alpha_identifier))
(identifier)
(parameters
(parameter
(identifier)
(type_identifier)))
(type_identifier)
(identifier))
(given_definition
(type_identifier)
(identifier))
(given_definition
(generic_type
(type_identifier)
(type_arguments
(type_identifier)))
(identifier))
(given_definition
(generic_type
(type_identifier)
(type_arguments
(type_identifier)))
(indented_cases
(case_clause
(identifier)
(string))))
(given_definition
(parameters
(parameter
(identifier)
(type_identifier)))
(type_identifier)
(identifier))
(given_definition
(type_parameters
(identifier)
(context_bound
@ -1304,14 +1385,33 @@ object A:
(identifier)
(unit))))
(given_definition
(type_identifier)
(identifier))
(given_definition
(type_parameters
(identifier))
(parameters
(generic_type
(type_identifier)
(type_arguments
(type_identifier))))
(generic_type
(type_identifier)
(type_arguments
(type_identifier)))
(identifier)))))
(generic_type
(type_identifier)
(type_arguments
(type_identifier)))))
(with_template_body
(function_definition
(identifier)
(unit))))
(trait_definition
(identifier)
(template_body
(given_definition
(identifier)
(generic_type
(type_identifier)
(type_arguments
(type_identifier)))))))))
================================================================================
Top-level Definitions (Scala 3 syntax)
@ -1578,16 +1678,16 @@ inline given Test =
(modifiers
(inline_modifier))
(type_identifier)
(instance_expression
(type_identifier))))
(indented_block
(instance_expression
(type_identifier)))))
================================================================================
Infix methods (Scala 3)
================================================================================
object Test:
infix private def hello = 25
inline infix private def hello = 25
--------------------------------------------------------------------------------
(compilation_unit
@ -1596,6 +1696,7 @@ object Test:
(template_body
(function_definition
(modifiers
(inline_modifier)
(infix_modifier)
(access_modifier))
(identifier)

@ -323,10 +323,16 @@ class C {
def other() {
if (a) {
b
}
};
else c()
}
def another() {
if (a) b match {
case _ => c
} else d
}
--------------------------------------------------------------------------------
(compilation_unit
@ -375,7 +381,21 @@ def other() {
(identifier))
(call_expression
(identifier)
(arguments))))))
(arguments)))))
(function_definition
(identifier)
(parameters)
(block
(if_expression
(parenthesized_expression
(identifier))
(match_expression
(identifier)
(case_block
(case_clause
(wildcard)
(identifier))))
(identifier)))))
================================================================================
If expressions (Scala 3 syntax)
@ -404,6 +424,11 @@ class C:
else
()
if (a) || b(c) then return true
if (a) && b.c then
()
--------------------------------------------------------------------------------
(compilation_unit
@ -445,6 +470,27 @@ class C:
(indented_block
(unit)
(comment))
(indented_block
(unit)))
(if_expression
(infix_expression
(parenthesized_expression
(identifier))
(operator_identifier)
(call_expression
(identifier)
(arguments
(identifier))))
(return_expression
(boolean_literal)))
(if_expression
(infix_expression
(parenthesized_expression
(identifier))
(operator_identifier)
(field_expression
(identifier)
(identifier)))
(indented_block
(unit))))))))
@ -643,6 +689,8 @@ def matchTest(x: Int): String = x match {
case 3 => {
"3"
}
case 4 =>
;
case A if a == 1 =>
case A if a == 2 => 2
case ((i, _)) => i
@ -680,6 +728,8 @@ def matchTest(x: Int): String = x match {
(integer_literal)
(block
(string)))
(case_clause
(integer_literal))
(case_clause
(identifier)
(guard
@ -1065,6 +1115,9 @@ object O {
}
{ b =>
if (c) d.e }
{ a => implicit b => b }
{ (a: Int) ?=> (b: Int) => b }
{ (_, a) => a }
}
--------------------------------------------------------------------------------
@ -1137,7 +1190,33 @@ object O {
(identifier))
(field_expression
(identifier)
(identifier)))))))))
(identifier))))))
(block
(lambda_expression
(identifier)
(lambda_expression
(identifier)
(identifier))))
(block
(lambda_expression
(bindings
(binding
(identifier)
(type_identifier)))
(lambda_expression
(bindings
(binding
(identifier)
(type_identifier)))
(identifier))))
(block
(lambda_expression
(bindings
(binding
(wildcard))
(binding
(identifier)))
(identifier))))))
================================================================================
Unit expressions
@ -1502,6 +1581,30 @@ def main() {
(identifier)
(identifier)))))
================================================================================
Macros (Scala 2 syntax)
================================================================================
class Foo {
def a: A =
macro B.b
}
--------------------------------------------------------------------------------
(compilation_unit
(class_definition
(identifier)
(template_body
(function_definition
(identifier)
(type_identifier)
(indented_block
(macro_body
(field_expression
(identifier)
(identifier))))))))
================================================================================
Macros (Scala 3 syntax)
================================================================================

@ -38,6 +38,15 @@ val string3 = raw"Not a new line \n${ha}"
val string4 = s"""
works even in multiline strings, ${name}
"""
val string5 = s"$works${without}$spaces"
val string6 = s"$a$b"
val string7 = s"$$ $a"
val string8 = s"$"$a"
--------------------------------------------------------------------------------
(compilation_unit
@ -73,7 +82,43 @@ works even in multiline strings, ${name}
(interpolated_string
(interpolation
(block
(identifier)))))))
(identifier))))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier))
(interpolation
(block
(identifier)))
(interpolation
(identifier)))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier))
(interpolation
(identifier)))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier)))))
(val_definition
(identifier)
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier))))))
================================================================================
Integer literals
@ -183,6 +228,8 @@ val myChar = 'c'
val otherChar = '\u0041'
val otherChar2 = '\uu0041'
val anotherChar = '\n'
def foo(a: Char = 'c') = a + 'd'
@ -199,6 +246,9 @@ def foo(a: Char = 'c') = a + 'd'
(val_definition
(identifier)
(character_literal))
(val_definition
(identifier)
(character_literal))
(function_definition
(identifier)
(parameters

@ -0,0 +1,334 @@
================================================================================
Alternative patterns
================================================================================
val x = y match {
case 1 | a => b
case "c" | "d" | "e" => f
}
--------------------------------------------------------------------------------
(compilation_unit
(val_definition
(identifier)
(match_expression
(identifier)
(case_block
(case_clause
(alternative_pattern
(integer_literal)
(identifier))
(identifier))
(case_clause
(alternative_pattern
(alternative_pattern
(string)
(string))
(string))
(identifier))))))
================================================================================
Typed patterns
================================================================================
val x = y match {
case 1 : Int => 2
case a : B with C => d
case _: B | _: C => 3
case Object.Constant => 3
}
--------------------------------------------------------------------------------
(compilation_unit
(val_definition
(identifier)
(match_expression
(identifier)
(case_block
(case_clause
(typed_pattern
(integer_literal)
(type_identifier))
(integer_literal))
(case_clause
(typed_pattern
(identifier)
(compound_type
(type_identifier)
(type_identifier)))
(identifier))
(case_clause
(alternative_pattern
(typed_pattern
(wildcard)
(type_identifier))
(typed_pattern
(wildcard)
(type_identifier)))
(integer_literal))
(case_clause
(stable_identifier
(identifier)
(identifier))
(integer_literal))))))
================================================================================
Tuple patterns
================================================================================
val (a, b) = if (c) (d, e) else (f, g)
val x = y match {
case (A, B) => X
}
--------------------------------------------------------------------------------
(compilation_unit
(val_definition
(tuple_pattern
(identifier)
(identifier))
(if_expression
(parenthesized_expression
(identifier))
(tuple_expression
(identifier)
(identifier))
(tuple_expression
(identifier)
(identifier))))
(val_definition
(identifier)
(match_expression
(identifier)
(case_block
(case_clause
(tuple_pattern
(identifier)
(identifier))
(identifier))))))
================================================================================
Case class patterns
================================================================================
def showNotification(notification: Notification): String = {
notification match {
case Email(email, title, _) =>
s"You got an email from $email with title: $title"
case SMS(number, message) =>
s"You got an SMS from $number! Message: $message"
case VoiceRecording(name, link) =>
s"you received a Voice Recording from $name! Click the link to hear it: $link"
}
}
--------------------------------------------------------------------------------
(compilation_unit
(function_definition
(identifier)
(parameters
(parameter
(identifier)
(type_identifier)))
(type_identifier)
(block
(match_expression
(identifier)
(case_block
(case_clause
(case_class_pattern
(type_identifier)
(identifier)
(identifier)
(wildcard))
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier))
(interpolation
(identifier)))))
(case_clause
(case_class_pattern
(type_identifier)
(identifier)
(identifier))
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier))
(interpolation
(identifier)))))
(case_clause
(case_class_pattern
(type_identifier)
(identifier)
(identifier))
(interpolated_string_expression
(identifier)
(interpolated_string
(interpolation
(identifier))
(interpolation
(identifier))))))))))
================================================================================
Infix patterns
================================================================================
def first(x: Seq[Int]) = x match {
case e :+ _ => Some(e)
case _ => None
}
--------------------------------------------------------------------------------
(compilation_unit
(function_definition
(identifier)
(parameters
(parameter
(identifier)
(generic_type
(type_identifier)
(type_arguments
(type_identifier)))))
(match_expression
(identifier)
(case_block
(case_clause
(infix_pattern
(identifier)
(operator_identifier)
(wildcard))
(call_expression
(identifier)
(arguments
(identifier))))
(case_clause
(wildcard)
(identifier))))))
================================================================================
Capture patterns
================================================================================
val x = y match {
case a @ B(1) => a
case b @ C(d @ (e @ X, _: Y)) => e
case req @ (POST | GET) -> Root / "test" => 5
case Array(a: Type, _@_*) => y
}
--------------------------------------------------------------------------------
(compilation_unit
(val_definition
(identifier)
(match_expression
(identifier)
(case_block
(case_clause
(capture_pattern
(identifier)
(case_class_pattern
(type_identifier)
(integer_literal)))
(identifier))
(case_clause
(capture_pattern
(identifier)
(case_class_pattern
(type_identifier)
(capture_pattern
(identifier)
(tuple_pattern
(capture_pattern
(identifier)
(identifier))
(typed_pattern
(wildcard)
(type_identifier))))))
(identifier))
(case_clause
(infix_pattern
(infix_pattern
(capture_pattern
(identifier)
(tuple_pattern
(alternative_pattern
(identifier)
(identifier))))
(operator_identifier)
(identifier))
(operator_identifier)
(string))
(integer_literal))
(case_clause
(case_class_pattern
(type_identifier)
(typed_pattern
(identifier)
(type_identifier))
(repeat_pattern
(capture_pattern
(wildcard)
(wildcard))))
(identifier))))))
================================================================================
Quoted patterns (Scala 3 syntax)
================================================================================
def foo =
x match
case '{ $boolExpr } => Some(true)
case _ => None
--------------------------------------------------------------------------------
(compilation_unit
(function_definition
(identifier)
(indented_block
(match_expression
(identifier)
(indented_cases
(case_clause
(quote_expression
(identifier))
(call_expression
(identifier)
(arguments
(boolean_literal))))
(case_clause
(wildcard)
(identifier)))))))
================================================================================
Given pattern (Scala 3 syntax)
================================================================================
for
given Int <- Some(1)
yield ()
--------------------------------------------------------------------------------
(compilation_unit
(for_expression
(enumerators
(enumerator
(given_pattern
(type_identifier))
(call_expression
(identifier)
(arguments
(integer_literal)))))
(unit)))

@ -585,3 +585,42 @@ type A = B[({ type f[x] = M[S, x] })#f]
(type_identifier)
(type_identifier))))))
(type_identifier))))))
================================================================================
Type Lambdas (Scala 3)
================================================================================
type A = [B <: C] =>> D
type Z = [X, Y] =>> Map[Y, X]
class A[
[B <: C] =>> D
]
--------------------------------------------------------------------------------
(compilation_unit
(type_definition
(type_identifier)
(type_lambda
(identifier)
(upper_bound
(type_identifier))
(type_identifier)))
(type_definition
(type_identifier)
(type_lambda
(identifier)
(identifier)
(generic_type
(type_identifier)
(type_arguments
(type_identifier)
(type_identifier)))))
(class_definition
(identifier)
(type_parameters
(type_lambda
(identifier)
(upper_bound
(type_identifier))
(type_identifier)))))

@ -26,19 +26,19 @@ object Hello {
trait Test {
// ^ keyword
// ^ type
def meth(i: Int)(implicit x: Boolean) = ???
def meth(i: Int)(implicit x: Boolean) = ???
// ^keyword.function
// ^keyword
// ^type
// ^method
// ^parameter
// ^parameter
val anonFun: Int => Int = (a: Int) => a
// ^variable
// ^type
// ^operator
// ^type
// ^parameter
val anonFun: Int => Int = (a: Int) => a
// ^variable
// ^type
// ^operator
// ^type
// ^parameter
}
protected abstract class Bla(test: String)
@ -79,5 +79,16 @@ object Hello {
// ^method
val hello2 = c"some $meth"
// ^method
val hello3 = s"$$$meth$hello2%"
// ^string
// ^punctuation.special
// ^method
// ^punctuation.special
// ^variable
// ^string
val hello4 = s"$"$hello3"
// ^string
// ^punctuation.special
// ^variable
}

@ -36,8 +36,8 @@ object O2:
// SIP-44
class C:
// ^keyword
// ^type
fooooo.map: x =>
// ^type
// ^method.call
x + 1
@ -117,7 +117,7 @@ object A:
// ^type
::(123)
//^function.call
//^operator
// ^number
object bla:

@ -1,50 +0,0 @@
#define DEBUG
#include "../src/stack.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
ScannerStack *stack = createStack();
printStack(stack, "hello");
assert(isEmptyStack(stack));
pushStack(stack, 27);
assert(!isEmptyStack(stack));
assert(peekStack(stack) == 27);
pushStack(stack, 42);
assert(!isEmptyStack(stack));
assert(peekStack(stack) == 42);
assert(popStack(stack) == 42);
assert(peekStack(stack) == 27);
assert(popStack(stack) == 27);
assert(peekStack(stack) == -1);
assert(isEmptyStack(stack));
char *buf = malloc(1024);
for (int i = 0; i < 100; i++) {
pushStack(stack, i);
}
assert(serialiseStack(stack, buf) == sizeof(int) * 103);
ScannerStack *newStack = createStack();
deserialiseStack(newStack, buf, sizeof(int) * 103);
assert(newStack -> top == 100);
assert(popStack(newStack) == 99);
resetStack(newStack);
assert(isEmptyStack(newStack));
printStack(stack, "hello");
printStack(newStack, "hello");
return 0;
}