Skip to main content

parse

Function parse 

Source
pub fn parse<P>(
    parser: &mut P,
    initial: P::Token,
) -> Response<P::Action, P::TimerId>
Expand description

Process a token through a shift-reduce parser, producing a Response.

§Loop semantics

The driver loop calls ShiftReduceParser::decide on the current token. Depending on the result:

ResultActions accumulatedon_reduce calledNext iteration
ShiftNoReturn immediately
ReduceYesYesReturn immediately
ReduceAndContinueYesYesLoop with remaining
PassThroughNoReturn immediately

§consumed semantics

The returned Response::consumed flag is true if any of the following is true:

  • The terminal action was Shift or Reduce (event was handled), or
  • At least one ReduceAndContinue step accumulated actions before a PassThrough was reached.

The second case means that even if the final PassThrough would normally mean “not consumed,” the event is still considered consumed because the parser did useful work in earlier iterations of the loop.

§Termination

The loop always terminates because every path through decide either returns a terminal variant (Shift, Reduce, PassThrough) — which causes an immediate return — or returns ReduceAndContinue with a new token. The new token is always a value that already existed in the caller’s token stream; the grammar must ensure no cycle exists (e.g., a token that always ReduceAndContinues into itself). In practice, the remaining token is typically processed via a different match arm that does not produce another ReduceAndContinue.