-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfold_many0.dart
50 lines (43 loc) · 1.1 KB
/
fold_many0.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
part of '../../multi.dart';
class FoldMany0<I, O> extends ParserBuilder<I, O> {
static const _template = '''
var {{acc}} = {{initialize}};
while (true) {
{{var1}}
{{p1}}
if (!state.ok) {
break;
}
final {{v}} = {{val1}};
{{combine}};
}
state.ok = true;
if (state.ok) {
{{res0}} = {{acc}};
}''';
static const _templateFast = '''
while (true) {
{{p1}}
if (!state.ok) {
break;
}
}
state.ok = true;''';
final SemanticAction<O> combine;
final SemanticAction<O> initialize;
final ParserBuilder<I, O> parser;
const FoldMany0(this.parser, this.initialize, this.combine);
@override
String build(Context context, ParserResult? result) {
final fast = result == null;
final values = context.allocateLocals(['acc', 'v']);
final r1 = context.getResult(parser, !fast);
values.addAll({
'combine':
combine.build(context, 'combine', [values['acc']!, values['v']!]),
'initialize': initialize.build(context, 'initialize', []),
'p1': parser.build(context, r1),
});
return render2(fast, _templateFast, _template, values, [result, r1]);
}
}