diff --git a/.editorconfig b/.editorconfig index f363cc5..7756ee9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,6 +11,10 @@ indent_size = 2 indent_style = space indent_size = 2 +[*.scm] +indent_style = space +indent_size = 2 + [*.{c,cc,h}] indent_style = space indent_size = 4 diff --git a/.gitattributes b/.gitattributes index 4cb1058..9d5c5d4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,4 +8,6 @@ bindings/** linguist-generated binding.gyp linguist-generated setup.py linguist-generated Makefile linguist-generated +CMakeLists.txt linguist-generated Package.swift linguist-generated +go.mod linguist-generated diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8c90ff..35bff3a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,11 +37,11 @@ jobs: - name: Run tests uses: tree-sitter/parser-test-action@v2 with: - test-rust: ${{runner.os == 'Linux'}} + test-rust: true test-node: true test-python: true test-go: true - test-swift: ${{runner.os == 'macOS'}} + test-swift: true - name: Parse examples uses: tree-sitter/parse-action@v4 with: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index cb7e16b..a1e6b57 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,11 +4,9 @@ on: push: tags: ["*"] -concurrency: - group: ${{github.workflow}}-${{github.ref}} - cancel-in-progress: true - jobs: + github: + uses: tree-sitter/workflows/.github/workflows/release.yml@main npm: uses: tree-sitter/workflows/.github/workflows/package-npm.yml@main secrets: diff --git a/.gitignore b/.gitignore index 2fd9dac..308fcab 100644 --- a/.gitignore +++ b/.gitignore @@ -5,11 +5,9 @@ target/ build/ prebuilds/ node_modules/ -*.tgz # Swift artifacts .build/ -Package.resolved # Go artifacts _obj/ @@ -35,3 +33,8 @@ dist/ *.wasm *.obj *.o + +# Archives +*.tar.gz +*.tgz +*.zip diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..635498c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,60 @@ +cmake_minimum_required(VERSION 3.13) + +project(tree-sitter-c + VERSION "0.23.1" + DESCRIPTION "C grammar for tree-sitter" + HOMEPAGE_URL "https://github.com/tree-sitter/tree-sitter-c" + 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 14 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-c src/parser.c) +if(EXISTS src/scanner.c) + target_sources(tree-sitter-c PRIVATE src/scanner.c) +endif() +target_include_directories(tree-sitter-c PRIVATE src) + +target_compile_definitions(tree-sitter-c PRIVATE + $<$:TREE_SITTER_REUSE_ALLOCATOR> + $<$:TREE_SITTER_DEBUG>) + +set_target_properties(tree-sitter-c + PROPERTIES + C_STANDARD 11 + POSITION_INDEPENDENT_CODE ON + SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}" + DEFINE_SYMBOL "") + +configure_file(bindings/c/tree-sitter-c.pc.in + "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-c.pc" @ONLY) + +include(GNUInstallDirs) + +install(FILES bindings/c/tree-sitter-c.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tree_sitter") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-c.pc" + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") +install(TARGETS tree-sitter-c + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") + +add_custom_target(test "${TREE_SITTER_CLI}" test + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "tree-sitter test") + +# vim:ft=cmake: diff --git a/Cargo.lock b/Cargo.lock index 59cd6dc..b4946e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.21" +version = "1.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" +checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" dependencies = [ "shlex", ] @@ -61,15 +61,22 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" + [[package]] name = "tree-sitter" -version = "0.23.0" +version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20f4cd3642c47a85052a887d86704f4eac272969f61b686bdd3f772122aabaff" +checksum = "f9871f16d6cf5c4757dcf30d5d2172a2df6987c510c017bbb7abfb7f9aa24d06" dependencies = [ "cc", "regex", "regex-syntax", + "streaming-iterator", "tree-sitter-language", ] diff --git a/Cargo.toml b/Cargo.toml index 67d88ac..e7bb182 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,4 @@ tree-sitter-language = "0.1.0" cc = "1.1.15" [dev-dependencies] -tree-sitter = "0.23" +tree-sitter = "0.24" diff --git a/Makefile b/Makefile index 5862d53..ae5d1ec 100644 --- a/Makefile +++ b/Makefile @@ -2,23 +2,13 @@ ifeq ($(OS),Windows_NT) $(error Windows is not supported) endif -VERSION := 0.23.1 - LANGUAGE_NAME := tree-sitter-c +HOMEPAGE_URL := https://github.com/tree-sitter/tree-sitter-c +VERSION := 0.23.1 # repository SRC_DIR := src -PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null) - -ifeq ($(PARSER_URL),) - PARSER_URL := $(subst .git,,$(PARSER_REPO_URL)) -ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),) - PARSER_URL := $(subst :,/,$(PARSER_URL)) - PARSER_URL := $(subst git@,https://,$(PARSER_URL)) -endif -endif - TS ?= tree-sitter # install directory layout @@ -37,28 +27,20 @@ ARFLAGS ?= rcs override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC # ABI versioning -SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION))) -SONAME_MINOR := $(shell sed -n 's/#define LANGUAGE_VERSION //p' $(PARSER)) +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 := $(LINKSHARED)-dynamiclib -Wl, - ifneq ($(ADDITIONAL_LIBS),) - LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS), - endif - LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks + 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 := $(LINKSHARED)-shared -Wl, - ifneq ($(ADDITIONAL_LIBS),) - LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS) - endif - LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).$(SOEXTVER) + LINKSHARED = -shared -Wl,-soname,lib$(LANGUAGE_NAME).$(SOEXTVER) endif ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),) PCLIBDIR := $(PREFIX)/libdata/pkgconfig @@ -76,17 +58,15 @@ ifneq ($(STRIP),) endif $(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in - sed -e 's|@URL@|$(PARSER_URL)|' \ - -e 's|@VERSION@|$(VERSION)|' \ - -e 's|@LIBDIR@|$(LIBDIR)|' \ - -e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \ - -e 's|@REQUIRES@|$(REQUIRES)|' \ - -e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|' \ - -e 's|=$(PREFIX)|=$${prefix}|' \ - -e 's|@PREFIX@|$(PREFIX)|' $< > $@ + 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 --no-bindings $^ + $(TS) generate $^ install: all install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)' diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..9e0a023 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "SwiftTreeSitter", + "repositoryURL": "https://github.com/ChimeHQ/SwiftTreeSitter", + "state": { + "branch": null, + "revision": "2599e95310b3159641469d8a21baf2d3d200e61f", + "version": "0.8.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index fbce1e3..e6132c8 100644 --- a/Package.swift +++ b/Package.swift @@ -14,29 +14,6 @@ let package = Package( name: "TreeSitterC", 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", ], diff --git a/go.mod b/go.mod index a680b3d..986a292 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,6 @@ module github.com/tree-sitter/tree-sitter-c go 1.23 -require github.com/tree-sitter/go-tree-sitter v0.23.1 +require github.com/tree-sitter/go-tree-sitter v0.24.0 require github.com/mattn/go-pointer v0.0.1 // indirect diff --git a/go.sum b/go.sum index fae4583..2bd1347 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tree-sitter/go-tree-sitter v0.23.1 h1:HCfaE19sKfG7q190xfM1loUZf6wEHa4TDqDEW46s9Lg= github.com/tree-sitter/go-tree-sitter v0.23.1/go.mod h1:EvIVhMvvPNvhu9x+ddSPxSnUEU5AnsSwi1LMqXIVE3A= +github.com/tree-sitter/go-tree-sitter v0.24.0 h1:kRZb6aBNfcI/u0Qh8XEt3zjNVnmxTisDBN+kXK0xRYQ= +github.com/tree-sitter/go-tree-sitter v0.24.0/go.mod h1:x681iFVoLMEwOSIHA1chaLkXlroXEN7WY+VHGFaoDbk= github.com/tree-sitter/tree-sitter-cpp v0.22.4-0.20240818224355-b1a4e2b25148 h1:AfFPZwtwGN01BW1jDdqBVqscTwetvMpydqYZz57RSlc= github.com/tree-sitter/tree-sitter-cpp v0.22.4-0.20240818224355-b1a4e2b25148/go.mod h1:Bh6U3viD57rFXRYIQ+kmiYtr+1Bx0AceypDLJJSyi9s= github.com/tree-sitter/tree-sitter-embedded-template v0.21.1-0.20240819044651-ffbf64942c33 h1:TwqSV3qLp3tKSqirGLRHnjFk9Tc2oy57LIl+FQ4GjI4= diff --git a/package.json b/package.json index 13e8544..6adc51f 100644 --- a/package.json +++ b/package.json @@ -49,17 +49,5 @@ "prestart": "tree-sitter build --wasm", "start": "tree-sitter playground", "test": "node --test bindings/node/*_test.js" - }, - "tree-sitter": [ - { - "scope": "source.c", - "file-types": [ - "c", - "h" - ], - "injection-regex": "^(c|h)$", - "highlights": "queries/highlights.scm", - "tags": "queries/tags.scm" - } - ] -} + } +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 56acf5a..ce8bb61 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,7 +26,7 @@ readme = "README.md" Homepage = "https://github.com/tree-sitter/tree-sitter-c" [project.optional-dependencies] -core = ["tree-sitter~=0.21"] +core = ["tree-sitter~=0.23"] [tool.cibuildwheel] build = "cp39-*" diff --git a/src/grammar.json b/src/grammar.json index 4878fff..fdea897 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -1,4 +1,5 @@ { + "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json", "name": "c", "word": "identifier", "rules": { diff --git a/src/node-types.json b/src/node-types.json index d47e5d1..f007e49 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -3624,6 +3624,7 @@ { "type": "translation_unit", "named": true, + "root": true, "fields": {}, "children": { "multiple": true, diff --git a/src/tree_sitter/alloc.h b/src/tree_sitter/alloc.h index 1f4466d..1abdd12 100644 --- a/src/tree_sitter/alloc.h +++ b/src/tree_sitter/alloc.h @@ -12,10 +12,10 @@ extern "C" { // Allow clients to override allocation functions #ifdef TREE_SITTER_REUSE_ALLOCATOR -extern void *(*ts_current_malloc)(size_t); -extern void *(*ts_current_calloc)(size_t, size_t); -extern void *(*ts_current_realloc)(void *, size_t); -extern void (*ts_current_free)(void *); +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 diff --git a/tree-sitter.json b/tree-sitter.json new file mode 100644 index 0000000..b48c526 --- /dev/null +++ b/tree-sitter.json @@ -0,0 +1,43 @@ +{ + "grammars": [ + { + "name": "c", + "camelcase": "C", + "scope": "source.c", + "path": ".", + "file-types": [ + "c", + "h" + ], + "highlights": "queries/highlights.scm", + "tags": "queries/tags.scm", + "injection-regex": "^(c|h)$" + } + ], + "metadata": { + "version": "0.23.1", + "license": "MIT", + "description": "C grammar for tree-sitter", + "authors": [ + { + "name": "Max Brunsfeld", + "email": "maxbrunsfeld@gmail.com" + }, + { + "name": "Amaan Qureshi", + "email": "amaanq12@gmail.com" + } + ], + "links": { + "repository": "https://github.com/tree-sitter/tree-sitter-c" + } + }, + "bindings": { + "c": true, + "go": true, + "node": true, + "python": true, + "rust": true, + "swift": true + } +} \ No newline at end of file