From 050af36dc98c38fd5660d897b3e3bffd07e9f727 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 12 Oct 2021 12:29:07 -0600 Subject: [PATCH] parseComplexType better error handling --- kiss/src/kiss/Helpers.hx | 41 ++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/kiss/src/kiss/Helpers.hx b/kiss/src/kiss/Helpers.hx index 90b7462d..9be3bb86 100644 --- a/kiss/src/kiss/Helpers.hx +++ b/kiss/src/kiss/Helpers.hx @@ -46,24 +46,45 @@ class Helpers { return s.charAt(0) == s.charAt(0).toUpperCase(); } - public static function parseTypePath(path:String, from:ReaderExp):TypePath { + public static function parseTypePath(path:String, ?from:ReaderExp):TypePath { return switch (parseComplexType(path, from)) { case TPath(path): path; default: - throw CompileError.fromExp(from, 'Haxe could not parse a type path from $path'); + var errorMessage = 'Haxe could not parse a type path from $path'; + if (from == null) { + throw errorMessage; + } else { + throw CompileError.fromExp(from, errorMessage); + } }; } - public static function parseComplexType(path:String, from:ReaderExp):ComplexType { + public static function parseComplexType(path:String, ?from:ReaderExp):ComplexType { // Trick Haxe into parsing it for us: - var typeCheckExpr = Context.parse('(thing : $path)', Context.currentPos()); - return switch (typeCheckExpr.expr) { - case EParenthesis({pos: _, expr: ECheckType(_, complexType)}): - complexType; - default: - throw CompileError.fromExp(from, 'Haxe could not parse a complex type from $path, parsed ${typeCheckExpr.expr}'); - }; + var typeCheckStr = '(thing : $path)'; + var errorMessage = 'Haxe could not parse a complex type from `$path` in `${typeCheckStr}`'; + + function throwError() { + if (from == null) { + throw errorMessage; + } else { + throw CompileError.fromExp(from, errorMessage); + }; + } + try { + var typeCheckExpr = Context.parse(typeCheckStr, Context.currentPos()); + return switch (typeCheckExpr.expr) { + case EParenthesis({pos: _, expr: ECheckType(_, complexType)}): + complexType; + default: + throwError(); + return null; + }; + } catch (err) { + throwError(); + return null; + } } public static function explicitType(nameExp:ReaderExp):ComplexType {