Documentation
¶
Overview ¶
Package parser implements a recursive-descent parser for JavaScript (ECMAScript), producing an ast.Program from source text.
Design ¶
The parser first tokenizes the entire input into a slice via the lexer package, then walks that slice with an index cursor. Buffering the whole token stream (rather than pulling one token at a time) makes arbitrary lookahead cheap, which the JavaScript grammar needs in several places — most notably to distinguish an arrow function `(a, b) => …` from a parenthesized expression `(a, b)`, and to tell a destructuring pattern from an object or array literal.
Expressions are parsed with precedence climbing (a compact form of Pratt parsing); see parse_expr.go. Statements are parsed by recursive descent; see parse_stmt.go.
Error handling ¶
The first error encountered stops the parse and is returned. Errors carry a token.Span so callers can report a precise source range. Automatic Semicolon Insertion (ECMA-262 §12.10) is implemented in expectSemicolon.
ECMA-262 Reference: §13–§16.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse parses source into a *ast.Program. sourceName is used in error messages and stored on the program.
Types ¶
type EvalContext ¶
type EvalContext struct {
Strict bool
AllowSuperCall bool
AllowSuperProperty bool
AllowNewTarget bool
// InFieldInitializer is true when the direct eval is lexically inside a class
// field initializer or static block (not crossing an ordinary function
// boundary), where a reference to `arguments` is an early SyntaxError.
InFieldInitializer bool
PrivateNames []string
}
EvalContext carries the surrounding lexical context of a direct eval so its code is parsed under the same early-error rules: the private names in scope (so `this.#x` resolves), whether a SuperCall is permitted (inside a derived constructor), and whether the caller is strict-mode code.