2021-03-16 09:55:35 +00:00
{ lib
, stdenv
, fetchgit
, fetchFromGitHub
2024-05-15 15:35:15 +00:00
, nix-update-script
2021-03-16 09:55:35 +00:00
, runCommand
, which
, rustPlatform
, emscripten
2020-12-25 13:55:36 +00:00
, Security
2020-04-24 23:36:52 +00:00
, callPackage
2021-03-16 09:55:35 +00:00
, linkFarm
2024-04-21 15:54:59 +00:00
, substitute
2023-04-12 12:48:02 +00:00
, CoreServices
2021-01-05 17:05:55 +00:00
, enableShared ? ! stdenv . hostPlatform . isStatic
, enableStatic ? stdenv . hostPlatform . isStatic
2020-12-25 13:55:36 +00:00
, webUISupport ? false
2022-07-18 16:21:45 +00:00
, extraGrammars ? { }
2024-04-21 15:54:59 +00:00
# tests
, lunarvim
2020-04-24 23:36:52 +00:00
} :
let
# to update:
# 1) change all these hashes
# 2) nix-build -A tree-sitter.updater.update-all-grammars
2022-11-04 12:27:35 +00:00
# 3) Set GITHUB_TOKEN env variable to avoid api rate limit (Use a Personal Access Token from https://github.com/settings/tokens It does not need any permissions)
2022-01-13 20:06:32 +00:00
# 4) run the ./result script that is output by that (it updates ./grammars)
2024-05-15 15:35:15 +00:00
version = " 0 . 2 2 . 5 " ;
hash = " s h a 2 5 6 - f 8 b d p i P N o 5 M 8 a e f T m r Q 2 M Q V g 7 l S 0 Y q 7 j 3 1 2 K 1 s l o r t A = " ;
2020-04-24 23:36:52 +00:00
src = fetchFromGitHub {
owner = " t r e e - s i t t e r " ;
repo = " t r e e - s i t t e r " ;
2021-03-09 03:18:52 +00:00
rev = " v ${ version } " ;
2024-04-21 15:54:59 +00:00
inherit hash ;
2020-04-24 23:36:52 +00:00
fetchSubmodules = true ;
} ;
2023-07-15 17:15:38 +00:00
update-all-grammars = callPackage ./update.nix { } ;
2020-04-24 23:36:52 +00:00
2021-03-16 09:55:35 +00:00
fetchGrammar = ( v : fetchgit { inherit ( v ) url rev sha256 fetchSubmodules ; } ) ;
2020-04-24 23:36:52 +00:00
grammars =
2021-03-16 09:55:35 +00:00
runCommand " g r a m m a r s " { } ( ''
mkdir $ out
'' + ( l i b . c o n c a t S t r i n g s ( l i b . m a p A t t r s T o L i s t
2022-03-30 09:31:56 +00:00
( name : grammar : " l n - s ${ if grammar ? src then grammar . src else fetchGrammar grammar } $ o u t / ${ name } \n " )
2021-12-06 16:07:01 +00:00
( import ./grammars { inherit lib ; } ) ) ) ) ;
2023-02-02 18:25:31 +00:00
buildGrammar = callPackage ./grammar.nix { } ;
2021-03-16 09:55:35 +00:00
builtGrammars =
let
2023-02-02 18:25:31 +00:00
build = name : grammar :
buildGrammar {
language = grammar . language or name ;
2021-03-16 09:55:35 +00:00
inherit version ;
2023-02-16 17:41:37 +00:00
src = grammar . src or ( fetchGrammar grammar ) ;
2023-02-02 18:25:31 +00:00
location = grammar . location or null ;
2023-07-15 17:15:38 +00:00
generate = grammar . generate or false ;
2021-03-16 09:55:35 +00:00
} ;
2022-03-30 09:31:56 +00:00
grammars' = import ./grammars { inherit lib ; } // extraGrammars ;
2021-03-16 09:55:35 +00:00
grammars = grammars' //
2024-05-15 15:35:15 +00:00
{ tree-sitter-latex = grammars' . tree-sitter-latex // { generate = true ; } ; } //
2024-04-21 15:54:59 +00:00
{ tree-sitter-ocaml = grammars' . tree-sitter-ocaml // { location = " g r a m m a r s / o c a m l " ; } ; } //
{ tree-sitter-ocaml-interface = grammars' . tree-sitter-ocaml // { location = " g r a m m a r s / i n t e r f a c e " ; } ; } //
2022-02-10 20:34:41 +00:00
{ tree-sitter-org-nvim = grammars' . tree-sitter-org-nvim // { language = " o r g " ; } ; } //
2021-03-16 09:55:35 +00:00
{ tree-sitter-typescript = grammars' . tree-sitter-typescript // { location = " t y p e s c r i p t " ; } ; } //
2022-07-18 16:21:45 +00:00
{ tree-sitter-tsx = grammars' . tree-sitter-typescript // { location = " t s x " ; } ; } //
{ tree-sitter-markdown = grammars' . tree-sitter-markdown // { location = " t r e e - s i t t e r - m a r k d o w n " ; } ; } //
2023-07-15 17:15:38 +00:00
{ tree-sitter-markdown-inline = grammars' . tree-sitter-markdown // { language = " m a r k d o w n _ i n l i n e " ; location = " t r e e - s i t t e r - m a r k d o w n - i n l i n e " ; } ; } //
2024-04-21 15:54:59 +00:00
{ tree-sitter-php = grammars' . tree-sitter-php // { location = " p h p " ; } ; } //
{ tree-sitter-sql = grammars' . tree-sitter-sql // { generate = true ; } ; } //
2023-07-15 17:15:38 +00:00
{ tree-sitter-wing = grammars' . tree-sitter-wing // { location = " l i b s / t r e e - s i t t e r - w i n g " ; generate = true ; } ; } ;
2021-03-16 09:55:35 +00:00
in
2023-02-02 18:25:31 +00:00
lib . mapAttrs build ( grammars ) ;
2020-04-24 23:36:52 +00:00
2021-04-25 03:57:28 +00:00
# Usage:
# pkgs.tree-sitter.withPlugins (p: [ p.tree-sitter-c p.tree-sitter-java ... ])
#
# or for all grammars:
# pkgs.tree-sitter.withPlugins (_: allGrammars)
# which is equivalent to
# pkgs.tree-sitter.withPlugins (p: builtins.attrValues p)
withPlugins = grammarFn :
let
grammars = grammarFn builtGrammars ;
in
linkFarm " g r a m m a r s "
( map
( drv :
let
name = lib . strings . getName drv ;
in
{
name =
2022-01-26 04:04:25 +00:00
( lib . strings . replaceStrings [ " - " ] [ " _ " ]
2021-09-18 10:52:07 +00:00
( lib . strings . removePrefix " t r e e - s i t t e r - "
( lib . strings . removeSuffix " - g r a m m a r " name ) ) )
2022-03-30 09:31:56 +00:00
+ " . s o " ;
2021-04-25 03:57:28 +00:00
path = " ${ drv } / p a r s e r " ;
}
)
grammars ) ;
allGrammars = builtins . attrValues builtGrammars ;
2021-03-16 09:55:35 +00:00
in
rustPlatform . buildRustPackage {
2020-04-24 23:36:52 +00:00
pname = " t r e e - s i t t e r " ;
2024-02-29 20:09:43 +00:00
inherit src version ;
2024-05-15 15:35:15 +00:00
cargoHash = " s h a 2 5 6 - F k 6 V / k P K c / G L / q 6 Q s a C U r q + Z G 0 R + N 5 F L O p I m 7 7 Y + n 2 A = " ;
2020-04-24 23:36:52 +00:00
2020-12-25 13:55:36 +00:00
buildInputs =
2023-07-15 17:15:38 +00:00
lib . optionals stdenv . isDarwin [ Security CoreServices ] ;
2020-12-25 13:55:36 +00:00
nativeBuildInputs =
[ which ]
++ lib . optionals webUISupport [ emscripten ] ;
2020-09-25 04:45:31 +00:00
2024-04-21 15:54:59 +00:00
patches = lib . optionals webUISupport [
( substitute {
src = ./fix-paths.patch ;
substitutions = [ " - - s u b s t - v a r - b y " " e m c c " " ${ emscripten } / b i n / e m c c " ] ;
} )
] ;
2020-12-25 13:55:36 +00:00
postPatch = lib . optionalString ( ! webUISupport ) ''
# remove web interface
2023-07-15 17:15:38 +00:00
sed - e ' /pub mod playground/d ' \
2020-12-25 13:55:36 +00:00
- i cli/src/lib.rs
2023-07-15 17:15:38 +00:00
sed - e ' s/playground , // ' \
2024-04-21 15:54:59 +00:00
- e ' s/playground : : serve ( & grammar_path . * $ /println ! ( " E R R O R : w e b - u i i s n o t a v a i l a b l e i n t h i s n i x p k g s b u i l d ; e n a b l e t h e w e b U I S u p p o r t " ) ; std::process::exit ( 1 ) ; / ' \
2020-12-25 13:55:36 +00:00
- i cli/src/main.rs
2020-04-24 23:36:52 +00:00
'' ;
# Compile web assembly with emscripten. The --debug flag prevents us from
# minifying the JavaScript; passing it allows us to side-step more Node
# JS dependencies for installation.
2020-12-25 13:55:36 +00:00
preBuild = lib . optionalString webUISupport ''
2023-10-09 19:29:22 +00:00
mkdir - p . emscriptencache
export EM_CACHE = $ ( pwd ) /.emscriptencache
2020-04-24 23:36:52 +00:00
bash ./script/build-wasm - - debug
'' ;
2020-11-12 09:05:59 +00:00
postInstall = ''
PREFIX = $ out make install
$ { lib . optionalString ( ! enableShared ) " r m $ o u t / l i b / * . s o { , . * } " }
$ { lib . optionalString ( ! enableStatic ) " r m $ o u t / l i b / * . a " }
'' ;
2020-04-24 23:36:52 +00:00
# test result: FAILED. 120 passed; 13 failed; 0 ignored; 0 measured; 0 filtered out
doCheck = false ;
passthru = {
updater = {
inherit update-all-grammars ;
} ;
2023-02-02 18:25:31 +00:00
inherit grammars buildGrammar builtGrammars withPlugins allGrammars ;
2021-01-05 17:05:55 +00:00
2024-05-15 15:35:15 +00:00
updateScript = nix-update-script { } ;
2021-01-05 17:05:55 +00:00
tests = {
# make sure all grammars build
builtGrammars = lib . recurseIntoAttrs builtGrammars ;
2024-04-21 15:54:59 +00:00
inherit lunarvim ;
2021-01-05 17:05:55 +00:00
} ;
2020-04-24 23:36:52 +00:00
} ;
2021-03-09 03:18:52 +00:00
meta = with lib ; {
2020-04-24 23:36:52 +00:00
homepage = " h t t p s : / / g i t h u b . c o m / t r e e - s i t t e r / t r e e - s i t t e r " ;
2024-06-20 14:57:18 +00:00
description = " P a r s e r g e n e r a t o r t o o l a n d a n i n c r e m e n t a l p a r s i n g l i b r a r y " ;
2024-04-21 15:54:59 +00:00
mainProgram = " t r e e - s i t t e r " ;
changelog = " h t t p s : / / g i t h u b . c o m / t r e e - s i t t e r / t r e e - s i t t e r / b l o b / v ${ version } / C H A N G E L O G . m d " ;
2020-04-24 23:36:52 +00:00
longDescription = ''
Tree-sitter is a parser generator tool and an incremental parsing library .
It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited .
Tree-sitter aims to be :
* General enough to parse any programming language
* Fast enough to parse on every keystroke in a text editor
* Robust enough to provide useful results even in the presence of syntax errors
* Dependency-free so that the runtime library ( which is written in pure C ) can be embedded in any application
'' ;
2021-03-09 03:18:52 +00:00
license = licenses . mit ;
2023-11-16 04:20:00 +00:00
maintainers = with maintainers ; [ Profpatsch ] ;
2020-04-24 23:36:52 +00:00
} ;
}