liveBook · Manning

Document

As explained in Fabulous Adventures in Data Structures and Algorithms

A document is an intermediate data structure that represents source-code layout rules and program text for pretty printing.

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

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines the document’s role and encoded layout concepts, while deferring detailed node definitions and rendering implementation.
Examples
Uses an expression tree for Foo.Bar((x + y.z) * q, A.B.C) as a parse-tree-to-document example.
What it assumes
Assumes parsing and parse trees while developing formatting rules and document transformation from expression structures.
What you can do after
Build a pretty-printing pipeline by transforming parse trees into documents and rendering documents as text.
How it connects
Connects parse trees, pretty printing, grouping, indentation, line breaks, and the visitor pattern.

Also explained in

Compare how 8 books explain Document →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.