Output fancier error message on parse error

This commit is contained in:
Tangent Wantwight 2023-08-04 00:26:15 -04:00
parent 7ad94d13a6
commit 58cd1a56b2
2 changed files with 10 additions and 6 deletions

6
3x5.js
View file

@ -25,7 +25,11 @@
*/
function renderCard(state, code) {
const script = Notcl.parse(code);
state.output = JSON.stringify(script, null, 2);
if (script[0]) {
state.output = JSON.stringify(script[1], null, 2);
} else {
state.output = script[1];
}
return state.output;
}

View file

@ -64,8 +64,8 @@ var Notcl = (() => {
* Parse out a Notcl script into an easier-to-interpret representation.
* No script is actually executed yet.
*
* @param {string} code
* @returns Script
* @param {string} code to parse
* @returns {[true, Notcl.Script] | [false, string]} parsed list of commands, or error message on failure
*/
parse(code) {
/* Preprocess */
@ -85,9 +85,9 @@ var Notcl = (() => {
);
return [
false,
`Error at position ${commands[1]}
${before}${after}
${"-".repeat(before.length)}^`,
`<pre>Error at position ${commands[1]}
${escapeHtml(before + "" + after)}
${"-".repeat(before.length)}^</pre>`,
];
}
},