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           annotated output and alignment
    v  String              final rendering

§A deliberately small 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 line breaking 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, so this formatter does not need groups, alternatives, or a fitting algorithm.

Column alignment does need hindsight. The formatter therefore retains a narrow IR over its ordinary output: semantic row and cell boundaries grouped into list-local scopes. Once every newline is final, an alignment pass measures adjacent one-line rows and inserts padding before the String is returned. Padding never feeds back into layout.

§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, payload untouched – except that a branching directive is left-aligned rather than indented with the code around it, having no place in the brace hierarchy. See the 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.