-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestpython.d
291 lines (264 loc) · 7.85 KB
/
testpython.d
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import dparsergen.core.dynamictree;
import dparsergen.core.grammarinfo;
import dparsergen.core.location;
import dparsergen.core.nodetype;
import dparsergen.core.utils;
static import grammarpython;
static import grammarpython_lexer;
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.file;
import std.path;
import std.stdio;
alias Location = LocationAll;
alias Tree = DynamicParseTree!(Location, LocationRangeStartLength);
alias Creator = DynamicParseTreeCreator!(grammarpython, Location, LocationRangeStartLength);
struct LexerWrapper
{
grammarpython_lexer.Lexer!(LocationAll, true) lexer;
alias Location = LocationAll;
alias LocationDiff = typeof(Location.init - Location.init);
this(string input, Location startLocation = Location.init)
{
lexer = grammarpython_lexer.Lexer!(LocationAll, true)(input, startLocation);
skipIgnoredTokens();
}
enum tokenID(string tok) = lexer.tokenID!(tok);
string tokenName(size_t id)
{
return lexer.tokenName(id);
}
ref front()
{
if (hasIndentDedentToken)
return indentDedentToken;
return lexer.front;
}
bool empty()
{
return !hasIndentDedentToken && lexer.empty;
}
void popFront()
{
if (hasIndentDedentToken)
{
hasIndentDedentToken = false;
if (currentSpace.length < indentStack.data[$ - 1].length)
{
indentStack.shrinkTo(indentStack.data.length - 1);
string lastSpace = indentStack.data.length ? indentStack.data[$ - 1] : "";
if (currentSpace.length < lastSpace.length)
{
enforce(lastSpace.startsWith(currentSpace));
hasIndentDedentToken = true;
indentDedentToken = typeof(lexer.front)("", lexer.tokenID!"Dedent", lexer.front.currentLocation);
}
else
enforce(lastSpace == currentSpace, text("lastSpace=\"", lastSpace, "\" currentSpace=\"", currentSpace, "\""));
}
return;
}
if (lexer.front.symbol.among(lexer.tokenID!"\"(\"", lexer.tokenID!"\"[\"", lexer.tokenID!"\"{\""))
{
parenDepth++;
}
else if (lexer.front.symbol.among(lexer.tokenID!"\")\"", lexer.tokenID!"\"]\"", lexer.tokenID!"\"}\""))
{
enforce(parenDepth);
parenDepth--;
}
lexer.popFront();
skipIgnoredTokens();
}
bool isLineStart = true;
bool hasIndentDedentToken;
typeof(lexer.front) indentDedentToken;
string lastSpace;
string currentSpace;
Appender!(string[]) indentStack;
size_t parenDepth;
this(this)
{
auto origInputStack = indentStack;
indentStack = Appender!(string[]).init;
indentStack.put(origInputStack.data);
}
void skipIgnoredTokens()
{
while (!lexer.empty)
{
if (lexer.front.symbol == lexer.tokenID!"Newline")
{
if (parenDepth || isLineStart)
{
currentSpace = "";
lexer.popFront;
continue;
}
isLineStart = true;
currentSpace = "";
}
else if (lexer.front.symbol == lexer.tokenID!"Space")
{
currentSpace = lexer.front.content;
}
else if (!lexer.front.isIgnoreToken)
{
if (isLineStart)
{
string lastSpace = indentStack.data.length ? indentStack.data[$ - 1] : "";
if (currentSpace.length > lastSpace.length)
{
enforce(currentSpace.startsWith(lastSpace));
hasIndentDedentToken = true;
indentDedentToken = typeof(lexer.front)("", lexer.tokenID!"Indent", lexer.front.currentLocation);
indentStack.put(currentSpace);
}
else if (currentSpace.length < lastSpace.length)
{
enforce(lastSpace.startsWith(currentSpace));
hasIndentDedentToken = true;
indentDedentToken = typeof(lexer.front)("", lexer.tokenID!"Dedent", lexer.front.currentLocation);
}
else
enforce(currentSpace == lastSpace);
}
isLineStart = false;
}
if (lexer.front.isIgnoreToken)
{
lexer.popFront;
}
else
{
break;
}
}
if (lexer.empty)
{
currentSpace = "";
if (indentStack.data.length)
{
hasIndentDedentToken = true;
indentDedentToken = typeof(lexer.front)("", lexer.tokenID!"Dedent", lexer.front.currentLocation);
}
}
}
}
int runTests(string dir)
{
int r = 0;
size_t testCount;
foreach (filename; dirEntries(dir, "*.py", SpanMode.breadth))
{
string f = baseName(filename);
if (f.startsWith("bad"))
continue;
string inText = cast(string) read(filename);
auto creator = new Creator;
try
{
auto tree = grammarpython.parse!(Creator,
LexerWrapper)(inText, creator,
LocationAll.init);
assert(tree.inputLength.bytePos <= inText.length);
}
catch (Exception e)
{
stderr.writeln(f, "\n", e);
r = 1;
}
testCount++;
}
if (testCount < 902)
{
stderr.writeln("Less tests than expected: ", testCount);
r = 1;
}
return r;
}
immutable help = q"EOS
Usage: testpython [OPTIONS] filename.py
-v Verbose output
--test-dir Check test directory from cpython
-h Show this help
EOS";
int main(string[] args)
{
string filename;
string testDir;
bool verbose;
for (size_t i = 1; i < args.length; i++)
{
string arg = args[i];
if (arg.startsWith("-"))
{
if (arg == "-v")
verbose = true;
else if (arg == "--test-dir")
{
if (i + 1 >= args.length)
{
stderr.writeln("Missing argument for ", arg);
return 1;
}
if (filename.length || testDir.length)
{
stderr.writeln("Too many arguments");
return 1;
}
i++;
testDir = args[i];
}
else if (arg == "-h")
{
stderr.write(help);
return 0;
}
else
{
stderr.writeln("Unknown option ", arg);
}
}
else
{
if (filename.length || testDir.length)
{
stderr.writeln("Too many arguments");
return 1;
}
filename = arg;
}
}
if (filename.length == 0 && testDir.length == 0)
{
stderr.write(help);
return 0;
}
if (testDir.length)
{
return runTests(testDir);
}
else
{
string inText = cast(string) read(filename);
Tree tree;
try
{
auto creator = new Creator;
tree = grammarpython.parse!(Creator,
LexerWrapper)(inText, creator,
LocationAll.init);
assert(tree.inputLength.bytePos <= inText.length);
}
catch (Exception e)
{
stderr.writeln(e);
return 1;
}
printTree(stdout, tree, verbose);
}
return 0;
}