2020-04-24 23:36:52 +00:00
|
|
|
{ stdenv
|
|
|
|
, 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
|
|
|
|
# version of tree-sitter
|
2020-04-24 23:36:52 +00:00
|
|
|
, version
|
2020-12-25 13:55:36 +00:00
|
|
|
# source for the language grammar
|
2020-04-24 23:36:52 +00:00
|
|
|
, source
|
2021-03-16 09:55:35 +00:00
|
|
|
, location ? null
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
stdenv.mkDerivation rec {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-01-05 17:05:55 +00:00
|
|
|
pname = "${language}-grammar";
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit version;
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
src = if location == null then source else "${source}/${location}";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
buildInputs = [ tree-sitter ];
|
|
|
|
|
|
|
|
dontUnpack = true;
|
2021-07-21 07:28:18 +00:00
|
|
|
dontConfigure = true;
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
CFLAGS = [ "-I${src}/src" "-O2" ];
|
|
|
|
CXXFLAGS = [ "-I${src}/src" "-O2" ];
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
stripDebugList = [ "parser" ];
|
|
|
|
|
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
|
2021-12-06 16:07:01 +00:00
|
|
|
if [[ -e "$src/src/scanner.cc" ]]; then
|
2022-09-09 14:08:57 +00:00
|
|
|
$CXX -fPIC -c "$src/src/scanner.cc" -o scanner.o $CXXFLAGS
|
2021-12-06 16:07:01 +00:00
|
|
|
elif [[ -e "$src/src/scanner.c" ]]; then
|
2022-09-09 14:08:57 +00:00
|
|
|
$CC -fPIC -c "$src/src/scanner.c" -o scanner.o $CFLAGS
|
2021-03-09 03:18:52 +00:00
|
|
|
fi
|
2022-09-09 14:08:57 +00:00
|
|
|
$CC -fPIC -c "$src/src/parser.c" -o parser.o $CFLAGS
|
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-09-09 14:08:57 +00:00
|
|
|
if [[ -d "$src/queries" ]]; then
|
|
|
|
cp -r $src/queries $out/
|
|
|
|
fi
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
}
|