Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ftk;
- import haxe.macro.Context;
- import haxe.macro.Expr;
- /**
- * ...
- * @author filt3rek
- */
- class Texts {
- #if !macro
- public static var list = new DotAccess();
- public static var sep = "|";
- public static var plural = "s";
- public static function set( lang = "fr" ) : Void {
- var s = haxe.Resource.getString( MyList.__res );
- var fast = new haxe.xml.Access( Xml.parse( s ).firstElement() );
- for ( n in fast.nodes.s ) {
- var str : String = switch ( lang ) {
- case "fr" : n.node.fr.innerData;
- case _ : null;
- }
- var ind = str.indexOf( sep );
- if ( ind != -1 ){
- list.set( n.att.id, str.substr( 0, ind ) );
- list.set( n.att.id + "_s", str.substr( ind + 1 ) );
- }else{
- list.set( n.att.id, str );
- list.set( n.att.id + "_s", str + plural );
- }
- }
- }
- #end
- /*
- * Checks if n > 1 add "_s" to the word's name ( Texts.list.)
- */
- public static macro function s( expr : Expr, i : haxe.macro.Expr.ExprOf<Float> ){
- var texpr = Context.typeExpr( expr );
- switch( texpr.expr ){
- case TCall( callee, args ) :
- var stcallee = Context.storeTypedExpr( callee );
- var stargs = [ for( arg in args ) macro cast ${ Context.storeTypedExpr( arg ) } ];
- var lastArg = stargs[ stargs.length - 1 ];
- stargs[ stargs.length - 1 ] = macro if ( $i > 1 ) $lastArg + "_s" else $lastArg;
- var ret = macro @:pos( expr.pos ) $stcallee( $a{ stargs } );
- return ret;
- default : Context.fatalError( "Unsupported expression", expr.pos );
- }
- return macro $expr;
- }
- }
- #if !macro
- #if display
- @:forward
- #end
- private abstract DotAccess(MyList) from MyList to MyList {
- public inline function new() {
- this = new MyList();
- }
- @:op(a.b) function get(field:String):String {
- return this.get( field );
- }
- public function set(field:String,val : String):String {
- this.set( field, val );
- return val;
- }
- }
- @:build( ftk.Texts.BuildXmlProxy.build() )
- private class MyList extends haxe.ds.StringMap<String>{}
- #else
- class BuildXmlProxy{
- public static function build(){
- var fields = Context.getBuildFields();
- var spath = Context.definedValue( "xmlProxy" );
- if( spath == null ){
- Context.fatalError( "Please define xmlProxy (-D xmlProxy=my/path.xml)", Context.currentPos() );
- }
- var path = new haxe.io.Path( spath );
- fields.push({
- name : "__res",
- access : [ Access.APublic, Access.AStatic ],
- kind : FieldType.FVar( macro:String, macro $v{ path.file } ),
- pos : Context.currentPos(),
- });
- var s = sys.io.File.getContent( spath );
- Context.addResource( path.file, haxe.io.Bytes.ofString( s ) );
- var fast = new haxe.xml.Access( Xml.parse( s ).firstElement() );
- for ( n in fast.nodes.s ) {
- fields.push({
- name : n.att.id,
- access : [ Access.APublic ],
- kind : FieldType.FVar( macro:String ),
- pos : Context.currentPos(),
- });
- }
- return fields;
- }
- }
- #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement