aboutsummaryrefslogtreecommitdiff
path: root/src/z/js/parser/root.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-07-30 09:19:48 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-07-30 09:19:48 +0200
commit006674d7e08146a9bd980e2138ce9444d2e40e6f (patch)
treead0cc364ac5b6f4e97906f8ecb1a5e1d6f189c34 /src/z/js/parser/root.zig
parent4d064042e8a6ebf874fecbc23f13073f39fa1ebe (diff)
add formatting
Diffstat (limited to 'src/z/js/parser/root.zig')
-rw-r--r--src/z/js/parser/root.zig94
1 files changed, 45 insertions, 49 deletions
diff --git a/src/z/js/parser/root.zig b/src/z/js/parser/root.zig
index 6d41c7b..af0a67d 100644
--- a/src/z/js/parser/root.zig
+++ b/src/z/js/parser/root.zig
@@ -2,90 +2,86 @@ const std = @import("std");
pub const lexical_grammar = @import("lexical_grammar.zig");
pub const ast = @import("ast.zig");
-pub const syntactic_grammar = @import("syntactic_grammar.zig");
+
+
pub const Result = @import("result.zig").Result;
pub const Parser = @import("parser.zig").Parser;
test "simple literal" {
- const source = "42;";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
- try std.testing.expect(result.body[0].data == .expr);
+ comptime {
+ _ = try ast.Program.parse("42;");
+ }
}
test "variable declaration" {
- const source = "var x = 1;";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
- try std.testing.expect(result.body[0].data == .variable);
+ comptime {
+ _ = try ast.Program.parse("var x = 1;");
+ }
}
test "function declaration" {
- const source = "function foo() { return 1; }";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
- try std.testing.expect(result.body[0].data == .function);
+ comptime {
+ _ = try ast.Program.parse("function foo() { return 1; }");
+ }
}
test "if statement" {
- const source = "if (x > 0) { x = 1; } else { x = 2; }";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
- try std.testing.expect(result.body[0].data == .@"if");
+ comptime {
+ _ = try ast.Program.parse("if (x > 0) { x = 1; } else { x = 2; }");
+ }
}
test "binary expression" {
- const source = "a + b * c;";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+ comptime {
+ _ = try ast.Program.parse("a + b * c;");
+ }
}
test "arrow function" {
- const source = "const f = (x) => x + 1;";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
-}
-
-test "object literal" {
- const source = "var obj = { a: 1, b: 2 };";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+ comptime {
+ _ = try ast.Program.parse("const f = (x) => x + 1;");
+ }
}
test "class declaration" {
- const source = "class Foo { constructor() { } }";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+ comptime {
+ _ = try ast.Program.parse("class Foo { constructor() { } }");
+ }
}
test "for loop" {
- const source = "for (var i = 0; i < 10; i++) { break; }";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+ comptime {
+ _ = try ast.Program.parse("for (var i = 0; i < 10; i++) { break; }");
+ }
}
test "try catch" {
- const source = "try { x; } catch (e) { y; }";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+ comptime {
+ _ = try ast.Program.parse("try { x; } catch (e) { y; }");
+ }
}
test "multiple statements" {
- const source = "var a = 1; var b = 2; var c = a + b;";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 3);
+ comptime {
+ _ = try ast.Program.parse("var a = 1; var b = 2; var c = a + b;");
+ }
}
-test "array literal with spread" {
- const source = "var arr = [1, ...rest, 3];";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+test "comprehensive fixture" {
+ comptime {
+ _ = try ast.Program.parse(@embedFile("test_fixture.js"));
+ }
}
-test "template literal" {
- const source = "const s = `hello ${name} world`;";
- const result = try syntactic_grammar.parse(source);
- try std.testing.expect(result.body.len == 1);
+test "format fixture" {
+ comptime {
+ var buf: [65536]u8 = undefined;
+ const ast1 = try ast.Program.parse(@embedFile("test_fixture.js"));
+ var f = ast.Formatter{ .buf = buf[0..], .pos = 0, .indent_level = 0, .options = .{} };
+ ast1.fmt(&f);
+ const formatted = buf[0..f.pos];
+ try std.testing.expect(formatted.len > 0);
+ }
}
test {