2020-04-24 23:36:52 +00:00
|
|
|
{ stdenv
|
2022-11-21 17:40:18 +00:00
|
|
|
, nodejs
|
2020-04-24 23:36:52 +00:00
|
|
|
, tree-sitter
|
2021-06-28 23:13:55 +00:00
|
|
|
, lib
|
2020-12-25 13:55:36 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# Build a parser grammar and put the resulting shared object in `$out/parser`
|
|
|
|
|
|
|
|
{
|
|
|
|
# language name
|
|
|
|
language
|
2020-04-24 23:36:52 +00:00
|
|
|
, version
|
2023-02-02 18:25:31 +00:00
|
|
|
, src
|
2021-03-16 09:55:35 +00:00
|
|
|
, location ? null
|
2022-11-21 17:40:18 +00:00
|
|
|
, generate ? false
|
|
|
|
, ...
|
|
|
|
}@args:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
stdenv.mkDerivation ({
|
2021-01-05 17:05:55 +00:00
|
|
|
pname = "${language}-grammar";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-02-02 18:25:31 +00:00
|
|
|
inherit src version;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
nativeBuildInputs = lib.optionals generate [ nodejs tree-sitter ];
|
2021-07-21 07:28:18 +00:00
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
CFLAGS = [ "-Isrc" "-O2" ];
|
|
|
|
CXXFLAGS = [ "-Isrc" "-O2" ];
|
2021-12-06 16:07:01 +00:00
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
stripDebugList = [ "parser" ];
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
configurePhase = lib.optionalString (location != null) ''
|
2022-11-21 17:40:18 +00:00
|
|
|
cd ${location}
|
2023-07-15 17:15:38 +00:00
|
|
|
'' + lib.optionalString generate ''
|
|
|
|
tree-sitter generate
|
2022-11-21 17:40:18 +00:00
|
|
|
'';
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
# When both scanner.{c,cc} exist, we should not link both since they may be the same but in
|
|
|
|
# different languages. Just randomly prefer C++ if that happens.
|
2020-04-24 23:36:52 +00:00
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
2022-11-21 17:40:18 +00:00
|
|
|
if [[ -e src/scanner.cc ]]; then
|
|
|
|
$CXX -fPIC -c src/scanner.cc -o scanner.o $CXXFLAGS
|
|
|
|
elif [[ -e src/scanner.c ]]; then
|
|
|
|
$CC -fPIC -c src/scanner.c -o scanner.o $CFLAGS
|
2021-03-09 03:18:52 +00:00
|
|
|
fi
|
2022-11-21 17:40:18 +00:00
|
|
|
$CC -fPIC -c src/parser.c -o parser.o $CFLAGS
|
2023-01-11 07:51:40 +00:00
|
|
|
rm -rf parser
|
2021-12-06 16:07:01 +00:00
|
|
|
$CXX -shared -o parser *.o
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postBuild
|
|
|
|
'';
|
2021-12-06 16:07:01 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
mkdir $out
|
|
|
|
mv parser $out/
|
2022-11-21 17:40:18 +00:00
|
|
|
if [[ -d queries ]]; then
|
|
|
|
cp -r queries $out
|
2022-09-09 14:08:57 +00:00
|
|
|
fi
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postInstall
|
|
|
|
'';
|
2023-02-02 18:25:31 +00:00
|
|
|
} // removeAttrs args [ "language" "location" "generate" ])
|