2021-03-16 09:55:35 +00:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
|
|
|
, fetchgit
|
|
|
|
, fetchFromGitHub
|
|
|
|
, fetchurl
|
|
|
|
, writeShellScript
|
|
|
|
, runCommand
|
|
|
|
, which
|
|
|
|
, formats
|
|
|
|
, rustPlatform
|
|
|
|
, jq
|
|
|
|
, nix-prefetch-git
|
|
|
|
, xe
|
|
|
|
, curl
|
|
|
|
, 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
|
2020-12-25 13:55:36 +00:00
|
|
|
|
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-03-30 09:31:56 +00:00
|
|
|
, extraGrammars ? {}
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# TODO: move to carnix or https://github.com/kolloch/crate2nix
|
|
|
|
let
|
|
|
|
# to update:
|
|
|
|
# 1) change all these hashes
|
|
|
|
# 2) nix-build -A tree-sitter.updater.update-all-grammars
|
2022-01-13 20:06:32 +00:00
|
|
|
# 3) OPTIONAL: Set GITHUB_TOKEN env variable to avoid api rate limit
|
|
|
|
# 4) run the ./result script that is output by that (it updates ./grammars)
|
2022-03-10 19:12:11 +00:00
|
|
|
version = "0.20.6";
|
|
|
|
sha256 = "sha256-zaxy8VCfJKK8NtfuFFojmmP5a19FP1zO/eB5q1EoQPw=";
|
|
|
|
cargoSha256 = "sha256-sOOhzm2nz+HC6dvT+8hj/wh19o+OB2zQ6Uz+H89txSA=";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "tree-sitter";
|
|
|
|
repo = "tree-sitter";
|
2021-03-09 03:18:52 +00:00
|
|
|
rev = "v${version}";
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit sha256;
|
|
|
|
fetchSubmodules = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
update-all-grammars = import ./update.nix {
|
2021-01-05 17:05:55 +00:00
|
|
|
inherit writeShellScript nix-prefetch-git curl jq xe src formats lib;
|
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 "grammars" { } (''
|
|
|
|
mkdir $out
|
|
|
|
'' + (lib.concatStrings (lib.mapAttrsToList
|
2022-03-30 09:31:56 +00:00
|
|
|
(name: grammar: "ln -s ${if grammar ? src then grammar.src else fetchGrammar grammar} $out/${name}\n")
|
2021-12-06 16:07:01 +00:00
|
|
|
(import ./grammars { inherit lib; }))));
|
2021-03-16 09:55:35 +00:00
|
|
|
builtGrammars =
|
|
|
|
let
|
|
|
|
change = name: grammar:
|
|
|
|
callPackage ./grammar.nix { } {
|
2022-02-10 20:34:41 +00:00
|
|
|
language = if grammar ? language then grammar.language else name;
|
2021-03-16 09:55:35 +00:00
|
|
|
inherit version;
|
2022-03-30 09:31:56 +00:00
|
|
|
source = if grammar ? src then grammar.src else fetchGrammar grammar;
|
2021-03-16 09:55:35 +00:00
|
|
|
location = if grammar ? location then grammar.location else null;
|
|
|
|
};
|
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 = "ocaml"; }; } //
|
|
|
|
{ tree-sitter-ocaml-interface = grammars'.tree-sitter-ocaml // { location = "interface"; }; } //
|
2022-02-10 20:34:41 +00:00
|
|
|
{ tree-sitter-org-nvim = grammars'.tree-sitter-org-nvim // { language = "org"; }; } //
|
2021-03-16 09:55:35 +00:00
|
|
|
{ tree-sitter-typescript = grammars'.tree-sitter-typescript // { location = "typescript"; }; } //
|
|
|
|
{ tree-sitter-tsx = grammars'.tree-sitter-typescript // { location = "tsx"; }; };
|
|
|
|
in
|
2022-03-30 09:31:56 +00:00
|
|
|
lib.mapAttrs change (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 "grammars"
|
|
|
|
(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 "tree-sitter-"
|
|
|
|
(lib.strings.removeSuffix "-grammar" name)))
|
2022-03-30 09:31:56 +00:00
|
|
|
+ ".so";
|
2021-04-25 03:57:28 +00:00
|
|
|
path = "${drv}/parser";
|
|
|
|
}
|
|
|
|
)
|
|
|
|
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 = "tree-sitter";
|
|
|
|
inherit src version cargoSha256;
|
|
|
|
|
2020-12-25 13:55:36 +00:00
|
|
|
buildInputs =
|
|
|
|
lib.optionals stdenv.isDarwin [ Security ];
|
|
|
|
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
|
|
|
|
sed -e '/pub mod web_ui/d' \
|
|
|
|
-i cli/src/lib.rs
|
|
|
|
sed -e 's/web_ui,//' \
|
|
|
|
-e 's/web_ui::serve(¤t_dir.*$/println!("ERROR: web-ui is not available in this nixpkgs build; enable the webUISupport"); std::process::exit(1);/' \
|
|
|
|
-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 ''
|
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) "rm $out/lib/*.so{,.*}"}
|
|
|
|
${lib.optionalString (!enableStatic) "rm $out/lib/*.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;
|
|
|
|
};
|
2021-04-25 03:57:28 +00:00
|
|
|
inherit grammars 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 = "https://github.com/tree-sitter/tree-sitter";
|
|
|
|
description = "A parser generator tool and an incremental parsing library";
|
|
|
|
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;
|
|
|
|
maintainers = with maintainers; [ Profpatsch ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|