parser
The JavaScript parser: `parser.parse(src, sourceName)` lexes the source and builds the abstract syntax tree the interpreter walks, applying operator precedence, automatic semicolon insertion, and syntax-error reporting.
parser
The JavaScript parser: parser.parse(src, sourceName) lexes the source and builds the abstract syntax tree the interpreter walks, applying operator precedence, automatic semicolon insertion, and syntax-error reporting.
Interface
What this asset declares: the schema it conforms to, what it exposes, and the rendered structured payload.
conforms to
zero/source-extract/v2strict
newParser(tokens: { Token }, sourceName: string, module: boolean?) → Parser
| arg | type | description |
|---|---|---|
| tokens | { Token } | |
| sourceName | string | |
| module | boolean? |
peek(self: Parser, ahead: number?) → Token
| arg | type | description |
|---|---|---|
| self | Parser | |
| ahead | number? |
next(self: Parser) → Token
| arg | type | description |
|---|---|---|
| self | Parser |
atEnd(self: Parser) → boolean
| arg | type | description |
|---|---|---|
| self | Parser |
raise(self: Parser, msg: string, tok: Token?) → never
| arg | type | description |
|---|---|---|
| self | Parser | |
| msg | string | |
| tok | Token? |
isPunct(self: Parser, value: string, ahead: number?) → boolean
Match a punct token by exact value without consuming.
| arg | type | description |
|---|---|---|
| self | Parser | |
| value | string | |
| ahead | number? |
isKeyword(self: Parser, value: string, ahead: number?) → boolean
| arg | type | description |
|---|---|---|
| self | Parser | |
| value | string | |
| ahead | number? |
expectPunct(self: Parser, value: string) → Token
Consume a punct token whose value must equal `value`, else raise.
| arg | type | description |
|---|---|---|
| self | Parser | |
| value | string |
node(type_: string, tok: { line: number, col: number, [string]: any }, fields: { [string]: any }) → Node
Base node factory. line/col come from the node's first token. `tok` is only read for its position, and callers pass whatever carries one: a lexer token where the construct starts at a token, and an already-built node where it starts at a sub-expression (`a.b`, `a[b]`, a call on a callee).
| arg | type | description |
|---|---|---|
| type_ | string | |
| tok | { line: number, col: number, [string]: any } | |
| fields | { [string]: any } |
toPattern(self: Parser, expr: Node) → Node
| arg | type | description |
|---|---|---|
| self | Parser | |
| expr | Node |
parseArguments(self: Parser) → void
Parse a comma-separated argument list up to the closing paren. Supports `...spread`. Assumes the opening `(` is already consumed.
| arg | type | description |
|---|---|---|
| self | Parser |
parseBlock(self: Parser) → Node
Parse a `{ ... }` statement block of any statements.
| arg | type | description |
|---|---|---|
| self | Parser |
parseFunctionExpr(self: Parser, kwTok: Token, isAsync: boolean) → Node
Parse a `function` expression, with the `function` keyword already consumed.
| arg | type | description |
|---|---|---|
| self | Parser | |
| kwTok | Token | |
| isAsync | boolean |
parseParamList(self: Parser) → void
Parse a `(...)` parameter list into patterns, opening `(` already consumed.
| arg | type | description |
|---|---|---|
| self | Parser |
parseBindingTarget(self: Parser) → Node
Parse a single binding target (identifier or nested destructuring pattern).
| arg | type | description |
|---|---|---|
| self | Parser |
arrowAhead(self: Parser) → boolean
Determine whether the parenthesised group starting at the current `(` is an arrow parameter list, by scanning to its matching `)` and checking for `=>`.
| arg | type | description |
|---|---|---|
| self | Parser |
finishArrow(self: Parser, startTok: Token, params: { Node }, isAsync: boolean) → Node
Parse an arrow function tail from an already-parsed parameter list. The `=>` is at the cursor.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token | |
| params | { Node } | |
| isAsync | boolean |
parseObjectLit(self: Parser, openTok: Token) → Node
Parse an object literal, opening `{` already consumed.
| arg | type | description |
|---|---|---|
| self | Parser | |
| openTok | Token |
parsePropertyKey(self: Parser) → void
Parse an object-literal property key, returning (keyNode, computed).
| arg | type | description |
|---|---|---|
| self | Parser |
parseArrayLit(self: Parser, openTok: Token) → Node
Parse an array literal, opening `[` already consumed.
| arg | type | description |
|---|---|---|
| self | Parser | |
| openTok | Token |
parseTemplateToken(self: Parser, tok: Token) → Node
Parse a template literal token into a Template node, re-parsing each embedded expression's token array with a nested parser.
| arg | type | description |
|---|---|---|
| self | Parser | |
| tok | Token |
parsePrimary(self: Parser) → Node
Parse a primary (leaf) expression.
| arg | type | description |
|---|---|---|
| self | Parser |
parseKeywordPrimary(self: Parser, tok: Token) → Node
Parse keyword-led primaries: literals, this, function expressions, arrows.
| arg | type | description |
|---|---|---|
| self | Parser | |
| tok | Token |
parseParenOrArrow(self: Parser, openTok: Token, isAsync: boolean) → Node
Parse a parenthesised expression or an arrow function. `isAsync` marks an `async (...)` head. The `(` is at the cursor (not yet consumed).
| arg | type | description |
|---|---|---|
| self | Parser | |
| openTok | Token | |
| isAsync | boolean |
parseNew(self: Parser, newTok: Token) → Node
Parse `new` expressions. `new` binds tighter than call chains: it consumes a member expression as the callee, then an optional single argument list.
| arg | type | description |
|---|---|---|
| self | Parser | |
| newTok | Token |
parseMemberTail(self: Parser, expr: Node) → Node
Parse the member-access tail of an expression (`.x` and `[e]`), without consuming call parens. Used for `new` callees.
| arg | type | description |
|---|---|---|
| self | Parser | |
| expr | Node |
parseCallTail(self: Parser, expr: Node, allowCall: boolean) → Node
Parse the full call/member tail: `.x`, `[e]`, `(args)`, and reject tagged templates. `allowCall` gates whether call parens are consumed here.
| arg | type | description |
|---|---|---|
| self | Parser | |
| expr | Node | |
| allowCall | boolean |
parsePostfix(self: Parser) → Node
Parse a postfix expression: a primary/new followed by member/call tails and a trailing `++`/`--`.
| arg | type | description |
|---|---|---|
| self | Parser |
parseUnary(self: Parser) → void
Parse a unary/prefix expression, returning the node and whether it was a unary form (used to forbid a unary operand directly on the left of `**`).
| arg | type | description |
|---|---|---|
| self | Parser |
parseExponent(self: Parser) → Node
Parse an exponentiation expression: `unary ** exponent`, right-associative. A unary operand on the left of `**` is a SyntaxError.
| arg | type | description |
|---|---|---|
| self | Parser |
parseBinary(self: Parser, minBp: number, noIn: boolean?) → Node
Parse binary/logical operators by precedence climbing from binding power `minBp`. When `noIn` is true the `in` operator is treated as unrecognised, which is required inside for-head initialisers (ECMA-262 §14.7.4). Parentheses reset the flag — `parseParenOrArrow` calls back without it.
| arg | type | description |
|---|---|---|
| self | Parser | |
| minBp | number | |
| noIn | boolean? |
parseConditional(self: Parser, noIn: boolean?) → Node
Parse a conditional (ternary) expression.
| arg | type | description |
|---|---|---|
| self | Parser | |
| noIn | boolean? |
parseAssign(self: Parser, noIn: boolean?) → Node
Parse an assignment expression (right-associative), converting object/array literal targets to patterns.
| arg | type | description |
|---|---|---|
| self | Parser | |
| noIn | boolean? |
parseExpression(self: Parser, noIn: boolean?) → Node
Parse a full expression, including comma sequences.
| arg | type | description |
|---|---|---|
| self | Parser | |
| noIn | boolean? |
expectSemicolon(self: Parser) → void
Automatic Semicolon Insertion. Accept an explicit `;`; otherwise a statement may end before `}`, at eof, or before a token that begins a new line.
| arg | type | description |
|---|---|---|
| self | Parser |
parseStatement(self: Parser) → Node
Parse a single statement. Keyword-led statements dispatch here; anything else is a labeled statement or an expression statement.
| arg | type | description |
|---|---|---|
| self | Parser |
parseVarDecl(self: Parser, inForHead: boolean?, noIn: boolean?) → Node
Parse a `var`/`let`/`const` declaration head, up to but not including its terminator. The declaration keyword is at the cursor. `inForHead` allows a `const` declarator without an initializer, as a for-in/for-of head requires. `noIn` suppresses the `in` operator inside declarator initialisers, which is required when this declaration appears as the init clause of a c-style for.
| arg | type | description |
|---|---|---|
| self | Parser | |
| inForHead | boolean? | |
| noIn | boolean? |
parseFunctionDecl(self: Parser, startTok: Token, isAsync: boolean) → Node
Parse a function declaration, `function` keyword already consumed.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token | |
| isAsync | boolean |
parseIf(self: Parser, startTok: Token) → Node
Parse an `if`/`else` statement. A trailing `else` binds to this `if`, so a dangling `else` attaches to the nearest unmatched `if`.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseWhile(self: Parser, startTok: Token) → Node
Parse a `while` loop.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseDoWhile(self: Parser, startTok: Token) → Node
Parse a `do`/`while` loop.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseFor(self: Parser, startTok: Token) → Node
Parse the four `for` forms: c-style, for-in, and for-of, with either a declaration head or a bare-pattern/expression head.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseSwitch(self: Parser, startTok: Token) → Node
Parse a `switch` statement. Case bodies are statement lists that run until the next `case`/`default`/closing brace. A `default` clause carries a nil test.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseTry(self: Parser, startTok: Token) → Node
Parse a `try` statement with any of an optional catch (with or without a binding param) and an optional finally.
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseClassDecl(self: Parser, startTok: Token) → Node
Parse a class declaration (id required).
| arg | type | description |
|---|---|---|
| self | Parser | |
| startTok | Token |
parseClassExpr(self: Parser) → Node
Parse a class expression (id optional), `class` keyword at the cursor.
| arg | type | description |
|---|---|---|
| self | Parser |
parseClassHeritage(self: Parser) → Node
Parse an optional `extends <LeftHandSideExpression>` heritage clause.
| arg | type | description |
|---|---|---|
| self | Parser |
parseClassBody(self: Parser) → void
Parse a `{ ... }` class body into a list of class members.
| arg | type | description |
|---|---|---|
| self | Parser |
parseClassMember(self: Parser) → Node
Parse one class member: a method, accessor, or field, with optional `static`, `async`, and `get`/`set` prefixes.
| arg | type | description |
|---|---|---|
| self | Parser |
memberPrefixEndsHere(self: Parser) → boolean
A `static`/`async`/`get`/`set` word is a member name rather than a prefix when the next token is a member terminator (`(`, `=`, `;`, `}`) or a line break.
| arg | type | description |
|---|---|---|
| self | Parser |
collectPatternNames(pat: Node, out: { string }) → void
Collect the binding names a declaration pattern introduces, recursing through array, object, assignment, and rest patterns.
| arg | type | description |
|---|---|---|
| pat | Node | |
| out | { string } |
isContextual(self: Parser, word: string) → boolean
Match a contextual word (`from`, `as`) that lexes as a plain identifier.
| arg | type | description |
|---|---|---|
| self | Parser | |
| word | string |
expectContextual(self: Parser, word: string) → Token
Consume a contextual word, raising if the current token is not it.
| arg | type | description |
|---|---|---|
| self | Parser | |
| word | string |
parseModuleName(self: Parser) → string
Read a module import/export name: an identifier, a keyword used as a name, or a string-literal name.
| arg | type | description |
|---|---|---|
| self | Parser |
parseBindingName(self: Parser) → string
Read a local binding identifier for an import specifier.
| arg | type | description |
|---|---|---|
| self | Parser |
recordExport(self: Parser, name: string, tok: Token?) → void
Record an exported name onto the module surface, rejecting a duplicate export of the same name.
| arg | type | description |
|---|---|---|
| self | Parser | |
| name | string | |
| tok | Token? |
parseModuleDeclaration(self: Parser) → Node
Dispatch a top-level module item to the import or export parser.
| arg | type | description |
|---|---|---|
| self | Parser |
parseNamedImports(self: Parser, specifiers: { any }) → void
Parse the `{ a, b as c }` clause of an import, appending named specifiers.
| arg | type | description |
|---|---|---|
| self | Parser | |
| specifiers | { any } |
finishImport(self: Parser, kwTok: Token, source: string, specifiers: { any }) → Node
Build an ImportDecl node and record its specifier on the module surface.
| arg | type | description |
|---|---|---|
| self | Parser | |
| kwTok | Token | |
| source | string | |
| specifiers | { any } |
parseImportDecl(self: Parser) → Node
Parse an `import` declaration in each of its forms: side-effect-only, default, namespace, named, and default combined with namespace or named.
| arg | type | description |
|---|---|---|
| self | Parser |
parseExportDecl(self: Parser) → Node
Parse an `export` declaration, dispatching to default, named/re-export, and declaration forms, and rejecting `export *`.
| arg | type | description |
|---|---|---|
| self | Parser |
parseExportDefault(self: Parser, kwTok: Token) → Node
Parse `export default <expression|function|class>`.
| arg | type | description |
|---|---|---|
| self | Parser | |
| kwTok | Token |
parseExportNamed(self: Parser, kwTok: Token) → Node
Parse `export { a, b as c }` and its `from "s"` re-export variant. A re-export creates no local binding.
| arg | type | description |
|---|---|---|
| self | Parser | |
| kwTok | Token |
parseExportDeclaration(self: Parser, kwTok: Token) → Node
Parse `export <var/let/const/function/class declaration>`. The declaration also binds locally, and each bound name becomes an exported name.
| arg | type | description |
|---|---|---|
| self | Parser | |
| kwTok | Token |
parseProgram(self: Parser) → Node
Parse a whole program: a list of statements terminated by eof.
| arg | type | description |
|---|---|---|
| self | Parser |
parse(src: string, options: (string | ParseOptions) → void
| arg | type | description |
|---|---|---|
| src | string | |
| options | (string | ParseOptions |
parseExpr(src: string) → Node
| arg | type | description |
|---|---|---|
| src | string |
Node = {Parser = typeof(setmetatable(Sub-parts
Everything contained inside this part. Assets are composite children (clickable cards). Files are leaf payloads. Expand any row to view its source.
Problems
Everything affecting this asset right now: its own problems, anything wrong inside it, and problems on its direct dependencies.
agent_score is exposed.+ quality × 0.35
+ performance × 0.25
± compat factor
Usability ratings
Did the part work as advertised when consumers tried to drop it in. Separate from upvotes: those are taste; this is "did it function".
Scoped to this part · feeds back into the world's score.