| OLD | NEW |
| (Empty) |
| 1 part of debug; | |
| 2 | |
| 3 /** | |
| 4 * Returns a transformed [parser] that when being used measures | |
| 5 * the activation count and total time of each parser. | |
| 6 * | |
| 7 * For example, the snippet | |
| 8 * | |
| 9 * var parser = letter() & word().star(); | |
| 10 * profile(parser).parse('f1234567890'); | |
| 11 * | |
| 12 * produces the following output: | |
| 13 * | |
| 14 * 1 2006 Instance of 'SequenceParser' | |
| 15 * 1 697 Instance of 'PossessiveRepeatingParser'[0..*] | |
| 16 * 11 406 Instance of 'CharacterParser'[letter or digit expected] | |
| 17 * 1 947 Instance of 'CharacterParser'[letter expected] | |
| 18 * | |
| 19 * The first number refers to the number of activations of each parser, and | |
| 20 * the second number is the microseconds spent in this parser and all its | |
| 21 * children. | |
| 22 */ | |
| 23 Parser profile(Parser root, [OutputHandler output = print]) { | |
| 24 var count = new Map(); | |
| 25 var watch = new Map(); | |
| 26 var parsers = new List(); | |
| 27 return new ContinuationParser(transformParser(root, (parser) { | |
| 28 parsers.add(parser); | |
| 29 return new ContinuationParser(parser, (continuation, context) { | |
| 30 count[parser]++; | |
| 31 watch[parser].start(); | |
| 32 var result = continuation(context); | |
| 33 watch[parser].stop(); | |
| 34 return result; | |
| 35 }); | |
| 36 }), (continuation, context) { | |
| 37 parsers.forEach((parser) { | |
| 38 count[parser] = 0; | |
| 39 watch[parser] = new Stopwatch(); | |
| 40 }); | |
| 41 var result = continuation(context); | |
| 42 parsers.forEach((parser) { | |
| 43 output('${count[parser]}\t' | |
| 44 '${watch[parser].elapsedMicroseconds}\t' | |
| 45 '${parser}'); | |
| 46 }); | |
| 47 return result; | |
| 48 }); | |
| 49 } | |
| OLD | NEW |