-
Notifications
You must be signed in to change notification settings - Fork 0
/
none_of_of.dart
74 lines (69 loc) · 1.73 KB
/
none_of_of.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
63
64
65
66
67
68
69
70
71
72
73
74
part of '../../character.dart';
/// Parses a single character and returns any character if it is not in the
/// list of characters obtained as the result returned by the parser [chars].
///
/// Example:
/// ```dart
/// NoneOfOf(GetChars()))
/// ```
class NoneOfOf extends ParserBuilder<String, int> {
static const _template = '''
if (state.pos < source.length) {
{{var1}}
{{p1}}
if (state.ok) {
final pos = state.pos;
final c = source.readRune(state);
final list = {{val1}};
for (var i = 0; i < list.length; i++) {
final ch = list[i];
if (c == ch) {
state.pos = pos;
state.ok = false;
state.fail(state.pos, ParseError.character);
break;
}
}
if (state.ok) {
{{res0}} = c;
}
}
} else {
state.fail(state.pos, ParseError.character);
state.ok = false;
}''';
static const _templateFast = '''
if (state.pos < source.length) {
{{var1}}
{{p1}}
if (state.ok) {
final pos = state.pos;
final c = source.readRune(state);
final list = {{val1}};
for (var i = 0; i < list.length; i++) {
final ch = list[i];
if (c == ch) {
state.pos = pos;
state.ok = false;
state.fail(state.pos, ParseError.character);
break;
}
}
}
} else {
state.fail(state.pos, ParseError.character);
state.ok = false;
}''';
final ParserBuilder<String, List<int>> chars;
const NoneOfOf(this.chars);
@override
String build(Context context, ParserResult? result) {
context.refersToStateSource = true;
final fast = result == null;
final r1 = context.getResult(chars, true);
final values = {
'p1': chars.build(context, r1),
};
return render2(fast, _templateFast, _template, values, [result, r1]);
}
}