2
0

Syntax.macro.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package python;
  23. import haxe.macro.Expr;
  24. import haxe.macro.Context;
  25. import haxe.macro.ExprTools;
  26. @:noPackageRestrict
  27. @:noClosure
  28. class Syntax {
  29. @:noUsing
  30. macro public static function importModule(module:String):Expr {
  31. return macro python.Syntax.code($v{"import " + module});
  32. }
  33. @:noUsing
  34. macro public static function importAs(module:String, className:String):ExprOf<Void> {
  35. var n = className.split(".").join("_");
  36. var e = "import " + module + " as " + n;
  37. return macro python.Syntax.code($v{e});
  38. }
  39. @:noUsing
  40. @:deprecated("python.Syntax.newInstance() is deprecated. Use python.Syntax.construct() instead.")
  41. macro public static function newInstance(c:Expr, params:Array<Expr>):Expr {
  42. return macro python.Syntax._newInstance($c, $a{params});
  43. }
  44. @:noUsing
  45. @:deprecated("python.Syntax.pythonCode() is deprecated. Use python.Syntax.code() instead.")
  46. macro public static function pythonCode(b:ExprOf<String>, rest:Array<Expr>):Expr {
  47. if (rest == null)
  48. rest = [];
  49. return macro @:pos(Context.currentPos()) untyped python.Syntax._pythonCode($b, $a{rest});
  50. }
  51. @:noUsing
  52. macro public static function arrayAccess(x:Expr, rest:Array<Expr>):ExprOf<Dynamic> {
  53. return macro python.Syntax._arrayAccess($x, $a{rest});
  54. }
  55. @:noUsing
  56. macro public static function arrayAccessWithTrailingColon(x:Expr, rest:Array<Expr>):ExprOf<Dynamic> {
  57. return macro python.Syntax._arrayAccess($x, $a{rest}, true);
  58. }
  59. @:noUsing
  60. macro public static function foreach<T>(v:Expr, it:Expr, b:Expr):Expr {
  61. var id = switch (v.expr) {
  62. case EConst(CIdent(x)): x;
  63. case _: Context.error("unexpected " + ExprTools.toString(v) + ": const ident expected", v.pos);
  64. }
  65. var iter = try {
  66. var it = macro($it.__iter__() : python.NativeIterator.NativeIteratorRaw<T>);
  67. Context.typeof(it);
  68. it;
  69. } catch (e:Dynamic) {
  70. macro($it : python.NativeIterable.NativeIterableRaw<T>);
  71. }
  72. return macro {
  73. var $id = null;
  74. python.Syntax._foreach($v, $it, cast $b);
  75. }
  76. }
  77. @:noUsing
  78. macro public static function importFromAs(from:String, module:String, className:String):ExprOf<Void> {
  79. var n = className.split(".").join("_");
  80. var e = "from " + from + " import " + module + " as " + n;
  81. return macro python.Syntax.code($v{e});
  82. }
  83. @:noUsing
  84. macro public static function callField(o:Expr, field:ExprOf<String>, params:Array<Expr>):Expr {
  85. return macro @:pos(o.pos) python.Syntax.call(python.Syntax.field($o, $field), $a{params});
  86. }
  87. @:noUsing
  88. macro public static function tuple(args:Array<Expr>):Dynamic {
  89. var args = macro $a{args};
  90. return macro python.Syntax._tuple($args);
  91. }
  92. macro public static function callNamedUntyped(e:Expr, args:Expr):Expr {
  93. return macro @:pos(e.pos) python.Syntax._callNamedUntyped($e, $args);
  94. }
  95. }