use + and - as late / early

+ add some extra error handling
This commit is contained in:
Felix Roos 2025-06-18 22:07:16 +02:00
parent 9412bf60bf
commit 10ac7c353c
No known key found for this signature in database
2 changed files with 16 additions and 3 deletions

View file

@ -20,8 +20,8 @@ export class MondoParser {
open_curly: /^\{/,
close_curly: /^\}/,
number: /^-?[0-9]*\.?[0-9]+/, // before pipe!
// "+" and "-" might be added here, but then "-" won't work as silence anymore..
op: /^[*/:!@%?]|^\.{2}/, // * / : ! @ % ? ..
// TODO: better error handling when "-" is used as rest, e.g "s [- bd]"
op: /^[*/:!@%?+-]|^\.{2}/, // * / : ! @ % ? ..
// dollar: /^\$/,
pipe: /^#/,
stack: /^[,$]/,
@ -173,6 +173,18 @@ export class MondoParser {
children[opIndex] = op;
continue;
}
// some careful error handling
if (left.type === 'op') {
throw new Error(`got 2 ops in a row: "${left.value}${op.value}"`);
}
if (right.type === 'op') {
let err = `got 2 ops in a row: "${op.value}${right.value}"`;
if (op.value === '-') {
// yes i know this file is not supposed to know about rests x.X
err += '. you probably want a rest, which is "_" in mondo!';
}
throw new Error(err);
}
const call = { type: 'list', children: [op, right, left] };
// insert call while keeping other siblings
children = [...children.slice(0, opIndex - 1), call, ...children.slice(opIndex + 2)];