cli: parse qualified constructor paths iteratively
This commit is contained in:
1 parent
d39a422b2b
commit
371329f3f0
5 files changed
+227
-12
No files matched your search
+144
-8
@@ -1098,14 +1098,32 @@ const Parser = struct {
|
||||
}
|
||||
|
||||
fn exprPath(p: *Parser, e: ast.Expr) Error!?[]const u8 {
|
||||
return switch (e.kind) {
|
||||
.name => |name| name,
|
||||
.field => |field| blk: {
|
||||
const parent = try p.exprPath(field.e.*) orelse break :blk null;
|
||||
break :blk try std.fmt.allocPrint(p.arena, "{s}.{s}", .{ parent, field.name });
|
||||
},
|
||||
else => null,
|
||||
};
|
||||
// Reject non-name roots before allocating, just as recursive descent
|
||||
// did. Only the final spelling is owned; no intermediate prefixes.
|
||||
var node = e;
|
||||
while (node.kind == .field) node = node.kind.field.e.*;
|
||||
if (node.kind != .name) return null;
|
||||
if (e.kind == .name) return node.kind.name;
|
||||
var length = node.kind.name.len;
|
||||
node = e;
|
||||
while (node.kind == .field) {
|
||||
length = std.math.add(usize, length, node.kind.field.name.len) catch return error.OutOfMemory;
|
||||
length = std.math.add(usize, length, 1) catch return error.OutOfMemory;
|
||||
node = node.kind.field.e.*;
|
||||
}
|
||||
const out = try p.arena.alloc(u8, length);
|
||||
var cursor = length;
|
||||
node = e;
|
||||
while (node.kind == .field) {
|
||||
const field = node.kind.field;
|
||||
cursor -= field.name.len;
|
||||
@memcpy(out[cursor .. cursor + field.name.len], field.name);
|
||||
cursor -= 1;
|
||||
out[cursor] = '.';
|
||||
node = field.e.*;
|
||||
}
|
||||
@memcpy(out[0..cursor], node.kind.name);
|
||||
return out;
|
||||
}
|
||||
|
||||
fn structLiteral(p: *Parser, start: lex.Pos, name: []const u8) Error!ast.Expr {
|
||||
@@ -1616,6 +1634,124 @@ test "unary metadata and output allocation failures publish no partial AST" {
|
||||
}
|
||||
}
|
||||
|
||||
test "parser qualified paths preserve recursive spelling and root classification" {
|
||||
const Reference = struct {
|
||||
fn path(a: Allocator, e: ast.Expr) Allocator.Error!?[]const u8 {
|
||||
return switch (e.kind) {
|
||||
.name => |name| name,
|
||||
.field => |field| blk: {
|
||||
const parent = try path(a, field.e.*) orelse break :blk null;
|
||||
break :blk try std.fmt.allocPrint(a, "{s}.{s}", .{ parent, field.name });
|
||||
},
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
};
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const span: ast.Span = .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 2 } };
|
||||
const root: ast.Expr = .{ .span = span, .kind = .{ .name = "Root" } };
|
||||
const invalid: ast.Expr = .{ .span = span, .kind = .{ .boolean = true } };
|
||||
var p: Parser = .{ .arena = a, .tokens = &.{} };
|
||||
for ([_]ast.Expr{ root, invalid }) |leaf| {
|
||||
var current = leaf;
|
||||
for (0..16) |i| {
|
||||
try std.testing.expectEqualDeep(try Reference.path(a, current), try p.exprPath(current));
|
||||
const child = try a.create(ast.Expr);
|
||||
child.* = current;
|
||||
current = .{ .span = span, .kind = .{ .field = .{ .e = child, .name = if (i % 2 == 0) "left" else "right" } } };
|
||||
}
|
||||
}
|
||||
var current = root;
|
||||
for (0..65_536) |i| {
|
||||
const child = try a.create(ast.Expr);
|
||||
child.* = current;
|
||||
current = .{ .span = span, .kind = .{ .field = .{ .e = child, .name = if (i % 2 == 0) "x" else "yz" } } };
|
||||
}
|
||||
var allocation: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
|
||||
p.arena = allocation.allocator();
|
||||
const owned = (try p.exprPath(current)).?;
|
||||
defer allocation.allocator().free(owned);
|
||||
try std.testing.expectEqual(@as(usize, 1), allocation.allocations);
|
||||
try std.testing.expectEqual(@as(usize, 4 + 32_768 * 5), owned.len);
|
||||
try std.testing.expectEqualStrings("Root", owned[0..4]);
|
||||
for (0..32_768) |i| try std.testing.expectEqualStrings(".x.yz", owned[4 + i * 5 .. 4 + (i + 1) * 5]);
|
||||
var failure: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
p.arena = failure.allocator();
|
||||
try std.testing.expectError(error.OutOfMemory, p.exprPath(current));
|
||||
try std.testing.expect(p.failure == null);
|
||||
var no_alloc: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
p.arena = no_alloc.allocator();
|
||||
try std.testing.expectEqualStrings("Root", (try p.exprPath(root)).?);
|
||||
const bad: ast.Expr = .{ .span = span, .kind = .{ .field = .{ .e = &invalid, .name = "field" } } };
|
||||
try std.testing.expect(try p.exprPath(bad) == null);
|
||||
try std.testing.expect(!no_alloc.has_induced_failure);
|
||||
}
|
||||
|
||||
test "deep qualified constructors preserve names type arguments spans and errors" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
var name: std.ArrayList(u8) = .empty;
|
||||
for (0..65_536) |_| try name.appendSlice(a, "ns.");
|
||||
try name.appendSlice(a, "Record");
|
||||
const header = "module deep\nfn f() -> void effects [] { let x = ";
|
||||
const col: u32 = @intCast(header.len - "module deep\n".len + 1);
|
||||
for ([_][]const u8{ "::A", "::<u64>::A", " { field: 1u64 }", "::<u64> { field: 1u64 }" }, 0..) |suffix, i| {
|
||||
const source = try std.fmt.allocPrint(a, "{s}{s}{s}; }}", .{ header, name.items, suffix });
|
||||
const result = try parseWithFile(a, source, "qualified.aji");
|
||||
try std.testing.expect(result == .ok);
|
||||
const value = result.ok.functions[0].body.stmts[0].kind.let.e;
|
||||
try std.testing.expectEqualStrings("qualified.aji", value.span.file.?);
|
||||
try std.testing.expectEqual(@as(u32, 2), value.span.start.line);
|
||||
try std.testing.expectEqual(col, value.span.start.col);
|
||||
try std.testing.expectEqual(col + @as(u32, @intCast(name.items.len + suffix.len)), value.span.end.col);
|
||||
if (i < 2) {
|
||||
try std.testing.expectEqualStrings(name.items, value.kind.variant.@"enum");
|
||||
try std.testing.expectEqualStrings("A", value.kind.variant.name);
|
||||
try std.testing.expectEqual(@as(usize, i), value.kind.variant.type_args.len);
|
||||
} else {
|
||||
try std.testing.expectEqualStrings(name.items, value.kind.struct_lit.name);
|
||||
try std.testing.expectEqualStrings("field", value.kind.struct_lit.fields[0].name);
|
||||
try std.testing.expectEqual(@as(usize, i - 2), value.kind.struct_lit.type_args.len);
|
||||
}
|
||||
}
|
||||
const broken = try std.fmt.allocPrint(a, "{s}{s}::; }}", .{ header, name.items });
|
||||
const result = try parse(a, broken);
|
||||
try std.testing.expect(result == .err);
|
||||
try std.testing.expectEqualStrings("a variant name", result.err.expected);
|
||||
try std.testing.expectEqualStrings(";", result.err.found);
|
||||
try std.testing.expectEqual(col + @as(u32, @intCast(name.items.len + 2)), result.err.pos.col);
|
||||
}
|
||||
|
||||
test "qualified constructor allocation failures publish no AST or syntax diagnostic" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
var source: std.ArrayList(u8) = .empty;
|
||||
try source.appendSlice(a, "module deep fn f() -> void effects [] { let x = ");
|
||||
for (0..64) |_| try source.appendSlice(a, "ns.");
|
||||
try source.appendSlice(a, "Record::<u64>::A; let y = ");
|
||||
for (0..64) |_| try source.appendSlice(a, "ns.");
|
||||
try source.appendSlice(a, "Record::<u64> { field: 1u64 }; }");
|
||||
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 arena: std.heap.ArenaAllocator = .init(failing.allocator());
|
||||
const result = parse(arena.allocator(), source.items);
|
||||
if (failing.has_induced_failure) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
} else {
|
||||
try std.testing.expect((try result) == .ok);
|
||||
}
|
||||
arena.deinit();
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
if (!failing.has_induced_failure) break;
|
||||
}
|
||||
}
|
||||
|
||||
test "qualified enum payload lists are nonempty, including grouped and generic variants" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
Reference in new issue
Block a user