Syntax.hx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 php;
  23. import haxe.extern.Rest;
  24. import haxe.extern.AsVar;
  25. import haxe.extern.EitherType;
  26. /**
  27. Special extern class to support PHP language specifics.
  28. Don't use these functions unless you are really sure what you are doing.
  29. **/
  30. @:noClosure
  31. extern class Syntax {
  32. /**
  33. Embeds plain php code.
  34. `php` should be a string literal with php code.
  35. It can contain placeholders like `{0}`, `{1}` which will be replaced with corresponding arguments from `args`.
  36. E.g.:
  37. ```haxe
  38. Syntax.code("var_dump({0}, {1})", a, b);
  39. ```
  40. will generate
  41. ```haxe
  42. var_dump($a, $b);
  43. ```
  44. **/
  45. static function code(code:String, args:Rest<Dynamic>):Dynamic;
  46. /**
  47. The same as `code()`, but adds dereferencing
  48. when required to workaround "cannot use temporary expression in write context" php error.
  49. **/
  50. static function codeDeref(code:String, args:Rest<Dynamic>):Dynamic;
  51. /**
  52. Generates `$left <=> $right`
  53. **/
  54. static inline function spaceship(left:Dynamic, right:Dynamic):Int {
  55. return code('({0} <=> {1})', left, right);
  56. }
  57. /**
  58. Generates `$left ?? $right`
  59. **/
  60. static function coalesce<T>(left:T, right:T):T;
  61. /**
  62. Generates `$left . $right`
  63. **/
  64. static inline function concat(left:String, right:String):String {
  65. return code('({0} . {1})', left, right);
  66. }
  67. /**
  68. Generates `$left == $right`
  69. **/
  70. static inline function equal(left:Dynamic, right:Dynamic):Bool {
  71. return code('({0} == {1})', left, right);
  72. }
  73. /**
  74. Generates `$left === $right`
  75. **/
  76. static inline function strictEqual(left:Dynamic, right:Dynamic):Bool {
  77. return code('({0} === {1})', left, right);
  78. }
  79. /**
  80. Generates `$left != $right`
  81. **/
  82. static inline function notEqual(left:Dynamic, right:Dynamic):Bool {
  83. return code('({0} != {1})', left, right);
  84. }
  85. /**
  86. Generates `$left !== $right`
  87. **/
  88. static inline function strictNotEqual(left:Dynamic, right:Dynamic):Bool {
  89. return code('({0} !== {1})', left, right);
  90. }
  91. /**
  92. Generates `$left + $right` for numbers.
  93. **/
  94. static inline function add<T:Float>(left:T, right:T):T {
  95. return code('({0} + {1})', left, right);
  96. }
  97. /**
  98. Generates `$left + $right` for php arrays.
  99. **/
  100. static inline function union(left:NativeArray, right:NativeArray):NativeArray {
  101. return codeDeref('({0} + {1})', left, right);
  102. }
  103. /**
  104. Generates `$left ** $right`
  105. **/
  106. static inline function exp<T:Float>(left:T, right:T):T {
  107. return code('({0} ** {1})', left, right);
  108. }
  109. /**
  110. Generates `$left % $right`
  111. **/
  112. static inline function mod(left:Float, right:Float):Int {
  113. return code('({0} % {1})', left, right);
  114. }
  115. /**
  116. Generates `$left ?: $right`
  117. **/
  118. static inline function shortTernary<T>(left:T, right:T):T {
  119. return codeDeref('({0} ?: {1})', left, right);
  120. }
  121. /**
  122. Generates `$left xor $right`
  123. **/
  124. static inline function xor(left:Bool, right:Bool):Bool {
  125. return code('({0} xor {1})', left, right);
  126. }
  127. /**
  128. Generates `(int)$value`
  129. **/
  130. static inline function int(value:Dynamic):Int {
  131. return code('(int)({0})', value);
  132. }
  133. /**
  134. Generates `(float)$value`
  135. **/
  136. static inline function float(value:Dynamic):Float {
  137. return code('(float)({0})', value);
  138. }
  139. /**
  140. Generates `(string)$value`
  141. **/
  142. static inline function string(value:Dynamic):String {
  143. return code('(string)({0})', value);
  144. }
  145. /**
  146. Generates `(bool)$value`
  147. **/
  148. static inline function bool(value:Dynamic):Bool {
  149. return code('(bool)({0})', value);
  150. }
  151. /**
  152. Generates `(object)$value`
  153. **/
  154. static inline function object(value:Dynamic):StdClass {
  155. return codeDeref('((object)({0}))', value);
  156. }
  157. /**
  158. Generates `(array)$value`
  159. **/
  160. static inline function array(value:Dynamic):NativeArray {
  161. return codeDeref('((array)({0}))', value);
  162. }
  163. /**
  164. Generates `$value instanceof $phpClassName`.
  165. Haxe generates `Std.is(value, Type)` calls as `$value instanceof Type` automatically where possible.
  166. So you may need this only if you have a `Class` stored in a variable.
  167. **/
  168. @:overload(function(value:AsVar<Dynamic>, phpClassName:AsVar<String>):Bool {})
  169. static function instanceof<V, C>(value:AsVar<V>, type:AsVar<Class<C>>):Bool;
  170. /**
  171. Generates PHP class name for a provided Haxe class.
  172. ```haxe
  173. trace(Syntax.nativeClassName(php.Web)); // outputs: php\Web
  174. ```
  175. **/
  176. static function nativeClassName<T>(cls:EitherType<Class<T>, Enum<T>>):String;
  177. /**
  178. ```haxe
  179. Syntax.foreach(collection, function(key, value) trace(key, value));
  180. ```
  181. generates:
  182. ```haxe
  183. foreach($collection as $key => $value) {
  184. trace($key, $value);
  185. }
  186. ```
  187. **/
  188. static inline function foreach<TCollection, TKey, TValue>(collection:TCollection, body:TKey->TValue->Void):Void {
  189. while (Syntax.foreachCondition) {
  190. Syntax.foreachCollection(collection);
  191. var key:TKey = Syntax.foreachKey();
  192. var value:TValue = Syntax.foreachValue();
  193. Syntax.keepVar(key, value, key, value);
  194. body(key, value);
  195. }
  196. }
  197. static private var foreachCondition:Bool;
  198. static private function foreachCollection<T>(collection:T):Void;
  199. static private function foreachKey<T>():T;
  200. static private function foreachValue<T>():T;
  201. /**
  202. Generates `new $className($arg1, ...$argN)`
  203. **/
  204. @:overload(function(className:AsVar<String>, args:Rest<Dynamic>):Dynamic {})
  205. static function construct<T>(cls:AsVar<Class<T>>, args:Rest<Dynamic>):T;
  206. /**
  207. Generates instance field access for reading on `object`
  208. **/
  209. static function field<T>(object:AsVar<T>, fieldName:String):Dynamic;
  210. /**
  211. Generates instance field access for reading on `object`
  212. **/
  213. @:deprecated("php.Syntax.getFiled() is deprecated. Use php.Syntax.field() instead.")
  214. static function getField<T>(object:AsVar<T>, fieldName:String):Dynamic;
  215. /**
  216. Generates instance field access for writing on `object`
  217. **/
  218. static function setField<T>(object:AsVar<T>, fieldName:String, value:Dynamic):Void;
  219. /**
  220. Generates static field access for reading on `className`
  221. **/
  222. static function getStaticField(className:AsVar<EitherType<Class<Dynamic>, String>>, fieldName:String):Dynamic;
  223. /**
  224. Generates static field access for writing on `object`
  225. **/
  226. static function setStaticField(object:AsVar<EitherType<Class<Dynamic>, String>>, fieldName:String, value:Dynamic):Void;
  227. /**
  228. Generates a call to instance method: `$object->{$methodName}(<args>)`
  229. **/
  230. static function call<T>(object:AsVar<T>, methodName:String, args:Rest<Dynamic>):Dynamic;
  231. /**
  232. Generates a call to static method: `$className::{$methodName}(<args>)`
  233. **/
  234. static function staticCall(className:AsVar<EitherType<Class<Dynamic>, String>>, methodName:String, args:Rest<Dynamic>):Dynamic;
  235. /**
  236. ```haxe
  237. Syntax.arrayDecl(arg1, arg2, arg3);
  238. ```
  239. Generates native array declaration:
  240. ```haxe
  241. [$arg1, $arg2, $arg3]
  242. ```
  243. **/
  244. static function arrayDecl<T>(args:Rest<T>):NativeIndexedArray<T>;
  245. /**
  246. ```haxe
  247. Syntax.assocDecl({field1:'first', field2:2}});
  248. ```
  249. Generates native associative array declaration:
  250. ```haxe
  251. ["field1" => "first", "field2" => 2];
  252. ```
  253. This method is not recursive.
  254. Accepts object declarations only.
  255. That means you can't pass an object stored in a variable to this method like `Syntax.assocDecl(someVar)`.
  256. Use `php.Lib.associativeArrayOfObject(someVar)` instead.
  257. **/
  258. static function assocDecl<T:{}>(?arg:T):NativeAssocArray<Dynamic>;
  259. /**
  260. Don't let compiler to optimize away local var passed to this method.
  261. **/
  262. static function keepVar(localVar:Rest<Dynamic>):Void;
  263. /**
  264. Adds `...` operator before `args`
  265. **/
  266. static inline function splat(args:EitherType<NativeArray, Traversable>):Rest<Dynamic> {
  267. return code('...{0}', args);
  268. }
  269. /**
  270. Add errors suppression operator `@` before `expression`
  271. **/
  272. static function suppress<T>(expression:T):T;
  273. /**
  274. Generates `clone $value`.
  275. @see http://php.net/manual/en/language.oop5.cloning.php
  276. **/
  277. static inline function clone<T>(value:T):T {
  278. return Syntax.code('(clone {0})', value);
  279. }
  280. /**
  281. Generates `yield $value`.
  282. @see http://php.net/manual/en/language.generators.syntax.php
  283. **/
  284. static inline function yield(value:Dynamic):Dynamic {
  285. return Syntax.code('yield {0}', value);
  286. }
  287. /**
  288. Generates `yield $key => $value`.
  289. @see http://php.net/manual/en/language.generators.syntax.php
  290. **/
  291. static inline function yieldPair(key:Dynamic, value:Dynamic):Dynamic {
  292. return Syntax.code('yield {0} => {1}', key, value);
  293. }
  294. /**
  295. Generates `yield for $value`.
  296. @see http://php.net/manual/en/language.generators.syntax.php
  297. **/
  298. static inline function yieldFrom(value:Dynamic):Dynamic {
  299. return Syntax.code('yield from {0}', value);
  300. }
  301. }