Skip to main content

Module parser

Module parser 

Source
Expand description

The syntactic parser: a recovered token stream to a recovered SourceFile.

The parser is total: it accepts any scanned token stream, never panics, and always produces a SourceFile whose statement list covers the whole source. Grammar errors are represented with Token::missing tokens and Missing* node products instead of aborting, and every list loop carries a forward-progress guard so recovery can never stall.

§Rescans

Two lexical forms depend on grammar context, and the scanner deliberately refuses to guess them. The parser resolves both with an explicit token-cursor rescan over the already-scanned stream (the design crate::scanner::Scanner documents as the alternative to driving the scanner directly):

Template literals never need a parser rescan here: the single-pass scanner already segments them deterministically with brace tracking.

§Context sensitivity

Contextual keywords are produced by the scanner as dedicated tokens; the parser decides from grammar position whether type, as, namespace, let, and the rest act as keywords or ordinary identifiers. ScriptKind drives the remaining decisions: TypeScript-only syntax in a JavaScript source parses (for recovery) but is diagnosed, <T>expr type assertions exist only in non-React TypeScript, and a < opening JSX in a React source is diagnosed as unsupported because the fixed NodeKind space has no JSX productions.

Functions§

parse
Parses a scanned source into a recovered SourceFile.