-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentifier_expression.dart
94 lines (86 loc) · 2.65 KB
/
identifier_expression.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
part of '../../expression.dart';
class IdentifierExpression extends ParserBuilder<String, String> {
static const _template = '''
final {{pos}} = state.pos;
state.ok = state.pos < source.length;
if (state.ok) {
final c = source.{{readStart}};
state.ok = {{identStart}};
if (state.ok) {
while (state.pos < source.length) {
final pos = state.pos;
final c = source.{{readCont}};
state.ok = {{identCont}};
if (!state.ok) {
state.pos = pos;
break;
}
}
final word = source.slice({{pos}}, state.pos);
const words = <String>{{words}};
state.ok = words.isEmpty || !words.contains(word);
if (state.ok) {
{{res0}} = word;
}
}
}
if (!state.ok) {
state.pos = {{pos}};
state.fail(state.pos, ParseError.expected, 'identifier');
}''';
static const _templateFast = '''
final {{pos}} = state.pos;
state.ok = state.pos < source.length;
if (state.ok) {
final c = source.{{readStart}};
state.ok = {{identStart}};
if (state.ok) {
while (state.pos < source.length) {
final pos = state.pos;
final c = source.{{readCont}};
state.ok = {{identCont}};
if (!state.ok) {
state.pos = pos;
break;
}
}
final word = source.slice({{pos}}, state.pos);
const words = <String>{{words}};
state.ok = words.isEmpty || !words.contains(word);
}
}
if (!state.ok) {
state.pos = {{pos}};
state.fail(state.pos, ParseError.expected, 'identifier');
}''';
final SemanticAction<bool> identCont;
final SemanticAction<bool> identStart;
final List<String> reservedWords;
const IdentifierExpression(
this.reservedWords, this.identStart, this.identCont);
@override
String build(Context context, ParserResult? result) {
for (final item in reservedWords) {
if (item.isEmpty) {
throw ArgumentError(
'The list of reserved words must not contain empty elements');
}
}
context.refersToStateSource = true;
final fast = result == null;
final values = context.allocateLocals(['pos']);
final isContUnicode = identCont.isUnicode;
final isStartUnicode = identStart.isUnicode;
final sortedWords = reservedWords.toSet().toList();
sortedWords.sort();
values.addAll({
'identCont': identCont.build(context, 'identCont', ['c']),
'identStart': identStart.build(context, 'identStart', ['c']),
'readCont': isContUnicode ? 'readRune(state)' : 'codeUnitAt(state.pos++)',
'readStart':
isStartUnicode ? 'readRune(state)' : 'codeUnitAt(state.pos++)',
'words': helper.getAsCode(reservedWords.toSet()),
});
return render2(fast, _templateFast, _template, values, [result]);
}
}