From 08a922826d96478012f2bfa65a9146241a6b1426 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:17:37 -0400 Subject: [PATCH] Output from yarn generate --- .editorconfig | 39 +++ .gitattributes | 11 + Makefile | 109 ++++++ Package.swift | 48 +++ binding.gyp | 12 +- bindings/c/tree-sitter-glimmer.h | 16 + bindings/c/tree-sitter-glimmer.pc.in | 11 + bindings/go/binding.go | 13 + bindings/go/binding_test.go | 15 + bindings/go/go.mod | 5 + bindings/node/binding.cc | 36 +- bindings/node/index.d.ts | 28 ++ bindings/node/index.js | 18 +- .../python/tree_sitter_glimmer/__init__.py | 5 + .../python/tree_sitter_glimmer/__init__.pyi | 1 + bindings/python/tree_sitter_glimmer/binding.c | 27 ++ bindings/python/tree_sitter_glimmer/py.typed | 0 bindings/swift/TreeSitterGlimmer/glimmer.h | 16 + package.json | 31 +- pyproject.toml | 29 ++ setup.py | 57 +++ src/grammar.json | 1 - src/parser.c | 330 ++++++++++-------- src/tree_sitter/alloc.h | 54 +++ src/tree_sitter/array.h | 287 +++++++++++++++ src/tree_sitter/parser.h | 16 +- 26 files changed, 1026 insertions(+), 189 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 Makefile create mode 100644 Package.swift create mode 100644 bindings/c/tree-sitter-glimmer.h create mode 100644 bindings/c/tree-sitter-glimmer.pc.in create mode 100644 bindings/go/binding.go create mode 100644 bindings/go/binding_test.go create mode 100644 bindings/go/go.mod create mode 100644 bindings/node/index.d.ts create mode 100644 bindings/python/tree_sitter_glimmer/__init__.py create mode 100644 bindings/python/tree_sitter_glimmer/__init__.pyi create mode 100644 bindings/python/tree_sitter_glimmer/binding.c create mode 100644 bindings/python/tree_sitter_glimmer/py.typed create mode 100644 bindings/swift/TreeSitterGlimmer/glimmer.h create mode 100644 pyproject.toml create mode 100644 setup.py create mode 100644 src/tree_sitter/alloc.h create mode 100644 src/tree_sitter/array.h diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d3a8b5b --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..ffb52ab --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +* text eol=lf + +src/*.json linguist-generated +src/parser.c linguist-generated +src/tree_sitter/* linguist-generated + +bindings/** linguist-generated +binding.gyp linguist-generated +setup.py linguist-generated +Makefile linguist-generated +Package.swift linguist-generated diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1ba2ad6 --- /dev/null +++ b/Makefile @@ -0,0 +1,109 @@ +VERSION := 0.0.1 + +LANGUAGE_NAME := tree-sitter-glimmer + +# 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 + +# object files +OBJS := $(patsubst %.c,%.o,$(wildcard $(SRC_DIR)/*.c)) + +# 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)|' $< > $@ + +$(SRC_DIR)/parser.c: grammar.js + $(TS) generate --no-bindings + +install: all + install -Dm644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h + install -Dm644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc + install -Dm755 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 diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..41c668a --- /dev/null +++ b/Package.swift @@ -0,0 +1,48 @@ +// swift-tools-version:5.3 +import PackageDescription + +let package = Package( + name: "TreeSitterGlimmer", + platforms: [.macOS(.v10_13), .iOS(.v11)], + products: [ + .library(name: "TreeSitterGlimmer", targets: ["TreeSitterGlimmer"]), + ], + dependencies: [], + targets: [ + .target(name: "TreeSitterGlimmer", + path: ".", + exclude: [ + "Cargo.toml", + "Makefile", + "binding.gyp", + "bindings/c", + "bindings/go", + "bindings/node", + "bindings/python", + "bindings/rust", + "prebuilds", + "grammar.js", + "package.json", + "package-lock.json", + "pyproject.toml", + "setup.py", + "test", + "examples", + ".editorconfig", + ".github", + ".gitignore", + ".gitattributes", + ".gitmodules", + ], + sources: [ + "src/parser.c", + // NOTE: if your language has an external scanner, add it here. + ], + resources: [ + .copy("queries") + ], + publicHeadersPath: "bindings/swift", + cSettings: [.headerSearchPath("src")]) + ], + cLanguageStandard: .c11 +) diff --git a/binding.gyp b/binding.gyp index 4bb80de..3f4f7e0 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,18 +2,20 @@ "targets": [ { "target_name": "tree_sitter_glimmer_binding", + "dependencies": [ + " -#include "nan.h" +#include -using namespace v8; +typedef struct TSLanguage TSLanguage; -extern "C" TSLanguage * tree_sitter_glimmer(); +extern "C" TSLanguage *tree_sitter_glimmer(); -namespace { +// "tree-sitter", "language" hashed with BLAKE2 +const napi_type_tag LANGUAGE_TYPE_TAG = { + 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16 +}; -NAN_METHOD(New) {} - -void Init(Local exports, Local module) { - Local tpl = Nan::New(New); - tpl->SetClassName(Nan::New("Language").ToLocalChecked()); - tpl->InstanceTemplate()->SetInternalFieldCount(1); - - Local constructor = Nan::GetFunction(tpl).ToLocalChecked(); - Local instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked(); - Nan::SetInternalFieldPointer(instance, 0, tree_sitter_glimmer()); - - Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("glimmer").ToLocalChecked()); - Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance); +Napi::Object Init(Napi::Env env, Napi::Object exports) { + exports["name"] = Napi::String::New(env, "glimmer"); + auto language = Napi::External::New(env, tree_sitter_glimmer()); + language.TypeTag(&LANGUAGE_TYPE_TAG); + exports["language"] = language; + return exports; } -NODE_MODULE(tree_sitter_glimmer_binding, Init) - -} // namespace +NODE_API_MODULE(tree_sitter_glimmer_binding, Init) diff --git a/bindings/node/index.d.ts b/bindings/node/index.d.ts new file mode 100644 index 0000000..efe259e --- /dev/null +++ b/bindings/node/index.d.ts @@ -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; diff --git a/bindings/node/index.js b/bindings/node/index.js index 6e57439..6657bcf 100644 --- a/bindings/node/index.js +++ b/bindings/node/index.js @@ -1,18 +1,6 @@ -try { - module.exports = require("../../build/Release/tree_sitter_glimmer_binding"); -} catch (error1) { - if (error1.code !== "MODULE_NOT_FOUND") { - throw error1; - } - try { - module.exports = require("../../build/Debug/tree_sitter_glimmer_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"); diff --git a/bindings/python/tree_sitter_glimmer/__init__.py b/bindings/python/tree_sitter_glimmer/__init__.py new file mode 100644 index 0000000..d2822b6 --- /dev/null +++ b/bindings/python/tree_sitter_glimmer/__init__.py @@ -0,0 +1,5 @@ +"Glimmer grammar for tree-sitter" + +from ._binding import language + +__all__ = ["language"] diff --git a/bindings/python/tree_sitter_glimmer/__init__.pyi b/bindings/python/tree_sitter_glimmer/__init__.pyi new file mode 100644 index 0000000..5416666 --- /dev/null +++ b/bindings/python/tree_sitter_glimmer/__init__.pyi @@ -0,0 +1 @@ +def language() -> int: ... diff --git a/bindings/python/tree_sitter_glimmer/binding.c b/bindings/python/tree_sitter_glimmer/binding.c new file mode 100644 index 0000000..fbee4c5 --- /dev/null +++ b/bindings/python/tree_sitter_glimmer/binding.c @@ -0,0 +1,27 @@ +#include + +typedef struct TSLanguage TSLanguage; + +TSLanguage *tree_sitter_glimmer(void); + +static PyObject* _binding_language(PyObject *self, PyObject *args) { + return PyLong_FromVoidPtr(tree_sitter_glimmer()); +} + +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); +} diff --git a/bindings/python/tree_sitter_glimmer/py.typed b/bindings/python/tree_sitter_glimmer/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/bindings/swift/TreeSitterGlimmer/glimmer.h b/bindings/swift/TreeSitterGlimmer/glimmer.h new file mode 100644 index 0000000..30fa862 --- /dev/null +++ b/bindings/swift/TreeSitterGlimmer/glimmer.h @@ -0,0 +1,16 @@ +#ifndef TREE_SITTER_GLIMMER_H_ +#define TREE_SITTER_GLIMMER_H_ + +typedef struct TSLanguage TSLanguage; + +#ifdef __cplusplus +extern "C" { +#endif + +const TSLanguage *tree_sitter_glimmer(void); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_GLIMMER_H_ diff --git a/package.json b/package.json index 3fbffe7..4fff0bd 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,35 @@ { "name": "tree-sitter-glimmer", - "version": "1.3.0", + "version": "1.4.0", "main": "bindings/node", + "types": "bindings/node", "license": "MIT", "scripts": { "generate": "tree-sitter generate", "parse": "tree-sitter parse", "test": "tree-sitter test", - "prepare": "husky install" + "prepare": "husky install", + "install": "node-gyp-build", + "prebuildify": "prebuildify --napi --strip" }, "dependencies": { - "nan": "^2.19.0" + "node-addon-api": "^7.1.0", + "node-gyp-build": "^4.8.0" + }, + "peerDependencies": { + "tree-sitter": "^0.21.0" + }, + "peerDependenciesMeta": { + "tree_sitter": { + "optional": true + } }, "devDependencies": { "husky": "^7.0.4", "lint-staged": "^15.2.2", "prettier": "^2.8.8", - "tree-sitter-cli": "^0.22.1" + "tree-sitter-cli": "^0.22.1", + "prebuildify": "^6.0.0" }, "tree-sitter": [ { @@ -34,5 +47,13 @@ "lint-staged": { "*.js": "prettier --write" }, - "packageManager": "yarn@3.6.4" + "packageManager": "yarn@3.6.4", + "files": [ + "grammar.js", + "binding.gyp", + "prebuilds/**", + "bindings/node/*", + "queries/*", + "src/**" + ] } diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..936ef5b --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,29 @@ +[build-system] +requires = ["setuptools>=42", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "tree-sitter-glimmer" +description = "Glimmer grammar for tree-sitter" +version = "0.0.1" +keywords = ["incremental", "parsing", "tree-sitter", "glimmer"] +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-glimmer" + +[project.optional-dependencies] +core = ["tree-sitter~=0.21"] + +[tool.cibuildwheel] +build = "cp38-*" +build-frontend = "build" diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..399f656 --- /dev/null +++ b/setup.py @@ -0,0 +1,57 @@ +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_glimmer", "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_glimmer": ["*.pyi", "py.typed"], + "tree_sitter_glimmer.queries": ["*.scm"], + }, + ext_package="tree_sitter_glimmer", + ext_modules=[ + Extension( + name="_binding", + sources=[ + "bindings/python/tree_sitter_glimmer/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 [] + ), + 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 +) diff --git a/src/grammar.json b/src/grammar.json index f96d7f1..bea5f9b 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -840,4 +840,3 @@ "inline": [], "supertypes": [] } - diff --git a/src/parser.c b/src/parser.c index 57a8c0d..35e89c1 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,7 +1,6 @@ -#include +#include "tree_sitter/parser.h" #if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif @@ -16,7 +15,7 @@ #define MAX_ALIAS_SEQUENCE_LENGTH 5 #define PRODUCTION_ID_COUNT 9 -enum { +enum ts_symbol_identifiers { sym_text_node = 1, anon_sym_SQUOTE = 2, aux_sym__single_quote_string_literal_token1 = 3, @@ -496,7 +495,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, }; -enum { +enum ts_field_identifiers { field_argument = 1, field_helper = 2, field_key = 3, @@ -812,48 +811,6 @@ static const TSStateId ts_primary_state_ids[STATE_COUNT] = { [251] = 251, }; -static inline bool sym_attribute_name_character_set_1(int32_t c) { - return (c < '\'' - ? (c < '\r' - ? (c < '\t' - ? c == 0 - : c <= '\n') - : (c <= '\r' || (c >= ' ' && c <= '"'))) - : (c <= ')' || (c < '<' - ? (c < '.' - ? c == ',' - : c <= '/') - : (c <= '?' || (c >= '{' && c <= '}'))))); -} - -static inline bool sym_identifier_character_set_1(int32_t c) { - return (c < '\'' - ? (c < '\r' - ? (c < '\t' - ? c == 0 - : c <= '\n') - : (c <= '\r' || (c >= ' ' && c <= '"'))) - : (c <= ')' || (c < '<' - ? (c < '.' - ? c == ',' - : c <= '/') - : (c <= '>' || (c >= '{' && c <= '}'))))); -} - -static inline bool sym_identifier_character_set_2(int32_t c) { - return (c < '\'' - ? (c < '\r' - ? (c < '\t' - ? c == 0 - : c <= '\n') - : (c <= '\r' || (c >= ' ' && c <= '"'))) - : (c <= ')' || (c < '<' - ? (c < '.' - ? c == ',' - : c <= '/') - : (c <= '>' || (c >= '{' && c <= '|'))))); -} - static bool ts_lex(TSLexer *lexer, TSStateId state) { START_LEXER(); eof = lexer->eof(lexer); @@ -876,9 +833,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '|') ADVANCE(62); if (lookahead == '}') ADVANCE(28); if (lookahead == '~') ADVANCE(71); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(0) if (('0' <= lookahead && lookahead <= '9')) ADVANCE(42); if (lookahead != 0 && @@ -896,9 +851,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == 't') ADVANCE(67); if (lookahead == '}') ADVANCE(28); if (lookahead == '~') ADVANCE(71); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(1) if (('0' <= lookahead && lookahead <= '9')) ADVANCE(42); if (lookahead != 0 && @@ -918,9 +871,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '=') ADVANCE(55); if (lookahead == 'f') ADVANCE(63); if (lookahead == 't') ADVANCE(67); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(2) if (('0' <= lookahead && lookahead <= '9')) ADVANCE(42); if (lookahead != 0 && @@ -941,9 +892,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == 't') ADVANCE(67); if (lookahead == '}') ADVANCE(28); if (lookahead == '~') ADVANCE(71); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(3) if (('0' <= lookahead && lookahead <= '9')) ADVANCE(42); if (lookahead != 0 && @@ -959,18 +908,14 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '"') ADVANCE(39); if (lookahead == '\'') ADVANCE(36); if (lookahead == '{') ADVANCE(27); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(4) if (('0' <= lookahead && lookahead <= '9')) ADVANCE(43); END_STATE(); case 5: if (lookahead == '"') ADVANCE(39); if (lookahead == '{') ADVANCE(27); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(58); if (lookahead != 0 && lookahead != '\\') ADVANCE(59); @@ -978,9 +923,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { case 6: if (lookahead == '\'') ADVANCE(36); if (lookahead == '{') ADVANCE(27); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(56); if (lookahead != 0 && lookahead != '\\') ADVANCE(57); @@ -989,9 +932,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == ')') ADVANCE(84); if (lookahead == '.') ADVANCE(73); if (lookahead == '|') ADVANCE(62); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(7) if (lookahead != 0 && lookahead != '!' && @@ -1014,9 +955,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == 'a') ADVANCE(68); if (lookahead == '}') ADVANCE(28); if (lookahead == '~') ADVANCE(71); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(10) if (lookahead != 0 && lookahead != '!' && @@ -1032,9 +971,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '.') ADVANCE(73); if (lookahead == '}') ADVANCE(28); if (lookahead == '~') ADVANCE(71); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(11) if (lookahead != 0 && lookahead != '!' && @@ -1053,9 +990,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '>') ADVANCE(49); if (lookahead == 'a') ADVANCE(52); if (lookahead == '{') ADVANCE(27); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(12) if (lookahead != 0 && lookahead != '!' && @@ -1069,9 +1004,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { case 13: if (lookahead == '<') ADVANCE(47); if (lookahead == '{') ADVANCE(25); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(34); if (lookahead != 0 && lookahead != '>' && @@ -1137,18 +1070,14 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { ('A' <= lookahead && lookahead <= 'Z') || ('a' <= lookahead && lookahead <= 'z')) ADVANCE(46); if (lookahead == '@') ADVANCE(30); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') SKIP(31) END_STATE(); case 32: if (eof) ADVANCE(33); if (lookahead == '<') ADVANCE(48); if (lookahead == '{') ADVANCE(26); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(34); if (lookahead != 0 && lookahead != '>' && @@ -1159,9 +1088,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 34: ACCEPT_TOKEN(sym_text_node); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(34); if (lookahead != 0 && lookahead != '<' && @@ -1182,9 +1109,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 37: ACCEPT_TOKEN(aux_sym__single_quote_string_literal_token1); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(37); if (lookahead != 0 && lookahead != '\'' && @@ -1201,9 +1126,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 40: ACCEPT_TOKEN(aux_sym__double_quote_string_literal_token1); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(40); if (lookahead != 0 && lookahead != '"' && @@ -1218,7 +1141,15 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { case 42: ACCEPT_TOKEN(sym_number_literal); if (('0' <= lookahead && lookahead <= '9')) ADVANCE(42); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 43: ACCEPT_TOKEN(sym_number_literal); @@ -1226,11 +1157,27 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 44: ACCEPT_TOKEN(anon_sym_true); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 45: ACCEPT_TOKEN(anon_sym_false); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 46: ACCEPT_TOKEN(sym_tag_name); @@ -1260,11 +1207,27 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { case 52: ACCEPT_TOKEN(sym_attribute_name); if (lookahead == 's') ADVANCE(60); - if (!sym_attribute_name_character_set_1(lookahead)) ADVANCE(53); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '?' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(53); END_STATE(); case 53: ACCEPT_TOKEN(sym_attribute_name); - if (!sym_attribute_name_character_set_1(lookahead)) ADVANCE(53); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '?' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(53); END_STATE(); case 54: ACCEPT_TOKEN(sym__splattributes); @@ -1274,9 +1237,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 56: ACCEPT_TOKEN(sym__mustache_safe_single_quote_string_literal_content); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(56); if (lookahead != 0 && lookahead != '\'' && @@ -1292,9 +1253,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 58: ACCEPT_TOKEN(sym__mustache_safe_double_quote_string_literal_content); - if (lookahead == '\t' || - lookahead == '\n' || - lookahead == '\r' || + if (('\t' <= lookahead && lookahead <= '\r') || lookahead == ' ') ADVANCE(58); if (lookahead != 0 && lookahead != '"' && @@ -1310,11 +1269,27 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { END_STATE(); case 60: ACCEPT_TOKEN(anon_sym_as); - if (!sym_attribute_name_character_set_1(lookahead)) ADVANCE(53); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '?' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(53); END_STATE(); case 61: ACCEPT_TOKEN(anon_sym_as); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 62: ACCEPT_TOKEN(anon_sym_PIPE); @@ -1322,51 +1297,132 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { case 63: ACCEPT_TOKEN(sym_identifier); if (lookahead == 'a') ADVANCE(66); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 64: ACCEPT_TOKEN(sym_identifier); if (lookahead == 'e') ADVANCE(44); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 65: ACCEPT_TOKEN(sym_identifier); if (lookahead == 'e') ADVANCE(45); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 66: ACCEPT_TOKEN(sym_identifier); if (lookahead == 'l') ADVANCE(69); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 67: ACCEPT_TOKEN(sym_identifier); if (lookahead == 'r') ADVANCE(70); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 68: ACCEPT_TOKEN(sym_identifier); if (lookahead == 's') ADVANCE(61); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 69: ACCEPT_TOKEN(sym_identifier); if (lookahead == 's') ADVANCE(65); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 70: ACCEPT_TOKEN(sym_identifier); if (lookahead == 'u') ADVANCE(64); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 71: ACCEPT_TOKEN(sym_identifier); if (lookahead == '}') ADVANCE(29); - if (!sym_identifier_character_set_2(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + lookahead != '{' && + lookahead != '|') ADVANCE(72); END_STATE(); case 72: ACCEPT_TOKEN(sym_identifier); - if (!sym_identifier_character_set_1(lookahead)) ADVANCE(72); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + (lookahead < ' ' || '"' < lookahead) && + (lookahead < '\'' || ')' < lookahead) && + lookahead != ',' && + lookahead != '.' && + lookahead != '/' && + (lookahead < '<' || '>' < lookahead) && + (lookahead < '{' || '}' < lookahead)) ADVANCE(72); END_STATE(); case 73: ACCEPT_TOKEN(anon_sym_DOT); @@ -1686,20 +1742,6 @@ static const TSLexMode ts_lex_modes[STATE_COUNT] = { [251] = {.lex_state = 7}, }; -enum { - ts_external_token_comment = 0, -}; - -static const TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { - [ts_external_token_comment] = sym_comment, -}; - -static const bool ts_external_scanner_states[2][EXTERNAL_TOKEN_COUNT] = { - [1] = { - [ts_external_token_comment] = true, - }, -}; - static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { [0] = { [ts_builtin_sym_end] = ACTIONS(1), @@ -5281,6 +5323,20 @@ static const TSParseActionEntry ts_parse_actions[] = { [686] = {.entry = {.count = 1, .reusable = true}}, SHIFT(12), }; +enum ts_external_scanner_symbol_identifiers { + ts_external_token_comment = 0, +}; + +static const TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { + [ts_external_token_comment] = sym_comment, +}; + +static const bool ts_external_scanner_states[2][EXTERNAL_TOKEN_COUNT] = { + [1] = { + [ts_external_token_comment] = true, + }, +}; + #ifdef __cplusplus extern "C" { #endif @@ -5291,10 +5347,12 @@ unsigned tree_sitter_glimmer_external_scanner_serialize(void *, char *); void tree_sitter_glimmer_external_scanner_deserialize(void *, const char *, unsigned); #ifdef _WIN32 -#define extern __declspec(dllexport) +#define TS_PUBLIC __declspec(dllexport) +#else +#define TS_PUBLIC __attribute__((visibility("default"))) #endif -extern const TSLanguage *tree_sitter_glimmer(void) { +TS_PUBLIC const TSLanguage *tree_sitter_glimmer() { static const TSLanguage language = { .version = LANGUAGE_VERSION, .symbol_count = SYMBOL_COUNT, diff --git a/src/tree_sitter/alloc.h b/src/tree_sitter/alloc.h new file mode 100644 index 0000000..1f4466d --- /dev/null +++ b/src/tree_sitter/alloc.h @@ -0,0 +1,54 @@ +#ifndef TREE_SITTER_ALLOC_H_ +#define TREE_SITTER_ALLOC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +// 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_ diff --git a/src/tree_sitter/array.h b/src/tree_sitter/array.h new file mode 100644 index 0000000..186ba67 --- /dev/null +++ b/src/tree_sitter/array.h @@ -0,0 +1,287 @@ +#ifndef TREE_SITTER_ARRAY_H_ +#define TREE_SITTER_ARRAY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./alloc.h" + +#include +#include +#include +#include +#include + +#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) \ + (_array__grow((Array *)(self), count, array_elem_size(self)), \ + memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)), \ + (self)->size += (count)) + +/// 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_ diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h index 2b14ac1..17b4fde 100644 --- a/src/tree_sitter/parser.h +++ b/src/tree_sitter/parser.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; @@ -130,9 +129,16 @@ struct TSLanguage { * 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; \ @@ -166,7 +172,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 +182,7 @@ struct TSLanguage { {{ \ .shift = { \ .type = TSParseActionTypeShift, \ - .state = state_value \ + .state = (state_value) \ } \ }} @@ -184,7 +190,7 @@ struct TSLanguage { {{ \ .shift = { \ .type = TSParseActionTypeShift, \ - .state = state_value, \ + .state = (state_value), \ .repetition = true \ } \ }}