How `refine fmt` lays out a source
refine fmt FILE.ldn rewrites a source in Landin's one layout, and refine fmt DIR every .ldn file under a directory, a dot-named directory left out. refine fmt --check writes nothing and reports every line that is not in the layout, each with the line as the layout writes it and marked from the column where it differs, a blank line it removes or a line end it changes; refine fmt --stdout FILE.ldn writes one source to standard output in the layout and leaves its file as it is (D359, D385). This page shows each rule of the layout with a program as written and the same program formatted.
D252 names a decision recorded in spec.md, and [0280] a numbered paragraph of tour.md or spec.md; refine explain D252 prints either.
A layout is a decision about space. The formatter never adds, removes or changes a token, never changes a comment's bytes, never breaks a long line and never joins two lines. It decides where each line starts, the space between two things on one line, which blank lines survive and how line ends outside raw literals and block comments are normalized. There is no option that changes any of that.
A file that does not scan or parse is refused with the report a compilation would give it, and nothing is written. Formatting reads nothing later than the parse, so a file whose names or types are wrong is formatted all the same. The layout is scanned and parsed again before it is given: one that changed a token or a comment, or that no longer parses, is a compiler defect and nothing is written. Where a line begins decides nothing in a program that parses (D1074), so the second cannot happen while that holds.
What refine fmt does to bytes no example can show:
| written | formatted |
|---|---|
| a CR LF or a lone CR ending a line outside a raw literal or block comment | LF |
| blanks or tabs at the end of a line | removed |
| a tab in a line's indentation or between two tokens | spaces, as the rules below place them |
| no line end after the last line | one LF |
| blank lines before the first line or after the last | removed |
| a file of nothing but space | an empty file |
The bytes inside a raw literal [0280] and inside a block comment [1780] are the literal's and the comment's, their line ends included, and never change. Formatted source can therefore retain CR LF or lone CR line ends inside them.
refine fmt writes the layout through the descriptor it read the original bytes from, into the same file, so everything the file carries stays with it: its owner and group, its extended attributes and ACLs, even those the caller cannot list, and every other name a hard link gives it, which sees the formatted bytes too (D358). It needs no privilege, and it follows a symlink to its target. It needs a writable ordinary file and write permission in that file's directory, because before the first byte changes it copies the original bytes into a file beside it, named .fmt-original- and a suffix unique to the run, and syncs that copy and the directory. When the new bytes are synced the copy is removed. A write that fails puts the original bytes back and removes the copy; one that cannot even do that keeps the copy and says where it is, as a crash during the write leaves it. Writing clears a set-user-ID or set-group-ID bit, so the formatter sets it again and reads the mode back; a file whose bit the caller could not set again, because it is not the owner or for set-group-ID not in the file's group, is refused before it is written, as is a Linux file with capabilities, which a write removes. A read-only file is refused. These checks refuse before writing and leave the original bytes as they were. A later failure says whether the original bytes were restored or where their recovery copy remains. The modification time changes, as for any write. The copy and the parent directory are pinned while used. A save renamed over the source is never overwritten. If the name changes after the last check, the formatter can have updated the old inode, which hard links still name, but reports that it did not install the layout at the new name and retains its recovery copy. It never restores over the editor's save. Concurrent writes through another descriptor to the same inode remain unsupported: identity checks do not exclude that writer. --check and --stdout need no writable descriptor. D907 records the shared publication and recovery rule.
Rules
Blocks
A block is indented four spaces from the line its construct begins on, and the end that closes it, and any else or elsif that divides it, stands on that line's indentation. A construct that opens and closes on one line stays on it, which is how a guarded trap is written (D436).
f: (n: i32) -> (r: i32) = if n > 99 then compiler.unreachable() end if if n > 0 then r = 1 else r = 0 end if end f
f: (n: i32) -> (r: i32) = if n > 99 then compiler.unreachable() end if if n > 0 then r = 1 else r = 0 end if end f
Declarations
A declaration of a module begins at the margin, and one written below its link or extern attributes begins where they do.
counter: u32 = 0 link(section: ".data") mirror: u32 = 0
counter: u32 = 0 link(section: ".data") mirror: u32 = 0
Blank lines
A run of blank lines becomes one, and a file neither begins nor ends with one.
first: u32 = 1 second: u32 = 2
first: u32 = 1 second: u32 = 2
Space between two tokens
Two tokens on one line are separated by one space, except where the table below says none. A comma or a colon takes one space after it and none before.
| no space | example |
|---|---|
inside (, ), [ and ] | f(a, b), [1, 2] |
before a call's, an application's or a pattern's ( | f(a), vec.list(u8), []u8(text), ptr u8(value), some(value): |
before an index's or a slice's [ | values[0], values[0..<2] |
either side of ., .. and ..< | a.b, 0..<n |
after a prefix - or ~ | -1, ~mask |
after the ] of an array or slice type | [4]u8, []mut u8 |
either side of an import path's / | import core/mem |
before the ( of ptr(...), any(...), extern(...), link(...) and layout(...) | ptr(address), extern(c) |
Every binary operator, =, :=, ->, ! and | has one space on each side. A type's or concept's formals, a conformance's entries, a recovery's name and a pointer arm's or a _ arm's binding stand one space from what precedes them: type (item: type), is log (note: n), else (problem), ptr (p):, _ (rest):. A case arm's payload touches its case, red(shade):, as a call's arguments touch what it calls: the parentheses take that case's payload apart. ptr and _ name no case, and their binding is the matched value itself (D434). A prefix - keeps a space before another -, because -- begins a comment. An applied written type keeps its ordinary type spacing and puts no space before the value argument's (: []mut u8(value), [4]u8(value) and ptr mut u8(value). A function type retains spaces around -> and joins the value list to its final return list, as in (x: i32) -> (y: i32)(value). A parameterized head joins both argument lists: tag(u32, 7)(value).
f:(a:i32,b :i32)->(r :i32)= r=a*( b+ -1 )- -a x :=values [0 ..< 2] y := g (a) bytes := [ ] u8 ( text ) pointer := ptr u8 (hidden) callback := (x:i32)->(y:i32) (wrapped) tagged := tag (u32,7) (value) match c red (shade): g(shade) _(rest): h(rest) end match end f
f: (a: i32, b: i32) -> (r: i32) = r = a * (b + -1) - -a x := values[0..<2] y := g(a) bytes := []u8(text) pointer := ptr u8(hidden) callback := (x: i32) -> (y: i32)(wrapped) tagged := tag(u32, 7)(value) match c red(shade): g(shade) _ (rest): h(rest) end match end f
Signatures
A signature continued on another line puts its -> and its ! under the ( that opens its parameters, and each further | of its error set under the !.
reserve: (item: type, inout value: list(item), want: usize) -> none ! out_of_memory | too_large = end reserve
reserve: (item: type, inout value: list(item), want: usize) -> none ! out_of_memory | too_large = end reserve
Lists
A list continued on another line stands under its first entry when that entry shares the opening bracket's line, and four spaces in from that line when the bracket ends it. A closing bracket that begins a line stands where the opening bracket's line began.
f: () -> none = state = (inner: inner, remaining: 3) table := [ 1, 2, 3 ] end f
f: () -> none = state = (inner: inner, remaining: 3) table := [ 1, 2, 3 ] end f
Operations
A line that continues an operation, with its operator or with the operand after one, stands under the operation's left operand. In a condition, after if, elsif, while or when, it stands two spaces in from the line the condition begins on, so that it is never taken for the block the condition opens.
f: () -> none = ok = first and second and third if first and second then g() end if end f
f: () -> none = ok = first and second and third if first and second then g() end if end f
Parentheses
Inside parentheses an operation continues under its first operand, one column past the (, because that is where its left operand begins. Outside them a parenthesized left operand begins at its (, so a line that continues the operation stands under the (, or in a condition two spaces in as any other.
f: () -> none = ok = (first and second) both = (first or second) and third if (first or second) and third then g() end if end f
f: () -> none = ok = (first and second) both = (first or second) and third if (first or second) and third then g() end if end f
Recovery
A recovery clause's body is indented from the line its call begins on, and its end stands on that line's indentation, however the else that opens it is placed. An else that begins a line continues the call, and a bound recovery in an if arm is laid out as it is anywhere else. else none has no body, so it is laid out as the one-value form is.
f: () -> none = block := allocate(state, size) else (problem) _ = problem return end end f
f: () -> none = block := allocate(state, size) else (problem) _ = problem return end end f
An if inside a line
An if that begins inside a line is an expression, laid out from its if rather than from the start of its line, wherever the if stands, an argument's place included. A then that begins a line, each elsif and else, and its end if stand under the if, and each arm's body four columns in from it. When the first arm's value shares the if's line, the arms form a table, each on a line of its own.
f: (byte: u8, wide: bool) -> (digit: u32) = digit = if byte == 48 then 0 elsif byte == 49 then 1 else 9 end if digit = if wide and byte > 57 then 10 else digit end if digit = add(digit, if wide then 16 else 0 end if) end f
f: (byte: u8, wide: bool) -> (digit: u32) = digit = if byte == 48 then 0 elsif byte == 49 then 1 else 9 end if digit = if wide and byte > 57 then 10 else digit end if digit = add(digit, if wide then 16 else 0 end if) end f
Other continuations
Any other line that continues a declaration or a statement stands four spaces in from the line it began on. A line that begins with a ( whose ) is followed by := continues nothing: it is a destructuring binding of its own [1750], and stands where its block's statements do.
f: () -> none = selected: map.entry(key, item) = map.next_entry(value, position) end f
f: () -> none = selected: map.entry(key, item) = map.next_entry(value, position) end f
Comments
A comment on a line of its own takes the indentation of the next line of code, or of the block's body when that line closes the block. A comment after code on the same line stands one space after it. Nothing inside a comment changes, and it stays between the same two tokens.
The layout holds a comment to no width, since it never breaks a line. The library holds its own --- documentation comments to 79 columns, the indentation and the marker included, and a full python3 check.py refuses a longer line in core, hosted or platform (D1317); a program's comments are its own.
f: () -> none = -- first g() -- still in the block end f -- at the end h: u32 = 1 -- one
f: () -> none = -- first g() -- still in the block end f -- at the end h: u32 = 1 -- one
Where this comes from
This page is derived: D252 in spec.md records the layout and what it was chosen over, and Landin.Formatting is the implementation. The test program formats every example on this page, requires the result shown beneath it, and requires the result to be formatted already. check.py holds each rule to one pair of examples and both to the grammar.