2021-03-16 09:55:35 +00:00
{ lib
, stdenv
, fetchgit
, fetchFromGitHub
, 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
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 ? { }
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-02-29 20:09:43 +00:00
version = " 0 . 2 0 . 9 " ;
sha256 = " s h a 2 5 6 - N x W q p M N w u 5 A j f f w 1 E 2 q 9 K S 4 T g k C H 6 M + c t F y i 9 J p 0 t q Q = " ;
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 } " ;
2020-04-24 23:36:52 +00:00
inherit sha256 ;
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' //
{ tree-sitter-ocaml = grammars' . tree-sitter-ocaml // { location = " o c a m l " ; } ; } //
{ tree-sitter-ocaml-interface = grammars' . tree-sitter-ocaml // { location = " 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 " ; } ; } //
2023-10-09 19:29:22 +00:00
{ tree-sitter-typst = grammars' . tree-sitter-typst // { generate = true ; } ; } //
2022-07-18 16:21:45 +00:00
{ 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 " ; } ; } //
{ 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 ;
cargoLock . lockFile = ./Cargo.lock ;
cargoLock . outputHashes . " c r a n e l i f t - b f o r e s t - 0 . 1 0 2 . 0 " = " s h a 2 5 6 - r J e R b R D r A n K b 8 s 9 8 g N n 1 N T M K u B 8 B 4 a O I 8 F h 6 J e L X 7 a s = " ;
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
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 , // ' \
- e ' s/playground : : serve ( & current_dir . * $ /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
tests = {
# make sure all grammars build
builtGrammars = lib . recurseIntoAttrs builtGrammars ;
} ;
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 " ;
description = " A 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 " ;
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
} ;
}