-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestgrammarjson.d
154 lines (139 loc) · 3.65 KB
/
testgrammarjson.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
import dparsergen.core.dynamictree;
import dparsergen.core.location;
import dparsergen.core.nodetype;
import dparsergen.core.utils;
import std.algorithm;
import std.array;
import std.file;
import std.path;
import std.stdio;
import P = grammarjson;
static import grammarjson_lexer;
alias Location = LocationAll;
alias L = grammarjson_lexer.Lexer!Location;
alias Creator = DynamicParseTreeCreator!(P, Location, LocationRangeStartLength);
alias Tree = DynamicParseTree!(Location, LocationRangeStartLength);
int runTests(string dir)
{
int r = 0;
size_t testCount;
foreach (filename; dirEntries(dir, "*.json", SpanMode.shallow))
{
string f = baseName(filename);
// TODO: implement depth limit
if (f == "n_structure_100000_opening_arrays.json")
continue;
if (f == "n_structure_open_array_object.json")
continue;
if (f == "i_structure_500_nested_arrays.json")
continue;
string inText = cast(string) read(filename);
auto creator = new Creator;
bool result;
try
{
auto tree = P.parse!(Creator, L)(inText, creator);
assert(tree.inputLength.bytePos <= inText.length);
result = true;
}
catch (Exception e)
{
//stderr.writeln("error");
if (f[0] == 'y')
stderr.writeln(f, "\n", e);
result = false;
}
if ((f[0] == 'y' && !result) || (f[0] == 'n' && result))
{
stderr.writeln(result, " ", f);
r = 1;
}
testCount++;
}
if (testCount < 315)
{
stderr.writeln("Less tests than expected: ", testCount);
r = 1;
}
return r;
}
immutable help = q"EOS
Usage: testgrammarjson [OPTIONS] filename
-v Verbose output
--test-dir Check JSON files in directory
-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 = readText(filename);
auto creator = new Creator;
try
{
auto tree = P.parse!(Creator, L)(inText, creator);
assert(tree.inputLength.bytePos <= inText.length);
printTree(stdout, tree, verbose);
return 0;
}
catch (Exception e)
{
stderr.writeln(e);
return 1;
}
}
}