forked from Ntsekees/ilmentufa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-camxes.js
45 lines (42 loc) · 1.47 KB
/
build-camxes.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
var start_t = process.hrtime();
var fs = require("fs")
var pegjs = require("pegjs")
var src = (process.argv.length >= 3) ? process.argv[2] : "camxes.pegjs";
var dst = src.replace(/.js.peg$/g, ".pegjs").replace(/^(.*?)(\.[^\\\/]+)?$/g, "$1.js");
console.log("-> " + dst);
var peg = fs.readFileSync(src).toString();
try {
var param = {
cache: true,
trace: false,
output: "source",
allowedStartRules: ["text"],
}
if (typeof pegjs.generate === 'function')
var camxes = pegjs.generate(peg, param);
else if (typeof pegjs.buildParser === 'function')
var camxes = pegjs.buildParser(peg, param);
else throw "ERROR: No parser generator method found in the PEGJS module.";
} catch (e) {
console.log(JSON.stringify(e));
throw e;
}
var fd = fs.openSync(dst, 'w+');
var buffer = new Buffer.from('var camxes = ');
fs.writeSync(fd, buffer, 0, buffer.length);
buffer = new Buffer.from(camxes);
fs.writeSync(fd, buffer, 0, buffer.length);
buffer = new Buffer.from(`
if (typeof module !== 'undefined') {
module.exports = camxes;
if (typeof process !== 'undefined' && require !== 'undefined' && require.main === module) {
var input = process.argv[2];
if (Object.prototype.toString.call(input) === '[object String]')
console.log(JSON.stringify(camxes.parse(input + ' ')));
}
}
`);
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
var diff_t = process.hrtime(start_t);
console.log(`Duration: ${diff_t[0] + diff_t[1] / 1e9} seconds`);