From c7ec4ec356c2af7ed885273a29bacf88e32d8150 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 5 May 2023 16:15:01 -0600 Subject: [PATCH] some better compile-time error messages --- src/kiss/Prelude.hx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/kiss/Prelude.hx b/src/kiss/Prelude.hx index 07f2bb1..031ce3c 100644 --- a/src/kiss/Prelude.hx +++ b/src/kiss/Prelude.hx @@ -493,12 +493,21 @@ class Prelude { return v; } + // GOTCHA: must be THROWN to type-unify + static function expected(s:ReaderExp, toBeWhat:String):String { + #if macro + throw 'expected ${kiss.Reader.toString(s.def)} to be ${toBeWhat}'; + #else + throw 'expected $s to be ${toBeWhat}'; + #end + } + public static function symbolNameValue(s:ReaderExp, allowTyped:Null = false, allowMeta:Null = false):String { return switch (s.def) { case Symbol(name): name; case TypedExp(_, innerExp) if (allowTyped): symbolNameValue(innerExp, false); // Meta must always precede type annotation case MetaExp(_, innerExp) if (allowMeta): symbolNameValue(innerExp, allowTyped, false); // TODO will more than one meta on the same expression be necessary? - default: throw 'expected $s to be a plain symbol'; // TODO convert s def to print & modify this message to reflect allowTyped and allowMeta parameters + default: throw expected(s, "a plain symbol"); // TODO modify this message to reflect allowTyped and allowMeta parameters }; } @@ -519,7 +528,7 @@ class Prelude { case Symbol(name): StrExp(name); case TypedExp(_, innerExp) if (allowTyped): symbolName(innerExp, false); // Meta must always precede type annotation case MetaExp(_, innerExp) if (allowMeta): symbolName(innerExp, allowTyped, false); // TODO will more than one meta on the same expression be necessary? - default: throw 'expected $s to be a plain symbol'; // TODO convert s def to print & modify this message to reflect allowTyped and allowMeta parameters; + default: throw expected(s, 'a plain symbol'); // TODO modify this message to reflect allowTyped and allowMeta parameters; }; } @@ -527,7 +536,7 @@ class Prelude { return switch (s.def) { case ListExp(exps): exps; - default: throw 'expected $s to be a list expression'; + default: throw expected(s, 'a list expression'); }; }