1
1
mirror of https://github.com/nmattia/snack.git synced 2024-09-21 00:29:37 +03:00

Merge pull request #10 from nmattia/nm-proper-module-parsing

Implement proper module parsing
This commit is contained in:
Nicolas Mattia 2018-06-09 18:14:39 +02:00 committed by GitHub
commit 7809627dad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 23 deletions

15
snack-lib/Imports.hs Normal file
View File

@ -0,0 +1,15 @@
{-# LANGUAGE ViewPatterns #-}
module Main where
import Language.Haskell.Exts
import System.Environment
main :: IO ()
main = do
[x] <- getArgs
ParseOk mod <- parseFile x
ParseOk (Module _ _ _ imps _) <- parseFile x
let modNames = (\(importModule -> (ModuleName _ name)) -> name) <$> imps
-- this is "JSON"
print modNames

View File

@ -13,6 +13,7 @@
, stdenv
, symlinkJoin
, writeScript
, runCommand
}:
let
# Takes a (string) filepath and creates a derivation for that file (and for
@ -193,29 +194,15 @@ let
# Lists all module dependencies, not limited to modules existing in this
# project
listAllModuleDependenciesJSON = base: modName:
stdenv.mkDerivation
{ name = "module-deps";
src = null;
builder = writeScript "dependencies-json"
''
echo "preparing dependencies"
source $stdenv/setup
# Poor man's module parser
FILTER=$(cat <<'EOF'
s/import\s*\(qualified\|\)\s*\(\S*\)\s*\(.*\)/\2/p;
EOF
)
JSON=$(cat <<'EOF'
s/\(.*\)/"\1"/;
$!s/$/,/;
EOF
)
sed -n "$FILTER" ${singleOutModulePath base modName} \
| (echo "["; sed "$JSON"; echo "]") > $out
echo "done:preparing dependencies"
cat $out
'';
};
let
importParser = runCommand "import-parser"
{ buildInputs =
[ (haskellPackages.ghcWithPackages
(ps: [ ps.haskell-src-exts ]))
];
} "ghc ${./Imports.hs} -o $out" ;
in runCommand "dependencies-json" {}
"${importParser} ${singleOutModulePath base modName} > $out";
# Returns an attribute set where the keys are the module names and the values
# are the '.o's