OLD | NEW |
(Empty) | |
| 1 part of petitparser.debug; |
| 2 |
| 3 /// Handler function for the [ContinuationParser]. |
| 4 typedef Result ContinuationHandler( |
| 5 Result continuation(Context context), Context context); |
| 6 |
| 7 /// Continuation parser that when activated captures a continuation function |
| 8 /// and passes it together with the current context into the handler. |
| 9 /// |
| 10 /// Handlers are not required to call the continuation, but can completely ignor
e |
| 11 /// it, call it multiple times, and/or store it away for later use. Similarly |
| 12 /// handlers can modify the current context and/or modify the returned result. |
| 13 /// |
| 14 /// The following example shows a simple wrapper. Messages are printed before an
d |
| 15 /// after the `digit()` parser is activated: |
| 16 /// |
| 17 /// var wrapped = digit(); |
| 18 /// var parser = new ContinuationParser(wrapped, (continuation, context) { |
| 19 /// print('Parser will be activated, the context is $context.'); |
| 20 /// var result = continuation(context); |
| 21 /// print('Parser was activated, the result is $result.'); |
| 22 /// return result; |
| 23 /// }); |
| 24 /// |
| 25 /// See [profile], [progress], and [trace] for more elaborate examples. |
| 26 class ContinuationParser extends DelegateParser { |
| 27 final ContinuationHandler handler; |
| 28 |
| 29 ContinuationParser(parser, this.handler) : super(parser); |
| 30 |
| 31 @override |
| 32 Result parseOn(Context context) { |
| 33 return handler((result) => super.parseOn(result), context); |
| 34 } |
| 35 |
| 36 @override |
| 37 Parser copy() => new ContinuationParser(children[0], handler); |
| 38 } |
OLD | NEW |