OLD | NEW |
(Empty) | |
| 1 part of petitparser.debug; |
| 2 |
| 3 /// Returns a transformed [parser] that when being used to read input prints a |
| 4 /// trace of all activated parsers and their respective parse results. |
| 5 /// |
| 6 /// For example, the snippet |
| 7 /// |
| 8 /// var parser = letter() & word().star(); |
| 9 /// trace(parser).parse('f1'); |
| 10 /// |
| 11 /// produces the following output: |
| 12 /// |
| 13 /// Instance of 'SequenceParser' |
| 14 /// Instance of 'CharacterParser'[letter expected] |
| 15 /// Success[1:2]: f |
| 16 /// Instance of 'PossessiveRepeatingParser'[0..*] |
| 17 /// Instance of 'CharacterParser'[letter or digit expected] |
| 18 /// Success[1:3]: 1 |
| 19 /// Instance of 'CharacterParser'[letter or digit expected] |
| 20 /// Failure[1:3]: letter or digit expected |
| 21 /// Success[1:3]: [1] |
| 22 /// Success[1:3]: [f, [1]] |
| 23 /// |
| 24 /// Indentation signifies the activation of a parser object. Reverse indentation |
| 25 /// signifies the returning of a parse result either with a success or failure |
| 26 /// context. |
| 27 Parser trace(Parser parser, [OutputHandler output = print]) { |
| 28 var level = 0; |
| 29 return transformParser(parser, (each) { |
| 30 return new ContinuationParser(each, (continuation, context) { |
| 31 output('${_repeat(level, ' ')}$each'); |
| 32 level++; |
| 33 var result = continuation(context); |
| 34 level--; |
| 35 output('${_repeat(level, ' ')}$result'); |
| 36 return result; |
| 37 }); |
| 38 }); |
| 39 } |
OLD | NEW |