module Outputable (
Outputable(..), -- class
- printDoc,
-
- interppSP, interpp'SP,
- ifnotPprForUser,
+ PprStyle(..),
+ codeStyle, ifaceStyle, userStyle,
ifPprDebug,
+ ifnotPprForUser,
ifPprShowAll, ifnotPprShowAll,
ifPprInterface,
- pprQuote,
+ pprQuote,
+
+ printDoc, interppSP, interpp'SP,
speakNth
) where
-IMP_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 -> Doc
-\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}
-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
+ | PprDebug -- Standard debugging output
+ | PprShowAll -- Debugging output which leaves
+ -- nothing to the imagination
+
+ | PprInterface -- Interface generation
+
+ | PprForC -- must print out C-acceptable names
+
+ | PprForAsm -- must print out assembler-acceptable names
+ Bool -- prefix CLabel with underscore?
+ (String -> String) -- format AsmTempLabel
- done = hPutChar hdl '\n'
\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}
-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))
+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}
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 }
+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)
+pprQuote PprQuote fn = quotes (fn (PprForUser 5{-opt_PprUserLength-}))
pprQuote sty fn = fn sty
\end{code}
+
+%************************************************************************
+%* *
+\subsection[Outputable-class]{The @Outputable@ class}
+%* *
+%************************************************************************
+
+\begin{code}
+class Outputable a where
+ ppr :: PprStyle -> a -> Doc
+\end{code}
+
\begin{code}
instance Outputable Bool where
ppr sty True = ptext SLIT("True")
\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.