-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.test.ts
191 lines (147 loc) · 4.6 KB
/
grammar.test.ts
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
import { performance } from 'perf_hooks';
import Parser, { SyntaxNode } from 'tree-sitter';
import orxePaths from './orxe-paths';
interface BaseTestResult {
expected: string[];
matched: string[];
missing?: string[];
unexpected?: string[];
parsed?: string;
}
type TestResult =
& BaseTestResult
& ({ missing: string[] } | { unexpected: string[] });
interface TestResults {
details: Array<[string, TestResult]>;
missing: number;
unexpected: number;
failed: number;
passed: number;
expressionsWithErrors: number;
errorNodes: number;
passingExpressionsWithErrors: string[];
}
const collectErrors = (node: Parser.SyntaxNode): Parser.SyntaxNode[] => {
let errors: Parser.SyntaxNode[] = [];
if (node.hasError() && node.type.match(/^[A-Z]/)) {
errors.push(node);
}
errors.push(...node.children.flatMap(collectErrors));
return errors;
};
(async () => {
const bindings = 'node';
const xPath = require(`../bindings/${bindings}`);
const pathExpressionsQuery = new Parser.Query(xPath, /* clj */`
(path_expr path: (_) @path.field)
(path_expr (step_expr node_test: (_))) @path.node_test
(path_expr (step_expr axis_movement: (_))) @path.axis_mopvement
(path_expr (step_expr (context_item_expr))) @path.named
(context_item_expr) @path.not_named
((path_expr) @path.root (#match? @path.root "^[/.]"))
(id_function_call) @path.id
`);
let results: TestResults | null = null;
let times: Record<string, number> = {};
times = {};
results = {
details: [],
missing: 0,
unexpected: 0,
failed: 0,
passed: 0,
expressionsWithErrors: 0,
errorNodes: 0,
passingExpressionsWithErrors: [],
};
let parsed = 0;
const parser = new Parser();
parser.setLanguage(xPath);
for (const [ expression, paths ] of Object.entries(orxePaths)) {
const start = performance.now();
const { rootNode } = parser.parse(expression);
parsed++;
let matchedPathNodes: Parser.SyntaxNode[] = [];
try {
matchedPathNodes = pathExpressionsQuery.captures(rootNode)
.reduce<SyntaxNode[]>((acc, { name: _name, node }) => {
const previousNodeIndex = acc.length - 1;
const previousNode = acc[previousNodeIndex];
const overlapsPreviousNode = node.startIndex <= (previousNode?.endIndex ?? -1);
if (!overlapsPreviousNode) {
acc.push(node);
}
return acc;
}, []);
}
catch (error) {
results.failed++;
}
const matchedPaths = new Set(matchedPathNodes.reduce<string[]>((acc, node) => {
const { text } = node;
if (/^\s+$/.test(text)) {
return acc;
} else {
acc.push(text.trim());
}
return acc;
}, []));
const expectedPaths = new Set(paths);
let missing: string[] = [];
let unexpected: string[] = [];
for (const path of expectedPaths.values()) {
if (!matchedPaths.has(path)) {
missing.push(path);
}
}
for (const path of matchedPaths.values()) {
if (!expectedPaths.has(path)) {
unexpected.push(path);
}
}
const missingLength = missing.length;
const unexpectedLength = unexpected.length;
const errorNodes = collectErrors(rootNode);
const errorNodesCount = errorNodes.length;
const isPassing = missingLength === 0 && unexpectedLength === 0;
if (errorNodesCount > 0) {
results.expressionsWithErrors += 1;
results.errorNodes += errorNodesCount;
if (isPassing) {
results.passingExpressionsWithErrors.push(expression, String(rootNode), ...errorNodes.map((node) => node.text));
}
}
if (!isPassing) {
let result: BaseTestResult = {
expected: Array.from(expectedPaths),
matched: Array.from(matchedPaths),
};
results.missing += missingLength;
results.unexpected += unexpectedLength;
results.failed++;
if (missingLength > 0) {
result.missing = missing;
}
if (unexpectedLength > 0) {
result.unexpected = unexpected;
}
result.parsed = String(rootNode);
results.details.unshift([expression, result as TestResult]);
// break;
} else {
results.passed++;
}
times[expression] = performance.now() - start;
}
if (results != null) {
const [slowest] = Object.entries(times).sort(([,a], [,b]) => b - a);
const finalResults = {
...results,
slowestResult: [slowest[0], `${slowest[1].toFixed(2)} ms`],
};
console.log('results', JSON.stringify(finalResults, null, 2));
if (results.failed > 0 || results.passingExpressionsWithErrors) {
process.exit(1);
}
}
})();