[project @ 1997-05-18 04:55:31 by sof]
[ghc-hetmet.git] / ghc / compiler / utils / Outputable.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1996
3 %
4 \section[Outputable]{Classes for pretty-printing}
5
6 Defines classes for pretty-printing and forcing, both forms of
7 ``output.''
8
9 \begin{code}
10 #include "HsVersions.h"
11
12 module Outputable (
13         Outputable(..),         -- class
14
15         printDoc,
16
17         interppSP, interpp'SP,
18         ifnotPprForUser,
19         ifPprDebug,
20         ifPprShowAll, ifnotPprShowAll,
21         ifPprInterface,
22         pprQuote,
23
24         speakNth
25         
26 #if __GLASGOW_HASKELL__ <= 200
27         , Mode
28 #endif
29
30     ) where
31
32 IMP_Ubiq(){-uitous-}
33
34 #if __GLASGOW_HASKELL__ >= 202
35 import IO
36 #endif
37
38 import PprStyle         ( PprStyle(..) )
39 import Pretty
40 import Util             ( cmpPString )
41 \end{code}
42
43 %************************************************************************
44 %*                                                                      *
45 \subsection[Outputable-class]{The @Outputable@ class}
46 %*                                                                      *
47 %************************************************************************
48
49 \begin{code}
50 class Outputable a where
51         ppr :: PprStyle -> a -> Doc
52 \end{code}
53
54 \begin{code}
55 printDoc :: Mode -> Handle -> Doc -> IO ()
56 printDoc mode hdl doc
57   = fullRender mode 100 1.5 put done doc
58   where
59     put (Chr c)  next = hPutChar hdl c >> next 
60     put (Str s)  next = hPutStr  hdl s >> next 
61     put (PStr s) next = hPutFS   hdl s >> next 
62
63     done = hPutChar hdl '\n'
64 \end{code}
65
66
67 \begin{code}
68 interppSP  :: Outputable a => PprStyle -> [a] -> Doc
69 interppSP  sty xs = hsep (map (ppr sty) xs)
70
71 interpp'SP :: Outputable a => PprStyle -> [a] -> Doc
72 interpp'SP sty xs
73   = hsep (punctuate comma (map (ppr sty) xs))
74 \end{code}
75
76 \begin{code}
77 ifPprDebug      sty p = case sty of PprDebug     -> p ; _ -> empty
78 ifPprShowAll    sty p = case sty of PprShowAll   -> p ; _ -> empty
79 ifPprInterface  sty p = case sty of PprInterface -> p ; _ -> empty
80
81 ifnotPprForUser   sty p = case sty of { PprForUser -> empty ; PprQuote -> empty; _ -> p }
82 ifnotPprShowAll   sty p = case sty of { PprShowAll -> empty ; _ -> p }
83 \end{code}
84
85 \begin{code}
86 pprQuote :: PprStyle -> (PprStyle -> Doc) -> Doc
87 pprQuote PprQuote fn = quotes (fn PprForUser)
88 pprQuote sty      fn = fn sty
89 \end{code}
90
91
92 \begin{code}
93 instance Outputable Bool where
94     ppr sty True = ptext SLIT("True")
95     ppr sty False = ptext SLIT("False")
96
97 instance (Outputable a) => Outputable [a] where
98     ppr sty xs = brackets (fsep (punctuate comma (map (ppr sty) xs)))
99
100 instance (Outputable a, Outputable b) => Outputable (a, b) where
101     ppr sty (x,y) =
102       hang (hcat [lparen, ppr sty x, comma]) 4 ((<>) (ppr sty y) rparen)
103
104 -- ToDo: may not be used
105 instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
106     ppr sty (x,y,z) =
107       parens (sep [ (<>) (ppr sty x) comma,
108                       (<>) (ppr sty y) comma,
109                       ppr sty z ])
110 \end{code}
111
112
113
114 @speakNth@ converts an integer to a verbal index; eg 1 maps to
115 ``first'' etc.
116
117 \begin{code}
118 speakNth :: Int -> Doc
119
120 speakNth 1 = ptext SLIT("first")
121 speakNth 2 = ptext SLIT("second")
122 speakNth 3 = ptext SLIT("third")
123 speakNth 4 = ptext SLIT("fourth")
124 speakNth 5 = ptext SLIT("fifth")
125 speakNth 6 = ptext SLIT("sixth")
126 speakNth n = hcat [ int n, text st_nd_rd_th ]
127   where
128     st_nd_rd_th | n_rem_10 == 1 = "st"
129                 | n_rem_10 == 2 = "nd"
130                 | n_rem_10 == 3 = "rd"
131                 | otherwise     = "th"
132
133     n_rem_10 = n `rem` 10
134 \end{code}