Syntax.hx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package php;
  2. import haxe.extern.Rest;
  3. import haxe.extern.AsVar;
  4. import haxe.extern.EitherType;
  5. /**
  6. Special extern class to support PHP language specifics.
  7. Don't use these functions unless you are really sure what you are doing.
  8. **/
  9. extern class Syntax {
  10. /**
  11. Embeds plain php code.
  12. `php` should be a string literal with php code.
  13. It can contain placeholders like `{0}`, `{1}` which will be replaced with corresponding arguments from `args`.
  14. E.g.:
  15. ```
  16. Syntax.code("var_dump({0}, {1})", a, b);
  17. ```
  18. will generate
  19. ```
  20. var_dump($a, $b);
  21. ```
  22. **/
  23. static function code(php:String, args:Rest<Dynamic>):Dynamic;
  24. /**
  25. The same as `code()`, but adds dereferencing
  26. when required to workaround "cannot use temporary expression in write context" php error.
  27. **/
  28. static function codeDeref(php:String, args:Rest<Dynamic>):Dynamic;
  29. /**
  30. Generates `$left <=> $right`
  31. **/
  32. static inline function spaceship( left:Dynamic, right:Dynamic ) : Int {
  33. return code('({0} <=> {1})', left, right);
  34. }
  35. /**
  36. Generates `$left ?? $right`
  37. **/
  38. static inline function coalesce<T>( left:T, right:T ) : T {
  39. return codeDeref('({0} ?? {1})', left, right);
  40. }
  41. /**
  42. Generates `$left . $right`
  43. **/
  44. static inline function concat( left:String, right:String ) : String {
  45. return code('({0} . {1})', left, right);
  46. }
  47. /**
  48. Generates `$left == $right`
  49. **/
  50. static inline function equal( left:Dynamic, right:Dynamic ) : Bool {
  51. return code('({0} == {1})', left, right);
  52. }
  53. /**
  54. Generates `$left === $right`
  55. **/
  56. static inline function strictEqual( left:Dynamic, right:Dynamic ) : Bool {
  57. return code('({0} === {1})', left, right);
  58. }
  59. /**
  60. Generates `$left != $right`
  61. **/
  62. static inline function notEqual( left:Dynamic, right:Dynamic ) : Bool {
  63. return code('({0} != {1})', left, right);
  64. }
  65. /**
  66. Generates `$left !== $right`
  67. **/
  68. static inline function strictNotEqual( left:Dynamic, right:Dynamic ) : Bool {
  69. return code('({0} !== {1})', left, right);
  70. }
  71. /**
  72. Generates `$left + $right` for numbers.
  73. **/
  74. static inline function add<T:Float>( left:T, right:T ) : T {
  75. return code('({0} + {1})', left, right);
  76. }
  77. /**
  78. Generates `$left + $right` for php arrays.
  79. **/
  80. static inline function union( left:NativeArray, right:NativeArray ) : NativeArray {
  81. return codeDeref('({0} + {1})', left, right);
  82. }
  83. /**
  84. Generates `$left ** $right`
  85. **/
  86. static inline function exp<T:Float>( left:T, right:T ) : T {
  87. return code('({0} ** {1})', left, right);
  88. }
  89. /**
  90. Generates `$left ?: $right`
  91. **/
  92. static inline function shortTernary<T>( left:T, right:T ) : T {
  93. return codeDeref('({0} ?: {1})', left, right);
  94. }
  95. /**
  96. Generates `$left xor $right`
  97. **/
  98. static inline function xor( left:Bool, right:Bool ) : Bool {
  99. return code('({0} xor {1})', left, right);
  100. }
  101. /**
  102. Generates `(int)$value`
  103. **/
  104. static inline function int( value:Dynamic ) : Int {
  105. return code('(int)({0})', value);
  106. }
  107. /**
  108. Generates `(float)$value`
  109. **/
  110. static inline function float( value:Dynamic ) : Float {
  111. return code('(float)({0})', value);
  112. }
  113. /**
  114. Generates `(string)$value`
  115. **/
  116. static inline function string( value:Dynamic ) : String {
  117. return code('(string)({0})', value);
  118. }
  119. /**
  120. Generates `(bool)$value`
  121. **/
  122. static inline function bool( value:Dynamic ) : Bool {
  123. return code('(bool)({0})', value);
  124. }
  125. /**
  126. Generates `(object)$value`
  127. **/
  128. static inline function object( value:Dynamic ) : StdClass {
  129. return codeDeref('(object)({0})', value);
  130. }
  131. /**
  132. Generates `(array)$value`
  133. **/
  134. static inline function array( value:Dynamic ) : NativeArray {
  135. return codeDeref('(array)({0})', value);
  136. }
  137. /**
  138. Generates `$value instanceof $phpClassName`.
  139. Haxe generates `Std.is(value, Type)` calls as `$value instanceof Type` automatically where possible.
  140. So you may need this only if you have a `Class` stored in a variable.
  141. **/
  142. static function instanceof<V,C>( value:AsVar<V>, type:AsVar<Class<C>> ) : Bool;
  143. /**
  144. ```
  145. Syntax.foreach(collection, function(key, value) trace(key, value));
  146. ```
  147. generates:
  148. ```
  149. foreach($collection as $key => $value) {
  150. trace($key, $value);
  151. }
  152. ```
  153. **/
  154. static function foreach<TCollection,TKey,TValue>( collection:TCollection, body:TKey->TValue->Void ) : Void;
  155. /**
  156. Generates `new $className($arg1, ...$argN)`
  157. **/
  158. static function construct( className:AsVar<String>, args:Rest<Dynamic>) : Dynamic;
  159. /**
  160. Generates instance field access for reading on `object`
  161. **/
  162. static function getField<T>( object:AsVar<T>, fieldName:AsVar<String> ) : Dynamic;
  163. /**
  164. Generates instance field access for writing on `object`
  165. **/
  166. static function setField<T>( object:AsVar<T>, fieldName:AsVar<String>, value:Dynamic ) : Void;
  167. /**
  168. Generates static field access for reading on `className`
  169. **/
  170. static function getStaticField( className:AsVar<EitherType<Class<Dynamic>,String>>, fieldName:AsVar<String> ) : Dynamic;
  171. /**
  172. Generates static field access for writing on `object`
  173. **/
  174. static function setStaticField( object:AsVar<EitherType<Class<Dynamic>,String>>, fieldName:AsVar<String>, value:Dynamic ) : Void;
  175. /**
  176. Generates a call to instance method: `$object->{$methodName}(<args>)`
  177. **/
  178. static function call<T>( object:AsVar<T>, methodName:AsVar<String>, args:Rest<Dynamic> ) : Dynamic;
  179. /**
  180. Generates a call to static method: `$className::{$methodName}(<args>)`
  181. **/
  182. static function staticCall( className:AsVar<EitherType<Class<Dynamic>,String>>, methodName:AsVar<String>, args:Rest<Dynamic> ) : Dynamic;
  183. /**
  184. ```
  185. Syntax.arrayDecl(arg1, arg2, arg3);
  186. ```
  187. Generates native array declaration:
  188. ```
  189. [$arg1, $arg2, $arg3]
  190. ```
  191. **/
  192. static function arrayDecl<T>( args:Rest<T> ) : NativeIndexedArray<T>;
  193. /**
  194. ```
  195. Syntax.assocDecl({field1:'first', field2:2}});
  196. ```
  197. Generates native associative array declaration:
  198. ```
  199. ["field1" => "first", "field2" => 2];
  200. ```
  201. This method is not recursive.
  202. Accepts object declarations only.
  203. That means you can't pass an object stored in a variable to this method like `Syntax.assocDecl(someVar)`.
  204. Use `php.Lib.associativeArrayOfObject(someVar)` instead.
  205. **/
  206. static function assocDecl<T:{}>( ?arg:T ) : NativeAssocArray<Dynamic>;
  207. /**
  208. Don't let compiler to optimize away local var passed to this method.
  209. **/
  210. static function keepVar( localVar:Dynamic ) : Void;
  211. /**
  212. Adds `...` operator before `args`
  213. **/
  214. static inline function splat( args:EitherType<NativeArray, Traversable> ) : Rest<Dynamic> {
  215. return code('...{0}', args);
  216. }
  217. /**
  218. Add errors suppression operator `@` before `expression`
  219. **/
  220. static function suppress<T>( expression:T ) : T;
  221. }