diff --git a/package/hemar/grammar/tree-sitter/.editorconfig b/package/hemar/grammar/tree-sitter/.editorconfig new file mode 100644 index 0000000..65330c4 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/.editorconfig @@ -0,0 +1,46 @@ +root = true + +[*] +charset = utf-8 + +[*.{json,toml,yml,gyp}] +indent_style = space +indent_size = 2 + +[*.js] +indent_style = space +indent_size = 2 + +[*.scm] +indent_style = space +indent_size = 2 + +[*.{c,cc,h}] +indent_style = space +indent_size = 4 + +[*.rs] +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 + +[parser.c] +indent_size = 2 + +[{alloc,array,parser}.h] +indent_size = 2 diff --git a/package/hemar/grammar/tree-sitter/.gitattributes b/package/hemar/grammar/tree-sitter/.gitattributes new file mode 100644 index 0000000..79475a5 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/.gitattributes @@ -0,0 +1,41 @@ +* text=auto eol=lf + +# Generated source files +src/*.json linguist-generated +src/parser.c linguist-generated +src/tree_sitter/* linguist-generated + +# C bindings +bindings/c/** linguist-generated +CMakeLists.txt linguist-generated +Makefile linguist-generated + +# Rust bindings +bindings/rust/* linguist-generated +Cargo.toml linguist-generated +Cargo.lock linguist-generated + +# Node.js bindings +bindings/node/* linguist-generated +binding.gyp linguist-generated +package.json linguist-generated +package-lock.json linguist-generated + +# Python bindings +bindings/python/** linguist-generated +setup.py linguist-generated +pyproject.toml linguist-generated + +# Go bindings +bindings/go/* linguist-generated +go.mod linguist-generated +go.sum linguist-generated + +# Swift bindings +bindings/swift/** linguist-generated +Package.swift linguist-generated +Package.resolved linguist-generated + +# Zig bindings +build.zig linguist-generated +build.zig.zon linguist-generated diff --git a/package/hemar/grammar/tree-sitter/.gitignore b/package/hemar/grammar/tree-sitter/.gitignore new file mode 100644 index 0000000..bc9e191 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/.gitignore @@ -0,0 +1,47 @@ +# Rust artifacts +target/ + +# Node artifacts +build/ +prebuilds/ +node_modules/ + +# Swift artifacts +.build/ + +# Go artifacts +_obj/ + +# Python artifacts +.venv/ +dist/ +*.egg-info +*.whl + +# C artifacts +*.a +*.so +*.so.* +*.dylib +*.dll +*.pc +*.exp +*.lib + +# Zig artifacts +.zig-cache/ +zig-cache/ +zig-out/ + +# Example dirs +/examples/*/ + +# Grammar volatiles +*.wasm +*.obj +*.o + +# Archives +*.tar.gz +*.tgz +*.zip diff --git a/package/hemar/grammar/tree-sitter/CMakeLists.txt b/package/hemar/grammar/tree-sitter/CMakeLists.txt new file mode 100644 index 0000000..345e605 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.13) + +project(tree-sitter-hemar + VERSION "0.1.0" + DESCRIPTION "templater" + HOMEPAGE_URL "https://github.com/hectic-lab/util.nix" + LANGUAGES C) + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) +option(TREE_SITTER_REUSE_ALLOCATOR "Reuse the library allocator" OFF) + +set(TREE_SITTER_ABI_VERSION 15 CACHE STRING "Tree-sitter ABI version") +if(NOT ${TREE_SITTER_ABI_VERSION} MATCHES "^[0-9]+$") + unset(TREE_SITTER_ABI_VERSION CACHE) + message(FATAL_ERROR "TREE_SITTER_ABI_VERSION must be an integer") +endif() + +find_program(TREE_SITTER_CLI tree-sitter DOC "Tree-sitter CLI") + +add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json" + COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json + --abi=${TREE_SITTER_ABI_VERSION} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "Generating parser.c") + +add_library(tree-sitter-hemar src/parser.c) +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scanner.c) + target_sources(tree-sitter-hemar PRIVATE src/scanner.c) +endif() +target_include_directories(tree-sitter-hemar + PRIVATE src + INTERFACE $ + $) + +target_compile_definitions(tree-sitter-hemar PRIVATE + $<$:TREE_SITTER_REUSE_ALLOCATOR> + $<$:TREE_SITTER_DEBUG>) + +set_target_properties(tree-sitter-hemar + PROPERTIES + C_STANDARD 11 + POSITION_INDEPENDENT_CODE ON + SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}" + DEFINE_SYMBOL "") + +configure_file(bindings/c/tree-sitter-hemar.pc.in + "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-hemar.pc" @ONLY) + +include(GNUInstallDirs) + +install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bindings/c/tree_sitter" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + FILES_MATCHING PATTERN "*.h") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-hemar.pc" + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") +install(TARGETS tree-sitter-hemar + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") + +file(GLOB QUERIES queries/*.scm) +install(FILES ${QUERIES} + DESTINATION "${CMAKE_INSTALL_DATADIR}/tree-sitter/queries/hemar") + +add_custom_target(ts-test "${TREE_SITTER_CLI}" test + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "tree-sitter test") diff --git a/package/hemar/grammar/tree-sitter/Cargo.toml b/package/hemar/grammar/tree-sitter/Cargo.toml new file mode 100644 index 0000000..52098eb --- /dev/null +++ b/package/hemar/grammar/tree-sitter/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "tree-sitter-hemar" +description = "templater" +version = "0.1.0" +authors = ["yukkop "] +license = "MIT" +readme = "README.md" +keywords = ["incremental", "parsing", "tree-sitter", "hemar"] +categories = ["parser-implementations", "parsing", "text-editors"] +repository = "https://github.com/hectic-lab/util.nix" +edition = "2021" +autoexamples = false + +build = "bindings/rust/build.rs" +include = [ + "bindings/rust/*", + "grammar.js", + "queries/*", + "src/*", + "tree-sitter.json", + "LICENSE", +] + +[lib] +path = "bindings/rust/lib.rs" + +[dependencies] +tree-sitter-language = "0.1" + +[build-dependencies] +cc = "1.2" + +[dev-dependencies] +tree-sitter = "0.25.3" diff --git a/package/hemar/grammar/tree-sitter/Makefile b/package/hemar/grammar/tree-sitter/Makefile new file mode 100644 index 0000000..3fcc528 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/Makefile @@ -0,0 +1,97 @@ +ifeq ($(OS),Windows_NT) +$(error Windows is not supported) +endif + +LANGUAGE_NAME := tree-sitter-hemar +HOMEPAGE_URL := https://github.com/hectic-lab/util.nix +VERSION := 0.1.0 + +# repository +SRC_DIR := src + +TS ?= tree-sitter + +# install directory layout +PREFIX ?= /usr/local +DATADIR ?= $(PREFIX)/share +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 + +# ABI versioning +SONAME_MAJOR = $(shell sed -n 's/\#define LANGUAGE_VERSION //p' $(PARSER)) +SONAME_MINOR = $(word 1,$(subst ., ,$(VERSION))) + +# OS-specific bits +ifeq ($(shell uname),Darwin) + SOEXT = dylib + SOEXTVER_MAJOR = $(SONAME_MAJOR).$(SOEXT) + SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).$(SOEXT) + LINKSHARED = -dynamiclib -Wl,-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks +else + SOEXT = so + SOEXTVER_MAJOR = $(SOEXT).$(SONAME_MAJOR) + SOEXTVER = $(SOEXT).$(SONAME_MAJOR).$(SONAME_MINOR) + LINKSHARED = -shared -Wl,-soname,lib$(LANGUAGE_NAME).$(SOEXTVER) +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|@PROJECT_VERSION@|$(VERSION)|' \ + -e 's|@CMAKE_INSTALL_LIBDIR@|$(LIBDIR:$(PREFIX)/%=%)|' \ + -e 's|@CMAKE_INSTALL_INCLUDEDIR@|$(INCLUDEDIR:$(PREFIX)/%=%)|' \ + -e 's|@PROJECT_DESCRIPTION@|$(DESCRIPTION)|' \ + -e 's|@PROJECT_HOMEPAGE_URL@|$(HOMEPAGE_URL)|' \ + -e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|' $< > $@ + +$(PARSER): $(SRC_DIR)/grammar.json + $(TS) generate $^ + +install: all + install -d '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/hemar '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)' + install -m644 bindings/c/tree_sitter/$(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) + install -m644 queries/*.scm '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/hemar + +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 + $(RM) -r '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/hemar + +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/hemar/grammar/tree-sitter/Package.swift b/package/hemar/grammar/tree-sitter/Package.swift new file mode 100644 index 0000000..deb92d3 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/Package.swift @@ -0,0 +1,41 @@ +// swift-tools-version:5.3 + +import Foundation +import PackageDescription + +var sources = ["src/parser.c"] +if FileManager.default.fileExists(atPath: "src/scanner.c") { + sources.append("src/scanner.c") +} + +let package = Package( + name: "TreeSitterHemar", + products: [ + .library(name: "TreeSitterHemar", targets: ["TreeSitterHemar"]), + ], + dependencies: [ + .package(url: "https://github.com/tree-sitter/swift-tree-sitter", from: "0.8.0"), + ], + targets: [ + .target( + name: "TreeSitterHemar", + dependencies: [], + path: ".", + sources: sources, + resources: [ + .copy("queries") + ], + publicHeadersPath: "bindings/swift", + cSettings: [.headerSearchPath("src")] + ), + .testTarget( + name: "TreeSitterHemarTests", + dependencies: [ + "SwiftTreeSitter", + "TreeSitterHemar", + ], + path: "bindings/swift/TreeSitterHemarTests" + ) + ], + cLanguageStandard: .c11 +) diff --git a/package/hemar/grammar/tree-sitter/binding.gyp b/package/hemar/grammar/tree-sitter/binding.gyp new file mode 100644 index 0000000..82be650 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/binding.gyp @@ -0,0 +1,35 @@ +{ + "targets": [ + { + "target_name": "tree_sitter_hemar_binding", + "dependencies": [ + " + +typedef struct TSLanguage TSLanguage; + +extern "C" TSLanguage *tree_sitter_hemar(); + +// "tree-sitter", "language" hashed with BLAKE2 +const napi_type_tag LANGUAGE_TYPE_TAG = { + 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16 +}; + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + auto language = Napi::External::New(env, tree_sitter_hemar()); + language.TypeTag(&LANGUAGE_TYPE_TAG); + exports["language"] = language; + return exports; +} + +NODE_API_MODULE(tree_sitter_hemar_binding, Init) diff --git a/package/hemar/grammar/tree-sitter/bindings/node/binding_test.js b/package/hemar/grammar/tree-sitter/bindings/node/binding_test.js new file mode 100644 index 0000000..55becac --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/node/binding_test.js @@ -0,0 +1,9 @@ +const assert = require("node:assert"); +const { test } = require("node:test"); + +const Parser = require("tree-sitter"); + +test("can load grammar", () => { + const parser = new Parser(); + assert.doesNotThrow(() => parser.setLanguage(require("."))); +}); diff --git a/package/hemar/grammar/tree-sitter/bindings/node/index.d.ts b/package/hemar/grammar/tree-sitter/bindings/node/index.d.ts new file mode 100644 index 0000000..528e060 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/node/index.d.ts @@ -0,0 +1,27 @@ +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 = { + language: unknown; + nodeTypeInfo: NodeInfo[]; +}; + +declare const language: Language; +export = language; diff --git a/package/hemar/grammar/tree-sitter/bindings/node/index.js b/package/hemar/grammar/tree-sitter/bindings/node/index.js new file mode 100644 index 0000000..e099aed --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/node/index.js @@ -0,0 +1,11 @@ +const root = require("path").join(__dirname, "..", ".."); + +module.exports = + typeof process.versions.bun === "string" + // Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time + ? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-hemar.node`) + : require("node-gyp-build")(root); + +try { + module.exports.nodeTypeInfo = require("../../src/node-types.json"); +} catch (_) {} diff --git a/package/hemar/grammar/tree-sitter/bindings/python/tests/test_binding.py b/package/hemar/grammar/tree-sitter/bindings/python/tests/test_binding.py new file mode 100644 index 0000000..c92f81b --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/python/tests/test_binding.py @@ -0,0 +1,12 @@ +from unittest import TestCase + +import tree_sitter +import tree_sitter_hemar + + +class TestLanguage(TestCase): + def test_can_load_grammar(self): + try: + tree_sitter.Language(tree_sitter_hemar.language()) + except Exception: + self.fail("Error loading Hemar grammar") diff --git a/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/__init__.py b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/__init__.py new file mode 100644 index 0000000..31dbaaa --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/__init__.py @@ -0,0 +1,42 @@ +"""templater""" + +from importlib.resources import files as _files + +from ._binding import language + + +def _get_query(name, file): + query = _files(f"{__package__}.queries") / file + globals()[name] = query.read_text() + return globals()[name] + + +def __getattr__(name): + # NOTE: uncomment these to include any queries that this grammar contains: + + # if name == "HIGHLIGHTS_QUERY": + # return _get_query("HIGHLIGHTS_QUERY", "highlights.scm") + # if name == "INJECTIONS_QUERY": + # return _get_query("INJECTIONS_QUERY", "injections.scm") + # if name == "LOCALS_QUERY": + # return _get_query("LOCALS_QUERY", "locals.scm") + # if name == "TAGS_QUERY": + # return _get_query("TAGS_QUERY", "tags.scm") + + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + + +__all__ = [ + "language", + # "HIGHLIGHTS_QUERY", + # "INJECTIONS_QUERY", + # "LOCALS_QUERY", + # "TAGS_QUERY", +] + + +def __dir__(): + return sorted(__all__ + [ + "__all__", "__builtins__", "__cached__", "__doc__", "__file__", + "__loader__", "__name__", "__package__", "__path__", "__spec__", + ]) diff --git a/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/__init__.pyi b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/__init__.pyi new file mode 100644 index 0000000..abf6633 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/__init__.pyi @@ -0,0 +1,10 @@ +from typing import Final + +# NOTE: uncomment these to include any queries that this grammar contains: + +# HIGHLIGHTS_QUERY: Final[str] +# INJECTIONS_QUERY: Final[str] +# LOCALS_QUERY: Final[str] +# TAGS_QUERY: Final[str] + +def language() -> object: ... diff --git a/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/binding.c b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/binding.c new file mode 100644 index 0000000..87ba1ed --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/binding.c @@ -0,0 +1,35 @@ +#include + +typedef struct TSLanguage TSLanguage; + +TSLanguage *tree_sitter_hemar(void); + +static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) { + return PyCapsule_New(tree_sitter_hemar(), "tree_sitter.Language", NULL); +} + +static struct PyModuleDef_Slot slots[] = { +#ifdef Py_GIL_DISABLED + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, +#endif + {0, NULL} +}; + +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 = 0, + .m_methods = methods, + .m_slots = slots, +}; + +PyMODINIT_FUNC PyInit__binding(void) { + return PyModuleDef_Init(&module); +} diff --git a/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/py.typed b/package/hemar/grammar/tree-sitter/bindings/python/tree_sitter_hemar/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/package/hemar/grammar/tree-sitter/bindings/rust/build.rs b/package/hemar/grammar/tree-sitter/bindings/rust/build.rs new file mode 100644 index 0000000..c696890 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/rust/build.rs @@ -0,0 +1,21 @@ +fn main() { + let src_dir = std::path::Path::new("src"); + + let mut c_config = cc::Build::new(); + c_config.std("c11").include(src_dir); + + #[cfg(target_env = "msvc")] + c_config.flag("-utf-8"); + + let parser_path = src_dir.join("parser.c"); + c_config.file(&parser_path); + println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); + + let scanner_path = src_dir.join("scanner.c"); + if scanner_path.exists() { + c_config.file(&scanner_path); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + } + + c_config.compile("tree-sitter-hemar"); +} diff --git a/package/hemar/grammar/tree-sitter/bindings/rust/lib.rs b/package/hemar/grammar/tree-sitter/bindings/rust/lib.rs new file mode 100644 index 0000000..41be700 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/rust/lib.rs @@ -0,0 +1,53 @@ +//! This crate provides Hemar language support for the [tree-sitter][] parsing library. +//! +//! Typically, you will use the [LANGUAGE][] constant to add this language to a +//! tree-sitter [Parser][], and then use the parser to parse some code: +//! +//! ``` +//! let code = r#" +//! "#; +//! let mut parser = tree_sitter::Parser::new(); +//! let language = tree_sitter_hemar::LANGUAGE; +//! parser +//! .set_language(&language.into()) +//! .expect("Error loading Hemar parser"); +//! let tree = parser.parse(code, None).unwrap(); +//! assert!(!tree.root_node().has_error()); +//! ``` +//! +//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html +//! [tree-sitter]: https://tree-sitter.github.io/ + +use tree_sitter_language::LanguageFn; + +extern "C" { + fn tree_sitter_hemar() -> *const (); +} + +/// The tree-sitter [`LanguageFn`][LanguageFn] for this grammar. +/// +/// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html +pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_hemar) }; + +/// The content of the [`node-types.json`][] file for this grammar. +/// +/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types +pub const NODE_TYPES: &str = include_str!("../../src/node-types.json"); + +// NOTE: uncomment these to include any queries that this grammar contains: + +// pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm"); +// pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm"); +// pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm"); +// pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm"); + +#[cfg(test)] +mod tests { + #[test] + fn test_can_load_grammar() { + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(&super::LANGUAGE.into()) + .expect("Error loading Hemar parser"); + } +} diff --git a/package/hemar/grammar/tree-sitter/bindings/swift/TreeSitterHemar/hemar.h b/package/hemar/grammar/tree-sitter/bindings/swift/TreeSitterHemar/hemar.h new file mode 100644 index 0000000..f53103e --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/swift/TreeSitterHemar/hemar.h @@ -0,0 +1,16 @@ +#ifndef TREE_SITTER_HEMAR_H_ +#define TREE_SITTER_HEMAR_H_ + +typedef struct TSLanguage TSLanguage; + +#ifdef __cplusplus +extern "C" { +#endif + +const TSLanguage *tree_sitter_hemar(void); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_HEMAR_H_ diff --git a/package/hemar/grammar/tree-sitter/bindings/swift/TreeSitterHemarTests/TreeSitterHemarTests.swift b/package/hemar/grammar/tree-sitter/bindings/swift/TreeSitterHemarTests/TreeSitterHemarTests.swift new file mode 100644 index 0000000..216e80e --- /dev/null +++ b/package/hemar/grammar/tree-sitter/bindings/swift/TreeSitterHemarTests/TreeSitterHemarTests.swift @@ -0,0 +1,12 @@ +import XCTest +import SwiftTreeSitter +import TreeSitterHemar + +final class TreeSitterHemarTests: XCTestCase { + func testCanLoadGrammar() throws { + let parser = Parser() + let language = Language(language: tree_sitter_hemar()) + XCTAssertNoThrow(try parser.setLanguage(language), + "Error loading Hemar grammar") + } +} diff --git a/package/hemar/grammar/tree-sitter/go.mod b/package/hemar/grammar/tree-sitter/go.mod new file mode 100644 index 0000000..8f89cb2 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/go.mod @@ -0,0 +1,5 @@ +module github.com/hectic-lab/util.nix + +go 1.22 + +require github.com/tree-sitter/go-tree-sitter v0.24.0 diff --git a/package/hemar/grammar/tree-sitter/grammar.js b/package/hemar/grammar/tree-sitter/grammar.js new file mode 100644 index 0000000..378d6c1 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/grammar.js @@ -0,0 +1,41 @@ +module.exports = grammar({ + name: "mytempl", + + rules: { + source_file: $ => repeat($.element), + + element: $ => choice($.interpolation, $.segment, $.text), + + interpolation: $ => seq("{[", $.path, "]}"), + + segment: $ => seq($.for, repeat($.element), $.done), + + + for: $ => seq("{[", "for", $.string, "in", $.path, "]}"), + done: $ => seq("{[", "done", "]}"), + //include: $ => seq("include", $.path), + //call: $ => seq("call", $.string, "in", $.language), + //call_end: $ => seq("end", $.string), + //standalone_call: $ => seq("call", $.string, "end"), + //language: $ => choice("dash", "plpgsql"), + + path: $ => choice( + ".", + seq( + $.string, + repeat(seq(".", $.string)), + ), + ), + + // anything but space + string: $ => choice( + // no whitespace, ], \, ., " + token(prec(-1, /[^] .\\"]+/)), + // " ... " with "" = escaped " + token(prec(-1, /"([^"]|"")*"/)), + ), + + // anything but {[ + text: $ => token(prec(-1, /(?:\{[^\[]|[^{])+/)), + } +}); diff --git a/package/hemar/grammar/tree-sitter/package.json b/package/hemar/grammar/tree-sitter/package.json new file mode 100644 index 0000000..c092ce6 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/package.json @@ -0,0 +1,52 @@ +{ + "name": "tree-sitter-hemar", + "version": "0.1.0", + "description": "templater", + "repository": "https://github.com/hectic-lab/util.nix", + "license": "MIT", + "author": { + "name": "yukkop", + "email": "hectic.yukkop@gmail.com", + "url": "https://yukkop.dev/" + }, + "main": "bindings/node", + "types": "bindings/node", + "keywords": [ + "incremental", + "parsing", + "tree-sitter", + "hemar" + ], + "files": [ + "grammar.js", + "tree-sitter.json", + "binding.gyp", + "prebuilds/**", + "bindings/node/*", + "queries/*", + "src/**", + "*.wasm" + ], + "dependencies": { + "node-addon-api": "^8.2.1", + "node-gyp-build": "^4.8.2" + }, + "devDependencies": { + "prebuildify": "^6.0.1", + "tree-sitter-cli": "^0.25.3" + }, + "peerDependencies": { + "tree-sitter": "^0.21.1" + }, + "peerDependenciesMeta": { + "tree-sitter": { + "optional": true + } + }, + "scripts": { + "install": "node-gyp-build", + "prestart": "tree-sitter build --wasm", + "start": "tree-sitter playground", + "test": "node --test bindings/node/*_test.js" + } +} diff --git a/package/hemar/grammar/tree-sitter/pyproject.toml b/package/hemar/grammar/tree-sitter/pyproject.toml new file mode 100644 index 0000000..ec3ec2c --- /dev/null +++ b/package/hemar/grammar/tree-sitter/pyproject.toml @@ -0,0 +1,29 @@ +[build-system] +requires = ["setuptools>=42", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "tree-sitter-hemar" +description = "templater" +version = "0.1.0" +keywords = ["incremental", "parsing", "tree-sitter", "hemar"] +classifiers = [ + "Intended Audience :: Developers", + "Topic :: Software Development :: Compilers", + "Topic :: Text Processing :: Linguistic", + "Typing :: Typed", +] +authors = [{ name = "yukkop", email = "hectic.yukkop@gmail.com" }] +requires-python = ">=3.10" +license.text = "MIT" +readme = "README.md" + +[project.urls] +Homepage = "https://github.com/hectic-lab/util.nix" + +[project.optional-dependencies] +core = ["tree-sitter~=0.24"] + +[tool.cibuildwheel] +build = "cp310-*" +build-frontend = "build" diff --git a/package/hemar/grammar/tree-sitter/setup.py b/package/hemar/grammar/tree-sitter/setup.py new file mode 100644 index 0000000..649612c --- /dev/null +++ b/package/hemar/grammar/tree-sitter/setup.py @@ -0,0 +1,77 @@ +from os import path +from platform import system +from sysconfig import get_config_var + +from setuptools import Extension, find_packages, setup +from setuptools.command.build import build +from setuptools.command.egg_info import egg_info +from wheel.bdist_wheel import bdist_wheel + +sources = [ + "bindings/python/tree_sitter_hemar/binding.c", + "src/parser.c", +] +if path.exists("src/scanner.c"): + sources.append("src/scanner.c") + +macros: list[tuple[str, str | None]] = [ + ("PY_SSIZE_T_CLEAN", None), + ("TREE_SITTER_HIDE_SYMBOLS", None), +] +if limited_api := not get_config_var("Py_GIL_DISABLED"): + macros.append(("Py_LIMITED_API", "0x030A0000")) + +if system() != "Windows": + cflags = ["-std=c11", "-fvisibility=hidden"] +else: + cflags = ["/std:c11", "/utf-8"] + + +class Build(build): + def run(self): + if path.isdir("queries"): + dest = path.join(self.build_lib, "tree_sitter_hemar", "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 = "cp310", "abi3" + return python, abi, platform + + +class EggInfo(egg_info): + def find_sources(self): + super().find_sources() + self.filelist.recursive_include("queries", "*.scm") + self.filelist.include("src/tree_sitter/*.h") + + +setup( + packages=find_packages("bindings/python"), + package_dir={"": "bindings/python"}, + package_data={ + "tree_sitter_hemar": ["*.pyi", "py.typed"], + "tree_sitter_hemar.queries": ["*.scm"], + }, + ext_package="tree_sitter_hemar", + ext_modules=[ + Extension( + name="_binding", + sources=sources, + extra_compile_args=cflags, + define_macros=macros, + include_dirs=["src"], + py_limited_api=limited_api, + ) + ], + cmdclass={ + "build": Build, + "bdist_wheel": BdistWheel, + "egg_info": EggInfo, + }, + zip_safe=False +) diff --git a/package/hemar/grammar/tree-sitter/src/grammar.json b/package/hemar/grammar/tree-sitter/src/grammar.json new file mode 100644 index 0000000..ec04b24 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/src/grammar.json @@ -0,0 +1,197 @@ +{ + "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json", + "name": "mytempl", + "rules": { + "source_file": { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "element" + } + }, + "element": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "interpolation" + }, + { + "type": "SYMBOL", + "name": "segment" + }, + { + "type": "SYMBOL", + "name": "text" + } + ] + }, + "interpolation": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "{[" + }, + { + "type": "SYMBOL", + "name": "path" + }, + { + "type": "STRING", + "value": "]}" + } + ] + }, + "segment": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "for" + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "element" + } + }, + { + "type": "SYMBOL", + "name": "done" + } + ] + }, + "for": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "{[" + }, + { + "type": "STRING", + "value": "for" + }, + { + "type": "SYMBOL", + "name": "string" + }, + { + "type": "STRING", + "value": "in" + }, + { + "type": "SYMBOL", + "name": "path" + }, + { + "type": "STRING", + "value": "]}" + } + ] + }, + "done": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "{[" + }, + { + "type": "STRING", + "value": "done" + }, + { + "type": "STRING", + "value": "]}" + } + ] + }, + "path": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "." + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "string" + }, + { + "type": "REPEAT", + "content": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "." + }, + { + "type": "SYMBOL", + "name": "string" + } + ] + } + } + ] + } + ] + }, + "string": { + "type": "CHOICE", + "members": [ + { + "type": "TOKEN", + "content": { + "type": "PREC", + "value": -1, + "content": { + "type": "PATTERN", + "value": "[^] .\\\\\"]+" + } + } + }, + { + "type": "TOKEN", + "content": { + "type": "PREC", + "value": -1, + "content": { + "type": "PATTERN", + "value": "\"([^\"]|\"\")*\"" + } + } + } + ] + }, + "text": { + "type": "TOKEN", + "content": { + "type": "PREC", + "value": -1, + "content": { + "type": "PATTERN", + "value": "(?:\\{[^\\[]|[^{])+" + } + } + } + }, + "extras": [ + { + "type": "PATTERN", + "value": "\\s" + } + ], + "conflicts": [], + "precedences": [], + "externals": [], + "inline": [], + "supertypes": [], + "reserved": {} +} \ No newline at end of file diff --git a/package/hemar/grammar/tree-sitter/src/node-types.json b/package/hemar/grammar/tree-sitter/src/node-types.json new file mode 100644 index 0000000..3b85208 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/src/node-types.json @@ -0,0 +1,151 @@ +[ + { + "type": "done", + "named": true, + "fields": {} + }, + { + "type": "element", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "interpolation", + "named": true + }, + { + "type": "segment", + "named": true + }, + { + "type": "text", + "named": true + } + ] + } + }, + { + "type": "for", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "path", + "named": true + }, + { + "type": "string", + "named": true + } + ] + } + }, + { + "type": "interpolation", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "path", + "named": true + } + ] + } + }, + { + "type": "path", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "string", + "named": true + } + ] + } + }, + { + "type": "segment", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "done", + "named": true + }, + { + "type": "element", + "named": true + }, + { + "type": "for", + "named": true + } + ] + } + }, + { + "type": "source_file", + "named": true, + "root": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "element", + "named": true + } + ] + } + }, + { + "type": "string", + "named": true, + "fields": {} + }, + { + "type": ".", + "named": false + }, + { + "type": "]}", + "named": false + }, + { + "type": "done", + "named": false + }, + { + "type": "for", + "named": false + }, + { + "type": "in", + "named": false + }, + { + "type": "text", + "named": true + }, + { + "type": "{[", + "named": false + } +] \ No newline at end of file diff --git a/package/hemar/grammar/tree-sitter/src/parser.c b/package/hemar/grammar/tree-sitter/src/parser.c new file mode 100644 index 0000000..1655a60 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/src/parser.c @@ -0,0 +1,823 @@ +/* Automatically generated by tree-sitter v0.25.3 */ + +#include "tree_sitter/parser.h" + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#define LANGUAGE_VERSION 15 +#define STATE_COUNT 28 +#define LARGE_STATE_COUNT 2 +#define SYMBOL_COUNT 20 +#define ALIAS_COUNT 0 +#define TOKEN_COUNT 10 +#define EXTERNAL_TOKEN_COUNT 0 +#define FIELD_COUNT 0 +#define MAX_ALIAS_SEQUENCE_LENGTH 6 +#define MAX_RESERVED_WORD_SET_SIZE 0 +#define PRODUCTION_ID_COUNT 1 +#define SUPERTYPE_COUNT 0 + +enum ts_symbol_identifiers { + anon_sym_LBRACE_LBRACK = 1, + anon_sym_RBRACK_RBRACE = 2, + anon_sym_for = 3, + anon_sym_in = 4, + anon_sym_done = 5, + anon_sym_DOT = 6, + aux_sym_string_token1 = 7, + aux_sym_string_token2 = 8, + sym_text = 9, + sym_source_file = 10, + sym_element = 11, + sym_interpolation = 12, + sym_segment = 13, + sym_for = 14, + sym_done = 15, + sym_path = 16, + sym_string = 17, + aux_sym_source_file_repeat1 = 18, + aux_sym_path_repeat1 = 19, +}; + +static const char * const ts_symbol_names[] = { + [ts_builtin_sym_end] = "end", + [anon_sym_LBRACE_LBRACK] = "{[", + [anon_sym_RBRACK_RBRACE] = "]}", + [anon_sym_for] = "for", + [anon_sym_in] = "in", + [anon_sym_done] = "done", + [anon_sym_DOT] = ".", + [aux_sym_string_token1] = "string_token1", + [aux_sym_string_token2] = "string_token2", + [sym_text] = "text", + [sym_source_file] = "source_file", + [sym_element] = "element", + [sym_interpolation] = "interpolation", + [sym_segment] = "segment", + [sym_for] = "for", + [sym_done] = "done", + [sym_path] = "path", + [sym_string] = "string", + [aux_sym_source_file_repeat1] = "source_file_repeat1", + [aux_sym_path_repeat1] = "path_repeat1", +}; + +static const TSSymbol ts_symbol_map[] = { + [ts_builtin_sym_end] = ts_builtin_sym_end, + [anon_sym_LBRACE_LBRACK] = anon_sym_LBRACE_LBRACK, + [anon_sym_RBRACK_RBRACE] = anon_sym_RBRACK_RBRACE, + [anon_sym_for] = anon_sym_for, + [anon_sym_in] = anon_sym_in, + [anon_sym_done] = anon_sym_done, + [anon_sym_DOT] = anon_sym_DOT, + [aux_sym_string_token1] = aux_sym_string_token1, + [aux_sym_string_token2] = aux_sym_string_token2, + [sym_text] = sym_text, + [sym_source_file] = sym_source_file, + [sym_element] = sym_element, + [sym_interpolation] = sym_interpolation, + [sym_segment] = sym_segment, + [sym_for] = sym_for, + [sym_done] = sym_done, + [sym_path] = sym_path, + [sym_string] = sym_string, + [aux_sym_source_file_repeat1] = aux_sym_source_file_repeat1, + [aux_sym_path_repeat1] = aux_sym_path_repeat1, +}; + +static const TSSymbolMetadata ts_symbol_metadata[] = { + [ts_builtin_sym_end] = { + .visible = false, + .named = true, + }, + [anon_sym_LBRACE_LBRACK] = { + .visible = true, + .named = false, + }, + [anon_sym_RBRACK_RBRACE] = { + .visible = true, + .named = false, + }, + [anon_sym_for] = { + .visible = true, + .named = false, + }, + [anon_sym_in] = { + .visible = true, + .named = false, + }, + [anon_sym_done] = { + .visible = true, + .named = false, + }, + [anon_sym_DOT] = { + .visible = true, + .named = false, + }, + [aux_sym_string_token1] = { + .visible = false, + .named = false, + }, + [aux_sym_string_token2] = { + .visible = false, + .named = false, + }, + [sym_text] = { + .visible = true, + .named = true, + }, + [sym_source_file] = { + .visible = true, + .named = true, + }, + [sym_element] = { + .visible = true, + .named = true, + }, + [sym_interpolation] = { + .visible = true, + .named = true, + }, + [sym_segment] = { + .visible = true, + .named = true, + }, + [sym_for] = { + .visible = true, + .named = true, + }, + [sym_done] = { + .visible = true, + .named = true, + }, + [sym_path] = { + .visible = true, + .named = true, + }, + [sym_string] = { + .visible = true, + .named = true, + }, + [aux_sym_source_file_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_path_repeat1] = { + .visible = false, + .named = false, + }, +}; + +static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { + [0] = {0}, +}; + +static const uint16_t ts_non_terminal_alias_map[] = { + 0, +}; + +static const TSStateId ts_primary_state_ids[STATE_COUNT] = { + [0] = 0, + [1] = 1, + [2] = 2, + [3] = 3, + [4] = 4, + [5] = 5, + [6] = 6, + [7] = 7, + [8] = 8, + [9] = 9, + [10] = 10, + [11] = 11, + [12] = 12, + [13] = 13, + [14] = 14, + [15] = 15, + [16] = 16, + [17] = 17, + [18] = 18, + [19] = 19, + [20] = 20, + [21] = 21, + [22] = 22, + [23] = 23, + [24] = 24, + [25] = 25, + [26] = 26, + [27] = 27, +}; + +static bool ts_lex(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + if (eof) ADVANCE(17); + if (lookahead == '"') ADVANCE(5); + if (lookahead == '.') ADVANCE(23); + if (lookahead == ']') ADVANCE(14); + if (lookahead == 'd') ADVANCE(12); + if (lookahead == 'f') ADVANCE(11); + if (lookahead == 'i') ADVANCE(9); + if (lookahead == '{') ADVANCE(6); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') SKIP(0); + END_STATE(); + case 1: + if (lookahead == ' ') SKIP(1); + if (lookahead == '"') ADVANCE(5); + if (lookahead == '.') ADVANCE(23); + if (lookahead == 'd') ADVANCE(30); + if (lookahead == 'f') ADVANCE(31); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(24); + if (lookahead != 0 && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 2: + if (lookahead == ' ') SKIP(2); + if (lookahead == '"') ADVANCE(5); + if (lookahead == '.') ADVANCE(23); + if (lookahead == 'f') ADVANCE(31); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(25); + if (lookahead != 0 && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 3: + if (lookahead == ' ') SKIP(3); + if (lookahead == '"') ADVANCE(5); + if (lookahead == '.') ADVANCE(23); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(26); + if (lookahead != 0 && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 4: + if (lookahead == ' ') SKIP(4); + if (lookahead == '"') ADVANCE(5); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(27); + if (lookahead != 0 && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 5: + if (lookahead == '"') ADVANCE(34); + if (lookahead != 0) ADVANCE(5); + END_STATE(); + case 6: + if (lookahead == '[') ADVANCE(18); + END_STATE(); + case 7: + if (lookahead == '[') ADVANCE(18); + if (lookahead != 0) ADVANCE(36); + END_STATE(); + case 8: + if (lookahead == 'e') ADVANCE(22); + END_STATE(); + case 9: + if (lookahead == 'n') ADVANCE(21); + END_STATE(); + case 10: + if (lookahead == 'n') ADVANCE(8); + END_STATE(); + case 11: + if (lookahead == 'o') ADVANCE(13); + END_STATE(); + case 12: + if (lookahead == 'o') ADVANCE(10); + END_STATE(); + case 13: + if (lookahead == 'r') ADVANCE(20); + END_STATE(); + case 14: + if (lookahead == '}') ADVANCE(19); + END_STATE(); + case 15: + if (lookahead != 0 && + lookahead != '[') ADVANCE(36); + END_STATE(); + case 16: + if (eof) ADVANCE(17); + if (lookahead == '{') ADVANCE(7); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') ADVANCE(35); + if (lookahead != 0) ADVANCE(36); + END_STATE(); + case 17: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + case 18: + ACCEPT_TOKEN(anon_sym_LBRACE_LBRACK); + END_STATE(); + case 19: + ACCEPT_TOKEN(anon_sym_RBRACK_RBRACE); + END_STATE(); + case 20: + ACCEPT_TOKEN(anon_sym_for); + END_STATE(); + case 21: + ACCEPT_TOKEN(anon_sym_in); + END_STATE(); + case 22: + ACCEPT_TOKEN(anon_sym_done); + END_STATE(); + case 23: + ACCEPT_TOKEN(anon_sym_DOT); + END_STATE(); + case 24: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == ' ') SKIP(1); + if (lookahead == '.') ADVANCE(23); + if (lookahead == 'd') ADVANCE(30); + if (lookahead == 'f') ADVANCE(31); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(24); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 25: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == ' ') SKIP(2); + if (lookahead == '.') ADVANCE(23); + if (lookahead == 'f') ADVANCE(31); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(25); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 26: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == ' ') SKIP(3); + if (lookahead == '.') ADVANCE(23); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(26); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 27: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == ' ') SKIP(4); + if (('\t' <= lookahead && lookahead <= '\r')) ADVANCE(27); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 28: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == 'e') ADVANCE(22); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 29: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == 'n') ADVANCE(28); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 30: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == 'o') ADVANCE(29); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 31: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == 'o') ADVANCE(32); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 32: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead == 'r') ADVANCE(20); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 33: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead != 0 && + lookahead != ' ' && + lookahead != '"' && + lookahead != '.' && + lookahead != '\\' && + lookahead != ']') ADVANCE(33); + END_STATE(); + case 34: + ACCEPT_TOKEN(aux_sym_string_token2); + if (lookahead == '"') ADVANCE(5); + END_STATE(); + case 35: + ACCEPT_TOKEN(sym_text); + if (lookahead == '{') ADVANCE(7); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') ADVANCE(35); + if (lookahead != 0) ADVANCE(36); + END_STATE(); + case 36: + ACCEPT_TOKEN(sym_text); + if (lookahead == '{') ADVANCE(15); + if (lookahead != 0) ADVANCE(36); + END_STATE(); + default: + return false; + } +} + +static const TSLexerMode ts_lex_modes[STATE_COUNT] = { + [0] = {.lex_state = 0}, + [1] = {.lex_state = 16}, + [2] = {.lex_state = 16}, + [3] = {.lex_state = 16}, + [4] = {.lex_state = 16}, + [5] = {.lex_state = 16}, + [6] = {.lex_state = 1}, + [7] = {.lex_state = 2}, + [8] = {.lex_state = 3}, + [9] = {.lex_state = 0}, + [10] = {.lex_state = 16}, + [11] = {.lex_state = 4}, + [12] = {.lex_state = 0}, + [13] = {.lex_state = 16}, + [14] = {.lex_state = 16}, + [15] = {.lex_state = 4}, + [16] = {.lex_state = 0}, + [17] = {.lex_state = 16}, + [18] = {.lex_state = 16}, + [19] = {.lex_state = 0}, + [20] = {.lex_state = 0}, + [21] = {.lex_state = 16}, + [22] = {.lex_state = 0}, + [23] = {.lex_state = 0}, + [24] = {.lex_state = 0}, + [25] = {.lex_state = 0}, + [26] = {.lex_state = 0}, + [27] = {.lex_state = 0}, +}; + +static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { + [STATE(0)] = { + [ts_builtin_sym_end] = ACTIONS(1), + [anon_sym_LBRACE_LBRACK] = ACTIONS(1), + [anon_sym_RBRACK_RBRACE] = ACTIONS(1), + [anon_sym_for] = ACTIONS(1), + [anon_sym_in] = ACTIONS(1), + [anon_sym_done] = ACTIONS(1), + [anon_sym_DOT] = ACTIONS(1), + [aux_sym_string_token2] = ACTIONS(1), + }, + [STATE(1)] = { + [sym_source_file] = STATE(24), + [sym_element] = STATE(3), + [sym_interpolation] = STATE(10), + [sym_segment] = STATE(10), + [sym_for] = STATE(2), + [aux_sym_source_file_repeat1] = STATE(3), + [ts_builtin_sym_end] = ACTIONS(3), + [anon_sym_LBRACE_LBRACK] = ACTIONS(5), + [sym_text] = ACTIONS(7), + }, +}; + +static const uint16_t ts_small_parse_table[] = { + [0] = 6, + ACTIONS(7), 1, + sym_text, + ACTIONS(9), 1, + anon_sym_LBRACE_LBRACK, + STATE(2), 1, + sym_for, + STATE(13), 1, + sym_done, + STATE(4), 2, + sym_element, + aux_sym_source_file_repeat1, + STATE(10), 2, + sym_interpolation, + sym_segment, + [21] = 6, + ACTIONS(5), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(7), 1, + sym_text, + ACTIONS(11), 1, + ts_builtin_sym_end, + STATE(2), 1, + sym_for, + STATE(5), 2, + sym_element, + aux_sym_source_file_repeat1, + STATE(10), 2, + sym_interpolation, + sym_segment, + [42] = 6, + ACTIONS(7), 1, + sym_text, + ACTIONS(9), 1, + anon_sym_LBRACE_LBRACK, + STATE(2), 1, + sym_for, + STATE(18), 1, + sym_done, + STATE(5), 2, + sym_element, + aux_sym_source_file_repeat1, + STATE(10), 2, + sym_interpolation, + sym_segment, + [63] = 6, + ACTIONS(13), 1, + ts_builtin_sym_end, + ACTIONS(15), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(18), 1, + sym_text, + STATE(2), 1, + sym_for, + STATE(5), 2, + sym_element, + aux_sym_source_file_repeat1, + STATE(10), 2, + sym_interpolation, + sym_segment, + [84] = 6, + ACTIONS(21), 1, + anon_sym_for, + ACTIONS(23), 1, + anon_sym_done, + ACTIONS(25), 1, + anon_sym_DOT, + STATE(9), 1, + sym_string, + STATE(22), 1, + sym_path, + ACTIONS(27), 2, + aux_sym_string_token1, + aux_sym_string_token2, + [104] = 5, + ACTIONS(21), 1, + anon_sym_for, + ACTIONS(25), 1, + anon_sym_DOT, + STATE(9), 1, + sym_string, + STATE(22), 1, + sym_path, + ACTIONS(27), 2, + aux_sym_string_token1, + aux_sym_string_token2, + [121] = 4, + ACTIONS(25), 1, + anon_sym_DOT, + STATE(9), 1, + sym_string, + STATE(27), 1, + sym_path, + ACTIONS(27), 2, + aux_sym_string_token1, + aux_sym_string_token2, + [135] = 3, + ACTIONS(29), 1, + anon_sym_RBRACK_RBRACE, + ACTIONS(31), 1, + anon_sym_DOT, + STATE(16), 1, + aux_sym_path_repeat1, + [145] = 2, + ACTIONS(35), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(33), 2, + ts_builtin_sym_end, + sym_text, + [153] = 3, + ACTIONS(27), 1, + aux_sym_string_token2, + ACTIONS(37), 1, + aux_sym_string_token1, + STATE(25), 1, + sym_string, + [163] = 1, + ACTIONS(39), 3, + anon_sym_RBRACK_RBRACE, + anon_sym_in, + anon_sym_DOT, + [169] = 2, + ACTIONS(43), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(41), 2, + ts_builtin_sym_end, + sym_text, + [177] = 2, + ACTIONS(47), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(45), 2, + ts_builtin_sym_end, + sym_text, + [185] = 3, + ACTIONS(27), 1, + aux_sym_string_token2, + ACTIONS(37), 1, + aux_sym_string_token1, + STATE(20), 1, + sym_string, + [195] = 3, + ACTIONS(31), 1, + anon_sym_DOT, + ACTIONS(49), 1, + anon_sym_RBRACK_RBRACE, + STATE(19), 1, + aux_sym_path_repeat1, + [205] = 2, + ACTIONS(53), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(51), 2, + ts_builtin_sym_end, + sym_text, + [213] = 2, + ACTIONS(57), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(55), 2, + ts_builtin_sym_end, + sym_text, + [221] = 3, + ACTIONS(59), 1, + anon_sym_RBRACK_RBRACE, + ACTIONS(61), 1, + anon_sym_DOT, + STATE(19), 1, + aux_sym_path_repeat1, + [231] = 1, + ACTIONS(59), 2, + anon_sym_RBRACK_RBRACE, + anon_sym_DOT, + [236] = 2, + ACTIONS(64), 1, + anon_sym_LBRACE_LBRACK, + ACTIONS(66), 1, + sym_text, + [243] = 1, + ACTIONS(68), 1, + anon_sym_RBRACK_RBRACE, + [247] = 1, + ACTIONS(29), 1, + anon_sym_RBRACK_RBRACE, + [251] = 1, + ACTIONS(70), 1, + ts_builtin_sym_end, + [255] = 1, + ACTIONS(72), 1, + anon_sym_in, + [259] = 1, + ACTIONS(74), 1, + anon_sym_RBRACK_RBRACE, + [263] = 1, + ACTIONS(76), 1, + anon_sym_RBRACK_RBRACE, +}; + +static const uint32_t ts_small_parse_table_map[] = { + [SMALL_STATE(2)] = 0, + [SMALL_STATE(3)] = 21, + [SMALL_STATE(4)] = 42, + [SMALL_STATE(5)] = 63, + [SMALL_STATE(6)] = 84, + [SMALL_STATE(7)] = 104, + [SMALL_STATE(8)] = 121, + [SMALL_STATE(9)] = 135, + [SMALL_STATE(10)] = 145, + [SMALL_STATE(11)] = 153, + [SMALL_STATE(12)] = 163, + [SMALL_STATE(13)] = 169, + [SMALL_STATE(14)] = 177, + [SMALL_STATE(15)] = 185, + [SMALL_STATE(16)] = 195, + [SMALL_STATE(17)] = 205, + [SMALL_STATE(18)] = 213, + [SMALL_STATE(19)] = 221, + [SMALL_STATE(20)] = 231, + [SMALL_STATE(21)] = 236, + [SMALL_STATE(22)] = 243, + [SMALL_STATE(23)] = 247, + [SMALL_STATE(24)] = 251, + [SMALL_STATE(25)] = 255, + [SMALL_STATE(26)] = 259, + [SMALL_STATE(27)] = 263, +}; + +static const TSParseActionEntry ts_parse_actions[] = { + [0] = {.entry = {.count = 0, .reusable = false}}, + [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), + [3] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 0, 0, 0), + [5] = {.entry = {.count = 1, .reusable = false}}, SHIFT(7), + [7] = {.entry = {.count = 1, .reusable = true}}, SHIFT(10), + [9] = {.entry = {.count = 1, .reusable = false}}, SHIFT(6), + [11] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1, 0, 0), + [13] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2, 0, 0), + [15] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_source_file_repeat1, 2, 0, 0), SHIFT_REPEAT(7), + [18] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2, 0, 0), SHIFT_REPEAT(10), + [21] = {.entry = {.count = 1, .reusable = false}}, SHIFT(11), + [23] = {.entry = {.count = 1, .reusable = false}}, SHIFT(26), + [25] = {.entry = {.count = 1, .reusable = false}}, SHIFT(23), + [27] = {.entry = {.count = 1, .reusable = false}}, SHIFT(12), + [29] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_path, 1, 0, 0), + [31] = {.entry = {.count = 1, .reusable = true}}, SHIFT(15), + [33] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_element, 1, 0, 0), + [35] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_element, 1, 0, 0), + [37] = {.entry = {.count = 1, .reusable = true}}, SHIFT(12), + [39] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_string, 1, 0, 0), + [41] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_segment, 2, 0, 0), + [43] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_segment, 2, 0, 0), + [45] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_interpolation, 3, 0, 0), + [47] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_interpolation, 3, 0, 0), + [49] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_path, 2, 0, 0), + [51] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_done, 3, 0, 0), + [53] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_done, 3, 0, 0), + [55] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_segment, 3, 0, 0), + [57] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_segment, 3, 0, 0), + [59] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_path_repeat1, 2, 0, 0), + [61] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_path_repeat1, 2, 0, 0), SHIFT_REPEAT(15), + [64] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_for, 6, 0, 0), + [66] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_for, 6, 0, 0), + [68] = {.entry = {.count = 1, .reusable = true}}, SHIFT(14), + [70] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), + [72] = {.entry = {.count = 1, .reusable = true}}, SHIFT(8), + [74] = {.entry = {.count = 1, .reusable = true}}, SHIFT(17), + [76] = {.entry = {.count = 1, .reusable = true}}, SHIFT(21), +}; + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef TREE_SITTER_HIDE_SYMBOLS +#define TS_PUBLIC +#elif defined(_WIN32) +#define TS_PUBLIC __declspec(dllexport) +#else +#define TS_PUBLIC __attribute__((visibility("default"))) +#endif + +TS_PUBLIC const TSLanguage *tree_sitter_mytempl(void) { + static const TSLanguage language = { + .abi_version = LANGUAGE_VERSION, + .symbol_count = SYMBOL_COUNT, + .alias_count = ALIAS_COUNT, + .token_count = TOKEN_COUNT, + .external_token_count = EXTERNAL_TOKEN_COUNT, + .state_count = STATE_COUNT, + .large_state_count = LARGE_STATE_COUNT, + .production_id_count = PRODUCTION_ID_COUNT, + .supertype_count = SUPERTYPE_COUNT, + .field_count = FIELD_COUNT, + .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, + .parse_table = &ts_parse_table[0][0], + .small_parse_table = ts_small_parse_table, + .small_parse_table_map = ts_small_parse_table_map, + .parse_actions = ts_parse_actions, + .symbol_names = ts_symbol_names, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, + .alias_sequences = &ts_alias_sequences[0][0], + .lex_modes = (const void*)ts_lex_modes, + .lex_fn = ts_lex, + .primary_state_ids = ts_primary_state_ids, + .name = "mytempl", + .max_reserved_word_set_size = 0, + .metadata = { + .major_version = 0, + .minor_version = 1, + .patch_version = 0, + }, + }; + return &language; +} +#ifdef __cplusplus +} +#endif diff --git a/package/hemar/grammar/tree-sitter/src/tree_sitter/alloc.h b/package/hemar/grammar/tree-sitter/src/tree_sitter/alloc.h new file mode 100644 index 0000000..1abdd12 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/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 size); +extern void *(*ts_current_calloc)(size_t count, size_t size); +extern void *(*ts_current_realloc)(void *ptr, size_t size); +extern void (*ts_current_free)(void *ptr); + +#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/package/hemar/grammar/tree-sitter/src/tree_sitter/array.h b/package/hemar/grammar/tree-sitter/src/tree_sitter/array.h new file mode 100644 index 0000000..a17a574 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/src/tree_sitter/array.h @@ -0,0 +1,291 @@ +#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(push) +#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(pop) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ARRAY_H_ diff --git a/package/hemar/grammar/tree-sitter/src/tree_sitter/parser.h b/package/hemar/grammar/tree-sitter/src/tree_sitter/parser.h new file mode 100644 index 0000000..cdbe64c --- /dev/null +++ b/package/hemar/grammar/tree-sitter/src/tree_sitter/parser.h @@ -0,0 +1,287 @@ +#ifndef TREE_SITTER_PARSER_H_ +#define TREE_SITTER_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define ts_builtin_sym_error ((TSSymbol)-1) +#define ts_builtin_sym_end 0 +#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 + +#ifndef TREE_SITTER_API_H_ +typedef uint16_t TSStateId; +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +typedef struct TSLanguageMetadata TSLanguageMetadata; +typedef struct TSLanguageMetadata { + uint8_t major_version; + uint8_t minor_version; + uint8_t patch_version; +} TSLanguageMetadata; +#endif + +typedef struct { + TSFieldId field_id; + uint8_t child_index; + bool inherited; +} TSFieldMapEntry; + +// Used to index the field and supertype maps. +typedef struct { + uint16_t index; + uint16_t length; +} TSMapSlice; + +typedef struct { + bool visible; + bool named; + bool supertype; +} TSSymbolMetadata; + +typedef struct TSLexer TSLexer; + +struct TSLexer { + int32_t lookahead; + TSSymbol result_symbol; + void (*advance)(TSLexer *, bool); + void (*mark_end)(TSLexer *); + uint32_t (*get_column)(TSLexer *); + bool (*is_at_included_range_start)(const TSLexer *); + bool (*eof)(const TSLexer *); + void (*log)(const TSLexer *, const char *, ...); +}; + +typedef enum { + TSParseActionTypeShift, + TSParseActionTypeReduce, + TSParseActionTypeAccept, + TSParseActionTypeRecover, +} TSParseActionType; + +typedef union { + struct { + uint8_t type; + TSStateId state; + bool extra; + bool repetition; + } shift; + struct { + uint8_t type; + uint8_t child_count; + TSSymbol symbol; + int16_t dynamic_precedence; + uint16_t production_id; + } reduce; + uint8_t type; +} TSParseAction; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; +} TSLexMode; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; + uint16_t reserved_word_set_id; +} TSLexerMode; + +typedef union { + TSParseAction action; + struct { + uint8_t count; + bool reusable; + } entry; +} TSParseActionEntry; + +typedef struct { + int32_t start; + int32_t end; +} TSCharacterRange; + +struct TSLanguage { + uint32_t abi_version; + uint32_t symbol_count; + uint32_t alias_count; + uint32_t token_count; + uint32_t external_token_count; + uint32_t state_count; + uint32_t large_state_count; + uint32_t production_id_count; + uint32_t field_count; + uint16_t max_alias_sequence_length; + const uint16_t *parse_table; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; + const TSParseActionEntry *parse_actions; + const char * const *symbol_names; + const char * const *field_names; + const TSMapSlice *field_map_slices; + const TSFieldMapEntry *field_map_entries; + const TSSymbolMetadata *symbol_metadata; + const TSSymbol *public_symbol_map; + const uint16_t *alias_map; + const TSSymbol *alias_sequences; + const TSLexerMode *lex_modes; + bool (*lex_fn)(TSLexer *, TSStateId); + bool (*keyword_lex_fn)(TSLexer *, TSStateId); + TSSymbol keyword_capture_token; + struct { + const bool *states; + const TSSymbol *symbol_map; + void *(*create)(void); + void (*destroy)(void *); + bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); + unsigned (*serialize)(void *, char *); + void (*deserialize)(void *, const char *, unsigned); + } external_scanner; + const TSStateId *primary_state_ids; + const char *name; + const TSSymbol *reserved_words; + uint16_t max_reserved_word_set_size; + uint32_t supertype_count; + const TSSymbol *supertype_symbols; + const TSMapSlice *supertype_map_slices; + const TSSymbol *supertype_map_entries; + TSLanguageMetadata metadata; +}; + +static inline bool set_contains(const 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; + const 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; + } + const 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; \ + next_state: \ + lexer->advance(lexer, skip); \ + start: \ + skip = false; \ + lookahead = lexer->lookahead; + +#define ADVANCE(state_value) \ + { \ + state = state_value; \ + 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; \ + state = state_value; \ + goto next_state; \ + } + +#define ACCEPT_TOKEN(symbol_value) \ + result = true; \ + lexer->result_symbol = symbol_value; \ + lexer->mark_end(lexer); + +#define END_STATE() return result; + +/* + * Parse Table Macros + */ + +#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) + +#define STATE(id) id + +#define ACTIONS(id) id + +#define SHIFT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = (state_value) \ + } \ + }} + +#define SHIFT_REPEAT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = (state_value), \ + .repetition = true \ + } \ + }} + +#define SHIFT_EXTRA() \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .extra = true \ + } \ + }} + +#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() \ + {{ \ + .type = TSParseActionTypeRecover \ + }} + +#define ACCEPT_INPUT() \ + {{ \ + .type = TSParseActionTypeAccept \ + }} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSER_H_ diff --git a/package/hemar/grammar/tree-sitter/test/corpus/basic.txt b/package/hemar/grammar/tree-sitter/test/corpus/basic.txt new file mode 100644 index 0000000..9104620 --- /dev/null +++ b/package/hemar/grammar/tree-sitter/test/corpus/basic.txt @@ -0,0 +1,114 @@ +================== +simple tag +================== +{[hello]} +--- +(source_file + (element + (interpolation + (path + (string))))) +================== +text + tag +================== +foo {[bar]} baz +--- +(source_file + (element + (text)) + (element + (interpolation + (path + (string)))) + (element + (text))) + +================== +text + tag with quoted string +================== +foo {[".\]"]} foo {[""""]} baz +--- +(source_file + (element + (text)) + (element + (interpolation + (path + (string)))) + (element + (text)) + (element + (interpolation + (path + (string)))) + (element + (text))) + +================== +quoted dote path vs root path +================== +{["."]} {[.]} +--- +(source_file + (element + (interpolation + (path + (string + )))) + (element + (interpolation + (path)))) +================== +long path +================== +{["key".subkey."subsubkey"]} +--- +(source_file + (element + (interpolation + (path + (string) + (string) + (string))))) +================== +segment error +================== +{[ for i in key ]} +--- +(source_file + (ERROR + (for + (string) + (path + (string))))) +================== +segment +================== +{[ for i in key ]} +{[ done ]} +--- +(source_file + (element + (segment + (for + (string) + (path + (string))) + (done)))) +================== +segment with text +================== +{[ for i in key ]} + foo bar baz +{[ done ]} +--- +(source_file + (element + (segment + (for + (string) + (path + (string))) + (element + (text)) + (done)))) diff --git a/package/hemar/grammar/tree-sitter/tree-sitter.json b/package/hemar/grammar/tree-sitter/tree-sitter.json new file mode 100644 index 0000000..3934ebf --- /dev/null +++ b/package/hemar/grammar/tree-sitter/tree-sitter.json @@ -0,0 +1,40 @@ +{ + "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json", + "grammars": [ + { + "name": "hemar", + "camelcase": "Hemar", + "title": "Hemar", + "scope": "source.hemar", + "file-types": [ + "hemar" + ], + "injection-regex": "^hemar$", + "class-name": "TreeSitterHemar" + } + ], + "metadata": { + "version": "0.1.0", + "license": "MIT", + "description": "templater", + "authors": [ + { + "name": "yukkop", + "email": "hectic.yukkop@gmail.com", + "url": "https://yukkop.dev/" + } + ], + "links": { + "repository": "https://github.com/hectic-lab/util.nix" + } + }, + "bindings": { + "c": true, + "go": true, + "node": true, + "python": true, + "rust": true, + "swift": true, + "zig": false + } +} \ No newline at end of file