nx_srcfmt.nx
buildroot/runtime/nx_srcfmt.nx
about
nx_srcfmt.nx -- THE CANONICAL SOURCE FORMATTER for NishiLang (lang.plan rung LN9, watch symbol
srcfmt_emit). One style, no options -- the gofmt / zig fmt / elm-format shape. The style was not
chosen, it was MEASURED: every rule below is the majority convention of the whole runtime corpus
(18,561 files, 2.97 M lines, census 2026-08-23; re-measure any time with `nx_srcfmt census`, which
uses this same scanner so the ruler and the rule cannot drift apart):
indent 4 spaces per brace depth (2,216,007 lines are multiples of 4 vs 9,644 that are not), tabs
never (0 tab-indented lines); a continuation line inside an unclosed ( or [ takes base + 4
(20,695 vs 7,210 at +0 vs 4,536 at +8), except a line that opens with ) or ] (base)
`{` stays on the line that opens it (179,575 vs 2 own-line); `} else {` on one line (33,968 vs
5,444 split) -- a lone `}` line followed by an `else` line is JOINED
`, ` (1,098,119 vs 110,794) ; `; ` (455,500 vs 26,006) ; ` = ` and every binary operator spaced
(955,786 vs 276,475 for `=`; 385,897 vs 134,264 for + == != <= >= && ||) ; ` -> ` (103,535 vs 1)
`x: T` (781,324 vs 2,337; a space BEFORE the colon 10) ; `*u8` `&x` `-1` `!b` unary glued
(700,588 vs 2 for the type star) ; `f(` (966,045 vs 19,129) but `if (` `return (` `while (`
keyword-then-paren spaced (15,689 vs 1,315 for `if`) ; `{ stmt }` one-line blocks padded
(338,077 vs 11,657) and `{}` empty blocks closed (457 vs 143) ; `a..b` `a.b` `a::b` glued
(4,644 vs 91 for `..`)
struct field TYPE COLUMNS are aligned (measured 2026-08-23: of 1,712 multi-field struct runs,
1,340 column-align their types, 193 single-space, 179 ragged) -- the type starts one space past
the run's longest field name; a blank line or a comment breaks the run. This is the ONE place a
token's spacing depends on other lines, and it is still whitespace between tokens so the oracle
and the byte-identical-build proof are untouched.
const / static / let / var declarations are NOT aligned -- single space after the colon. The
corpus does NOT decisively align these the way it aligns struct fields: const runs split 4,234
aligned vs 4,760 single (single is the plurality), static 47 vs 174 single, and body-level
let/var are single-space 437,751 vs 6,278. Where the corpus has no clear majority the simpler
rule wins, so aligned const blocks reformat to single-space -- expected, measured churn.
LF line endings (2,524 of 18,801 files carried CRLF), no trailing whitespace (1,914 lines had it),
blank-line runs collapsed to one (348 runs of two or more), exactly one final newline (53 files
had none)
PRESERVED BYTE-FOR-BYTE, by construction: every string literal (including the multi-line literals
this dialect permits -- 205 files carry one), every comment's text, every @-directive line
(@macro bodies through their real extent, found by the tokenizer's own lexm_body_end), and the
whitespace run that separates code from a trailing `//` comment (the corpus aligns those by column
-- widths 2..6 are all common -- so canonicalising them would destroy deliberate alignment; this is
a DECLARED floor, not an oversight).
WHY THE TOKENIZER IS THE VERIFIER AND NOT THE EMITTER (measured, nx_tokenizer.nx): lex_source STRIPS
comments (skip_ws_comments), PRE-EXPANDS @macro into a new buffer (so token positions no longer
address the source), and CONSUMES @ifdef/@else/@endif branches at lex time (inactive code never
dependencies 3 imports · 1 importers
imports: nx_syscalls.nxnx_tokenizer.nxnx_gatekit_lib.nx
imported by: nx_srcfmt_gate.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 120 | struct Sf |
consts
| 71 | const SF_INDENT: i64 = 4 // the measured unit: 2,216,007 multiple-of-4 lines vs 9,644 other |
| 72 | const SF_NAME_SLOT: i64 = 128 // per-path byte slot for the corpus census (same as MP_SLOT) |
| 73 | const SF_CORPUS_CAP: i64 = 32768 // corpus is 18,561; 16,384 would REFUSE (same bar as nx_srclint) |
| 74 | const SF_CENSUS_ROWS: i64 = 24 // rule rows the census verb reports |
| 77 | const SP_NONE: i64 = 0 // start of line |
| 78 | const SP_IDENT: i64 = 1 |
| 79 | const SP_KW: i64 = 2 // keyword that is not an operand (if while return as let ...) |
| 80 | const SP_OPERAND: i64 = 3 // int / float / string / true / false |
| 81 | const SP_LPAREN: i64 = 4 |
| 82 | const SP_RPAREN: i64 = 5 |
| 83 | const SP_LBRACK: i64 = 6 |
| 84 | const SP_RBRACK: i64 = 7 |
| 85 | const SP_LBRACE: i64 = 8 |
| 86 | const SP_RBRACE: i64 = 9 |
| 87 | const SP_COMMA: i64 = 10 |
| 88 | const SP_SEMI: i64 = 11 |
| 89 | const SP_COLON: i64 = 12 |
| 90 | const SP_GLUE: i64 = 13 // . .. :: -- no space either side |
| 91 | const SP_BINOP: i64 = 14 // every binary operator, = -> => ? and the ternary : |
| 92 | const SP_UNARY: i64 = 15 // - * & + ! ~ used as a prefix: nothing after it |
| 93 | const SP_ATTR: i64 = 16 // @priv and any @name that is not a directive; #name |
| 96 | const CR_INDENT: i64 = 0 |
| 97 | const CR_TRAIL: i64 = 1 |
| 98 | const CR_CRLF: i64 = 2 |
| 99 | const CR_COMMA: i64 = 3 |
| 100 | const CR_SEMI: i64 = 4 |
| 101 | const CR_BINOP: i64 = 5 |
| 102 | const CR_ASSIGN: i64 = 6 |
| 103 | const CR_ARROW: i64 = 7 |
| 104 | const CR_COLON: i64 = 8 |
| 105 | const CR_UNARY: i64 = 9 |
| 106 | const CR_CALLPAREN: i64 = 10 |
| 107 | const CR_KWPAREN: i64 = 11 |
| 108 | const CR_LBRACE: i64 = 12 |
| 109 | const CR_BLOCKPAD: i64 = 13 |
| 110 | const CR_EMPTYBLOCK: i64 = 14 |
| 111 | const CR_GLUE: i64 = 15 |
| 112 | const CR_ELSEJOIN: i64 = 16 |
| 113 | const CR_BLANKRUN: i64 = 17 |
| 114 | const CR_CONT: i64 = 18 |
| 115 | const CR_FINALNL: i64 = 19 |
| 116 | const CR_TYPESUFFIX: i64 = 20 |
| 117 | const CR_BRACEOWN: i64 = 21 |
| 118 | const CR_STRUCTALIGN: i64 = 22 |
functions
| 154 | func sf_at(s: *u8, i: i64) -> i64 { return s[i] as i64 } |
| 155 | func sf_is_ws(c: i64) -> i64 { if c == 32 { return 1 } if c == 9 { return 1 } if c == 13 { return 1 } return 0 } |
| 156 | func sf_is_idstart(c: i64) -> i64 { if is_alpha(c) == 1 { return 1 } if c == 95 { return 1 } return 0 } |
| 157 | func sf_is_idch(c: i64) -> i64 { if is_alnum(c) == 1 { return 1 } if c == 95 { return 1 } return 0 } |
| 159 | func sf_put(F: *Sf, c: i64) -> i64 |
| 166 | func sf_put_raw(F: *Sf, a: i64, b: i64) -> i64 |
| 171 | func sf_spaces(F: *Sf, k: i64) -> i64 { var i: i64 = 0; while i < k { sf_put(F, 32); i = i + 1 } return 0 } |
| 174 | func sf_kw_class(s: *u8, a: i64, b: i64) -> i64 called by 1: sf_run |
| 201 | func sf_directive(s: *u8, i: i64, n: i64) -> i64 |
| 213 | func _sf_eq(p: *u8, lit: *u8, k: i64) -> i64 |
| 220 | func sf_string_end(s: *u8, i: i64, n: i64) -> i64 |
| 236 | func sf_macro_end(s: *u8, i: i64, n: i64) -> i64 |
| 253 | func sf_eol(s: *u8, i: i64, n: i64) -> i64 |
| 261 | func sf_rtrim(s: *u8, a: i64, b: i64) -> i64 |
| 267 | func sf_cen(F: *Sf, rule: i64, agree: i64) -> i64 |
| 278 | func sf_field_run_target(s: *u8, j: i64, n: i64) -> i64 |
| 315 | func sf_sep(F: *Sf, prev: i64, cur: i64) -> i64 called by 1: sf_emit_sep |
| 349 | func sf_cen_rule(prev: i64, cur: i64, kind_cur: i64, kind_prev: i64) -> i64 called by 1: sf_emit_sep |
| 373 | func sf_emit_sep(F: *Sf, cur: i64, kind_cur: i64, kind_prev: i64, gap_a: i64, gap_b: i64) -> i64 |
| 396 | func sf_is_binary(F: *Sf) -> i64 called by 1: sf_run |
| 406 | func sf_punct(s: *u8, i: i64, n: i64, out: *i64) -> i64 |
| 451 | func sf_line_shape(s: *u8, i: i64, n: i64, out: *i64) -> i64 |
| 482 | func sf_end_line(F: *Sf, kind: i64) -> i64 |
| 501 | func sf_begin_line(F: *Sf, nclose: i64, closer: i64, first_tok: i64, raw_indent: i64) -> i64 |
| 530 | func srcfmt_emit(src: *u8, n: i64, out: *u8, cap: i64) -> i64 |
| 547 | func srcfmt_emit_census(src: *u8, n: i64, out: *u8, cap: i64, cen: *i64) -> i64 |
| 563 | func sf_run(F: *Sf) -> i64 |
| 839 | func srcfmt_cap(src: *u8, n: i64) -> i64 |
| 845 | func srcfmt_cap_ceiling(src: *u8, n: i64) -> i64 |
| 854 | func srcfmt_emit_grow(src: *u8, n: i64, outp: *i64, capp: *i64, cen: *i64) -> i64 |
| 872 | func srcfmt_verify(a: *u8, an: i64, b: *u8, bn: i64) -> i64 |
| 894 | func sf_tok_same(x: *Tok, y: *Tok) -> i64 called by 1: srcfmt_verify |
| 918 | func sf_puts(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } |
| 919 | func sf_putn(fd: i64, v: i64) -> i64 |
| 932 | func sf_streq(a: *u8, b: *u8) -> i64 called by 1: main |
| 938 | func sf_same_bytes(a: *u8, an: i64, b: *u8, bn: i64) -> i64 |
| 947 | func srcfmt_file(path: *u8, outp: *i64, inp: *i64, inn: *i64) -> i64 |
| 963 | func sf_census_name(r: i64) -> *u8 called by 1: sf_census |
| 990 | func sf_usage() -> i64 |
| 995 | func sf_census(dir: *u8) -> i64 |
| 1051 | func main(argc: i64, argv: *i64) -> i64 |
| 1088 | func mp_write_all_sf(fd: i64, buf: *u8, n: i64) -> i64 |