-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminated.dart
41 lines (35 loc) · 877 Bytes
/
terminated.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
part of '../../sequence.dart';
class Terminated<I, O> extends ParserBuilder<I, O> {
static const _template = '''
final {{pos}} = state.pos;
{{p1}}
if (state.ok) {
{{p2}}
if (!state.ok) {
{{res0}} = null;
state.pos = {{pos}};
}
}''';
static const _templateFast = '''
final {{pos}} = state.pos;
{{p1}}
if (state.ok) {
{{p2}}
if (!state.ok) {
state.pos = {{pos}};
}
}''';
final ParserBuilder<I, O> parser;
final ParserBuilder<I, dynamic> terminate;
const Terminated(this.parser, this.terminate);
@override
String build(Context context, ParserResult? result) {
final fast = result == null;
final values = context.allocateLocals(['pos']);
values.addAll({
'p1': parser.build(context, result),
'p2': terminate.build(context, null),
});
return render2(fast, _templateFast, _template, values, [result]);
}
}