On this page

0015. Canonical source formatting from the AST

The semicolon and one-line-if rules were later superseded by ADR-0018; the remaining decisions stand.

Context

M2.4 requires one idempotent style without options, preserving comments. The lexer retains comments separately and the AST discards redundant grouping. Newlines inside any parentheses are suppressed, including in if-expression bodies.

Decision

Print the AST with four-space indentation, one statement per line, a blank line between functions, canonical operator spacing and necessary precedence grouping. Use semicolons after simple statements, and after control statements when inside parentheses. Emit lexer comments in source order on their own lines, with their text intact except trailing whitespace. Retain source grouping around commented expressions when needed to keep newlines from changing expression boundaries; do not add nesting merely because an expression contains a comment.

Preserve string token spellings, including escapes and interpolation contents. Format syntax without requiring name or type checking. fmt <file> rewrites one file; fmt --check <path> checks a file or recursively checks directory .hn files in sorted order, without writing. Do not follow directory-entry symlinks.

Alternatives considered

Consequences

Formatting twice must equal formatting once and preserve the span-free AST. Program goldens are canonical and checked by the gate; successful parse fixtures also test idempotency, including deliberately non-canonical layout inputs. Comment placement normalizes to standalone lines; literal contents are opaque.