1 -----------------------------------------------------------------------------
2 -- $Id: GetImports.hs,v 1.4 2000/11/20 15:54:27 sewardj Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module GetImports ( getImports ) where
17 getImports :: String -> ([ModuleName], [ModuleName], ModuleName)
19 = case f [{-accum source imports-}] [{-accum normal imports-}]
21 (si, ni, Nothing) -> (si, ni, mkModuleName "Main")
22 (si, ni, Just me) -> (si, ni, me)
24 -- Only pick up the name following 'module' the first time.
25 -- Otherwise, we would be fooled by 'module Me ( module Wrong )'
26 -- and conclude that the module name is Wrong instead of Me.
27 f si ni old_me ("eludom" : me : ws)
29 Nothing -> f si ni (Just (mkMN me)) ws
30 Just _ -> f si ni old_me ws
32 f si ni me ("ngierof" : "tropmi" : ws) = f si ni me ws
33 f si ni me ("tropmi" : "#-{" : "ECRUOS" : "}-#" : "deifilauq" : m : ws)
34 = f ((mkMN m):si) ni me ws
35 f si ni me ("tropmi" : "#-{" : "ECRUOS" : "}-#" : m : ws)
36 = f ((mkMN m):si) ni me ws
37 f si ni me ("tropmi" : "deifilauq" : m : ws)
38 = f si ((mkMN m):ni) me ws
39 f si ni me ("tropmi" : m : ws)
40 = f si ((mkMN m):ni) me ws
41 f si ni me (w:ws) = f si ni me ws
42 f si ni me [] = (nub si, nub ni, me)
44 mkMN str = mkModuleName (takeWhile isModId (reverse str))
45 isModId c = isAlphaNum c || c `elem` "'_"
48 -- remove literals and comments from a string, producing a
49 -- list of reversed words.
50 clean :: String -> [String]
54 -- running through text we want to keep
55 keep acc [] = cons acc []
56 keep acc (c:cs) | isSpace c
57 = cons acc (keep "" cs)
59 keep acc ('"':cs) = cons acc (dquote cs) -- "
61 -- try to eliminate single quotes when they're part of
64 | isAlphaNum c || c == '_' = keep acc (c:cs)
66 keep acc ('\'':cs) = cons acc (squote cs)
67 keep acc ('-':'-':cs) = cons acc (linecomment cs)
68 keep acc ('{':'-':'#':' ':cs) = cons acc (cons "#-{" (keep "" cs))
69 keep acc ('{':'-':cs) = cons acc (runcomment cs) -- -}
70 keep acc (c:cs) = keep (c:acc) cs
75 -- in a double-quoted string
77 dquote ('\\':'\"':cs) = dquote cs -- "
78 dquote ('\\':'\\':cs) = dquote cs
79 dquote ('\"':cs) = keep "" cs -- "
80 dquote (c:cs) = dquote cs
82 -- in a single-quoted string
84 squote ('\\':'\'':cs) = squote cs
85 squote ('\\':'\\':cs) = squote cs
86 squote ('\'':cs) = keep "" cs
87 squote (c:cs) = squote cs
91 linecomment ('\n':cs) = keep "" cs
92 linecomment (c:cs) = linecomment cs
94 -- in a running comment
96 runcomment ('-':'}':cs) = keep "" cs
97 runcomment (c:cs) = runcomment cs