frontend: collect independent source diagnostics without cascades
This commit is contained in:
1 parent
2c6c7a3dcd
commit
4cda44823d
47 files changed
+697
-44
No files matched your search
+78
-10
@@ -1,6 +1,6 @@
|
||||
//! Parser for `.aji` source 0.1 (reference/grammar.ebnf). Recursive
|
||||
//! descent. On the first error it stops with exactly one `E7001` carrying
|
||||
//! the span, what was expected and what was found: no recovery, no cascade.
|
||||
//! descent. At most one E7001 per independently bounded top-level item;
|
||||
//! malformed boundaries stop collection. No partial AST escapes on error.
|
||||
|
||||
const std = @import("std");
|
||||
const lex = @import("lex.zig");
|
||||
@@ -18,7 +18,7 @@ pub const Failure = struct {
|
||||
repair_candidates: []const diag.RepairCandidate = &.{},
|
||||
};
|
||||
|
||||
pub const Result = union(enum) { ok: ast.Module, err: Failure };
|
||||
pub const Result = union(enum) { ok: ast.Module, err: Failure, errors: []const Failure };
|
||||
|
||||
pub fn parse(arena: Allocator, src: []const u8) Allocator.Error!Result {
|
||||
return parseWithFile(arena, src, null);
|
||||
@@ -28,7 +28,7 @@ pub fn parseWithFile(arena: Allocator, src: []const u8, file: ?[]const u8) Alloc
|
||||
const tokens = try lex.tokenize(arena, src);
|
||||
var p: Parser = .{ .arena = arena, .tokens = try arena.dupe(Token, tokens), .file = file };
|
||||
const m = p.module() catch |e| switch (e) {
|
||||
error.Syntax => return .{ .err = p.failure.? },
|
||||
error.Syntax => return if (p.failures.items.len > 1) .{ .errors = p.failures.items } else .{ .err = if (p.failures.items.len == 1) p.failures.items[0] else p.failure.? },
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
};
|
||||
return .{ .ok = m };
|
||||
@@ -43,6 +43,7 @@ const Parser = struct {
|
||||
/// Also tracks a consumed half of `>>` while the other half stays at pos.
|
||||
last_end: ?lex.Pos = null,
|
||||
failure: ?Failure = null,
|
||||
failures: std.ArrayList(Failure) = .empty,
|
||||
file: ?[]const u8 = null,
|
||||
module_name: ?[]const u8 = null,
|
||||
/// Struct literals are not allowed where a `{` starts a block (conditions).
|
||||
@@ -234,15 +235,82 @@ const Parser = struct {
|
||||
var types: std.ArrayList(ast.TypeDecl) = .empty;
|
||||
var fns: std.ArrayList(ast.FnDecl) = .empty;
|
||||
while (p.peek().tag != .eof) {
|
||||
if (p.atWord("struct") or p.atWord("enum")) {
|
||||
try types.append(p.arena, try p.typeDecl());
|
||||
} else if (p.atWord("fn")) {
|
||||
try fns.append(p.arena, try p.fnDecl());
|
||||
} else return p.fail(p.peek(), "`struct`, `enum` or `fn`");
|
||||
const start = p.pos;
|
||||
const item = p.declaration() catch |err| switch (err) {
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
error.Syntax => {
|
||||
try p.failures.append(p.arena, p.failure.?);
|
||||
if (p.failures.items.len == diag.source_diagnostic_limit) return error.Syntax;
|
||||
const end = try p.recoveryEnd(start) orelse return error.Syntax;
|
||||
p.pos = end;
|
||||
p.last_end = p.tokens[end - 1].end;
|
||||
p.failure = null;
|
||||
p.no_struct = false;
|
||||
p.type_advice = null;
|
||||
p.borrow_advice = null;
|
||||
continue;
|
||||
},
|
||||
};
|
||||
switch (item) {
|
||||
.type_decl => |t| try types.append(p.arena, t),
|
||||
.function => |f| try fns.append(p.arena, f),
|
||||
}
|
||||
}
|
||||
if (p.failures.items.len != 0) return error.Syntax;
|
||||
return .{ .name = name, .imports = imports.items, .types = types.items, .functions = fns.items };
|
||||
}
|
||||
|
||||
fn declaration(p: *Parser) Error!union(enum) { type_decl: ast.TypeDecl, function: ast.FnDecl } {
|
||||
if (p.atWord("struct") or p.atWord("enum")) return .{ .type_decl = try p.typeDecl() };
|
||||
if (p.atWord("fn")) return .{ .function = try p.fnDecl() };
|
||||
return p.fail(p.peek(), "`struct`, `enum` or `fn`");
|
||||
}
|
||||
|
||||
fn itemKeyword(t: Token) bool {
|
||||
return isWord(t, "fn") or isWord(t, "struct") or isWord(t, "enum");
|
||||
}
|
||||
|
||||
/// This scan proves a delimiter boundary, not the failed item's semantics.
|
||||
/// It never trusts a keyword's column and never rewinds past a failure.
|
||||
fn recoveryEnd(p: *Parser, start: usize) Error!?usize {
|
||||
if (!itemKeyword(p.tokens[start])) return null;
|
||||
var stack: std.ArrayList(Tag) = .empty;
|
||||
var body = false;
|
||||
for (p.tokens[start..], start..) |t, i| {
|
||||
if (t.tag == .invalid and !std.mem.eql(u8, t.text, "!")) return null;
|
||||
if (!body and stack.items.len == 0 and i != start and itemKeyword(t)) {
|
||||
// Static function contracts use fn(...), selections fn path or
|
||||
// fn path::<...>; neither is a new fn Name(...) declaration.
|
||||
if (!isWord(t, "fn") or (i + 2 < p.tokens.len and p.tokens[i + 1].tag == .ident and
|
||||
(p.tokens[i + 2].tag == .l_paren or p.tokens[i + 2].tag == .lt))) return null;
|
||||
}
|
||||
switch (t.tag) {
|
||||
.l_paren, .l_bracket, .l_brace => {
|
||||
if (t.tag == .l_brace and stack.items.len == 0) body = true;
|
||||
try stack.append(p.arena, t.tag);
|
||||
},
|
||||
.r_paren, .r_bracket, .r_brace => {
|
||||
const open = stack.pop() orelse return null;
|
||||
const wanted: Tag = switch (t.tag) {
|
||||
.r_paren => .l_paren,
|
||||
.r_bracket => .l_bracket,
|
||||
else => .l_brace,
|
||||
};
|
||||
if (open != wanted) return null;
|
||||
if (body and stack.items.len == 0) {
|
||||
const end = i + 1;
|
||||
if (end <= p.pos or end >= p.tokens.len) return null;
|
||||
if (p.tokens[end].tag != .eof and !itemKeyword(p.tokens[end])) return null;
|
||||
return end;
|
||||
}
|
||||
},
|
||||
.eof => return null,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
fn path(p: *Parser) Error![]const u8 {
|
||||
var out: std.ArrayList(u8) = .empty;
|
||||
try out.appendSlice(p.arena, try p.ident("a name"));
|
||||
@@ -359,7 +427,7 @@ 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"))
|
||||
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")) {
|
||||
|
||||
Reference in new issue
Block a user