From 006674d7e08146a9bd980e2138ce9444d2e40e6f Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 30 Jul 2026 09:19:48 +0200 Subject: add formatting --- src/z/js/parser/ast.zig | 2702 ++++++++++++++++++++++++++++++++- src/z/js/parser/lexical_grammar.zig | 214 +-- src/z/js/parser/parser.zig | 53 +- src/z/js/parser/root.zig | 94 +- src/z/js/parser/syntactic_grammar.zig | 1907 ----------------------- src/z/js/parser/test_fixture.js | 86 ++ src/z/root.zig | 1 + 7 files changed, 2941 insertions(+), 2116 deletions(-) delete mode 100644 src/z/js/parser/syntactic_grammar.zig create mode 100644 src/z/js/parser/test_fixture.js (limited to 'src') diff --git a/src/z/js/parser/ast.zig b/src/z/js/parser/ast.zig index 1fb8797..3dc29a3 100644 --- a/src/z/js/parser/ast.zig +++ b/src/z/js/parser/ast.zig @@ -1,8 +1,1919 @@ +const std = @import("std"); + pub const Loc = struct { start: usize, end: usize, }; +const lex = @import("lexical_grammar.zig"); +const Token = lex.Token; +const TokenType = lex.TokenType; + +pub const FormatOptions = struct { + indent_size: usize = 4, +}; + +pub const Formatter = struct { + buf: []u8, + pos: usize, + indent_level: usize, + options: FormatOptions, + + pub fn write(self: *@This(), s: []const u8) void { + @memcpy(self.buf[self.pos..][0..s.len], s); + self.pos += s.len; + } + + pub fn writeChar(self: *@This(), c: u8) void { + self.buf[self.pos] = c; + self.pos += 1; + } + + pub fn writeIndent(self: *@This()) void { + const spaces = self.indent_level * self.options.indent_size; + var i: usize = 0; + while (i < spaces) : (i += 1) { + self.writeChar(' '); + } + } + + pub fn newline(self: *@This()) void { + self.writeChar('\n'); + } +}; + +pub const ParseError = error{ + UnexpectedToken, + ExpectedToken, + ExpectedExpression, + ExpectedStatement, + ExpectedIdentifier, + UnclosedString, + InvalidNumber, + InvalidEscape, + UnterminatedComment, + ExpectedParameterName, + ExpectedPropertyName, + UnterminatedTemplate, + InvalidAssignmentTarget, + TooManyTokens, +}; + +const BP = struct { + const comma = 1; + const assign = 2; + const yield = 3; + const conditional = 4; + const coalesce = 5; + const log_or = 6; + const log_and = 7; + const bit_or = 8; + const bit_xor = 9; + const bit_and = 10; + const equality = 11; + const relational = 12; + const shift = 13; + const additive = 14; + const multiplicative = 15; + const exponentiation = 16; + const unary = 17; + const update = 18; + const call = 19; + const member = 20; +}; + +pub const NodeArena = struct { + exprs: [8192]Expr = undefined, + expr_count: usize = 0, + stmts: [8192]Stmt = undefined, + stmt_count: usize = 0, + decls: [1024]Decl = undefined, + decl_count: usize = 0, + pats: [1024]Pat = undefined, + pat_count: usize = 0, + lits: [1024]Lit = undefined, + lit_count: usize = 0, + string_buf: [65536]u8 = undefined, + string_pos: usize = 0, + + fn exprPtr(self: *NodeArena, data: ExprType, loc: Loc) *const Expr { + const idx = self.expr_count; + self.exprs[idx] = .{ .loc = loc, .data = data }; + self.expr_count += 1; + return &self.exprs[idx]; + } + + fn stmtPtr(self: *NodeArena, data: StmtType, loc: Loc) *const Stmt { + const idx = self.stmt_count; + self.stmts[idx] = .{ .loc = loc, .data = data }; + self.stmt_count += 1; + return &self.stmts[idx]; + } + + fn declPtr(self: *NodeArena, data: DeclType, loc: Loc) *const Decl { + const idx = self.decl_count; + self.decls[idx] = .{ .loc = loc, .data = data }; + self.decl_count += 1; + return &self.decls[idx]; + } + + fn patPtr(self: *NodeArena, data: PatType, loc: Loc) *const Pat { + const idx = self.pat_count; + self.pats[idx] = .{ .loc = loc, .data = data }; + self.pat_count += 1; + return &self.pats[idx]; + } + + fn litPtr(self: *NodeArena, data: LitType, loc: Loc) *const Lit { + const idx = self.lit_count; + self.lits[idx] = .{ .loc = loc, .data = data }; + self.lit_count += 1; + return &self.lits[idx]; + } +}; + +pub const Parser = struct { + tokens: []const Token, + pos: usize, + source: []const u8, + arena: *NodeArena, + + pub fn init(tokens: []const Token, source: []const u8, arena: *NodeArena) @This() { + return .{ .tokens = tokens, .pos = 0, .source = source, .arena = arena }; + } + + fn tok(self: *@This()) Token { + return self.tokens[self.pos]; + } + + fn peek(self: *@This()) TokenType { + return self.tokens[self.pos].kind; + } + + fn peekAt(self: *@This(), offset: usize) TokenType { + const idx = self.pos + offset; + if (idx >= self.tokens.len) return .eof; + return self.tokens[idx].kind; + } + + fn advance(self: *@This()) void { + self.pos += 1; + } + + fn advanceTok(self: *@This()) Token { + const t = self.tokens[self.pos]; + self.pos += 1; + return t; + } + + fn expect(self: *@This(), kind: TokenType) ParseError!void { + if (self.peek() != kind) return ParseError.ExpectedToken; + self.advance(); + } + + fn expectTok(self: *@This(), kind: TokenType) ParseError!Token { + if (self.peek() != kind) return ParseError.ExpectedToken; + return self.advanceTok(); + } + + fn match(self: *@This(), kind: TokenType) bool { + if (self.peek() == kind) { + _ = self.advanceTok(); + return true; + } + return false; + } + + fn matchKeyword(self: *@This(), kw: TokenType) bool { + if (self.peek() == kw) { + _ = self.advanceTok(); + return true; + } + return false; + } + + fn isLineTerminatorBeforeNext(self: *@This()) bool { + if (self.pos == 0) return false; + const prev_token = self.tokens[self.pos - 1]; + const next_token = self.tokens[self.pos]; + var i = prev_token.loc.end; + while (i < next_token.loc.start) : (i += 1) { + if (i >= self.source.len) break; + const c = self.source[i]; + switch (c) { + '\n', '\r' => return true, + else => {}, + } + } + return false; + } + + fn as_identifier(self: *@This(), t: Token) []const u8 { + return self.source[t.loc.start..t.loc.end]; + } + + fn tokenSlice(self: *@This(), t: Token) []const u8 { + return self.source[t.loc.start..t.loc.end]; + } + + pub fn parseProgram(self: *@This()) ParseError!Program { + var stmts: [2048]Stmt = undefined; + var count: usize = 0; + + while (self.peek() != .eof) { + const item = try self.parseStatementListItem(); + stmts[count] = item; + count += 1; + if (count >= stmts.len) return ParseError.TooManyTokens; + } + + const body = stmts[0..count]; + const loc: Loc = if (body.len > 0) .{ .start = body[0].loc.start, .end = body[body.len - 1].loc.end } else .{ .start = 0, .end = 0 }; + + return .{ .body = body, .loc = loc }; + } + + fn parseStatementListItem(self: *@This()) ParseError!Stmt { + switch (self.peek()) { + .function => return (try self.parseFunctionDecl(.normal, .stmt)).?, + .async => { + if (self.peekAt(1) == .function) { + return (try self.parseFunctionDecl(.async, .stmt)).?; + } + if (self.peekAt(1) == .@"*" and self.peekAt(2) == .function) { + return (try self.parseFunctionDecl(.async_generator, .stmt)).?; + } + return self.parseStatement(); + }, + .class => return (try self.parseClassDecl(.stmt)).?, + .@"const", .let => return self.parseLexicalDecl(), + .@"var" => return self.parseVarStmt(), + .using => return self.parseUsingStmt(), + .import => return try self.parseImport(), + .@"export" => return try self.parseExport(), + else => return self.parseStatement(), + } + } + + fn parseStatement(self: *@This()) ParseError!Stmt { + switch (self.peek()) { + .lbrace => return self.parseBlock(), + .semicolon => { + const t = self.advanceTok(); + return Stmt{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .data = .{ .empty = {} } }; + }, + .@"if" => return self.parseIf(), + .@"while" => return self.parseWhile(), + .do => return self.parseDoWhile(), + .@"for" => return self.parseFor(), + .@"continue" => return self.parseContinue(), + .@"break" => return self.parseBreak(), + .@"return" => return self.parseReturn(), + .@"switch" => return self.parseSwitch(), + .throw => return self.parseThrow(), + .@"try" => return self.parseTry(), + .debugger => { + const t = self.advanceTok(); + self.semicolon(); + return Stmt{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .data = .{ .debugger = {} } }; + }, + .with => return self.parseWith(), + .function => return (try self.parseFunctionDecl(.normal, .stmt)).?, + .async => { + if (self.peekAt(1) == .function) { + return (try self.parseFunctionDecl(.async, .stmt)).?; + } + if (self.peekAt(1) == .@"*") return self.parseExpressionStatement(); + if (self.peekAt(1).isIdentifier()) { + return self.parseExpressionStatement(); + } + return self.parseExpressionStatement(); + }, + .class => return (try self.parseClassDecl(.stmt)).?, + .@"const", .let => return self.parseLexicalDecl(), + .@"var" => return self.parseVarStmt(), + .import => return try self.parseImport(), + .@"export" => return try self.parseExport(), + else => return self.parseExpressionStatement(), + } + } + + fn parseBlock(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + var stmts: [1024]Stmt = undefined; + var count: usize = 0; + + while (self.peek() != .rbrace and self.peek() != .eof) { + const s = try self.parseStatementListItem(); + stmts[count] = s; + count += 1; + if (count >= stmts.len) return ParseError.TooManyTokens; + } + + const end_tok = try self.expectTok(.rbrace); + const end = end_tok.loc.end; + + const block = Stmt{ + .loc = .{ .start = start, .end = end }, + .data = .{ .block = .{ .loc = .{ .start = start, .end = end }, .body = stmts[0..count] } }, + }; + return block; + } + + fn parseIf(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + try self.expect(.lparen); + const condition = try self.parseExpr(0); + try self.expect(.rparen); + const consequent = try self.statementPointer(); + var alternate: ?*const Stmt = null; + if (self.matchKeyword(.@"else")) { + alternate = try self.statementPointer(); + } + const end = if (alternate) |a| a.loc.end else consequent.loc.end; + return Stmt{ + .loc = .{ .start = start, .end = end }, + .data = .{ .@"if" = .{ .loc = .{ .start = start, .end = end }, .condition = condition, .consequent = consequent, .alternate = alternate } }, + }; + } + + fn parseWhile(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + try self.expect(.lparen); + const condition = try self.parseExpr(0); + try self.expect(.rparen); + const body = try self.statementPointer(); + return Stmt{ + .loc = .{ .start = start, .end = body.loc.end }, + .data = .{ .@"while" = .{ .loc = .{ .start = start, .end = body.loc.end }, .condition = condition, .body = body } }, + }; + } + + fn parseDoWhile(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + const body = try self.statementPointer(); + try self.expect(.@"while"); + try self.expect(.lparen); + const condition = try self.parseExpr(0); + try self.expect(.rparen); + self.semicolon(); + return Stmt{ + .loc = .{ .start = start, .end = condition.loc.end }, + .data = .{ .do_while = .{ .loc = .{ .start = start, .end = condition.loc.end }, .body = body, .condition = condition } }, + }; + } + + fn parseFor(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + try self.expect(.lparen); + + var for_init: ?ForInit = null; + var condition: ?*const Expr = null; + var update: ?*const Expr = null; + + if (self.peek() != .semicolon) { + if (self.peek() == .@"var" or self.peek() == .let or self.peek() == .@"const") { + const decl = try self.parseForDeclaration(); + for_init = .{ .decl = decl }; + } else { + const expr = try self.parseExpr(0); + for_init = .{ .expr = expr }; + } + } + + if (self.matchKeyword(.in)) { + const right = try self.parseExpr(0); + try self.expect(.rparen); + const body = try self.statementPointer(); + return Stmt{ + .loc = .{ .start = start, .end = body.loc.end }, + .data = .{ .for_in = .{ .loc = .{ .start = start, .end = body.loc.end }, .left = for_init.?, .right = right, .body = body } }, + }; + } + + if (self.matchKeyword(.of)) { + const right = try self.parseExpr(0); + try self.expect(.rparen); + const body = try self.statementPointer(); + return Stmt{ + .loc = .{ .start = start, .end = body.loc.end }, + .data = .{ .for_of = .{ .loc = .{ .start = start, .end = body.loc.end }, .await_token = false, .left = for_init.?, .right = right, .body = body } }, + }; + } + + try self.expect(.semicolon); + if (self.peek() != .semicolon and self.peek() != .rparen) { + condition = try self.parseExpr(0); + } + try self.expect(.semicolon); + if (self.peek() != .rparen) { + update = try self.parseExpr(0); + } + try self.expect(.rparen); + const body = try self.statementPointer(); + return Stmt{ + .loc = .{ .start = start, .end = body.loc.end }, + .data = .{ .@"for" = .{ .loc = .{ .start = start, .end = body.loc.end }, .init = for_init, .condition = condition, .update = update, .body = body } }, + }; + } + + fn parseForDeclaration(self: *@This()) ParseError!*const Decl { + const kind_tok = self.advanceTok(); + var decls: [128]VarDeclarator = undefined; + var count: usize = 0; + + while (true) { + const pat = try self.parseBindingPattern(); + var decl_init: ?*const Expr = null; + if (self.match(.@"=")) { + decl_init = try self.parseExpr(0); + } + decls[count] = .{ .loc = pat.loc, .id = pat, .init = decl_init }; + count += 1; + if (!self.match(.comma)) break; + } + + const kind: VarKind = switch (kind_tok.kind) { + .@"var" => .@"var", + .let => .let, + .@"const" => .@"const", + else => return ParseError.UnexpectedToken, + }; + + return self.arena.declPtr(.{ .@"var" = .{ .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, .kind = kind, .decls = decls[0..count] } }, .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }); + } + + fn parseContinue(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + var label: ?[]const u8 = null; + if (self.peek() == .identifier and !self.isLineTerminatorBeforeNext()) { + const t = self.advanceTok(); + label = self.tokenSlice(t); + } + self.semicolon(); + return Stmt{ .loc = .{ .start = start, .end = start + 8 }, .data = .{ .@"continue" = .{ .loc = .{ .start = start, .end = start + 8 }, .label = label } } }; + } + + fn parseBreak(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + var label: ?[]const u8 = null; + if (self.peek() == .identifier and !self.isLineTerminatorBeforeNext()) { + const t = self.advanceTok(); + label = self.tokenSlice(t); + } + self.semicolon(); + return Stmt{ .loc = .{ .start = start, .end = start + 5 }, .data = .{ .@"break" = .{ .loc = .{ .start = start, .end = start + 5 }, .label = label } } }; + } + + fn parseReturn(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + var arg: ?*const Expr = null; + if (self.peek() != .semicolon and self.peek() != .rbrace and self.peek() != .eof and !self.isLineTerminatorBeforeNext()) { + arg = try self.parseExpr(0); + } + self.semicolon(); + return Stmt{ .loc = .{ .start = start, .end = arg.?.loc.end }, .data = .{ .@"return" = .{ .loc = .{ .start = start, .end = if (arg) |a| a.loc.end else start + 6 }, .arg = arg } } }; + } + + fn parseSwitch(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + try self.expect(.lparen); + const discriminant = try self.parseExpr(0); + try self.expect(.rparen); + try self.expect(.lbrace); + + var cases: [256]SwitchCase = undefined; + var case_count: usize = 0; + + while (self.peek() == .case or self.peek() == .default) { + const case_start = self.tok().loc.start; + if (self.matchKeyword(.case)) { + const case_test = try self.parseExpr(0); + try self.expect(.@":"); + var consequent: [128]Stmt = undefined; + var cons_count: usize = 0; + while (self.peek() != .case and self.peek() != .default and self.peek() != .rbrace and self.peek() != .eof) { + const s = try self.parseStatementListItem(); + consequent[cons_count] = s; + cons_count += 1; + } + cases[case_count] = .{ .loc = .{ .start = case_start, .end = self.tok().loc.start }, .condition = case_test, .consequent = consequent[0..cons_count] }; + case_count += 1; + } else if (self.matchKeyword(.default)) { + try self.expect(.@":"); + var consequent: [128]Stmt = undefined; + var cons_count: usize = 0; + while (self.peek() != .case and self.peek() != .default and self.peek() != .rbrace and self.peek() != .eof) { + const s = try self.parseStatementListItem(); + consequent[cons_count] = s; + cons_count += 1; + } + cases[case_count] = .{ .loc = .{ .start = case_start, .end = self.tok().loc.start }, .condition = null, .consequent = consequent[0..cons_count] }; + case_count += 1; + } + } + + const end_tok = try self.expectTok(.rbrace); + return Stmt{ + .loc = .{ .start = start, .end = end_tok.loc.end }, + .data = .{ .@"switch" = .{ .loc = .{ .start = start, .end = end_tok.loc.end }, .discriminant = discriminant, .cases = cases[0..case_count] } }, + }; + } + + fn parseThrow(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + if (self.isLineTerminatorBeforeNext()) return ParseError.ExpectedExpression; + const arg = try self.parseExpr(0); + self.semicolon(); + return Stmt{ .loc = .{ .start = start, .end = arg.loc.end }, .data = .{ .throw = .{ .loc = .{ .start = start, .end = arg.loc.end }, .arg = arg } } }; + } + + fn parseTry(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + const block = try self.parseBlockStmt(); + var handler: ?CatchClause = null; + var finalizer: ?BlockStmt = null; + + if (self.matchKeyword(.@"catch")) { + var param: ?Pat = null; + if (self.match(.lparen)) { + const pat = try self.parseBindingPattern(); + param = pat; + try self.expect(.rparen); + } + const catch_body = try self.parseBlockStmt(); + handler = .{ .loc = .{ .start = block.loc.start, .end = catch_body.loc.end }, .param = param, .body = catch_body }; + } + + if (self.matchKeyword(.finally)) { + const finally_body = try self.parseBlockStmt(); + finalizer = finally_body; + } + + const end = if (finalizer) |f| f.loc.end else if (handler) |h| h.loc.end else block.loc.end; + return Stmt{ + .loc = .{ .start = start, .end = end }, + .data = .{ .@"try" = .{ .loc = .{ .start = start, .end = end }, .block = block, .handler = handler, .finalizer = finalizer } }, + }; + } + + fn parseWith(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + try self.expect(.lparen); + const obj = try self.parseExpr(0); + try self.expect(.rparen); + const body = try self.statementPointer(); + return Stmt{ + .loc = .{ .start = start, .end = body.loc.end }, + .data = .{ .with = .{ .loc = .{ .start = start, .end = body.loc.end }, .obj = obj, .body = body } }, + }; + } + + fn parseBlockStmt(self: *@This()) ParseError!BlockStmt { + const start = try self.expectTok(.lbrace); + var stmts: [1024]Stmt = undefined; + var count: usize = 0; + + while (self.peek() != .rbrace and self.peek() != .eof) { + const s = try self.parseStatementListItem(); + stmts[count] = s; + count += 1; + if (count >= stmts.len) return ParseError.TooManyTokens; + } + + const end = try self.expectTok(.rbrace); + return .{ .loc = .{ .start = start.loc.start, .end = end.loc.end }, .body = stmts[0..count] }; + } + + fn parseVarStmt(self: *@This()) ParseError!Stmt { + const kind_tok = self.advanceTok(); + var decls: [128]VarDeclarator = undefined; + var count: usize = 0; + + while (true) { + const pat = try self.parseBindingPattern(); + var decl_init: ?*const Expr = null; + if (self.match(.@"=")) { + decl_init = try self.parseExpr(0); + } + decls[count] = .{ .loc = pat.loc, .id = pat, .init = decl_init }; + count += 1; + if (!self.match(.comma)) break; + } + + self.semicolon(); + const kind: VarKind = .@"var"; + return Stmt{ + .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, + .data = .{ .variable = .{ .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, .kind = kind, .decls = decls[0..count] } }, + }; + } + + fn parseLexicalDecl(self: *@This()) ParseError!Stmt { + const kind_tok = self.advanceTok(); + var decls: [128]VarDeclarator = undefined; + var count: usize = 0; + + while (true) { + const pat = try self.parseBindingPattern(); + var decl_init: ?*const Expr = null; + if (kind_tok.kind == .@"const") { + if (!self.match(.@"=")) return ParseError.ExpectedToken; + decl_init = try self.parseExpr(0); + } else if (self.match(.@"=")) { + decl_init = try self.parseExpr(0); + } + decls[count] = .{ .loc = pat.loc, .id = pat, .init = decl_init }; + count += 1; + if (!self.match(.comma)) break; + } + + self.semicolon(); + const kind: VarKind = switch (kind_tok.kind) { + .let => .let, + .@"const" => .@"const", + else => return ParseError.UnexpectedToken, + }; + return Stmt{ + .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, + .data = .{ .lexical = .{ .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, .kind = kind, .decls = decls[0..count] } }, + }; + } + + fn parseUsingStmt(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + var decls: [128]VarDeclarator = undefined; + var count: usize = 0; + + while (true) { + const id_tok = try self.expectTok(.identifier); + const id = self.tokenSlice(id_tok); + var decl_init: ?*const Expr = null; + if (self.match(.@"=")) { + decl_init = try self.parseExpr(0); + } + decls[count] = .{ .loc = .{ .start = id_tok.loc.start, .end = if (decl_init) |e| e.loc.end else id_tok.loc.end }, .id = Pat{ .loc = .{ .start = id_tok.loc.start, .end = id_tok.loc.end }, .data = .{ .ident = id } }, .init = decl_init }; + count += 1; + if (!self.match(.comma)) break; + } + + self.semicolon(); + return Stmt{ + .loc = .{ .start = start, .end = decls[count - 1].loc.end }, + .data = .{ .using = .{ .loc = .{ .start = start, .end = decls[count - 1].loc.end }, .decls = decls[0..count] } }, + }; + } + + fn parseExpressionStatement(self: *@This()) ParseError!Stmt { + const expr = try self.parseExpr(0); + self.semicolon(); + return Stmt{ .loc = expr.loc, .data = .{ .expr = .{ .loc = expr.loc, .expr = expr } } }; + } + + fn parseFunctionDecl(self: *@This(), fn_type_arg: FnType, mode: enum { stmt, expr }) ParseError!?Stmt { + const start = self.tok().loc.start; + var fn_type = fn_type_arg; + + if (fn_type == .async) { + _ = self.advanceTok(); // async + } else if (fn_type == .async_generator) { + _ = self.advanceTok(); // async + _ = self.advanceTok(); // * + } + + _ = self.advanceTok(); // function + + if (fn_type == .normal and self.peek() == .@"*") { + self.advance(); + fn_type = .generator; + } + + var id: ?[]const u8 = null; + if (self.peek().isIdentifier()) { + const id_tok = self.advanceTok(); + id = self.tokenSlice(id_tok); + } + + const params = try self.parseParams(); + const body = try self.parseFunctionBody(); + + const end = body.loc.end; + + if (mode == .stmt) { + return Stmt{ + .loc = .{ .start = start, .end = end }, + .data = .{ .function = .{ .loc = .{ .start = start, .end = end }, .fn_type = fn_type, .id = id, .params = params, .body = body } }, + }; + } + return null; + } + + fn parseFnExpr(self: *@This()) ParseError!*const Expr { + const start = self.advanceTok().loc.start; + + var fn_type: FnType = .normal; + if (self.peek() == .@"*") { + fn_type = .generator; + self.advance(); + } + + var id: ?[]const u8 = null; + if (self.peek() == .identifier or self.peek().isKeywordButNotLet()) { + const id_tok = self.advanceTok(); + id = self.tokenSlice(id_tok); + } + + const params = try self.parseParams(); + const body = try self.parseFunctionBody(); + + return self.arena.exprPtr( + .{ .func = .{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = fn_type, .id = id, .params = params, .body = body } }, + .{ .start = start, .end = body.loc.end }, + ); + } + + fn parseParams(self: *@This()) ParseError![]const Pat { + try self.expect(.lparen); + var params: [128]Pat = undefined; + var count: usize = 0; + + if (self.peek() != .rparen) { + while (true) { + if (self.peek() == .@"...") { + self.advance(); + const pat = try self.parseBindingPattern(); + params[count] = Pat{ .loc = pat.loc, .data = .{ .rest = .{ .loc = pat.loc, .arg = self.arena.patPtr(pat.data, pat.loc) } } }; + count += 1; + break; + } + const pat = try self.parseBindingPattern(); + if (self.match(.@"=")) { + const expr = try self.parseExpr(0); + params[count] = Pat{ .loc = .{ .start = pat.loc.start, .end = expr.loc.end }, .data = .{ .assign = .{ .loc = .{ .start = pat.loc.start, .end = expr.loc.end }, .left = self.arena.patPtr(pat.data, pat.loc), .right = expr } } }; + } else { + params[count] = pat; + } + count += 1; + if (!self.match(.comma)) break; + if (self.peek() == .@"...") { + self.advance(); + const rest_pat = try self.parseBindingPattern(); + params[count] = Pat{ .loc = rest_pat.loc, .data = .{ .rest = .{ .loc = rest_pat.loc, .arg = self.arena.patPtr(rest_pat.data, rest_pat.loc) } } }; + count += 1; + break; + } + } + } + + try self.expect(.rparen); + return params[0..count]; + } + + fn parseFunctionBody(self: *@This()) ParseError!BlockStmt { + return self.parseBlockStmt(); + } + + fn parseClassDecl(self: *@This(), mode: enum { stmt, expr }) ParseError!?Stmt { + const start = self.advanceTok().loc.start; + + var id: ?[]const u8 = null; + if (self.peek() == .identifier) { + const id_tok = self.advanceTok(); + id = self.tokenSlice(id_tok); + } + + var super_class: ?*const Expr = null; + if (self.matchKeyword(.extends)) { + super_class = try self.parseExpr(0); + } + + try self.expect(.lbrace); + var elems: [128]ClassElem = undefined; + var elem_count: usize = 0; + + while (self.peek() != .rbrace and self.peek() != .eof) { + const elem = try self.parseClassElement(); + elems[elem_count] = elem; + elem_count += 1; + if (elem_count >= elems.len) return ParseError.TooManyTokens; + } + + const end_tok = try self.expectTok(.rbrace); + + if (mode == .stmt) { + return Stmt{ + .loc = .{ .start = start, .end = end_tok.loc.end }, + .data = .{ .class = .{ .loc = .{ .start = start, .end = end_tok.loc.end }, .id = id, .super_class = super_class, .body = elems[0..elem_count] } }, + }; + } + return null; + } + + fn parseClassExpr(self: *@This()) ParseError!*const Expr { + const start = self.advanceTok().loc.start; + + var id: ?[]const u8 = null; + if (self.peek() == .identifier) { + const id_tok = self.advanceTok(); + id = self.tokenSlice(id_tok); + } + + var super_class: ?*const Expr = null; + if (self.matchKeyword(.extends)) { + super_class = try self.parseExpr(0); + } + + try self.expect(.lbrace); + var elems: [128]ClassElem = undefined; + var elem_count: usize = 0; + + while (self.peek() != .rbrace and self.peek() != .eof) { + const elem = try self.parseClassElement(); + elems[elem_count] = elem; + elem_count += 1; + } + + const end_tok = try self.expectTok(.rbrace); + return self.arena.exprPtr( + .{ .class = .{ .loc = .{ .start = start, .end = end_tok.loc.end }, .id = id, .super_class = super_class, .body = elems[0..elem_count] } }, + .{ .start = start, .end = end_tok.loc.end }, + ); + } + + fn parseClassElement(self: *@This()) ParseError!ClassElem { + const start = self.tok().loc.start; + const is_static = self.matchKeyword(.static); + + if (is_static and self.peek() == .lbrace) { + const body = try self.parseBlockStmt(); + return ClassElem{ .static_block = body }; + } + + if (self.matchKeyword(.get)) { + const key = try self.parsePropKey(); + try self.expect(.lparen); + try self.expect(.rparen); + const body = try self.parseFunctionBody(); + if (is_static) return ClassElem{ .static_get = .{ .key = key, .body = body } }; + return ClassElem{ .get = .{ .key = key, .body = body } }; + } + + if (self.matchKeyword(.set)) { + const key = try self.parsePropKey(); + try self.expect(.lparen); + const param = try self.parseBindingPattern(); + try self.expect(.rparen); + const body = try self.parseFunctionBody(); + if (is_static) return ClassElem{ .static_set = .{ .key = key, .param = param, .body = body } }; + return ClassElem{ .set = .{ .key = key, .param = param, .body = body } }; + } + + if (self.match(.@"*")) { + _ = try self.parsePropKey(); + try self.expect(.lparen); + const params = try self.parseParams(); + const body = try self.parseFunctionBody(); + const fn_expr = FnExpr{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = .generator, .id = null, .params = params, .body = body }; + if (is_static) return ClassElem{ .static_method = fn_expr }; + return ClassElem{ .method = fn_expr }; + } + + const key = try self.parsePropKey(); + + if (self.peek() == .lparen) { + const params = try self.parseParams(); + const body = try self.parseFunctionBody(); + const fn_expr = FnExpr{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = .normal, .id = null, .params = params, .body = body }; + if (is_static) return ClassElem{ .static_method = fn_expr }; + return ClassElem{ .method = fn_expr }; + } + + var value: ?*const Expr = null; + if (self.match(.@"=")) { + value = try self.parseExpr(0); + } + if (self.peek() == .semicolon) self.advance(); + + const field = FieldDef{ .loc = .{ .start = start, .end = if (value) |v| v.loc.end else self.tok().loc.start }, .key = key, .value = value }; + if (is_static) return ClassElem{ .static_field = field }; + return ClassElem{ .field = field }; + } + + fn parseImport(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + var specifiers: [32]ImportSpecifier = undefined; + var spec_count: usize = 0; + + if (self.peek() != .string and self.peek() != .lparen) { + if (self.peek() == .identifier or self.peek().isKeyword()) { + const local_tok = self.advanceTok(); + const local = self.tokenSlice(local_tok); + specifiers[spec_count] = .{ .default = local }; + spec_count += 1; + if (self.match(.comma)) {} + } + + if (self.match(.@"*")) { + try self.expect(.identifier); // as + const ns_tok = try self.expectTok(.identifier); + const ns = self.tokenSlice(ns_tok); + specifiers[spec_count] = .{ .namespace = ns }; + spec_count += 1; + } else if (self.peek() == .lbrace) { + self.advance(); + if (self.peek() != .rbrace) { + while (true) { + const imported_tok = try self.expectTok(.identifier); + const imported = self.tokenSlice(imported_tok); + var local = imported; + if (self.match(.identifier)) { + local = self.tokenSlice(self.tokens[self.pos - 1]); + } + specifiers[spec_count] = .{ .named = .{ .imported = imported, .local = local } }; + spec_count += 1; + if (!self.match(.comma)) break; + } + } + try self.expect(.rbrace); + } + } + + var source: []const u8 = ""; + var attrs: [8]ImportAttribute = undefined; + var attr_count: usize = 0; + + if (self.matchKeyword(.from)) { + const str_tok = try self.expectTok(.string); + source = self.tokenSlice(str_tok); + } else if (self.peek() == .string) { + const str_tok = self.advanceTok(); + source = self.tokenSlice(str_tok); + } + + if (self.matchKeyword(.with)) { + try self.expect(.lbrace); + while (self.peek() != .rbrace and self.peek() != .eof) { + const key_tok = self.advanceTok(); + const key = self.tokenSlice(key_tok); + try self.expect(.@":"); + const val_tok = try self.expectTok(.string); + const val = self.tokenSlice(val_tok); + attrs[attr_count] = .{ .key = key, .value = val }; + attr_count += 1; + _ = self.match(.comma); + } + try self.expect(.rbrace); + } + + try self.expect(.semicolon); + return Stmt{ + .loc = .{ .start = start, .end = self.tok().loc.start }, + .data = .{ .import = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .specifiers = specifiers[0..spec_count], .source = source, .attributes = attrs[0..attr_count] } }, + }; + } + + fn parseExport(self: *@This()) ParseError!Stmt { + const start = self.advanceTok().loc.start; + + if (self.matchKeyword(.default)) { + if (self.peek() == .function or self.peek() == .async or self.peek() == .class) { + const decl = try self.parseStatementListItem(); + self.semicolon(); + return Stmt{ .loc = .{ .start = start, .end = decl.loc.end }, .data = decl.data }; + } + const expr = try self.parseExpr(0); + self.semicolon(); + return Stmt{ .loc = .{ .start = start, .end = expr.loc.end }, .data = .{ .expr = .{ .loc = expr.loc, .expr = expr } } }; + } + + if (self.peek() == .@"*") { + self.advance(); + try self.expect(.identifier); // from + const source_tok = try self.expectTok(.string); + _ = self.tokenSlice(source_tok); + try self.expect(.semicolon); + return Stmt{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .empty = {} } }; + } + + if (self.peek() == .lbrace) { + self.advance(); + while (self.peek() != .rbrace and self.peek() != .eof) { + _ = self.advanceTok(); + _ = self.match(.identifier); // as + if (!self.match(.comma)) break; + } + try self.expect(.rbrace); + if (self.matchKeyword(.from)) { + _ = self.advanceTok(); + } + try self.expect(.semicolon); + return Stmt{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .empty = {} } }; + } + + const decl = try self.parseStatementListItem(); + self.semicolon(); + return decl; + } + + fn parseExpr(self: *@This(), min_bp: u32) ParseError!*const Expr { + var left = try self.parsePrimaryExpr(); + + while (true) { + const cur_bp = self.bp(self.peek()); + if (cur_bp <= min_bp) break; + + switch (self.peek()) { + .@"=", + .@"+=", + .@"-=", + .@"*=", + .@"/=", + .@"%=", + .@"**=", + .@"<<=", + .@">>=", + .@">>>=", + .@"&=", + .@"|=", + .@"^=", + .@"&&=", + .@"||=", + .@"??=", + => { + self.advance(); + const op = self.assignOpFromToken(); + const right = try self.parseExpr(BP.assign); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .assign = .{ .loc = loc, .op = op, .left = left, .right = right } }, loc); + }, + + .@"?" => { + self.advance(); + const consequent = try self.parseExpr(0); + try self.expect(.@":"); + const alternate = try self.parseExpr(BP.conditional); + const loc: Loc = .{ .start = left.loc.start, .end = alternate.loc.end }; + left = self.arena.exprPtr(.{ .conditional = .{ .loc = loc, .condition = left, .consequent = consequent, .alternate = alternate } }, loc); + }, + + .@"??" => { + self.advance(); + const right = try self.parseExpr(BP.coalesce); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"??", .left = left, .right = right } }, loc); + }, + + .@"||" => { + self.advance(); + const right = try self.parseExpr(BP.log_or); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"||", .left = left, .right = right } }, loc); + }, + + .@"&&" => { + self.advance(); + const right = try self.parseExpr(BP.log_and); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"&&", .left = left, .right = right } }, loc); + }, + + .@"|" => { + self.advance(); + const right = try self.parseExpr(BP.bit_or); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"|", .left = left, .right = right } }, loc); + }, + + .@"^" => { + self.advance(); + const right = try self.parseExpr(BP.bit_xor); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"^", .left = left, .right = right } }, loc); + }, + + .@"&" => { + self.advance(); + const right = try self.parseExpr(BP.bit_and); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"&", .left = left, .right = right } }, loc); + }, + + .@"==" => { + self.advance(); + const right = try self.parseExpr(BP.equality); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"==", .left = left, .right = right } }, loc); + }, + + .@"!=" => { + self.advance(); + const right = try self.parseExpr(BP.equality); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"!=", .left = left, .right = right } }, loc); + }, + + .@"===" => { + self.advance(); + const right = try self.parseExpr(BP.equality); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"===", .left = left, .right = right } }, loc); + }, + + .@"!==" => { + self.advance(); + const right = try self.parseExpr(BP.equality); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"!==", .left = left, .right = right } }, loc); + }, + + .@"<" => { + self.advance(); + const right = try self.parseExpr(BP.relational); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"<", .left = left, .right = right } }, loc); + }, + + .@">" => { + self.advance(); + const right = try self.parseExpr(BP.relational); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">", .left = left, .right = right } }, loc); + }, + + .@"<=" => { + self.advance(); + const right = try self.parseExpr(BP.relational); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"<=", .left = left, .right = right } }, loc); + }, + + .@">=" => { + self.advance(); + const right = try self.parseExpr(BP.relational); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">=", .left = left, .right = right } }, loc); + }, + + .in => { + self.advance(); + const right = try self.parseExpr(BP.relational); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .in, .left = left, .right = right } }, loc); + }, + + .instanceof => { + self.advance(); + const right = try self.parseExpr(BP.relational); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .instanceof, .left = left, .right = right } }, loc); + }, + + .@"<<" => { + self.advance(); + const right = try self.parseExpr(BP.shift); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"<<", .left = left, .right = right } }, loc); + }, + + .@">>" => { + self.advance(); + const right = try self.parseExpr(BP.shift); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">>", .left = left, .right = right } }, loc); + }, + + .@">>>" => { + self.advance(); + const right = try self.parseExpr(BP.shift); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">>>", .left = left, .right = right } }, loc); + }, + + .@"+" => { + self.advance(); + const right = try self.parseExpr(BP.additive); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"+", .left = left, .right = right } }, loc); + }, + + .@"-" => { + self.advance(); + const right = try self.parseExpr(BP.additive); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"-", .left = left, .right = right } }, loc); + }, + + .@"*" => { + self.advance(); + const right = try self.parseExpr(BP.multiplicative); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"*", .left = left, .right = right } }, loc); + }, + + .@"/" => { + self.advance(); + const right = try self.parseExpr(BP.multiplicative); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"/", .left = left, .right = right } }, loc); + }, + + .@"%" => { + self.advance(); + const right = try self.parseExpr(BP.multiplicative); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"%", .left = left, .right = right } }, loc); + }, + + .@"**" => { + self.advance(); + const right = try self.parseExpr(BP.exponentiation - 1); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"**", .left = left, .right = right } }, loc); + }, + + .lparen => { + self.advance(); + var args: [256]Expr = undefined; + var arg_count: usize = 0; + + if (self.peek() != .rparen) { + while (true) { + if (self.peek() == .@"...") { + self.advance(); + const arg = try self.parseExpr(0); + args[arg_count] = Expr{ .loc = arg.loc, .data = .{ .spread = .{ .loc = arg.loc, .arg = self.arena.exprPtr(arg.data, arg.loc) } } }; + arg_count += 1; + } else { + const arg = try self.parseExpr(0); + args[arg_count] = arg.*; + arg_count += 1; + } + if (!self.match(.comma)) break; + } + } + + try self.expect(.rparen); + const loc: Loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; + left = self.arena.exprPtr(.{ .call = .{ .loc = loc, .callee = left, .args = args[0..arg_count], .optional = false } }, loc); + }, + + .@"?." => { + self.advance(); + if (self.peek() == .lparen) { + self.advance(); + var args: [256]Expr = undefined; + var arg_count: usize = 0; + if (self.peek() != .rparen) { + while (true) { + const arg = try self.parseExpr(0); + args[arg_count] = arg.*; + arg_count += 1; + if (!self.match(.comma)) break; + } + } + try self.expect(.rparen); + const loc: Loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; + left = self.arena.exprPtr(.{ .call = .{ .loc = loc, .callee = left, .args = args[0..arg_count], .optional = true } }, loc); + } else if (self.peek() == .lbracket) { + self.advance(); + const expr = try self.parseExpr(0); + try self.expect(.rbracket); + const loc: Loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; + left = self.arena.exprPtr(.{ .computed_member = .{ .loc = loc, .obj = left, .expr = expr } }, loc); + } else if (self.peek() == .identifier or self.peek().isKeyword()) { + const prop_tok = self.advanceTok(); + const prop = self.tokenSlice(prop_tok); + const loc: Loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; + left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .ident = prop } } }, loc); + } else if (self.peek() == .private_identifier) { + const prop_tok = self.advanceTok(); + const prop = self.tokenSlice(prop_tok); + const loc: Loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; + left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .private = prop } } }, loc); + } + }, + + .lbracket => { + self.advance(); + const expr = try self.parseExpr(0); + try self.expect(.rbracket); + const loc: Loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; + left = self.arena.exprPtr(.{ .computed_member = .{ .loc = loc, .obj = left, .expr = expr } }, loc); + }, + + .@"." => { + self.advance(); + if (self.peek() == .private_identifier) { + const prop_tok = self.advanceTok(); + const prop = self.tokenSlice(prop_tok); + const loc: Loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; + left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .private = prop } } }, loc); + } else { + const prop_tok = try self.expectTok(.identifier); + const prop = self.tokenSlice(prop_tok); + const loc: Loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; + left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .ident = prop } } }, loc); + } + }, + + .template_head, .no_sub_template => { + const template = try self.parseTemplateLit(); + const loc: Loc = .{ .start = left.loc.start, .end = template.loc.end }; + left = self.arena.exprPtr(.{ .tagged_template = .{ .loc = loc, .tag = left, .quasi = template } }, loc); + }, + + .@"++" => { + if (!self.isLineTerminatorBeforeNext()) { + self.advance(); + const loc: Loc = .{ .start = left.loc.start, .end = left.loc.end + 2 }; + left = self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"++", .arg = left, .prefix = false } }, loc); + } else break; + }, + + .@"--" => { + if (!self.isLineTerminatorBeforeNext()) { + self.advance(); + const loc: Loc = .{ .start = left.loc.start, .end = left.loc.end + 2 }; + left = self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"--", .arg = left, .prefix = false } }, loc); + } else break; + }, + + .comma => { + self.advance(); + const right = try self.parseExpr(BP.comma); + const loc: Loc = .{ .start = left.loc.start, .end = right.loc.end }; + var exprs: [2]Expr = .{ left.*, right.* }; + left = self.arena.exprPtr(.{ .sequence = .{ .loc = loc, .exprs = &exprs } }, loc); + }, + + else => break, + } + } + + return left; + } + + fn parsePrimaryExpr(self: *@This()) ParseError!*const Expr { + const start = self.tok().loc.start; + + switch (self.peek()) { + .this => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .this = {} }, .{ .start = start, .end = t.loc.end }); + }, + .super => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .super = {} }, .{ .start = start, .end = t.loc.end }); + }, + .null => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .null = {} }, .{ .start = start, .end = t.loc.end }); + }, + .true => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .bool = true }, .{ .start = start, .end = t.loc.end }); + }, + .false => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .bool = false }, .{ .start = start, .end = t.loc.end }); + }, + .number => { + const t = self.advanceTok(); + const val = std.fmt.parseFloat(f64, self.tokenSlice(t)) catch 0.0; + return self.arena.exprPtr(.{ .number = val }, .{ .start = start, .end = t.loc.end }); + }, + .string => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .string = self.tokenSlice(t) }, .{ .start = start, .end = t.loc.end }); + }, + .bigint => { + const t = self.advanceTok(); + return self.arena.exprPtr(.{ .number = 0 }, .{ .start = start, .end = t.loc.end }); + }, + .regex => { + const t = self.advanceTok(); + const slice = self.tokenSlice(t); + const slash_pos = std.mem.indexOfScalar(u8, slice[1..], '/') orelse 0; + const pattern = slice[1 .. slash_pos + 1]; + const flags = slice[slash_pos + 2 ..]; + return self.arena.exprPtr( + .{ .regex = .{ .pattern = pattern, .flags = flags } }, + .{ .start = start, .end = t.loc.end }, + ); + }, + .identifier => { + const t = self.advanceTok(); + const name = self.tokenSlice(t); + return self.arena.exprPtr(.{ .identifier = name }, .{ .start = start, .end = t.loc.end }); + }, + .private_identifier => { + const t = self.advanceTok(); + const name = self.tokenSlice(t); + return self.arena.exprPtr(.{ .private_ident = name }, .{ .start = start, .end = t.loc.end }); + }, + .lbrace => return self.parseObjLiteral(), + .lbracket => return self.parseArrayLiteral(), + .lparen => { + self.advance(); + if (self.peek() == .rparen) { + self.advance(); + if (self.peek() == .@"=>") { + return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = self.tok().loc.start }, .params = &.{} }); + } + return self.arena.exprPtr(.{ .identifier = "" }, .{ .start = start, .end = self.tok().loc.start }); + } + if (self.peek() == .@"...") { + self.advance(); + const arg = try self.parseExpr(0); + try self.expect(.rparen); + if (self.peek() == .@"=>") { + return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = arg.loc.end }, .params = &.{} }); + } + const loc: Loc = .{ .start = start, .end = self.tok().loc.start }; + return self.arena.exprPtr(.{ .spread = .{ .loc = loc, .arg = arg } }, loc); + } + const expr = try self.parseExpr(0); + if (self.peek() == .comma) { + var params: [128]Pat = undefined; + var count: usize = 0; + params[count] = Pat{ .loc = expr.loc, .data = .{ .ident = expr.data.identifier } }; + count += 1; + while (self.match(.comma)) { + const p = try self.parseBindingPattern(); + params[count] = p; + count += 1; + } + try self.expect(.rparen); + if (self.peek() == .@"=>") { + return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = self.tok().loc.start }, .params = params[0..count] }); + } + return self.arena.exprPtr(.{ .identifier = "" }, .{ .start = start, .end = self.tok().loc.start }); + } + try self.expect(.rparen); + if (self.peek() == .@"=>") { + const pat = Pat{ .loc = expr.loc, .data = .{ .ident = expr.data.identifier } }; + return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = self.tok().loc.start }, .params = &.{pat} }); + } + return self.arena.exprPtr(.{ .parenthesized = expr }, .{ .start = start, .end = self.tok().loc.start }); + }, + .function => return self.parseFnExpr(), + .class => return self.parseClassExpr(), + .async => { + self.advance(); + if (self.peek() == .function) { + return self.parseFnExpr(); + } + const id_tok = try self.expectTok(.identifier); + const name = self.tokenSlice(id_tok); + if (self.peek() == .@"=>") { + const pat = Pat{ .loc = .{ .start = start, .end = id_tok.loc.end }, .data = .{ .ident = name } }; + return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = id_tok.loc.end }, .params = &.{pat} }); + } + return self.arena.exprPtr(.{ .identifier = name }, .{ .start = start, .end = id_tok.loc.end }); + }, + .new => { + self.advance(); + const callee = try self.parseExpr(BP.member); + var args: [256]Expr = undefined; + var arg_count: usize = 0; + if (self.peek() == .lparen) { + self.advance(); + if (self.peek() != .rparen) { + while (true) { + const arg = try self.parseExpr(0); + args[arg_count] = arg.*; + arg_count += 1; + if (!self.match(.comma)) break; + } + } + try self.expect(.rparen); + } + const loc: Loc = .{ .start = start, .end = if (arg_count > 0) args[arg_count - 1].loc.end else callee.loc.end }; + return self.arena.exprPtr(.{ .new = .{ .loc = loc, .callee = callee, .args = args[0..arg_count] } }, loc); + }, + .delete, .typeof, .void, .@"!", .@"~", .@"+", .@"-" => { + const op = self.unaryOpFromToken(); + self.advance(); + const arg = try self.parseExpr(BP.unary); + const loc: Loc = .{ .start = start, .end = arg.loc.end }; + return self.arena.exprPtr(.{ .unary = .{ .loc = loc, .op = op, .arg = arg, .prefix = true } }, loc); + }, + .@"++" => { + self.advance(); + const arg = try self.parseExpr(BP.update); + const loc: Loc = .{ .start = start, .end = arg.loc.end }; + return self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"++", .arg = arg, .prefix = true } }, loc); + }, + .@"--" => { + self.advance(); + const arg = try self.parseExpr(BP.update); + const loc: Loc = .{ .start = start, .end = arg.loc.end }; + return self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"--", .arg = arg, .prefix = true } }, loc); + }, + .await => { + self.advance(); + const arg = try self.parseExpr(BP.unary); + const loc: Loc = .{ .start = start, .end = arg.loc.end }; + return self.arena.exprPtr(.{ .await = .{ .loc = loc, .arg = arg } }, loc); + }, + .yield => { + self.advance(); + var delegate = false; + var arg: ?*const Expr = null; + if (!self.isLineTerminatorBeforeNext()) { + if (self.match(.@"*")) { + delegate = true; + } + if (self.peek() != .rbrace and self.peek() != .rparen and self.peek() != .rbracket and self.peek() != .semicolon and self.peek() != .comma and self.peek() != .@":" and self.peek() != .eof) { + arg = try self.parseExpr(0); + } + } + const loc: Loc = .{ .start = start, .end = if (arg) |a| a.loc.end else start + 5 }; + return self.arena.exprPtr(.{ .yield = .{ .loc = loc, .arg = arg, .delegate = delegate } }, loc); + }, + .template_head, .no_sub_template => return self.arena.exprPtr(.{ .template = try self.parseTemplateLit() }, .{ .start = start, .end = self.tok().loc.start }), + .import => { + self.advance(); + try self.expect(.lparen); + _ = try self.parseExpr(0); + try self.expect(.rparen); + return self.arena.exprPtr(.{ .identifier = "import" }, .{ .start = start, .end = self.tok().loc.start }); + }, + .@"#" => { + self.advance(); + const id_tok = try self.expectTok(.identifier); + return self.arena.exprPtr(.{ .private_ident = self.tokenSlice(id_tok) }, .{ .start = start, .end = id_tok.loc.end }); + }, + else => { + if (self.peek().isIdentifier()) { + const t = self.advanceTok(); + const name = self.tokenSlice(t); + if (self.peek() == .@"=>") { + const pat = Pat{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .data = .{ .ident = name } }; + return self.parseArrowExpr(.{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .params = &.{pat} }); + } + return self.arena.exprPtr(.{ .identifier = name }, .{ .start = t.loc.start, .end = t.loc.end }); + } + return ParseError.ExpectedExpression; + }, + } + } + + fn parseArrowExpr(self: *@This(), params_info: struct { loc: Loc, params: []const Pat }) ParseError!*const Expr { + try self.expect(.@"=>"); + const async_token = false; + + if (self.peek() == .lbrace) { + const body = try self.parseFunctionBody(); + return self.arena.exprPtr( + .{ .arrow = .{ .loc = .{ .start = params_info.loc.start, .end = body.loc.end }, .async_token = async_token, .params = params_info.params, .body = .{ .block = body } } }, + .{ .start = params_info.loc.start, .end = body.loc.end }, + ); + } + + const expr = try self.parseExpr(0); + return self.arena.exprPtr( + .{ .arrow = .{ .loc = .{ .start = params_info.loc.start, .end = expr.loc.end }, .async_token = async_token, .params = params_info.params, .body = .{ .expr = expr } } }, + .{ .start = params_info.loc.start, .end = expr.loc.end }, + ); + } + + fn parseObjLiteral(self: *@This()) ParseError!*const Expr { + const start = self.advanceTok().loc.start; + var props: [128]Prop = undefined; + var count: usize = 0; + + while (self.peek() != .rbrace and self.peek() != .eof) { + if (self.peek() == .@"...") { + self.advance(); + const arg = try self.parseExpr(0); + props[count] = .{ .loc = arg.loc, .key = .{ .ident = "" }, .data = .{ .spread = .{ .loc = arg.loc, .arg = arg } } }; + count += 1; + } else if (self.peek() == .identifier) { + const name_tok = self.tok(); + const name = self.tokenSlice(name_tok); + self.advance(); + if (self.peek() == .@":") { + self.advance(); + const value = try self.parseExpr(0); + props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = value.loc.end }, .key = .{ .ident = name }, .data = .{ .init = .{ .key = .{ .ident = name }, .value = value } } }; + count += 1; + } else if (self.peek() == .lparen) { + const params = try self.parseParams(); + const body = try self.parseFunctionBody(); + props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = body.loc.end }, .key = .{ .ident = name }, .data = .{ .method = .{ .loc = .{ .start = name_tok.loc.start, .end = body.loc.end }, .fn_type = .normal, .id = null, .params = params, .body = body } } }; + count += 1; + } else if (self.peek() == .@"=") { + self.advance(); + const value = try self.parseExpr(0); + props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = value.loc.end }, .key = .{ .ident = name }, .data = .{ .init = .{ .key = .{ .ident = name }, .value = value } } }; + count += 1; + } else if (self.peek() == .comma or self.peek() == .rbrace) { + props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = name_tok.loc.end }, .key = .{ .ident = name }, .data = .{ .shorthand = name } }; + count += 1; + } + } else if (self.peek() == .number or self.peek() == .string) { + const key = try self.parsePropKey(); + try self.expect(.@":"); + const value = try self.parseExpr(0); + props[count] = .{ .loc = .{ .start = start, .end = value.loc.end }, .key = key, .data = .{ .init = .{ .key = key, .value = value } } }; + count += 1; + } else if (self.peek() == .lbracket) { + const key = try self.parsePropKey(); + try self.expect(.@":"); + const value = try self.parseExpr(0); + props[count] = .{ .loc = .{ .start = start, .end = value.loc.end }, .key = key, .data = .{ .init = .{ .key = key, .value = value } } }; + count += 1; + } else if (self.matchKeyword(.get)) { + const key = try self.parsePropKey(); + try self.expect(.lparen); + try self.expect(.rparen); + const body = try self.parseFunctionBody(); + props[count] = .{ .loc = .{ .start = start, .end = body.loc.end }, .key = key, .data = .{ .get = .{ .key = key, .body = body } } }; + count += 1; + } else if (self.matchKeyword(.set)) { + const key = try self.parsePropKey(); + try self.expect(.lparen); + const param = try self.parseBindingPattern(); + try self.expect(.rparen); + const body = try self.parseFunctionBody(); + props[count] = .{ .loc = .{ .start = start, .end = body.loc.end }, .key = key, .data = .{ .set = .{ .key = key, .param = param, .body = body } } }; + count += 1; + } else if (self.match(.@"*")) { + const key = try self.parsePropKey(); + const params = try self.parseParams(); + const body = try self.parseFunctionBody(); + props[count] = .{ .loc = .{ .start = start, .end = body.loc.end }, .key = key, .data = .{ .method = .{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = .generator, .id = null, .params = params, .body = body } } }; + count += 1; + } else { + break; + } + + if (!self.match(.comma)) break; + } + + try self.expect(.rbrace); + return self.arena.exprPtr(.{ .object = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .props = props[0..count] } }, .{ .start = start, .end = self.tok().loc.start }); + } + + fn parseArrayLiteral(self: *@This()) ParseError!*const Expr { + const start = self.advanceTok().loc.start; + var elems: [256]?Expr = undefined; + var count: usize = 0; + + while (self.peek() != .rbracket and self.peek() != .eof) { + if (self.match(.comma)) { + elems[count] = null; + count += 1; + } else if (self.peek() == .@"...") { + self.advance(); + const arg = try self.parseExpr(0); + elems[count] = Expr{ .loc = arg.loc, .data = .{ .spread = .{ .loc = arg.loc, .arg = arg } } }; + count += 1; + if (!self.match(.comma)) break; + } else { + const expr = try self.parseExpr(0); + elems[count] = expr.*; + count += 1; + if (!self.match(.comma)) break; + } + } + + try self.expect(.rbracket); + return self.arena.exprPtr(.{ .array = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .elems = elems[0..count] } }, .{ .start = start, .end = self.tok().loc.start }); + } + + fn parseTemplateLit(self: *@This()) ParseError!TemplateLit { + const start = self.tok().loc.start; + var quasis: [64]TemplateElem = undefined; + var exprs: [64]Expr = undefined; + var q_count: usize = 0; + var e_count: usize = 0; + + while (true) { + switch (self.peek()) { + .no_sub_template => { + const t = self.advanceTok(); + const slice = self.tokenSlice(t); + quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = true }; + q_count += 1; + break; + }, + .template_head => { + const t = self.advanceTok(); + const slice = self.tokenSlice(t); + quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = false }; + q_count += 1; + const expr = try self.parseExpr(0); + exprs[e_count] = expr.*; + e_count += 1; + try self.expect(.rbrace); + }, + .template_middle => { + const t = self.advanceTok(); + const slice = self.tokenSlice(t); + quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = false }; + q_count += 1; + const expr = try self.parseExpr(0); + exprs[e_count] = expr.*; + e_count += 1; + try self.expect(.rbrace); + }, + .template_tail => { + const t = self.advanceTok(); + const slice = self.tokenSlice(t); + quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = true }; + q_count += 1; + break; + }, + else => break, + } + } + + return .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .quasis = quasis[0..q_count], .exprs = exprs[0..e_count] }; + } + + fn parsePropKey(self: *@This()) ParseError!PropKey { + switch (self.peek()) { + .identifier, .let, .static, .get, .set, .async => { + const t = self.advanceTok(); + return PropKey{ .ident = self.tokenSlice(t) }; + }, + .string => { + const t = self.advanceTok(); + return PropKey{ .string = self.tokenSlice(t) }; + }, + .number => { + const t = self.advanceTok(); + const val = std.fmt.parseFloat(f64, self.tokenSlice(t)) catch 0.0; + return PropKey{ .number = val }; + }, + .lbracket => { + self.advance(); + const expr = try self.parseExpr(0); + try self.expect(.rbracket); + return PropKey{ .computed = expr }; + }, + .private_identifier => { + const t = self.advanceTok(); + return PropKey{ .private = self.tokenSlice(t) }; + }, + else => return ParseError.ExpectedPropertyName, + } + } + + fn parseBindingPattern(self: *@This()) ParseError!Pat { + const start = self.tok().loc.start; + + switch (self.peek()) { + .identifier, .await, .yield => { + const t = self.advanceTok(); + const name = self.tokenSlice(t); + return Pat{ .loc = .{ .start = start, .end = t.loc.end }, .data = .{ .ident = name } }; + }, + .lbrace => { + self.advance(); + var props: [128]PatProp = undefined; + var count: usize = 0; + var rest: ?*const Pat = null; + + while (self.peek() != .rbrace and self.peek() != .eof) { + if (self.peek() == .@"...") { + self.advance(); + const pat = try self.parseBindingPattern(); + rest = self.arena.patPtr(pat.data, pat.loc); + break; + } + if (self.peek() == .identifier) { + const name_tok = self.tok(); + const name = self.tokenSlice(name_tok); + self.advance(); + if (self.peek() == .@":") { + self.advance(); + const val_pat = try self.parseBindingPattern(); + props[count] = .{ .key_value = .{ .key = .{ .ident = name }, .value = self.arena.patPtr(val_pat.data, val_pat.loc) } }; + count += 1; + } else { + props[count] = .{ .shorthand = name }; + count += 1; + } + } else if (self.peek() == .lbracket) { + const key = try self.parsePropKey(); + try self.expect(.@":"); + const val_pat = try self.parseBindingPattern(); + props[count] = .{ .key_value = .{ .key = key, .value = self.arena.patPtr(val_pat.data, val_pat.loc) } }; + count += 1; + } else { + break; + } + if (!self.match(.comma)) break; + } + + try self.expect(.rbrace); + return Pat{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .object = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .props = props[0..count], .rest = rest } } }; + }, + .lbracket => { + self.advance(); + var elems: [128]?Pat = undefined; + var count: usize = 0; + var rest: ?*const Pat = null; + + while (self.peek() != .rbracket and self.peek() != .eof) { + if (self.match(.comma)) { + elems[count] = null; + count += 1; + } else if (self.peek() == .@"...") { + self.advance(); + const pat = try self.parseBindingPattern(); + rest = self.arena.patPtr(pat.data, pat.loc); + break; + } else { + const pat = try self.parseBindingPattern(); + elems[count] = pat; + count += 1; + if (!self.match(.comma)) break; + } + } + + try self.expect(.rbracket); + return Pat{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .array = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .elems = elems[0..count], .rest = rest } } }; + }, + else => return ParseError.ExpectedParameterName, + } + } + + fn bp(_: *@This(), kind: TokenType) u32 { + return switch (kind) { + .comma => BP.comma, + .@"=", .@"+=", .@"-=", .@"*=", .@"/=", .@"%=", .@"**=", .@"<<=", .@">>=", .@">>>=", .@"&=", .@"|=", .@"^=", .@"&&=", .@"||=", .@"??=" => BP.assign, + .@"?" => BP.conditional, + .@"??" => BP.coalesce, + .@"||" => BP.log_or, + .@"&&" => BP.log_and, + .@"|" => BP.bit_or, + .@"^" => BP.bit_xor, + .@"&" => BP.bit_and, + .@"==", .@"!=", .@"===", .@"!==" => BP.equality, + .@"<", .@">", .@"<=", .@">=", .in, .instanceof => BP.relational, + .@"<<", .@">>", .@">>>" => BP.shift, + .@"+", .@"-" => BP.additive, + .@"*", .@"/", .@"%" => BP.multiplicative, + .@"**" => BP.exponentiation, + .lparen => BP.call, + .lbracket, .@".", .@"?." => BP.member, + .@"++", .@"--" => BP.update, + .template_head, .no_sub_template => BP.member, + else => 0, + }; + } + + fn unaryOpFromToken(self: *@This()) UnaryOp { + return switch (self.tok().kind) { + .@"+" => .@"+", + .@"-" => .@"-", + .@"!" => .@"!", + .@"~" => .@"~", + .typeof => .typeof, + .void => .void, + .delete => .delete, + else => .@"+", + }; + } + + fn assignOpFromToken(self: *@This()) AssignOp { + return switch (self.tokens[self.pos - 1].kind) { + .@"=" => .@"=", + .@"+=" => .@"+=", + .@"-=" => .@"-=", + .@"*=" => .@"*=", + .@"/=" => .@"/=", + .@"%=" => .@"%=", + .@"**=" => .@"**=", + .@"<<=" => .@"<<=", + .@">>=" => .@">>=", + .@">>>=" => .@">>>=", + .@"&=" => .@"&=", + .@"|=" => .@"|=", + .@"^=" => .@"^=", + .@"&&=" => .@"&&=", + .@"||=" => .@"||=", + .@"??=" => .@"??=", + else => .@"=", + }; + } + + fn semicolon(self: *@This()) void { + if (self.peek() == .semicolon) { + self.advance(); + } + } + + fn statementPointer(self: *@This()) ParseError!*const Stmt { + const s = try self.parseStatement(); + return self.arena.stmtPtr(s.data, s.loc); + } +}; + pub const Node = union(enum) { program: Program, @@ -13,8 +1924,8 @@ pub const Node = union(enum) { decl: Decl, literal: Lit, property: Prop, - template: Template, - spread: Spread, + template: TemplateLit, + spread: SpreadElem, decorator: Decorator, import_specifier: ImportSpecifier, @@ -25,8 +1936,26 @@ pub const Node = union(enum) { }; pub const Program = struct { - body: []const Node, + body: []const Stmt, loc: Loc, + + pub fn parse(comptime source: []const u8) ParseError!Program { + return comptime blk: { + var token_buf: [4096]Token = undefined; + const tokens = lex.Lexer.tokenizeBuf(source, &token_buf); + var arena: NodeArena = .{}; + var parser = Parser.init(tokens, source, &arena); + break :blk try parser.parseProgram(); + }; + } + + pub fn fmt(self: @This(), f: *Formatter) void { + @setEvalBranchQuota(100000); + for (self.body, 0..) |stmt, i| { + if (i > 0) f.newline(); + stmt.fmt(f); + } + } }; pub const StmtType = union(enum) { @@ -49,6 +1978,9 @@ pub const StmtType = union(enum) { labelled: LabelledStmt, with: WithStmt, variable: VarStmt, + lexical: LexicalDecl, + function: FnDecl, + class: ClassDecl, using: UsingStmt, import: ImportDecl, @"export": ExportDecl, @@ -57,6 +1989,80 @@ pub const StmtType = union(enum) { pub const Stmt = struct { loc: Loc, data: StmtType, + + pub fn fmt(self: @This(), f: *Formatter) void { + switch (self.data) { + .block => |b| b.fmt(f), + .empty => f.writeChar(';'), + .debugger => f.write("debugger;"), + .expr => |e| { + e.expr.*.fmt(f); + f.writeChar(';'); + }, + .@"if" => |s| s.fmt(f), + .@"while" => |w| w.fmt(f), + .do_while => |d| d.fmt(f), + .@"for" => |fo| fo.fmt(f), + .for_in => |fi| fi.fmt(f), + .for_of => |fo| fo.fmt(f), + .@"continue" => |c| { + f.write("continue"); + if (c.label) |l| { + f.writeChar(' '); + f.write(l); + } + f.writeChar(';'); + }, + .@"break" => |b| { + f.write("break"); + if (b.label) |l| { + f.writeChar(' '); + f.write(l); + } + f.writeChar(';'); + }, + .@"return" => |r| { + f.write("return"); + if (r.arg) |a| { + f.writeChar(' '); + a.*.fmt(f); + } + f.writeChar(';'); + }, + .@"switch" => |s| s.fmt(f), + .throw => |t| { + f.write("throw "); + t.arg.*.fmt(f); + f.writeChar(';'); + }, + .@"try" => |t| t.fmt(f), + .labelled => |l| { + f.write(l.label); + f.write(": "); + l.body.*.fmt(f); + }, + .with => |w| { + f.write("with ("); + w.obj.*.fmt(f); + f.write(") "); + w.body.*.fmt(f); + }, + .variable => |v| v.fmt(f), + .lexical => |l| l.fmt(f), + .using => |u| { + f.write("using "); + for (u.decls, 0..) |d, i| { + if (i > 0) f.write(", "); + d.fmt(f); + } + f.writeChar(';'); + }, + .import => |i| i.fmt(f), + .@"export" => |e| e.fmt(f), + .class => |c| c.fmt(f), + .function => |d| d.fmt(f), + } + } }; pub const ExprType = union(enum) { @@ -97,6 +2103,127 @@ pub const ExprType = union(enum) { pub const Expr = struct { loc: Loc, data: ExprType, + + pub fn fmt(self: @This(), f: *Formatter) void { + switch (self.data) { + .identifier => |name| f.write(name), + .this => f.write("this"), + .super => f.write("super"), + .null => f.write("null"), + .bool => |b| f.write(if (b) "true" else "false"), + .number => |n| { + var buf: [64]u8 = undefined; + f.write(std.fmt.bufPrint(&buf, "{d}", .{n}) catch "0"); + }, + .string => |s| { + f.writeChar('"'); + f.write(s); + f.writeChar('"'); + }, + .regex => |r| { + f.writeChar('/'); + f.write(r.pattern); + f.writeChar('/'); + f.write(r.flags); + }, + .array => |a| a.fmt(f), + .object => |o| o.fmt(f), + .func => |fn_expr| fn_expr.fmt(f), + .arrow => |arrow| arrow.fmt(f), + .class => |c| c.fmt(f), + .template => |t| t.fmt(f), + .tagged_template => |tt| tt.fmt(f), + .member => |m| m.fmt(f), + .computed_member => |cm| cm.fmt(f), + .call => |call| call.fmt(f), + .new => |n| { + f.write("new "); + n.callee.*.fmt(f); + f.writeChar('('); + for (n.args, 0..) |a, i| { + if (i > 0) f.write(", "); + a.fmt(f); + } + f.writeChar(')'); + }, + .chain => |c| c.expr.*.fmt(f), + .unary => |u| { + if (u.prefix) { + f.write(@tagName(u.op)); + u.arg.*.fmt(f); + } else { + u.arg.*.fmt(f); + f.write(@tagName(u.op)); + } + }, + .binary => |b| { + f.writeChar('('); + b.left.*.fmt(f); + f.writeChar(' '); + f.write(@tagName(b.op)); + f.writeChar(' '); + b.right.*.fmt(f); + f.writeChar(')'); + }, + .update => |u| { + const s = if (u.op == .@"++") "++" else "--"; + if (u.prefix) { + f.write(s); + u.arg.*.fmt(f); + } else { + u.arg.*.fmt(f); + f.write(s); + } + }, + .conditional => |c| { + c.condition.*.fmt(f); + f.write(" ? "); + c.consequent.*.fmt(f); + f.write(" : "); + c.alternate.*.fmt(f); + }, + .assign => |a| { + a.left.*.fmt(f); + f.writeChar(' '); + f.write(@tagName(a.op)); + f.writeChar(' '); + a.right.*.fmt(f); + }, + .sequence => |s| { + for (s.exprs, 0..) |e, i| { + if (i > 0) f.write(", "); + e.fmt(f); + } + }, + .spread => |s| { + f.write("..."); + s.arg.*.fmt(f); + }, + .yield => |y| { + f.write("yield"); + if (y.delegate) f.write("*"); + if (y.arg) |a| { + f.writeChar(' '); + a.*.fmt(f); + } + }, + .await => |a| { + f.write("await "); + a.arg.*.fmt(f); + }, + .meta_prop => |m| { + f.write(m.meta); + f.writeChar('.'); + f.write(m.prop); + }, + .parenthesized => |p| { + f.writeChar('('); + p.*.fmt(f); + f.writeChar(')'); + }, + .private_ident => |name| f.write(name), + } + } }; pub const PatType = union(enum) { @@ -110,13 +2237,29 @@ pub const PatType = union(enum) { pub const Pat = struct { loc: Loc, data: PatType, + + pub fn fmt(self: @This(), f: *Formatter) void { + switch (self.data) { + .ident => |name| f.write(name), + .object => |o| o.fmt(f), + .array => |a| a.fmt(f), + .assign => |a| { + a.left.*.fmt(f); + f.write(" = "); + a.right.*.fmt(f); + }, + .rest => |r| { + f.write("..."); + r.arg.*.fmt(f); + }, + } + } }; pub const DeclType = union(enum) { @"fn": FnDecl, class: ClassDecl, @"var": VarDecl, - lexical: LexicalDecl, using: UsingDecl, import: ImportDecl, @"export": ExportDecl, @@ -140,6 +2283,32 @@ pub const LitType = union(enum) { pub const Lit = struct { loc: Loc, data: LitType, + + pub fn fmt(self: @This(), f: *Formatter) void { + switch (self.data) { + .null => f.write("null"), + .bool => |b| f.write(if (b) "true" else "false"), + .number => |n| { + var buf: [64]u8 = undefined; + f.write(std.fmt.bufPrint(&buf, "{d}", .{n}) catch "0"); + }, + .string => |s| { + f.writeChar('"'); + f.write(s); + f.writeChar('"'); + }, + .regex => |r| { + f.writeChar('/'); + f.write(r.pattern); + f.writeChar('/'); + f.write(r.flags); + }, + .bigint => |s| { + f.write(s); + f.writeChar('n'); + }, + } + } }; pub const Regex = struct { @@ -160,6 +2329,24 @@ pub const Prop = struct { loc: Loc, key: PropKey, data: PropType, + + pub fn fmt(self: @This(), f: *Formatter) void { + switch (self.data) { + .init => |p_init| { + self.key.fmt(f); + f.write(": "); + p_init.value.*.fmt(f); + }, + .get => |g| g.fmt(f), + .set => |s| s.fmt(f), + .shorthand => |name| f.write(name), + .spread => |sp| { + f.write("..."); + sp.arg.*.fmt(f); + }, + .method => |m| m.fmt(f), + } + } }; pub const PropKey = union(enum) { @@ -178,12 +2365,28 @@ pub const PropInit = struct { pub const PropGet = struct { key: PropKey, body: BlockStmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("get "); + self.key.fmt(f); + f.write("() "); + self.body.fmt(f); + } }; pub const PropSet = struct { key: PropKey, param: Pat, body: BlockStmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("set "); + self.key.fmt(f); + f.writeChar('('); + self.param.fmt(f); + f.write(") "); + self.body.fmt(f); + } }; pub const SpreadElem = struct { @@ -194,6 +2397,22 @@ pub const SpreadElem = struct { pub const BlockStmt = struct { loc: Loc, body: []const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('{'); + if (self.body.len > 0) { + f.newline(); + f.indent_level += 1; + for (self.body) |s| { + f.writeIndent(); + s.fmt(f); + f.newline(); + } + f.indent_level -= 1; + f.writeIndent(); + } + f.writeChar('}'); + } }; pub const ExprStmt = struct { @@ -206,50 +2425,120 @@ pub const IfStmt = struct { condition: *const Expr, consequent: *const Stmt, alternate: ?*const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("if ("); + self.condition.*.fmt(f); + f.write(") "); + self.consequent.*.fmt(f); + if (self.alternate) |alt| { + f.write(" else "); + alt.*.fmt(f); + } + } }; pub const WhileStmt = struct { loc: Loc, condition: *const Expr, body: *const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("while ("); + self.condition.*.fmt(f); + f.write(") "); + self.body.*.fmt(f); + } }; pub const DoWhileStmt = struct { loc: Loc, body: *const Stmt, condition: *const Expr, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("do "); + self.body.*.fmt(f); + f.write(" while ("); + self.condition.*.fmt(f); + f.write(");"); + } +}; + +pub const ForInit = union(enum) { + expr: *const Expr, + decl: *const Decl, }; pub const ForStmt = struct { loc: Loc, - init: ?union(enum) { - expr: *const Expr, - decl: *const Decl, - }, + init: ?ForInit, condition: ?*const Expr, update: ?*const Expr, body: *const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("for ("); + if (self.init) |init| { + switch (init) { + .expr => |e| e.*.fmt(f), + .decl => |d| d.*.fmt(f), + } + } + f.writeChar(';'); + if (self.condition) |c| { + f.writeChar(' '); + c.*.fmt(f); + } + f.writeChar(';'); + if (self.update) |u| { + f.writeChar(' '); + u.*.fmt(f); + } + f.write(") "); + self.body.*.fmt(f); + } }; pub const ForInStmt = struct { loc: Loc, - left: union(enum) { - expr: *const Expr, - decl: *const Decl, - }, + left: ForInit, right: *const Expr, body: *const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("for ("); + switch (self.left) { + .expr => |e| e.*.fmt(f), + .decl => |d| d.*.fmt(f), + } + f.write(" in "); + self.right.*.fmt(f); + f.write(") "); + self.body.*.fmt(f); + } }; pub const ForOfStmt = struct { loc: Loc, await_token: bool, - left: union(enum) { - expr: *const Expr, - decl: *const Decl, - }, + left: ForInit, right: *const Expr, body: *const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("for"); + if (self.await_token) f.write(" await"); + f.write(" ("); + switch (self.left) { + .expr => |e| e.*.fmt(f), + .decl => |d| d.*.fmt(f), + } + f.write(" of "); + self.right.*.fmt(f); + f.write(") "); + self.body.*.fmt(f); + } }; pub const ContinueStmt = struct { @@ -271,12 +2560,42 @@ pub const SwitchStmt = struct { loc: Loc, discriminant: *const Expr, cases: []const SwitchCase, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("switch ("); + self.discriminant.*.fmt(f); + f.write(") {"); + f.newline(); + f.indent_level += 1; + for (self.cases) |c| { + f.writeIndent(); + c.fmt(f); + f.newline(); + } + f.indent_level -= 1; + f.writeIndent(); + f.writeChar('}'); + } }; pub const SwitchCase = struct { loc: Loc, condition: ?*const Expr, consequent: []const Stmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + if (self.condition) |c| { + f.write("case "); + c.*.fmt(f); + } else { + f.write("default"); + } + f.writeChar(':'); + for (self.consequent) |s| { + f.writeChar(' '); + s.fmt(f); + } + } }; pub const ThrowStmt = struct { @@ -289,12 +2608,37 @@ pub const TryStmt = struct { block: BlockStmt, handler: ?CatchClause, finalizer: ?BlockStmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("try "); + self.block.fmt(f); + if (self.handler) |h| { + f.writeChar(' '); + h.fmt(f); + } + if (self.finalizer) |fin| { + f.write(" finally "); + fin.fmt(f); + } + } }; pub const CatchClause = struct { loc: Loc, param: ?Pat, body: BlockStmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("catch"); + if (self.param) |p| { + f.write(" ("); + p.fmt(f); + f.write(") "); + } else { + f.writeChar(' '); + } + self.body.fmt(f); + } }; pub const LabelledStmt = struct { @@ -313,11 +2657,29 @@ pub const VarStmt = struct { loc: Loc, kind: VarKind, decls: []const VarDeclarator, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write(@tagName(self.kind)); + f.writeChar(' '); + for (self.decls, 0..) |d, i| { + if (i > 0) f.write(", "); + d.fmt(f); + } + f.writeChar(';'); + } }; pub const UsingStmt = struct { loc: Loc, decls: []const VarDeclarator, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("using "); + for (self.decls, 0..) |d, i| { + if (i > 0) f.write(", "); + d.fmt(f); + } + } }; pub const VarKind = enum { @@ -336,6 +2698,16 @@ pub const LexicalDecl = struct { loc: Loc, kind: VarKind, decls: []const VarDeclarator, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write(@tagName(self.kind)); + f.writeChar(' '); + for (self.decls, 0..) |d, i| { + if (i > 0) f.write(", "); + d.fmt(f); + } + f.writeChar(';'); + } }; pub const UsingDecl = struct { @@ -348,12 +2720,47 @@ pub const VarDeclarator = struct { loc: Loc, id: Pat, init: ?*const Expr, + + pub fn fmt(self: @This(), f: *Formatter) void { + self.id.fmt(f); + if (self.init) |init| { + f.write(" = "); + init.*.fmt(f); + } + } }; pub const ObjectPat = struct { loc: Loc, props: []const PatProp, rest: ?*const Pat, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('{'); + var first = true; + for (self.props) |prop| { + if (!first) f.write(", "); + first = false; + switch (prop) { + .key_value => |kv| { + kv.key.fmt(f); + f.write(": "); + kv.value.*.fmt(f); + }, + .shorthand => |name| f.write(name), + .rest => |r| { + f.write("..."); + r.*.fmt(f); + }, + } + } + if (self.rest) |rest| { + if (!first) f.write(", "); + f.write("..."); + rest.*.fmt(f); + } + f.writeChar('}'); + } }; pub const PatProp = union(enum) { @@ -369,6 +2776,20 @@ pub const ArrayPat = struct { loc: Loc, elems: []const ?Pat, rest: ?*const Pat, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('['); + for (self.elems, 0..) |elem, i| { + if (i > 0) f.write(", "); + if (elem) |pat| pat.fmt(f); + } + if (self.rest) |rest| { + if (self.elems.len > 0) f.write(", "); + f.write("..."); + rest.*.fmt(f); + } + f.writeChar(']'); + } }; pub const AssignPat = struct { @@ -395,6 +2816,27 @@ pub const FnDecl = struct { id: ?[]const u8, params: []const Pat, body: BlockStmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("function"); + if (self.fn_type == .generator or self.fn_type == .async_generator) { + f.write("*"); + } + if (self.fn_type == .async or self.fn_type == .async_generator) { + f.write(" async"); + } + if (self.id) |id| { + f.writeChar(' '); + f.write(id); + } + f.writeChar('('); + for (self.params, 0..) |p, i| { + if (i > 0) f.write(", "); + p.fmt(f); + } + f.write(") "); + self.body.fmt(f); + } }; pub const FnExpr = struct { @@ -403,6 +2845,27 @@ pub const FnExpr = struct { id: ?[]const u8, params: []const Pat, body: BlockStmt, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("function"); + if (self.fn_type == .generator or self.fn_type == .async_generator) { + f.write("*"); + } + if (self.fn_type == .async or self.fn_type == .async_generator) { + f.write(" async"); + } + if (self.id) |id| { + f.writeChar(' '); + f.write(id); + } + f.writeChar('('); + for (self.params, 0..) |p, i| { + if (i > 0) f.write(", "); + p.fmt(f); + } + f.write(") "); + self.body.fmt(f); + } }; pub const ArrowFn = struct { @@ -413,6 +2876,20 @@ pub const ArrowFn = struct { block: BlockStmt, expr: *const Expr, }, + + pub fn fmt(self: @This(), f: *Formatter) void { + if (self.async_token) f.write("async "); + f.writeChar('('); + for (self.params, 0..) |p, i| { + if (i > 0) f.write(", "); + p.fmt(f); + } + f.write(") => "); + switch (self.body) { + .block => |b| b.fmt(f), + .expr => |e| e.*.fmt(f), + } + } }; pub const ClassDecl = struct { @@ -420,6 +2897,29 @@ pub const ClassDecl = struct { id: ?[]const u8, super_class: ?*const Expr, body: []const ClassElem, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("class"); + if (self.id) |id| { + f.writeChar(' '); + f.write(id); + } + if (self.super_class) |sc| { + f.write(" extends "); + sc.*.fmt(f); + } + f.write(" {"); + f.newline(); + f.indent_level += 1; + for (self.body) |elem| { + f.writeIndent(); + elem.fmt(f); + f.newline(); + } + f.indent_level -= 1; + f.writeIndent(); + f.writeChar('}'); + } }; pub const ClassExpr = struct { @@ -427,6 +2927,29 @@ pub const ClassExpr = struct { id: ?[]const u8, super_class: ?*const Expr, body: []const ClassElem, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("class"); + if (self.id) |id| { + f.writeChar(' '); + f.write(id); + } + if (self.super_class) |sc| { + f.write(" extends "); + sc.*.fmt(f); + } + f.write(" {"); + f.newline(); + f.indent_level += 1; + for (self.body) |elem| { + f.writeIndent(); + elem.fmt(f); + f.newline(); + } + f.indent_level -= 1; + f.writeIndent(); + f.writeChar('}'); + } }; pub const ClassElem = union(enum) { @@ -452,6 +2975,26 @@ pub const ImportDecl = struct { specifiers: []const ImportSpecifier, source: []const u8, attributes: []const ImportAttribute, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("import "); + if (self.specifiers.len > 0) { + for (self.specifiers, 0..) |spec, j| { + if (j > 0) f.write(", "); + spec.fmt(f); + } + f.write(" from "); + } + f.writeChar('"'); + f.write(self.source); + f.writeChar('"'); + for (self.attributes) |a| { + f.write(" /* "); + a.fmt(f); + f.write(" */"); + } + f.writeChar(';'); + } }; pub const ImportSpecifier = union(enum) { @@ -463,6 +3006,13 @@ pub const ImportSpecifier = union(enum) { pub const ImportAttribute = struct { key: []const u8, value: []const u8, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write(self.key); + f.write(": \""); + f.write(self.value); + f.writeChar('"'); + } }; pub const ExportDecl = struct { @@ -470,27 +3020,82 @@ pub const ExportDecl = struct { declaration: ?*const Decl, specifiers: ?[]const ExportSpecifier, source: ?[]const u8, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("export "); + if (self.declaration) |d| { + d.*.fmt(f); + } else if (self.specifiers) |s| { + f.writeChar('{'); + for (s, 0..) |sp, i| { + if (i > 0) f.write(", "); + sp.fmt(f); + } + f.writeChar('}'); + if (self.source) |src| { + f.write(" from \""); + f.write(src); + f.writeChar('"'); + } + f.writeChar(';'); + } else if (self.source) |src| { + f.write("* from \""); + f.write(src); + f.writeChar("\";"); + } + } }; pub const ExportSpecifier = struct { loc: Loc, exported: []const u8, local: []const u8, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write(self.local); + if (!std.mem.eql(u8, self.local, self.exported)) { + f.write(" as "); + f.write(self.exported); + } + } }; pub const ExportDefault = struct { loc: Loc, declaration: *const Decl, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.write("export default "); + self.declaration.*.fmt(f); + } }; pub const ArrayExpr = struct { loc: Loc, elems: []const ?Expr, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('['); + for (self.elems, 0..) |elem, i| { + if (i > 0) f.write(", "); + if (elem) |e| e.fmt(f); + } + f.writeChar(']'); + } }; pub const ObjectExpr = struct { loc: Loc, props: []const Prop, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('{'); + for (self.props, 0..) |p, i| { + if (i > 0) f.write(", "); + p.fmt(f); + } + f.writeChar('}'); + } }; pub const MemberExpr = struct { @@ -500,12 +3105,33 @@ pub const MemberExpr = struct { ident: []const u8, private: []const u8, }, + + pub fn fmt(self: @This(), f: *Formatter) void { + self.obj.*.fmt(f); + switch (self.prop) { + .ident => |name| { + f.writeChar('.'); + f.write(name); + }, + .private => |name| { + f.writeChar('.'); + f.write(name); + }, + } + } }; pub const ComputedMember = struct { loc: Loc, obj: *const Expr, expr: *const Expr, + + pub fn fmt(self: @This(), f: *Formatter) void { + self.obj.*.fmt(f); + f.writeChar('['); + self.expr.*.fmt(f); + f.writeChar(']'); + } }; pub const CallExpr = struct { @@ -513,6 +3139,20 @@ pub const CallExpr = struct { callee: *const Expr, args: []const Expr, optional: bool, + + pub fn fmt(self: @This(), f: *Formatter) void { + self.callee.*.fmt(f); + if (self.optional) { + f.write("?.("); + } else { + f.writeChar('('); + } + for (self.args, 0..) |a, i| { + if (i > 0) f.write(", "); + a.fmt(f); + } + f.writeChar(')'); + } }; pub const NewExpr = struct { @@ -628,11 +3268,6 @@ pub const SeqExpr = struct { exprs: []const Expr, }; -pub const SpreadElem = struct { - loc: Loc, - arg: *const Expr, -}; - pub const YieldExpr = struct { loc: Loc, arg: ?*const Expr, @@ -654,6 +3289,21 @@ pub const TemplateLit = struct { loc: Loc, quasis: []const TemplateElem, exprs: []const Expr, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('`'); + var ei: usize = 0; + for (self.quasis, 0..) |q, i| { + if (i > 0 and ei < self.exprs.len) { + f.write("${"); + self.exprs[ei].fmt(f); + f.writeChar('}'); + ei += 1; + } + f.write(q.value); + } + f.writeChar('`'); + } }; pub const TemplateElem = struct { @@ -666,9 +3316,19 @@ pub const TaggedTemplate = struct { loc: Loc, tag: *const Expr, quasi: TemplateLit, + + pub fn fmt(self: @This(), f: *Formatter) void { + self.tag.*.fmt(f); + self.quasi.fmt(f); + } }; pub const Decorator = struct { loc: Loc, expr: *const Expr, + + pub fn fmt(self: @This(), f: *Formatter) void { + f.writeChar('@'); + self.expr.*.fmt(f); + } }; diff --git a/src/z/js/parser/lexical_grammar.zig b/src/z/js/parser/lexical_grammar.zig index bd54e8f..2211fbe 100644 --- a/src/z/js/parser/lexical_grammar.zig +++ b/src/z/js/parser/lexical_grammar.zig @@ -1,9 +1,7 @@ const std = @import("std"); -pub const Loc = struct { - start: usize, - end: usize, -}; +const ast = @import("ast.zig"); +pub const Loc = ast.Loc; pub const TokenType = enum { // Keywords @@ -47,6 +45,11 @@ pub const TokenType = enum { with, yield, using, + async, + static, + get, + set, + of, // Identifiers and literals identifier, @@ -122,6 +125,7 @@ pub const TokenType = enum { @"#", // Special + from, eof, unknown, @@ -167,6 +171,12 @@ pub const TokenType = enum { .with, .yield, .using, + .async, + .static, + .get, + .set, + .of, + .from, => true, else => false, }; @@ -175,6 +185,10 @@ pub const TokenType = enum { pub fn isIdentifier(self: TokenType) bool { return self == .identifier or self.isKeyword(); } + + pub fn isKeywordButNotLet(self: TokenType) bool { + return self.isKeyword() and self != .let; + } }; pub const Token = struct { @@ -194,8 +208,13 @@ pub const Lexer = struct { } pub fn tokenize(source: []const u8) []const Token { + var buf: [4096]Token = undefined; + return tokenizeBuf(source, &buf); + } + + pub fn tokenizeBuf(source: []const u8, tokens: *[4096]Token) []const Token { + @setEvalBranchQuota(100000); var lexer = Self.init(source); - var tokens: [4096]Token = undefined; var count: usize = 0; while (true) { @@ -237,7 +256,6 @@ pub const Lexer = struct { fn isLineTerminator(c: u8) bool { return switch (c) { '\n', '\r' => true, - 0x2028, 0x2029 => true, else => false, }; } @@ -291,11 +309,13 @@ pub const Lexer = struct { self.advance(); self.advance(); while (self.ch()) |c| { - if (c == '*' and self.chAt(1)) |n| { - if (n == '/') { - self.advance(); - self.advance(); - return; + if (c == '*') { + if (self.chAt(1)) |n| { + if (n == '/') { + self.advance(); + self.advance(); + return; + } } } self.advance(); @@ -309,8 +329,8 @@ pub const Lexer = struct { if (self.ch()) |n| { if (n == '!') { self.advance(); - while (self.ch()) |ch| { - if (isLineTerminator(ch)) break; + while (self.ch()) |c2| { + if (isLineTerminator(c2)) break; self.advance(); } } @@ -367,15 +387,17 @@ pub const Lexer = struct { .slice = self.source[start .. self.pos - 1], }; } - if (c == '$' and self.chAt(1)) |n| { - if (n == '{') { - self.advance(); - self.advance(); - return .{ - .kind = .template_head, - .loc = .{ .start = start, .end = self.pos }, - .slice = self.source[start .. self.pos - 2], - }; + if (c == '$') { + if (self.chAt(1)) |n| { + if (n == '{') { + self.advance(); + self.advance(); + return .{ + .kind = .template_head, + .loc = .{ .start = start, .end = self.pos }, + .slice = self.source[start .. self.pos - 2], + }; + } } } if (c == '\\') { @@ -406,15 +428,17 @@ pub const Lexer = struct { .slice = self.source[start .. self.pos - 1], }; } - if (c == '$' and self.chAt(1)) |n| { - if (n == '{') { - self.advance(); - self.advance(); - return .{ - .kind = .template_middle, - .loc = .{ .start = start, .end = self.pos }, - .slice = self.source[start .. self.pos - 2], - }; + if (c == '$') { + if (self.chAt(1)) |n| { + if (n == '{') { + self.advance(); + self.advance(); + return .{ + .kind = .template_middle, + .loc = .{ .start = start, .end = self.pos }, + .slice = self.source[start .. self.pos - 2], + }; + } } } if (c == '\\') { @@ -764,11 +788,13 @@ pub const Lexer = struct { '.' => { self.advance(); if (self.ch()) |n| { - if (n == '.' and self.chAt(1)) |n2| { - if (n2 == '.') { - self.advance(); - self.advance(); - return simple(.@"...", start); + if (n == '.') { + if (self.chAt(1)) |n2| { + if (n2 == '.') { + self.advance(); + self.advance(); + return simple(.@"...", start); + } } } } @@ -1019,23 +1045,23 @@ pub const Lexer = struct { } fn simple(kind: TokenType, start: usize) Token { - _ = start; - return .{ .kind = kind, .loc = undefined, .slice = "" }; + return .{ .kind = kind, .loc = .{ .start = start, .end = start }, .slice = "" }; } fn simpleWithSlice(comptime kind: TokenType, start: usize, slice: []const u8) Token { - return .{ .kind = kind, .loc = undefined, .slice = slice }; + return .{ .kind = kind, .loc = .{ .start = start, .end = start }, .slice = slice }; } pub fn next(self: *Self) Token { - const start = self.pos; if (self.ch()) |c| { _ = c; // Handle hashbang at beginning - if (self.pos == 0 and self.ch()) |c2| { - if (c2 == '#') { - self.skipHashbang(); - return self.next(); + if (self.pos == 0) { + if (self.ch()) |c2| { + if (c2 == '#') { + self.skipHashbang(); + return self.next(); + } } } return self.scanPunctuator(); @@ -1044,50 +1070,62 @@ pub const Lexer = struct { } fn keywordFromString(s: []const u8) TokenType { - const keywords = std.ComptimeStringMap(TokenType, .{ - .{ "await", .await }, - .{ "break", .@"break" }, - .{ "case", .case }, - .{ "catch", .@"catch" }, - .{ "class", .class }, - .{ "const", .@"const" }, - .{ "continue", .@"continue" }, - .{ "debugger", .debugger }, - .{ "default", .default }, - .{ "delete", .delete }, - .{ "do", .do }, - .{ "else", .@"else" }, - .{ "enum", .@"enum" }, - .{ "export", .@"export" }, - .{ "extends", .extends }, - .{ "false", .false }, - .{ "finally", .finally }, - .{ "for", .@"for" }, - .{ "function", .function }, - .{ "if", .@"if" }, - .{ "import", .import }, - .{ "in", .in }, - .{ "instanceof", .instanceof }, - .{ "let", .let }, - .{ "new", .new }, - .{ "null", .null }, - .{ "return", .@"return" }, - .{ "super", .super }, - .{ "switch", .@"switch" }, - .{ "this", .this }, - .{ "throw", .throw }, - .{ "true", .true }, - .{ "try", .@"try" }, - .{ "typeof", .typeof }, - .{ "var", .@"var" }, - .{ "void", .void }, - .{ "while", .@"while" }, - .{ "with", .with }, - .{ "yield", .yield }, - .{ "using", .using }, - }); - - if (keywords.get(s)) |k| return k; + const keywords = comptime keywords: { + const arr = struct { + const data = [_]struct { key: []const u8, val: TokenType }{ + .{ .key = "await", .val = .await }, + .{ .key = "break", .val = .@"break" }, + .{ .key = "case", .val = .case }, + .{ .key = "catch", .val = .@"catch" }, + .{ .key = "class", .val = .class }, + .{ .key = "const", .val = .@"const" }, + .{ .key = "continue", .val = .@"continue" }, + .{ .key = "debugger", .val = .debugger }, + .{ .key = "default", .val = .default }, + .{ .key = "delete", .val = .delete }, + .{ .key = "do", .val = .do }, + .{ .key = "else", .val = .@"else" }, + .{ .key = "enum", .val = .@"enum" }, + .{ .key = "export", .val = .@"export" }, + .{ .key = "extends", .val = .extends }, + .{ .key = "false", .val = .false }, + .{ .key = "finally", .val = .finally }, + .{ .key = "for", .val = .@"for" }, + .{ .key = "function", .val = .function }, + .{ .key = "if", .val = .@"if" }, + .{ .key = "import", .val = .import }, + .{ .key = "in", .val = .in }, + .{ .key = "instanceof", .val = .instanceof }, + .{ .key = "let", .val = .let }, + .{ .key = "new", .val = .new }, + .{ .key = "null", .val = .null }, + .{ .key = "return", .val = .@"return" }, + .{ .key = "super", .val = .super }, + .{ .key = "switch", .val = .@"switch" }, + .{ .key = "this", .val = .this }, + .{ .key = "throw", .val = .throw }, + .{ .key = "true", .val = .true }, + .{ .key = "try", .val = .@"try" }, + .{ .key = "typeof", .val = .typeof }, + .{ .key = "var", .val = .@"var" }, + .{ .key = "void", .val = .void }, + .{ .key = "while", .val = .@"while" }, + .{ .key = "with", .val = .with }, + .{ .key = "yield", .val = .yield }, + .{ .key = "using", .val = .using }, + .{ .key = "async", .val = .async }, + .{ .key = "static", .val = .static }, + .{ .key = "get", .val = .get }, + .{ .key = "set", .val = .set }, + .{ .key = "of", .val = .of }, + .{ .key = "from", .val = .from }, + }; + }; + break :keywords arr.data; + }; + inline for (keywords) |entry| { + if (std.mem.eql(u8, s, entry.key)) return entry.val; + } return .identifier; } diff --git a/src/z/js/parser/parser.zig b/src/z/js/parser/parser.zig index f269401..050f8c1 100644 --- a/src/z/js/parser/parser.zig +++ b/src/z/js/parser/parser.zig @@ -1,7 +1,7 @@ const std = @import("std"); -const mod = @import("root.zig"); -const Result = mod.Result; +const root = @import("root.zig"); +const Result = root.Result; pub fn Parser(T: type) type { const R = Result(T); @@ -66,52 +66,3 @@ pub fn Parser(T: type) type { } }; } - -test "variants" { - const Char = union(enum) { - upper: u8, - lower: u8, - space: void, - }; - - const parser: Parser(Char) = .variants(struct { - const upper: Parser(u8) = .any(void, &.{ - .{ mod.builtin.literal("A"), 'A' }, - .{ mod.builtin.literal("B"), 'B' }, - }); - - const lower: Parser(u8) = .any(void, &.{ - .{ mod.builtin.literal("a"), 'a' }, - .{ mod.builtin.literal("b"), 'b' }, - }); - - const space: Parser(void) = mod.builtin.literal(" "); - }); - - { - const result = try parser.parseAll("a"); - try std.testing.expect(result.lower == 'a'); - } - - { - const result = try parser.parseAll("b"); - try std.testing.expect(result.lower == 'b'); - } - - { - const result = try parser.parseAll("A"); - try std.testing.expect(result.upper == 'A'); - } - - { - const result = try parser.parseAll("B"); - try std.testing.expect(result.upper == 'B'); - } - - { - const result = try parser.parseAll(" "); - try std.testing.expect(result == .space); - } - - try std.testing.expect(parser.parseAll("c") == error.Variants); -} 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 { diff --git a/src/z/js/parser/syntactic_grammar.zig b/src/z/js/parser/syntactic_grammar.zig deleted file mode 100644 index 065e827..0000000 --- a/src/z/js/parser/syntactic_grammar.zig +++ /dev/null @@ -1,1907 +0,0 @@ -const std = @import("std"); - -const lex = @import("lexical_grammar.zig"); -const Token = lex.Token; -const TokenType = lex.TokenType; -const Loc = lex.Loc; - -const ast = @import("ast.zig"); -const Expr = ast.Expr; -const ExprType = ast.ExprType; -const Stmt = ast.Stmt; -const StmtType = ast.StmtType; -const Pat = ast.Pat; -const PatType = ast.PatType; -const Decl = ast.Decl; -const DeclType = ast.DeclType; -const Lit = ast.Lit; -const LitType = ast.LitType; - -pub const ParseError = error{ - UnexpectedToken, - ExpectedToken, - ExpectedExpression, - ExpectedStatement, - ExpectedIdentifier, - UnclosedString, - InvalidNumber, - InvalidEscape, - UnterminatedComment, - ExpectedParameterName, - ExpectedPropertyName, - UnterminatedTemplate, - InvalidAssignmentTarget, - TooManyTokens, -}; - -const BP = struct { - const comma = 1; - const assign = 2; - const yield = 3; - const conditional = 4; - const coalesce = 5; - const log_or = 6; - const log_and = 7; - const bit_or = 8; - const bit_xor = 9; - const bit_and = 10; - const equality = 11; - const relational = 12; - const shift = 13; - const additive = 14; - const multiplicative = 15; - const exponentiation = 16; - const unary = 17; - const update = 18; - const call = 19; - const member = 20; -}; - -pub const NodeArena = struct { - exprs: [8192]Expr = undefined, - expr_count: usize = 0, - stmts: [8192]Stmt = undefined, - stmt_count: usize = 0, - decls: [1024]Decl = undefined, - decl_count: usize = 0, - pats: [1024]Pat = undefined, - pat_count: usize = 0, - lits: [1024]Lit = undefined, - lit_count: usize = 0, - string_buf: [65536]u8 = undefined, - string_pos: usize = 0, - - fn exprPtr(self: *NodeArena, data: ExprType, loc: Loc) *const Expr { - const idx = self.expr_count; - self.exprs[idx] = .{ .loc = loc, .data = data }; - self.expr_count += 1; - return &self.exprs[idx]; - } - - fn stmtPtr(self: *NodeArena, data: StmtType, loc: Loc) *const Stmt { - const idx = self.stmt_count; - self.stmts[idx] = .{ .loc = loc, .data = data }; - self.stmt_count += 1; - return &self.stmts[idx]; - } - - fn declPtr(self: *NodeArena, data: DeclType, loc: Loc) *const Decl { - const idx = self.decl_count; - self.decls[idx] = .{ .loc = loc, .data = data }; - self.decl_count += 1; - return &self.decls[idx]; - } - - fn patPtr(self: *NodeArena, data: PatType, loc: Loc) *const Pat { - const idx = self.pat_count; - self.pats[idx] = .{ .loc = loc, .data = data }; - self.pat_count += 1; - return &self.pats[idx]; - } - - fn litPtr(self: *NodeArena, data: LitType, loc: Loc) *const Lit { - const idx = self.lit_count; - self.lits[idx] = .{ .loc = loc, .data = data }; - self.lit_count += 1; - return &self.lits[idx]; - } -}; - -pub const Parser = struct { - tokens: []const Token, - pos: usize, - source: []const u8, - arena: *NodeArena, - - pub fn init(tokens: []const Token, source: []const u8, arena: *NodeArena) @This() { - return .{ .tokens = tokens, .pos = 0, .source = source, .arena = arena }; - } - - fn tok(self: *@This()) Token { - return self.tokens[self.pos]; - } - - fn peek(self: *@This()) TokenType { - return self.tokens[self.pos].kind; - } - - fn peekAt(self: *@This(), offset: usize) TokenType { - const idx = self.pos + offset; - if (idx >= self.tokens.len) return .eof; - return self.tokens[idx].kind; - } - - fn advance(self: *@This()) Token { - const t = self.tokens[self.pos]; - self.pos += 1; - return t; - } - - fn expect(self: *@This(), kind: TokenType) ParseError!Token { - if (self.peek() != kind) return ParseError.ExpectedToken; - return self.advance(); - } - - fn match(self: *@This(), kind: TokenType) bool { - if (self.peek() == kind) { - self.advance(); - return true; - } - return false; - } - - fn matchKeyword(self: *@This(), kw: TokenType) bool { - if (self.peek() == kw) { - self.advance(); - return true; - } - return false; - } - - fn isLineTerminatorBeforeNext(self: *@This()) bool { - if (self.pos == 0) return false; - const prev_token = self.tokens[self.pos - 1]; - const next_token = self.tokens[self.pos]; - // Check if there's a line terminator between prev and next tokens - var i = prev_token.loc.end; - while (i < next_token.loc.start) : (i += 1) { - if (i >= self.source.len) break; - const c = self.source[i]; - switch (c) { - '\n', '\r', 0x2028, 0x2029 => return true, - else => {}, - } - } - return false; - } - - fn as_identifier(self: *@This(), tok: Token) []const u8 { - return self.source[tok.loc.start..tok.loc.end]; - } - - fn tokenSlice(self: *@This(), tok: Token) []const u8 { - return self.source[tok.loc.start..tok.loc.end]; - } - - pub fn parseProgram(self: *@This()) ParseError!ast.Program { - var stmts: [2048]Stmt = undefined; - var count: usize = 0; - - while (self.peek() != .eof) { - const item = try self.parseStatementListItem(); - stmts[count] = item; - count += 1; - if (count >= stmts.len) return ParseError.TooManyTokens; - } - - const body = stmts[0..count]; - const loc: Loc = if (body.len > 0) .{ .start = body[0].loc.start, .end = body[body.len - 1].loc.end } else .{ .start = 0, .end = 0 }; - - return .{ .body = body, .loc = loc }; - } - - fn parseStatementListItem(self: *@This()) ParseError!Stmt { - switch (self.peek()) { - .function => return (try self.parseFunctionDecl(.normal, .stmt)).?, - .async => { - if (self.peekAt(1) == .function) { - return (try self.parseFunctionDecl(.async, .stmt)).?; - } - if (self.peekAt(1) == .@"*" and self.peekAt(2) == .function) { - return (try self.parseFunctionDecl(.async_generator, .stmt)).?; - } - return self.parseStatement(); - }, - .class => return (try self.parseClassDecl(.stmt)).?, - .@"const", .let => return self.parseLexicalDecl(), - .@"var" => return self.parseVarStmt(), - .using => return self.parseUsingStmt(), - .import => return try self.parseImport(), - .@"export" => return try self.parseExport(), - else => return self.parseStatement(), - } - } - - fn parseStatement(self: *@This()) ParseError!Stmt { - switch (self.peek()) { - .lbrace => return self.parseBlock(), - .semicolon => { - const t = self.advance(); - return Stmt{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .data = .{ .empty = {} } }; - }, - .@"if" => return self.parseIf(), - .@"while" => return self.parseWhile(), - .do => return self.parseDoWhile(), - .@"for" => return self.parseFor(), - .@"continue" => return self.parseContinue(), - .@"break" => return self.parseBreak(), - .@"return" => return self.parseReturn(), - .@"switch" => return self.parseSwitch(), - .throw => return self.parseThrow(), - .@"try" => return self.parseTry(), - .debugger => { - const t = self.advance(); - self.semicolon(); - return Stmt{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .data = .{ .debugger = {} } }; - }, - .with => return self.parseWith(), - .function => return (try self.parseFunctionDecl(.normal, .stmt)).?, - .async => { - if (self.peekAt(1) == .function) { - return (try self.parseFunctionDecl(.async, .stmt)).?; - } - if (self.peekAt(1) == .@"*") return self.parseExpressionStatement(); - if (self.peekAt(1).isIdentifier()) { - return self.parseExpressionStatement(); - } - return self.parseExpressionStatement(); - }, - .class => return (try self.parseClassDecl(.stmt)).?, - .@"const", .let => return self.parseLexicalDecl(), - .@"var" => return self.parseVarStmt(), - .import => return try self.parseImport(), - .@"export" => return try self.parseExport(), - else => return self.parseExpressionStatement(), - } - } - - fn parseBlock(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - var stmts: [1024]Stmt = undefined; - var count: usize = 0; - - while (self.peek() != .rbrace and self.peek() != .eof) { - const s = try self.parseStatementListItem(); - stmts[count] = s; - count += 1; - if (count >= stmts.len) return ParseError.TooManyTokens; - } - - const end_tok = try self.expect(.rbrace); - const end = end_tok.loc.end; - - const block = Stmt{ - .loc = .{ .start = start, .end = end }, - .data = .{ .block = .{ .loc = .{ .start = start, .end = end }, .body = stmts[0..count] } }, - }; - return block; - } - - fn parseIf(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - try self.expect(.lparen); - const condition = try self.parseExpr(0); - try self.expect(.rparen); - const consequent = try self.statementPointer(); - var alternate: ?*const Stmt = null; - if (self.matchKeyword(.@"else")) { - alternate = try self.statementPointer(); - } - const end = if (alternate) |a| a.loc.end else consequent.loc.end; - return Stmt{ - .loc = .{ .start = start, .end = end }, - .data = .{ .@"if" = .{ .loc = .{ .start = start, .end = end }, .condition = condition, .consequent = consequent, .alternate = alternate } }, - }; - } - - fn parseWhile(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - try self.expect(.lparen); - const condition = try self.parseExpr(0); - try self.expect(.rparen); - const body = try self.statementPointer(); - return Stmt{ - .loc = .{ .start = start, .end = body.loc.end }, - .data = .{ .@"while" = .{ .loc = .{ .start = start, .end = body.loc.end }, .condition = condition, .body = body } }, - }; - } - - fn parseDoWhile(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - const body = try self.statementPointer(); - try self.expect(.@"while"); - try self.expect(.lparen); - const condition = try self.parseExpr(0); - try self.expect(.rparen); - self.semicolon(); - return Stmt{ - .loc = .{ .start = start, .end = condition.loc.end }, - .data = .{ .do_while = .{ .loc = .{ .start = start, .end = condition.loc.end }, .body = body, .condition = condition } }, - }; - } - - fn parseFor(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - try self.expect(.lparen); - - var init: ?union(enum) { expr: *const Expr, decl: *const Decl } = null; - var condition: ?*const Expr = null; - var update: ?*const Expr = null; - - if (self.peek() != .semicolon) { - if (self.peek() == .@"var" or self.peek() == .let or self.peek() == .@"const") { - const decl = try self.parseForDeclaration(); - init = .{ .decl = decl }; - } else { - const expr = try self.parseExpr(0); - init = .{ .expr = expr }; - } - } - - if (self.matchKeyword(.in)) { - const right = try self.parseExpr(0); - try self.expect(.rparen); - const body = try self.statementPointer(); - return Stmt{ - .loc = .{ .start = start, .end = body.loc.end }, - .data = .{ .for_in = .{ .loc = .{ .start = start, .end = body.loc.end }, .left = init.?, .right = right, .body = body } }, - }; - } - - if (self.matchKeyword(.of)) { - const right = try self.parseExpr(0); - try self.expect(.rparen); - const body = try self.statementPointer(); - return Stmt{ - .loc = .{ .start = start, .end = body.loc.end }, - .data = .{ .for_of = .{ .loc = .{ .start = start, .end = body.loc.end }, .await_token = false, .left = init.?, .right = right, .body = body } }, - }; - } - - try self.expect(.semicolon); - if (self.peek() != .semicolon and self.peek() != .rparen) { - condition = try self.parseExpr(0); - } - try self.expect(.semicolon); - if (self.peek() != .rparen) { - update = try self.parseExpr(0); - } - try self.expect(.rparen); - const body = try self.statementPointer(); - return Stmt{ - .loc = .{ .start = start, .end = body.loc.end }, - .data = .{ .@"for" = .{ .loc = .{ .start = start, .end = body.loc.end }, .init = init, .condition = condition, .update = update, .body = body } }, - }; - } - - fn parseForDeclaration(self: *@This()) ParseError!*const Decl { - const kind_tok = self.advance(); - var decls: [128]ast.VarDeclarator = undefined; - var count: usize = 0; - - while (true) { - const pat = try self.parseBindingPattern(); - var init: ?*const Expr = null; - if (self.match(.@"=")) { - init = try self.parseExpr(0); - } - decls[count] = .{ .loc = pat.loc, .id = pat, .init = init }; - count += 1; - if (!self.match(.comma)) break; - } - - const kind: ast.VarKind = switch (kind_tok.kind) { - .@"var" => .@"var", - .let => .let, - .@"const" => .@"const", - else => return ParseError.UnexpectedToken, - }; - - return self.arena.declPtr(.{ .@"var" = .{ .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, .kind = kind, .decls = decls[0..count] } }, .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }); - } - - fn parseContinue(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - var label: ?[]const u8 = null; - if (self.peek() == .identifier and !self.isLineTerminatorBeforeNext()) { - const t = self.advance(); - label = self.tokenSlice(t); - } - self.semicolon(); - return Stmt{ .loc = .{ .start = start, .end = start + 8 }, .data = .{ .@"continue" = .{ .loc = .{ .start = start, .end = start + 8 }, .label = label } } }; - } - - fn parseBreak(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - var label: ?[]const u8 = null; - if (self.peek() == .identifier and !self.isLineTerminatorBeforeNext()) { - const t = self.advance(); - label = self.tokenSlice(t); - } - self.semicolon(); - return Stmt{ .loc = .{ .start = start, .end = start + 5 }, .data = .{ .@"break" = .{ .loc = .{ .start = start, .end = start + 5 }, .label = label } } }; - } - - fn parseReturn(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - var arg: ?*const Expr = null; - if (self.peek() != .semicolon and self.peek() != .rbrace and self.peek() != .eof and !self.isLineTerminatorBeforeNext()) { - arg = try self.parseExpr(0); - } - self.semicolon(); - return Stmt{ .loc = .{ .start = start, .end = arg.?.loc.end }, .data = .{ .@"return" = .{ .loc = .{ .start = start, .end = if (arg) |a| a.loc.end else start + 6 }, .arg = arg } } }; - } - - fn parseSwitch(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - try self.expect(.lparen); - const discriminant = try self.parseExpr(0); - try self.expect(.rparen); - try self.expect(.lbrace); - - var cases: [256]ast.SwitchCase = undefined; - var case_count: usize = 0; - - while (self.peek() == .case or self.peek() == .default) { - const case_start = self.tok().loc.start; - if (self.matchKeyword(.case)) { - const case_test = try self.parseExpr(0); - try self.expect(.@":"); - var consequent: [128]Stmt = undefined; - var cons_count: usize = 0; - while (self.peek() != .case and self.peek() != .default and self.peek() != .rbrace and self.peek() != .eof) { - const s = try self.parseStatementListItem(); - consequent[cons_count] = s; - cons_count += 1; - } - cases[case_count] = .{ .loc = .{ .start = case_start, .end = self.tok().loc.start }, .condition = case_test, .consequent = consequent[0..cons_count] }; - case_count += 1; - } else if (self.matchKeyword(.default)) { - try self.expect(.@":"); - var consequent: [128]Stmt = undefined; - var cons_count: usize = 0; - while (self.peek() != .case and self.peek() != .default and self.peek() != .rbrace and self.peek() != .eof) { - const s = try self.parseStatementListItem(); - consequent[cons_count] = s; - cons_count += 1; - } - cases[case_count] = .{ .loc = .{ .start = case_start, .end = self.tok().loc.start }, .condition = null, .consequent = consequent[0..cons_count] }; - case_count += 1; - } - } - - const end_tok = try self.expect(.rbrace); - return Stmt{ - .loc = .{ .start = start, .end = end_tok.loc.end }, - .data = .{ .@"switch" = .{ .loc = .{ .start = start, .end = end_tok.loc.end }, .discriminant = discriminant, .cases = cases[0..case_count] } }, - }; - } - - fn parseThrow(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - if (self.isLineTerminatorBeforeNext()) return ParseError.ExpectedExpression; - const arg = try self.parseExpr(0); - self.semicolon(); - return Stmt{ .loc = .{ .start = start, .end = arg.loc.end }, .data = .{ .throw = .{ .loc = .{ .start = start, .end = arg.loc.end }, .arg = arg } } }; - } - - fn parseTry(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - const block = try self.parseBlockStmt(); - var handler: ?ast.CatchClause = null; - var finalizer: ?ast.BlockStmt = null; - - if (self.matchKeyword(.@"catch")) { - var param: ?Pat = null; - if (self.match(.lparen)) { - const pat = try self.parseBindingPattern(); - param = pat; - try self.expect(.rparen); - } - const catch_body = try self.parseBlockStmt(); - handler = .{ .loc = .{ .start = block.loc.start, .end = catch_body.loc.end }, .param = param, .body = catch_body }; - } - - if (self.matchKeyword(.finally)) { - const finally_body = try self.parseBlockStmt(); - finalizer = finally_body; - } - - const end = if (finalizer) |f| f.loc.end else if (handler) |h| h.loc.end else block.loc.end; - return Stmt{ - .loc = .{ .start = start, .end = end }, - .data = .{ .@"try" = .{ .loc = .{ .start = start, .end = end }, .block = block, .handler = handler, .finalizer = finalizer } }, - }; - } - - fn parseWith(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - try self.expect(.lparen); - const obj = try self.parseExpr(0); - try self.expect(.rparen); - const body = try self.statementPointer(); - return Stmt{ - .loc = .{ .start = start, .end = body.loc.end }, - .data = .{ .with = .{ .loc = .{ .start = start, .end = body.loc.end }, .obj = obj, .body = body } }, - }; - } - - fn parseBlockStmt(self: *@This()) ParseError!ast.BlockStmt { - const start = try self.expect(.lbrace); - var stmts: [1024]Stmt = undefined; - var count: usize = 0; - - while (self.peek() != .rbrace and self.peek() != .eof) { - const s = try self.parseStatementListItem(); - stmts[count] = s; - count += 1; - if (count >= stmts.len) return ParseError.TooManyTokens; - } - - const end = try self.expect(.rbrace); - return .{ .loc = .{ .start = start.loc.start, .end = end.loc.end }, .body = stmts[0..count] }; - } - - fn parseVarStmt(self: *@This()) ParseError!Stmt { - const kind_tok = self.advance(); - var decls: [128]ast.VarDeclarator = undefined; - var count: usize = 0; - - while (true) { - const pat = try self.parseBindingPattern(); - var init: ?*const Expr = null; - if (self.match(.@"=")) { - init = try self.parseExpr(0); - } - decls[count] = .{ .loc = pat.loc, .id = pat, .init = init }; - count += 1; - if (!self.match(.comma)) break; - } - - self.semicolon(); - const kind: ast.VarKind = .@"var"; - return Stmt{ - .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, - .data = .{ .variable = .{ .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, .kind = kind, .decls = decls[0..count] } }, - }; - } - - fn parseLexicalDecl(self: *@This()) ParseError!Stmt { - const kind_tok = self.advance(); - var decls: [128]ast.VarDeclarator = undefined; - var count: usize = 0; - - while (true) { - const pat = try self.parseBindingPattern(); - var init: ?*const Expr = null; - if (kind_tok.kind == .@"const") { - if (!self.match(.@"=")) return ParseError.ExpectedToken; - init = try self.parseExpr(0); - } else if (self.match(.@"=")) { - init = try self.parseExpr(0); - } - decls[count] = .{ .loc = pat.loc, .id = pat, .init = init }; - count += 1; - if (!self.match(.comma)) break; - } - - self.semicolon(); - const kind: ast.VarKind = switch (kind_tok.kind) { - .let => .let, - .@"const" => .@"const", - else => return ParseError.UnexpectedToken, - }; - return Stmt{ - .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, - .data = .{ .lexical = .{ .loc = .{ .start = kind_tok.loc.start, .end = decls[count - 1].loc.end }, .kind = kind, .decls = decls[0..count] } }, - }; - } - - fn parseUsingStmt(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - var decls: [128]ast.VarDeclarator = undefined; - var count: usize = 0; - - while (true) { - const id_tok = try self.expect(.identifier); - const id = self.tokenSlice(id_tok); - var init: ?*const Expr = null; - if (self.match(.@"=")) { - init = try self.parseExpr(0); - } - decls[count] = .{ .loc = .{ .start = id_tok.loc.start, .end = if (init) |e| e.loc.end else id_tok.loc.end }, .id = Pat{ .loc = .{ .start = id_tok.loc.start, .end = id_tok.loc.end }, .data = .{ .ident = id } }, .init = init }; - count += 1; - if (!self.match(.comma)) break; - } - - self.semicolon(); - return Stmt{ - .loc = .{ .start = start, .end = decls[count - 1].loc.end }, - .data = .{ .using = .{ .loc = .{ .start = start, .end = decls[count - 1].loc.end }, .decls = decls[0..count] } }, - }; - } - - fn parseExpressionStatement(self: *@This()) ParseError!Stmt { - const expr = try self.parseExpr(0); - self.semicolon(); - return Stmt{ .loc = expr.loc, .data = .{ .expr = .{ .loc = expr.loc, .expr = expr } } }; - } - - fn parseFunctionDecl(self: *@This(), fn_type: ast.FnType, mode: enum { stmt, expr }) ParseError!?Stmt { - const start = self.tok().loc.start; - - if (fn_type == .async) { - _ = self.advance(); // async - } else if (fn_type == .async_generator) { - _ = self.advance(); // async - _ = self.advance(); // * - } - - _ = self.advance(); // function - - if (fn_type == .normal and self.peek() == .@"*") { - self.advance(); - fn_type = .generator; - } - - var id: ?[]const u8 = null; - if (self.peek().isIdentifier()) { - const id_tok = self.advance(); - id = self.tokenSlice(id_tok); - } - - const params = try self.parseParams(); - const body = try self.parseFunctionBody(); - - const end = body.loc.end; - - if (mode == .stmt) { - return Stmt{ - .loc = .{ .start = start, .end = end }, - .data = .{ .function = .{ .loc = .{ .start = start, .end = end }, .fn_type = fn_type, .id = id, .params = params, .body = body } }, - }; - } - return null; - } - - fn parseFnExpr(self: *@This()) ParseError!*const Expr { - const start = self.advance().loc.start; - - var fn_type: ast.FnType = .normal; - if (self.peek() == .@"*") { - fn_type = .generator; - self.advance(); - } - - var id: ?[]const u8 = null; - if (self.peek() == .identifier or self.peek().isKeywordButNotLet()) { - const id_tok = self.advance(); - id = self.tokenSlice(id_tok); - } - - const params = try self.parseParams(); - const body = try self.parseFunctionBody(); - - return self.arena.exprPtr( - .{ .func = .{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = fn_type, .id = id, .params = params, .body = body } }, - .{ .start = start, .end = body.loc.end }, - ); - } - - fn parseParams(self: *@This()) ParseError![]const Pat { - try self.expect(.lparen); - var params: [128]Pat = undefined; - var count: usize = 0; - - if (self.peek() != .rparen) { - while (true) { - if (self.peek() == .@"...") { - self.advance(); - const pat = try self.parseBindingPattern(); - params[count] = Pat{ .loc = pat.loc, .data = .{ .rest = .{ .loc = pat.loc, .arg = self.arena.patPtr(pat.data, pat.loc) } } }; - count += 1; - break; - } - const pat = try self.parseBindingPattern(); - if (self.match(.@"=")) { - const expr = try self.parseExpr(0); - params[count] = Pat{ .loc = .{ .start = pat.loc.start, .end = expr.loc.end }, .data = .{ .assign = .{ .loc = .{ .start = pat.loc.start, .end = expr.loc.end }, .left = self.arena.patPtr(pat.data, pat.loc), .right = expr } } }; - } else { - params[count] = pat; - } - count += 1; - if (!self.match(.comma)) break; - if (self.peek() == .@"...") { - self.advance(); - const rest_pat = try self.parseBindingPattern(); - params[count] = Pat{ .loc = rest_pat.loc, .data = .{ .rest = .{ .loc = rest_pat.loc, .arg = self.arena.patPtr(rest_pat.data, rest_pat.loc) } } }; - count += 1; - break; - } - } - } - - try self.expect(.rparen); - return params[0..count]; - } - - fn parseFunctionBody(self: *@This()) ParseError!ast.BlockStmt { - return self.parseBlockStmt(); - } - - fn parseClassDecl(self: *@This(), mode: enum { stmt, expr }) ParseError!?Stmt { - const start = self.advance().loc.start; - - var id: ?[]const u8 = null; - if (self.peek() == .identifier) { - const id_tok = self.advance(); - id = self.tokenSlice(id_tok); - } - - var super_class: ?*const Expr = null; - if (self.matchKeyword(.extends)) { - super_class = try self.parseExpr(0); - } - - try self.expect(.lbrace); - var elems: [128]ast.ClassElem = undefined; - var elem_count: usize = 0; - - while (self.peek() != .rbrace and self.peek() != .eof) { - const elem = try self.parseClassElement(); - elems[elem_count] = elem; - elem_count += 1; - if (elem_count >= elems.len) return ParseError.TooManyTokens; - } - - const end_tok = try self.expect(.rbrace); - - if (mode == .stmt) { - return Stmt{ - .loc = .{ .start = start, .end = end_tok.loc.end }, - .data = .{ .class = .{ .loc = .{ .start = start, .end = end_tok.loc.end }, .id = id, .super_class = super_class, .body = elems[0..elem_count] } }, - }; - } - return null; - } - - fn parseClassExpr(self: *@This()) ParseError!*const Expr { - const start = self.advance().loc.start; - - var id: ?[]const u8 = null; - if (self.peek() == .identifier) { - const id_tok = self.advance(); - id = self.tokenSlice(id_tok); - } - - var super_class: ?*const Expr = null; - if (self.matchKeyword(.extends)) { - super_class = try self.parseExpr(0); - } - - try self.expect(.lbrace); - var elems: [128]ast.ClassElem = undefined; - var elem_count: usize = 0; - - while (self.peek() != .rbrace and self.peek() != .eof) { - const elem = try self.parseClassElement(); - elems[elem_count] = elem; - elem_count += 1; - } - - const end_tok = try self.expect(.rbrace); - return self.arena.exprPtr( - .{ .class = .{ .loc = .{ .start = start, .end = end_tok.loc.end }, .id = id, .super_class = super_class, .body = elems[0..elem_count] } }, - .{ .start = start, .end = end_tok.loc.end }, - ); - } - - fn parseClassElement(self: *@This()) ParseError!ast.ClassElem { - const start = self.tok().loc.start; - const is_static = self.matchKeyword(.static); - - if (is_static and self.peek() == .lbrace) { - const body = try self.parseBlockStmt(); - return ast.ClassElem{ .static_block = body }; - } - - if (self.matchKeyword(.get)) { - const key = try self.parsePropKey(); - try self.expect(.lparen); - try self.expect(.rparen); - const body = try self.parseFunctionBody(); - if (is_static) return ast.ClassElem{ .static_get = .{ .key = key, .body = body } }; - return ast.ClassElem{ .get = .{ .key = key, .body = body } }; - } - - if (self.matchKeyword(.set)) { - const key = try self.parsePropKey(); - try self.expect(.lparen); - const param = try self.parseBindingPattern(); - try self.expect(.rparen); - const body = try self.parseFunctionBody(); - if (is_static) return ast.ClassElem{ .static_set = .{ .key = key, .param = param, .body = body } }; - return ast.ClassElem{ .set = .{ .key = key, .param = param, .body = body } }; - } - - if (self.match(.@"*")) { - const key = try self.parsePropKey(); - try self.expect(.lparen); - const params = try self.parseParams(); - const body = try self.parseFunctionBody(); - const fn_expr = ast.FnExpr{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = .generator, .id = null, .params = params, .body = body }; - if (is_static) return ast.ClassElem{ .static_method = fn_expr }; - return ast.ClassElem{ .method = fn_expr }; - } - - const key = try self.parsePropKey(); - - if (self.peek() == .lparen) { - const params = try self.parseParams(); - const body = try self.parseFunctionBody(); - const fn_expr = ast.FnExpr{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = .normal, .id = null, .params = params, .body = body }; - if (is_static) return ast.ClassElem{ .static_method = fn_expr }; - return ast.ClassElem{ .method = fn_expr }; - } - - var value: ?*const Expr = null; - if (self.match(.@"=")) { - value = try self.parseExpr(0); - } - if (self.peek() == .semicolon) self.advance(); - - const field = ast.FieldDef{ .loc = .{ .start = start, .end = if (value) |v| v.loc.end else self.tok().loc.start }, .key = key, .value = value }; - if (is_static) return ast.ClassElem{ .static_field = field }; - return ast.ClassElem{ .field = field }; - } - - fn parseImport(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - var specifiers: [32]ast.ImportSpecifier = undefined; - var spec_count: usize = 0; - - if (self.peek() != .string and self.peek() != .lparen) { - if (self.peek() == .identifier or self.peek().isKeyword()) { - const local_tok = self.advance(); - const local = self.tokenSlice(local_tok); - specifiers[spec_count] = .{ .default = local }; - spec_count += 1; - if (self.match(.comma)) {} - } - - if (self.match(.@"*")) { - try self.expect(.identifier); // as - const ns_tok = try self.expect(.identifier); - const ns = self.tokenSlice(ns_tok); - specifiers[spec_count] = .{ .namespace = ns }; - spec_count += 1; - } else if (self.peek() == .lbrace) { - self.advance(); - if (self.peek() != .rbrace) { - while (true) { - const imported_tok = try self.expect(.identifier); - const imported = self.tokenSlice(imported_tok); - var local = imported; - if (self.match(.identifier)) { - local = self.tokenSlice(self.tokens[self.pos - 1]); - } - specifiers[spec_count] = .{ .named = .{ .imported = imported, .local = local } }; - spec_count += 1; - if (!self.match(.comma)) break; - } - } - try self.expect(.rbrace); - } - } - - var source: []const u8 = ""; - var attrs: [8]ast.ImportAttribute = undefined; - var attr_count: usize = 0; - - if (self.matchKeyword(.from)) { - const str_tok = try self.expect(.string); - source = self.tokenSlice(str_tok); - } else if (self.peek() == .string) { - const str_tok = self.advance(); - source = self.tokenSlice(str_tok); - } - - if (self.matchKeyword(.with)) { - try self.expect(.lbrace); - while (self.peek() != .rbrace and self.peek() != .eof) { - const key_tok = self.advance(); - const key = self.tokenSlice(key_tok); - try self.expect(.@":"); - const val_tok = try self.expect(.string); - const val = self.tokenSlice(val_tok); - attrs[attr_count] = .{ .key = key, .value = val }; - attr_count += 1; - _ = self.match(.comma); - } - try self.expect(.rbrace); - } - - try self.expect(.semicolon); - return Stmt{ - .loc = .{ .start = start, .end = self.tok().loc.start }, - .data = .{ .import = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .specifiers = specifiers[0..spec_count], .source = source, .attributes = attrs[0..attr_count] } }, - }; - } - - fn parseExport(self: *@This()) ParseError!Stmt { - const start = self.advance().loc.start; - - if (self.matchKeyword(.default)) { - if (self.peek() == .function or self.peek() == .async or self.peek() == .class) { - const decl = try self.parseStatementListItem(); - self.semicolon(); - return Stmt{ .loc = .{ .start = start, .end = decl.loc.end }, .data = decl.data }; - } - const expr = try self.parseExpr(0); - self.semicolon(); - return Stmt{ .loc = .{ .start = start, .end = expr.loc.end }, .data = .{ .expr = .{ .loc = expr.loc, .expr = expr } } }; - } - - if (self.peek() == .@"*") { - self.advance(); - try self.expect(.identifier); // from - const source_tok = try self.expect(.string); - _ = self.tokenSlice(source_tok); - try self.expect(.semicolon); - return Stmt{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .empty = {} } }; - } - - if (self.peek() == .lbrace) { - self.advance(); - while (self.peek() != .rbrace and self.peek() != .eof) { - _ = self.advance(); - _ = self.match(.identifier); // as - if (!self.match(.comma)) break; - } - try self.expect(.rbrace); - if (self.matchKeyword(.from)) { - _ = self.advance(); - } - try self.expect(.semicolon); - return Stmt{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .empty = {} } }; - } - - const decl = try self.parseStatementListItem(); - self.semicolon(); - return decl; - } - - // Expression parsing with precedence climbing - fn parseExpr(self: *@This(), min_bp: u32) ParseError!*const Expr { - var left = try self.parsePrimaryExpr(); - - while (true) { - const bp = self.bp(self.peek()); - if (bp <= min_bp) break; - - switch (self.peek()) { - .@"=", - .@"+=", - .@"-=", - .@"*=", - .@"/=", - .@"%=", - .@"**=", - .@"<<=", - .@">>=", - .@">>>=", - .@"&=", - .@"|=", - .@"^=", - .@"&&=", - .@"||=", - .@"??=", - => { - self.advance(); - const op = self.assignOpFromToken(); - const right = try self.parseExpr(BP.assign); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .assign = .{ .loc = loc, .op = op, .left = left, .right = right } }, loc); - }, - - .@"?" => { - self.advance(); - const consequent = try self.parseExpr(0); - try self.expect(.@":"); - const alternate = try self.parseExpr(BP.conditional); - const loc = .{ .start = left.loc.start, .end = alternate.loc.end }; - left = self.arena.exprPtr(.{ .conditional = .{ .loc = loc, .condition = left, .consequent = consequent, .alternate = alternate } }, loc); - }, - - .@"??" => { - self.advance(); - const right = try self.parseExpr(BP.coalesce); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"??", .left = left, .right = right } }, loc); - }, - - .@"||" => { - self.advance(); - const right = try self.parseExpr(BP.log_or); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"||", .left = left, .right = right } }, loc); - }, - - .@"&&" => { - self.advance(); - const right = try self.parseExpr(BP.log_and); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"&&", .left = left, .right = right } }, loc); - }, - - .@"|" => { - self.advance(); - const right = try self.parseExpr(BP.bit_or); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"|", .left = left, .right = right } }, loc); - }, - - .@"^" => { - self.advance(); - const right = try self.parseExpr(BP.bit_xor); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"^", .left = left, .right = right } }, loc); - }, - - .@"&" => { - self.advance(); - const right = try self.parseExpr(BP.bit_and); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"&", .left = left, .right = right } }, loc); - }, - - .@"==" => { - self.advance(); - const right = try self.parseExpr(BP.equality); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"==", .left = left, .right = right } }, loc); - }, - - .@"!=" => { - self.advance(); - const right = try self.parseExpr(BP.equality); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"!=", .left = left, .right = right } }, loc); - }, - - .@"===" => { - self.advance(); - const right = try self.parseExpr(BP.equality); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"===", .left = left, .right = right } }, loc); - }, - - .@"!==" => { - self.advance(); - const right = try self.parseExpr(BP.equality); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"!==", .left = left, .right = right } }, loc); - }, - - .@"<" => { - self.advance(); - const right = try self.parseExpr(BP.relational); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"<", .left = left, .right = right } }, loc); - }, - - .@">" => { - self.advance(); - const right = try self.parseExpr(BP.relational); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">", .left = left, .right = right } }, loc); - }, - - .@"<=" => { - self.advance(); - const right = try self.parseExpr(BP.relational); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"<=", .left = left, .right = right } }, loc); - }, - - .@">=" => { - self.advance(); - const right = try self.parseExpr(BP.relational); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">=", .left = left, .right = right } }, loc); - }, - - .in => { - self.advance(); - const right = try self.parseExpr(BP.relational); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .in, .left = left, .right = right } }, loc); - }, - - .instanceof => { - self.advance(); - const right = try self.parseExpr(BP.relational); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .instanceof, .left = left, .right = right } }, loc); - }, - - .@"<<" => { - self.advance(); - const right = try self.parseExpr(BP.shift); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"<<", .left = left, .right = right } }, loc); - }, - - .@">>" => { - self.advance(); - const right = try self.parseExpr(BP.shift); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">>", .left = left, .right = right } }, loc); - }, - - .@">>>" => { - self.advance(); - const right = try self.parseExpr(BP.shift); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@">>>", .left = left, .right = right } }, loc); - }, - - .@"+" => { - self.advance(); - const right = try self.parseExpr(BP.additive); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"+", .left = left, .right = right } }, loc); - }, - - .@"-" => { - self.advance(); - const right = try self.parseExpr(BP.additive); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"-", .left = left, .right = right } }, loc); - }, - - .@"*" => { - self.advance(); - const right = try self.parseExpr(BP.multiplicative); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"*", .left = left, .right = right } }, loc); - }, - - .@"/" => { - self.advance(); - const right = try self.parseExpr(BP.multiplicative); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"/", .left = left, .right = right } }, loc); - }, - - .@"%" => { - self.advance(); - const right = try self.parseExpr(BP.multiplicative); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"%", .left = left, .right = right } }, loc); - }, - - .@"**" => { - self.advance(); - const right = try self.parseExpr(BP.exponentiation - 1); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - left = self.arena.exprPtr(.{ .binary = .{ .loc = loc, .op = .@"**", .left = left, .right = right } }, loc); - }, - - .lparen => { - // Call expression - self.advance(); - var args: [256]Expr = undefined; - var arg_count: usize = 0; - - if (self.peek() != .rparen) { - while (true) { - if (self.peek() == .@"...") { - self.advance(); - const arg = try self.parseExpr(0); - args[arg_count] = Expr{ .loc = arg.loc, .data = .{ .spread = .{ .loc = arg.loc, .arg = self.arena.exprPtr(arg.data, arg.loc) } } }; - arg_count += 1; - } else { - const arg = try self.parseExpr(0); - args[arg_count] = arg.*; - arg_count += 1; - } - if (!self.match(.comma)) break; - } - } - - try self.expect(.rparen); - const loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; - left = self.arena.exprPtr(.{ .call = .{ .loc = loc, .callee = left, .args = args[0..arg_count], .optional = false } }, loc); - }, - - .@"?." => { - self.advance(); - if (self.peek() == .lparen) { - // optional call - self.advance(); - var args: [256]Expr = undefined; - var arg_count: usize = 0; - if (self.peek() != .rparen) { - while (true) { - const arg = try self.parseExpr(0); - args[arg_count] = arg.*; - arg_count += 1; - if (!self.match(.comma)) break; - } - } - try self.expect(.rparen); - const loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; - left = self.arena.exprPtr(.{ .call = .{ .loc = loc, .callee = left, .args = args[0..arg_count], .optional = true } }, loc); - } else if (self.peek() == .lbracket) { - self.advance(); - const expr = try self.parseExpr(0); - try self.expect(.rbracket); - const loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; - left = self.arena.exprPtr(.{ .computed_member = .{ .loc = loc, .obj = left, .expr = expr } }, loc); - } else if (self.peek() == .identifier or self.peek().isKeyword()) { - const prop_tok = self.advance(); - const prop = self.tokenSlice(prop_tok); - const loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; - left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .ident = prop } } }, loc); - } else if (self.peek() == .private_identifier) { - const prop_tok = self.advance(); - const prop = self.tokenSlice(prop_tok); - const loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; - left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .private = prop } } }, loc); - } - }, - - .@"[" => { - self.advance(); - const expr = try self.parseExpr(0); - try self.expect(.rbracket); - const loc = .{ .start = left.loc.start, .end = self.tok().loc.start }; - left = self.arena.exprPtr(.{ .computed_member = .{ .loc = loc, .obj = left, .expr = expr } }, loc); - }, - - .@"." => { - self.advance(); - if (self.peek() == .private_identifier) { - const prop_tok = self.advance(); - const prop = self.tokenSlice(prop_tok); - const loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; - left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .private = prop } } }, loc); - } else { - const prop_tok = try self.expect(.identifier); - const prop = self.tokenSlice(prop_tok); - const loc = .{ .start = left.loc.start, .end = prop_tok.loc.end }; - left = self.arena.exprPtr(.{ .member = .{ .loc = loc, .obj = left, .prop = .{ .ident = prop } } }, loc); - } - }, - - .template_head, .no_sub_template => { - const template = try self.parseTemplateLit(); - const loc = .{ .start = left.loc.start, .end = template.loc.end }; - left = self.arena.exprPtr(.{ .tagged_template = .{ .loc = loc, .tag = left, .quasi = template } }, loc); - }, - - .@"++" => { - if (!self.isLineTerminatorBeforeNext()) { - self.advance(); - const loc = .{ .start = left.loc.start, .end = left.loc.end + 2 }; - left = self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"++", .arg = left, .prefix = false } }, loc); - } else break; - }, - - .@"--" => { - if (!self.isLineTerminatorBeforeNext()) { - self.advance(); - const loc = .{ .start = left.loc.start, .end = left.loc.end + 2 }; - left = self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"--", .arg = left, .prefix = false } }, loc); - } else break; - }, - - .comma => { - self.advance(); - const right = try self.parseExpr(BP.comma); - const loc = .{ .start = left.loc.start, .end = right.loc.end }; - var exprs: [2]Expr = .{ left.*, right.* }; - left = self.arena.exprPtr(.{ .sequence = .{ .loc = loc, .exprs = &exprs } }, loc); - }, - - else => break, - } - } - - return left; - } - - fn parsePrimaryExpr(self: *@This()) ParseError!*const Expr { - const start = self.tok().loc.start; - - switch (self.peek()) { - .this => { - const t = self.advance(); - return self.arena.exprPtr(.{ .this = {} }, .{ .start = start, .end = t.loc.end }); - }, - .super => { - const t = self.advance(); - return self.arena.exprPtr(.{ .super = {} }, .{ .start = start, .end = t.loc.end }); - }, - .null => { - const t = self.advance(); - return self.arena.exprPtr(.{ .null = {} }, .{ .start = start, .end = t.loc.end }); - }, - .true => { - const t = self.advance(); - return self.arena.exprPtr(.{ .bool = true }, .{ .start = start, .end = t.loc.end }); - }, - .false => { - const t = self.advance(); - return self.arena.exprPtr(.{ .bool = false }, .{ .start = start, .end = t.loc.end }); - }, - .number => { - const t = self.advance(); - const val = std.fmt.parseFloat(f64, self.tokenSlice(t)) catch 0.0; - return self.arena.exprPtr(.{ .number = val }, .{ .start = start, .end = t.loc.end }); - }, - .string => { - const t = self.advance(); - return self.arena.exprPtr(.{ .string = self.tokenSlice(t) }, .{ .start = start, .end = t.loc.end }); - }, - .bigint => { - const t = self.advance(); - const slice = self.tokenSlice(t); - return self.arena.exprPtr(.{ .number = 0 }, .{ .start = start, .end = t.loc.end }); - }, - .regex => { - const t = self.advance(); - const slice = self.tokenSlice(t); - const slash_pos = std.mem.indexOfScalar(u8, slice[1..], '/') orelse 0; - const pattern = slice[1 .. slash_pos + 1]; - const flags = slice[slash_pos + 2 ..]; - return self.arena.exprPtr( - .{ .regex = .{ .pattern = pattern, .flags = flags } }, - .{ .start = start, .end = t.loc.end }, - ); - }, - .identifier => { - const t = self.advance(); - const name = self.tokenSlice(t); - return self.arena.exprPtr(.{ .identifier = name }, .{ .start = start, .end = t.loc.end }); - }, - .private_identifier => { - const t = self.advance(); - const name = self.tokenSlice(t); - return self.arena.exprPtr(.{ .private_ident = name }, .{ .start = start, .end = t.loc.end }); - }, - .lbrace => return self.parseObjLiteral(), - .lbracket => return self.parseArrayLiteral(), - .lparen => { - self.advance(); - if (self.peek() == .rparen) { - self.advance(); - if (self.peek() == .@"=>") { - return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = self.tok().loc.start }, .params = &.{} }); - } - return self.arena.exprPtr(.{ .identifier = "" }, .{ .start = start, .end = self.tok().loc.start }); - } - if (self.peek() == .@"...") { - self.advance(); - const arg = try self.parseExpr(0); - try self.expect(.rparen); - if (self.peek() == .@"=>") { - return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = arg.loc.end }, .params = &.{} }); - } - const loc = .{ .start = start, .end = self.tok().loc.start }; - return self.arena.exprPtr(.{ .spread = .{ .loc = loc, .arg = arg } }, loc); - } - const expr = try self.parseExpr(0); - if (self.peek() == .comma) { - var params: [128]Pat = undefined; - var count: usize = 0; - params[count] = Pat{ .loc = expr.loc, .data = .{ .ident = expr.data.identifier } }; - count += 1; - while (self.match(.comma)) { - const p = try self.parseBindingPattern(); - params[count] = p; - count += 1; - } - try self.expect(.rparen); - if (self.peek() == .@"=>") { - return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = self.tok().loc.start }, .params = params[0..count] }); - } - return self.arena.exprPtr(.{ .identifier = "" }, .{ .start = start, .end = self.tok().loc.start }); - } - try self.expect(.rparen); - if (self.peek() == .@"=>") { - const pat = Pat{ .loc = expr.loc, .data = .{ .ident = expr.data.identifier } }; - return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = self.tok().loc.start }, .params = &.{pat} }); - } - return self.arena.exprPtr(.{ .parenthesized = expr }, .{ .start = start, .end = self.tok().loc.start }); - }, - .function => return self.parseFnExpr(), - .class => return self.parseClassExpr(), - .async => { - self.advance(); - if (self.peek() == .function) { - return self.parseFnExpr(); - } - // async arrow function - const id_tok = try self.expect(.identifier); - const name = self.tokenSlice(id_tok); - if (self.peek() == .@"=>") { - const pat = Pat{ .loc = .{ .start = start, .end = id_tok.loc.end }, .data = .{ .ident = name } }; - return self.parseArrowExpr(.{ .loc = .{ .start = start, .end = id_tok.loc.end }, .params = &.{pat} }); - } - return self.arena.exprPtr(.{ .identifier = name }, .{ .start = start, .end = id_tok.loc.end }); - }, - .new => { - self.advance(); - const callee = try self.parseExpr(BP.member); - var args: [256]Expr = undefined; - var arg_count: usize = 0; - if (self.peek() == .lparen) { - self.advance(); - if (self.peek() != .rparen) { - while (true) { - const arg = try self.parseExpr(0); - args[arg_count] = arg.*; - arg_count += 1; - if (!self.match(.comma)) break; - } - } - try self.expect(.rparen); - } - const loc = .{ .start = start, .end = if (arg_count > 0) args[arg_count - 1].loc.end else callee.loc.end }; - return self.arena.exprPtr(.{ .new = .{ .loc = loc, .callee = callee, .args = args[0..arg_count] } }, loc); - }, - .delete, .typeof, .void, .@"!", .@"~", .@"+", .@"-" => { - const op = self.unaryOpFromToken(); - self.advance(); - const arg = try self.parseExpr(BP.unary); - const loc = .{ .start = start, .end = arg.loc.end }; - return self.arena.exprPtr(.{ .unary = .{ .loc = loc, .op = op, .arg = arg, .prefix = true } }, loc); - }, - .@"++" => { - self.advance(); - const arg = try self.parseExpr(BP.update); - const loc = .{ .start = start, .end = arg.loc.end }; - return self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"++", .arg = arg, .prefix = true } }, loc); - }, - .@"--" => { - self.advance(); - const arg = try self.parseExpr(BP.update); - const loc = .{ .start = start, .end = arg.loc.end }; - return self.arena.exprPtr(.{ .update = .{ .loc = loc, .op = .@"--", .arg = arg, .prefix = true } }, loc); - }, - .await => { - self.advance(); - const arg = try self.parseExpr(BP.unary); - const loc = .{ .start = start, .end = arg.loc.end }; - return self.arena.exprPtr(.{ .await = .{ .loc = loc, .arg = arg } }, loc); - }, - .yield => { - self.advance(); - var delegate = false; - var arg: ?*const Expr = null; - if (!self.isLineTerminatorBeforeNext()) { - if (self.match(.@"*")) { - delegate = true; - } - if (self.peek() != .rbrace and self.peek() != .rparen and self.peek() != .rbracket and self.peek() != .semicolon and self.peek() != .comma and self.peek() != .@":" and self.peek() != .eof) { - arg = try self.parseExpr(0); - } - } - const loc = .{ .start = start, .end = if (arg) |a| a.loc.end else start + 5 }; - return self.arena.exprPtr(.{ .yield = .{ .loc = loc, .arg = arg, .delegate = delegate } }, loc); - }, - .template_head, .no_sub_template => return self.arena.exprPtr(.{ .template = try self.parseTemplateLit() }, .{ .start = start, .end = self.tok().loc.start }), - .import => { - self.advance(); - try self.expect(.lparen); - const source = try self.parseExpr(0); - try self.expect(.rparen); - return self.arena.exprPtr(.{ .identifier = "import" }, .{ .start = start, .end = self.tok().loc.start }); - }, - .@"#" => { - self.advance(); - const id_tok = try self.expect(.identifier); - return self.arena.exprPtr(.{ .private_ident = self.tokenSlice(id_tok) }, .{ .start = start, .end = id_tok.loc.end }); - }, - else => { - if (self.peek().isIdentifier()) { - const t = self.advance(); - const name = self.tokenSlice(t); - if (self.peek() == .@"=>") { - const pat = Pat{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .data = .{ .ident = name } }; - return self.parseArrowExpr(.{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .params = &.{pat} }); - } - return self.arena.exprPtr(.{ .identifier = name }, .{ .start = t.loc.start, .end = t.loc.end }); - } - return ParseError.ExpectedExpression; - }, - } - } - - fn parseArrowExpr(self: *@This(), params_info: struct { loc: Loc, params: []const Pat }) ParseError!*const Expr { - _ = params_info; - try self.expect(.@"=>"); - const async_token = false; - - if (self.peek() == .lbrace) { - const body = try self.parseFunctionBody(); - return self.arena.exprPtr( - .{ .arrow = .{ .loc = .{ .start = params_info.loc.start, .end = body.loc.end }, .async_token = async_token, .params = params_info.params, .body = .{ .block = body } } }, - .{ .start = params_info.loc.start, .end = body.loc.end }, - ); - } - - const expr = try self.parseExpr(0); - return self.arena.exprPtr( - .{ .arrow = .{ .loc = .{ .start = params_info.loc.start, .end = expr.loc.end }, .async_token = async_token, .params = params_info.params, .body = .{ .expr = expr } } }, - .{ .start = params_info.loc.start, .end = expr.loc.end }, - ); - } - - fn parseObjLiteral(self: *@This()) ParseError!*const Expr { - const start = self.advance().loc.start; - var props: [128]ast.Prop = undefined; - var count: usize = 0; - - while (self.peek() != .rbrace and self.peek() != .eof) { - if (self.peek() == .@"...") { - self.advance(); - const arg = try self.parseExpr(0); - props[count] = .{ .loc = arg.loc, .key = .{ .ident = "" }, .data = .{ .spread = .{ .loc = arg.loc, .arg = arg } } }; - count += 1; - } else if (self.peek() == .identifier) { - const name_tok = self.tok(); - const name = self.tokenSlice(name_tok); - self.advance(); - if (self.peek() == .@":") { - self.advance(); - const value = try self.parseExpr(0); - props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = value.loc.end }, .key = .{ .ident = name }, .data = .{ .init = .{ .key = .{ .ident = name }, .value = value } } }; - count += 1; - } else if (self.peek() == .@"(") { - const params = try self.parseParams(); - const body = try self.parseFunctionBody(); - props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = body.loc.end }, .key = .{ .ident = name }, .data = .{ .method = .{ .loc = .{ .start = name_tok.loc.start, .end = body.loc.end }, .fn_type = .normal, .id = null, .params = params, .body = body } } }; - count += 1; - } else if (self.peek() == .@"=") { - self.advance(); - const value = try self.parseExpr(0); - props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = value.loc.end }, .key = .{ .ident = name }, .data = .{ .init = .{ .key = .{ .ident = name }, .value = value } } }; - count += 1; - } else if (self.peek() == .@"," or self.peek() == .rbrace) { - props[count] = .{ .loc = .{ .start = name_tok.loc.start, .end = name_tok.loc.end }, .key = .{ .ident = name }, .data = .{ .shorthand = name } }; - count += 1; - } - } else if (self.peek() == .number or self.peek() == .string) { - const key = try self.parsePropKey(); - try self.expect(.@":"); - const value = try self.parseExpr(0); - props[count] = .{ .loc = .{ .start = start, .end = value.loc.end }, .key = key, .data = .{ .init = .{ .key = key, .value = value } } }; - count += 1; - } else if (self.peek() == .lbracket) { - const key = try self.parsePropKey(); - try self.expect(.@":"); - const value = try self.parseExpr(0); - props[count] = .{ .loc = .{ .start = start, .end = value.loc.end }, .key = key, .data = .{ .init = .{ .key = key, .value = value } } }; - count += 1; - } else if (self.matchKeyword(.get)) { - const key = try self.parsePropKey(); - try self.expect(.lparen); - try self.expect(.rparen); - const body = try self.parseFunctionBody(); - props[count] = .{ .loc = .{ .start = start, .end = body.loc.end }, .key = key, .data = .{ .get = .{ .key = key, .body = body } } }; - count += 1; - } else if (self.matchKeyword(.set)) { - const key = try self.parsePropKey(); - try self.expect(.lparen); - const param = try self.parseBindingPattern(); - try self.expect(.rparen); - const body = try self.parseFunctionBody(); - props[count] = .{ .loc = .{ .start = start, .end = body.loc.end }, .key = key, .data = .{ .set = .{ .key = key, .param = param, .body = body } } }; - count += 1; - } else if (self.match(.@"*")) { - const key = try self.parsePropKey(); - const params = try self.parseParams(); - const body = try self.parseFunctionBody(); - props[count] = .{ .loc = .{ .start = start, .end = body.loc.end }, .key = key, .data = .{ .method = .{ .loc = .{ .start = start, .end = body.loc.end }, .fn_type = .generator, .id = null, .params = params, .body = body } } }; - count += 1; - } else { - break; - } - - if (!self.match(.comma)) break; - } - - try self.expect(.rbrace); - return self.arena.exprPtr(.{ .object = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .props = props[0..count] } }, .{ .start = start, .end = self.tok().loc.start }); - } - - fn parseArrayLiteral(self: *@This()) ParseError!*const Expr { - const start = self.advance().loc.start; - var elems: [256]?Expr = undefined; - var count: usize = 0; - - while (self.peek() != .rbracket and self.peek() != .eof) { - if (self.match(.comma)) { - elems[count] = null; - count += 1; - } else if (self.peek() == .@"...") { - self.advance(); - const arg = try self.parseExpr(0); - elems[count] = Expr{ .loc = arg.loc, .data = .{ .spread = .{ .loc = arg.loc, .arg = arg } } }; - count += 1; - if (!self.match(.comma)) break; - } else { - const expr = try self.parseExpr(0); - elems[count] = expr.*; - count += 1; - if (!self.match(.comma)) break; - } - } - - try self.expect(.rbracket); - return self.arena.exprPtr(.{ .array = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .elems = elems[0..count] } }, .{ .start = start, .end = self.tok().loc.start }); - } - - fn parseTemplateLit(self: *@This()) ParseError!ast.TemplateLit { - const start = self.tok().loc.start; - var quasis: [64]ast.TemplateElem = undefined; - var exprs: [64]Expr = undefined; - var q_count: usize = 0; - var e_count: usize = 0; - - while (true) { - switch (self.peek()) { - .no_sub_template => { - const t = self.advance(); - const slice = self.tokenSlice(t); - quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = true }; - q_count += 1; - break; - }, - .template_head => { - const t = self.advance(); - const slice = self.tokenSlice(t); - quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = false }; - q_count += 1; - const expr = try self.parseExpr(0); - exprs[e_count] = expr.*; - e_count += 1; - try self.expect(.rbrace); - }, - .template_middle => { - const t = self.advance(); - const slice = self.tokenSlice(t); - quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = false }; - q_count += 1; - const expr = try self.parseExpr(0); - exprs[e_count] = expr.*; - e_count += 1; - try self.expect(.rbrace); - }, - .template_tail => { - const t = self.advance(); - const slice = self.tokenSlice(t); - quasis[q_count] = .{ .loc = .{ .start = t.loc.start, .end = t.loc.end }, .value = slice, .tail = true }; - q_count += 1; - break; - }, - else => break, - } - } - - return .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .quasis = quasis[0..q_count], .exprs = exprs[0..e_count] }; - } - - fn parsePropKey(self: *@This()) ParseError!ast.PropKey { - switch (self.peek()) { - .identifier, .let, .static, .get, .set, .async => { - const t = self.advance(); - return ast.PropKey{ .ident = self.tokenSlice(t) }; - }, - .string => { - const t = self.advance(); - return ast.PropKey{ .string = self.tokenSlice(t) }; - }, - .number => { - const t = self.advance(); - const val = std.fmt.parseFloat(f64, self.tokenSlice(t)) catch 0.0; - return ast.PropKey{ .number = val }; - }, - .lbracket => { - self.advance(); - const expr = try self.parseExpr(0); - try self.expect(.rbracket); - return ast.PropKey{ .computed = expr }; - }, - .private_identifier => { - const t = self.advance(); - return ast.PropKey{ .private = self.tokenSlice(t) }; - }, - else => return ParseError.ExpectedPropertyName, - } - } - - fn parseBindingPattern(self: *@This()) ParseError!Pat { - const start = self.tok().loc.start; - - switch (self.peek()) { - .identifier, .await, .yield => { - const t = self.advance(); - const name = self.tokenSlice(t); - return Pat{ .loc = .{ .start = start, .end = t.loc.end }, .data = .{ .ident = name } }; - }, - .lbrace => { - self.advance(); - var props: [128]ast.PatProp = undefined; - var count: usize = 0; - var rest: ?*const Pat = null; - - while (self.peek() != .rbrace and self.peek() != .eof) { - if (self.peek() == .@"...") { - self.advance(); - const pat = try self.parseBindingPattern(); - rest = self.arena.patPtr(pat.data, pat.loc); - break; - } - if (self.peek() == .identifier) { - const name_tok = self.tok(); - const name = self.tokenSlice(name_tok); - self.advance(); - if (self.peek() == .@":") { - self.advance(); - const val_pat = try self.parseBindingPattern(); - props[count] = .{ .key_value = .{ .key = .{ .ident = name }, .value = self.arena.patPtr(val_pat.data, val_pat.loc) } }; - count += 1; - } else { - props[count] = .{ .shorthand = name }; - count += 1; - } - } else if (self.peek() == .lbracket) { - const key = try self.parsePropKey(); - try self.expect(.@":"); - const val_pat = try self.parseBindingPattern(); - props[count] = .{ .key_value = .{ .key = key, .value = self.arena.patPtr(val_pat.data, val_pat.loc) } }; - count += 1; - } else { - break; - } - if (!self.match(.comma)) break; - } - - try self.expect(.rbrace); - return Pat{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .object = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .props = props[0..count], .rest = rest } } }; - }, - .lbracket => { - self.advance(); - var elems: [128]?Pat = undefined; - var count: usize = 0; - var rest: ?*const Pat = null; - - while (self.peek() != .rbracket and self.peek() != .eof) { - if (self.match(.comma)) { - elems[count] = null; - count += 1; - } else if (self.peek() == .@"...") { - self.advance(); - const pat = try self.parseBindingPattern(); - rest = self.arena.patPtr(pat.data, pat.loc); - break; - } else { - const pat = try self.parseBindingPattern(); - elems[count] = pat; - count += 1; - if (!self.match(.comma)) break; - } - } - - try self.expect(.rbracket); - return Pat{ .loc = .{ .start = start, .end = self.tok().loc.start }, .data = .{ .array = .{ .loc = .{ .start = start, .end = self.tok().loc.start }, .elems = elems[0..count], .rest = rest } } }; - }, - else => return ParseError.ExpectedParameterName, - } - } - - fn bp(self: *@This(), kind: TokenType) u32 { - return switch (kind) { - .comma => BP.comma, - .@"=", .@"+=", .@"-=", .@"*=", .@"/=", .@"%=", .@"**=", .@"<<=", .@">>=", .@">>>=", .@"&=", .@"|=", .@"^=", .@"&&=", .@"||=", .@"??=" => BP.assign, - .@"?" => BP.conditional, - .@"??" => BP.coalesce, - .@"||" => BP.log_or, - .@"&&" => BP.log_and, - .@"|" => BP.bit_or, - .@"^" => BP.bit_xor, - .@"&" => BP.bit_and, - .@"==", .@"!=", .@"===", .@"!==" => BP.equality, - .@"<", .@">", .@"<=", .@">=", .in, .instanceof => BP.relational, - .@"<<", .@">>", .@">>>" => BP.shift, - .@"+", .@"-" => BP.additive, - .@"*", .@"/", .@"%" => BP.multiplicative, - .@"**" => BP.exponentiation, - .lparen => BP.call, - .@"[", .@".", .@"?." => BP.member, - .@"++", .@"--" => BP.update, - .template_head, .no_sub_template => BP.member, - else => 0, - }; - } - - fn unaryOpFromToken(self: *@This()) ast.UnaryOp { - return switch (self.tok().kind) { - .@"+" => .@"+", - .@"-" => .@"-", - .@"!" => .@"!", - .@"~" => .@"~", - .typeof => .typeof, - .void => .void, - .delete => .delete, - else => .@"+", - }; - } - - fn assignOpFromToken(self: *@This()) ast.AssignOp { - return switch (self.tokens[self.pos - 1].kind) { - .@"=" => .@"=", - .@"+=" => .@"+=", - .@"-=" => .@"-=", - .@"*=" => .@"*=", - .@"/=" => .@"/=", - .@"%=" => .@"%=", - .@"**=" => .@"**=", - .@"<<=" => .@"<<=", - .@">>=" => .@">>=", - .@">>>=" => .@">>>=", - .@"&=" => .@"&=", - .@"|=" => .@"|=", - .@"^=" => .@"^=", - .@"&&=" => .@"&&=", - .@"||=" => .@"||=", - .@"??=" => .@"??=", - else => .@"=", - }; - } - - fn semicolon(self: *@This()) void { - if (self.peek() == .semicolon) { - self.advance(); - } - } - - fn statementPointer(self: *@This()) ParseError!*const Stmt { - const s = try self.parseStatement(); - return self.arena.stmtPtr(s.data, s.loc); - } -}; - -pub fn parse(comptime source: []const u8) ParseError!ast.Program { - comptime { - const tokens = lex.Lexer.tokenize(source); - var arena: NodeArena = .{}; - var parser = Parser.init(tokens, source, &arena); - const result = try parser.parseProgram(); - return result; - } -} - -pub fn parseTokens(comptime source: []const u8) ParseError![]const Token { - comptime { - return lex.Lexer.tokenize(source); - } -} - -pub fn tokenType(kind: TokenType) []const u8 { - return @tagName(kind); -} diff --git a/src/z/js/parser/test_fixture.js b/src/z/js/parser/test_fixture.js new file mode 100644 index 0000000..378ceb3 --- /dev/null +++ b/src/z/js/parser/test_fixture.js @@ -0,0 +1,86 @@ +#!/usr/bin/env node +// comment line +/* block + comment */ +var x = 42; +let y = "hello"; +const z = `template ${x} literal`; +let a = 0b1010; +let b = 0xFF; +let c = 0o77; +let d = 123n; +let e = /pattern/gi; +let f = true; +let g = null; +function hello(a, b) { + if (a > b) { + return a; + } else { + return b; + } +} +const arrow = (x) => x * 2; +const obj = { + a: 1, + b: "two", + c, + [d]: 4, + method() { return 5; }, + get prop() { return 6; }, + set prop(v) { }, +}; +const arr = [1, 2, ...rest, 4]; +for (var i = 0; i < 10; i++) { + continue; +} +for (const key in obj) { + break; +} +for (const val of arr) { + ; +} +while (x < 10) { + x++; +} +do { + x--; +} while (x > 0); +try { + throw "err"; +} catch (e) { + console.log(e); +} finally { + cleanup(); +} +switch (x) { + case 1: + break; + case 2: + return; + default: + break; +} +class Foo extends Bar { + constructor() { super(); } + static method() { } + get prop() { return 1; } + set prop(v) { } +} +const { a: p, b: q } = obj; +const [head, ...tail] = arr; +import { something } from "module"; +export default x; +export const named = 1; +export function exported() { } +import "side-effect"; +import * as ns from "module"; +export { x as y } from "module"; +export * from "module"; +delete obj.a; +typeof x; +void x; +x ?? y; +x?.y?.z; +x ??= y; +x &&= y; +x ||= y; diff --git a/src/z/root.zig b/src/z/root.zig index b23ebc5..e8d22ab 100644 --- a/src/z/root.zig +++ b/src/z/root.zig @@ -3,6 +3,7 @@ const std = @import("std"); pub const Minifier = @import("Minifier.zig"); pub const Component = @import("Component.zig"); pub const File = @import("File.zig"); +pub const js = @import("js/parser/root.zig"); const Element = @import("html").Element; -- cgit v1.2.3-70-g09d2