A document, or doc, is an intermediate data structure between a parse tree and formatted source text. It represents both program text and layout rules as a directed graph of nodes. section 9.3
A document encodes the formatter’s layout intentions, including:
- Concatenation, indentation, grouping, spacing, and optional line breaks. section 9.3
- A separation between describing desired layout and greedily rendering text to fit the current line. section 9.3
- A boundary between parse-tree structure and formatting policy, allowing multiple analyses or pretty printers without adding methods to the tree classes. section 9.4
From expression tree to formatted text
For an expression such as Foo.Bar((x + y.z) * q, A.B.C), phase 1 transforms the parse tree into a doc describing grouping and indentation; phase 2 turns that doc into a string. section 9.4







