Skip to main content

Module parser

Module parser 

Source
Expand description

The RON parser: tokens → a lossless rowan green tree, wrapped in CstDocument.

Hand-written recursive-descent (AD-006) feeding rowan::GreenNodeBuilder. It covers the full RON 0.12 value surface (T014): structs (named + anonymous), tuples, lists/sequences, maps (incl. non-string keys), enum variants, unit (), Option/implicit_some, and extension attributes.

§Trivia (AD-001, T015)

Trivia is attached per the rule documented in crate::syntax: leading trivia (whitespace / comments / BOM) binds to the following significant token; trailing trivia at EOF binds to the SyntaxKind::Root node. The parser realizes this by, before consuming any significant token, flushing all pending trivia tokens into the current open node, then any trailing trivia at EOF into the still-open Root node.

§Losslessness (INV-1/INV-2)

Every lexer token — significant or trivia — is emitted into the green tree exactly once, in source order, so concatenating all token texts reproduces the input byte-for-byte. This holds for valid input and for malformed input that triggers error recovery (INV-3).

§Error recovery + diagnostics (OBJ2, T021–T024)

Malformed or incomplete input never panics and never drops bytes. The parser:

  • wraps unexpected tokens in SyntaxKind::Error nodes and represents absent constructs as missing/empty nodes, using recovery sets on , ) ] } and field identifiers (T021), so the tree always covers all input (INV-3);
  • emits exactly one Diagnostic per recovery point with a precise byte range (T022, TR-006/TR-013) and enforces the must-consume-a-token invariant — every loop iteration consumes ≥ 1 token — so parsing always terminates (HINT-004);
  • enforces a configurable nesting-depth guard (default 128, ParseOptions) that stops descent at the limit, emits a DiagnosticCode::NestingDepthExceeded diagnostic, and still tokenizes the remaining bytes into Error nodes so no stack overflow occurs and byte coverage holds (T023, INV-5);
  • is deterministic — identical input yields an identical tree and an identical diagnostics set (same codes, order, and ranges), since parsing is a pure function of the token stream with no nondeterministic inputs (T024, INV-6/TR-012).

Structs§

CstDocument
The parsed, lossless concrete syntax tree of a RON document.
ParseOptions
Configuration for parse_with_options.

Constants§

DEFAULT_MAX_DEPTH
The default nesting-depth guard (AD-005 / TR-014). Descent past this many nested composite values stops and emits an over-limit diagnostic instead of risking a stack overflow.

Functions§

parse
Parse a UTF-8 &str into a lossless CstDocument with the default ParseOptions (depth guard DEFAULT_MAX_DEPTH). Never panics.
parse_bytes
Parse raw bytes into a CstDocument, rejecting non-UTF-8 cleanly.
parse_with_options
Parse a UTF-8 &str into a lossless CstDocument with explicit ParseOptions (e.g. a custom nesting-depth guard). Never panics.