From 58608e479673200d22538b7ca3d004498d1a0d4b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 5 Nov 2025 14:33:55 +0100 Subject: [PATCH] update outdated files via `tree-sitter init -u` --- .gitattributes | 4 ++ .gitignore | 15 ++++- CMakeLists.txt | 66 +++++++++++++++++++ Makefile | 2 +- Package.swift | 2 +- .../c/{ => tree_sitter}/tree-sitter-dotenv.h | 0 bindings/node/index.js | 6 +- bindings/python/tests/test_binding.py | 2 +- bindings/python/tree_sitter_dotenv/binding.c | 14 +++- pyproject.toml | 6 +- setup.py | 47 ++++++++----- 11 files changed, 137 insertions(+), 27 deletions(-) create mode 100644 CMakeLists.txt rename bindings/c/{ => tree_sitter}/tree-sitter-dotenv.h (100%) diff --git a/.gitattributes b/.gitattributes index 4cb1058..6042937 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,3 +9,7 @@ binding.gyp linguist-generated setup.py linguist-generated Makefile linguist-generated Package.swift linguist-generated + +# Zig bindings +build.zig linguist-generated +build.zig.zon linguist-generated diff --git a/.gitignore b/.gitignore index 2fd9dac..87a0c80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,12 @@ # Rust artifacts target/ +Cargo.lock # Node artifacts build/ prebuilds/ node_modules/ -*.tgz +package-lock.json # Swift artifacts .build/ @@ -27,6 +28,13 @@ dist/ *.dylib *.dll *.pc +*.exp +*.lib + +# Zig artifacts +.zig-cache/ +zig-cache/ +zig-out/ # Example dirs /examples/*/ @@ -35,3 +43,8 @@ dist/ *.wasm *.obj *.o + +# Archives +*.tar.gz +*.tgz +*.zip diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..386d60a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.13) + +project(tree-sitter-dotenv + VERSION "0.0.4" + DESCRIPTION "Dotenv grammar for tree-sitter" + HOMEPAGE_URL "https://github.com/pnx/tree-sitter-dotenv" + 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() + +include(GNUInstallDirs) + +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-dotenv src/parser.c) +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scanner.c) + target_sources(tree-sitter-dotenv PRIVATE src/scanner.c) +endif() +target_include_directories(tree-sitter-dotenv + PRIVATE src + INTERFACE $ + $) + +target_compile_definitions(tree-sitter-dotenv PRIVATE + $<$:TREE_SITTER_REUSE_ALLOCATOR> + $<$:TREE_SITTER_DEBUG>) + +set_target_properties(tree-sitter-dotenv + PROPERTIES + C_STANDARD 11 + POSITION_INDEPENDENT_CODE ON + SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}" + DEFINE_SYMBOL "") + +configure_file(bindings/c/tree-sitter-dotenv.pc.in + "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-dotenv.pc" @ONLY) + +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-dotenv.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +install(TARGETS tree-sitter-dotenv + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") + +file(GLOB QUERIES queries/*.scm) +install(FILES ${QUERIES} + DESTINATION "${CMAKE_INSTALL_DATADIR}/tree-sitter/queries/dotenv") + +add_custom_target(ts-test "${TREE_SITTER_CLI}" test + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "tree-sitter test") diff --git a/Makefile b/Makefile index fcc7915..c23853d 100644 --- a/Makefile +++ b/Makefile @@ -90,7 +90,7 @@ $(PARSER): $(SRC_DIR)/grammar.json install: all install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)' - install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h + install -m644 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) diff --git a/Package.swift b/Package.swift index 558017c..ade26a5 100644 --- a/Package.swift +++ b/Package.swift @@ -7,7 +7,7 @@ let package = Package( .library(name: "TreeSitterDotenv", targets: ["TreeSitterDotenv"]), ], dependencies: [ - .package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"), + .package(name: "SwiftTreeSitter", url: "https://github.com/tree-sitter/swift-tree-sitter", from: "0.8.0"), ], targets: [ .target( diff --git a/bindings/c/tree-sitter-dotenv.h b/bindings/c/tree_sitter/tree-sitter-dotenv.h similarity index 100% rename from bindings/c/tree-sitter-dotenv.h rename to bindings/c/tree_sitter/tree-sitter-dotenv.h diff --git a/bindings/node/index.js b/bindings/node/index.js index 6657bcf..385b046 100644 --- a/bindings/node/index.js +++ b/bindings/node/index.js @@ -1,6 +1,10 @@ const root = require("path").join(__dirname, "..", ".."); -module.exports = require("node-gyp-build")(root); +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-dotenv.node`) + : require("node-gyp-build")(root); try { module.exports.nodeTypeInfo = require("../../src/node-types.json"); diff --git a/bindings/python/tests/test_binding.py b/bindings/python/tests/test_binding.py index c0b9c22..9fa99ce 100644 --- a/bindings/python/tests/test_binding.py +++ b/bindings/python/tests/test_binding.py @@ -6,6 +6,6 @@ import tree_sitter, tree_sitter_dotenv class TestLanguage(TestCase): def test_can_load_grammar(self): try: - tree_sitter.Language(tree_sitter_dotenv.language()) + Parser(Language(tree_sitter_dotenv.language())) except Exception: self.fail("Error loading Dotenv grammar") diff --git a/bindings/python/tree_sitter_dotenv/binding.c b/bindings/python/tree_sitter_dotenv/binding.c index 2c76a8b..4dcddc6 100644 --- a/bindings/python/tree_sitter_dotenv/binding.c +++ b/bindings/python/tree_sitter_dotenv/binding.c @@ -8,6 +8,13 @@ static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSE return PyCapsule_New(tree_sitter_dotenv(), "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."}, @@ -18,10 +25,11 @@ static struct PyModuleDef module = { .m_base = PyModuleDef_HEAD_INIT, .m_name = "_binding", .m_doc = NULL, - .m_size = -1, - .m_methods = methods + .m_size = 0, + .m_methods = methods, + .m_slots = slots, }; PyMODINIT_FUNC PyInit__binding(void) { - return PyModule_Create(&module); + return PyModuleDef_Init(&module); } diff --git a/pyproject.toml b/pyproject.toml index b92263d..a7bb6df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ classifiers = [ "Topic :: Text Processing :: Linguistic", "Typing :: Typed" ] -requires-python = ">=3.9" +requires-python = ">=3.10" license.text = "MIT" readme = "README.md" @@ -22,8 +22,8 @@ readme = "README.md" Homepage = "https://github.com/pnx/tree-sitter-dotenv" [project.optional-dependencies] -core = ["tree-sitter~=0.22"] +core = ["tree-sitter~=0.24"] [tool.cibuildwheel] -build = "cp39-*" +build = "cp310-*" build-frontend = "build" diff --git a/setup.py b/setup.py index 9f4f9f4..9028693 100644 --- a/setup.py +++ b/setup.py @@ -1,27 +1,49 @@ -from os.path import isdir, join -from platform import system +from os import path +from sysconfig import get_config_var from setuptools import Extension, find_packages, setup from setuptools.command.build import build +from setuptools.command.build_ext import build_ext +from setuptools.command.egg_info import egg_info from wheel.bdist_wheel import bdist_wheel class Build(build): def run(self): - if isdir("queries"): - dest = join(self.build_lib, "tree_sitter_dotenv", "queries") + if path.isdir("queries"): + dest = path.join(self.build_lib, "tree_sitter_dotenv", "queries") self.copy_tree("queries", dest) super().run() +class BuildExt(build_ext): + def build_extension(self, ext: Extension): + if self.compiler.compiler_type != "msvc": + ext.extra_compile_args = ["-std=c11", "-fvisibility=hidden"] + else: + ext.extra_compile_args = ["/std:c11", "/utf-8"] + if path.exists("src/scanner.c"): + ext.sources.append("src/scanner.c") + if ext.py_limited_api: + ext.define_macros.append(("Py_LIMITED_API", "0x030A0000")) + super().build_extension(ext) + + class BdistWheel(bdist_wheel): def get_tag(self): python, abi, platform = super().get_tag() if python.startswith("cp"): - python, abi = "cp39", "abi3" + 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"}, @@ -36,27 +58,20 @@ setup( sources=[ "bindings/python/tree_sitter_dotenv/binding.c", "src/parser.c", - # NOTE: if your language uses an external scanner, add it here. - ], - extra_compile_args=[ - "-std=c11", - "-fvisibility=hidden", - ] if system() != "Windows" else [ - "/std:c11", - "/utf-8", ], define_macros=[ - ("Py_LIMITED_API", "0x03090000"), ("PY_SSIZE_T_CLEAN", None), ("TREE_SITTER_HIDE_SYMBOLS", None), ], include_dirs=["src"], - py_limited_api=True, + py_limited_api=not get_config_var("Py_GIL_DISABLED"), ) ], cmdclass={ "build": Build, - "bdist_wheel": BdistWheel + "build_ext": BuildExt, + "bdist_wheel": BdistWheel, + "egg_info": EggInfo, }, zip_safe=False )