From 10ac7c353cb16034bce8da900c60702e431e4c9f Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 18 Jun 2025 22:07:16 +0200 Subject: [PATCH] use + and - as late / early + add some extra error handling --- packages/mondo/mondo.mjs | 16 ++++++++++++++-- packages/mondough/mondough.mjs | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/mondo/mondo.mjs b/packages/mondo/mondo.mjs index e3f62846b..b879f24bf 100644 --- a/packages/mondo/mondo.mjs +++ b/packages/mondo/mondo.mjs @@ -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)]; diff --git a/packages/mondough/mondough.mjs b/packages/mondough/mondough.mjs index cd0521bad..a4e7c93b3 100644 --- a/packages/mondough/mondough.mjs +++ b/packages/mondough/mondough.mjs @@ -27,7 +27,8 @@ let nope = (...args) => args[args.length - 1]; let lib = {}; lib['nope'] = nope; -lib['-'] = silence; +lib['-'] = (a, b) => b.early(a); +lib['+'] = (a, b) => b.late(a); lib['_'] = silence; lib['~'] = silence; lib.curly = stepcat;