diff --git a/hscript/Expr.hx b/hscript/Expr.hx index bdda752..836baaf 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -159,7 +159,7 @@ enum Error { enum ModuleDecl { DPackage( path : Array ); - DImport( path : Array, ?everything : Bool ); + DImport( path : Array, ?everything : Bool, ?asname:String ); DClass( c : ClassDecl ); DTypedef( c : TypeDecl ); } diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 87af3c8..203039d 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -81,10 +81,11 @@ class Interp { return _customClasses.exists(name); } - private static function registerCustomClass(c:CustomClassDecl) { + private static function registerCustomClass(c:CustomClassDecl, ?as:String) { #if CUSTOM_CLASSES var name = c.classDecl.name; - if (c.pkg != null) { + if (as != null) name = as; + else if (c.pkg != null) { name = c.pkg.join(".") + "." + name; } _customClasses.set(name, c); @@ -129,7 +130,7 @@ class Interp { return scriptObject = v; } public var errorHandler:Error->Void; - public var importFailedCallback:Array->Bool; + public var importFailedCallback:Array -> ?Null -> Bool; public var customClasses:Map; public var variables:Map; @@ -829,7 +830,7 @@ class Interp { } if (cl == null && en == null) { - if (importFailedCallback == null || !importFailedCallback(oldSplitName)) + if (importFailedCallback == null || !importFailedCallback(oldSplitName, n)) error(EInvalidClass(oldClassName)); } else { if (en != null) { @@ -1593,24 +1594,26 @@ class Interp { } } - public function registerModule(module:Array) { + public function registerModule(module:Array, ?as:String) { var pkg:Array = null; var imports:Map = []; for (decl in module) { switch (decl) { case DPackage(path): pkg = path; - case DImport(path, _): + case DImport(path, _, asname): if (importBlocklist.contains(path.join("."))) continue; var last = path[path.length - 1]; + var hasAlias = asname != null; var importedClass:CustomClassImport = { name: last, pkg: [for (e in path) e.trim()], - fullPath: path.join(".") + fullPath: path.join("."), + as: asname } - imports.set(importedClass.name, importedClass); + imports.set(hasAlias ? asname : importedClass.name, importedClass); case DClass(c): var extend = c.extend; if (extend != null) { @@ -1640,7 +1643,7 @@ class Interp { pkg: pkg } customClassDecl.cacheFields(); - registerCustomClass(customClassDecl); + registerCustomClass(customClassDecl, as); case DTypedef(_): //TODO: maybe make this work :3 } diff --git a/hscript/Parser.hx b/hscript/Parser.hx index 79875c4..fb752da 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -1509,10 +1509,22 @@ class Parser { return DPackage(path); case "import": var path = [getIdent()]; - var star = false; + var asname:String = null; + var star = false; // For now, unused while( true ) { var t = token(); if( t != TDot ) { + if (t.match(TId("as"))) { + t = token(); + switch (t) { + case TId(id): + asname = id; + default: + unexpected(t); + } + break; + } + push(t); break; } @@ -1520,34 +1532,52 @@ class Parser { switch( t ) { case TId(id): path.push(id); + /* idk if put this work case TOp("*"): star = true; + */ default: unexpected(t); } } ensure(TSemicolon); - return DImport(path, star); + return DImport(path, star, asname); case "class": var name = getIdent(); var params = parseParams(); - var extend = null; - var implement = []; + var extend:Null = null; + var implement:Array = []; while( true ) { var t = token(); switch( t ) { case TId("extends"): - extend = parseType(); + var e = parseType(); + switch(e) { + case CTPath(path, params): + if(extend != null) { + error(ECustom('Cannot extend a class twice.'), 0, 0); + } + extend = e; + default: + error(ECustom('${Std.string(e)} is not a valid path.'), 0, 0); + } case TId("implements"): - implement.push(parseType()); + var i = parseType(); + switch(i) { + case CTPath(path, params): + implement.push(i); + default: + error(ECustom('${Std.string(i)} is not a valid interface path.'), 0, 0); + } + //implement.push(parseType()); default: push(t); break; } } - var fields = []; + var fields:Array = []; ensure(TBrOpen); while( !maybe(TBrClose) ) fields.push(parseField()); diff --git a/hscript/customclass/CustomClass.hx b/hscript/customclass/CustomClass.hx index 0fdc3f9..1ec813a 100644 --- a/hscript/customclass/CustomClass.hx +++ b/hscript/customclass/CustomClass.hx @@ -152,22 +152,23 @@ class CustomClass implements IHScriptCustomAccessBehaviour{ var i:Int = 0; for(_import in __class.imports) { var importedClass = _import.fullPath; + var importAlias = _import.as; if(Interp.customClassExist(importedClass) && this.interp.importFailedCallback != null) { - this.interp.importFailedCallback(importedClass.split(".")); + this.interp.importFailedCallback(importedClass.split("."), importAlias); continue; } #if hscriptPos var e:Expr = { - e: ExprDef.EImport(importedClass), + e: ExprDef.EImport(importedClass, importAlias), pmin: 0, pmax: 0, origin: this.className, line: i }; #else - var e = Expr.EImport(importedClass); + var e = Expr.EImport(importedClass, importAlias); #end this.interp.expr(e); i++; diff --git a/hscript/customclass/CustomClassDecl.hx b/hscript/customclass/CustomClassDecl.hx index d3f9f95..a92e1ee 100644 --- a/hscript/customclass/CustomClassDecl.hx +++ b/hscript/customclass/CustomClassDecl.hx @@ -157,4 +157,5 @@ typedef CustomClassImport = { var ?name:String; var ?pkg:Array; var ?fullPath:String; // pkg.pkg.pkg.name + var ?as:Null; // import pkg.Name as OtherName }