mainland-pretty-0.6.1: Pretty printing designed for printing source code.

Copyright (c) 2006-2011 Harvard University(c) 2011-2012 Geoffrey Mainland(c) 2015-2017 Drexel University BSD-style mainland@drexel.edu provisional portable None Haskell98

Text.PrettyPrint.Mainland

Description

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 the ability to automatically track the source locations associated with pretty printed values and output appropriate #line pragmas and the use of Text for output.

Synopsis

# The document type

data Doc #

The abstract type of documents.

Instances

 # Methods # Methodsmappend :: Doc -> Doc -> Doc #mconcat :: [Doc] -> Doc # # Methodsppr :: Doc -> Doc #pprPrec :: Int -> Doc -> Doc #pprList :: [Doc] -> Doc #

# Constructing documents

## Converting values into documents

text :: String -> Doc #

The document text s consists of the string s, which should not contain any newlines. For a string that may include newlines, use string.

bool :: Bool -> Doc #

The document bool b is equivalent to text (show b).

char :: Char -> Doc #

The document char c consists the single character c.

string :: String -> Doc #

The document string s consists of all the characters in s but with newlines replaced by line.

int :: Int -> Doc #

The document int i is equivalent to text (show i).

The document integer i is equivalent to text (show i). text.

float :: Float -> Doc #

The document float f is equivalent to text (show f).

double :: Double -> Doc #

The document double d is equivalent to text (show d).

The document rational r is equivalent to text (show r).

The document strictText s consists of the Text s, which should not contain any newlines.

lazyText :: Text -> Doc #

The document lazyText s consists of the Text s, which should not contain any newlines.

## Simple documents documents

star :: Doc #

The document star consists of an asterisk, "*".

The document colon consists of a colon, ":".

The document comma consists of a comma, ",".

dot :: Doc #

The document dot consists of a period, ".".

The document equals consists of an equals sign, "=".

semi :: Doc #

The document semi consists of a semicolon, ";".

The document space consists of a space, " ".

spaces :: Int -> Doc #

The document space n consists of n spaces.

The document backquote consists of a backquote, "".

The document squote consists of a single quote, "\'".

The document dquote consists of a double quote, "\"".

The document langle consists of a less-than sign, "<".

The document rangle consists of a greater-than sign, ">".

The document lbrace consists of a left brace, "{".

The document rbrace consists of a right brace, "}".

The document lbracket consists of a right brace, "[".

The document rbracket consists of a right brace, "]".

The document lparen consists of a right brace, "(".

The document rparen consists of a right brace, ")".

## Basic document combinators

The empty document.

srcloc :: Located a => a -> Doc #

The document srcloc x tags the current line with locOf x. Only shown when running prettyPragma and friends.

line :: Doc #

The document line advances to the next line and indents to the current indentation level. When undone by group, it behaves like space.

Becomes space if there is room, otherwise line.

