-
Notifications
You must be signed in to change notification settings - Fork 0
/
separated_list1.dart
62 lines (56 loc) · 1.25 KB
/
separated_list1.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
58
59
60
61
62
part of '../../multi.dart';
class SeparatedList1<I, O> extends ParserBuilder<I, List<O>> {
static const _template = '''
var {{pos}} = state.pos;
final {{list}} = <{{O}}>[];
while (true) {
{{var1}}
{{p1}}
if (!state.ok) {
state.pos = {{pos}};
break;
}
{{list}}.add({{val1}});
{{pos}} = state.pos;
{{p2}}
if (!state.ok) {
break;
}
}
state.ok = {{list}}.isNotEmpty;
if (state.ok) {
{{res0}} = {{list}};
}''';
static const _templateFast = '''
var {{pos}} = state.pos;
var {{ok}} = false;
while (true) {
{{p1}}
if (!state.ok) {
state.pos = {{pos}};
break;
}
{{ok}} = true;
{{pos}} = state.pos;
{{p2}}
if (!state.ok) {
break;
}
}
state.ok = {{ok}};''';
final ParserBuilder<I, O> parser;
final ParserBuilder<I, dynamic> separator;
const SeparatedList1(this.parser, this.separator);
@override
String build(Context context, ParserResult? result) {
final fast = result == null;
final values = context.allocateLocals(['list', 'ok', 'pos']);
final r1 = context.getResult(parser, !fast);
values.addAll({
'O': '$O',
'p1': parser.build(context, r1),
'p2': separator.build(context, null),
});
return render2(fast, _templateFast, _template, values, [result, r1]);
}
}