view builder update

This commit is contained in:
2015-07-13 14:59:40 +03:00
parent a060118589
commit 7f172ab7a0
3 changed files with 28 additions and 17 deletions

View File

@@ -41,10 +41,11 @@ class Builder {
private function getPosition(?position:JsonKeyPosition):Position {
var min = position == null ? 1 : position.min + 32; // :-(
var max = position == null ? 1 : position.max + 32;
return Context.makePosition({min:min, max:max, file:templateFile});
var file = position == null || position.file == null ? templateFile : position.file;
return Context.makePosition({min:min, max:max, file:file});
}
private function specialValue(name:String, key:String, a:Array<String>):Dynamic {
private function specialValue(name:String, key:String, a:Array<String>, position:JsonKeyPosition):Dynamic {
return switch (a[0]) {
case "asset":
switch (a[1]) {
@@ -56,7 +57,7 @@ class Builder {
case "res":
var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1];
var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")";
exprs.push(Context.parse(bindExpr, getPosition()));
exprs.push(Context.parse(bindExpr, getPosition(position)));
//res + ".get(\"" + a[2] + "\")";
null;
case "locale":
@@ -65,7 +66,7 @@ class Builder {
a[1];
case "layout":
var template = BuilderUtil.loadFile(a[1]);
return getValue(name, key, template);
return getValue(name, key, template, position);
case "link":
"(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")";
case _:
@@ -73,12 +74,14 @@ class Builder {
}
}
private function getValue(name:String, key:String, value:Dynamic):Dynamic {
private function getValue(name:String, key:String, value:Dynamic, position:JsonKeyPosition):Dynamic {
return if (Std.is(value, Array)) {
value.map(function(v) { return getValue(null, null, v); });
value.map(function(v) {
return getValue(null, null, v, position);
});
} else if (Std.is(value, String)) {
if (value.charAt(0) == "@") {
specialValue(name, key, value.substring(1, value.length).split(":"));
specialValue(name, key, value.substring(1, value.length).split(":"), position);
} else if (~/(0x|#)[A-Fa-f\d]{6}/.match(value)) {
Std.parseInt(StringTools.replace(Std.string(value), "#", "0x"));
} else {
@@ -90,11 +93,12 @@ class Builder {
if (Reflect.hasField(value, "type")) {
var n = "a" + i++;
var type = Reflect.field(value, "type");
exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition()));
exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition(position)));
createElement(value, n);
n;
} else {
value;
Context.error("Need type field", getPosition(position));
null;
}
} else {
value;
@@ -104,8 +108,11 @@ class Builder {
private function createElement(template:Dynamic, name:String):String {
if (Reflect.hasField(template, "style")) {
var s = Reflect.field(style, Reflect.field(template, "style"));
for (key in Reflect.fields(s)) if (!Reflect.hasField(template, key)) {
for (key in Reflect.fields(s)) {
if (key.charAt(0) != "$" && !Reflect.hasField(template, key)) {
Reflect.setField(template, key, Reflect.field(s, key));
Reflect.setField(template, "$" + key, Reflect.field(s, "$" + key));
}
}
}
@@ -129,7 +136,7 @@ class Builder {
for (key in Reflect.fields(template)) {
if (key.charAt(0) == "$" || ["type", "style"].indexOf(key) > -1) continue;
var position = Reflect.field(template, "$" + key);
var value = getValue(name, key, Reflect.field(template, key));
var value = getValue(name, key, Reflect.field(template, key), position);
if (value != null) {
exprs.push(Context.parse(name + "." + key + " = " + value, getPosition(position)));
}

View File

@@ -11,7 +11,7 @@ class BuilderUtil {
Context.registerModuleDependency(Context.getLocalModule(), path);
var content = sys.io.File.getContent(path);
Context.parse(content, Context.makePosition({min:0, max:0, file:path}));
return json ? PositionJsonParser.parse(content) : content;
return json ? PositionJsonParser.parse(content, path) : content;
}
public static function getMeta(key:String):Array<String> {

View File

@@ -3,26 +3,30 @@ package haxework.gui.build;
typedef JsonKeyPosition = {
var min:Int;
var max:Int;
var file:String;
}
class PositionJsonParser {
static public inline function parse(str : String) : Dynamic {
return new PositionJsonParser(str).parseRec();
static public inline function parse(str : String, file : String) : Dynamic {
return new PositionJsonParser(str, file).parseRec();
}
var str : String;
var pos : Int;
var file : String;
function new( str : String ) {
function new( str : String, file:String ) {
this.str = str;
this.pos = 0;
this.file = file;
}
function getKeyPosition():JsonKeyPosition {
return {
min: pos,
max: pos
max: pos,
file: file
}
}