pretty 11 $text "foo" <+/> text "bar" <+/> text "baz" =="foo bar baz" pretty 7$ text "foo" <+/> text "bar" <+/> text "baz" == "foo bar\nbaz"
pretty  6 text "foo" <+/> text "bar" <+/> text "baz" == "foo\nbar\nbaz" Becomes empty if there is room, otherwise line. (<>) :: Monoid m => m -> m -> m infixr 6 # An infix synonym for mappend. Since: 4.5.0.0 (<|>) :: Doc -> Doc -> Doc infixl 3 # Provide alternative layouts of the same content. Invariant: both arguments must flatten to the same document. (<+>) :: Doc -> Doc -> Doc infixr 6 # Concatenates two documents with a space in between, with identity empty. (</>) :: Doc -> Doc -> Doc infixr 5 # Concatenates two documents with a line in between, with identity empty. (<+/>) :: Doc -> Doc -> Doc infixr 5 # Concatenates two documents with a softline in between, with identity empty. (<//>) :: Doc -> Doc -> Doc infixr 5 # Concatenates two documents with a softbreak in between. group :: Doc -> Doc # The document group d will flatten d to one line if there is room for it, otherwise the original d. flatten :: Doc -> Doc # The document flatten d will flatten d to one line. ## Wrapping documents in delimiters enclose :: Doc -> Doc -> Doc -> Doc # The document enclose l r d encloses the document d between the documents l and r using <>. It obeys the law enclose l r d = l <> d <> r squotes :: Doc -> Doc # The document squotes d encloses the alinged document d in '...'. dquotes :: Doc -> Doc # The document dquotes d encloses the aligned document d in "...". angles :: Doc -> Doc # The document angles d encloses the aligned document d in <...>. The document backquotes d encloses the aligned document d in .... braces :: Doc -> Doc # The document braces d encloses the aligned document d in {...}. brackets :: Doc -> Doc # The document brackets d encloses the aligned document d in [...]. parens :: Doc -> Doc # The document parens d encloses the aligned document d in (...). parensIf :: Bool -> Doc -> Doc # The document parensIf p d encloses the document d in parenthesis if p is True, and otherwise yields just d. # Combining lists of documents folddoc :: (Doc -> Doc -> Doc) -> [Doc] -> Doc # The document folddoc f ds obeys the laws: • folddoc f [] = empty • folddoc f [d1, d2, ..., dnm1, dn] = d1 f (d2 f ... (dnm1 f dn)) spread :: [Doc] -> Doc # The document spread ds concatenates the documents ds with space. stack :: [Doc] -> Doc # The document stack ds concatenates the documents ds with line. cat :: [Doc] -> Doc # The document cat ds concatenates the documents ds with the empty document as long as there is room, and uses line when there isn't. sep :: [Doc] -> Doc # The document sep ds concatenates the documents ds with the space document as long as there is room, and uses line when there isn't. punctuate :: Doc -> [Doc] -> [Doc] # The document punctuate p ds obeys the law: punctuate p [d1, d2, ..., dn] = [d1 <> p, d2 <> p, ..., dn] commasep :: [Doc] -> Doc # The document commasep ds comma-space separates ds, aligning the resulting document to the current nesting level. semisep :: [Doc] -> Doc # The document semisep ds semicolon-space separates ds, aligning the resulting document to the current nesting level. enclosesep :: Doc -> Doc -> Doc -> [Doc] -> Doc # The document enclosesep l r p ds separates ds 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)  tuple :: [Doc] -> Doc # The document tuple ds separates ds with commas and encloses them with parentheses. list :: [Doc] -> Doc # The document list ds separates ds with commas and encloses them with brackets. ## Alignment and indentation align :: Doc -> Doc # The document align d renders d with a nesting level set to the current column. hang :: Int -> Doc -> Doc # The document hang i d renders d with a nesting level set to the current column plus i, not including the first line. indent :: Int -> Doc -> Doc # The document indent i d renders d with a nesting level set to the current column plus i, including the first line. nest :: Int -> Doc -> Doc # The document nest i d renders the document d with the current indentation level increased by i. column :: (Int -> Doc) -> Doc # The document column f is produced by calling f with the current column. nesting :: (Int -> Doc) -> Doc # The document column f is produced by calling f with the current nesting level. width :: Doc -> (Int -> Doc) -> Doc # The document width d f is produced by concatenating d with the result of calling f with the width of the document d. fill :: Int -> Doc -> Doc # The document fill i d renders document x, appending spaces until the width is equal to i. If the width of d is already greater than i, nothing is appended. fillbreak :: Int -> Doc -> Doc # The document fillbreak i d renders document d, appending spaces until the width is equal to i. If the width of d is already greater than i, the nesting level is increased by i and a line is appended. ## Utilities faildoc :: Monad m => Doc -> m a # Equivalent of fail, but with a document instead of a string. errordoc :: Doc -> a # Equivalent of error, but with a document instead of a string. # The rendered document type data RDoc # A rendered document. Constructors  REmpty The empty document RChar !Char RDoc A single character RString !Int String RDoc String with associated length (to avoid recomputation) RText Text RDoc Text RLazyText Text RDoc Text RPos Pos RDoc Tag output with source location RLine !Int RDoc A newline with the indentation of the subsequent line. If this is followed by a RPos, output an appropriate #line pragma before the newline. # Document rendering render :: Int -> Doc -> RDoc # Render a document given a maximum width. 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. Display a rendered document. prettyS :: Int -> Doc -> ShowS # Render and display a document. pretty :: Int -> Doc -> String # Render and convert a document to a String. Render and display a document compactly. Render and convert a document to a String compactly. Display a rendered document with #line pragmas. Render and display a document with #line pragmas. Render and convert a document to a String with #line pragmas. > let loc = Loc (Pos "filename" 3 5 7) (Pos "filename" 5 7 9) > in putStrLn prettyPragma 80 \$ srcloc loc <> text "foo" </> text "bar" </> text "baz"

will be printed as

foo
#line 3 "filename"
bar
baz


Display a rendered document as Text. Uses a builder.

Render and display a document as Text. Uses a builder.

Display a rendered document with #line pragmas as Text. Uses a builder.

Render and convert a document to Text` with #line pragmas. Uses a builder.

# Document output

putDoc :: Doc -> IO () #

Render a document with a width of 80 and print it to standard output.

putDocLn :: Doc -> IO () #

Render a document with a width of 80 and print it to standard output, followed by a newline.

hPutDoc :: Handle -> Doc -> IO () #

Render a document with a width of 80 and print it to the specified handle.

hPutDocLn :: Handle -> Doc -> IO () #

Render a document with a width of 80 and print it to the specified handle, followed by a newline.