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):
- At an expression start, a
TokenKind::Slash/TokenKind::SlashEqtoken is re-lexed as a regular-expression literal directly from the source text, using the same lexical rules ascrate::scanner::Scanner::rescan_regex. The tokens covered by the new literal are replaced so the stored token stream still tiles the source. - When a type-argument, type-parameter, or heritage list closes, a greedily
formed
>>/>>>/>=-family token is split into a singleTokenKind::GreaterThanplus its remainder token, mirroringcrate::scanner::Scanner::rescan_greater_than. The symmetric split is applied to<<when a type context opens.
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.