Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'grammars.nix')
| -rw-r--r-- | grammars.nix | 135 |
1 files changed, 57 insertions, 78 deletions
diff --git a/grammars.nix b/grammars.nix index 1b4da224..ada14aaf 100644 --- a/grammars.nix +++ b/grammars.nix @@ -1,17 +1,17 @@ -{ - stdenv, - lib, - runCommand, - includeGrammarIf ? _: true, - grammarOverlays ? [], - ... -}: let - languagesConfig = - builtins.fromTOML (builtins.readFile ./languages.toml); - isGitGrammar = grammar: - builtins.hasAttr "source" grammar - && builtins.hasAttr "git" grammar.source - && builtins.hasAttr "rev" grammar.source; +{ stdenv, lib, runCommand, yj }: +let + # HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON + # before parsing + languages-json = runCommand "languages-toml-to-json" { } '' + ${yj}/bin/yj -t < ${./languages.toml} > $out + ''; + 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); isGitHubGrammar = grammar: lib.hasPrefix "https://github.com" grammar.source.git; toGitHubFetcher = url: let match = builtins.match "https://github\.com/([^/]*)/([^/]*)/?" url; @@ -19,53 +19,40 @@ owner = builtins.elemAt match 0; repo = builtins.elemAt match 1; }; - # 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; - 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 - stdenv.mkDerivation { + gitGrammars = builtins.filter isGitGrammar languagesConfig.grammar; + 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 stdenv.mkDerivation rec { # 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; - src = source; - sourceRoot = - if builtins.hasAttr "subpath" grammar.source - then "source/${grammar.source.subpath}" - else "source"; + src = if builtins.hasAttr "subpath" grammar.source then + "${source}/${grammar.source.subpath}" + else + source; + dontUnpack = true; dontConfigure = true; FLAGS = [ - "-Isrc" + "-I${src}/src" "-g" "-O3" "-fPIC" @@ -78,15 +65,17 @@ buildPhase = '' runHook preBuild - 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 + if [[ -e "$src/src/scanner.cc" ]]; then + $CXX -c "$src/src/scanner.cc" -o scanner.o $FLAGS + elif [[ -e "$src/src/scanner.c" ]]; then + $CC -c "$src/src/scanner.c" -o scanner.o $FLAGS fi - $CC -c src/parser.c -o parser.o $FLAGS + $CC -c "$src/src/parser.c" -o parser.o $FLAGS $CXX -shared -o $NAME.so *.o + ls -al + runHook postBuild ''; @@ -104,24 +93,14 @@ runHook postFixup ''; }; - grammarsToBuild = builtins.filter includeGrammarIf gitGrammars; - builtGrammars = - builtins.map (grammar: { - inherit (grammar) name; - value = buildGrammar grammar; - }) - grammarsToBuild; - extensibleGrammars = - lib.makeExtensible (self: builtins.listToAttrs builtGrammars); - overlaidGrammars = - 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) overlaidGrammars); -in - runCommand "consolidated-helix-grammars" {} '' - mkdir -p $out - ${builtins.concatStringsSep "\n" grammarLinks} - '' + builtGrammars = builtins.map (grammar: { + inherit (grammar) name; + artifact = buildGrammar grammar; + }) gitGrammars; + grammarLinks = builtins.map (grammar: + "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so") + builtGrammars; +in runCommand "consolidated-helix-grammars" { } '' + mkdir -p $out + ${builtins.concatStringsSep "\n" grammarLinks} +'' |