gnu: Add tree-sitter-grammar-html.
[jackhill/guix/guix.git] / gnu / packages / tree-sitter.scm
CommitLineData
ce7d0d68
PL
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2022 Luis Henrique Gomes Higino <luishenriquegh2701@gmail.com>
3;;; Copyright © 2022 Pierre Langlois <pierre.langlois@gmx.com>
53b00b91
AT
4;;; Copyright © 2022 muradm <mail@muradm.net>
5;;; Copyright © 2022 Aleksandr Vityazev <avityazev@posteo.org>
6;;; Copyright © 2023 Andrew Tropin <andrew@trop.in>
ce7d0d68
PL
7;;;
8;;; This file is part of GNU Guix.
9;;;
10;;; GNU Guix is free software; you can redistribute it and/or modify it
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
15;;; GNU Guix is distributed in the hope that it will be useful, but
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23(define-module (gnu packages tree-sitter)
24 #:use-module ((guix licenses) #:prefix license:)
0cd9bd5d
PL
25 #:use-module (gnu packages crates-graphics)
26 #:use-module (gnu packages crates-io)
ce7d0d68 27 #:use-module (gnu packages icu4c)
0cd9bd5d 28 #:use-module (guix build-system cargo)
ce7d0d68
PL
29 #:use-module (guix build-system gnu)
30 #:use-module (guix gexp)
31 #:use-module (guix git-download)
32 #:use-module (guix packages)
33 #:use-module (guix utils))
34
35(define-public tree-sitter
36 (package
37 (name "tree-sitter")
8f9edc9e 38 (version "0.20.7")
ce7d0d68
PL
39 (source (origin
40 (method git-fetch)
41 (uri (git-reference
42 (url "https://github.com/tree-sitter/tree-sitter")
43 (commit (string-append "v" version))))
44 (file-name (git-file-name name version))
45 (sha256
46 (base32
8f9edc9e 47 "1nv2a2hr22w8ix71b6rkkxv9rfvhvwlmyql0g6lva9qzj4vy50p4"))
ce7d0d68 48 (modules '((guix build utils)))
1378bb53
AT
49 (snippet #~(begin
50 ;; Remove bundled ICU parts
51 (delete-file-recursively "lib/src/unicode")))))
ce7d0d68
PL
52 (build-system gnu-build-system)
53 (inputs (list icu4c))
54 (arguments
55 (list #:phases
1378bb53
AT
56 #~(modify-phases %standard-phases
57 (delete 'configure))
ce7d0d68
PL
58 #:tests? #f ; there are no tests for the runtime library
59 #:make-flags
1378bb53
AT
60 #~(list (string-append "PREFIX=" #$output)
61 (string-append "CC=" #$(cc-for-target)))))
ce7d0d68
PL
62 (home-page "https://tree-sitter.github.io/tree-sitter/")
63 (synopsis "Incremental parsing system for programming tools")
64 (description
65 "Tree-sitter is a parser generator tool and an incremental parsing
66library. It can build a concrete syntax tree for a source file and efficiently
67update the syntax tree as the source file is edited.
68
69Tree-sitter aims to be:
70
71@itemize
72@item General enough to parse any programming language
73@item Fast enough to parse on every keystroke in a text editor
74@item Robust enough to provide useful results even in the presence of syntax errors
75@item Dependency-free so that the runtime library (which is written in pure C)
76can be embedded in any application
77@end itemize
78
1378bb53 79This package includes the @code{libtree-sitter} runtime library.")
ce7d0d68 80 (license license:expat)))
0cd9bd5d
PL
81
82(define-public tree-sitter-cli
83 (package (inherit tree-sitter)
84 (name "tree-sitter-cli")
85 (source (origin
86 (inherit (package-source tree-sitter))
87 (snippet
88 #~(begin
89 ;; Remove the runtime library code and dynamically link to
90 ;; it instead.
91 (delete-file-recursively "lib/src")
92 (delete-file "lib/binding_rust/build.rs")
93 (with-output-to-file "lib/binding_rust/build.rs"
94 (lambda _
95 (format #t "fn main() {~@
96 println!(\"cargo:rustc-link-lib=tree-sitter\");~@
97 }~%")))))))
98 (build-system cargo-build-system)
99 (inputs (list tree-sitter))
100 (arguments
101 (list
102 ;; Running test requires downloading fixtures, see the
103 ;; script/fetch-fixtures script, which fetches grammars. Maybe it make
104 ;; sence to run tests in the grammar's packages?
105 #:tests? #f
106 ;; We're only packaging the CLI program so we do not need to install
107 ;; sources.
108 #:install-source? #f
109 #:cargo-inputs
110 `(("rust-ansi-term" ,rust-ansi-term-0.12)
111 ("rust-anyhow" ,rust-anyhow-1)
112 ("rust-atty" ,rust-atty-0.2)
113 ("rust-clap" ,rust-clap-2)
114 ("rust-difference" ,rust-difference-2)
115 ("rust-dirs" ,rust-dirs-3)
116 ("rust-html-escape" ,rust-html-escape-0.2)
117 ("rust-libloading" ,rust-libloading-0.7)
118 ("rust-rand" ,rust-rand-0.8)
119 ("rust-rustc-hash" ,rust-rustc-hash-1)
120 ("rust-semver" ,rust-semver-1)
121 ("rust-smallbitvec" ,rust-smallbitvec-2)
122 ("rust-thiserror" ,rust-thiserror-1)
123 ("rust-tiny-http" ,rust-tiny-http-0.8)
124 ("rust-toml" ,rust-toml-0.5)
125 ("rust-walkdir" ,rust-walkdir-2)
126 ("rust-webbrowser" ,rust-webbrowser-0.5)
127 ("rust-which" ,rust-which-4))
128 #:cargo-development-inputs
129 `(("rust-pretty-assertions" ,rust-pretty-assertions-0.7))
130 #:phases
131 #~(modify-phases %standard-phases
132 (add-after 'unpack 'delete-cargo-lock
133 (lambda _
134 (delete-file "Cargo.lock")))
135 (replace 'install
136 (lambda* (#:key outputs #:allow-other-keys)
137 (let ((bin (string-append #$output "/bin")))
138 (mkdir-p bin)
139 (install-file "target/release/tree-sitter" bin)))))))
140 (description "Tree-sitter is a parser generator tool and an incremental
141parsing library. It can build a concrete syntax tree for a source file and
142efficiently update the syntax tree as the source file is edited.
143
144Tree-sitter aims to be:
145
146@enumerate
147@item General enough to parse any programming language.
148@item Fast enough to parse on every keystroke in a text editor.
149@item Robust enough to provide useful results even in the presence of syntax
150errors.
151@item Dependency-free so that the runtime library (which is written in pure C)
152can be embedded in any application.
153@end enumerate
154
155This package includes the @command{tree-sitter} command-line tool.")
156 (license license:expat)))
53b00b91
AT
157
158(define* (tree-sitter-grammar
159 language language-for-synopsis version commit hash
160 #:key
161 (repository-url
162 (format #f "https://github.com/tree-sitter/tree-sitter-~a" language))
163 (source-directory ""))
164 (let ((synopsis (string-append language-for-synopsis
165 " grammar for tree-sitter"))
166 (name (string-append "tree-sitter-grammar-" language))
167 (src-dir source-directory)
168 (lib (format #f "libtree-sitter-~a.so" language)))
169 (package
170 (name name)
171 (version version)
172 (home-page repository-url)
173 (source (origin
174 (method git-fetch)
175 (uri (git-reference
176 (url repository-url)
177 (commit commit)))
178 (file-name (git-file-name name version))
179 (sha256 (base32 hash))))
180 (build-system gnu-build-system)
181 (arguments
182 (list
183 #:phases
184 #~(modify-phases %standard-phases
185 (delete 'configure)
186 (replace 'build
187 (lambda _
188 (with-directory-excursion (string-append #$src-dir "src")
189 (let* ((scanner? (or (file-exists? "scanner.c")
190 (file-exists? "scanner.cc")))
191 (CC (if (file-exists? "scanner.cc") "g++" "gcc"))
192 (compile (lambda (f) (invoke CC "-fPIC" "-c" "-I." f)))
193 (link-args `("-fPIC" "-shared" "parser.o"
194 ,@(if scanner? '("scanner.o") '())
195 "-o" ,#$lib)))
196 (invoke "gcc" "-fPIC" "-c" "-I." "parser.c")
197 (for-each
198 (lambda (f) (when (file-exists? f) (compile f)))
199 '("scanner.c" "scanner.cc"))
200 (apply invoke CC link-args)))))
201 (delete 'check)
202 (replace 'install
203 (lambda _
204 (install-file (string-append #$src-dir "src/" #$lib)
205 (string-append #$output "/lib/tree-sitter")))))))
206 (synopsis synopsis)
207 (description (string-append synopsis "."))
208 (license license:expat))))
209
210(define-public tree-sitter-grammar-html
211 (tree-sitter-grammar
212 "html" "HTML"
213 "0.19.0" "v0.19.0"
214 "1hg7vbcy7bir6b8x11v0a4x0glvqnsqc3i2ixiarbxmycbgl3axy"))