Macro.hx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #if macro
  2. import haxe.macro.Expr;
  3. import haxe.macro.Context;
  4. using haxe.macro.Tools;
  5. #end
  6. class Macro {
  7. #if macro
  8. static function build():Array<Field> {
  9. var dotPath = Context.getLocalClass().toString();
  10. return (macro class {
  11. public final f = $v{dotPath};
  12. public function new() {}
  13. public static function UpperCase() return $v{dotPath + ".UpperCase"};
  14. public static function lowerCase() return $v{dotPath + ".lowerCase"};
  15. }).fields;
  16. }
  17. #end
  18. public static macro function assert(path:String) {
  19. var pos = Context.currentPos();
  20. var nameExpr = Context.parse("new " + path + "().f", pos);
  21. var uCallExpr = Context.parse(path + ".UpperCase()", pos);
  22. var lCallExpr = Context.parse(path + ".lowerCase()", pos);
  23. var fullPath = Context.getType(path).toString();
  24. return macro @:pos(pos) {
  25. utest.Assert.equals($v{fullPath}, $nameExpr);
  26. utest.Assert.equals($v{fullPath + ".UpperCase"}, $uCallExpr);
  27. utest.Assert.equals($v{fullPath + ".lowerCase"}, $lCallExpr);
  28. };
  29. }
  30. public static macro function resolves(path:String) {
  31. return try { Context.getType(path); macro true; } catch (_:Any) macro false;
  32. }
  33. }