@@ -1,11 +1,10 @@
1−module Hackage4Nix.Nix where
1+module Hackage4Nix.Nix ( parseNixExpr, nixExpr2CabalExpr ) where
22
33import Hackage4Nix.Nix.Lex
44import Hackage4Nix.Nix.Par
55import Hackage4Nix.Nix.ErrM
66import Hackage4Nix.Nix.Abs
77import Cabal2Nix.Package
8−import Cabal2Nix.License
98import Data.List
109import Distribution.Package
1110import Distribution.Version
@@ -27,26 +26,52 @@ nixExpr2CabalExpr (Expr _ _ dict) =
2726 buildTools = [ Dependency (PackageName dep) anyVersion | dep <- getRefList "buildTools" dict ]
2827 extraLibs = getRefList "extraLibraries" dict
2928 pkgconfDeps = getRefList "pkgconfDepends" dict
30− platforms = getRefList "platforms" (getDict "meta" dict)
29+ platforms = [getRef "platforms" (getDict "meta" dict)]
3130 maintainers = getRefList "maintainers" (getDict "meta" dict)
3231
32+get' :: String -> Dictionary -> [Value]
3333get' ref (Dictionary attrs) = [ v | Attribute (Reference r) v <- attrs, r == [Ident ref] ]
34+
35+get :: String -> Dictionary -> Value
3436get ref dict
3537 | [v] <- get' ref dict = v
3638 | otherwise = error $ "attribute " ++ ref ++ " doesn't exist in " ++ show dict
37−getBool ref dict = let BoolV b = get ref dict in if b == Yes then True else False
38−getString ref dict = let StringV x = get ref dict in x
39+
40+getBool :: String -> Dictionary -> Bool
41+getBool ref dict
42+ | [] <- get' ref dict = False
43+ | BoolV b <- get ref dict = if b == Yes then True else False
44+
45+getString :: String -> Dictionary -> String
46+getString ref dict
47+ | [] <- get' ref dict = ""
48+ | StringV x <- get ref dict = x
49+ | AttributeV x <- get ref dict = flattenReference x
50+ | otherwise = error $ "getURL: unexpected value of " ++ show ref ++ " in " ++ show dict
51+
52+getDict :: String -> Dictionary -> Dictionary
3953getDict ref dict = let DictionaryV x = get ref dict in x
54+
55+getURL :: String -> Dictionary -> String
4056getURL ref dict
57+ | [] <- get' ref dict = ""
4158 | URLV (URL x) <- get ref dict = x
42− | StringV x <- get ref dict = x
59+ | StringV x <- get ref dict = x
60+ | otherwise = error "getURL: unexpected URL value"
4361
62+getRefList :: String -> Dictionary -> [String]
4463getRefList ref dict =
4564 let vals = get' ref dict
4665 rs = [ refs | ListV vs <- vals, AttributeV refs <- vs ]
4766 in
4867 map flattenReference rs
4968
69+getRef :: String -> Dictionary -> String
70+getRef ref dict
71+ | [] <- get' ref dict = ""
72+ | AttributeV r <- get ref dict = flattenReference r
73+
74+flattenReference :: Reference -> String
5075flattenReference (Reference rs) = concat (intersperse "." [ r | Ident r <- rs ])
5176
5277parseNixExpr :: Monad m => String -> m Expr
@@ -58,10 +83,3 @@ run :: Monad m => ParseFun a -> String -> m a
5883run p s = case p (myLexer s) of
5984 Bad err -> fail err
6085 Ok tree -> return tree
61−
62−runTest = do
63− buf <- readFile "/home/simons/.nix-defexpr/pkgs/development/libraries/haskell/hsemail/default.nix"
64− tree <- parseNixExpr buf
65− print tree
66− let pkg = nixExpr2CabalExpr tree
67− putStr (showNixPkg pkg)