parser

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 8 Imported by: 0

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

func Parse(sourceName, source string) (*ast.Program, error)

Parse parses source into a *ast.Program. sourceName is used in error messages and stored on the program.

func ParseEval

func ParseEval(sourceName, source string, ec EvalContext) (*ast.Program, error)

ParseEval parses the source of a direct eval, seeding the surrounding context so context-sensitive early errors match the call site.

func ParseModule

func ParseModule(sourceName, source string) (*ast.Program, error)

ParseModule parses source under the Module goal symbol, so `export` declarations are permitted and the code is strict by default. It is used to load the target of a dynamic import().

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL