@@ -19,10 +19,12 @@
(define-module (gnu packages tree-sitter)
#:use-module ((guix licenses) #:prefix license:)
#:use-module (guix build-system cargo)
+ #:use-module (guix build-system node)
#:use-module (guix git-download)
#:use-module (guix packages)
#:use-module (gnu packages crates-io)
- #:use-module (gnu packages crates-graphics))
+ #:use-module (gnu packages crates-graphics)
+ #:use-module (gnu packages node-xyz))
(define-public tree-sitter
(package
@@ -96,3 +98,62 @@ can be embedded in any application.
This package includes the @command{tree-sitter} tool as well as the runtime
library.")
(license license:expat)))
+
+(define-public tree-sitter-c
+ (package
+ (name "tree-sitter-c")
+ (version "0.19.0")
+ (source
+ (origin
+ (method git-fetch)
+ (uri (git-reference
+ (url "https://github.com/tree-sitter/tree-sitter-c")
+ (commit (string-append "v" version))))
+ (file-name (git-file-name name version))
+ (sha256
+ (base32
+ "1diys8yigvhm4ppbmp3a473yxjg2d5lk11y0ay7qprcz7233lakv"))))
+ (build-system node-build-system)
+ (native-inputs
+ `(("tree-sitter" ,tree-sitter)
+ ("node-nan" ,node-nan)))
+ (arguments
+ `(#:phases
+ (modify-phases %standard-phases
+ ;; tree-sitter-cli is listed as a Node.js dependency, however the
+ ;; node tree-sitter-cli package is just a wrapper which downloads a
+ ;; tree-sitter binary, see
+ ;; https://github.com/tree-sitter/tree-sitter/tree/master/cli/npm
+ ;; Instead we remove it as a dependency so that we can use our own
+ ;; tree-sitter package.
+ (add-after 'unpack 'fix-configure
+ (lambda _
+ (with-atomic-file-replacement "package.json"
+ (lambda (in out)
+ (use-modules ((guix build json)))
+ (let ((package-meta (read-json in)))
+ (assoc-remove! (assoc-ref package-meta "devDependencies")
+ "tree-sitter-cli")
+ (write-json package-meta out))))))
+ (add-before 'build 'set-cc
+ (lambda _
+ (setenv "CC" "gcc")))
+ (add-before 'build 'make-files-writable
+ (lambda _
+ (for-each make-file-writable (find-files "src" ".*"))))
+ (add-after 'install 'install-native-lib
+ (lambda* (#:key outputs #:allow-other-keys)
+ (let ((lib (string-append (assoc-ref outputs "out")
+ "/lib/tree-sitter")))
+ ;; Invoking `tree-sitter test' loads the grammar which
+ ;; compiles it to a .so binary that we install.
+ (invoke "tree-sitter" "test")
+ (mkdir-p lib)
+ (copy-recursively (string-append (getenv "HOME")
+ "/.tree-sitter/bin")
+ lib)))))))
+ (home-page "https://github.com/tree-sitter/tree-sitter-c")
+ (synopsis "Tree-sitter C grammar")
+ (description
+ "This package provides a C grammar for the Tree-sitter library.")
+ (license license:expat)))