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