cli: parse nested source types with explicit continuations

This commit is contained in:
dev committed 2026-10-10 12:41:41 +03:00
1 parent 35635603f9
commit cef944d30e
4 files changed
+244 -38

No files matched your search

+226 -38
View File
@@ -415,36 +415,105 @@ const Parser = struct {
// ------------------------------------------------------------ types
fn typeExpr(p: *Parser) Error!ast.Type {
const start = p.peek().pos;
if (p.peek().tag == .l_bracket) {
_ = p.next();
const elem = try p.typeExpr();
_ = try p.expect(.semicolon, "`;` in `[T; N]`");
const n = try p.expect(.int, "array length");
_ = try p.expect(.r_bracket, "`]`");
const len = parseIntText(n.text) orelse return p.fail(n, "an array length");
return .{ .span = p.span(start), .kind = .{ .array = .{ .elem = try p.box(ast.Type, elem), .len = @intCast(len) } } };
return p.typeTree(false);
}
// Suspend array suffixes and argument lists explicitly. A static function
// reference is an argument only, never an ordinary type annotation.
// Argument buffers belong to the returned AST; only frames are temporary.
const TypeFrame = union(enum) {
array: lex.Pos,
arguments: struct {
start: lex.Pos,
token: Token,
name: []const u8,
function: bool,
values: std.ArrayList(ast.Type) = .empty,
},
};
fn typeTree(p: *Parser, initial_argument: bool) Error!ast.Type {
var frames: std.ArrayList(TypeFrame) = .empty;
defer frames.deinit(p.arena);
var argument = initial_argument;
while (true) {
const first = p.peek();
const start = first.pos;
var ty: ast.Type = undefined;
if (argument and p.atWord("fn")) {
_ = p.next();
const name = try p.path();
if (p.peek().tag == .colon_colon) {
_ = p.next();
_ = try p.expect(.lt, "`<`");
try frames.append(p.arena, .{ .arguments = .{ .start = start, .token = first, .name = name, .function = true } });
argument = true;
continue;
}
ty = .{ .span = p.span(start), .kind = .{ .function_ref = .{ .name = name } } };
} else if (p.peek().tag == .l_bracket) {
_ = p.next();
try frames.append(p.arena, .{ .array = start });
argument = false;
continue;
} else {
const t = p.peek();
if (t.tag != .ident) return p.fail(t, "a type");
_ = p.next();
const name = if (p.peek().tag == .dot) try p.pathTail(t.text) else t.text;
if (!p.speculative and std.mem.eql(u8, name, t.text) and (isWord(t, "mut") or isWord(t, "ref")) and p.atWord("slice"))
p.type_advice = .{ .at = p.pos, .kind = if (isWord(t, "mut")) .slice_mut_type else .slice_ref_type };
if (p.peek().tag != .lt) {
ty = .{ .span = p.span(start), .kind = .{ .name = name } };
} else if (std.mem.eql(u8, name, "cap") or std.mem.eql(u8, name, "file")) {
_ = p.next();
const value = try p.path();
try p.closeAngle();
ty = .{ .span = p.span(start), .kind = if (std.mem.eql(u8, name, "cap")) .{ .cap = value } else .{ .file = value } };
} else {
_ = p.next();
try frames.append(p.arena, .{ .arguments = .{ .start = start, .token = t, .name = name, .function = false } });
argument = true;
continue;
}
}
while (frames.items.len != 0) {
const top = &frames.items[frames.items.len - 1];
switch (top.*) {
.array => |outer| {
_ = try p.expect(.semicolon, "`;` in `[T; N]`");
const n = try p.expect(.int, "array length");
_ = try p.expect(.r_bracket, "`]`");
const len = parseIntText(n.text) orelse return p.fail(n, "an array length");
const elem = try p.box(ast.Type, ty);
ty = .{ .span = p.span(outer), .kind = .{ .array = .{ .elem = elem, .len = @intCast(len) } } };
},
.arguments => |*args| {
try args.values.append(p.arena, ty);
if (p.peek().tag == .comma) {
_ = p.next();
argument = true;
break;
}
try p.closeAngle();
ty = .{ .span = p.span(args.start), .kind = .{ .applied = .{ .name = args.name, .args = args.values.items } } };
if (args.function) {
ty.kind = .{ .function_ref = .{ .name = args.name, .args = args.values.items } };
} else {
const wrappers = [_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "iref", "atomic" };
for (wrappers) |wrapper| if (std.mem.eql(u8, args.name, wrapper)) {
// Preserve the name-token location even after a
// split >> closer; source spelling is unchanged.
if (args.values.items.len != 1) return p.fail(args.token, "one wrapper type argument");
ty.kind = .{ .wrapped = .{ .wrapper = args.name, .inner = try p.box(ast.Type, args.values.items[0]) } };
break;
};
}
},
}
_ = frames.pop();
} else return ty;
}
const t = p.peek();
if (t.tag != .ident) return p.fail(t, "a type");
_ = p.next();
const name = if (p.peek().tag == .dot) try p.pathTail(t.text) else t.text;
if (!p.speculative and std.mem.eql(u8, name, t.text) and (isWord(t, "mut") or isWord(t, "ref")) and p.atWord("slice"))
p.type_advice = .{ .at = p.pos, .kind = if (isWord(t, "mut")) .slice_mut_type else .slice_ref_type };
if (p.peek().tag != .lt) return .{ .span = p.span(start), .kind = .{ .name = name } };
if (std.mem.eql(u8, name, "cap") or std.mem.eql(u8, name, "file")) {
_ = p.next();
const value = try p.path();
try p.closeAngle();
return .{ .span = p.span(start), .kind = if (std.mem.eql(u8, name, "cap")) .{ .cap = value } else .{ .file = value } };
}
const args = try p.typeArgs();
const wrappers = [_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "iref", "atomic" };
for (wrappers) |wrapper| if (std.mem.eql(u8, name, wrapper)) {
if (args.len != 1) return p.fail(t, "one wrapper type argument");
return .{ .span = p.span(start), .kind = .{ .wrapped = .{ .wrapper = name, .inner = try p.box(ast.Type, args[0]) } } };
};
return .{ .span = p.span(start), .kind = .{ .applied = .{ .name = name, .args = args } } };
}
fn typeArgs(p: *Parser) Error![]const ast.Type {
@@ -460,15 +529,7 @@ const Parser = struct {
}
fn genericArg(p: *Parser) Error!ast.Type {
if (!p.atWord("fn")) return p.typeExpr();
const start = p.next().pos;
const name = try p.path();
var args: []const ast.Type = &.{};
if (p.peek().tag == .colon_colon) {
_ = p.next();
args = try p.typeArgs();
}
return .{ .span = p.span(start), .kind = .{ .function_ref = .{ .name = name, .args = args } } };
return p.typeTree(true);
}
fn functionContract(p: *Parser) Error!ast.FunctionContract {
@@ -1489,3 +1550,130 @@ test "grammar surface preserves typed-call, comparison and body-brace choices" {
try std.testing.expect((try parse(a, source)) == .err);
}
}
test "type parsing suspends deep array wrapper and sibling argument frames" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const depth = 32_768;
var source: std.ArrayList(u8) = .empty;
try source.appendSlice(a, "module deep\nfn f(x: ");
for (0..depth) |i| try source.appendSlice(a, switch (i % 3) {
0 => "[",
1 => "own<",
else => "Pair<",
});
try source.appendSlice(a, "u8");
var i: usize = depth;
while (i != 0) {
i -= 1;
try source.appendSlice(a, switch (i % 3) {
0 => "; 1]",
1 => ">",
else => ", bool>",
});
}
const end: u32 = @intCast(source.items.len - "module deep\n".len + 1);
try source.appendSlice(a, ") -> void effects [] {}\n");
const result = try parse(a, source.items);
try std.testing.expect(result == .ok);
var t = result.ok.functions[0].params[0].ty;
try std.testing.expectEqual(@as(u32, 9), t.span.start.col);
try std.testing.expectEqual(end, t.span.end.col);
for (0..depth) |level| {
try std.testing.expectEqual(@as(u32, 2), t.span.start.line);
switch (level % 3) {
0 => {
try std.testing.expect(t.kind == .array);
try std.testing.expectEqual(@as(u64, 1), t.kind.array.len);
t = t.kind.array.elem.*;
},
1 => {
try std.testing.expect(t.kind == .wrapped);
try std.testing.expectEqualStrings("own", t.kind.wrapped.wrapper);
t = t.kind.wrapped.inner.*;
},
else => {
try std.testing.expect(t.kind == .applied);
try std.testing.expectEqualStrings("Pair", t.kind.applied.name);
try std.testing.expectEqual(@as(usize, 2), t.kind.applied.args.len);
try std.testing.expect(t.kind.applied.args[1].kind == .name);
try std.testing.expectEqualStrings("bool", t.kind.applied.args[1].kind.name);
t = t.kind.applied.args[0];
},
}
}
try std.testing.expect(t.kind == .name);
try std.testing.expectEqualStrings("u8", t.kind.name);
}
test "nested static function arguments and split angle spans need no native recursion" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const depth = 16_384;
var source: std.ArrayList(u8) = .empty;
try source.appendSlice(a, "module deep\nfn f(x: ");
for (0..depth) |_| try source.appendSlice(a, "Box<fn pick::<");
try source.appendSlice(a, "u8");
for (0..depth) |_| try source.appendSlice(a, ">>");
try source.appendSlice(a, ") -> void effects [] {}\n");
const result = try parse(a, source.items);
try std.testing.expect(result == .ok);
var t = result.ok.functions[0].params[0].ty;
for (0..depth) |_| {
try std.testing.expect(t.kind == .applied);
try std.testing.expectEqualStrings("Box", t.kind.applied.name);
try std.testing.expectEqual(@as(usize, 1), t.kind.applied.args.len);
const f = t.kind.applied.args[0];
try std.testing.expect(f.kind == .function_ref);
try std.testing.expectEqualStrings("pick", f.kind.function_ref.name);
try std.testing.expectEqual(@as(usize, 1), f.kind.function_ref.args.len);
try std.testing.expectEqual(t.span.end.col - 1, f.span.end.col);
t = f.kind.function_ref.args[0];
}
try std.testing.expect(t.kind == .name);
try std.testing.expectEqualStrings("u8", t.kind.name);
}
test "deep type failures retain leaf and wrapper name locations" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
var source: std.ArrayList(u8) = .empty;
try source.appendSlice(a, "module deep\nfn f(x: ");
for (0..8_192) |_| try source.appendSlice(a, "Box<");
const col: u32 = @intCast(source.items.len - "module deep\n".len + 1);
try source.appendSlice(a, "own<u8, u16>");
for (0..8_192) |_| try source.append(a, '>');
try source.appendSlice(a, ") -> void effects [] {}\n");
const result = try parse(a, source.items);
try std.testing.expect(result == .err);
try std.testing.expectEqualStrings("one wrapper type argument", result.err.expected);
try std.testing.expectEqualStrings("own", result.err.found);
try std.testing.expectEqual(@as(u32, 2), result.err.pos.line);
try std.testing.expectEqual(col, result.err.pos.col);
const missing = try parse(a, source.items[0 .. col - 1 + "module deep\n".len]);
try std.testing.expect(missing == .err);
try std.testing.expectEqualStrings("a type", missing.err.expected);
try std.testing.expectEqualStrings("end of file", missing.err.found);
}
test "type parser metadata failures propagate without exposing a partial AST" {
const source = "module m fn f(x: Pair<[own<u8>; 2], fn pick::<Pair<u8, bool>>>) -> void effects [] {}";
var fail_index: usize = 0;
while (true) : (fail_index += 1) {
try std.testing.expect(fail_index < 100);
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
var scratch: std.heap.ArenaAllocator = .init(failing.allocator());
const result = parse(scratch.allocator(), source);
if (failing.has_induced_failure) {
try std.testing.expectError(error.OutOfMemory, result);
} else {
try std.testing.expect((try result) == .ok);
}
scratch.deinit();
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
if (!failing.has_induced_failure) break;
}
}