Skip to main content

parse_with

Function parse_with 

pub fn parse_with(
    job: ParseJob<'_, '_>,
    transform: Option<&mut dyn ReduceCallback>,
) -> Result<ParsedTree, ParseError>
Expand description

Parse with an explicit lexer source (04: the single shared shift/reduce loop).

Flow: lex → feed_token shift/reduce loop → synthetic $END feed (a reject raises UnexpectedToken('$END')) → finish_parse (re-wraps a root Splice). propagate_positions fills each reduce’s crate::model::Meta (see lalr::materialize); off-mode leaves the arena empty. Both arms feed only non-%ignore tokens (the lexer drops them).

  • Basic: iterate the basic lexer over the whole input, context-free.
  • Custom: pull the caller’s crate::custom::TokenSource one token at a time, keyed on the live parser state like the contextual arm (Lark hands the custom lexer the live parser_state). Token positions are the source’s verbatim ($END borrows them unchanged); an unknown type name is Lark’s KeyError analogue — ParseError::UnexpectedToken with the current-state accepts; a source failure keeps its class (crate::custom::CustomLexError). Postlex composes on top, same as the other arms (Lark’s PostLexConnector wraps a custom lexer too).
  • Contextual: drive [ContextualLexer::next_token] one token at a time, keyed on the current parser state (state.position(); Lark’s parser_state.position), threading one LexerState across the whole parse. A LexError::WrongContextToken (the char is a real terminal but not in this state) maps to ParseError::UnexpectedToken with the root-lexed token and the precise accepts set at the current state (parse_from_state’s interactive_parser attach; the carried per-state allowed is intentionally dropped — see [ParserState::context_unexpected_token]).

A $END reject is returned as-is (no UnexpectedEOF rewrite); an ordinary lexer failure surfaces as ParseError::UnexpectedCharacters (basic path: the basic-lexer allowed; contextual path: the parser-context per-state allowed after the root re-raise).

transform: when Some, the reduce-time transform seam replaces the tree builder — the boxed crate::model::ReduceCallback fires once per reduction (named / _-splice / __-synthetic alike) and its return becomes the reduced node’s value (Lark’s embedded transformer=, LALR-only). The returned ParsedTree’s root is then the start rule’s transformed value (a Tree, or a binding Foreign); the arenas carry any trees the hooks built via crate::model::ReduceCx::build_tree. None is the ordinary tree-building parse. The facade wave boxes its binding transformer and passes it here.