parseComplexType better error handling

This commit is contained in:
2021-10-12 12:29:07 -06:00
parent b1b3fd27de
commit 050af36dc9

View File

@@ -46,24 +46,45 @@ class Helpers {
return s.charAt(0) == s.charAt(0).toUpperCase(); 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)) { return switch (parseComplexType(path, from)) {
case TPath(path): case TPath(path):
path; path;
default: 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: // Trick Haxe into parsing it for us:
var typeCheckExpr = Context.parse('(thing : $path)', Context.currentPos()); var typeCheckStr = '(thing : $path)';
return switch (typeCheckExpr.expr) { var errorMessage = 'Haxe could not parse a complex type from `$path` in `${typeCheckStr}`';
case EParenthesis({pos: _, expr: ECheckType(_, complexType)}):
complexType; function throwError() {
default: if (from == null) {
throw CompileError.fromExp(from, 'Haxe could not parse a complex type from $path, parsed ${typeCheckExpr.expr}'); 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 { public static function explicitType(nameExp:ReaderExp):ComplexType {