-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsatisfy.dart
172 lines (155 loc) · 4.23 KB
/
satisfy.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
part of '../../character.dart';
/// Parses a single character, and if [chars] satisfies the criteria,
/// returns that character.
///
/// Example:
/// ```dart
/// Satisfy(isSomeChar)
/// ```
class Satisfy extends ParserBuilder<String, int> {
static const _template16 = '''
if (state.pos < source.length) {
final c = source.codeUnitAt(state.pos);
state.ok = {{test}};
if (state.ok) {
state.pos++;
{{res0}} = c;
} else {
state.fail(state.pos, ParseError.character);
}
} else {
state.fail(state.pos, ParseError.character);
}''';
static const _template16Fast = '''
if (state.pos < source.length) {
final c = source.codeUnitAt(state.pos);
state.ok = {{test}};
if (state.ok) {
state.pos++;
} else {
state.fail(state.pos, ParseError.character);
}
} else {
state.fail(state.pos, ParseError.character);
}''';
static const _template32 = '''
final {{pos}} = state.pos;
if ({{pos}} < source.length) {
final c = source.readRune(state);
state.ok = {{test}};
if (state.ok) {
{{res0}} = c;
} else {
state.pos = {{pos}};
state.fail({{pos}}, ParseError.character);
}
} else {
state.fail({{pos}}, ParseError.character);
}''';
static const _template32Fast = '''
final {{pos}} = state.pos;
if ({{pos}} < source.length) {
final c = source.readRune(state);
state.ok = {{test}};
if (!state.ok) {
state.pos = {{pos}};
state.fail({{pos}}, ParseError.character);
}
} else {
state.fail({{pos}}, ParseError.character);
}''';
static const _templateOne16 = '''
if (source.contains1(state.pos, {{c0}})) {
state.ok = true;
state.pos++;
{{res0}} = {{cc}};
} else {
state.fail(state.pos, ParseError.character);
}''';
static const _templateOne16Fast = '''
if (source.contains1(state.pos, {{c0}})) {
state.ok = true;
state.pos++;
} else {
state.fail(state.pos, ParseError.character);
}''';
static const _templateOne32 = '''
if (source.contains2(state.pos, {{c0}}, {{c1}})) {
state.ok = true;
state.pos += 2;
{{res0}} = {{cc}};
} else {
state.fail(state.pos, ParseError.character);
}''';
static const _templateOne32Fast = '''
if (source.contains2(state.pos, {{c0}}, {{c1}})) {
state.ok = true;
state.pos += 2;
} else {
state.fail(state.pos, ParseError.character);
}''';
final SemanticAction<bool> predicate;
const Satisfy(this.predicate);
@override
String build(Context context, ParserResult? result) {
context.refersToStateSource = true;
if (predicate is CharClass) {
final charClass = predicate as CharClass;
final charList = charClass.getCharList();
if (charList.length == 2 && charList[0] == charList[1]) {
return _buildOne(context, result);
}
}
return _build(context, result);
}
String _build(Context context, ParserResult? result) {
final fast = result == null;
final values = context.allocateLocals(['pos']);
values.addAll({
'test': predicate.build(context, 'test', ['c']),
});
final isUnicode = predicate.isUnicode;
final String template;
if (isUnicode) {
if (fast) {
template = _template32Fast;
} else {
template = _template32;
}
} else {
if (fast) {
template = _template16Fast;
} else {
template = _template16;
}
}
return render(template, values, [result]);
}
String _buildOne(Context context, ParserResult? result) {
final fast = result == null;
final charClass = predicate as CharClass;
final charList = charClass.getCharList();
if (charList.length != 2 || charList[0] != charList[1]) {
throw StateError('Internal error');
}
final char = charList[0];
final str = String.fromCharCode(char);
final isUnicode = str.length > 1;
final String template;
if (isUnicode) {
ParseRuntime.addCapability(
context, ParseRuntimeCapability.contains2, true);
template = fast ? _templateOne32Fast : _templateOne32;
} else {
ParseRuntime.addCapability(
context, ParseRuntimeCapability.contains1, true);
template = fast ? _templateOne16Fast : _templateOne16;
}
final values = {
'c0': '${str.codeUnitAt(0)}',
'c1': isUnicode ? '${str.codeUnitAt(1)}' : '',
'cc': '$char',
};
return render(template, values, [result]);
}
}