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