Pretty-printing
joelburget opened this issue · 0 comments
The three classic pretty-printer papers:
- Pretty Printing -- Derek C. Oppen, 1979.
- The Design of a Pretty-printing Library -- John Hughes, 1995.
- A prettier printer -- Philip Wadler, 1997. This algorithm is fairly widely used, eg Prettier uses it.
There's an adaptation of Wadler's algorithm to OCaml -- Strictly Pretty -- Christian Linding, 2000.
Wadler's algorithm is presented as a set of combinators
(<>) :: Doc -> Doc -> Doc
nil :: Doc
text :: String -> Doc
line :: Doc
nest :: Int -> Doc -> Doc
group :: Doc -> Doc
(<|>) :: Doc -> Doc -> Doc
flatten :: Doc -> Doc
However, we want to specify layout as part of the concrete syntax declaration. Probably with boxes. Eg:
com :=
| "skip" { skip() }
| [<hv 1,3,0> [<h 1> name ":="] iexp] { assign($1; $2) }
| [<hov 1, 0, 0>
[<h 1> "if" bexp]
[<h 1> "then" com]
[<h 1> "else" com]
] { if($2; $4; $6) }
...
This example is taken with only minor modifications from Syn: a single language for specifying abstract syntax trees, lexical analysis, parsing and pretty-printing -- Richard J Boulton, 1996.
This is also quite similar to Ocaml's Format (they even have almost exactly the same types of boxes):
- Format module.
- Format Unraveled - Richard Bonichon, Pierre Weis, 2017.
The Syn declaration seems a little heavy-weight. Ocaml also has break hints. This seems like roughly the right direction -- I'm still weighing the tradeoffs.
Other:
- A Pretty But Not Greedy Printer (Functional Pearl) Jean-Philippe Bernardy, 2017. Possibly useful while implementing the layout algorithm.