cli: capture complete source parser ranges
This commit is contained in:
1 parent
1a80c42289
commit
ba2cb1340a
3 files changed
+180
-16
No files matched your search
@@ -131,7 +131,7 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
|
||||
| ID | Status | Task | Depends | Output | Done when |
|
||||
|---|---|---|---|---|---|
|
||||
| P6-1 | todo | Complete the source grammar: expressions, `match`, `borrow` blocks, `task.scope`, `select`, `defer`, `?`, generics, lifetimes, contracts, attributes | — (may start after P2) | `reference/grammar.ebnf`, spec 01 | every construct in `examples/` is covered; grammar is LL(k) or otherwise documented as unambiguous |
|
||||
| P6-2 | todo | Lexer and parser with spans | P6-1 | `src/frontend/{lex,parse}.zig` | all `examples/` parse; syntax errors are registered diagnostics |
|
||||
| P6-2 | doing | Lexer and parser with spans | P6-1 | `src/frontend/{lex,parse}.zig` | all `examples/` parse; syntax errors are registered diagnostics |
|
||||
| P6-3 | done (RFC 0021 Implemented) | Explicit package modules, direct imports and qualified nominal types | P6-2 | `src/frontend/modules.zig`, spec 15 §14 | deterministic graph/name resolution; source/AIR and CLI tests |
|
||||
| P6-4 | done (RFC 0022 Implemented) | Type checking and monomorphized generics with structural constraints (`Copy`, `Send`, `Share`, `Freeze`, `Local`) | P6-3 | `src/frontend/generics.zig`, spec 15 §15 | generic code instantiates deterministically; constraint violations diagnosed |
|
||||
| P6-5 | todo | Lowering to AIR: control flow, `?`, `defer` cleanup edges, borrow blocks, task scopes | P6-4 | `src/frontend/lower.zig` | lowered AIR verifies; lowering is a pure function (same input ⇒ same AIR bytes) |
|
||||
@@ -213,4 +213,12 @@ Eight Phase 2 AIR rejection twins and five successful source runs cover owner/re
|
||||
|
||||
Validation: unit tests and full conformance pass, 1,225/1,225; all 24 new cases pass with per-instruction collection. Lifetime CLI checks pass 188 assertions, related-location checks 301, coverage 120/120 and formatting checks pass. The formatter checks 940 public files with 606 identical AIR outputs. Across 916 original public sources, check outcomes and all 421 accepted canonical AIR outputs are unchanged; four diagnostic outputs gain source positions, one also recovering a previously suppressed error. Independent read-only Claude review found no remaining issues.
|
||||
|
||||
Next P6-2 follow-up: capture full parser spans after parsing block/unary operands. Current annotated ranges can end before an `if` starts; this is separate from the primary-position correction above.
|
||||
The parser-range follow-up is resolved in the P6-2 increment below.
|
||||
|
||||
## P6-2 complete parser ranges (2026-10-07)
|
||||
|
||||
Twelve parser sites now finish parsing their bodies/operands before capturing the node's end. Nested type closers track the consumed first half of `>>` independently of the token cursor. These changes restore complete block, prefix, `move` and `if` ranges, including cancellation arms; syntax, acceptance rules, primary diagnostic positions and canonical AIR are unchanged.
|
||||
|
||||
Three unit regressions failed before the correction and pass afterwards. They check exact ranges, AST containment, file/namespace provenance, comments, CRLF and nested closers. The existing CI formatter suite also validates nonempty ordered annotation ranges and reconstruction of canonical AIR across successful public lowerings. No normative document, bundle or evaluation input changes; P6-2 remains open for its remaining grammar work.
|
||||
|
||||
Validation: unit tests, formatting checks and full conformance pass, 1,225/1,225. Across 940 public sources, all check/lower outputs and exit codes remain unchanged, including 606 successful canonical lowerings. The formatter passes 4,139 checks; lifetime and related-position CLI suites pass 188/301 checks. Independent read-only Claude review found no remaining issues.
|
||||
+154
-14
@@ -34,6 +34,8 @@ const Parser = struct {
|
||||
arena: Allocator,
|
||||
tokens: []Token,
|
||||
pos: usize = 0,
|
||||
/// Also tracks a consumed half of `>>` while the other half stays at pos.
|
||||
last_end: ?lex.Pos = null,
|
||||
failure: ?Failure = null,
|
||||
file: ?[]const u8 = null,
|
||||
module_name: ?[]const u8 = null,
|
||||
@@ -50,12 +52,15 @@ const Parser = struct {
|
||||
|
||||
fn next(p: *Parser) Token {
|
||||
const t = p.tokens[p.pos];
|
||||
if (t.tag != .eof) p.pos += 1;
|
||||
if (t.tag != .eof) {
|
||||
p.pos += 1;
|
||||
p.last_end = t.end;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
fn prevEnd(p: *Parser) lex.Pos {
|
||||
return if (p.pos == 0) p.tokens[0].pos else p.tokens[p.pos - 1].end;
|
||||
return p.last_end orelse p.tokens[0].pos;
|
||||
}
|
||||
|
||||
fn fail(p: *Parser, t: Token, expected: []const u8) Error {
|
||||
@@ -79,6 +84,7 @@ const Parser = struct {
|
||||
rest.text = t.text[1..];
|
||||
rest.pos.col += 1;
|
||||
p.tokens[p.pos] = rest;
|
||||
p.last_end = rest.pos;
|
||||
return;
|
||||
}
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
@@ -431,7 +437,8 @@ const Parser = struct {
|
||||
if (isWord(t, "while")) {
|
||||
_ = p.next();
|
||||
const cond = try p.condition();
|
||||
return .{ .span = p.span(start), .kind = .{ .@"while" = .{ .cond = cond, .body = try p.block() } } };
|
||||
const body = try p.block();
|
||||
return .{ .span = p.span(start), .kind = .{ .@"while" = .{ .cond = cond, .body = body } } };
|
||||
}
|
||||
if (isWord(t, "for")) {
|
||||
_ = p.next();
|
||||
@@ -443,20 +450,24 @@ const Parser = struct {
|
||||
_ = try p.expect(.dot_dot, "`..`");
|
||||
const hi = try p.binary(0);
|
||||
p.no_struct = saved;
|
||||
return .{ .span = p.span(start), .kind = .{ .@"for" = .{ .name = name, .lo = lo, .hi = hi, .body = try p.block() } } };
|
||||
const body = try p.block();
|
||||
return .{ .span = p.span(start), .kind = .{ .@"for" = .{ .name = name, .lo = lo, .hi = hi, .body = body } } };
|
||||
}
|
||||
if (isWord(t, "loop")) {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .loop = try p.block() } };
|
||||
const body = try p.block();
|
||||
return .{ .span = p.span(start), .kind = .{ .loop = body } };
|
||||
}
|
||||
if (isWord(t, "scope")) {
|
||||
_ = p.next();
|
||||
const name = try p.ident("scope name");
|
||||
return .{ .span = p.span(start), .kind = .{ .scope = .{ .name = name, .body = try p.block() } } };
|
||||
const body = try p.block();
|
||||
return .{ .span = p.span(start), .kind = .{ .scope = .{ .name = name, .body = body } } };
|
||||
}
|
||||
if (isWord(t, "shield")) {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .shield = try p.block() } };
|
||||
const body = try p.block();
|
||||
return .{ .span = p.span(start), .kind = .{ .shield = body } };
|
||||
}
|
||||
if (isWord(t, "lock")) {
|
||||
_ = p.next();
|
||||
@@ -469,9 +480,13 @@ const Parser = struct {
|
||||
const name = try p.ident("a name for the locked data");
|
||||
const body = try p.block();
|
||||
_ = try p.expectWord("cancelled");
|
||||
return .{ .span = p.span(start), .kind = .{ .lock = .{ .place = place, .name = name, .body = body, .cancelled = try p.block() } } };
|
||||
const cancelled = try p.block();
|
||||
return .{ .span = p.span(start), .kind = .{ .lock = .{ .place = place, .name = name, .body = body, .cancelled = cancelled } } };
|
||||
}
|
||||
if (isWord(t, "select")) {
|
||||
const selection = try p.selectStmt();
|
||||
return .{ .span = p.span(start), .kind = .{ .select = selection } };
|
||||
}
|
||||
if (isWord(t, "select")) return .{ .span = p.span(start), .kind = .{ .select = try p.selectStmt() } };
|
||||
|
||||
// A leading bare block construct has priority over an outer expression
|
||||
// continuation in statement/tail position (spec 15 §4). Recursive
|
||||
@@ -701,15 +716,18 @@ const Parser = struct {
|
||||
const t = p.peek();
|
||||
if (t.tag == .minus) {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .neg = try p.box(ast.Expr, try p.unary()) } };
|
||||
const operand = try p.box(ast.Expr, try p.unary());
|
||||
return .{ .span = p.span(start), .kind = .{ .neg = operand } };
|
||||
}
|
||||
if (isWord(t, "not")) {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .not = try p.box(ast.Expr, try p.unary()) } };
|
||||
const operand = try p.box(ast.Expr, try p.unary());
|
||||
return .{ .span = p.span(start), .kind = .{ .not = operand } };
|
||||
}
|
||||
if (t.tag == .star) {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .deref = try p.box(ast.Expr, try p.unary()) } };
|
||||
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();
|
||||
@@ -723,7 +741,8 @@ const Parser = struct {
|
||||
}
|
||||
if (isWord(t, "move")) {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .move = try p.ident("the name of the value to move") } };
|
||||
const name = try p.ident("the name of the value to move");
|
||||
return .{ .span = p.span(start), .kind = .{ .move = name } };
|
||||
}
|
||||
return p.postfix();
|
||||
}
|
||||
@@ -906,7 +925,10 @@ const Parser = struct {
|
||||
_ = p.next();
|
||||
return .{ .span = p.span(start), .kind = .{ .boolean = isWord(t, "true") } };
|
||||
}
|
||||
if (isWord(t, "if")) return .{ .span = p.span(start), .kind = .{ .if_expr = try p.box(ast.If, try p.ifExpr()) } };
|
||||
if (isWord(t, "if")) {
|
||||
const conditional = try p.box(ast.If, try p.ifExpr());
|
||||
return .{ .span = p.span(start), .kind = .{ .if_expr = conditional } };
|
||||
}
|
||||
if (isWord(t, "match")) {
|
||||
_ = p.next();
|
||||
const scrut = try p.condition();
|
||||
@@ -1053,3 +1075,121 @@ test "tail, explicit discard and parentheses choose expression context syntactic
|
||||
try std.testing.expect((try parse(a, source)) == .err);
|
||||
}
|
||||
}
|
||||
|
||||
fn testPos(source: []const u8, offset: usize) lex.Pos {
|
||||
var pos: lex.Pos = .{ .line = 1, .col = 1 };
|
||||
for (source[0..offset]) |c| {
|
||||
if (c == '\n') {
|
||||
pos.line += 1;
|
||||
pos.col = 1;
|
||||
} else pos.col += 1;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
fn expectSourceRange(source: []const u8, span_value: ast.Span, text: []const u8) !void {
|
||||
const offset = std.mem.indexOf(u8, source, text).?;
|
||||
try std.testing.expectEqualDeep(testPos(source, offset), span_value.start);
|
||||
try std.testing.expectEqualDeep(testPos(source, offset + text.len), span_value.end);
|
||||
try std.testing.expectEqualStrings("ranges.aji", span_value.file.?);
|
||||
try std.testing.expectEqualStrings("spans", span_value.namespace.?);
|
||||
}
|
||||
|
||||
fn expectContainedRanges(value: anytype, parent: ?ast.Span) anyerror!void {
|
||||
const T = @TypeOf(value);
|
||||
if (T == ast.Span) return;
|
||||
switch (@typeInfo(T)) {
|
||||
.@"struct" => |info| {
|
||||
var enclosing = parent;
|
||||
if (@hasField(T, "span")) {
|
||||
const s = value.span;
|
||||
try std.testing.expect(posBefore(s.start, s.end));
|
||||
if (parent) |p| {
|
||||
try std.testing.expect(!posBefore(s.start, p.start));
|
||||
try std.testing.expect(!posBefore(p.end, s.end));
|
||||
}
|
||||
enclosing = s;
|
||||
}
|
||||
inline for (info.field_names) |name| try expectContainedRanges(@field(value, name), enclosing);
|
||||
},
|
||||
.@"union" => switch (value) {
|
||||
inline else => |payload| try expectContainedRanges(payload, parent),
|
||||
},
|
||||
.optional => if (value) |payload| try expectContainedRanges(payload, parent),
|
||||
.pointer => |info| if (info.child != u8) switch (info.size) {
|
||||
.one => try expectContainedRanges(value.*, parent),
|
||||
.slice => for (value) |item| try expectContainedRanges(item, parent),
|
||||
else => {},
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn posBefore(a: lex.Pos, b: lex.Pos) bool {
|
||||
return a.line < b.line or (a.line == b.line and a.col < b.col);
|
||||
}
|
||||
|
||||
test "block statements cover their complete bodies and cancellation arms" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
for ([_][]const u8{
|
||||
"while true {\n continue;\n}",
|
||||
"for i in 0usize..2usize {\n break;\n}",
|
||||
"loop {\n break;\n}",
|
||||
"scope work {\n trap;\n}",
|
||||
"shield {\n trap;\n}",
|
||||
"lock ref mutex_owner as data {} cancelled {\n trap;\n}",
|
||||
"select { recv(rx) { ok(value) => {} closed => {} } cancelled => {\n trap;\n} }",
|
||||
"if true {} else if false {} else {\n trap;\n}",
|
||||
}) |fragment| {
|
||||
const source = try std.fmt.allocPrint(a, "module spans\nfn f() -> void effects [] {{\n{s}\n// next statement\ntrap;\n}}", .{fragment});
|
||||
const r = try parseWithFile(a, source, "ranges.aji");
|
||||
try std.testing.expect(r == .ok);
|
||||
try expectContainedRanges(r.ok, null);
|
||||
const statement = r.ok.functions[0].body.stmts[0];
|
||||
try expectSourceRange(source, statement.span, fragment);
|
||||
if (statement.kind == .expr) try expectSourceRange(source, statement.kind.expr.span, fragment);
|
||||
if (statement.kind == .select) try expectSourceRange(source, statement.kind.select.span, fragment);
|
||||
}
|
||||
}
|
||||
|
||||
test "prefix and if expressions include their last operand token" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
for ([_][]const u8{
|
||||
"-\n value",
|
||||
"not // comment\n flag",
|
||||
"*\n pointer",
|
||||
"move\n owner",
|
||||
"if true { 1u64 } else if false { 2u64 } else { 3u64 }",
|
||||
"-(*pointer + 2i64)",
|
||||
}) |fragment| {
|
||||
for ([_][]const u8{ "\n", "\r\n" }) |newline| {
|
||||
const body = try std.mem.replaceOwned(u8, a, fragment, "\n", newline);
|
||||
const source = try std.fmt.allocPrint(a, "module spans{s}// UTF-8: λ{s}fn f() -> void effects [] {{{s}let result = {s}; // after{s}}}", .{ newline, newline, newline, body, newline });
|
||||
const r = try parseWithFile(a, source, "ranges.aji");
|
||||
try std.testing.expect(r == .ok);
|
||||
try expectContainedRanges(r.ok, null);
|
||||
try expectSourceRange(source, r.ok.functions[0].body.stmts[0].kind.let.e.span, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "each half of a nested type closer contributes to its type range" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const source = "module spans\nfn f(value: mut<file<write>>, other: Box<ref<own<u64>>>) -> void effects [] {}";
|
||||
const r = try parseWithFile(a, source, "ranges.aji");
|
||||
try std.testing.expect(r == .ok);
|
||||
try expectContainedRanges(r.ok, null);
|
||||
const params = r.ok.functions[0].params;
|
||||
try expectSourceRange(source, params[0].ty.span, "mut<file<write>>");
|
||||
try expectSourceRange(source, params[0].ty.kind.wrapped.inner.span, "file<write>");
|
||||
const box_type = params[1].ty;
|
||||
try expectSourceRange(source, box_type.span, "Box<ref<own<u64>>>");
|
||||
try expectSourceRange(source, box_type.kind.applied.args[0].span, "ref<own<u64>>");
|
||||
try expectSourceRange(source, box_type.kind.applied.args[0].kind.wrapped.inner.span, "own<u64>");
|
||||
}
|
||||
@@ -7,6 +7,7 @@ in place; --write tests use temporary files. Runs use fuel, without timeouts.
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
@@ -23,6 +24,20 @@ def cli(*args, code=0):
|
||||
return result.stdout
|
||||
|
||||
|
||||
def annotation_ranges(path, canonical):
|
||||
annotated = cli("lower", "--annotate", path)
|
||||
ordinary = []
|
||||
for line in annotated.splitlines(keepends=True):
|
||||
match = re.fullmatch(rb'\s*; (?:(.+):)?(\d+):(\d+)-(\d+):(\d+)\n', line)
|
||||
if match:
|
||||
start = tuple(map(int, match.group(2, 3)))
|
||||
end = tuple(map(int, match.group(4, 5)))
|
||||
assert min(*start, *end) >= 1 and start < end, (path, line)
|
||||
else:
|
||||
ordinary.append(line)
|
||||
assert b''.join(ordinary) == canonical, path
|
||||
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="aji-format-") as directory:
|
||||
root = Path(directory)
|
||||
fixture = REPO / "tests/format/layout.aji"
|
||||
@@ -182,6 +197,7 @@ with tempfile.TemporaryDirectory(prefix="aji-format-") as directory:
|
||||
checks += 1
|
||||
if lower.returncode == 0:
|
||||
assert cli("lower", formatted) == lower.stdout, path
|
||||
annotation_ranges(path, lower.stdout)
|
||||
lowered += 1
|
||||
else:
|
||||
assert lower.returncode in (1, 3), (path, lower.returncode, lower.stderr)
|
||||
|
||||
Reference in new issue
Block a user