[project @ 2004-11-03 01:10:59 by igloo]
[ghc.git] / libraries / template-haskell / Language / Haskell / TH / Ppr.hs
1 -- TH.Ppr contains a prettyprinter for the
2 -- Template Haskell datatypes
3
4 module Language.Haskell.TH.Ppr where
5 -- All of the exports from this module should
6 -- be "public" functions. The main module TH
7 -- re-exports them all.
8
9 import Text.PrettyPrint.HughesPJ (render)
10 import Language.Haskell.TH.PprLib
11 import Language.Haskell.TH.Syntax
12 import Data.Char ( toLower )
13
14 nestDepth :: Int
15 nestDepth = 4
16
17 type Precedence = Int
18 appPrec, opPrec, noPrec :: Precedence
19 appPrec = 2 -- Argument of a function application
20 opPrec = 1 -- Argument of an infix operator
21 noPrec = 0 -- Others
22
23 parensIf :: Bool -> Doc -> Doc
24 parensIf True d = parens d
25 parensIf False d = d
26
27 ------------------------------
28
29 pprint :: Ppr a => a -> String
30 pprint x = render $ to_HPJ_Doc $ ppr x
31
32 class Ppr a where
33 ppr :: a -> Doc
34 ppr_list :: [a] -> Doc
35 ppr_list = vcat . map ppr
36
37 instance Ppr a => Ppr [a] where
38 ppr x = ppr_list x
39
40 ------------------------------
41 instance Ppr Name where
42 ppr v = pprName v -- text (show v)
43
44 ------------------------------
45 instance Ppr Info where
46 ppr (ClassI d) = ppr d
47 ppr (TyConI d) = ppr d
48 ppr (ClassOpI v ty cls fix)
49 = text "Class op from" <+> ppr cls <> colon <+>
50 vcat [ppr_sig v ty, pprFixity v fix]
51 ppr (DataConI v ty tc fix)
52 = text "Constructor from" <+> ppr tc <> colon <+>
53 vcat [ppr_sig v ty, pprFixity v fix]
54 ppr (TyVarI v ty)
55 = text "Type variable" <+> ppr v <+> equals <+> ppr ty
56 ppr (VarI v ty mb_d fix)
57 = vcat [ppr_sig v ty, pprFixity v fix,
58 case mb_d of { Nothing -> empty; Just d -> ppr d }]
59
60 ppr_sig v ty = ppr v <+> text "::" <+> ppr ty
61
62 pprFixity :: Name -> Fixity -> Doc
63 pprFixity v f | f == defaultFixity = empty
64 pprFixity v (Fixity i d) = ppr_fix d <+> int i <+> ppr v
65 where ppr_fix InfixR = text "infixr"
66 ppr_fix InfixL = text "infixl"
67 ppr_fix InfixN = text "infix"
68
69
70 ------------------------------
71 instance Ppr Exp where
72 ppr = pprExp noPrec
73
74 pprExp :: Precedence -> Exp -> Doc
75 pprExp _ (VarE v) = ppr v
76 pprExp _ (ConE c) = ppr c
77 pprExp i (LitE l) = pprLit i l
78 pprExp i (AppE e1 e2) = parensIf (i >= appPrec) $ pprExp opPrec e1
79 <+> pprExp appPrec e2
80 pprExp i (InfixE (Just e1) op (Just e2))
81 = parensIf (i >= opPrec) $ pprExp opPrec e1
82 <+> ppr op
83 <+> pprExp opPrec e2
84 pprExp _ (InfixE me1 op me2) = parens $ pprMaybeExp noPrec me1
85 <+> ppr op
86 <+> pprMaybeExp noPrec me2
87 pprExp i (LamE ps e) = parensIf (i > noPrec) $ char '\\' <> hsep (map ppr ps)
88 <+> text "->" <+> ppr e
89 pprExp _ (TupE es) = parens $ sep $ punctuate comma $ map ppr es
90 -- Nesting in Cond is to avoid potential problems in do statments
91 pprExp i (CondE guard true false)
92 = parensIf (i > noPrec) $ sep [text "if" <+> ppr guard,
93 nest 1 $ text "then" <+> ppr true,
94 nest 1 $ text "else" <+> ppr false]
95 pprExp i (LetE ds e) = parensIf (i > noPrec) $ text "let" <+> ppr ds
96 $$ text " in" <+> ppr e
97 pprExp i (CaseE e ms)
98 = parensIf (i > noPrec) $ text "case" <+> ppr e <+> text "of"
99 $$ nest nestDepth (ppr ms)
100 pprExp i (DoE ss) = parensIf (i > noPrec) $ text "do" <+> ppr ss
101 pprExp _ (CompE []) = error "Can't happen: pprExp (CompExp [])"
102 -- This will probably break with fixity declarations - would need a ';'
103 pprExp _ (CompE ss) = text "[" <> ppr s
104 <+> text "|"
105 <+> (sep $ punctuate comma $ map ppr ss')
106 <> text "]"
107 where s = last ss
108 ss' = init ss
109 pprExp _ (ArithSeqE d) = ppr d
110 pprExp _ (ListE es) = brackets $ sep $ punctuate comma $ map ppr es
111 pprExp i (SigE e t) = parensIf (i > noPrec) $ ppr e <+> text "::" <+> ppr t
112 pprExp _ (RecConE nm fs) = ppr nm <> braces (pprFields fs)
113 pprExp _ (RecUpdE e fs) = pprExp appPrec e <> braces (pprFields fs)
114
115 pprFields :: [(Name,Exp)] -> Doc
116 pprFields = sep . punctuate comma . map (\(s,e) -> ppr s <+> equals <+> ppr e)
117
118 pprMaybeExp :: Precedence -> Maybe Exp -> Doc
119 pprMaybeExp _ Nothing = empty
120 pprMaybeExp i (Just e) = pprExp i e
121
122 ------------------------------
123 instance Ppr Stmt where
124 ppr (BindS p e) = ppr p <+> text "<-" <+> ppr e
125 ppr (LetS ds) = text "let" <+> ppr ds
126 ppr (NoBindS e) = ppr e
127 ppr (ParS sss) = sep $ punctuate (text "|")
128 $ map (sep . punctuate comma . map ppr) sss
129
130 ------------------------------
131 instance Ppr Match where
132 ppr (Match p rhs ds) = ppr p <+> pprBody False rhs
133 $$ where_clause ds
134
135 ------------------------------
136 pprBody :: Bool -> Body -> Doc
137 pprBody eq (GuardedB xs) = nest nestDepth $ vcat $ map do_guard xs
138 where eqd = if eq then text "=" else text "->"
139 do_guard (NormalG g, e) = text "|" <+> ppr g <+> eqd <+> ppr e
140 do_guard (PatG ss, e) = text "|" <+> vcat (map ppr ss)
141 $$ nest nestDepth (eqd <+> ppr e)
142 pprBody eq (NormalB e) = (if eq then text "=" else text "->") <+> ppr e
143
144 ------------------------------
145 pprLit :: Precedence -> Lit -> Doc
146 pprLit i (IntPrimL x) = parensIf (i > noPrec && x < 0)
147 (integer x <> char '#')
148 pprLit i (FloatPrimL x) = parensIf (i > noPrec && x < 0)
149 (float (fromRational x) <> char '#')
150 pprLit i (DoublePrimL x) = parensIf (i > noPrec && x < 0)
151 (double (fromRational x) <> text "##")
152 pprLit i (IntegerL x) = parensIf (i > noPrec && x < 0) (integer x)
153 pprLit _ (CharL c) = text (show c)
154 pprLit _ (StringL s) = text (show s)
155 pprLit i (RationalL rat) = parensIf (i > noPrec) $ rational rat
156
157 ------------------------------
158 instance Ppr Pat where
159 ppr = pprPat noPrec
160
161 pprPat :: Precedence -> Pat -> Doc
162 pprPat i (LitP l) = pprLit i l
163 pprPat _ (VarP v) = ppr v
164 pprPat _ (TupP ps) = parens $ sep $ punctuate comma $ map ppr ps
165 pprPat i (ConP s ps) = parensIf (i > noPrec) $ ppr s
166 <+> sep (map (pprPat appPrec) ps)
167 pprPat i (InfixP p1 n p2)
168 = parensIf (i > noPrec)
169 $ pprPat opPrec p1 <+> ppr n <+> pprPat opPrec p2
170 pprPat i (TildeP p) = parensIf (i > noPrec) $ pprPat appPrec p
171 pprPat i (AsP v p) = parensIf (i > noPrec) $ ppr v <> text "@"
172 <> pprPat appPrec p
173 pprPat _ WildP = text "_"
174 pprPat _ (RecP nm fs)
175 = parens $ ppr nm
176 <+> braces (sep $ punctuate comma $
177 map (\(s,p) -> ppr s <+> equals <+> ppr p) fs)
178 pprPat _ (ListP ps) = brackets $ sep $ punctuate comma $ map ppr ps
179 pprPat i (SigP p t) = parensIf (i > noPrec) $ ppr p <+> text "::" <+> ppr t
180
181 ------------------------------
182 instance Ppr Dec where
183 ppr (FunD f cs) = vcat $ map (\c -> ppr f <+> ppr c) cs
184 ppr (ValD p r ds) = ppr p <+> pprBody True r
185 $$ where_clause ds
186 ppr (TySynD t xs rhs) = text "type" <+> ppr t <+> hsep (map ppr xs)
187 <+> text "=" <+> ppr rhs
188 ppr (DataD ctxt t xs cs decs)
189 = text "data"
190 <+> pprCxt ctxt
191 <+> ppr t <+> hsep (map ppr xs)
192 <+> sep (pref $ map ppr cs)
193 $$ if null decs
194 then empty
195 else nest nestDepth
196 $ text "deriving"
197 <+> parens (hsep $ punctuate comma $ map ppr decs)
198 where pref :: [Doc] -> [Doc]
199 pref [] = [char '='] -- Can't happen in H98
200 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
201 ppr (NewtypeD ctxt t xs c decs)
202 = text "newtype"
203 <+> pprCxt ctxt
204 <+> ppr t <+> hsep (map ppr xs)
205 <+> char '=' <+> ppr c
206 $$ if null decs
207 then empty
208 else nest nestDepth
209 $ text "deriving"
210 <+> parens (hsep $ punctuate comma $ map ppr decs)
211 ppr (ClassD ctxt c xs ds) = text "class" <+> pprCxt ctxt
212 <+> ppr c <+> hsep (map ppr xs)
213 $$ where_clause ds
214 ppr (InstanceD ctxt i ds) = text "instance" <+> pprCxt ctxt <+> ppr i
215 $$ where_clause ds
216 ppr (SigD f t) = ppr f <+> text "::" <+> ppr t
217 ppr (ForeignD f) = ppr f
218
219 ------------------------------
220 instance Ppr Foreign where
221 ppr (ImportF callconv safety impent as typ)
222 = text "foreign import"
223 <+> showtextl callconv
224 <+> showtextl safety
225 <+> text (show impent)
226 <+> ppr as
227 <+> text "::" <+> ppr typ
228 ppr (ExportF callconv expent as typ)
229 = text "foreign export"
230 <+> showtextl callconv
231 <+> text (show expent)
232 <+> ppr as
233 <+> text "::" <+> ppr typ
234
235 ------------------------------
236 instance Ppr Clause where
237 ppr (Clause ps rhs ds) = hsep (map ppr ps) <+> pprBody True rhs
238 $$ where_clause ds
239
240 ------------------------------
241 instance Ppr Con where
242 ppr (NormalC c sts) = ppr c <+> hsep (map pprStrictType sts)
243 ppr (RecC c vsts)
244 = ppr c <+> braces (hsep (punctuate comma $ map pprVarStrictType vsts))
245 ppr (InfixC st1 c st2) = pprStrictType st1 <+> ppr c <+> pprStrictType st2
246 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
247 <+> char '.' <+> pprCxt ctxt <+> ppr con
248
249 ------------------------------
250 pprVarStrictType :: (Name, Strict, Type) -> Doc
251 -- Slight infelicity: with print non-atomic type with parens
252 pprVarStrictType (v, str, t) = ppr v <+> text "::" <+> pprStrictType (str, t)
253
254 ------------------------------
255 pprStrictType :: (Strict, Type) -> Doc
256 -- Prints with parens if not already atomic
257 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
258 pprStrictType (NotStrict, t) = pprParendType t
259
260 ------------------------------
261 pprParendType :: Type -> Doc
262 pprParendType (VarT v) = ppr v
263 pprParendType (ConT c) = ppr c
264 pprParendType (TupleT 0) = text "()"
265 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
266 pprParendType ArrowT = parens (text "->")
267 pprParendType ListT = text "[]"
268 pprParendType other = parens (ppr other)
269
270 instance Ppr Type where
271 ppr (ForallT tvars ctxt ty) =
272 text "forall" <+> hsep (map ppr tvars) <+> text "."
273 <+> pprCxt ctxt <+> ppr ty
274 ppr ty = pprTyApp (split ty)
275
276 pprTyApp :: (Type, [Type]) -> Doc
277 pprTyApp (ArrowT, [arg1,arg2]) = sep [ppr arg1 <+> text "->", ppr arg2]
278 pprTyApp (ListT, [arg]) = brackets (ppr arg)
279 pprTyApp (TupleT n, args)
280 | length args == n = parens (sep (punctuate comma (map ppr args)))
281 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
282
283 split :: Type -> (Type, [Type]) -- Split into function and args
284 split t = go t []
285 where go (AppT t1 t2) args = go t1 (t2:args)
286 go ty args = (ty, args)
287
288 ------------------------------
289 pprCxt :: Cxt -> Doc
290 pprCxt [] = empty
291 pprCxt [t] = ppr t <+> text "=>"
292 pprCxt ts = parens (hsep $ punctuate comma $ map ppr ts) <+> text "=>"
293
294 ------------------------------
295 instance Ppr Range where
296 ppr = brackets . pprRange
297 where pprRange :: Range -> Doc
298 pprRange (FromR e) = ppr e <> text ".."
299 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
300 <> ppr e2 <> text ".."
301 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
302 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
303 <> ppr e2 <> text ".."
304 <> ppr e3
305
306 ------------------------------
307 where_clause :: [Dec] -> Doc
308 where_clause [] = empty
309 where_clause ds = text "where" <+> vcat (map ppr ds)
310
311 showtextl :: Show a => a -> Doc
312 showtextl = text . map toLower . show
313