[macro] update template parser
This commit is contained in:
@@ -7,10 +7,7 @@ import haxe.macro.Expr;
|
||||
class ProvideMacro {
|
||||
|
||||
public static function has(field:Field):Bool {
|
||||
for (md in field.meta) if (md.name == ":provide") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return Util.hasFieldMeta(":provide", field);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,10 +9,7 @@ class StyleMacro {
|
||||
private static inline var metaName:String = ':style';
|
||||
|
||||
public static function has(classType:ClassType):Bool {
|
||||
for (md in classType.meta.get()) if (md.name == metaName) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return Util.hasClassMeta(metaName, classType);
|
||||
}
|
||||
|
||||
public static var style:Dynamic;
|
||||
|
||||
@@ -11,10 +11,7 @@ class TemplateMacro {
|
||||
private static inline var metaName:String = ':template';
|
||||
|
||||
public static function has(classType:ClassType):Bool {
|
||||
for (md in classType.meta.get()) if (md.name == metaName) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return Util.hasClassMeta(metaName, classType);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,32 +74,30 @@ class TemplateMacro {
|
||||
case "asset":
|
||||
switch (a[1]) {
|
||||
case "image":
|
||||
"openfl.Assets.getBitmapData(\"" + a[2] + "\")";
|
||||
'openfl.Assets.getBitmapData("${a[2]}")';
|
||||
case _:
|
||||
a[2];
|
||||
}
|
||||
case "res":
|
||||
var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1];
|
||||
var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")";
|
||||
var bindExpr = 'haxework.provider.Provider.get(haxework.resources.IResources).${a[1]}.bind("${[2]}", "${name}", "${key}")';
|
||||
exprs.push(Context.parse(bindExpr, getPosition(position)));
|
||||
//res + ".get(\"" + a[2] + "\")";
|
||||
null;
|
||||
case "locale":
|
||||
"new haxework.locale.LString(\"" + a[1] + "\")";
|
||||
case "class":
|
||||
'new haxework.locale.LString("${[1]}")';
|
||||
case "raw":
|
||||
a[1];
|
||||
case "this":
|
||||
'this.${a[1]}';
|
||||
case "layout":
|
||||
var template = FileUtil.loadJsonFile(a[1]);
|
||||
return createValue(name, key, template, position, exprs);
|
||||
case "link":
|
||||
"(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")";
|
||||
case _:
|
||||
Context.error("Unsupported prefix \"" + a[0] + "\"", getPosition(position));
|
||||
Context.error('Unsupported prefix "${a[0]}"', getPosition(position));
|
||||
}
|
||||
}
|
||||
|
||||
private static function getType(value:Dynamic, position:Position):String {
|
||||
var type:String = getSpecField(value, "type");
|
||||
private static function getType(value:Dynamic, position:Position):Dynamic {
|
||||
var type:Dynamic = getSpecField(value, "type");
|
||||
if (type == null) {
|
||||
Context.error("Need @type field", position);
|
||||
}
|
||||
@@ -125,19 +120,26 @@ class TemplateMacro {
|
||||
} else if (Std.is(value, Float) || (Std.is(value, Bool))) {
|
||||
value;
|
||||
} else if (value != null) {
|
||||
var type = getType(value, getPosition(position));
|
||||
if (type != null) {
|
||||
var n = 'a${i++}';
|
||||
if (type == "Dynamic") {
|
||||
//ToDo:
|
||||
exprs.push(Context.parse("var " + n + " = cast {}", getPosition(position)));
|
||||
} else {
|
||||
exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition(position)));
|
||||
}
|
||||
createElement(n, value, exprs);
|
||||
n;
|
||||
var className:String = getSpecField(value, "class");
|
||||
if (className != null) {
|
||||
className;
|
||||
} else {
|
||||
null;
|
||||
var type:Dynamic = getType(value, getPosition(position));
|
||||
if (type != null) {
|
||||
var varName = 'a${i++}';
|
||||
if (Std.is(type, Array)) {
|
||||
var args = cast(type,Array<Dynamic>).slice(1).join(",");
|
||||
exprs.push(Context.parse('var ${varName} = ${type[0]}(${args})', getPosition(position)));
|
||||
} else if (type == "Dynamic") {
|
||||
exprs.push(Context.parse('var ${varName} = cast {}', getPosition(position)));
|
||||
} else {
|
||||
exprs.push(Context.parse('var ${varName} = new ${type}()', getPosition(position)));
|
||||
}
|
||||
createElement(varName, value, exprs);
|
||||
varName;
|
||||
} else {
|
||||
null;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
value;
|
||||
@@ -169,7 +171,11 @@ class TemplateMacro {
|
||||
var position = Reflect.field(data, "$" + key);
|
||||
var value = createValue(name, key, Reflect.field(data, key), position, exprs);
|
||||
if (value != null) {
|
||||
exprs.push(Context.parse(name + "." + key + " = " + value, getPosition(position)));
|
||||
if (key.charAt(0) == "+") {
|
||||
exprs.push(Context.parse('${name}.${key.substr(1)}.connect(${value})', getPosition(position)));
|
||||
} else {
|
||||
exprs.push(Context.parse('${name}.${key} = ${value}', getPosition(position)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package haxework.macro;
|
||||
|
||||
import haxe.macro.Type.ClassType;
|
||||
import haxe.macro.Expr;
|
||||
|
||||
|
||||
@@ -15,4 +16,18 @@ class Util {
|
||||
public inline static function DynamicType():ComplexType {
|
||||
return ComplexType.TPath({name:'Dynamic', pack:[], params:[]});
|
||||
}
|
||||
}
|
||||
|
||||
public static function hasClassMeta(metaName:String, classType:ClassType):Bool {
|
||||
for (md in classType.meta.get()) if (md.name == metaName) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function hasFieldMeta(metaName:String, field:Field):Bool {
|
||||
for (md in field.meta) if (md.name == metaName) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user