Copyright | (c) Harvard University 2006-2011 (c) Geoffrey Mainland 2011-2012 |
---|---|
License | BSD-style |
Maintainer | mainland@eecs.harvard.edu |
Stability | provisional |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell98 |
This module is based on A Prettier Printer by Phil Wadler in /The Fun of Programming/, Jeremy Gibbons and Oege de Moor (eds) http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
At the time it was originally written I didn't know about Daan Leijen's
pretty printing module based on the same paper. I have since incorporated
many of his improvements. This module is geared towards pretty printing
source code; its main advantages over other libraries are a Pretty
class
that handles precedence and the ability to automatically track the source
locations associated with pretty printed values and output appropriate
#line pragmas.
- data Doc
- empty :: Doc
- text :: String -> Doc
- char :: Char -> Doc
- string :: String -> Doc
- fromText :: Text -> Doc
- fromLazyText :: Text -> Doc
- line :: Doc
- nest :: Int -> Doc -> Doc
- srcloc :: Located a => a -> Doc
- column :: (Int -> Doc) -> Doc
- nesting :: (Int -> Doc) -> Doc
- softline :: Doc
- softbreak :: Doc
- group :: Doc -> Doc
- (<>) :: Monoid m => m -> m -> m
- (<+>) :: Doc -> Doc -> Doc
- (</>) :: Doc -> Doc -> Doc
- (<+/>) :: Doc -> Doc -> Doc
- (<//>) :: Doc -> Doc -> Doc
- backquote :: Doc
- colon :: Doc
- comma :: Doc
- dot :: Doc
- dquote :: Doc
- equals :: Doc
- semi :: Doc
- space :: Doc
- spaces :: Int -> Doc
- squote :: Doc
- star :: Doc
- langle :: Doc
- rangle :: Doc
- lbrace :: Doc
- rbrace :: Doc
- lbracket :: Doc
- rbracket :: Doc
- lparen :: Doc
- rparen :: Doc
- enclose :: Doc -> Doc -> Doc -> Doc
- angles :: Doc -> Doc
- backquotes :: Doc -> Doc
- braces :: Doc -> Doc
- brackets :: Doc -> Doc
- dquotes :: Doc -> Doc
- parens :: Doc -> Doc
- parensIf :: Bool -> Doc -> Doc
- squotes :: Doc -> Doc
- align :: Doc -> Doc
- hang :: Int -> Doc -> Doc
- indent :: Int -> Doc -> Doc
- folddoc :: (Doc -> Doc -> Doc) -> [Doc] -> Doc
- spread :: [Doc] -> Doc
- stack :: [Doc] -> Doc
- cat :: [Doc] -> Doc
- sep :: [Doc] -> Doc
- punctuate :: Doc -> [Doc] -> [Doc]
- commasep :: [Doc] -> Doc
- semisep :: [Doc] -> Doc
- encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc
- tuple :: [Doc] -> Doc
- list :: [Doc] -> Doc
- data RDoc
- render :: Int -> Doc -> RDoc
- renderCompact :: Doc -> RDoc
- displayS :: RDoc -> ShowS
- prettyS :: Int -> Doc -> ShowS
- pretty :: Int -> Doc -> String
- displayPragmaS :: RDoc -> ShowS
- prettyPragmaS :: Int -> Doc -> ShowS
- prettyPragma :: Int -> Doc -> String
- displayLazyText :: RDoc -> Text
- prettyLazyText :: Int -> Doc -> Text
- displayPragmaLazyText :: RDoc -> Text
- prettyPragmaLazyText :: Int -> Doc -> Text
- putDoc :: Doc -> IO ()
- hPutDoc :: Handle -> Doc -> IO ()
- class Pretty a where
- faildoc :: Monad m => Doc -> m a
- errordoc :: Doc -> a
The document type
Basic combinators
fromLazyText :: Text -> Doc
The document
consists of the fromLazyText
sText
s
, which should
not contain any newlines.
The document
renders the document nest
i dd
with the current
indentation level increased by i
.
Operators
Character documents
Bracketing combinators
backquotes :: Doc -> Doc
The document
encloses the aligned document backquotes
dd
in ...
.
parensIf :: Bool -> Doc -> Doc
The document
encloses the document parensIf
p dd
in parenthesis if
p
is True
, and otherwise yields just d
.
Alignment and indentation
Combining lists of documents
The document
separates the documents cat
dsds
with the empty
document as long as there is room, and uses newlines when there isn't.
The document
separates the documents sep
dsds
with the empty
document as long as there is room, and uses spaces when there isn't.
The document
comma-space separates commasep
dsds
, aligning the
resulting document to the current nesting level.
The document
semicolon-space separates semisep
dsds
, aligning the
resulting document to the current nesting level.
encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc
The document
separates encloseSep
l r p dsds
with the punctuation p
and encloses the result using l
and r
. When wrapped, punctuation appears
at the end of the line. The enclosed portion of the document is aligned one
column to the right of the opening document.
> ws = map text (words "The quick brown fox jumps over the lazy dog") > test = pretty 15 (encloseSep lparen rparen comma ws)
will be layed out as:
(The, quick, brown, fox, jumps, over, the, lazy, dog)
The document
separates tuple
dsds
with commas and encloses them with
parentheses.
The rendered document type
data RDoc
A rendered document.
Document rendering
renderCompact :: Doc -> RDoc
Render a document without indentation on infinitely long lines. Since no 'pretty' printing is involved, this renderer is fast. The resulting output contains fewer characters.
displayPragmaS :: RDoc -> ShowS
Display a rendered document with #line pragmas.
prettyPragmaS :: Int -> Doc -> ShowS
Render and display a document with #line pragmas.
prettyPragma :: Int -> Doc -> String
Render and convert a document to a String
with #line pragmas.
displayLazyText :: RDoc -> Text
Display a rendered document as Text
. Uses a builder.
prettyLazyText :: Int -> Doc -> Text
Render and display a document as Text
. Uses a builder.
displayPragmaLazyText :: RDoc -> Text
Display a rendered document with #line pragmas as Text
. Uses a builder.
prettyPragmaLazyText :: Int -> Doc -> Text
Render and convert a document to Text
with #line pragmas. Uses a builder.
Document output
hPutDoc :: Handle -> Doc -> IO ()
Render a document with a width of 80 and print it to the specified handle.
The Pretty
type class for pretty printing
class Pretty a where
Nothing