OLD | NEW |
| (Empty) |
1 part of reflection; | |
2 | |
3 /** | |
4 * A function transforming one parser to another one. | |
5 */ | |
6 typedef Parser TransformationHandler(Parser parser); | |
7 | |
8 /** | |
9 * Transforms all parsers reachable from [parser] with the given [handler]. | |
10 * The identity function returns a copy of the the incoming parser. | |
11 * | |
12 * The implementation first creates a copy of each parser reachable in the | |
13 * input grammar; then the resulting grammar is traversed until all references | |
14 * to old parsers are replaced with the transformed ones. | |
15 */ | |
16 Parser transformParser(Parser parser, TransformationHandler handler) { | |
17 var mapping = new Map.identity(); | |
18 for (var each in allParser(parser)) { | |
19 mapping[each] = handler(each.copy()); | |
20 } | |
21 var seen = new Set.from(mapping.values); | |
22 var todo = new List.from(mapping.values); | |
23 while (todo.isNotEmpty) { | |
24 var parent = todo.removeLast(); | |
25 for (var child in parent.children) { | |
26 if (mapping.containsKey(child)) { | |
27 parent.replace(child, mapping[child]); | |
28 } else if (!seen.contains(child)) { | |
29 seen.add(child); | |
30 todo.add(child); | |
31 } | |
32 } | |
33 } | |
34 return mapping[parser]; | |
35 } | |
OLD | NEW |