✓
Passing This code compiles and runs correctly.
Code
// Test: Parser as runtime library
// Parses Koru source code and inspects the AST at runtime
// This proves the parser is fully usable as a library!
~import "$std/parser"
const std = @import("std");
// Sample Koru code to parse
const SAMPLE_CODE =
\\~event greet { name: []const u8 }
\\| greeted {}
\\
\\~proc greet {
\\ return .{ .greeted = .{} };
\\}
;
// Event to inspect AST - using module:Type syntax!
~event inspect_ast { result: std.parser:ParseResult }
~proc inspect_ast {
std.debug.print("Parsed {} items\n", .{result.source_file.items.len});
for (result.source_file.items) |item| {
switch (item) {
.event_decl => |event| {
std.debug.print("Found event: ", .{});
for (event.path.segments) |seg| {
std.debug.print("{s}", .{seg});
}
std.debug.print("\n", .{});
},
.proc_decl => |proc_decl| {
std.debug.print("Found proc: ", .{});
for (proc_decl.path.segments) |seg| {
std.debug.print("{s}", .{seg});
}
std.debug.print("\n", .{});
},
else => {},
}
}
}
// Event to report error - returns done when complete
~event report_error { message: []const u8 }
~proc report_error {
std.debug.print("Parse error: {s}\n", .{message});
}
// Modern Koru: proc = flow composition
~std.parser:parse.source(source: SAMPLE_CODE, file_name: "sample.kz", allocator: std.heap.page_allocator)
| parsed p |> inspect_ast(result: p.ast)
| parse_error err |> report_error(message: err.message)
Test Configuration
MUST_RUN