-
Notifications
You must be signed in to change notification settings - Fork 0
/
separated_pair.dart
57 lines (50 loc) · 1.27 KB
/
separated_pair.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
51
52
53
54
55
56
57
part of '../../sequence.dart';
class SeparatedPair<I, O1, O2> extends ParserBuilder<I, Result2<O1, O2>> {
static const _template = '''
final {{pos}} = state.pos;
{{var1}}
{{p1}}
if (state.ok) {
{{p2}}
if (state.ok) {
{{var2}}
{{p3}}
if (state.ok) {
{{res0}} = Result2({{val1}}, {{val2}});
}
}
}
if (!state.ok) {
state.pos = {{pos}};
}''';
static const _templateFast = '''
final {{pos}} = state.pos;
{{p1}}
if (state.ok) {
{{p2}}
if (state.ok) {
{{p3}}
}
}
if (!state.ok) {
state.pos = {{pos}};
}''';
final ParserBuilder<I, O1> first;
final ParserBuilder<I, dynamic> separator;
final ParserBuilder<I, O2> second;
const SeparatedPair(this.first, this.separator, this.second);
@override
String build(Context context, ParserResult? result) {
final fast = result == null;
ParseRuntime.addCapability(context, ParseRuntimeCapability.result2, !fast);
final values = context.allocateLocals(['pos']);
final r1 = context.getResult(first, !fast);
final r2 = context.getResult(second, !fast);
values.addAll({
'p1': first.build(context, r1),
'p2': separator.build(context, null),
'p3': second.build(context, r2),
});
return render2(fast, _templateFast, _template, values, [result, r1, r2]);
}
}