From db7a7dd5391944d505cf45a50073d26585ab0f85 Mon Sep 17 00:00:00 2001 From: Alexander Satretdinov Date: Mon, 16 Nov 2026 21:14:26 +0200 Subject: [PATCH] fmt: a block holding a block goes on lines of its own, with the blocks chained by else or catch and the arms of such a match; the parser tells the formatter which braces open blocks --- docs/src/getting-started.md | 4 +- examples/kv/main.rn | 19 ++++++-- examples/kv/store.rn | 8 +++- examples/log-report.rn | 4 +- examples/query/aggregate.rn | 16 ++++--- examples/query/plan.rn | 8 +++- examples/todo.rn | 31 +++++++----- pkg/format/format.go | 94 +++++++++++++++++++++++++++++++++---- pkg/format/format_test.go | 2 +- pkg/rune/parse_match.go | 2 + pkg/rune/parse_stmt.go | 10 +--- pkg/rune/parser.go | 12 ++++- pkg/rune/scanner.go | 13 ++--- testdata/fmt/layout.golden | 37 +++++++++++++++ testdata/fmt/layout.rn | 9 ++++ 15 files changed, 217 insertions(+), 52 deletions(-) diff --git a/docs/src/getting-started.md b/docs/src/getting-started.md index 088cdeb..8d30a38 100644 --- a/docs/src/getting-started.md +++ b/docs/src/getting-started.md @@ -80,7 +80,9 @@ hello, ann! | `rune version` | Prints the interpreter's version. | `rune fmt` gives every program one layout: 4-space indents, spaces around -operators, one statement per line. +operators, one statement per line. A block that holds another block goes +on lines of its own, and so do the blocks chained to it by `else` or +`catch`, and each arm of such a `match`. ```sh $ cat messy.rn diff --git a/examples/kv/main.rn b/examples/kv/main.rn index 6ddba55..093f44f 100644 --- a/examples/kv/main.rn +++ b/examples/kv/main.rn @@ -13,7 +13,9 @@ import .codec; fun ms(since) { "%.1f ms".format((time.now() - since) * 1000) } -fun kb(n) { if n < 1024 { "{n} B" } else { "%.1f KB".format(n / 1024) } } +fun kb(n) { + if n < 1024 { "{n} B" } else { "%.1f KB".format(n / 1024) } +} fun show(info) { "{info.keys} keys, {info.segments} segments, {kb(info.size)} on disk, {kb(info.dead)} dead" } @@ -75,12 +77,23 @@ fun demo() { let mut broken = bytes.buffer(good); broken[100] ^= 0xFF; fs.write(first, broken); - try { store.Store.open(dir, opts); } catch e is store.KvError { print(" open failed: {e.message}"); } + + try { + store.Store.open(dir, opts); + } catch e is store.KvError { + print(" open failed: {e.message}"); + } + fs.write(first, good); print("\n== a second writer"); db = store.Store.open(dir, opts); - try { store.Store.open(dir, opts); } catch e is store.KvError { print(" open failed: {e.message.replace(dir, "")}"); } + + try { + store.Store.open(dir, opts); + } catch e is store.KvError { + print(" open failed: {e.message.replace(dir, "")}"); + } print("\n== compaction"); t = time.now(); diff --git a/examples/kv/store.rn b/examples/kv/store.rn index 00344cc..ff10000 100644 --- a/examples/kv/store.rn +++ b/examples/kv/store.rn @@ -92,7 +92,13 @@ impl Store { // sync: fsync after every write, not only flush it to the system fun open(dir, { maxSegment = 4 << 20, sync = false } = {}) { fs.mkdir(dir); - let lock = try { fs.lock(fs.join(dir, "LOCK")) } catch e is IOError { throw if e.code == "locked" { KvError { message: "{dir} is open elsewhere" } } else { e } }; + + let lock = try { + fs.lock(fs.join(dir, "LOCK")) + } catch e is IOError { + throw if e.code == "locked" { KvError { message: "{dir} is open elsewhere" } } else { e } + }; + let mut db = Store { dir, maxSegment, sync, lock }; try { diff --git a/examples/log-report.rn b/examples/log-report.rn index 3b0145c..85e225b 100644 --- a/examples/log-report.rn +++ b/examples/log-report.rn @@ -65,7 +65,9 @@ impl Stats { return ps.map(p => xs[math.min(xs.len() - 1, math.floor(xs.len() * p / 100))]); } - fun errorRate(self) { if self.count == 0 { 0 } else { self.errors / self.count * 100 } } + fun errorRate(self) { + if self.count == 0 { 0 } else { self.errors / self.count * 100 } + } } const TOP_SLOW = 5; diff --git a/examples/query/aggregate.rn b/examples/query/aggregate.rn index ee1cf39..7f94f74 100644 --- a/examples/query/aggregate.rn +++ b/examples/query/aggregate.rn @@ -91,12 +91,16 @@ fun accumulate(mut acc, aggs, r) { acc[i][0] += v; acc[i][1] += 1; }, - Agg.Min => { if acc[i] == nil || expr.compare(v, acc[i]) < 0 { - acc[i] = v; - } }, - Agg.Max => { if acc[i] == nil || expr.compare(v, acc[i]) > 0 { - acc[i] = v; - } } + Agg.Min => { + if acc[i] == nil || expr.compare(v, acc[i]) < 0 { + acc[i] = v; + } + }, + Agg.Max => { + if acc[i] == nil || expr.compare(v, acc[i]) > 0 { + acc[i] = v; + } + } } } } diff --git a/examples/query/plan.rn b/examples/query/plan.rn index faafa4c..5c93561 100644 --- a/examples/query/plan.rn +++ b/examples/query/plan.rn @@ -116,9 +116,13 @@ fun filter(conds, ctx) { return rows => rows.filter(r => ok(r) == true); } -fun whereText(conds) { if conds { " where {ast.sql(ast.allOf(conds))}" } else { "" } } +fun whereText(conds) { + if conds { " where {ast.sql(ast.allOf(conds))}" } else { "" } +} -fun describe(ref) { if ref.alias == ref.name { ref.name } else { "{ref.name} {ref.alias}" } } +fun describe(ref) { + if ref.alias == ref.name { ref.name } else { "{ref.name} {ref.alias}" } +} // An ON with a conjunct a = b, each side on one table, is a hash join on // it, the rest checked on each match; anything else is a nested loop. diff --git a/examples/todo.rn b/examples/todo.rn index 3821142..4d6ebe2 100644 --- a/examples/todo.rn +++ b/examples/todo.rn @@ -48,6 +48,7 @@ fun parseDate(s, now) { } catch e is ValueError { throw UsageError { message: "bad date '{s}'" }; } + return s; } @@ -254,18 +255,24 @@ fun run(mut store, [cmd = "list", ...args], now) { print(" added #{id}"); }, "list" | "ls" => list(store, args, now), - "done" => { for id in ids(args) { - let t = store.update(id, t => Task { ...t, completed: now }); - print(" done #{id} {t.text}"); - } }, - "undo" => { for id in ids(args) { - store.update(id, t => Task { ...t, completed: nil }); - print(" reopened #{id}"); - } }, - "rm" => { for id in ids(args) { - let t = store.remove(id); - print(" removed #{id} {t.text}"); - } }, + "done" => { + for id in ids(args) { + let t = store.update(id, t => Task { ...t, completed: now }); + print(" done #{id} {t.text}"); + } + }, + "undo" => { + for id in ids(args) { + store.update(id, t => Task { ...t, completed: nil }); + print(" reopened #{id}"); + } + }, + "rm" => { + for id in ids(args) { + let t = store.remove(id); + print(" removed #{id} {t.text}"); + } + }, "edit" => { let [id = throw UsageError { message: "which task? give an id" }, ...words] = args; let changes = parseTask(words, now); diff --git a/pkg/format/format.go b/pkg/format/format.go index 967b4cd..de0fc84 100644 --- a/pkg/format/format.go +++ b/pkg/format/format.go @@ -1,6 +1,7 @@ // Package format lays out rune source: indentation, spacing between -// tokens, a line per statement and blank lines around blocks. Other line -// breaks, comments and string literals stay as written. +// tokens, a line per statement, a block holding another block on lines of +// its own and blank lines around blocks. Other line breaks, comments and +// string literals stay as written. package format import ( @@ -19,6 +20,8 @@ type atom struct { text string role role before gap + // brace says what a { opens. + brace brace // indent is how far the source line of the atom is indented. indent int } @@ -37,6 +40,16 @@ const ( func (a *atom) ended() bool { return a.role == operand } // gap is what precedes an atom: comments, and the newlines before each. +// brace is what a { opens: a block, a match body, or neither, as for an +// object. +type brace int + +const ( + plain brace = iota + block + matchBody +) + type gap struct { comments []comment breaks int @@ -65,12 +78,13 @@ type printer struct { // Source formats src, or returns the syntax errors that stop it. func Source(src []byte) ([]byte, []error) { - tokens, errs := rune.Parse(src) + tokens, braces, errs := rune.Parse(src) if len(errs) > 0 { return nil, errs } - atoms, tail := split(string(src), tokens) + atoms, tail := split(string(src), tokens, braces) + expand(atoms) layout(atoms) p := &printer{frames: []frame{{}}} @@ -95,14 +109,22 @@ func Source(src []byte) ([]byte, []error) { return out, nil } -// split makes atoms of tokens, and returns them with the comments after -// the last one. -func split(s string, tokens []ast.Token) ([]atom, []comment) { +// split makes atoms of tokens, marking what each { opens, and returns +// them with the comments after the last one. +func split(s string, tokens []ast.Token, braces rune.Braces) ([]atom, []comment) { var atoms []atom from, ended, importing := 0, false, false + blocks, matches := braces.Blocks, braces.Matches for i := 0; i < len(tokens)-1; i++ { t := tokens[i] + opens := plain + switch { + case len(blocks) > 0 && blocks[0] == i: + opens, blocks = block, blocks[1:] + case len(matches) > 0 && matches[0] == i: + opens, matches = matchBody, matches[1:] + } start := t.Offset if t.TokenType == ast.INTERPOLATION { @@ -111,7 +133,7 @@ func split(s string, tokens []ast.Token) ([]atom, []comment) { t.TokenType = ast.STRING } - a := atom{tok: t, text: t.Lexeme, before: parseGap(s[from:start]), indent: lineIndent(s, start)} + a := atom{tok: t, text: t.Lexeme, before: parseGap(s[from:start]), indent: lineIndent(s, start), brace: opens} a.role = classify(&a, ended) if importing { a.role = path @@ -126,6 +148,62 @@ func split(s string, tokens []ast.Token) ([]atom, []comment) { return atoms, parseGap(s[from:]).comments } +// expand puts the inside of a block that holds another block on lines of +// its own, each arm of a match body too, and so the blocks chained to it +// by else or catch. +func expand(atoms []atom) { + n := len(atoms) + closing, owner, chained := make([]int, n), make([]int, n), make([]int, n) + nested := make([]bool, n) + + var opens []int + // link is the block an else or catch chains the next block at its depth to. + link := -1 + for i := range atoms { + owner[i], chained[i] = -1, i + if len(opens) > 0 { + owner[i] = opens[len(opens)-1] + } + + switch t := atoms[i].tok.TokenType; { + case opener(t): + if atoms[i].brace != plain { + for _, o := range opens { + nested[o] = true + } + if link >= 0 && owner[i] == owner[link] { + chained[i], link = chained[link], -1 + } + } + opens = append(opens, i) + case closer(t) && len(opens) > 0: + o := opens[len(opens)-1] + opens = opens[:len(opens)-1] + closing[o] = i + if atoms[o].brace != plain && i+1 < n && (atoms[i+1].tok.TokenType == ast.ELSE || atoms[i+1].tok.TokenType == ast.CATCH) { + link = o + } + } + } + + for i := range atoms { + if atoms[i].brace != plain && nested[i] { + nested[chained[i]] = true + } + } + + open := func(o int) bool { return o >= 0 && atoms[o].brace != plain && nested[chained[o]] } + for i := range atoms { + switch { + case open(i) && closing[i] > i+1: + lineBreak(&atoms[i+1], 1) + lineBreak(&atoms[closing[i]], 1) + case atoms[i].tok.TokenType == ast.COMMA && open(owner[i]) && atoms[owner[i]].brace == matchBody && i+1 < closing[owner[i]]: + lineBreak(&atoms[i+1], 1) + } + } +} + // layout puts each statement on a line of its own, and blank lines // around a statement holding a block that spans lines. func layout(atoms []atom) { diff --git a/pkg/format/format_test.go b/pkg/format/format_test.go index 7433601..a1c5df9 100644 --- a/pkg/format/format_test.go +++ b/pkg/format/format_test.go @@ -16,7 +16,7 @@ func TestRepoSources(t *testing.T) { return err } src, _ := os.ReadFile(path) - if _, errs := rune.Parse(src); len(errs) > 0 { + if _, _, errs := rune.Parse(src); len(errs) > 0 { return nil } diff --git a/pkg/rune/parse_match.go b/pkg/rune/parse_match.go index f0af0dd..014e25f 100644 --- a/pkg/rune/parse_match.go +++ b/pkg/rune/parse_match.go @@ -21,6 +21,8 @@ func (s *Parser) matchExpr() (*ast.MatchExpr, error) { return nil, err } + s.braces.Matches = append(s.braces.Matches, s.current-1) + var arms []ast.MatchArm for !s.check(ast.RIGHT_BRACE) && !s.isAtEnd() { diff --git a/pkg/rune/parse_stmt.go b/pkg/rune/parse_stmt.go index d40145f..9e2fcc9 100644 --- a/pkg/rune/parse_stmt.go +++ b/pkg/rune/parse_stmt.go @@ -515,21 +515,13 @@ func (s *Parser) braced(valued bool) (*ast.BlockStmt, error) { } s.advance() + s.braces.Blocks = append(s.braces.Blocks, s.current-1) defer s.allowStructs(structsAll)() return s.blockOf(valued) } -func (s *Parser) block() ([]ast.Stmt, error) { - b, err := s.blockOf(false) - if err != nil { - return nil, err - } - - return b.Stmts, nil -} - // blockOf parses statements up to "}". The last one, when no ';' ends // it, is the value of a valued block: an expression, or an if or match // with values. diff --git a/pkg/rune/parser.go b/pkg/rune/parser.go index e5b4e92..55f945b 100644 --- a/pkg/rune/parser.go +++ b/pkg/rune/parser.go @@ -19,7 +19,14 @@ type Parser struct { blockDepth int // inout has a bit for each argument a mut parameter of some function // parsed takes. - inout uint32 + inout uint32 + braces Braces +} + +// Braces holds the indexes of the '{' tokens that open a block, and of +// those that open a match body. +type Braces struct { + Blocks, Matches []int } func newParser(tokens []ast.Token) *Parser { @@ -57,6 +64,7 @@ type fileAST struct { stmts []ast.Stmt exports []ast.Stmt inout uint32 + braces Braces } // parseFile parses a file: its imports, then its statements, which in a @@ -108,7 +116,7 @@ func parseFile(tokens []ast.Token, module bool) (*fileAST, []error) { return nil, parser.errors } - file.inout = parser.inout + file.inout, file.braces = parser.inout, parser.braces return file, nil } diff --git a/pkg/rune/scanner.go b/pkg/rune/scanner.go index 95c40c9..49224e3 100644 --- a/pkg/rune/scanner.go +++ b/pkg/rune/scanner.go @@ -39,18 +39,19 @@ func Scan(source []byte) ([]ast.Token, []error) { } // Parse scans and parses source as an entry file, for the tokens of -// valid code or every error of invalid code. -func Parse(source []byte) ([]ast.Token, []error) { +// valid code and its braces, or every error of invalid code. +func Parse(source []byte) ([]ast.Token, Braces, []error) { s := scan(source, 0) if s.truncated { - return nil, s.errors + return nil, Braces{}, s.errors } - if _, errs := parseFile(s.tokens, false); len(s.errors)+len(errs) > 0 { - return nil, append(s.errors, errs...) + file, errs := parseFile(s.tokens, false) + if len(s.errors)+len(errs) > 0 { + return nil, Braces{}, append(s.errors, errs...) } - return s.tokens, nil + return s.tokens, file.braces, nil } func scan(source []byte, file int) *Scanner { diff --git a/testdata/fmt/layout.golden b/testdata/fmt/layout.golden index 9adc1e5..4854fce 100644 --- a/testdata/fmt/layout.golden +++ b/testdata/fmt/layout.golden @@ -87,5 +87,42 @@ fun blocks() { let c = 3; } + +fun nest() { + let lock = try { + open(d) + } catch e is IOError { + throw if e.code == "x" { E { m: 1 } } else { e } + }; + + let n = if a { + if b { 1 } else { 2 } + } else { + 3 + }; + + let k = match v { + 1 => { + if a { 1 } else { 2 } + }, + _ => 0 + }; + + let s = match v { 1 => "one", _ => "many" }; + + xs.map(x => { + if x { 1 } else { 2 } + }); + + for i, x in xs { + if x { + p(i); + } + } +} + +fun short(x) { + if x { 1 } else { 2 } +} // end -- stderr -- diff --git a/testdata/fmt/layout.rn b/testdata/fmt/layout.rn index 8c8023a..8f97c3c 100644 --- a/testdata/fmt/layout.rn +++ b/testdata/fmt/layout.rn @@ -51,4 +51,13 @@ fun blocks() { try { t(); } catch e { p(e); } let c = 3; } +fun nest() { + let lock = try { open(d) } catch e is IOError { throw if e.code == "x" { E { m: 1 } } else { e } }; + let n = if a { if b { 1 } else { 2 } } else { 3 }; + let k = match v { 1 => { if a { 1 } else { 2 } }, _ => 0 }; + let s = match v { 1 => "one", _ => "many" }; + xs.map(x => { if x { 1 } else { 2 } }); + for i, x in xs { if x { p(i); } } +} +fun short(x) { if x { 1 } else { 2 } } // end