From 28490b35e25748ba7b208087594debc5e340606f Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Mon, 9 Sep 2024 10:56:37 -0400 Subject: [PATCH] build: update bindings --- .editorconfig | 10 +-- .gitattributes | 2 +- .gitignore | 5 +- Cargo.lock | 89 ++++++++++++++++++++ Cargo.toml | 8 +- Package.swift | 85 +++++++++++-------- binding.gyp | 13 ++- bindings/node/binding.cc | 2 +- bindings/python/tree_sitter_lua/__init__.py | 39 ++++++++- bindings/python/tree_sitter_lua/__init__.pyi | 9 +- bindings/rust/build.rs | 7 +- bindings/rust/lib.rs | 12 ++- go.mod | 2 +- package.json | 3 +- pyproject.toml | 10 +-- setup.py | 17 ++-- 16 files changed, 238 insertions(+), 75 deletions(-) create mode 100644 Cargo.lock diff --git a/.editorconfig b/.editorconfig index d3a8b5b..f363cc5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,9 +2,6 @@ root = true [*] charset = utf-8 -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true [*.{json,toml,yml,gyp}] indent_style = space @@ -14,11 +11,11 @@ indent_size = 2 indent_style = space indent_size = 2 -[*.rs] +[*.{c,cc,h}] indent_style = space indent_size = 4 -[*.{c,cc,h}] +[*.rs] indent_style = space indent_size = 4 @@ -37,3 +34,6 @@ indent_size = 8 [Makefile] indent_style = tab indent_size = 8 + +[parser.c] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes index ffb52ab..4cb1058 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,4 @@ -* text eol=lf +* text=auto eol=lf src/*.json linguist-generated src/parser.c linguist-generated diff --git a/.gitignore b/.gitignore index 70bef74..2fd9dac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,17 @@ # Rust artifacts -Cargo.lock target/ # Node artifacts build/ +prebuilds/ node_modules/ *.tgz # Swift artifacts .build/ +Package.resolved # Go artifacts -go.sum _obj/ # Python artifacts @@ -32,7 +32,6 @@ dist/ /examples/*/ # Grammar volatiles -dsl.d.ts *.wasm *.obj *.o diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b4d69ab --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "cc" +version = "1.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" +dependencies = [ + "shlex", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "tree-sitter" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20f4cd3642c47a85052a887d86704f4eac272969f61b686bdd3f772122aabaff" +dependencies = [ + "cc", + "regex", + "regex-syntax", + "tree-sitter-language", +] + +[[package]] +name = "tree-sitter-language" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2545046bd1473dac6c626659cc2567c6c0ff302fc8b84a56c4243378276f7f57" + +[[package]] +name = "tree-sitter-lua" +version = "0.2.0" +dependencies = [ + "cc", + "tree-sitter", + "tree-sitter-language", +] diff --git a/Cargo.toml b/Cargo.toml index ce14e12..f502436 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,8 @@ path = "bindings/rust/lib.rs" [dependencies] tree-sitter-language = "0.1.0" -[dev-dependencies] -tree-sitter = "0.23.0" - [build-dependencies] -cc = "^1.0.89" +cc = "1.1.18" + +[dev-dependencies] +tree-sitter = "0.23" diff --git a/Package.swift b/Package.swift index 7022307..f8f602e 100644 --- a/Package.swift +++ b/Package.swift @@ -3,47 +3,58 @@ import PackageDescription let package = Package( name: "TreeSitterLua", - platforms: [.macOS(.v10_13), .iOS(.v11)], products: [ .library(name: "TreeSitterLua", targets: ["TreeSitterLua"]), ], - dependencies: [], + dependencies: [ + .package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"), + ], targets: [ - .target(name: "TreeSitterLua", - path: ".", - exclude: [ - "Cargo.toml", - "Makefile", - "binding.gyp", - "bindings/c", - "bindings/go", - "bindings/node", - "bindings/python", - "bindings/rust", - "prebuilds", - "node_modules", - "grammar.js", - "package.json", - "package-lock.json", - "pyproject.toml", - "setup.py", - "test", - "types", - "examples", - ".editorconfig", - ".github", - ".gitignore", - ".gitattributes", - ], - sources: [ - "src/parser.c", - "src/scanner.c", - ], - resources: [ - .copy("queries") - ], - publicHeadersPath: "bindings/swift", - cSettings: [.headerSearchPath("src")]) + .target( + name: "TreeSitterLua", + dependencies: [], + 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", + "src/scanner.c", + ], + resources: [ + .copy("queries") + ], + publicHeadersPath: "bindings/swift", + cSettings: [.headerSearchPath("src")] + ), + .testTarget( + name: "TreeSitterLuaTests", + dependencies: [ + "SwiftTreeSitter", + "TreeSitterLua", + ], + path: "bindings/swift/TreeSitterLuaTests" + ) ], cLanguageStandard: .c11 ) diff --git a/binding.gyp b/binding.gyp index b8142ff..5c83586 100644 --- a/binding.gyp +++ b/binding.gyp @@ -13,8 +13,17 @@ "src/parser.c", "src/scanner.c", ], - "cflags_c": [ - "-std=c11", + "conditions": [ + ["OS!='win'", { + "cflags_c": [ + "-std=c11", + ], + }, { # OS == "win" + "cflags_c": [ + "/std:c11", + "/utf-8", + ], + }], ], } ] diff --git a/bindings/node/binding.cc b/bindings/node/binding.cc index 10b452d..896ea30 100644 --- a/bindings/node/binding.cc +++ b/bindings/node/binding.cc @@ -6,7 +6,7 @@ extern "C" TSLanguage *tree_sitter_lua(); // "tree-sitter", "language" hashed with BLAKE2 const napi_type_tag LANGUAGE_TYPE_TAG = { - 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16 + 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16 }; Napi::Object Init(Napi::Env env, Napi::Object exports) { diff --git a/bindings/python/tree_sitter_lua/__init__.py b/bindings/python/tree_sitter_lua/__init__.py index 1ccaaef..3f96411 100644 --- a/bindings/python/tree_sitter_lua/__init__.py +++ b/bindings/python/tree_sitter_lua/__init__.py @@ -1,3 +1,40 @@ -"Lua grammar for tree-sitter" +"""Lua grammar for tree-sitter""" + +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): + 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/bindings/python/tree_sitter_lua/__init__.pyi b/bindings/python/tree_sitter_lua/__init__.pyi index 5416666..991b926 100644 --- a/bindings/python/tree_sitter_lua/__init__.pyi +++ b/bindings/python/tree_sitter_lua/__init__.pyi @@ -1 +1,8 @@ -def language() -> int: ... +from typing import Final + +HIGHLIGHTS_QUERY: Final[str] +INJECTIONS_QUERY: Final[str] +LOCALS_QUERY: Final[str] +TAGS_QUERY: Final[str] + +def language() -> object: ... diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs index 49916c1..7ac371e 100644 --- a/bindings/rust/build.rs +++ b/bindings/rust/build.rs @@ -2,11 +2,8 @@ fn main() { let src_dir = std::path::Path::new("src"); let mut c_config = cc::Build::new(); - c_config.include(src_dir); - c_config - .flag_if_supported("-Wno-unused-parameter") - .flag_if_supported("-Wno-unused-but-set-variable") - .flag_if_supported("-Wno-trigraphs"); + c_config.std("c11").include(src_dir); + #[cfg(target_env = "msvc")] c_config.flag("-utf-8"); diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs index 4811462..adbb219 100644 --- a/bindings/rust/lib.rs +++ b/bindings/rust/lib.rs @@ -4,9 +4,12 @@ //! tree-sitter [Parser][], and then use the parser to parse some code: //! //! ``` +//! use tree_sitter::Parser; +//! //! let code = r#" +//! return 42 //! "#; -//! let mut parser = tree_sitter::Parser::new(); +//! let mut parser = Parser::new(); //! let language = tree_sitter_lua::LANGUAGE; //! parser //! .set_language(&language.into()) @@ -34,9 +37,16 @@ pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_lua) /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types pub const NODE_TYPES: &str = include_str!("../../src/node-types.json"); +/// The syntax highlighting query for this language. pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm"); + +/// The injection query for this language. pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm"); + +/// The local-variable syntax highlighting query for this language. pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm"); + +/// The symbol tagging query for this language. pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm"); #[cfg(test)] diff --git a/go.mod b/go.mod index e28b362..234a677 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/tree-sitter/tree-sitter-lua +module github.com/tree-sitter-grammars/tree-sitter-lua go 1.23 diff --git a/package.json b/package.json index 3025aa4..472984c 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "prebuilds/**", "bindings/node/*", "queries/**", - "src/**" + "src/**", + "*.wasm" ], "dependencies": { "node-addon-api": "^8.0.0", diff --git a/pyproject.toml b/pyproject.toml index 50d7152..be78c58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,12 +12,10 @@ classifiers = [ "License :: OSI Approved :: MIT License", "Topic :: Software Development :: Compilers", "Topic :: Text Processing :: Linguistic", - "Typing :: Typed" + "Typing :: Typed", ] -authors = [ - {name = "Munif Tanjim", email = "hello@muniftanjim.dev"} -] -requires-python = ">=3.8" +authors = [{ name = "Munif Tanjim", email = "hello@muniftanjim.dev" }] +requires-python = ">=3.9" license.text = "MIT" readme = "README.md" @@ -28,5 +26,5 @@ Homepage = "https://github.com/tree-sitter-grammars/tree-sitter-lua" core = ["tree-sitter~=0.23"] [tool.cibuildwheel] -build = "cp38-*" +build = "cp39-*" build-frontend = "build" diff --git a/setup.py b/setup.py index c1d124c..481dcc8 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ class BdistWheel(bdist_wheel): def get_tag(self): python, abi, platform = super().get_tag() if python.startswith("cp"): - python, abi = "cp38", "abi3" + python, abi = "cp39", "abi3" return python, abi, platform @@ -38,12 +38,17 @@ def get_tag(self): "src/parser.c", "src/scanner.c", ], - extra_compile_args=( - ["-std=c11"] if system() != 'Windows' else [] - ), + extra_compile_args=[ + "-std=c11", + "-fvisibility=hidden", + ] if system() != "Windows" else [ + "/std:c11", + "/utf-8", + ], define_macros=[ - ("Py_LIMITED_API", "0x03080000"), - ("PY_SSIZE_T_CLEAN", None) + ("Py_LIMITED_API", "0x03090000"), + ("PY_SSIZE_T_CLEAN", None), + ("TREE_SITTER_HIDE_SYMBOLS", None), ], include_dirs=["src"], py_limited_api=True,