module Outputable (
Outputable(..), -- class
- interppSP, interpp'SP,
- ifnotPprForUser,
+ PprStyle(..),
+ codeStyle, ifaceStyle, userStyle,
ifPprDebug,
+ ifnotPprForUser,
ifPprShowAll, ifnotPprShowAll,
- ifPprInterface
+ ifPprInterface,
+ pprQuote,
+
+ printDoc, interppSP, interpp'SP,
+
+ speakNth
+
+#if __GLASGOW_HASKELL__ <= 200
+ , Mode
+#endif
+
) where
-import Ubiq{-uitous-}
+#if __GLASGOW_HASKELL__ >= 202
+import IO
+import GlaExts
+#else
+import Ubiq ( Uniquable(..), Unique, Name ) -- FastString mentions it; todo: rm
+
+#endif
-import PprStyle ( PprStyle(..) )
+import FastString
import Pretty
import Util ( cmpPString )
\end{code}
+
%************************************************************************
%* *
-\subsection[Outputable-class]{The @Outputable@ class}
+\subsection{The @PprStyle@ data type}
%* *
%************************************************************************
\begin{code}
-class Outputable a where
- ppr :: PprStyle -> a -> Pretty
-\end{code}
+data PprStyle
+ = PprForUser Int -- Pretty-print in a way that will
+ -- make sense to the ordinary user;
+ -- must be very close to Haskell
+ -- syntax, etc.
+ -- Parameterised over how much to expand
+ -- a pretty-printed value (<= 0 => stop pp).
+ | PprQuote -- Like PprForUser, but also quote the whole thing
-\begin{code}
--- the ppSep in the ppInterleave puts in the spaces
--- Death to ppSep! (WDP 94/11)
+ | PprDebug -- Standard debugging output
+ | PprShowAll -- Debugging output which leaves
+ -- nothing to the imagination
-interppSP :: Outputable a => PprStyle -> [a] -> Pretty
-interppSP sty xs = ppIntersperse ppSP (map (ppr sty) xs)
+ | PprInterface -- Interface generation
-interpp'SP :: Outputable a => PprStyle -> [a] -> Pretty
-interpp'SP sty xs
- = ppInterleave sep (map (ppr sty) xs)
- where
- sep = ppBeside ppComma ppSP
+ | PprForC -- must print out C-acceptable names
-#ifdef USE_ATTACK_PRAGMAS
-{-# SPECIALIZE interppSP :: PprStyle -> [Id] -> Pretty #-}
-{-# SPECIALIZE interppSP :: PprStyle -> [TyVar] -> Pretty #-}
+ | PprForAsm -- must print out assembler-acceptable names
+ Bool -- prefix CLabel with underscore?
+ (String -> String) -- format AsmTempLabel
-{-# SPECIALIZE interpp'SP :: PprStyle -> [(Id, Id)] -> Pretty #-}
-{-# SPECIALIZE interpp'SP :: PprStyle -> [Id] -> Pretty #-}
-{-# SPECIALIZE interpp'SP :: PprStyle -> [TyVarTemplate] -> Pretty #-}
-{-# SPECIALIZE interpp'SP :: PprStyle -> [TyVar] -> Pretty #-}
-{-# SPECIALIZE interpp'SP :: PprStyle -> [Type] -> Pretty #-}
-#endif
\end{code}
+Orthogonal to the above printing styles are (possibly) some
+command-line flags that affect printing (often carried with the
+style). The most likely ones are variations on how much type info is
+shown.
+
+The following test decides whether or not we are actually generating
+code (either C or assembly), or generating interface files.
\begin{code}
-ifPprDebug sty p = case sty of PprDebug -> p ; _ -> ppNil
-ifPprShowAll sty p = case sty of PprShowAll -> p ; _ -> ppNil
-ifPprInterface sty p = case sty of PprInterface -> p ; _ -> ppNil
+codeStyle :: PprStyle -> Bool
+codeStyle PprForC = True
+codeStyle (PprForAsm _ _) = True
+codeStyle _ = False
+
+ifaceStyle :: PprStyle -> Bool
+ifaceStyle PprInterface = True
+ifaceStyle other = False
+
+userStyle :: PprStyle -> Bool
+userStyle PprQuote = True
+userStyle (PprForUser _) = True
+userStyle other = False
+\end{code}
+
+\begin{code}
+ifPprDebug sty p = case sty of PprDebug -> p ; _ -> empty
+ifPprShowAll sty p = case sty of PprShowAll -> p ; _ -> empty
+ifPprInterface sty p = case sty of PprInterface -> p ; _ -> empty
+
+ifnotPprForUser sty p = case sty of { PprForUser _ -> empty ; PprQuote -> empty; _ -> p }
+ifnotPprShowAll sty p = case sty of { PprShowAll -> empty ; _ -> p }
+\end{code}
+
+\begin{code}
+pprQuote :: PprStyle -> (PprStyle -> Doc) -> Doc
+pprQuote PprQuote fn = quotes (fn (PprForUser 5{-opt_PprUserLength-}))
+pprQuote sty fn = fn sty
+\end{code}
+
+
+
+%************************************************************************
+%* *
+\subsection[Outputable-class]{The @Outputable@ class}
+%* *
+%************************************************************************
-ifnotPprForUser sty p = case sty of PprForUser -> ppNil ; _ -> p
-ifnotPprShowAll sty p = case sty of PprShowAll -> ppNil ; _ -> p
+\begin{code}
+class Outputable a where
+ ppr :: PprStyle -> a -> Doc
\end{code}
\begin{code}
instance Outputable Bool where
- ppr sty True = ppPStr SLIT("True")
- ppr sty False = ppPStr SLIT("False")
+ ppr sty True = ptext SLIT("True")
+ ppr sty False = ptext SLIT("False")
instance (Outputable a) => Outputable [a] where
- ppr sty xs =
- ppBesides [ ppLbrack, ppInterleave ppComma (map (ppr sty) xs), ppRbrack ]
+ ppr sty xs = brackets (fsep (punctuate comma (map (ppr sty) xs)))
instance (Outputable a, Outputable b) => Outputable (a, b) where
ppr sty (x,y) =
- ppHang (ppBesides [ppLparen, ppr sty x, ppComma]) 4 (ppBeside (ppr sty y) ppRparen)
+ hang (hcat [lparen, ppr sty x, comma]) 4 ((<>) (ppr sty y) rparen)
-- ToDo: may not be used
instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
ppr sty (x,y,z) =
- ppSep [ ppBesides [ppLparen, ppr sty x, ppComma],
- ppBeside (ppr sty y) ppComma,
- ppBeside (ppr sty z) ppRparen ]
+ parens (sep [ (<>) (ppr sty x) comma,
+ (<>) (ppr sty y) comma,
+ ppr sty z ])
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Other helper functions}
+%* *
+%************************************************************************
+
+\begin{code}
+printDoc :: Mode -> Handle -> Doc -> IO ()
+printDoc mode hdl doc
+ = fullRender mode 100 1.5 put done doc
+ where
+ put (Chr c) next = hPutChar hdl c >> next
+ put (Str s) next = hPutStr hdl s >> next
+ put (PStr s) next = hPutFS hdl s >> next
+
+ done = hPutChar hdl '\n'
+\end{code}
+
+
+\begin{code}
+interppSP :: Outputable a => PprStyle -> [a] -> Doc
+interppSP sty xs = hsep (map (ppr sty) xs)
+
+interpp'SP :: Outputable a => PprStyle -> [a] -> Doc
+interpp'SP sty xs
+ = hsep (punctuate comma (map (ppr sty) xs))
+\end{code}
+
+
+
+
+%************************************************************************
+%* *
+\subsection{Printing numbers verbally}
+%* *
+%************************************************************************
+
+@speakNth@ converts an integer to a verbal index; eg 1 maps to
+``first'' etc.
+
+\begin{code}
+speakNth :: Int -> Doc
+
+speakNth 1 = ptext SLIT("first")
+speakNth 2 = ptext SLIT("second")
+speakNth 3 = ptext SLIT("third")
+speakNth 4 = ptext SLIT("fourth")
+speakNth 5 = ptext SLIT("fifth")
+speakNth 6 = ptext SLIT("sixth")
+speakNth n = hcat [ int n, text st_nd_rd_th ]
+ where
+ st_nd_rd_th | n_rem_10 == 1 = "st"
+ | n_rem_10 == 2 = "nd"
+ | n_rem_10 == 3 = "rd"
+ | otherwise = "th"
+
+ n_rem_10 = n `rem` 10
\end{code}