// 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(.pure)` parses (naked-asm ABI).
test "parser: abi(.c) and abi(.pure) 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(.pure) { 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.pure, decls[1].data.fn_decl.abi);
}

// Lock: the postfix `abi(...)` slot PARSES on a STRUCT decl — `Name :: struct
// abi(.compiler) extern <lib> { … }`. 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 <lib> 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);
}
