Syntax.hx 9.4 KB

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