Template reporting a regex problem on execution

I am making my Exporter and using the Haxe Template system to execute and get the results of generated code. But I’ve come across a problem when I attempt to export and can’t figure out why this may be the case. The following line is being flagged and then the application crashes:

https://github.com/ColourID/HaxeLive/blob/master/templates/openfl/Sizer.txt#L22

Does the Haxe Template system handle negative numbers as well as positive or is it only positive?

EDIT: Just tested and that seems to be the case. Why does it only accept positive, that is a bit annoying.

Looks like you’ve gone ahead and converted everything to positive numbers, but I’d like to suggest another option: use strings instead.

Consider this part:

::if (align.alignment == 0)::
Location.setLocationLeftOf(::name::, getChildByName(::align.name::), ::align.padding::);
::elseif (align.alignment == 1)::
Location.setLocationTopOf(::name::, getChildByName(::align.name::), ::align.padding::);

The only difference is that in case 1, “Left” has been replaced by “Top.” You never actually use the number 0 or 1.

So why not turn align.alignment into a string? It could be “Left,” “Top'," "Right," or "Bottom`.” Then you’d be able to replace the whole section (lines 16-24) with this:

Location.setLocation::align.alignment 
::Of(::name::, getChildByName(::align.name::), ::align.padding::);

But there’s more. The next section (lines 26-47) looks like this:

::if (align == 7)::
Location.screenFromBottom(::name::, padding);
::elseif (align == 8)::
Location.screenFromRight(::name::, padding);

Similar issue, so why not use the same solution there? Well, except you can’t do that, because that section goes on like this:

::elseif (align == 0)::
Alignment.alignLeft(::name::, padding);

Different function name, different class name. However, it could still work. What you need to do is include both the class name and the full function name. So instead of just “Left,” you’d set it to “Alignment.alignLeft.” Now this section can be reduced to one line as well:

::align::(::name::);

Honestly, there isn’t much reason for this template to exist at all. The whole thing is a series of if/else statements that would have been much easier to program in pure Haxe.

For one thing, Haxe lets you use enums:

typedef Sizer = {
    var name:String;
    var position:Position;
    @:optional var padding:Int;
    @:optional var width:Int;
    @:optional var height:Int;
    @:optional var x:Int;
    @:optional var y:Int;
    @:optional var first:Bool;
}

enum Position {
    flow(fromTop:Bool);
    nextTo(name:String, padding:Int, edge:Edge);
    align(edge:Edge);
    centerVertically;
    centerHorizontally;
    alignCenter;
}

enum Edge {
    Left;
    Top;
    Right;
    Bottom;
}

Then the entire Sizer.txt file can be replaced with this:

public static function executeLocator(data:Sizer):Void {
    var result:String = "\t";
    var name:String = data.name;
    
    if(data.first) {
        if(data.padding == null) {
            data.padding = 2;
        }
        result = 'var padding = ${data.padding};\n\t';
    }
    
    if(data.width != null) {
        result += '$name.width = ${data.width};\n\t';
    }
    if(data.height != null) {
        result += '$name.height = ${data.height};\n\t';
    }
    
    switch(data.position) {
        case flow(fromTop):
            var source:String = (fromTop ? "Top" : "Left");
            result += 'Flow.flowFrom$source($name, padding);\n\t';
        case nextTo(otherName, padding, edge):
            result += 'Location.setLocation${edge.getName()}Of($name, getChildByName($otherName), $padding);\n\t';
        case align(edge):
            result += 'Alignment.align${edge.getName()}($name, ${data.padding});\n\t';
        default:
            result += 'Alignment.${data.position.getName()}($name);\n\t';
    }
    
    if(data.x != null) {
        result += '$name.x = ${data.x};\n\t';
    }
    if(data.y != null) {
        result += '$name.y = ${data.y};\n\t';
    }
}

Maybe I should have suggested string interpolation in the first place. Edit: nah. All of your other templates are appropriate.