[英]How to get a list of imports in macro mode in haxe?
How do you get the list of imports a .hx file uses in macro mode? 如何获得.hx文件在宏模式下使用的导入列表? More specificity, suppose I have a "haxe.macro.Position" that tells me which file an expression belongs to. 更具体,假设我有一个“haxe.macro.Position”告诉我表达式属于哪个文件。 I need to know the imports the file uses in order to convert abridged type declarations such as "Car" into full-package-path types such as "vehicles.fourWheels.Car". 我需要知道文件使用的导入,以便将删除类型的声明(如“Car”)转换为完整的包路径类型,例如“vehicles.fourWheels.Car”。
It is planned to add in haxe 3.3: https://github.com/HaxeFoundation/haxe/issues/3560 计划添加haxe 3.3: https : //github.com/HaxeFoundation/haxe/issues/3560
At the moment, you may try to read the source file directly (as String
) and parse the import
statements manually. 此时,您可能尝试直接读取源文件(作为String
)并手动解析import
语句。
For getting the fully-qualified type name, there is also a suggestion made in the above mentioned issue : 要获得完全限定的类型名称,还会在上述问题中提出建议 :
var someComplexType:ComplexType = getComplexType();
switch (Context.typeof( macro ( _ : $someComplexType )))
{
case TInst(_.get() => c,_):
var pack = c.pack, name = c.name;
case _:
}
edit: 编辑:
Here is a more complete solution to get the fully-qualified type name: 以下是获取完全限定类型名称的更完整的解决方案:
#if macro
import haxe.macro.*;
import haxe.macro.Expr;
import haxe.macro.Type;
#end
import haxe.ds.Option;
class Test {
#if macro
static public function typeToTypePath(t:Type):TypePath {
return switch (Context.follow(t)) {
case TInst(t, params):
baseTypeToTypePath(t.get(), [for (p in params) TPType(Context.toComplexType(p))]);
case TEnum(t, params):
baseTypeToTypePath(t.get(), [for (p in params) TPType(Context.toComplexType(p))]);
case TType(t, params):
baseTypeToTypePath(t.get(), [for (p in params) TPType(Context.toComplexType(p))]);
case TAbstract(t, params):
baseTypeToTypePath(t.get(), [for (p in params) TPType(Context.toComplexType(p))]);
case _: throw 'Cannot convert this to TypePath: $t';
};
}
static function baseTypeToTypePath(t:BaseType, params:Array<TypeParam>):TypePath {
return {
pack:t.pack,
name:t.module.substring(t.module.lastIndexOf(".")+1),
sub:t.name,
params:params
};
}
/**
Fully-qualify a `ComplexType`.
For example, turn `Option<Int>` to `haxe.ds.Option.Option<StdTypes.Int>`.
In case the process fail, it will return the input `ComplexType`.
*/
static public function qualifyComplexType(ct:ComplexType):ComplexType {
var type = Context.typeof(macro ( null : $ct ));
try {
var tp = typeToTypePath(type);
return TPath(tp);
} catch(e:Dynamic) {
return ct;
}
}
#end
/**
Just an example to demostrate `qualifyComplexType`.
It accepts an `e` expression in the form of `var _:Type`,
and trace the fully-qualified name of `Type` at compile-time.
*/
macro static public function printType(e:Expr):Expr {
switch (e) {
case macro var a:$ct:
trace(ComplexTypeTools.toString(qualifyComplexType(ct)));
case _:
}
return e;
}
static function main() {
printType(var a:Int); //StdTypes.Int
printType(var a:Test); //Test.Test
printType(var a:Option<Int>); //haxe.ds.Option.Option<StdTypes.Int>
printType(var a:Void->Void); //Void -> Void
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.