0015. Canonical source formatting from the AST
- Status: Accepted; semicolon rule superseded by ADR-0018
- Date: 2026-10-29
- Plan: M2.4
- Review: Authoring, tooling and context
- Owner review: Maintainer reviewed and accepted the semicolon canonical style on 2026-10-30.
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
- Concrete syntax tree or document-layout engine — unnecessary machinery for the current grammar; the agreed plan calls for AST plus comment spans.
- Configurable or source-layout-sensitive styles — more choices for models and less predictable diffs; one canonical style is the milestone goal.
- Rewrite interpolation contents — requires preserving nested decoded-source comments separately; retaining literal spellings avoids changing string bytes.
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.