cli: add deterministic source formatter
This commit is contained in:
1 parent
d901d5227b
commit
462ddd2c5c
14 files changed
+787
-13
No files matched your search
@@ -0,0 +1,470 @@
|
||||
//! Source formatter (spec 15 §11). Only token gaps are rewritten.
|
||||
//! AST annotations choose layout; unchanged tokens preserve syntax and meaning.
|
||||
const std = @import("std");
|
||||
const lex = @import("lex.zig");
|
||||
const ast = @import("ast.zig");
|
||||
const parse = @import("parse.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Tag = lex.Tag;
|
||||
const Token = lex.Token;
|
||||
const Error = error{ OutOfMemory, Invariant };
|
||||
pub const Result = union(enum) { ok: []const u8, err: parse.Failure };
|
||||
|
||||
const Curly = enum { block, list, literal };
|
||||
const Mark = struct { angle: bool = false, prefix: bool = false, curly: Curly = .block, top: bool = false, module_end: bool = false, mate: usize = 0, multiline: bool = false, layout_scope: usize = 0 };
|
||||
const Node = union(enum) { block: *const ast.Block, stmt: *const ast.Stmt, expr: *const ast.Expr };
|
||||
|
||||
pub fn format(arena: Allocator, source: []const u8) Error!Result {
|
||||
const module = switch (try parse.parse(arena, source)) {
|
||||
.err => |f| return .{ .err = f },
|
||||
.ok => |m| m,
|
||||
};
|
||||
const tokens = try lex.tokenize(arena, source);
|
||||
var f: Formatter = .{ .arena = arena, .source = source, .tokens = tokens, .marks = try arena.alloc(Mark, tokens.len) };
|
||||
@memset(f.marks, .{});
|
||||
try f.annotate(module);
|
||||
try f.continuations();
|
||||
const output = try f.render();
|
||||
// Check the boundary invariant before exposing output or allowing a write.
|
||||
const after = try lex.tokenize(arena, output);
|
||||
if (tokens.len != after.len) return error.Invariant;
|
||||
for (tokens, after) |a, b| if (a.tag != b.tag or !std.mem.eql(u8, a.text, b.text)) return error.Invariant;
|
||||
return .{ .ok = output };
|
||||
}
|
||||
|
||||
const Frame = struct { tag: Tag, curly: Curly = .block, expanded: bool = false, indented: bool = false };
|
||||
const Formatter = struct {
|
||||
arena: Allocator,
|
||||
source: []const u8,
|
||||
tokens: []const Token,
|
||||
marks: []Mark,
|
||||
output: std.ArrayList(u8) = .empty,
|
||||
frames: std.ArrayList(Frame) = .empty,
|
||||
indent: usize = 0,
|
||||
pending_line: bool = false,
|
||||
|
||||
fn offset(f: *Formatter, t: Token) usize {
|
||||
return @intFromPtr(t.text.ptr) - @intFromPtr(f.source.ptr);
|
||||
}
|
||||
fn index(f: *Formatter, pos: lex.Pos) usize {
|
||||
var lo: usize = 0;
|
||||
var hi = f.tokens.len;
|
||||
while (lo < hi) {
|
||||
const mid = lo + (hi - lo) / 2;
|
||||
const p = f.tokens[mid].pos;
|
||||
if (p.line < pos.line or (p.line == pos.line and p.col < pos.col)) lo = mid + 1 else hi = mid;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
fn angles(f: *Formatter, start: lex.Pos, end: lex.Pos) void {
|
||||
for (f.marks[f.index(start)..f.index(end)], f.tokens[f.index(start)..f.index(end)]) |*m, t| switch (t.tag) {
|
||||
.lt, .gt, .shr => m.angle = true,
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
fn curly(f: *Formatter, span: ast.Span, kind: Curly) void {
|
||||
const end = f.index(span.end);
|
||||
if (end != 0 and f.tokens[end - 1].tag == .r_brace) f.marks[f.marks[end - 1].mate].curly = kind;
|
||||
}
|
||||
fn pattern(f: *Formatter, p: ast.Pattern) void {
|
||||
f.angles(p.span.start, p.span.end);
|
||||
}
|
||||
fn arms(f: *Formatter, queue: *std.ArrayList(Node), values: []const ast.Arm) Error!void {
|
||||
for (values) |*arm| {
|
||||
f.pattern(arm.pattern);
|
||||
try queue.append(f.arena, .{ .expr = &arm.body });
|
||||
}
|
||||
}
|
||||
fn annotate(f: *Formatter, module: ast.Module) Error!void {
|
||||
var opens: std.ArrayList(usize) = .empty;
|
||||
for (f.tokens, 0..) |t, i| switch (t.tag) {
|
||||
.l_brace, .l_paren, .l_bracket => try opens.append(f.arena, i),
|
||||
.r_brace, .r_paren, .r_bracket => {
|
||||
const open = opens.pop().?;
|
||||
f.marks[i].mate = open;
|
||||
f.marks[open].mate = i;
|
||||
},
|
||||
else => {},
|
||||
};
|
||||
var queue: std.ArrayList(Node) = .empty;
|
||||
var first = f.tokens.len - 1;
|
||||
for (module.imports) |imp| first = @min(first, f.index(imp.span.start));
|
||||
for (module.types) |t| {
|
||||
const i = f.index(t.span.start);
|
||||
first = @min(first, i);
|
||||
f.marks[i].top = true;
|
||||
f.angles(t.span.start, t.span.end);
|
||||
f.curly(t.span, .list);
|
||||
}
|
||||
for (module.functions) |*func| {
|
||||
const i = f.index(func.span.start);
|
||||
first = @min(first, i);
|
||||
f.marks[i].top = true;
|
||||
f.angles(func.span.start, func.body.span.start);
|
||||
try queue.append(f.arena, .{ .block = &func.body });
|
||||
}
|
||||
f.marks[first - 1].module_end = true;
|
||||
// Iterative traversal also handles long left-associated expressions.
|
||||
while (queue.pop()) |node| switch (node) {
|
||||
.block => |b| {
|
||||
for (b.stmts) |*s| try queue.append(f.arena, .{ .stmt = s });
|
||||
if (b.tail) |e| try queue.append(f.arena, .{ .expr = e });
|
||||
},
|
||||
.stmt => |s| switch (s.kind) {
|
||||
.let => |*x| {
|
||||
if (x.ty) |t| f.angles(t.span.start, t.span.end);
|
||||
try queue.append(f.arena, .{ .expr = &x.e });
|
||||
},
|
||||
.@"var" => |*x| {
|
||||
f.angles(x.ty.span.start, x.ty.span.end);
|
||||
if (x.init) |*e| try queue.append(f.arena, .{ .expr = e });
|
||||
},
|
||||
.assign => |*x| {
|
||||
try queue.append(f.arena, .{ .expr = &x.place });
|
||||
try queue.append(f.arena, .{ .expr = &x.e });
|
||||
},
|
||||
.borrow => |*x| try queue.append(f.arena, .{ .expr = &x.place }),
|
||||
.@"return" => |*e| if (e.*) |*v| {
|
||||
try queue.append(f.arena, .{ .expr = v });
|
||||
},
|
||||
.expr => |*e| try queue.append(f.arena, .{ .expr = e }),
|
||||
.@"while" => |*x| {
|
||||
try queue.append(f.arena, .{ .expr = &x.cond });
|
||||
try queue.append(f.arena, .{ .block = &x.body });
|
||||
},
|
||||
.@"for" => |*x| {
|
||||
try queue.append(f.arena, .{ .expr = &x.lo });
|
||||
try queue.append(f.arena, .{ .expr = &x.hi });
|
||||
try queue.append(f.arena, .{ .block = &x.body });
|
||||
},
|
||||
.loop, .shield => |*b| try queue.append(f.arena, .{ .block = b }),
|
||||
.scope => |*x| try queue.append(f.arena, .{ .block = &x.body }),
|
||||
.lock => |*x| {
|
||||
try queue.append(f.arena, .{ .expr = &x.place });
|
||||
try queue.append(f.arena, .{ .block = &x.body });
|
||||
try queue.append(f.arena, .{ .block = &x.cancelled });
|
||||
},
|
||||
.select => |*x| {
|
||||
f.curly(x.span, .list);
|
||||
for (x.arms) |*a| {
|
||||
f.curly(a.span, .list);
|
||||
if (a.value) |*e| try queue.append(f.arena, .{ .expr = e });
|
||||
try f.arms(&queue, a.arms);
|
||||
}
|
||||
try queue.append(f.arena, .{ .expr = &x.cancelled });
|
||||
},
|
||||
.@"break", .@"continue", .trap => {},
|
||||
},
|
||||
.expr => |e| switch (e.kind) {
|
||||
.neg, .not, .deref => |x| {
|
||||
f.marks[f.index(e.span.start)].prefix = true;
|
||||
try queue.append(f.arena, .{ .expr = x });
|
||||
},
|
||||
.borrow => |x| try queue.append(f.arena, .{ .expr = x.place }),
|
||||
.binary => |x| {
|
||||
try queue.append(f.arena, .{ .expr = x.l });
|
||||
try queue.append(f.arena, .{ .expr = x.r });
|
||||
},
|
||||
.cast => |x| {
|
||||
f.angles(x.ty.span.start, x.ty.span.end);
|
||||
try queue.append(f.arena, .{ .expr = x.e });
|
||||
},
|
||||
.specialize => |x| {
|
||||
f.angles(x.base.span.end, e.span.end);
|
||||
try queue.append(f.arena, .{ .expr = x.base });
|
||||
},
|
||||
.call => |*x| {
|
||||
if (x.type_arg) |t| {
|
||||
const end = if (x.args.len != 0) x.args[0].span.start else e.span.end;
|
||||
f.angles(x.callee.span.end, end);
|
||||
f.angles(t.span.start, t.span.end);
|
||||
}
|
||||
try queue.append(f.arena, .{ .expr = x.callee });
|
||||
for (x.args) |*a| try queue.append(f.arena, .{ .expr = a });
|
||||
},
|
||||
.field => |x| try queue.append(f.arena, .{ .expr = x.e }),
|
||||
.index => |x| {
|
||||
try queue.append(f.arena, .{ .expr = x.e });
|
||||
try queue.append(f.arena, .{ .expr = x.lo });
|
||||
if (x.hi) |h| try queue.append(f.arena, .{ .expr = h });
|
||||
},
|
||||
.struct_lit => |x| {
|
||||
f.curly(e.span, .literal);
|
||||
const open = f.marks[f.index(e.span.end) - 1].mate;
|
||||
f.angles(e.span.start, f.tokens[open].pos);
|
||||
for (x.fields) |*field| try queue.append(f.arena, .{ .expr = &field.e });
|
||||
},
|
||||
.array_rep => |x| try queue.append(f.arena, .{ .expr = x.e }),
|
||||
.array_list => |xs| for (xs) |*x| {
|
||||
try queue.append(f.arena, .{ .expr = x });
|
||||
},
|
||||
.or_trap => |x| try queue.append(f.arena, .{ .expr = x }),
|
||||
.if_expr => |x| {
|
||||
try queue.append(f.arena, .{ .expr = &x.cond });
|
||||
try queue.append(f.arena, .{ .block = &x.then });
|
||||
var els = x.@"else";
|
||||
while (els) |branch| switch (branch) {
|
||||
.block => |*b| { // Payload copies cannot be queued by address.
|
||||
for (b.stmts) |*s| try queue.append(f.arena, .{ .stmt = s });
|
||||
if (b.tail) |v| try queue.append(f.arena, .{ .expr = v });
|
||||
els = null;
|
||||
},
|
||||
.@"if" => |next| {
|
||||
try queue.append(f.arena, .{ .expr = &next.cond });
|
||||
try queue.append(f.arena, .{ .block = &next.then });
|
||||
els = next.@"else";
|
||||
},
|
||||
};
|
||||
},
|
||||
.match_expr => |x| {
|
||||
f.curly(e.span, .list);
|
||||
try queue.append(f.arena, .{ .expr = &x.scrutinee });
|
||||
try f.arms(&queue, x.arms);
|
||||
},
|
||||
.block => |b| try queue.append(f.arena, .{ .block = b }),
|
||||
.variant => f.angles(e.span.start, e.span.end),
|
||||
.int, .boolean, .bytes, .name, .move => {},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn continuations(f: *Formatter) Error!void {
|
||||
const counts = try f.arena.alloc(usize, f.tokens.len + 1);
|
||||
@memset(counts, 0);
|
||||
const starts = try f.arena.alloc(usize, f.tokens.len);
|
||||
var scopes: std.ArrayList(usize) = .empty;
|
||||
try scopes.append(f.arena, 0);
|
||||
var end: usize = 0;
|
||||
for (f.tokens, 0..) |t, i| {
|
||||
const start = if (t.tag == .eof) f.source.len else f.offset(t);
|
||||
const scope = scopes.items[scopes.items.len - 1];
|
||||
if (std.mem.indexOf(u8, f.source[end..start], "//") != null) counts[scope] += 1;
|
||||
switch (t.tag) {
|
||||
.l_brace => if (f.marks[i].curly != .literal) {
|
||||
// A block's internal comments belong to its own layout,
|
||||
// not to a surrounding argument or compact literal.
|
||||
try scopes.append(f.arena, i + 1);
|
||||
} else {
|
||||
f.marks[i].layout_scope = scope;
|
||||
starts[i] = counts[scope];
|
||||
},
|
||||
.l_paren, .l_bracket => {
|
||||
f.marks[i].layout_scope = scope;
|
||||
starts[i] = counts[scope];
|
||||
},
|
||||
.r_brace, .r_paren, .r_bracket => {
|
||||
const open = f.marks[i].mate;
|
||||
if (t.tag == .r_brace and f.marks[open].curly != .literal) {
|
||||
_ = scopes.pop();
|
||||
} else f.marks[open].multiline = counts[f.marks[open].layout_scope] > starts[open];
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
end = start + t.text.len;
|
||||
}
|
||||
}
|
||||
fn newline(f: *Formatter) Error!void {
|
||||
while (f.output.items.len != 0 and f.output.items[f.output.items.len - 1] == ' ') _ = f.output.pop();
|
||||
if (f.output.items.len != 0 and f.output.items[f.output.items.len - 1] != '\n') try f.output.append(f.arena, '\n');
|
||||
f.pending_line = false;
|
||||
}
|
||||
fn blank(f: *Formatter) Error!void {
|
||||
try f.newline();
|
||||
if (f.output.items.len != 0 and (f.output.items.len < 2 or f.output.items[f.output.items.len - 2] != '\n')) try f.output.append(f.arena, '\n');
|
||||
}
|
||||
fn padding(f: *Formatter) Error!void {
|
||||
if (f.output.items.len == 0 or f.output.items[f.output.items.len - 1] == '\n') try f.output.appendNTimes(f.arena, ' ', f.indent * 4);
|
||||
}
|
||||
fn space(f: *Formatter) Error!void {
|
||||
if (f.output.items.len != 0 and f.output.items[f.output.items.len - 1] != ' ' and f.output.items[f.output.items.len - 1] != '\n') try f.output.append(f.arena, ' ');
|
||||
}
|
||||
fn gap(f: *Formatter, bytes: []const u8, top: bool, has_prev: bool, allow_blank: bool) Error!void {
|
||||
var make_blank = top;
|
||||
var blank_pending = false;
|
||||
var lines = std.mem.splitScalar(u8, bytes, '\n');
|
||||
var first_line = true;
|
||||
while (lines.next()) |line| {
|
||||
const body = std.mem.trimEnd(u8, line, "\r");
|
||||
const text = std.mem.trimStart(u8, body, " \t");
|
||||
if (std.mem.startsWith(u8, text, "//")) {
|
||||
if (first_line and has_prev) {
|
||||
try f.space();
|
||||
} else {
|
||||
if (make_blank or blank_pending) {
|
||||
try f.blank();
|
||||
make_blank = false;
|
||||
} else try f.newline();
|
||||
try f.padding();
|
||||
}
|
||||
try f.output.appendSlice(f.arena, text);
|
||||
// Comment contents, including trailing spaces/tabs, are data.
|
||||
try f.output.append(f.arena, '\n');
|
||||
f.pending_line = false;
|
||||
blank_pending = false;
|
||||
} else if (allow_blank and !first_line and text.len == 0 and lines.index != null) {
|
||||
blank_pending = true;
|
||||
}
|
||||
first_line = false;
|
||||
}
|
||||
if (make_blank or blank_pending) try f.blank() else if (f.pending_line) try f.newline();
|
||||
}
|
||||
fn render(f: *Formatter) Error![]const u8 {
|
||||
var end: usize = 0;
|
||||
for (f.tokens, 0..) |t, i| {
|
||||
const start = if (t.tag == .eof) f.source.len else f.offset(t);
|
||||
const bytes = f.source[end..start];
|
||||
const allow_blank = i != 0 and t.tag != .r_brace and f.tokens[i - 1].tag != .l_brace and
|
||||
(f.pending_line or std.mem.indexOf(u8, bytes, "//") != null);
|
||||
try f.gap(bytes, f.marks[i].top, i != 0, allow_blank);
|
||||
if (t.tag == .eof) break;
|
||||
end = start + t.text.len;
|
||||
var closing: ?Frame = null;
|
||||
if (t.tag == .r_brace or t.tag == .r_paren or t.tag == .r_bracket) {
|
||||
closing = f.frames.pop().?;
|
||||
if (closing.?.indented) f.indent -= 1;
|
||||
if (closing.?.expanded) try f.newline();
|
||||
}
|
||||
try f.padding();
|
||||
if (i != 0 and (needsSpace(f.tokens[i - 1], t, f.marks[i - 1], f.marks[i]) or merges(f.tokens[i - 1].text, t.text))) try f.space();
|
||||
try f.output.appendSlice(f.arena, t.text);
|
||||
const next = f.tokens[i + 1];
|
||||
switch (t.tag) {
|
||||
.l_brace => {
|
||||
const between = f.source[end..(if (next.tag == .eof) f.source.len else f.offset(next))];
|
||||
const expanded = f.marks[i].curly != .literal and (next.tag != .r_brace or std.mem.trim(u8, between, " \t\r\n").len != 0);
|
||||
const indented = expanded or f.marks[i].multiline;
|
||||
try f.frames.append(f.arena, .{ .tag = t.tag, .curly = f.marks[i].curly, .expanded = expanded, .indented = indented });
|
||||
if (indented) f.indent += 1;
|
||||
if (expanded) f.pending_line = true;
|
||||
},
|
||||
.l_paren, .l_bracket => {
|
||||
try f.frames.append(f.arena, .{ .tag = t.tag, .indented = f.marks[i].multiline });
|
||||
if (f.marks[i].multiline) f.indent += 1;
|
||||
},
|
||||
.semicolon => f.pending_line = f.frames.items.len == 0 or f.frames.items[f.frames.items.len - 1].tag == .l_brace,
|
||||
.comma => if (f.frames.items.len != 0) {
|
||||
const frame = f.frames.items[f.frames.items.len - 1];
|
||||
f.pending_line = frame.tag == .l_brace and frame.curly == .list;
|
||||
},
|
||||
.r_brace => f.pending_line = closing.?.curly != .literal and afterBlock(next),
|
||||
else => {},
|
||||
}
|
||||
if (f.marks[i].module_end) f.pending_line = true;
|
||||
}
|
||||
try f.newline();
|
||||
while (f.output.items.len >= 2 and f.output.items[f.output.items.len - 2] == '\n') _ = f.output.pop();
|
||||
return f.output.items;
|
||||
}
|
||||
};
|
||||
|
||||
fn word(t: Token, name: []const u8) bool {
|
||||
return t.tag == .ident and std.mem.eql(u8, t.text, name);
|
||||
}
|
||||
fn afterBlock(next: Token) bool {
|
||||
if (word(next, "else") or word(next, "cancelled")) return false;
|
||||
return switch (next.tag) {
|
||||
.ident, .int, .bytes, .underscore, .l_brace, .eof => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
fn needsSpace(a: Token, b: Token, am: Mark, bm: Mark) bool {
|
||||
if (b.tag == .comma or b.tag == .semicolon or b.tag == .colon or b.tag == .dot or b.tag == .colon_colon or b.tag == .r_paren or b.tag == .r_bracket) return false;
|
||||
if (a.tag == .dot or a.tag == .colon_colon or a.tag == .l_paren or a.tag == .l_bracket) return false;
|
||||
if (bm.angle) return false;
|
||||
if (am.angle and a.tag == .lt) return false;
|
||||
if (am.prefix) return word(a, "not");
|
||||
if (b.tag == .l_paren) {
|
||||
if (a.tag == .ident) return word(a, "if") or word(a, "while") or word(a, "return") or word(a, "match") or word(a, "not") or word(a, "and") or word(a, "or");
|
||||
if (am.angle and (a.tag == .gt or a.tag == .shr)) return false;
|
||||
return a.tag != .r_paren and a.tag != .r_bracket and a.tag != .int and a.tag != .bytes;
|
||||
}
|
||||
if (b.tag == .l_bracket and (a.tag == .ident or a.tag == .r_bracket or a.tag == .r_paren)) return word(a, "effects") or word(a, "return") or word(a, "in") or word(a, "and") or word(a, "or");
|
||||
if (a.tag == .l_brace and b.tag == .r_brace) return false;
|
||||
if (a.tag == .dot_dot or b.tag == .dot_dot) return false;
|
||||
return true;
|
||||
}
|
||||
fn merges(a: []const u8, b: []const u8) bool {
|
||||
const x = a[a.len - 1];
|
||||
const y = b[0];
|
||||
if ((std.ascii.isAlphanumeric(x) or x == '_') and (std.ascii.isAlphanumeric(y) or y == '_')) return true;
|
||||
const pair = [_]u8{ x, y };
|
||||
for ([_][]const u8{ "::", "..", "->", "=>", "==", "!=", "<=", ">=", "<<", ">>", "+%", "-%", "*%", "//" }) |p| if (std.mem.eql(u8, &pair, p)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
test "source layout, comments, token boundaries and idempotence" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "// header\r\nmodule demo\r\nfn main()->u64 effects[]{let x=1u64; // trailing\nreturn x+2u64;} // final\n";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
try std.testing.expectEqualStrings("// header\nmodule demo\n\nfn main() -> u64 effects [] {\n let x = 1u64; // trailing\n return x + 2u64;\n} // final\n", output);
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
test "libraries and syntax failures are independent of semantics" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
try std.testing.expect((try format(a.allocator(), "module lib import absent; fn f(x: Unknown)->Unknown effects [] {x}")) == .ok);
|
||||
try std.testing.expect((try format(a.allocator(), "module bad fn main()->u64 effects [] {return 1}")) == .err);
|
||||
}
|
||||
|
||||
test "angles, shifts, literals, grouping and standalone comments" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "module m fn f<T>(p:own<own<own<T> > >)->u64 effects [] {let b=b\"//\\x41\\n\"; let n=\n// standalone\n(1u64+2u64)*3u64; if n<12u64 { return -(-1i64) as! u64; } else { return n>>1u64; }}";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "own<own<own<T> > >") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "b\"//\\x41\\n\"") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "let n =\n // standalone\n (1u64 + 2u64) * 3u64;") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "n >> 1u64") != null);
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "long left-associated expressions use an iterative layout traversal" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
var source: std.ArrayList(u8) = .empty;
|
||||
try source.appendSlice(a.allocator(), "module m fn f()->u64 effects [] { return 0u64");
|
||||
for (0..10000) |_| try source.appendSlice(a.allocator(), "+1u64");
|
||||
try source.appendSlice(a.allocator(), ";}");
|
||||
const output = (try format(a.allocator(), source.items)).ok;
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "continuation indentation, comment contents and statement grouping" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "module m struct P {a:u64,b:u64} fn f()->void effects [] {let p=P{a:1u64, // lit \t\nb:2u64};\n\n\nlet v=g( // arg\n1u64,2u64);let n=not (true and false);}";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "// lit \t\n b: 2u64 };") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "};\n\n let v = g( // arg\n 1u64, 2u64);") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "not (true and false)") != null);
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "terminal CR normalization is idempotent" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const output = (try format(a.allocator(), "// heading\r\r\nmodule m // ending\r\r")).ok;
|
||||
try std.testing.expectEqualStrings("// heading\nmodule m // ending\n", output);
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "blank statement groups beside comments and block arguments" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "module m fn f()->void effects [] {let x=1u64;\n\n// section two\nlet y=2u64;\n// trailing note\n\nlet w=g(match x {0=>1u64,_=>2u64,});}";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "let x = 1u64;\n\n // section two\n let y = 2u64;") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "// trailing note\n\n let w = g(match x {\n 0 => 1u64,") != null);
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "comments inside a block argument do not indent the enclosing call" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "module m fn f()->void effects [] {let w=g(match 1u64 { // block\n0=>1u64,_=>2u64,});}";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "g(match 1u64 { // block\n 0 => 1u64,") != null);
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, "\n });") != null);
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
Reference in new issue
Block a user