-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelimited.dart
50 lines (43 loc) · 1 KB
/
delimited.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 '../../sequence.dart';
class Delimited<I, O> extends ParserBuilder<I, O> {
static const _template = '''
final {{pos}} = state.pos;
{{p1}}
if (state.ok) {
{{p2}}
if (state.ok) {
{{p3}}
}
if (!state.ok) {
{{res0}} = null;
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, dynamic> after;
final ParserBuilder<I, dynamic> before;
final ParserBuilder<I, O> parser;
const Delimited(this.before, this.parser, this.after);
@override
String build(Context context, ParserResult? result) {
final fast = result == null;
final values = context.allocateLocals(['pos']);
values.addAll({
'p1': before.build(context, null),
'p2': parser.build(context, result),
'p3': after.build(context, null),
});
return render2(fast, _templateFast, _template, values, [result]);
}
}