2 % (c) The GRASP Project, Glasgow University, 1992-1996
4 \section[Outputable]{Classes for pretty-printing}
6 Defines classes for pretty-printing and forcing, both forms of
10 #include "HsVersions.h"
13 Outputable(..), -- class
16 codeStyle, ifaceStyle, userStyle,
19 ifPprShowAll, ifnotPprShowAll,
23 printDoc, printErrs, pprCols, pprDumpStyle, pprErrorsStyle,
25 interppSP, interpp'SP,
29 #if __GLASGOW_HASKELL__ <= 200
35 #if __GLASGOW_HASKELL__ >= 202
39 import Ubiq ( Uniquable(..), Unique, Name ) -- FastString mentions it; todo: rm
43 import CmdLineOpts ( opt_PprStyle_All, opt_PprStyle_Debug, opt_PprStyle_User )
46 import Util ( cmpPString )
50 %************************************************************************
52 \subsection{The @PprStyle@ data type}
54 %************************************************************************
58 = PprForUser Int -- Pretty-print in a way that will
59 -- make sense to the ordinary user;
60 -- must be very close to Haskell
62 -- Parameterised over how much to expand
63 -- a pretty-printed value (<= 0 => stop pp).
64 | PprQuote -- Like PprForUser, but also quote the whole thing
66 | PprDebug -- Standard debugging output
67 | PprShowAll -- Debugging output which leaves
68 -- nothing to the imagination
70 | PprInterface -- Interface generation
72 | PprForC -- must print out C-acceptable names
74 | PprForAsm -- must print out assembler-acceptable names
75 Bool -- prefix CLabel with underscore?
76 (String -> String) -- format AsmTempLabel
80 Orthogonal to the above printing styles are (possibly) some
81 command-line flags that affect printing (often carried with the
82 style). The most likely ones are variations on how much type info is
85 The following test decides whether or not we are actually generating
86 code (either C or assembly), or generating interface files.
88 codeStyle :: PprStyle -> Bool
89 codeStyle PprForC = True
90 codeStyle (PprForAsm _ _) = True
93 ifaceStyle :: PprStyle -> Bool
94 ifaceStyle PprInterface = True
95 ifaceStyle other = False
97 userStyle :: PprStyle -> Bool
98 userStyle PprQuote = True
99 userStyle (PprForUser _) = True
100 userStyle other = False
104 ifPprDebug sty p = case sty of PprDebug -> p ; _ -> empty
105 ifPprShowAll sty p = case sty of PprShowAll -> p ; _ -> empty
106 ifPprInterface sty p = case sty of PprInterface -> p ; _ -> empty
108 ifnotPprForUser sty p = case sty of { PprForUser _ -> empty ; PprQuote -> empty; _ -> p }
109 ifnotPprShowAll sty p = case sty of { PprShowAll -> empty ; _ -> p }
113 pprQuote :: PprStyle -> (PprStyle -> Doc) -> Doc
114 pprQuote PprQuote fn = quotes (fn (PprForUser 5{-opt_PprUserLength-}))
115 pprQuote sty fn = fn sty
120 %************************************************************************
122 \subsection[Outputable-class]{The @Outputable@ class}
124 %************************************************************************
127 class Outputable a where
128 ppr :: PprStyle -> a -> Doc
132 instance Outputable Bool where
133 ppr sty True = ptext SLIT("True")
134 ppr sty False = ptext SLIT("False")
136 instance Outputable Int where
139 instance (Outputable a) => Outputable [a] where
140 ppr sty xs = brackets (fsep (punctuate comma (map (ppr sty) xs)))
142 instance (Outputable a, Outputable b) => Outputable (a, b) where
144 hang (hcat [lparen, ppr sty x, comma]) 4 ((<>) (ppr sty y) rparen)
146 -- ToDo: may not be used
147 instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
149 parens (sep [ (<>) (ppr sty x) comma,
150 (<>) (ppr sty y) comma,
155 %************************************************************************
157 \subsection{Other helper functions}
159 %************************************************************************
162 pprCols = (100 :: Int) -- could make configurable
164 -- pprErrorsStyle is the style to print ordinary error messages with
165 -- pprDumpStyle is the style to print -ddump-xx information in
166 (pprDumpStyle, pprErrorsStyle)
167 | opt_PprStyle_All = (PprShowAll, PprShowAll)
168 | opt_PprStyle_Debug = (PprDebug, PprDebug)
169 | otherwise = (PprDebug, PprQuote)
171 printDoc :: Mode -> Handle -> Doc -> IO ()
172 printDoc mode hdl doc
173 = fullRender mode pprCols 1.5 put done doc
175 put (Chr c) next = hPutChar hdl c >> next
176 put (Str s) next = hPutStr hdl s >> next
177 put (PStr s) next = hPutFS hdl s >> next
179 done = hPutChar hdl '\n'
181 -- I'm not sure whether the direct-IO approach of printDoc
182 -- above is better or worse than the put-big-string approach here
183 printErrs :: Doc -> IO ()
184 printErrs doc = hPutStr stderr (show (doc $$ text ""))
189 interppSP :: Outputable a => PprStyle -> [a] -> Doc
190 interppSP sty xs = hsep (map (ppr sty) xs)
192 interpp'SP :: Outputable a => PprStyle -> [a] -> Doc
194 = hsep (punctuate comma (map (ppr sty) xs))
200 %************************************************************************
202 \subsection{Printing numbers verbally}
204 %************************************************************************
206 @speakNth@ converts an integer to a verbal index; eg 1 maps to
210 speakNth :: Int -> Doc
212 speakNth 1 = ptext SLIT("first")
213 speakNth 2 = ptext SLIT("second")
214 speakNth 3 = ptext SLIT("third")
215 speakNth 4 = ptext SLIT("fourth")
216 speakNth 5 = ptext SLIT("fifth")
217 speakNth 6 = ptext SLIT("sixth")
218 speakNth n = hcat [ int n, text st_nd_rd_th ]
220 st_nd_rd_th | n_rem_10 == 1 = "st"
221 | n_rem_10 == 2 = "nd"
222 | n_rem_10 == 3 = "rd"
225 n_rem_10 = n `rem` 10