1 -----------------------------------------------------------------------------
2 -- $Id: GetImports.hs,v 1.3 2000/11/20 11:39:57 sewardj Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module GetImports ( getImports ) where
16 getImports :: String -> ([ModuleName], [ModuleName], ModuleName)
18 = f [{-accum source imports-}] [{-accum normal imports-}]
19 (mkModuleName "Main") (words (clean s))
21 f si ni _ ("module" : me : ws) = f si ni (mkMN me) ws
23 f si ni me ("foreign" : "import" : ws) = f si ni me ws
24 f si ni me ("import" : "{-#" : "SOURCE" : "#-}" : "qualified" : m : ws)
25 = f ((mkMN m):si) ni me ws
26 f si ni me ("import" : "{-#" : "SOURCE" : "#-}" : m : ws)
27 = f ((mkMN m):si) ni me ws
28 f si ni me ("import" : "qualified" : m : ws)
29 = f si ((mkMN m):ni) me ws
30 f si ni me ("import" : m : ws)
31 = f si ((mkMN m):ni) me ws
32 f si ni me (w:ws) = f si ni me ws
33 f si ni me [] = (nub si, nub ni, me)
35 mkMN str = mkModuleName (takeWhile isModId str)
36 isModId c = isAlphaNum c || c `elem` "'_"
38 -- remove literals and comments from a string
39 clean :: String -> String
43 -- running through text we want to keep
45 keep ('"':cs) = dquote cs -- "
46 -- try to eliminate single quotes when they're part of
48 keep (c:'\'':cs) | isAlphaNum c || c == '_' = keep (dropWhile (=='\'') cs)
49 keep ('\'':cs) = squote cs
50 keep ('-':'-':cs) = linecomment cs
51 keep ('{':'-':'#':' ':cs) = "{-# " ++ keep cs
52 keep ('{':'-':cs) = runcomment cs -- -}
53 keep (c:cs) = c : keep cs
55 -- in a double-quoted string
57 dquote ('\\':'\"':cs) = dquote cs -- "
58 dquote ('\\':'\\':cs) = dquote cs
59 dquote ('\"':cs) = keep cs -- "
60 dquote (c:cs) = dquote cs
62 -- in a single-quoted string
64 squote ('\\':'\'':cs) = squote cs
65 squote ('\\':'\\':cs) = squote cs
66 squote ('\'':cs) = keep cs
67 squote (c:cs) = squote cs
71 linecomment ('\n':cs) = '\n':keep cs
72 linecomment (c:cs) = linecomment cs
74 -- in a running comment
76 runcomment ('-':'}':cs) = keep cs
77 runcomment (c:cs) = runcomment cs