// Parser tests — pin parse-level shapes the example corpus can't isolate // (the corpus runs the full `sx run` pipeline, never the parser alone). const std = @import("std"); const ast = @import("ast.zig"); const Node = ast.Node; const Parser = @import("parser.zig").Parser; // Lock: the comptime type-metaprogramming surface in `library/modules/std/meta.sx` // must PARSE — the data types as struct/enum decls, and the four comptime builtins // (`declare` / `define` / `type_info` / `field_type`) as bodyless `#builtin` // consts. Mirrors the exact spellings in meta.sx. test "parser: comptime type-metaprogramming surface parses" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const src = \\EnumVariant :: struct { \\ name: string; \\ payload: Type; \\} \\EnumInfo :: struct { \\ name: string; \\ variants: []EnumVariant; \\} \\TypeInfo :: enum { \\ `enum: EnumInfo; \\} \\declare :: () -> Type #builtin; \\define :: (handle: Type, info: TypeInfo) -> Type #builtin; \\type_info :: ($T: Type) -> TypeInfo #builtin; \\field_type :: ($T: Type, idx: i64) -> Type #builtin; \\ ; var parser = Parser.init(alloc, src); const root = try parser.parse(); try std.testing.expect(root.data == .root); const decls = root.data.root.decls; try std.testing.expectEqual(@as(usize, 7), decls.len); const Found = struct { // A top-level `Name :: struct/enum {…}` parses to a `.struct_decl` / // `.enum_decl` node DIRECTLY (not wrapped in a const_decl); only the // `#builtin` forms are `.fn_decl`. Match on the shared `declName`. fn byName(ds: []const *Node, name: []const u8) ?*const Node { for (ds) |d| { if (d.data.declName()) |n| { if (std.mem.eql(u8, n, name)) return d; } } return null; } }; // Data types: struct / struct / enum, parsed as their decl nodes directly. const ev = Found.byName(decls, "EnumVariant") orelse return error.MissingDecl; try std.testing.expect(ev.data == .struct_decl); const ei = Found.byName(decls, "EnumInfo") orelse return error.MissingDecl; try std.testing.expect(ei.data == .struct_decl); const ti = Found.byName(decls, "TypeInfo") orelse return error.MissingDecl; try std.testing.expect(ti.data == .enum_decl); // The single `` `enum `` variant of TypeInfo. The backtick raw escape // stores the bare keyword as the variant name. const ed = ti.data.enum_decl; try std.testing.expectEqual(@as(usize, 1), ed.variant_names.len); try std.testing.expectEqualStrings("enum", ed.variant_names[0]); // Builtins: the `(params) -> Ret #builtin;` form parses as a `.fn_decl` // (the `->` triggers the function-def path) whose body is a `#builtin` // marker — same shape as the existing reflection builtins in core.sx. for ([_][]const u8{ "declare", "define", "type_info", "field_type" }) |bn| { const d = Found.byName(decls, bn) orelse return error.MissingDecl; try std.testing.expect(d.data == .fn_decl); try std.testing.expect(d.data.fn_decl.body.data == .builtin_expr); try std.testing.expect(d.data.fn_decl.return_type != null); } } // Lock: the `compiler`-library binding surface PARSES — `name :: #library "x";` // (already supported) plus the postfix `abi(.compiler)` annotation, marking a // compiler-domain / compiler-API function — no `extern`, no fake `#library`. The // AST must carry `abi == .compiler`, `extern_export == .none`, `extern_lib == // null`, and a synthesized empty-block (bodiless) body. test "parser: abi(.compiler) binding parses on a bodiless fn decl" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const src = \\text_of :: (id: StringId) -> string abi(.compiler); \\intern :: (s: string) -> StringId abi(.compiler); \\ ; var parser = Parser.init(alloc, src); const root = try parser.parse(); try std.testing.expect(root.data == .root); const decls = root.data.root.decls; try std.testing.expectEqual(@as(usize, 2), decls.len); // The two `abi(.compiler)` fns: `.fn_decl` with the compiler-domain ABI set, // NO extern linkage, NO bound library. for ([_][]const u8{ "text_of", "intern" }) |bn| { var found: ?*const Node = null; for (decls) |d| { if (d.data.declName()) |n| { if (std.mem.eql(u8, n, bn)) found = d; } } const d = found orelse return error.MissingDecl; try std.testing.expect(d.data == .fn_decl); const fd = d.data.fn_decl; try std.testing.expectEqual(ast.ABI.compiler, fd.abi); try std.testing.expectEqual(ast.ExternExportModifier.none, fd.extern_export); try std.testing.expect(fd.extern_lib == null); // Bodyless compiler-domain decl: synthesized empty block, no `#builtin`/`#compiler`. try std.testing.expect(fd.body.data == .block); } } // Lock: a bare `extern` (no abi annotation) leaves `abi == .default` — the // unannotated case is unchanged by the new `abi(...)` slot. test "parser: bare extern leaves abi == .default" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const src = \\puts :: (s: *u8) -> i32 extern; \\ ; var parser = Parser.init(alloc, src); const root = try parser.parse(); const decls = root.data.root.decls; try std.testing.expectEqual(@as(usize, 1), decls.len); try std.testing.expect(decls[0].data == .fn_decl); const fd = decls[0].data.fn_decl; try std.testing.expectEqual(ast.ExternExportModifier.extern_, fd.extern_export); try std.testing.expectEqual(ast.ABI.default, fd.abi); } // Lock: `abi(.c)` parses standalone (no extern/export) in the postfix slot — the // migrated spelling of the old `callconv(.c)` on an ordinary function pointer / // fn decl. And `abi(.naked)` parses (naked-asm ABI). test "parser: abi(.c) and abi(.naked) parse standalone" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const src = \\cb :: () -> i64 abi(.c) { 0; } \\nk :: () -> i64 abi(.naked) { 0; } \\ ; var parser = Parser.init(alloc, src); const root = try parser.parse(); const decls = root.data.root.decls; try std.testing.expectEqual(@as(usize, 2), decls.len); try std.testing.expect(decls[0].data == .fn_decl); try std.testing.expectEqual(ast.ABI.c, decls[0].data.fn_decl.abi); try std.testing.expectEqual(ast.ExternExportModifier.none, decls[0].data.fn_decl.extern_export); try std.testing.expect(decls[1].data == .fn_decl); try std.testing.expectEqual(ast.ABI.naked, decls[1].data.fn_decl.abi); } // Lock: the postfix `abi(...)` slot PARSES on a STRUCT decl — `Name :: struct // abi(.compiler) extern { … }`. The AST struct_decl carries the abi + the // library handle in `extern_lib`, with the field list intact. Parse-only — the // struct-weld semantics were stripped (compiler-API types are VM-native now); this // just locks that the annotation slot still parses without perturbing fields. test "parser: abi(...) extern annotation parses on a struct decl" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const src = \\compiler :: #library "compiler"; \\Field :: struct abi(.compiler) extern compiler { name: StringId; ty: Type; } \\ ; var parser = Parser.init(alloc, src); const root = try parser.parse(); const decls = root.data.root.decls; try std.testing.expectEqual(@as(usize, 2), decls.len); try std.testing.expect(decls[1].data == .struct_decl); const sd = decls[1].data.struct_decl; try std.testing.expectEqual(ast.ABI.compiler, sd.abi); try std.testing.expect(sd.extern_lib != null); try std.testing.expectEqualStrings("compiler", sd.extern_lib.?); // Field list survives the binding annotation. try std.testing.expectEqual(@as(usize, 2), sd.field_names.len); try std.testing.expectEqualStrings("name", sd.field_names[0]); try std.testing.expectEqualStrings("ty", sd.field_names[1]); } // Lock: an ordinary struct (no binding) leaves `abi == .default` / `extern_lib == // null` — the new annotation slot doesn't perturb the common case. test "parser: plain struct leaves abi == .default, extern_lib == null" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const src = \\Point :: struct { x: i64; y: i64; } \\ ; var parser = Parser.init(alloc, src); const root = try parser.parse(); const decls = root.data.root.decls; try std.testing.expectEqual(@as(usize, 1), decls.len); try std.testing.expect(decls[0].data == .struct_decl); const sd = decls[0].data.struct_decl; try std.testing.expectEqual(ast.ABI.default, sd.abi); try std.testing.expect(sd.extern_lib == null); } // ── New tuple syntax (additive; the inline `(a, b)` forms stay valid) ── // `Tuple(A, B)` magic type id → positional tuple_type_expr, mirroring `(A, B)`. // Exercised in a genuine type position (a fn return type), since a `::` RHS is // an EXPRESSION position where `Tuple(...)` is an ordinary call. test "parser: Tuple(A, B) type parses to positional tuple_type_expr" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> Tuple(i64, i32) { 0 }"); const root = try parser.parse(); const rt = root.data.root.decls[0].data.fn_decl.return_type.?; try std.testing.expect(rt.data == .tuple_type_expr); const t = rt.data.tuple_type_expr; try std.testing.expectEqual(@as(usize, 2), t.field_types.len); try std.testing.expect(t.field_names == null); } // `Tuple(x: A, y: B)` keeps `:` and stores field names. test "parser: named Tuple(x: A, y: B) stores field names" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> Tuple(x: i64, y: i32) { 0 }"); const root = try parser.parse(); const t = root.data.root.decls[0].data.fn_decl.return_type.?.data.tuple_type_expr; try std.testing.expectEqual(@as(usize, 2), t.field_types.len); try std.testing.expect(t.field_names != null); try std.testing.expectEqualStrings("x", t.field_names.?[0]); try std.testing.expectEqualStrings("y", t.field_names.?[1]); } // 1-tuple `Tuple(T)` and empty `Tuple()`. A `Tuple(T)` stays a 1-tuple — unlike // the inline `(T)` which is a grouping; my block never unwraps. test "parser: Tuple(T) is a 1-tuple, Tuple() is empty" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var p1 = Parser.init(arena.allocator(), "f :: () -> Tuple(i64) { 0 }"); const r1 = try p1.parse(); const t1 = r1.data.root.decls[0].data.fn_decl.return_type.?.data.tuple_type_expr; try std.testing.expectEqual(@as(usize, 1), t1.field_types.len); var p2 = Parser.init(arena.allocator(), "f :: () -> Tuple() { 0 }"); const r2 = try p2.parse(); const t2 = r2.data.root.decls[0].data.fn_decl.return_type.?.data.tuple_type_expr; try std.testing.expectEqual(@as(usize, 0), t2.field_types.len); } // `Tuple(..Ts)` reuses the spread/pack machinery (spread_expr field). Checked // in a PARAM type position (the inline `(..Ts)` form parses there too — a pack // tuple in bare RETURN position is a separate pre-existing parser limitation // that affects `(..Ts)` and `Tuple(..Ts)` identically). test "parser: Tuple(..Ts) pack field is a spread_expr" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: (t: Tuple(..Ts)) { }"); const root = try parser.parse(); const t = root.data.root.decls[0].data.fn_decl.params[0].type_expr.data.tuple_type_expr; try std.testing.expectEqual(@as(usize, 1), t.field_types.len); try std.testing.expect(t.field_types[0].data == .spread_expr); } // A trailing `->` after `Tuple(...)` is a hard error (no return type). test "parser: Tuple(A, B) -> C is rejected" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> Tuple(i64, i64) -> i64 { 0 }"); try std.testing.expectError(error.ParseError, parser.parse()); } // A bare `Tuple` not followed by `(` stays an ordinary identifier. test "parser: bare Tuple (no paren) is an identifier, not a tuple type" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> i64 { Tuple := 1; Tuple }"); const root = try parser.parse(); // Parses without error; the body references `Tuple` as a value name. try std.testing.expect(root.data.root.decls[0].data == .fn_decl); } // `.(a, b)` value literal → tuple_literal, same node as inline `(a, b)`. test "parser: .(a, b) parses to tuple_literal" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () { x := .(1, 2); }"); const root = try parser.parse(); const body = root.data.root.decls[0].data.fn_decl.body; const stmt = body.data.block.stmts[0]; const val = stmt.data.var_decl.value.?; try std.testing.expect(val.data == .tuple_literal); try std.testing.expectEqual(@as(usize, 2), val.data.tuple_literal.elements.len); try std.testing.expect(val.data.tuple_literal.elements[0].name == null); } // Named `.(x = a, y = b)` uses `=` and binds names onto TupleElement. test "parser: named .(x = a, y = b) uses = and stores names" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () { x := .(x = 1, y = 2); }"); const root = try parser.parse(); const val = root.data.root.decls[0].data.fn_decl.body.data.block.stmts[0].data.var_decl.value.?; try std.testing.expect(val.data == .tuple_literal); const els = val.data.tuple_literal.elements; try std.testing.expectEqual(@as(usize, 2), els.len); try std.testing.expectEqualStrings("x", els[0].name.?); try std.testing.expectEqualStrings("y", els[1].name.?); } // 1-tuple `.(x)` and empty `.()`. test "parser: .(x) is a 1-tuple, .() is empty" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var p1 = Parser.init(arena.allocator(), "f :: () { x := .(7); }"); const r1 = try p1.parse(); const v1 = r1.data.root.decls[0].data.fn_decl.body.data.block.stmts[0].data.var_decl.value.?; try std.testing.expect(v1.data == .tuple_literal); try std.testing.expectEqual(@as(usize, 1), v1.data.tuple_literal.elements.len); var p2 = Parser.init(arena.allocator(), "f :: () { x := .(); }"); const r2 = try p2.parse(); const v2 = r2.data.root.decls[0].data.fn_decl.body.data.block.stmts[0].data.var_decl.value.?; try std.testing.expect(v2.data == .tuple_literal); try std.testing.expectEqual(@as(usize, 0), v2.data.tuple_literal.elements.len); } // `-> T !` folds to the same `(T, !)` representation: tuple_type_expr whose // last field is an error_type_expr. test "parser: -> T ! folds to (T, !) tuple_type_expr" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> i64 ! { 0 }"); const root = try parser.parse(); const rt = root.data.root.decls[0].data.fn_decl.return_type.?; try std.testing.expect(rt.data == .tuple_type_expr); const fields = rt.data.tuple_type_expr.field_types; try std.testing.expectEqual(@as(usize, 2), fields.len); try std.testing.expect(fields[0].data == .type_expr); try std.testing.expect(fields[1].data == .error_type_expr); try std.testing.expect(fields[1].data.error_type_expr.name == null); } // `-> Tuple(T1, T2) !` flattens to (T1, T2, !). test "parser: -> Tuple(A, B) ! flattens to (A, B, !)" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> Tuple(i64, i32) !ParseErr { 0 }"); const root = try parser.parse(); const rt = root.data.root.decls[0].data.fn_decl.return_type.?; try std.testing.expect(rt.data == .tuple_type_expr); const fields = rt.data.tuple_type_expr.field_types; try std.testing.expectEqual(@as(usize, 3), fields.len); try std.testing.expect(fields[0].data == .type_expr); try std.testing.expect(fields[1].data == .type_expr); try std.testing.expect(fields[2].data == .error_type_expr); try std.testing.expectEqualStrings("ParseErr", fields[2].data.error_type_expr.name.?); } // `-> !` (void + error) stays a bare error_type_expr — the trailing-`!` fold // must NOT double-wrap it. test "parser: -> ! stays a bare error_type_expr" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> ! { }"); const root = try parser.parse(); const rt = root.data.root.decls[0].data.fn_decl.return_type.?; try std.testing.expect(rt.data == .error_type_expr); } // Old inline `-> (T, !)` failable form is gone — rejected with the new-form hint. test "parser: old inline -> (T, !) is rejected" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> (i64, !) { 0 }"); try std.testing.expectError(error.ParseError, parser.parse()); } // Bare-paren tuple TYPE `(A, B)` is gone — rejected (tuple types use `Tuple(...)`). test "parser: bare-paren tuple type (A, B) is rejected" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: (t: (i64, i32)) { }"); try std.testing.expectError(error.ParseError, parser.parse()); } // Bare-paren tuple VALUE `(a, b)` is gone — rejected (tuple values use `.(...)`). test "parser: bare-paren tuple value (a, b) is rejected" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () { x := (1, 2); }"); try std.testing.expectError(error.ParseError, parser.parse()); } // Bare-paren grouping `(a + b)` still works — single inner, no top-level comma. test "parser: bare-paren grouping (a + b) still parses" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var parser = Parser.init(arena.allocator(), "f :: () -> i64 { (1 + 2) }"); const root = try parser.parse(); try std.testing.expect(root.data.root.decls[0].data == .fn_decl); }