progress<R> function

  1. @useResult
Parser<R> progress<R>(
  1. Parser<R> root, {
  2. VoidCallback<ProgressFrame> output = print,
  3. Predicate<Parser>? predicate,
})

Returns a transformed Parser that when being used to read input visually prints its progress while progressing.

For example, the snippet

final parser = letter() & word().star();
progress(parser).parse('f123');

prints the following output:

* Instance of 'SequenceParser'
* Instance of 'SingleCharacterParser'[letter expected]
** Instance of 'PossessiveRepeatingParser'[0..*]
** Instance of 'SingleCharacterParser'[letter or digit expected]
*** Instance of 'SingleCharacterParser'[letter or digit expected]
**** Instance of 'SingleCharacterParser'[letter or digit expected]
***** Instance of 'SingleCharacterParser'[letter or digit expected]

Jumps backwards mean that the parser is back-tracking. Often choices can be reordered to avoid such expensive parses.

The optional output callback can be used to continuously receive ProgressFrame updates with the current progress information.

Implementation

@useResult
Parser<R> progress<R>(Parser<R> root,
    {VoidCallback<ProgressFrame> output = print,
    Predicate<Parser>? predicate}) {
  return transformParser(root, <R>(parser) {
    if (predicate == null || predicate(parser)) {
      return parser.callCC((continuation, context) {
        output(_ProgressFrame(parser, context));
        return continuation(context);
      });
    } else {
      return parser;
    }
  });
}