1 -----------------------------------------------------------------------------
2 -- $Id: GetImports.hs,v 1.1 2000/11/16 15:57:05 simonmar Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module GetImports ( getImports ) where
16 getImports :: String -> ([ModuleName], [ModuleName])
18 = let all_imps = (nub . gmiBase . clean) str
19 srcs = concatMap (either unit nil) all_imps
20 normals = concatMap (either nil unit) all_imps
25 -- really get the imports from a de-litted, cpp'd, de-literal'd string
26 -- Lefts are source imports. Rights are normal ones.
27 gmiBase :: String -> [Either ModuleName ModuleName]
31 f ("foreign" : "import" : ws) = f ws
32 f ("import" : "{-#" : "SOURCE" : "#-}" : "qualified" : m : ws)
33 = Left (mkMN m) : f ws
34 f ("import" : "{-#" : "SOURCE" : "#-}" : m : ws)
35 = Left (mkMN m) : f ws
36 f ("import" : "qualified" : m : ws)
37 = Right (mkMN m) : f ws
39 = Right (mkMN m) : f ws
43 mkMN str = mkModuleName (takeWhile isModId str)
44 isModId c = isAlphaNum c || c `elem` "'_"
46 -- remove literals and comments from a string
47 clean :: String -> String
51 -- running through text we want to keep
53 keep ('"':cs) = dquote cs -- "
54 -- try to eliminate single quotes when they're part of
56 keep (c:'\'':cs) | isAlphaNum c || c == '_' = keep (dropWhile (=='\'') cs)
57 keep ('\'':cs) = squote cs
58 keep ('-':'-':cs) = linecomment cs
59 keep ('{':'-':'#':' ':cs) = "{-# " ++ keep cs
60 keep ('{':'-':cs) = runcomment cs -- -}
61 keep (c:cs) = c : keep cs
63 -- in a double-quoted string
65 dquote ('\\':'\"':cs) = dquote cs -- "
66 dquote ('\\':'\\':cs) = dquote cs
67 dquote ('\"':cs) = keep cs -- "
68 dquote (c:cs) = dquote cs
70 -- in a single-quoted string
72 squote ('\\':'\'':cs) = squote cs
73 squote ('\\':'\\':cs) = squote cs
74 squote ('\'':cs) = keep cs
75 squote (c:cs) = squote cs
79 linecomment ('\n':cs) = '\n':keep cs
80 linecomment (c:cs) = linecomment cs
82 -- in a running comment
84 runcomment ('-':'}':cs) = keep cs
85 runcomment (c:cs) = runcomment cs