Spaces:
Running
Running
File size: 1,793 Bytes
4afcdfb |
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 |
var start_t = process.hrtime();
var fs = require("fs");
var pathmod = require("path");
var pegjs = require("pegjs");
var src = (process.argv.length >= 3) ? process.argv[2] : "camxes.pegjs";
if (pathmod.extname(src) === ".peg") {
var pegjs_conv = require("./pegjs_conv.js");
var peg_src = src;
// src = pathmod.dirname(peg_src) + pathmod.sep + pathmod.basename(peg_src) + ".pegjs";
src = pegjs_conv.convert_file(peg_src);
console.log("-> " + src);
}
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 parser_code = pegjs.generate(peg, param);
else if (typeof pegjs.buildParser === 'function')
var parser_code = 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(parser_code);
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`);
|