Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions examples/kv/main.rn
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down Expand Up @@ -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, "<dir>")}"); }

try {
store.Store.open(dir, opts);
} catch e is store.KvError {
print(" open failed: {e.message.replace(dir, "<dir>")}");
}

print("\n== compaction");
t = time.now();
Expand Down
8 changes: 7 additions & 1 deletion examples/kv/store.rn
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion examples/log-report.rn
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions examples/query/aggregate.rn
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/query/plan.rn
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 19 additions & 12 deletions examples/todo.rn
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fun parseDate(s, now) {
} catch e is ValueError {
throw UsageError { message: "bad date '{s}'" };
}

return s;
}

Expand Down Expand Up @@ -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);
Expand Down
94 changes: 86 additions & 8 deletions pkg/format/format.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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{{}}}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/rune/parse_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 1 addition & 9 deletions pkg/rune/parse_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions pkg/rune/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Loading
Loading