A pretty printer implementation in the style of Hughes, Peyton Jones, Wadler
List of Import References :
See BOOL
See Char
See DENOTATION
See Int
See Nat
See Option
See Real
See Seq
See String
SIGNATURE PrettyPrinter
$Date: 2012-10-02 14:17:54 +0200 (Tue, 02 Oct 2012) $ ($Revision: 838 $)
This pretty printer is an alternative to the Fmt
structure. It is
more tailored to printing tree data structures with indentation than text.
IMPORT Char ONLY char Int ONLY int Nat ONLY nat Real ONLY real Seq ONLY seq[doc] String ONLY string
The abstract type of documents that can be pretty printed to some textual representation
SORT doc
FUN num: nat -> doc FUN num: int -> doc FUN num: real -> doc FUN bool: bool -> doc FUN char: char -> doc
The given denotations or strings should not contain newline characters. They may mess up the formatting.
FUN text: denotation -> doc FUN text: string -> doc FUN $ : denotation -> doc
Whitespace
FUN blank: doc FUN line: doc FUN tab: doc
Punctuation
FUN quote: doc -- \" FUN apostrophe: doc -- ' FUN backtick: doc -- ` FUN colon: doc -- : FUN comma: doc -- , FUN semi: doc -- ; FUN bang: doc -- ! FUN question: doc -- ? FUN dot: doc -- .
Grouping characters
FUN lparen rparen : doc -- ( ) FUN lbrace rbrace : doc -- { } FUN lbrack rbrack : doc -- [ ] FUN langle rangle : doc -- < >
Other characters
FUN slash: doc -- / FUN minus: doc -- - FUN plus: doc -- + FUN star: doc -- * FUN equal: doc -- = FUN pipe: doc -- | FUN backslash: doc -- \ FUN underscore: doc -- _ FUN caret: doc -- ^ FUN ampersand: doc -- & FUN at: doc -- @ FUN tilde: doc -- ~ FUN hash: doc -- # FUN dollar: doc -- $ FUN percent: doc -- \%
FUN parens: doc -> doc -- (...) FUN braces: doc -> doc -- {...} FUN brackets: doc -> doc -- [...] FUN angles: doc -> doc -- <...> FUN quotes: doc -> doc -- "..." FUN squotes: doc -> doc -- '...' FUN squoteslr: doc -> doc -- `...'
Empty document
FUN empty: doc
Besides
FUN <> : doc ** doc -> doc
Besides separated with space (unless one of the arguments is empty)
FUN <+> : doc ** doc -> doc
Sequence version of <>
FUN hcat: seq[doc] -> doc
Sequence version of <+>
FUN hsep: seq[doc] -> doc
Above
FUN ^^ : doc ** doc -> doc
Sequence version of ^^
FUN vcat: seq[doc] -> doc
Either hsep or vcat
FUN sep: seq[doc] -> doc
Either hcat or vcat
FUN cat: seq[doc] -> doc
Nest (or indent) all but the first line by given amount
FUN nest: nat ** doc -> doc
Fill in punctuation (punctuate(p,doc1 :: ... :: docn :: <>) = doc1<>p :: ... :: docn :: <>
)
FUN punctuate: doc ** seq[doc] -> seq[doc]
Fill the given documents on as few lines as possible
FUN fill: seq[doc] -> doc
<>
, <+>
have higher precedence than ^^
/$ BRACKET RIGHT [^^] [<>,<+>] $/ /$ BRACKET LEFT [<>,<+>] [^^] $/
Document operators associate to the right
/$ BRACKET RIGHT [<>,<+>] [<>,<+>] $/ /$ BRACKET RIGHT [^^] [^^] $/
Format for the given width
FUN pretty: nat ** doc -> string FUN pretty: nat ** doc -> denotation
Format for 80 columns
FUN pretty: doc -> string FUN pretty: doc -> denotation
Convert to a sequence of lines (not ending in newline)
FUN pretty: nat ** doc -> seq[string] FUN pretty: nat ** doc -> seq[denotation] FUN pretty: doc -> seq[string] FUN pretty: doc -> seq[denotation]
next node: PrettyPrinter,
prev node: Subsystem Pretty Printer,
up to node: Subsystem Pretty Printer