Skip to main content

Crate rdlfmt

Crate rdlfmt 

Source
Expand description

A formatter for SystemRDL.

source text
    |
    v  syntax              lossless CST, comments and all
    v  rules               one function per node kind
    v  formatter           direct emission into a String

§Why there is no intermediate representation

Pretty-printers usually build a document IR (Wadler groups, Oppen’s algorithm) because their layout decisions depend on rendered width: whether a list fits on one line cannot be known until everything inside it has been laid out, so the decision has to be deferred and the alternatives measured.

None of the rules here are width-dependent. Following the PeakRDL style guide, braces always break, statements are one per line, expressions never break, and a parenthesised list breaks when it holds more than one element. Every one of those is decidable from the tree alone, before a single character is written. With nothing to defer there is nothing for a document IR to represent, so rules write straight into the output buffer.

Each decision that does exist is still isolated in its own function returning a layout, rather than being spelled out inline at the point of emission. Should one of them ever need to consult rendered width, it can render flat into a scratch buffer and measure it – at the size of a register description the cost of that is not worth an IR to avoid.

§What the formatter will not do

Reformat a file the parser did not fully understand. format() returns FormatError when the parse reports errors, because the rules assume a tree shape that error recovery does not guarantee, and rewriting a file whose structure was guessed at is how a formatter corrupts code.

Preprocessor directives need no separate rule against them, which is the point of treating even the conditionals as trivia: a `ifdef whose branches hand a brace back and forth leaves the braces unbalanced, and so is refused by the same check as any other input the parser could not follow. Everything else formats like a comment – its own line, indented with the code around it, payload untouched. See the module docs in crate::syntax::parser for why ignoring a conditional cannot corrupt the file.

Modules§

syntax
A lossless syntax tree for SystemRDL.

Enums§

FormatError
Why no formatted output was produced.

Functions§

format
Formats SystemRDL source.