2022-06-06 21:24:52 +02:00
|
|
|
{
|
|
|
|
stdenv,
|
|
|
|
lib,
|
|
|
|
runCommandLocal,
|
2022-09-27 15:01:58 +02:00
|
|
|
runCommand,
|
2022-06-06 21:24:52 +02:00
|
|
|
yj,
|
2022-07-27 19:52:07 +02:00
|
|
|
includeGrammarIf ? _: true,
|
2023-11-09 09:56:57 +01:00
|
|
|
grammarOverlays ? [],
|
2022-07-27 19:52:07 +02:00
|
|
|
...
|
2022-06-06 21:24:52 +02:00
|
|
|
}: let
|
2022-02-15 04:50:08 +01:00
|
|
|
# HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON
|
|
|
|
# before parsing
|
2022-06-06 21:24:52 +02:00
|
|
|
languages-json = runCommandLocal "languages-toml-to-json" {} ''
|
2022-02-15 04:50:08 +01:00
|
|
|
${yj}/bin/yj -t < ${./languages.toml} > $out
|
|
|
|
'';
|
2022-06-06 21:24:52 +02:00
|
|
|
languagesConfig =
|
|
|
|
if lib.versionAtLeast builtins.nixVersion "2.6.0"
|
|
|
|
then builtins.fromTOML (builtins.readFile ./languages.toml)
|
|
|
|
else builtins.fromJSON (builtins.readFile (builtins.toPath languages-json));
|
|
|
|
isGitGrammar = grammar:
|
|
|
|
builtins.hasAttr "source" grammar
|
|
|
|
&& builtins.hasAttr "git" grammar.source
|
|
|
|
&& builtins.hasAttr "rev" grammar.source;
|
2022-03-28 23:50:18 +02:00
|
|
|
isGitHubGrammar = grammar: lib.hasPrefix "https://github.com" grammar.source.git;
|
|
|
|
toGitHubFetcher = url: let
|
|
|
|
match = builtins.match "https://github\.com/([^/]*)/([^/]*)/?" url;
|
|
|
|
in {
|
|
|
|
owner = builtins.elemAt match 0;
|
|
|
|
repo = builtins.elemAt match 1;
|
|
|
|
};
|
2024-01-14 15:11:40 +01:00
|
|
|
# If `use-grammars.only` is set, use only those grammars.
|
|
|
|
# If `use-grammars.except` is set, use all other grammars.
|
|
|
|
# Otherwise use all grammars.
|
|
|
|
useGrammar = grammar:
|
|
|
|
if languagesConfig?use-grammars.only then
|
|
|
|
builtins.elem grammar.name languagesConfig.use-grammars.only
|
|
|
|
else if languagesConfig?use-grammars.except then
|
|
|
|
!(builtins.elem grammar.name languagesConfig.use-grammars.except)
|
|
|
|
else true;
|
|
|
|
grammarsToUse = builtins.filter useGrammar languagesConfig.grammar;
|
|
|
|
gitGrammars = builtins.filter isGitGrammar grammarsToUse;
|
2022-06-06 21:24:52 +02:00
|
|
|
buildGrammar = grammar: let
|
|
|
|
gh = toGitHubFetcher grammar.source.git;
|
|
|
|
sourceGit = builtins.fetchTree {
|
|
|
|
type = "git";
|
|
|
|
url = grammar.source.git;
|
|
|
|
rev = grammar.source.rev;
|
|
|
|
ref = grammar.source.ref or "HEAD";
|
|
|
|
shallow = true;
|
|
|
|
};
|
|
|
|
sourceGitHub = builtins.fetchTree {
|
|
|
|
type = "github";
|
|
|
|
owner = gh.owner;
|
|
|
|
repo = gh.repo;
|
|
|
|
inherit (grammar.source) rev;
|
|
|
|
};
|
|
|
|
source =
|
|
|
|
if isGitHubGrammar grammar
|
|
|
|
then sourceGitHub
|
|
|
|
else sourceGit;
|
|
|
|
in
|
2023-11-09 09:56:57 +01:00
|
|
|
stdenv.mkDerivation {
|
2022-02-15 04:50:08 +01:00
|
|
|
# see https://github.com/NixOS/nixpkgs/blob/fbdd1a7c0bc29af5325e0d7dd70e804a972eb465/pkgs/development/tools/parsing/tree-sitter/grammar.nix
|
|
|
|
|
|
|
|
pname = "helix-tree-sitter-${grammar.name}";
|
|
|
|
version = grammar.source.rev;
|
|
|
|
|
2023-11-09 09:56:57 +01:00
|
|
|
src = source;
|
|
|
|
sourceRoot = if builtins.hasAttr "subpath" grammar.source then
|
|
|
|
"source/${grammar.source.subpath}"
|
|
|
|
else
|
|
|
|
"source";
|
2022-02-15 04:50:08 +01:00
|
|
|
|
|
|
|
dontConfigure = true;
|
|
|
|
|
|
|
|
FLAGS = [
|
2023-11-09 09:56:57 +01:00
|
|
|
"-Isrc"
|
2022-02-15 04:50:08 +01:00
|
|
|
"-g"
|
|
|
|
"-O3"
|
|
|
|
"-fPIC"
|
|
|
|
"-fno-exceptions"
|
|
|
|
"-Wl,-z,relro,-z,now"
|
|
|
|
];
|
|
|
|
|
|
|
|
NAME = grammar.name;
|
|
|
|
|
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
|
|
|
|
2023-11-09 09:56:57 +01:00
|
|
|
if [[ -e src/scanner.cc ]]; then
|
|
|
|
$CXX -c src/scanner.cc -o scanner.o $FLAGS
|
|
|
|
elif [[ -e src/scanner.c ]]; then
|
|
|
|
$CC -c src/scanner.c -o scanner.o $FLAGS
|
2022-02-15 04:50:08 +01:00
|
|
|
fi
|
|
|
|
|
2023-11-09 09:56:57 +01:00
|
|
|
$CC -c src/parser.c -o parser.o $FLAGS
|
2022-02-15 04:50:08 +01:00
|
|
|
$CXX -shared -o $NAME.so *.o
|
|
|
|
|
|
|
|
ls -al
|
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
mkdir $out
|
|
|
|
mv $NAME.so $out/
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Strip failed on darwin: strip: error: symbols referenced by indirect symbol table entries that can't be stripped
|
|
|
|
fixupPhase = lib.optionalString stdenv.isLinux ''
|
|
|
|
runHook preFixup
|
|
|
|
$STRIP $out/$NAME.so
|
|
|
|
runHook postFixup
|
|
|
|
'';
|
|
|
|
};
|
2022-07-27 19:52:07 +02:00
|
|
|
grammarsToBuild = builtins.filter includeGrammarIf gitGrammars;
|
2023-11-09 09:56:57 +01:00
|
|
|
builtGrammars = builtins.map (grammar: {
|
|
|
|
inherit (grammar) name;
|
|
|
|
value = buildGrammar grammar;
|
|
|
|
}) grammarsToBuild;
|
|
|
|
extensibleGrammars =
|
|
|
|
lib.makeExtensible (self: builtins.listToAttrs builtGrammars);
|
|
|
|
overlayedGrammars = lib.pipe extensibleGrammars
|
|
|
|
(builtins.map (overlay: grammar: grammar.extend overlay) grammarOverlays);
|
|
|
|
grammarLinks = lib.mapAttrsToList
|
|
|
|
(name: artifact: "ln -s ${artifact}/${name}.so $out/${name}.so")
|
|
|
|
(lib.filterAttrs (n: v: lib.isDerivation v) overlayedGrammars);
|
2022-06-06 21:24:52 +02:00
|
|
|
in
|
2022-09-27 15:01:58 +02:00
|
|
|
runCommand "consolidated-helix-grammars" {} ''
|
2022-06-06 21:24:52 +02:00
|
|
|
mkdir -p $out
|
|
|
|
${builtins.concatStringsSep "\n" grammarLinks}
|
|
|
|
''
|