Merge pull request #209198 from erikarvstedt/tree-sitter-build-grammar

tree-sitter: add `buildGrammar` to `passthru`
This commit is contained in:
figsoda 2023-01-23 18:10:27 -05:00 committed by GitHub
commit d811234cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 165 additions and 164 deletions

View File

@ -4,7 +4,7 @@ self: super:
let
generatedGrammars = callPackage ./generated.nix {
buildGrammar = callPackage ../../../../../development/tools/parsing/tree-sitter/grammar.nix { };
inherit (tree-sitter) buildGrammar;
};
generatedDerivations = lib.filterAttrs (_: lib.isDerivation) generatedGrammars;

View File

@ -36,7 +36,7 @@ def generate_grammar(item):
generated = f""" {lang} = buildGrammar {{
language = "{lang}";
version = "{rev[:7]}";
source = """
src = """
generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True)
generated += ";"

View File

@ -51,14 +51,17 @@ let
'' + (lib.concatStrings (lib.mapAttrsToList
(name: grammar: "ln -s ${if grammar ? src then grammar.src else fetchGrammar grammar} $out/${name}\n")
(import ./grammars { inherit lib; }))));
buildGrammar = callPackage ./grammar.nix { };
builtGrammars =
let
change = name: grammar:
callPackage ./grammar.nix { } {
language = if grammar ? language then grammar.language else name;
build = name: grammar:
buildGrammar {
language = grammar.language or name;
inherit version;
source = if grammar ? src then grammar.src else fetchGrammar grammar;
location = if grammar ? location then grammar.location else null;
src = grammar.src or fetchGrammar grammar;
location = grammar.location or null;
};
grammars' = import ./grammars { inherit lib; } // extraGrammars;
grammars = grammars' //
@ -70,7 +73,7 @@ let
{ tree-sitter-markdown = grammars'.tree-sitter-markdown // { location = "tree-sitter-markdown"; }; } //
{ tree-sitter-markdown-inline = grammars'.tree-sitter-markdown // { language = "markdown_inline"; location = "tree-sitter-markdown-inline"; }; };
in
lib.mapAttrs change (grammars);
lib.mapAttrs build (grammars);
# Usage:
# pkgs.tree-sitter.withPlugins (p: [ p.tree-sitter-c p.tree-sitter-java ... ])
@ -142,7 +145,7 @@ rustPlatform.buildRustPackage {
updater = {
inherit update-all-grammars;
};
inherit grammars builtGrammars withPlugins allGrammars;
inherit grammars buildGrammar builtGrammars withPlugins allGrammars;
tests = {
# make sure all grammars build

View File

@ -9,10 +9,8 @@
{
# language name
language
# version of tree-sitter
, version
# source for the language grammar
, source
, src
, location ? null
, generate ? false
, ...
@ -21,7 +19,7 @@
stdenv.mkDerivation ({
pname = "${language}-grammar";
src = source;
inherit src version;
nativeBuildInputs = lib.optionals generate [ nodejs tree-sitter ];
@ -60,4 +58,4 @@ stdenv.mkDerivation ({
fi
runHook postInstall
'';
} // removeAttrs args [ "language" "source" "location" "generate" ])
} // removeAttrs args [ "language" "location" "generate" ])