x-lang

← Index

x/tool/fmt

Comment-preserving s-expression formatter on the Fmt class.

Formatting logic is %-private (the mutually-recursive printers call each other directly); the Fmt class is the API.

Class Fmt

Comment-preserving, data-driven pretty printer for x-lang s-expressions.

Build a formatter table from construct declarations with (Fmt build-table …), then (Fmt tokens toks table) prints them. A form under 60 chars wide prints as-is; wider ones indent per the table’s fmt property (head-1 / head-kw / body / default).

(Fmt build-table constructs)

Build a formatter lookup table from construct declarations.

Parameters:

Returns: ALIST — Lookup table mapping names to property lists

(Fmt lookup name table)

Look up formatting properties for a construct name.

Parameters:

Returns: LIST — Property list, or nil

(Fmt get-prop key props)

Get a specific property from a construct property list.

Parameters:

Returns: ANY — Property value, or nil

(Fmt comment? tok)

Test whether a token is a comment token.

Parameters:

Returns: BOOL — True if tok is a (%comment …) token

(Fmt width form)

Estimate the display width of a form in CODE POINTS (via write-to-str; a comment counts as 80). Code points approximate display columns except for double-width/combining glyphs (wcwidth is a known gap).

Parameters:

Returns: INT — Estimated width in code points

(Fmt expr form col)

Format any expression, writing the result to output.

Parameters:

Returns: ANY — nil (output via display)

(Fmt list form col table)

Format a list form with indentation-aware pretty printing, writing the result.

Parameters:

Returns: ANY — nil (output via display)

(Fmt body forms col)

Format a sequence of body forms, one per line, writing the result.

Parameters:

Returns: ANY — nil (output via display)

(Fmt tokens tokens table)

Format a list of top-level tokens with the given construct table, writing the result.

Parameters:

Returns: ANY — nil (output via display)

(Fmt fold-sugar! on)

Set whether (lit x) / (quasi x) / (unquote x) fold to ‘ ` , sugar in output. ON is the docs/syntax.md ruling for post-boot code; a driver formatting a BOOT-CONSTRAINED file (its text loads before the quote reader arms) must turn it OFF or the output cannot boot (#307).

Parameters:

Returns: ANY — nil