git.iko.soy/typeable/cabal2nix
on master help sign in
nixExpr2CabalExpr: added lots of hacks so that the function succeeds parsing all of Nixpkgs
427d3e9Peter Simons
src/Hackage4Nix/Nix.hs+31−13
@@ -1,11 +1,10 @@
1−module Hackage4Nix.Nix where
1+module Hackage4Nix.Nix ( parseNixExpr, nixExpr2CabalExpr ) where
2
2
3import Hackage4Nix.Nix.Lex
3import Hackage4Nix.Nix.Lex
4import Hackage4Nix.Nix.Par
4import Hackage4Nix.Nix.Par
5import Hackage4Nix.Nix.ErrM
5import Hackage4Nix.Nix.ErrM
6import Hackage4Nix.Nix.Abs
6import Hackage4Nix.Nix.Abs
7import Cabal2Nix.Package
7import Cabal2Nix.Package
8−import Cabal2Nix.License
9import Data.List
8import Data.List
10import Distribution.Package
9import Distribution.Package
11import Distribution.Version
10import Distribution.Version
@@ -27,26 +26,52 @@ nixExpr2CabalExpr (Expr _ _ dict) =
27 buildTools = [ Dependency (PackageName dep) anyVersion | dep <- getRefList "buildTools" dict ]
26 buildTools = [ Dependency (PackageName dep) anyVersion | dep <- getRefList "buildTools" dict ]
28 extraLibs = getRefList "extraLibraries" dict
27 extraLibs = getRefList "extraLibraries" dict
29 pkgconfDeps = getRefList "pkgconfDepends" dict
28 pkgconfDeps = getRefList "pkgconfDepends" dict
30− platforms = getRefList "platforms" (getDict "meta" dict)
29+ platforms = [getRef "platforms" (getDict "meta" dict)]
31 maintainers = getRefList "maintainers" (getDict "meta" dict)
30 maintainers = getRefList "maintainers" (getDict "meta" dict)
32
31
32+get' :: String -> Dictionary -> [Value]
33get' ref (Dictionary attrs) = [ v | Attribute (Reference r) v <- attrs, r == [Ident ref] ]
33get' ref (Dictionary attrs) = [ v | Attribute (Reference r) v <- attrs, r == [Ident ref] ]
34+
35+get :: String -> Dictionary -> Value
34get ref dict
36get ref dict
35 | [v] <- get' ref dict = v
37 | [v] <- get' ref dict = v
36 | otherwise = error $ "attribute " ++ ref ++ " doesn't exist in " ++ show dict
38 | otherwise = error $ "attribute " ++ ref ++ " doesn't exist in " ++ show dict
39+
40+getBool :: String -> Dictionary -> Bool
41+getBool ref dict
42+ | [] <- get' ref dict = False
37−getBool ref dict = let BoolV b = get ref dict in if b == Yes then True else False
43+ | BoolV b <- get ref dict = if b == Yes then True else False
44+
45+getString :: String -> Dictionary -> String
38−getString ref dict = let StringV x = get ref dict in x
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
39getDict ref dict = let DictionaryV x = get ref dict in x
53getDict ref dict = let DictionaryV x = get ref dict in x
54+
55+getURL :: String -> Dictionary -> String
40getURL ref dict
56getURL ref dict
57+ | [] <- get' ref dict = ""
41 | URLV (URL x) <- get ref dict = x
58 | 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"
43
61
62+getRefList :: String -> Dictionary -> [String]
44getRefList ref dict =
63getRefList ref dict =
45 let vals = get' ref dict
64 let vals = get' ref dict
46 rs = [ refs | ListV vs <- vals, AttributeV refs <- vs ]
65 rs = [ refs | ListV vs <- vals, AttributeV refs <- vs ]
47 in
66 in
48 map flattenReference rs
67 map flattenReference rs
49
68
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
50flattenReference (Reference rs) = concat (intersperse "." [ r | Ident r <- rs ])
75flattenReference (Reference rs) = concat (intersperse "." [ r | Ident r <- rs ])
51
76
52parseNixExpr :: Monad m => String -> m Expr
77parseNixExpr :: Monad m => String -> m Expr
@@ -58,10 +83,3 @@ run :: Monad m => ParseFun a -> String -> m a
58run p s = case p (myLexer s) of
83run p s = case p (myLexer s) of
59 Bad err -> fail err
84 Bad err -> fail err
60 Ok tree -> return tree
85 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)