X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FPretty.lhs;h=47d4b1e19e582225cae5e7da5d80aa0966706c52;hb=4fa44a3ae9c36222ccb460ba3ed24e46bf7c70ae;hp=7713d03cfcb88c900c10406c10584659f50234f4;hpb=9412e62942ebab0599c7fb0b358a9d4869647b67;p=ghc-hetmet.git diff --git a/compiler/utils/Pretty.lhs b/compiler/utils/Pretty.lhs index 7713d03..47d4b1e 100644 --- a/compiler/utils/Pretty.lhs +++ b/compiler/utils/Pretty.lhs @@ -1,15 +1,15 @@ -********************************************************************************* -* * -* John Hughes's and Simon Peyton Jones's Pretty Printer Combinators * -* * -* based on "The Design of a Pretty-printing Library" * -* in Advanced Functional Programming, * -* Johan Jeuring and Erik Meijer (eds), LNCS 925 * -* http://www.cs.chalmers.se/~rjmh/Papers/pretty.ps * -* * -* Heavily modified by Simon Peyton Jones, Dec 96 * -* * -********************************************************************************* +%********************************************************************************* +%* * +%* John Hughes's and Simon Peyton Jones's Pretty Printer Combinators * +%* * +%* based on "The Design of a Pretty-printing Library" * +%* in Advanced Functional Programming, * +%* Johan Jeuring and Erik Meijer (eds), LNCS 925 * +%* http://www.cs.chalmers.se/~rjmh/Papers/pretty.ps * +%* * +%* Heavily modified by Simon Peyton Jones, Dec 96 * +%* * +%********************************************************************************* Version 3.0 28 May 1997 * Cured massive performance bug. If you write @@ -162,7 +162,7 @@ module Pretty ( empty, isEmpty, nest, - text, char, ftext, ptext, + char, text, ftext, ptext, int, integer, float, double, rational, parens, brackets, braces, quotes, doubleQuotes, semi, comma, colon, space, equals, @@ -176,7 +176,8 @@ module Pretty ( hang, punctuate, -- renderStyle, -- Haskell 1.3 only - render, fullRender, printDoc, showDocWith + render, fullRender, printDoc, showDocWith, + bufLeftRender -- performance hack ) where import BufWrite @@ -211,11 +212,11 @@ infixl 5 $$, $+$ \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{The interface} -* * -********************************************************* +%* * +%********************************************************* The primitive @Doc@ values @@ -298,11 +299,11 @@ data Mode = PageMode -- Normal \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{The @Doc@ calculus} -* * -********************************************************* +%* * +%********************************************************* The @Doc@ combinators satisfy the following laws: \begin{verbatim} @@ -336,8 +337,8 @@ Laws for nest nest k empty = empty x <> nest k y = x <> y, if x non-empty -** Note the side condition on ! It is this that -** makes it OK for empty to be a left unit for <>. + - Note the side condition on ! It is this that + makes it OK for empty to be a left unit for <>. Miscellaneous ~~~~~~~~~~~~~ @@ -377,11 +378,11 @@ But it doesn't work, for if x=empty, we would have -********************************************************* -* * +%********************************************************* +%* * \subsection{Simple derived definitions} -* * -********************************************************* +%* * +%********************************************************* \begin{code} semi = char ';' @@ -427,11 +428,11 @@ punctuate p (d:ds) = go d ds \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{The @Doc@ data type} -* * -********************************************************* +%* * +%********************************************************* A @Doc@ represents a {\em set} of layouts. A @Doc@ with no occurrences of @Union@ or @NoDoc@ represents just one layout. @@ -531,7 +532,6 @@ union_ p q = Union (LOCAL_ASSERT( _ok p ) p) (LOCAL_ASSERT( _ok q ) q) _ok _ = False \end{code} - Notice the difference between * NoDoc (no documents) * Empty (one empty document; no height and no width) @@ -540,11 +540,11 @@ Notice the difference between -********************************************************* -* * +%********************************************************* +%* * \subsection{@empty@, @text@, @nest@, @union@} -* * -********************************************************* +%* * +%********************************************************* \begin{code} empty = Empty @@ -557,7 +557,7 @@ text s = case iUnbox (length s) of {sl -> textBeside_ (Str s) sl Empty} ftext :: FastString -> Doc ftext s = case iUnbox (lengthFS s) of {sl -> textBeside_ (PStr s) sl Empty} ptext :: LitString -> Doc -ptext s_= case iUnbox (strLength s) of {sl -> textBeside_ (LStr s sl) sl Empty} +ptext s_= case iUnbox (lengthLS s) of {sl -> textBeside_ (LStr s sl) sl Empty} where s = {-castPtr-} s_ #if defined(__GLASGOW_HASKELL__) @@ -584,11 +584,11 @@ mkUnion Empty _ = Empty mkUnion p q = p `union_` q \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{Vertical composition @$$@} -* * -********************************************************* +%* * +%********************************************************* \begin{code} @@ -615,7 +615,7 @@ aboveNest (Nest k1 p) g k q = nest_ k1 (aboveNest p g (k -# k1) q) aboveNest (NilAbove p) g k q = nilAbove_ (aboveNest p g k q) aboveNest (TextBeside s sl p) g k q = textBeside_ s sl rest where - k1 = k -# sl + !k1 = k -# sl rest = case p of Empty -> nilAboveNest g k1 q _ -> aboveNest p g k1 q @@ -637,11 +637,11 @@ nilAboveNest g k q | (not g) && (k ># _ILIT(0)) -- No newline i \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{Horizontal composition @<>@} -* * -********************************************************* +%* * +%********************************************************* \begin{code} p <> q = Beside p False q @@ -679,11 +679,11 @@ nilBeside g p | g = textBeside_ space_text (_ILIT(1)) p | otherwise = p \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{Separate, @sep@, Hughes version} -* * -********************************************************* +%* * +%********************************************************* \begin{code} -- Specification: sep ps = oneLiner (hsep ps) @@ -732,11 +732,11 @@ sepNB g Empty k ys = oneLiner (nilBeside g (reduceDoc rest)) sepNB g p k ys = sep1 g p k ys \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{@fill@} -* * -********************************************************* +%* * +%********************************************************* \begin{code} fsep = fill True @@ -775,18 +775,18 @@ fillNB g Empty k (y:ys) = nilBeside g (fill1 g (oneLiner (reduceDoc y)) k1 ys `mkUnion` nilAboveNest False k (fill g (y:ys)) where - k1 | g = k -# _ILIT(1) - | otherwise = k + !k1 | g = k -# _ILIT(1) + | otherwise = k fillNB g p k ys = fill1 g p k ys \end{code} -********************************************************* -* * +%********************************************************* +%* * \subsection{Selecting the best layout} -* * -********************************************************* +%* * +%********************************************************* \begin{code} best :: Int -- Line length @@ -797,7 +797,7 @@ best :: Int -- Line length best w_ r_ p = get (iUnbox w_) p where - r = iUnbox r_ + !r = iUnbox r_ get :: FastInt -- (Remaining) width of line -> Doc -> Doc get _ Empty = Empty @@ -873,11 +873,11 @@ oneLiner _ = panic "oneLiner: Unhandled case" -********************************************************* -* * +%********************************************************* +%* * \subsection{Displaying the best layout} -* * -********************************************************* +%* * +%********************************************************* \begin{code} @@ -1043,9 +1043,12 @@ hPutLitString handle a l = if l ==# _ILIT(0) printLeftRender :: Handle -> Doc -> IO () printLeftRender hdl doc = do b <- newBufHandle hdl - layLeft b (reduceDoc doc) + bufLeftRender b doc bFlush b +bufLeftRender :: BufHandle -> Doc -> IO () +bufLeftRender b doc = layLeft b (reduceDoc doc) + -- HACK ALERT! the "return () >>" below convinces GHC to eta-expand -- this function with the IO state lambda. Otherwise we end up with -- closures in all the case branches.