diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..271b173 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,60 @@ +cmake_minimum_required(VERSION 3.13) + +project(tree-sitter-query + VERSION "0.4.0" + DESCRIPTION "TS query grammar for tree-sitter" + HOMEPAGE_URL "https://github.com/tree-sitter-grammars/tree-sitter-query" + 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-query src/parser.c) +if(EXISTS src/scanner.c) + target_sources(tree-sitter-query PRIVATE src/scanner.c) +endif() +target_include_directories(tree-sitter-query PRIVATE src) + +target_compile_definitions(tree-sitter-query PRIVATE + $<$:TREE_SITTER_REUSE_ALLOCATOR> + $<$:TREE_SITTER_DEBUG>) + +set_target_properties(tree-sitter-query + PROPERTIES + C_STANDARD 11 + POSITION_INDEPENDENT_CODE ON + SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}" + DEFINE_SYMBOL "") + +configure_file(bindings/c/tree-sitter-query.pc.in + "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-query.pc" @ONLY) + +include(GNUInstallDirs) + +install(FILES bindings/c/tree-sitter-query.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tree_sitter") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-query.pc" + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") +install(TARGETS tree-sitter-query + 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/bindings/go/binding.go b/bindings/go/binding.go index b4d2e1a..316defd 100644 --- a/bindings/go/binding.go +++ b/bindings/go/binding.go @@ -2,6 +2,7 @@ package tree_sitter_query // #cgo CFLAGS: -std=c11 -fPIC // #include "../../src/parser.c" +// // NOTE: if your language has an external scanner, add it here. import "C" import "unsafe" diff --git a/bindings/go/binding_test.go b/bindings/go/binding_test.go index fdabcd1..8d0b0dc 100644 --- a/bindings/go/binding_test.go +++ b/bindings/go/binding_test.go @@ -3,8 +3,8 @@ package tree_sitter_query_test import ( "testing" - tree_sitter "github.com/smacker/go-tree-sitter" - "github.com/tree-sitter-grammars/tree-sitter-query" + tree_sitter "github.com/tree-sitter/go-tree-sitter" + tree_sitter_query "github.com/tree-sitter-grammars/tree-sitter-query/bindings/go" ) func TestCanLoadGrammar(t *testing.T) { diff --git a/bindings/go/go.mod b/bindings/go/go.mod deleted file mode 100644 index 10247b3..0000000 --- a/bindings/go/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module github.com/tree-sitter-grammars/tree-sitter-query - -go 1.22 - -require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8 diff --git a/bindings/node/binding_test.js b/bindings/node/binding_test.js new file mode 100644 index 0000000..afede30 --- /dev/null +++ b/bindings/node/binding_test.js @@ -0,0 +1,9 @@ +/// + +const assert = require("node:assert"); +const { test } = require("node:test"); + +test("can load grammar", () => { + const parser = new (require("tree-sitter"))(); + assert.doesNotThrow(() => parser.setLanguage(require("."))); +}); diff --git a/bindings/python/tests/test_binding.py b/bindings/python/tests/test_binding.py new file mode 100644 index 0000000..2a01099 --- /dev/null +++ b/bindings/python/tests/test_binding.py @@ -0,0 +1,11 @@ +from unittest import TestCase + +import tree_sitter, tree_sitter_query + + +class TestLanguage(TestCase): + def test_can_load_grammar(self): + try: + tree_sitter.Language(tree_sitter_query.language()) + except Exception: + self.fail("Error loading Query grammar") diff --git a/bindings/swift/TreeSitterQueryTests/TreeSitterQueryTests.swift b/bindings/swift/TreeSitterQueryTests/TreeSitterQueryTests.swift new file mode 100644 index 0000000..f168fbb --- /dev/null +++ b/bindings/swift/TreeSitterQueryTests/TreeSitterQueryTests.swift @@ -0,0 +1,12 @@ +import XCTest +import SwiftTreeSitter +import TreeSitterQuery + +final class TreeSitterQueryTests: XCTestCase { + func testCanLoadGrammar() throws { + let parser = Parser() + let language = Language(language: tree_sitter_query()) + XCTAssertNoThrow(try parser.setLanguage(language), + "Error loading Query grammar") + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..052f9ec --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/tree-sitter-grammars/tree-sitter-query + +go 1.22 + +require github.com/tree-sitter/go-tree-sitter v0.23.1 diff --git a/package.json b/package.json index 15bd6d9..3e4d2be 100644 --- a/package.json +++ b/package.json @@ -53,16 +53,5 @@ "test": "make test", "version": "make -s update", "prebuildify": "prebuildify --napi --strip" - }, - "tree-sitter": [ - { - "scope": "source.query", - "injection-regex": "^query$", - "highlights": "queries/query/highlights.scm", - "injections": "queries/query/injections.scm", - "file-types": [ - "scm" - ] - } - ] -} + } +} \ No newline at end of file diff --git a/tree-sitter.json b/tree-sitter.json new file mode 100644 index 0000000..064b104 --- /dev/null +++ b/tree-sitter.json @@ -0,0 +1,40 @@ +{ + "grammars": [ + { + "name": "query", + "camelcase": "Query", + "scope": "source.query", + "path": ".", + "file-types": [ + "scm" + ], + "highlights": "queries/query/highlights.scm", + "injections": "queries/query/injections.scm", + "injection-regex": "^query$" + } + ], + "metadata": { + "version": "0.4.0", + "license": "Apache-2.0", + "description": "TS query grammar for tree-sitter", + "authors": [ + { + "name": "Steven Sojka" + }, + { + "name": "Stephan Seitz" + } + ], + "links": { + "repository": "https://github.com/tree-sitter-grammars/tree-sitter-query" + } + }, + "bindings": { + "c": true, + "go": true, + "node": true, + "python": true, + "rust": true, + "swift": true + } +} \ No newline at end of file