forked from antlr/grammars-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.js
65 lines (59 loc) · 1.68 KB
/
preprocess.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
// Node.js v10
// Minimal preprocessor for TJS2
// See https://krkrz.github.io/krkr2doc/tjs2doc/contents/pp.html
const fs = require('fs');
const vm = require('vm');
const fstr = fs.readFileSync(0).toString(); // STDIN=0
const lines = fstr.split(/\r?\n/);
// You may need modify this
let sandbox = {
version: 0x02040009,
kirikiriz: 1,
environment: 0,
KAGHOOK_EVENTACTION: 0,
APPEND_EXTENTION: 0,
DEBUG: 0,
PACKED: 0,
SYSVOICE_USE_CLEARTYPEVOICE: 0,
};
vm.createContext(sandbox);
const exec = (code) => {
try {
return vm.runInContext(code, sandbox);
} catch(e){
console.error(`// Preprocessor: ${e.toString()}`)
}
}
let canOutput = true;
const mapped = lines.map((l, i) => {
const line = l.trim()
let writeback = l;
if (!canOutput) writeback = '//' + l;
if (line[0] === '@' && (line[1] !== '"' && line[1] !== "'")) {
const match = line.match(/\@([^(]*)(\(.*\))?/);
writeback = '// ' + l;
switch (match[1].trim()) {
case "if":
if (!canOutput) break;
const cond = match[2];
const result = exec(cond);
if (!result) {
canOutput = false;
}
break;
case "endif":
canOutput = true;
break;
case "set":
if (!canOutput) break;
exec(match[2]);
break;
default:
// console.error(` Preprocessor unknown command at ${i}: '${line}', '${match[1]}'`);
writeback = l;
break;
}
}
return writeback;
});
console.log(mapped.join('\n'));