frontend: provide contextual source repair advice
This commit is contained in:
1 parent
008867fe48
commit
2c6c7a3dcd
37 files changed
+562
-24
No files matched your search
+111
-9
@@ -5,12 +5,18 @@
|
||||
const std = @import("std");
|
||||
const lex = @import("lex.zig");
|
||||
const ast = @import("ast.zig");
|
||||
const diag = @import("../diag.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Tag = lex.Tag;
|
||||
const Token = lex.Token;
|
||||
|
||||
pub const Failure = struct { pos: lex.Pos, expected: []const u8, found: []const u8 };
|
||||
pub const Failure = struct {
|
||||
pos: lex.Pos,
|
||||
expected: []const u8,
|
||||
found: []const u8,
|
||||
repair_candidates: []const diag.RepairCandidate = &.{},
|
||||
};
|
||||
|
||||
pub const Result = union(enum) { ok: ast.Module, err: Failure };
|
||||
|
||||
@@ -41,6 +47,11 @@ const Parser = struct {
|
||||
module_name: ?[]const u8 = null,
|
||||
/// Struct literals are not allowed where a `{` starts a block (conditions).
|
||||
no_struct: bool = false,
|
||||
/// Hints are attached only at the exact token where the original parser
|
||||
/// fails, preserving caller-specific expected/found and source positions.
|
||||
type_advice: ?struct { at: usize, kind: diag.SourceRepairKind } = null,
|
||||
borrow_advice: ?struct { at: usize, kind: diag.SourceRepairKind } = null,
|
||||
speculative: bool = false,
|
||||
|
||||
fn peek(p: *Parser) Token {
|
||||
return p.tokens[p.pos];
|
||||
@@ -65,9 +76,96 @@ const Parser = struct {
|
||||
|
||||
fn fail(p: *Parser, t: Token, expected: []const u8) Error {
|
||||
p.failure = .{ .pos = t.pos, .expected = expected, .found = if (t.tag == .eof) "end of file" else t.text };
|
||||
if (p.type_advice) |a| if (a.at == p.pos) {
|
||||
p.failure.?.repair_candidates = try p.sliceAdvice(a.kind);
|
||||
};
|
||||
return error.Syntax;
|
||||
}
|
||||
|
||||
fn failAdvice(p: *Parser, t: Token, expected: []const u8, comptime kind: diag.SourceRepairKind) Error {
|
||||
const err = p.fail(t, expected);
|
||||
if (err == error.Syntax) p.failure.?.repair_candidates = kind.one();
|
||||
return err;
|
||||
}
|
||||
|
||||
/// The invalid type is already rejected. Copy only its angle-balanced
|
||||
/// fragment for speculative validation; closeAngle may split >> in place.
|
||||
/// Never consume or rewrite the real parser's tokens.
|
||||
fn sliceAdvice(p: *Parser, kind: diag.SourceRepairKind) Error![]const diag.RepairCandidate {
|
||||
var candidate = kind.candidate(&.{});
|
||||
if (p.peekAt(1).tag == .lt) fragment: {
|
||||
var depth: usize = 0;
|
||||
var end = p.pos + 1;
|
||||
var half = false;
|
||||
while (end < p.tokens.len) : (end += 1) {
|
||||
switch (p.tokens[end].tag) {
|
||||
.lt => depth += 1,
|
||||
.gt, .shr => {
|
||||
const closes: usize = if (p.tokens[end].tag == .shr) 2 else 1;
|
||||
if (depth <= closes) {
|
||||
half = depth == 1 and closes == 2;
|
||||
break;
|
||||
}
|
||||
depth -= closes;
|
||||
},
|
||||
.eof, .l_brace, .r_brace, .eq => break :fragment,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
if (end == p.tokens.len) break :fragment;
|
||||
const count = end - p.pos + 1;
|
||||
const tokens = try p.arena.alloc(Token, count + 1);
|
||||
@memcpy(tokens[0..count], p.tokens[p.pos .. end + 1]);
|
||||
if (half) {
|
||||
tokens[count - 1].tag = .gt;
|
||||
tokens[count - 1].text = tokens[count - 1].text[0..1];
|
||||
tokens[count - 1].end.col = tokens[count - 1].pos.col + 1;
|
||||
}
|
||||
tokens[count] = .{ .tag = .eof, .text = "", .pos = tokens[count - 1].end, .end = tokens[count - 1].end };
|
||||
var q: Parser = .{ .arena = p.arena, .tokens = tokens, .speculative = true };
|
||||
const ty = q.typeExpr() catch |err| switch (err) {
|
||||
error.Syntax => break :fragment,
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
};
|
||||
if (q.peek().tag != .eof or ty.kind != .wrapped or !std.mem.eql(u8, ty.kind.wrapped.wrapper, "slice")) break :fragment;
|
||||
// Render from an unmodified fragment: speculative parsing may have
|
||||
// split its private >> tokens, so use the original token spellings.
|
||||
var text: std.ArrayList(u8) = .empty;
|
||||
try text.appendSlice(p.arena, if (kind == .slice_mut_type) "slice_mut" else "slice");
|
||||
var previous: Tag = .ident;
|
||||
for (p.tokens[p.pos + 1 .. end + 1], 0..) |token, i| {
|
||||
if ((previous == .ident or previous == .int) and (token.tag == .ident or token.tag == .int)) try text.append(p.arena, ' ');
|
||||
try text.appendSlice(p.arena, if (half and i == count - 2) token.text[0..1] else token.text);
|
||||
previous = token.tag;
|
||||
}
|
||||
candidate.alternatives = try p.arena.dupe([]const u8, &.{text.items});
|
||||
}
|
||||
return try p.arena.dupe(diag.RepairCandidate, &.{candidate});
|
||||
}
|
||||
|
||||
fn exprWithBorrowAdvice(p: *Parser, kind: diag.SourceRepairKind) Error!ast.Expr {
|
||||
const saved = p.borrow_advice;
|
||||
p.borrow_advice = .{ .at = p.pos, .kind = kind };
|
||||
defer p.borrow_advice = saved;
|
||||
return p.expr();
|
||||
}
|
||||
|
||||
fn failPrimary(p: *Parser, t: Token) Error {
|
||||
if (t.tag == .invalid and std.mem.eql(u8, t.text, "!")) return p.failAdvice(t, "an expression", .boolean_negation);
|
||||
if (isWord(t, "zero")) return p.failAdvice(t, "an expression", .zero_storage);
|
||||
if (t.tag == .amp) if (p.borrow_advice) |a| if (a.at == p.pos) {
|
||||
var alternatives: []const []const u8 = &.{};
|
||||
if (a.kind == .borrow_argument) {
|
||||
const mutable = isWord(p.peekAt(1), "mut");
|
||||
const place = p.peekAt(if (mutable) 2 else 1);
|
||||
if (place.tag == .ident and !isKeyword(place.text)) alternatives = if (mutable) &.{"mut"} else &.{"ref"};
|
||||
}
|
||||
p.failure = .{ .pos = t.pos, .expected = "an expression", .found = t.text, .repair_candidates = try p.arena.dupe(diag.RepairCandidate, &.{a.kind.candidate(alternatives)}) };
|
||||
return error.Syntax;
|
||||
};
|
||||
return p.fail(t, "an expression");
|
||||
}
|
||||
|
||||
fn expect(p: *Parser, tag: Tag, what: []const u8) Error!Token {
|
||||
const t = p.peek();
|
||||
if (t.tag != tag) return p.fail(t, what);
|
||||
@@ -261,6 +359,8 @@ const Parser = struct {
|
||||
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 (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();
|
||||
@@ -394,7 +494,7 @@ const Parser = struct {
|
||||
ty = try p.typeExpr();
|
||||
}
|
||||
_ = try p.expect(.eq, "`=`");
|
||||
const e = try p.expr();
|
||||
const e = try p.exprWithBorrowAdvice(.borrow_binding);
|
||||
_ = try p.expect(.semicolon, "`;`");
|
||||
return .{ .span = p.span(start), .kind = .{ .let = .{ .names = names.items, .ty = ty, .e = e, .tuple = tuple } } };
|
||||
}
|
||||
@@ -411,13 +511,14 @@ const Parser = struct {
|
||||
if (isWord(t, "var")) {
|
||||
_ = p.next();
|
||||
const name = try p.ident("a name");
|
||||
if (p.peek().tag == .eq) return p.failAdvice(p.peek(), "`:` and the variable's type", .annotate_var_type);
|
||||
_ = try p.expect(.colon, "`:` and the variable's type");
|
||||
const ty = try p.typeExpr();
|
||||
_ = try p.expect(.eq, "`=`");
|
||||
var init: ?ast.Expr = null;
|
||||
if (p.atWord("zero")) {
|
||||
_ = p.next();
|
||||
} else init = try p.expr();
|
||||
} else init = try p.exprWithBorrowAdvice(.borrow_binding);
|
||||
_ = try p.expect(.semicolon, "`;`");
|
||||
return .{ .span = p.span(start), .kind = .{ .@"var" = .{ .name = name, .ty = ty, .init = init } } };
|
||||
}
|
||||
@@ -783,7 +884,7 @@ const Parser = struct {
|
||||
_ = p.next();
|
||||
const ty = try p.typeExpr();
|
||||
try p.closeAngle();
|
||||
const args = try p.callArgs();
|
||||
const args = try p.callArgs(true);
|
||||
const callee = try p.box(ast.Expr, e);
|
||||
e = .{ .span = p.span(start), .kind = .{ .call = .{ .callee = callee, .type_arg = ty, .args = args } } };
|
||||
}
|
||||
@@ -801,7 +902,7 @@ const Parser = struct {
|
||||
.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 args = try p.callArgs(e.kind != .variant);
|
||||
const callee = try p.box(ast.Expr, e);
|
||||
e = .{ .span = p.span(start), .kind = .{ .call = .{ .callee = callee, .type_arg = null, .args = args } } };
|
||||
},
|
||||
@@ -813,6 +914,7 @@ const Parser = struct {
|
||||
var hi: ?*const ast.Expr = null;
|
||||
if (p.peek().tag == .dot_dot) {
|
||||
_ = p.next();
|
||||
if (p.peek().tag == .r_bracket) return p.failAdvice(p.peek(), "an expression", .explicit_range_end);
|
||||
hi = try p.box(ast.Expr, try p.expr());
|
||||
}
|
||||
p.no_struct = saved;
|
||||
@@ -826,14 +928,14 @@ const Parser = struct {
|
||||
}
|
||||
}
|
||||
|
||||
fn callArgs(p: *Parser) Error![]const ast.Expr {
|
||||
fn callArgs(p: *Parser, borrow_arguments: bool) Error![]const ast.Expr {
|
||||
_ = try p.expect(.l_paren, "`(`");
|
||||
const saved = p.no_struct;
|
||||
p.no_struct = false;
|
||||
defer p.no_struct = saved;
|
||||
var args: std.ArrayList(ast.Expr) = .empty;
|
||||
while (p.peek().tag != .r_paren) {
|
||||
try args.append(p.arena, try p.expr());
|
||||
try args.append(p.arena, if (borrow_arguments) try p.exprWithBorrowAdvice(.borrow_argument) else try p.expr());
|
||||
if (p.peek().tag != .comma) break;
|
||||
_ = p.next();
|
||||
}
|
||||
@@ -950,7 +1052,7 @@ const Parser = struct {
|
||||
};
|
||||
return .{ .span = p.span(start), .kind = .{ .match_expr = try p.box(ast.Match, .{ .scrutinee = scrut, .arms = as, .span = p.span(start), .kind = kind }) } };
|
||||
}
|
||||
if (isKeyword(t.text) and !(isWord(t, "struct") and p.peekAt(1).tag == .dot and isWord(p.peekAt(2), "unpack"))) return p.fail(t, "an expression");
|
||||
if (isKeyword(t.text) and !(isWord(t, "struct") and p.peekAt(1).tag == .dot and isWord(p.peekAt(2), "unpack"))) return p.failPrimary(t);
|
||||
_ = p.next();
|
||||
if (p.peek().tag == .colon_colon and p.peekAt(1).tag != .lt) {
|
||||
_ = p.next();
|
||||
@@ -962,7 +1064,7 @@ const Parser = struct {
|
||||
}
|
||||
return .{ .span = p.span(start), .kind = .{ .name = t.text } };
|
||||
},
|
||||
else => return p.fail(t, "an expression"),
|
||||
else => return p.failPrimary(t),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user