during<R> method

Parser<R> during<R>(
  1. Parser<R> parser
)

Runs parser in a deeper indentation scope.

Before running parser, verifies that the next line has a deeper indentation than the current level and pushes the previous level onto the indentation stack. After parser succeeds, pops and restores the previous indentation level.

If parser fails, the indentation state is automatically rolled back to its state before entering the block, preserving the original parse failure position and message. This ensures that subsequent branches in choice combinators can backtrack safely without state corruption.

For example, to parse an indented block of statements:

final block = indent.during(statement.plus());

Implementation

Parser<R> during<R>(Parser<R> parser) =>
    [parser, failure<R>().skip(before: decrease)]
        .toChoiceParser(failureJoiner: selectFirst)
        .skip(before: increase, after: decrease);