Yuku
Parser
Yuku’s parser turns JavaScript, TypeScript, and JSX into an AST. It recovers from errors, so a parse with diagnostics still returns a tree of everything it could read.
JavaScript
npm install yuku-parser
import { parse } from "yuku-parser";
const { program, comments, diagnostics } = parse("const x = 1 + 2;");
An ESTree / TypeScript-ESTree AST, matching Oxc. See yuku-parser on npm for the full API.
Zig
const std = @import("std");
const parser = @import("parser");
pub fn main() !void {
var tree = try parser.parse(std.heap.smp_allocator, "const x: number = 1;", .{ .lang = .ts });
defer tree.deinit();
for (tree.diagnostics.items) |d| {
std.debug.print("{s}\n", .{d.message});
}
}
parse returns a Tree holding the AST, its diagnostics, and the source, and fails only with error.OutOfMemory. Everything lives in one arena, so tree.deinit() frees it all at once. The tree borrows source, which must outlive it. AST shows how to read it.
Options
| Field | Default | Description |
|---|---|---|
source_type | .module | .script, .module (strict, with import and export), or .commonjs (top-level return) |
lang | .js | .js, .ts, .jsx, .tsx, or .dts |
preserve_parens | true | Keep parenthesized_expression nodes, or only their inner expression |
comments | .flat | .none, .flat, .attached, or .both, see Comments |
tokens | false | Keep every token, see Tokens |
.fromPath infers source_type and lang from a file name, as in .lang = .fromPath("app.tsx").
Diagnostics
The parser reports a syntax error and parses on past it.
var tree = try parser.parse(allocator, "const a = 1;\nlet x = ;\nconst y = 2;", .{});
// one diagnostic, "Unexpected token ';'", and a tree holding `a` and `y`
Every Yuku tool reports diagnostics in one shape, ast.Diagnostic: a severity of .error, .warning, .hint, or .info, a message, a byte span, labels pointing at related code, and optional help.
for (tree.diagnostics.items) |d| {
std.debug.print("[{s}] {s} at {d}..{d}\n", .{
d.severity.toString(), d.message, d.span.start, d.span.end,
});
if (d.help) |help| std.debug.print(" help: {s}\n", .{help});
}
tree.hasErrors() tells whether any error occurred, and tree.hasDiagnostics() whether anything was reported. Semantic analysis adds the early errors that need scopes, such as duplicate let declarations and break outside a loop.