code wiki / (root) / nx_srcfmt.nx

nx_srcfmt.nx

buildroot/runtime/nx_srcfmt.nx

51757 B1096 linesdepth 3pulls 6 transitivereach 1 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_tokenizer.nx nx_gatekit_lib.nx nx_srcfmt.nx nx_srcfmt_gate.nx

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

main sf_usage sf_puts sys_write sf_streq sf_census sys_mmap nxa_die sys_write ↻ sys_exit nxa_lock_take nxa_lock_addr sys_write ↻ nxa_lock_give nxa_lock_addr ↻ nxa_report_overrun sys_write ↻ nxa_dump_printable sys_write ↻ nxa_dump_sizes sys_write ↻ gk_corpus_scan sys_mmap ↻ gk_corpus_root sys_mmap ↻ gk_cat gk_exists sys_openat_rd sys_close gk_corpus_hdl gk_corpus_root ↻ gk_len gk_cat ↻ gk_dirscan gk_dirscan_ext sys_openat_rd ↻ sys_mmap ↻ gk_ext_is gk_len ↻ gk_cat ↻

structs

120struct Sf

consts

71const SF_INDENT: i64 = 4 // the measured unit: 2,216,007 multiple-of-4 lines vs 9,644 other
72const SF_NAME_SLOT: i64 = 128 // per-path byte slot for the corpus census (same as MP_SLOT)
73const SF_CORPUS_CAP: i64 = 32768 // corpus is 18,561; 16,384 would REFUSE (same bar as nx_srclint)
74const SF_CENSUS_ROWS: i64 = 24 // rule rows the census verb reports
77const SP_NONE: i64 = 0 // start of line
78const SP_IDENT: i64 = 1
79const SP_KW: i64 = 2 // keyword that is not an operand (if while return as let ...)
80const SP_OPERAND: i64 = 3 // int / float / string / true / false
81const SP_LPAREN: i64 = 4
82const SP_RPAREN: i64 = 5
83const SP_LBRACK: i64 = 6
84const SP_RBRACK: i64 = 7
85const SP_LBRACE: i64 = 8
86const SP_RBRACE: i64 = 9
87const SP_COMMA: i64 = 10
88const SP_SEMI: i64 = 11
89const SP_COLON: i64 = 12
90const SP_GLUE: i64 = 13 // . .. :: -- no space either side
91const SP_BINOP: i64 = 14 // every binary operator, = -> => ? and the ternary :
92const SP_UNARY: i64 = 15 // - * & + ! ~ used as a prefix: nothing after it
93const SP_ATTR: i64 = 16 // @priv and any @name that is not a directive; #name
96const CR_INDENT: i64 = 0
97const CR_TRAIL: i64 = 1
98const CR_CRLF: i64 = 2
99const CR_COMMA: i64 = 3
100const CR_SEMI: i64 = 4
101const CR_BINOP: i64 = 5
102const CR_ASSIGN: i64 = 6
103const CR_ARROW: i64 = 7
104const CR_COLON: i64 = 8
105const CR_UNARY: i64 = 9
106const CR_CALLPAREN: i64 = 10
107const CR_KWPAREN: i64 = 11
108const CR_LBRACE: i64 = 12
109const CR_BLOCKPAD: i64 = 13
110const CR_EMPTYBLOCK: i64 = 14
111const CR_GLUE: i64 = 15
112const CR_ELSEJOIN: i64 = 16
113const CR_BLANKRUN: i64 = 17
114const CR_CONT: i64 = 18
115const CR_FINALNL: i64 = 19
116const CR_TYPESUFFIX: i64 = 20
117const CR_BRACEOWN: i64 = 21
118const CR_STRUCTALIGN: i64 = 22

functions

