frontend: align source grammar and reject empty enum payload lists

This commit is contained in:
dev committed 2026-10-08 14:14:45 +03:00
1 parent 48b9b42fed
commit 4c9c5626c9
44 files changed
+501 -33

No files matched your search

+77
View File
@@ -623,6 +623,8 @@ const Parser = struct {
} else if (!std.mem.eql(u8, name, t.text)) return p.fail(p.peek(), "`::` after enum path");
var binds: std.ArrayList([]const u8) = .empty;
if (p.peek().tag == .l_paren) {
if (qualifier != null and p.peekAt(1).tag == .r_paren)
return p.fail(p.peek(), "payload bindings or no parentheses for a payload-free variant");
_ = p.next();
while (p.peek().tag != .r_paren) {
const b = p.peek();
@@ -797,6 +799,8 @@ const Parser = struct {
e.kind.struct_lit.type_args = type_args;
},
.l_paren => {
if (e.kind == .variant and p.peekAt(1).tag == .r_paren)
return p.fail(p.peek(), "payload values or no parentheses for a payload-free variant");
const args = try p.callArgs();
const callee = try p.box(ast.Expr, e);
e = .{ .span = p.span(start), .kind = .{ .call = .{ .callee = callee, .type_arg = null, .args = args } } };
@@ -1240,3 +1244,76 @@ test "dereferenced borrow consumes unary place without swallowing outer operatio
try std.testing.expect((try parse(a, source)) == .err);
}
}
test "qualified enum payload lists are nonempty, including grouped and generic variants" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
for ([_][]const u8{
"E::Empty()", "E::Pair()", "M.E::<u64>::Empty()", "(E::Empty)()",
"alloc_error::budget_exceeded()", "E::Empty( // payload\n )",
}) |expression| {
const source = try std.fmt.allocPrint(a, "module grammar fn f() -> void effects [] {{ let x = {s}; }}", .{expression});
const r = try parse(a, source);
try std.testing.expect(r == .err);
const at = std.mem.lastIndexOfScalar(u8, source, '(').?;
try std.testing.expectEqual(@as(u32, @intCast(at + 1)), r.err.pos.col);
try std.testing.expectEqualStrings("(", r.err.found);
}
for ([_][]const u8{ "E::Empty()", "E::Pair()", "M.E::<u64>::Empty()", "alloc_error::budget_exceeded()" }) |pattern_text| {
const source = try std.fmt.allocPrint(a, "module grammar fn f() -> void effects [] {{ match v {{ {s} => {{}} }} }}", .{pattern_text});
const r = try parse(a, source);
try std.testing.expect(r == .err);
const at = std.mem.lastIndexOfScalar(u8, source, '(').?;
try std.testing.expectEqual(@as(u32, @intCast(at + 1)), r.err.pos.col);
try std.testing.expectEqualStrings("(", r.err.found);
}
for ([_][]const u8{
"let x = f();", "let x = E::Empty;", "let x = E::Pair(1u64,);",
"let x = (E::Pair)(1u64);", "match v { E::Pair(_, n,) => {} }", "match operation() { ok() => {}, closed() => {} cancelled() => {} }",
"select { cancelled() => {} }",
}) |statement_text| {
const source = try std.fmt.allocPrint(a, "module grammar fn f() -> void effects [] {{ {s} }}", .{statement_text});
try std.testing.expect((try parse(a, source)) == .ok);
}
}
test "grammar surface preserves typed-call, comparison and body-brace choices" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const r = try parse(a,
\\module grammar
\\struct Node { value: u64, }
\\fn wrappers(a: island<Node>, b: frozen<Node>, c: arc<Node>, d: iref<Node>, e: atomic<u64>,) -> void effects [] {}
\\fn f() -> void effects [] {
\\ let compare = left < right;
\\ let ordinary = identity::<u64>(42u64,);
\\ match channel.make<u64>(1u64, heap,) { ok(a, b,) => {}, err(e,) => {} }
\\ let buffer_value = buffer.new<u64>(heap,);
\\ if test(Node {value: 42u64}) {}
\\ while (Node {value: 42u64}).value == 42u64 {break;}
\\ let (a, b,) = split();
\\ ref item = first::<u64>(slice_value,);
\\ select { recv(rx) { ok(n,) => n, closed() => 0u64 } cancelled() => 0u64 }
\\}
);
try std.testing.expect(r == .ok);
const statements = r.ok.functions[1].body.stmts;
try std.testing.expect(statements[0].kind.let.e.kind == .binary);
try std.testing.expect(statements[1].kind.let.e.kind.call.callee.kind == .specialize);
try std.testing.expect(statements[2].kind.expr.kind.match_expr.scrutinee.kind.call.type_arg != null);
try std.testing.expect(statements[3].kind.let.e.kind.call.type_arg != null);
try std.testing.expect(statements[4].kind.expr.kind.if_expr.cond.kind.call.args[0].kind == .struct_lit);
try std.testing.expect(statements[6].kind.let.names.len == 2);
try std.testing.expect(statements[7].kind.borrow.place.kind.call.callee.kind == .specialize);
for ([_][]const u8{
"let x = channel.make < value;", // Typed built-in commits, no fallback.
"let x = buffer.new < value;",
"if Node {value: 42u64} {}", // Header brace belongs to the body.
"select { cancelled(_) => {} }", // Cancellation names are not discard bindings.
}) |statement_text| {
const source = try std.fmt.allocPrint(a, "module grammar fn f() -> void effects [] {{ {s} }}", .{statement_text});
try std.testing.expect((try parse(a, source)) == .err);
}
}