Merge Haddock comment support from ghc.haddock -- big patch
[ghc-hetmet.git] / compiler / hsSyn / HsImpExp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsImpExp]{Abstract syntax: imports, exports, interfaces}
5
6 \begin{code}
7 module HsImpExp where
8
9 #include "HsVersions.h"
10
11 import Module           ( ModuleName )
12 import HsDoc            ( HsDoc )
13
14 import Outputable
15 import FastString
16 import SrcLoc           ( Located(..) )
17 import Char             ( isAlpha )
18 \end{code}
19
20 %************************************************************************
21 %*                                                                      *
22 \subsection{Import and export declaration lists}
23 %*                                                                      *
24 %************************************************************************
25
26 One per \tr{import} declaration in a module.
27 \begin{code}
28 type LImportDecl name = Located (ImportDecl name)
29
30 data ImportDecl name
31   = ImportDecl    (Located ModuleName)          -- module name
32                   Bool                          -- True <=> {-# SOURCE #-} import
33                   Bool                          -- True => qualified
34                   (Maybe ModuleName)            -- as Module
35                   (Maybe (Bool, [LIE name]))    -- (True => hiding, names)
36 \end{code}
37
38 \begin{code}
39 instance (Outputable name) => Outputable (ImportDecl name) where
40     ppr (ImportDecl mod from qual as spec)
41       = hang (hsep [ptext SLIT("import"), ppr_imp from, 
42                     pp_qual qual, ppr mod, pp_as as])
43              4 (pp_spec spec)
44       where
45         pp_qual False   = empty
46         pp_qual True    = ptext SLIT("qualified")
47
48         pp_as Nothing   = empty
49         pp_as (Just a)  = ptext SLIT("as ") <+> ppr a
50
51         ppr_imp True  = ptext SLIT("{-# SOURCE #-}")
52         ppr_imp False = empty
53
54         pp_spec Nothing = empty
55         pp_spec (Just (False, spec))
56                         = parens (interpp'SP spec)
57         pp_spec (Just (True, spec))
58                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
59
60 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
61 \end{code}
62
63 %************************************************************************
64 %*                                                                      *
65 \subsection{Imported and exported entities}
66 %*                                                                      *
67 %************************************************************************
68
69 \begin{code}
70 type LIE name = Located (IE name)
71
72 data IE name
73   = IEVar               name
74   | IEThingAbs          name             -- Class/Type (can't tell)
75   | IEThingAll          name             -- Class/Type plus all methods/constructors
76   | IEThingWith         name [name]      -- Class/Type plus some methods/constructors
77   | IEModuleContents    ModuleName       -- (Export Only)
78   | IEGroup             Int (HsDoc name) -- Doc section heading
79   | IEDoc               (HsDoc name)     -- Some documentation
80   | IEDocNamed          String           -- Reference to named doc
81 \end{code}
82
83 \begin{code}
84 ieName :: IE name -> name
85 ieName (IEVar n)         = n
86 ieName (IEThingAbs  n)   = n
87 ieName (IEThingWith n _) = n
88 ieName (IEThingAll  n)   = n
89
90 ieNames :: IE a -> [a]
91 ieNames (IEVar            n   ) = [n]
92 ieNames (IEThingAbs       n   ) = [n]
93 ieNames (IEThingAll       n   ) = [n]
94 ieNames (IEThingWith      n ns) = n:ns
95 ieNames (IEModuleContents _   ) = []
96 ieNames (IEGroup          _ _ ) = []
97 ieNames (IEDoc            _   ) = []
98 ieNames (IEDocNamed       _   ) = []        
99 \end{code}
100
101 \begin{code}
102 instance (Outputable name) => Outputable (IE name) where
103     ppr (IEVar          var)    = pprHsVar var
104     ppr (IEThingAbs     thing)  = ppr thing
105     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
106     ppr (IEThingWith thing withs)
107         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
108     ppr (IEModuleContents mod)
109         = ptext SLIT("module") <+> ppr mod
110     ppr (IEGroup n doc)         = text ("<IEGroup: " ++ (show n) ++ ">") 
111     ppr (IEDoc doc)             = ppr doc
112     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
113 \end{code}
114
115 \begin{code}
116 pprHsVar :: Outputable name => name -> SDoc
117 pprHsVar v | isOperator ppr_v = parens ppr_v
118            | otherwise        = ppr_v
119            where
120              ppr_v = ppr v
121
122 isOperator :: SDoc -> Bool
123 isOperator ppr_v 
124   = case showSDocUnqual ppr_v of
125         ('(':s)   -> False              -- (), (,) etc
126         ('[':s)   -> False              -- []
127         ('$':c:s) -> not (isAlpha c)    -- Don't treat $d as an operator
128         (':':c:s) -> not (isAlpha c)    -- Don't treat :T as an operator
129         ('_':s)   -> False              -- Not an operator
130         (c:s)     -> not (isAlpha c)    -- Starts with non-alpha
131         other     -> False
132     -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
133     -- that we don't need NamedThing in the context of all these functions.
134     -- Gruesome, but simple.
135 \end{code}
136