-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.js
59 lines (51 loc) · 1.81 KB
/
debug.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
const parser = require("./lib/");
const prepareInput = require("./src/prepareInput.js");
const { program } = require("commander");
const { peg$computeLocation, error, input } = require("./tests/pegjsPolyFill");
// -----------------------------------
// pretty verpose logging
// -----------------------------------
function log(fn, tex, ...args) {
console.log("start >>>>>>>>>>>>>>");
console.log("===============");
console.log(tex);
console.log("===============");
console.time(fn.name + " is done after");
try {
console.log(fn(tex, ...args));
} catch (e) {
if (e instanceof parser.SyntaxError) {
console.log("SyntaxError:", e.message);
printErrorLocation(e, tex);
} else {
throw e;
}
}
console.log("===============");
console.timeEnd(fn.name + " is done after");
}
function printErrorLocation(e, tex) {
const errorLine = e.location.start.line - 1;
const lines = tex.split("\n");
if (errorLine - 2 >= 0) console.log(lines[errorLine - 2]);
if (errorLine - 1 >= 0) console.log(lines[errorLine - 1]);
console.log();
console.log(lines[errorLine]);
console.log(new Array(e.location.start.column - 1).fill("_").join("") + "^");
console.log();
if (errorLine + 1 < lines.length) console.log(lines[errorLine + 1]);
if (errorLine + 2 < lines.length) console.log(lines[errorLine + 2]);
}
// -----------------------------------
// finnally excute the desire
// function with the desired args
// -----------------------------------
program
.argument("<tex>", "The TeX expression to debug the operation using it")
.option("--prepare", "Debug `prepareInput` function rather than `parse`")
.action((tex, options) => {
input.value = tex;
if (options.prepare) log(prepareInput, tex, peg$computeLocation, error);
else log(parser.parse, tex);
})
.parse();