Skip to main content

Module parser

Module parser 

Source
Expand description

The parser: tokens to crate::ast.

A recursive-descent parser with precedence climbing for expressions. It follows the specification’s grammar productions closely, which keeps the early errors next to the code that can detect them.

Two productions need backtracking / reinterpretation, exactly as in an engine:

  • arrow functions use the cover grammar. ( starts either a parenthesized expression or a parameter list; the parser speculatively parses a parameter list and commits only if => follows, otherwise it restores the lexer and parses an expression.
  • let at statement start is a declaration keyword when followed by a binding, otherwise an identifier.

Automatic semicolon insertion is implemented where the grammar allows it (Stmt terminators, do…while, return/throw restricted productions, postfix ++/--, async before arrow parameters, yield/await).

Early errors implemented here (not at run time):

  • with, legacy octal literals/escapes, delete identifier, eval/arguments as binding or assignment target — strict mode only;
  • super outside a method, super() outside a derived constructor, new.target outside a function;
  • await/yield used as identifiers where they are reserved;
  • duplicate __proto__ in an object literal, duplicate private names;
  • break/continue target validity, return outside a function;
  • ?? mixed with &&/|| without parentheses, -x ** y, assignment to a non-assignable target, new on an optional chain;
  • duplicate parameter names (always for arrows/methods/non-simple lists).

Not implemented yet (tracked for M1b.2): scope-analysis early errors such as let x; let x; in the same block, and parameter/var conflicts.

Structs§

EvalContext
What the code that called a direct eval allows its body to use.
Parser
ECMAScript 2025 parser.

Constants§

DEFAULT_MAX_DEPTH
Default nesting limit.

Functions§

parse_eval_script
Parse src as a direct eval body, inheriting new.target from the caller.
parse_eval_script_in
Parse src as a direct eval body with the caller’s privileges.
parse_expression
Parse src as a single Expression (used by tests and by eval-like entry points that need the expression grammar only).
parse_module
Parse src as a Module.
parse_script
Parse src as a Script.
parse_script_strict
Parse src as a Script whose code is strict from the first token.
parse_suspensible_global_eval
Parse src as the suspensible (async) form of a global eval body.
parse_suspensible_script
Parse src as a Script whose top level is the body of an async function.