cli: parse unary prefix chains iteratively
This commit is contained in:
1 parent
fb80413b37
commit
c36b269289
5 files changed
+270
-32
No files matched your search
+170
-32
@@ -946,41 +946,52 @@ const Parser = struct {
|
||||
}
|
||||
|
||||
fn unary(p: *Parser) Error!ast.Expr {
|
||||
const start = p.peek().pos;
|
||||
const t = p.peek();
|
||||
if (t.tag == .minus) {
|
||||
_ = p.next();
|
||||
const operand = try p.box(ast.Expr, try p.unary());
|
||||
return .{ .span = p.span(start), .kind = .{ .neg = operand } };
|
||||
}
|
||||
if (isWord(t, "not")) {
|
||||
_ = p.next();
|
||||
const operand = try p.box(ast.Expr, try p.unary());
|
||||
return .{ .span = p.span(start), .kind = .{ .not = operand } };
|
||||
}
|
||||
if (t.tag == .star) {
|
||||
_ = p.next();
|
||||
const operand = try p.box(ast.Expr, try p.unary());
|
||||
return .{ .span = p.span(start), .kind = .{ .deref = operand } };
|
||||
}
|
||||
if (isWord(t, "ref") or isWord(t, "mut")) {
|
||||
_ = p.next();
|
||||
var unchecked = false;
|
||||
if (p.atWord("unchecked")) {
|
||||
const Prefix = struct {
|
||||
start: lex.Pos,
|
||||
kind: enum { neg, not, deref, borrow },
|
||||
mutable: bool = false,
|
||||
unchecked: bool = false,
|
||||
};
|
||||
var pending: std.ArrayList(Prefix) = .empty;
|
||||
defer pending.deinit(p.arena);
|
||||
var e: ast.Expr = while (true) {
|
||||
const t = p.peek();
|
||||
const kind: ?@FieldType(Prefix, "kind") = if (t.tag == .minus) .neg else if (isWord(t, "not")) .not else if (t.tag == .star) .deref else null;
|
||||
if (kind) |k| {
|
||||
_ = p.next();
|
||||
unchecked = true;
|
||||
try pending.append(p.arena, .{ .start = t.pos, .kind = k });
|
||||
continue;
|
||||
}
|
||||
// The grammar's place production also permits `* unary`.
|
||||
// Keep other prefix forms out of an inline borrow operand.
|
||||
const place = if (p.peek().tag == .star) try p.unary() else try p.postfix();
|
||||
return .{ .span = p.span(start), .kind = .{ .borrow = .{ .mutable = isWord(t, "mut"), .unchecked = unchecked, .place = try p.box(ast.Expr, place) } } };
|
||||
if (isWord(t, "ref") or isWord(t, "mut")) {
|
||||
_ = p.next();
|
||||
var unchecked = false;
|
||||
if (p.atWord("unchecked")) {
|
||||
_ = p.next();
|
||||
unchecked = true;
|
||||
}
|
||||
try pending.append(p.arena, .{ .start = t.pos, .kind = .borrow, .mutable = isWord(t, "mut"), .unchecked = unchecked });
|
||||
// A borrow admits `* unary`; all other operands still use
|
||||
// postfix directly, preserving the place grammar boundary.
|
||||
if (p.peek().tag == .star) continue;
|
||||
break try p.postfix();
|
||||
}
|
||||
if (isWord(t, "move")) {
|
||||
_ = p.next();
|
||||
const name = try p.ident("the name of the value to move");
|
||||
break .{ .span = p.span(t.pos), .kind = .{ .move = name } };
|
||||
}
|
||||
break try p.postfix();
|
||||
};
|
||||
while (pending.pop()) |prefix| {
|
||||
const operand = try p.box(ast.Expr, e);
|
||||
e = .{ .span = p.span(prefix.start), .kind = switch (prefix.kind) {
|
||||
.neg => .{ .neg = operand },
|
||||
.not => .{ .not = operand },
|
||||
.deref => .{ .deref = operand },
|
||||
.borrow => .{ .borrow = .{ .mutable = prefix.mutable, .unchecked = prefix.unchecked, .place = operand } },
|
||||
} };
|
||||
}
|
||||
if (isWord(t, "move")) {
|
||||
_ = p.next();
|
||||
const name = try p.ident("the name of the value to move");
|
||||
return .{ .span = p.span(start), .kind = .{ .move = name } };
|
||||
}
|
||||
return p.postfix();
|
||||
return e;
|
||||
}
|
||||
|
||||
fn postfix(p: *Parser) Error!ast.Expr {
|
||||
@@ -1478,6 +1489,133 @@ test "dereferenced borrow consumes unary place without swallowing outer operatio
|
||||
}
|
||||
}
|
||||
|
||||
// Independent copy of the pre-repair unary derivation, used only on shallow
|
||||
// prefixes. Postfix parsing is outside this oracle's scope.
|
||||
fn referenceUnary(p: *Parser) Error!ast.Expr {
|
||||
const start = p.peek().pos;
|
||||
const t = p.peek();
|
||||
if (t.tag == .minus or Parser.isWord(t, "not") or t.tag == .star) {
|
||||
_ = p.next();
|
||||
const operand = try p.box(ast.Expr, try referenceUnary(p));
|
||||
return .{ .span = p.span(start), .kind = if (t.tag == .minus) .{ .neg = operand } else if (t.tag == .star) .{ .deref = operand } else .{ .not = operand } };
|
||||
}
|
||||
if (Parser.isWord(t, "ref") or Parser.isWord(t, "mut")) {
|
||||
_ = p.next();
|
||||
var unchecked = false;
|
||||
if (p.atWord("unchecked")) {
|
||||
_ = p.next();
|
||||
unchecked = true;
|
||||
}
|
||||
const place = if (p.peek().tag == .star) try referenceUnary(p) else try p.postfix();
|
||||
return .{ .span = p.span(start), .kind = .{ .borrow = .{ .mutable = Parser.isWord(t, "mut"), .unchecked = unchecked, .place = try p.box(ast.Expr, place) } } };
|
||||
}
|
||||
if (Parser.isWord(t, "move")) {
|
||||
_ = p.next();
|
||||
const name = try p.ident("the name of the value to move");
|
||||
return .{ .span = p.span(start), .kind = .{ .move = name } };
|
||||
}
|
||||
return p.postfix();
|
||||
}
|
||||
|
||||
test "unary continuations retain shallow syntax, ranges and exact failures" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const prefixes = [_][]const u8{ "", "- ", "not\n", "* ", "ref * ", "mut unchecked * ", "ref ", "move " };
|
||||
const leaves = [_][]const u8{ "value[1u8].field", "(value)", "callee(arg)", "move owner", "!flag", ";", "" };
|
||||
for (prefixes) |outer| for (prefixes) |inner| for (leaves) |leaf| {
|
||||
const source = try std.fmt.allocPrint(a, "// UTF-8: λ\n{s}{s}{s}", .{ outer, inner, leaf });
|
||||
const tokens = try lex.tokenize(a, source);
|
||||
var old: Parser = .{ .arena = a, .tokens = try a.dupe(Token, tokens), .file = "prefix.aji" };
|
||||
var current: Parser = .{ .arena = a, .tokens = try a.dupe(Token, tokens), .file = "prefix.aji" };
|
||||
const expected = referenceUnary(&old);
|
||||
const actual = current.unary();
|
||||
if (expected) |expression| {
|
||||
try std.testing.expectEqualDeep(expression, try actual);
|
||||
} else |err| {
|
||||
try std.testing.expectError(err, actual);
|
||||
try std.testing.expectEqualDeep(old.failure, current.failure);
|
||||
}
|
||||
try std.testing.expectEqual(old.pos, current.pos);
|
||||
try std.testing.expectEqualDeep(old.last_end, current.last_end);
|
||||
};
|
||||
}
|
||||
|
||||
test "deep mixed unary chains parse with exact inner and outer spans" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 32_768;
|
||||
const prefixes = [_][]const u8{ "- ", "not ", "* ", "ref * ", "mut unchecked * " };
|
||||
const header = "module deep\nfn f() -> void effects [] { apply(";
|
||||
const column: u32 = @intCast(header.len - "module deep\n".len + 1);
|
||||
var source: std.ArrayList(u8) = .empty;
|
||||
try source.appendSlice(a, header);
|
||||
for (0..depth) |i| try source.appendSlice(a, prefixes[i % prefixes.len]);
|
||||
const leaf_col: u32 = @intCast(source.items.len - "module deep\n".len + 1);
|
||||
try source.appendSlice(a, "value.field");
|
||||
const end_col: u32 = @intCast(source.items.len - "module deep\n".len + 1);
|
||||
try source.appendSlice(a, "); }");
|
||||
const result = try parseWithFile(a, source.items, "deep.aji");
|
||||
try std.testing.expect(result == .ok);
|
||||
var e = result.ok.functions[0].body.stmts[0].kind.expr.kind.call.args[0];
|
||||
var col = column;
|
||||
for (0..depth) |i| {
|
||||
try std.testing.expectEqualStrings("deep.aji", e.span.file.?);
|
||||
try std.testing.expectEqual(@as(u32, 2), e.span.start.line);
|
||||
try std.testing.expectEqual(col, e.span.start.col);
|
||||
try std.testing.expectEqual(end_col, e.span.end.col);
|
||||
switch (i % prefixes.len) {
|
||||
0 => e = e.kind.neg.*,
|
||||
1 => e = e.kind.not.*,
|
||||
2 => e = e.kind.deref.*,
|
||||
3, 4 => {
|
||||
try std.testing.expectEqual(i % prefixes.len == 4, e.kind.borrow.mutable);
|
||||
try std.testing.expectEqual(i % prefixes.len == 4, e.kind.borrow.unchecked);
|
||||
const place = e.kind.borrow.place;
|
||||
try std.testing.expectEqual(col + @as(u32, if (i % prefixes.len == 3) 4 else 14), place.span.start.col);
|
||||
try std.testing.expectEqual(end_col, place.span.end.col);
|
||||
e = place.kind.deref.*;
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
col += @intCast(prefixes[i % prefixes.len].len);
|
||||
}
|
||||
try std.testing.expectEqual(leaf_col, e.span.start.col);
|
||||
try std.testing.expect(e.kind == .field);
|
||||
try std.testing.expectEqualStrings("field", e.kind.field.name);
|
||||
const broken = try std.fmt.allocPrint(a, "{s}); }}", .{source.items[0 .. leaf_col - 1 + "module deep\n".len]});
|
||||
const failure = try parse(a, broken);
|
||||
try std.testing.expect(failure == .err);
|
||||
try std.testing.expectEqualStrings("an expression", failure.err.expected);
|
||||
try std.testing.expectEqualStrings(")", failure.err.found);
|
||||
try std.testing.expectEqual(leaf_col, failure.err.pos.col);
|
||||
}
|
||||
|
||||
test "unary metadata and output allocation failures publish no partial AST" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
var source: std.ArrayList(u8) = .empty;
|
||||
try source.appendSlice(fixture.allocator(), "module m fn f() -> void effects [] { apply(");
|
||||
for (0..64) |_| try source.appendSlice(fixture.allocator(), "not ");
|
||||
try source.appendSlice(fixture.allocator(), "- * ref * mut unchecked * value.field); }");
|
||||
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.items);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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