154func sf_at(s: *u8, i: i64) -> i64 { return s[i] as i64 }
155func sf_is_ws(c: i64) -> i64 { if c == 32 { return 1 } if c == 9 { return 1 } if c == 13 { return 1 } return 0 }
156func sf_is_idstart(c: i64) -> i64 { if is_alpha(c) == 1 { return 1 } if c == 95 { return 1 } return 0 }
called by 2: sf_field_run_targetsf_run calls 1: is_alpha
157func sf_is_idch(c: i64) -> i64 { if is_alnum(c) == 1 { return 1 } if c == 95 { return 1 } return 0 }
159func sf_put(F: *Sf, c: i64) -> i64
166func sf_put_raw(F: *Sf, a: i64, b: i64) -> i64
called by 1: sf_run calls 2: sf_putsf_at
171func sf_spaces(F: *Sf, k: i64) -> i64 { var i: i64 = 0; while i < k { sf_put(F, 32); i = i + 1 } return 0 }
called by 2: sf_emit_sepsf_begin_line calls 1: sf_put
174func sf_kw_class(s: *u8, a: i64, b: i64) -> i64
called by 1: sf_run
201func sf_directive(s: *u8, i: i64, n: i64) -> i64
called by 1: sf_run calls 1: _sf_eq
213func _sf_eq(p: *u8, lit: *u8, k: i64) -> i64
220func sf_string_end(s: *u8, i: i64, n: i64) -> i64
called by 1: sf_run calls 1: sf_at
236func sf_macro_end(s: *u8, i: i64, n: i64) -> i64
called by 1: sf_run calls 3: sf_atis_alnumlexm_body_end
253func sf_eol(s: *u8, i: i64, n: i64) -> i64
261func sf_rtrim(s: *u8, a: i64, b: i64) -> i64
called by 2: sf_line_shapesf_run calls 2: sf_is_wssf_at
267func sf_cen(F: *Sf, rule: i64, agree: i64) -> i64
278func sf_field_run_target(s: *u8, j: i64, n: i64) -> i64
315func sf_sep(F: *Sf, prev: i64, cur: i64) -> i64
called by 1: sf_emit_sep
349func sf_cen_rule(prev: i64, cur: i64, kind_cur: i64, kind_prev: i64) -> i64
called by 1: sf_emit_sep
373func sf_emit_sep(F: *Sf, cur: i64, kind_cur: i64, kind_prev: i64, gap_a: i64, gap_b: i64) -> i64
396func sf_is_binary(F: *Sf) -> i64
called by 1: sf_run
406func sf_punct(s: *u8, i: i64, n: i64, out: *i64) -> i64
called by 1: sf_run calls 1: sf_at
451func sf_line_shape(s: *u8, i: i64, n: i64, out: *i64) -> i64
482func sf_end_line(F: *Sf, kind: i64) -> i64
called by 1: sf_run calls 3: sf_is_wssf_atsf_put
501func sf_begin_line(F: *Sf, nclose: i64, closer: i64, first_tok: i64, raw_indent: i64) -> i64
called by 1: sf_run calls 3: sf_putsf_censf_spaces
530func srcfmt_emit(src: *u8, n: i64, out: *u8, cap: i64) -> i64
called by 1: srcfmt_emit_grow calls 2: sys_mmapsf_run
547func srcfmt_emit_census(src: *u8, n: i64, out: *u8, cap: i64, cen: *i64) -> i64
called by 1: srcfmt_emit_grow calls 2: sys_mmapsf_run
563func sf_run(F: *Sf) -> i64
839func srcfmt_cap(src: *u8, n: i64) -> i64
called by 1: srcfmt_emit_grow calls 1: sf_at
845func srcfmt_cap_ceiling(src: *u8, n: i64) -> i64
called by 1: srcfmt_emit_grow calls 1: sf_at
854func srcfmt_emit_grow(src: *u8, n: i64, outp: *i64, capp: *i64, cen: *i64) -> i64
872func srcfmt_verify(a: *u8, an: i64, b: *u8, bn: i64) -> i64
894func sf_tok_same(x: *Tok, y: *Tok) -> i64
called by 1: srcfmt_verify
918func 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 }
919func sf_putn(fd: i64, v: i64) -> i64
called by 2: sf_censusmain calls 2: sys_mmapsf_puts
932func sf_streq(a: *u8, b: *u8) -> i64
called by 1: main
938func sf_same_bytes(a: *u8, an: i64, b: *u8, bn: i64) -> i64
called by 2: sf_censusmain
947func srcfmt_file(path: *u8, outp: *i64, inp: *i64, inn: *i64) -> i64
963func sf_census_name(r: i64) -> *u8
called by 1: sf_census
990func sf_usage() -> i64
called by 1: main calls 1: sf_puts
995func sf_census(dir: *u8) -> i64
1051func main(argc: i64, argv: *i64) -> i64
1088func mp_write_all_sf(fd: i64, buf: *u8, n: i64) -> i64
called by 1: main calls 1: sys_write