-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathgenerate-tokenizer-tests.js
executable file
·67 lines (57 loc) · 1.4 KB
/
generate-tokenizer-tests.js
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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
const packageDir = "./packages/wast-parser/test/tokenizer/";
const allIntegers = fs
.readFileSync(path.join(packageDir, "raw/int_literals.txt"), "utf-8")
.split("\n")
.map(s => s.trim())
.filter(s => s.length > 0);
const allFloats = fs
.readFileSync(path.join(packageDir, "raw/float_literals.txt"), "utf-8")
.split("\n")
.map(s => s.trim())
.filter(s => s.length > 0);
const all = [...allIntegers, ...allFloats];
const normalizeName = name =>
name
.replace(/^\+/, "plus-")
.replace("+", "-plus-")
.replace(/^\./, "dot-")
.replace(".", "-dot-")
.replace(":", "-colon-")
.replace("e", "-lowere-")
.replace("E", "-uppere-");
const expected = literal =>
JSON.stringify(
[
{
type: "number",
value: literal,
loc: {
start: {
line: 1,
column: 1
},
end: {
line: 1,
column: literal.length
}
}
}
],
null,
2
);
all.forEach(literal => {
const dir = path.join(
packageDir,
`number-literals/generated/${normalizeName(literal)}/`
);
if (!fs.existsSync(dir)) {
mkdirp.sync(dir);
}
fs.writeFileSync(path.join(dir, "actual.wast"), literal);
fs.writeFileSync(path.join(dir, "expected.json"), expected(literal));
});