Created
March 12, 2014 18:14
-
-
Save nadako/9512885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
class EnumAbstractMacro | |
{ | |
public static function build():Array<Field> | |
{ | |
var fields = Context.getBuildFields(); | |
var values = []; | |
for (field in fields) | |
{ | |
if (!hasMeta(field.meta, ":enum") || !hasMeta(field.meta, ":impl")) | |
continue; | |
switch (field.kind) | |
{ | |
case FVar(_, {expr: ECast(e, _)}): | |
values.push(e); | |
default: throw false; | |
} | |
} | |
fields.push({ | |
name: "validValues", | |
access: [APublic, AStatic], | |
pos: Context.currentPos(), | |
kind: FVar(null, macro $a{values}) | |
}); | |
return fields; | |
} | |
static function hasMeta(meta:Metadata, name:String):Bool | |
{ | |
for (m in meta) | |
{ | |
if (m.name == name) | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@:build(EnumAbstractMacro.build()) | |
@:enum abstract MyEnum(Int) | |
{ | |
var A = 1; | |
var B = 2; | |
var C = 3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment