cli: disambiguate block statement boundaries
This commit is contained in:
1 parent
462ddd2c5c
commit
911982dd1c
33 files changed
+465
-7
No files matched your search
@@ -145,6 +145,8 @@ pub const Select = struct {
|
||||
|
||||
pub const Stmt = struct {
|
||||
span: Span,
|
||||
/// Bare if/match/block without `;`: a value must not be silently discarded.
|
||||
block_statement: bool = false,
|
||||
kind: union(enum) {
|
||||
let: struct { names: []const []const u8, ty: ?Type, e: Expr, tuple: bool = false },
|
||||
/// `init == null` is `= zero`.
|
||||
|
||||
+18
-3
@@ -11,7 +11,7 @@ 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 Mark = struct { angle: bool = false, prefix: bool = false, curly: Curly = .block, top: bool = false, module_end: bool = false, statement_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 {
|
||||
@@ -107,7 +107,11 @@ const Formatter = struct {
|
||||
// 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 });
|
||||
for (b.stmts) |*s| {
|
||||
const end = f.index(s.span.end);
|
||||
if (end != 0 and f.tokens[end - 1].tag == .r_brace) f.marks[end - 1].statement_end = true;
|
||||
try queue.append(f.arena, .{ .stmt = s });
|
||||
}
|
||||
if (b.tail) |e| try queue.append(f.arena, .{ .expr = e });
|
||||
},
|
||||
.stmt => |s| switch (s.kind) {
|
||||
@@ -346,7 +350,7 @@ const Formatter = struct {
|
||||
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),
|
||||
.r_brace => f.pending_line = f.marks[i].statement_end or (closing.?.curly != .literal and afterBlock(next)),
|
||||
else => {},
|
||||
}
|
||||
if (f.marks[i].module_end) f.pending_line = true;
|
||||
@@ -407,6 +411,17 @@ test "libraries and syntax failures are independent of semantics" {
|
||||
try std.testing.expect((try format(a.allocator(), "module bad fn main()->u64 effects [] {return 1}")) == .err);
|
||||
}
|
||||
|
||||
test "AST statement boundaries separate unary starts without splitting expression contexts" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "module m fn f()->u64 effects [] {if true {} else {} *p=2u64; match 0u64 {0=>{} _=>{}} -3i64; {} (1u64); {} [1u64;2]; let x={2u64}*3u64; ({2u64})*3u64}";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
for ([_][]const u8{ "else {}\n *p =", "}\n -3i64;", "{}\n (1u64);", "{}\n [1u64; 2];", "} * 3u64;", "}) * 3u64" }) |part| {
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, part) != null);
|
||||
}
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "angles, shifts, literals, grouping and standalone comments" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
|
||||
@@ -1184,7 +1184,7 @@ const Lowerer = struct {
|
||||
},
|
||||
.expr => |e| {
|
||||
const v = try l.expr(e, null);
|
||||
_ = v;
|
||||
if (s.block_statement and v.ty != .void and v.ty != .never) return l.fail("E7006", s.span, "value-producing block statement: use `;` to discard the value or parentheses to continue the expression", &.{});
|
||||
},
|
||||
.@"while" => |x| try l.whileLoop(s.span, x.cond, x.body, null),
|
||||
.loop => |b| try l.whileLoop(s.span, null, b, null),
|
||||
|
||||
@@ -473,6 +473,20 @@ const Parser = struct {
|
||||
}
|
||||
if (isWord(t, "select")) return .{ .span = p.span(start), .kind = .{ .select = try p.selectStmt() } };
|
||||
|
||||
// A leading bare block construct has priority over an outer expression
|
||||
// continuation in statement/tail position (spec 15 §4). Recursive
|
||||
// expression contexts still use expr(), including parentheses.
|
||||
if (isWord(t, "if") or isWord(t, "match") or t.tag == .l_brace) {
|
||||
const e = try p.primary();
|
||||
if (p.peek().tag == .r_brace) {
|
||||
tail.* = try p.box(ast.Expr, e);
|
||||
return null;
|
||||
}
|
||||
const discard = p.peek().tag == .semicolon;
|
||||
if (discard) _ = p.next();
|
||||
return .{ .span = p.span(start), .block_statement = !discard, .kind = .{ .expr = e } };
|
||||
}
|
||||
|
||||
// Expression statement, assignment or tail expression.
|
||||
const e = try p.expr();
|
||||
if (p.peek().tag == .eq) {
|
||||
@@ -1003,3 +1017,39 @@ test "parse a function" {
|
||||
try std.testing.expectEqualStrings("`;`", bad.err.expected);
|
||||
try std.testing.expectEqual(@as(u32, 2), bad.err.pos.line);
|
||||
}
|
||||
|
||||
test "bare block constructs end before unary-leading statements regardless of gaps" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
for ([_][]const u8{ "{}", "if true {} else if false {} else {}", "match 0u64 { 0 => {} _ => {} }" }) |construct| {
|
||||
for ([_][]const u8{ " ", "\n", " // boundary\n" }) |gap| {
|
||||
const source = try std.fmt.allocPrint(a, "module m fn f()->u64 effects [] {{{s}{s}*p=3u64; 42u64}}", .{ construct, gap });
|
||||
const r = try parse(a, source);
|
||||
try std.testing.expect(r == .ok);
|
||||
const b = r.ok.functions[0].body;
|
||||
try std.testing.expectEqual(@as(usize, 2), b.stmts.len);
|
||||
try std.testing.expect(b.stmts[0].block_statement);
|
||||
try std.testing.expect(b.stmts[1].kind == .assign);
|
||||
try std.testing.expect(b.stmts[1].kind.assign.place.kind == .deref);
|
||||
try std.testing.expect(b.tail.?.kind == .int);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "tail, explicit discard and parentheses choose expression context syntactically" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const r = try parse(a, "module m fn f()->u64 effects [] {{2u64}; ({2u64})*3u64; {4u64}}");
|
||||
try std.testing.expect(r == .ok);
|
||||
const b = r.ok.functions[0].body;
|
||||
try std.testing.expectEqual(@as(usize, 2), b.stmts.len);
|
||||
try std.testing.expect(!b.stmts[0].block_statement);
|
||||
try std.testing.expect(b.stmts[1].kind.expr.kind == .binary);
|
||||
try std.testing.expect(b.tail.?.kind == .block);
|
||||
for ([_][]const u8{ "+3u64", "as u64", ".x" }) |suffix| {
|
||||
const source = try std.fmt.allocPrint(a, "module m fn f()->u64 effects [] {{{{2u64}} {s}}}", .{suffix});
|
||||
try std.testing.expect((try parse(a, source)) == .err);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user