/
Syntax.hx
356 lines (311 loc) · 9.63 KB
/
Syntax.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package php;
import haxe.Rest;
import haxe.extern.AsVar;
import haxe.extern.EitherType;
/**
Special extern class to support PHP language specifics.
Don't use these functions unless you are really sure what you are doing.
**/
@:noClosure
extern class Syntax {
/**
Embeds plain php code.
`php` should be a string literal with php code.
It can contain placeholders like `{0}`, `{1}` which will be replaced with corresponding arguments from `args`.
E.g.:
```haxe
Syntax.code("var_dump({0}, {1})", a, b);
```
will generate
```haxe
var_dump($a, $b);
```
**/
static function code(code:String, args:Rest<Dynamic>):Dynamic;
/**
The same as `code()`, but adds dereferencing
when required to workaround "cannot use temporary expression in write context" php error.
**/
static function codeDeref(code:String, args:Rest<Dynamic>):Dynamic;
/**
Generates `$left <=> $right`
**/
static inline function spaceship(left:Dynamic, right:Dynamic):Int {
return code('({0} <=> {1})', left, right);
}
/**
Generates `$left ?? $right`
**/
static function coalesce<T>(left:T, right:T):T;
/**
Generates `$left . $right`
**/
static inline function concat(left:String, right:String):String {
return code('({0} . {1})', left, right);
}
/**
Generates `$left == $right`
**/
static inline function equal(left:Dynamic, right:Dynamic):Bool {
return code('({0} == {1})', left, right);
}
/**
Generates `$left === $right`
**/
static inline function strictEqual(left:Dynamic, right:Dynamic):Bool {
return code('({0} === {1})', left, right);
}
/**
Generates `$left != $right`
**/
static inline function notEqual(left:Dynamic, right:Dynamic):Bool {
return code('({0} != {1})', left, right);
}
/**
Generates `$left !== $right`
**/
static inline function strictNotEqual(left:Dynamic, right:Dynamic):Bool {
return code('({0} !== {1})', left, right);
}
/**
Generates `$left + $right` for numbers.
**/
static inline function add<T:Float>(left:T, right:T):T {
return code('({0} + {1})', left, right);
}
/**
Generates `$left + $right` for php arrays.
**/
static inline function union(left:NativeArray, right:NativeArray):NativeArray {
return codeDeref('({0} + {1})', left, right);
}
/**
Generates `$left ** $right`
**/
static inline function exp<T:Float>(left:T, right:T):T {
return code('({0} ** {1})', left, right);
}
/**
Generates `$left % $right`
**/
static inline function mod(left:Float, right:Float):Int {
return code('({0} % {1})', left, right);
}
/**
Generates `$left ?: $right`
**/
static inline function shortTernary<T>(left:T, right:T):T {
return codeDeref('({0} ?: {1})', left, right);
}
/**
Generates `$left xor $right`
**/
static inline function xor(left:Bool, right:Bool):Bool {
return code('({0} xor {1})', left, right);
}
/**
Generates `(int)$value`
**/
static inline function int(value:Dynamic):Int {
return code('(int)({0})', value);
}
/**
Generates `(float)$value`
**/
static inline function float(value:Dynamic):Float {
return code('(float)({0})', value);
}
/**
Generates `(string)$value`
**/
static inline function string(value:Dynamic):String {
return code('(string)({0})', value);
}
/**
Generates `(bool)$value`
**/
static inline function bool(value:Dynamic):Bool {
return code('(bool)({0})', value);
}
/**
Generates `(object)$value`
**/
static inline function object(value:Dynamic):StdClass {
return codeDeref('((object)({0}))', value);
}
/**
Generates `(array)$value`
**/
static inline function array(value:Dynamic):NativeArray {
return codeDeref('((array)({0}))', value);
}
/**
Generates `$value instanceof $phpClassName`.
Haxe generates `Std.isOfType(value, Type)` calls as `$value instanceof Type` automatically where possible.
So you may need this only if you have a `Class` stored in a variable.
**/
@:overload(function(value:AsVar<Dynamic>, phpClassName:AsVar<String>):Bool {})
static function instanceof<V, C>(value:AsVar<V>, type:AsVar<Class<C>>):Bool;
/**
Generates PHP class name for a provided Haxe class.
```haxe
trace(Syntax.nativeClassName(php.Web)); // outputs: php\Web
```
**/
static function nativeClassName<T>(cls:EitherType<Class<T>, Enum<T>>):String;
/**
```haxe
Syntax.foreach(collection, function(key, value) trace(key, value));
```
generates:
```haxe
foreach($collection as $key => $value) {
trace($key, $value);
}
```
**/
static inline function foreach<TCollection, TKey, TValue>(collection:TCollection, body:TKey->TValue->Void):Void {
while (Syntax.foreachCondition) {
Syntax.foreachCollection(collection);
var key:TKey = Syntax.foreachKey();
var value:TValue = Syntax.foreachValue();
Syntax.keepVar(key, value, key, value);
body(key, value);
}
}
static private var foreachCondition:Bool;
static private function foreachCollection<T>(collection:T):Void;
static private function foreachKey<T>():T;
static private function foreachValue<T>():T;
/**
Generates `new $className($arg1, ...$argN)`
**/
@:overload(function(className:AsVar<String>, args:Rest<Dynamic>):Dynamic {})
static function construct<T>(cls:AsVar<Class<T>>, args:Rest<Dynamic>):T;
/**
Generates instance field access for reading on `object`
**/
static function field<T>(object:AsVar<T>, fieldName:String):Dynamic;
/**
Generates instance field access for reading on `object`
**/
@:deprecated("php.Syntax.getField() is deprecated. Use php.Syntax.field() instead.")
static function getField<T>(object:AsVar<T>, fieldName:String):Dynamic;
/**
Generates instance field access for writing on `object`
**/
static function setField<T>(object:AsVar<T>, fieldName:String, value:Dynamic):Void;
/**
Generates static field access for reading on `className`
**/
static function getStaticField(className:AsVar<EitherType<Class<Dynamic>, String>>, fieldName:String):Dynamic;
/**
Generates static field access for writing on `object`
**/
static function setStaticField(object:AsVar<EitherType<Class<Dynamic>, String>>, fieldName:String, value:Dynamic):Void;
/**
Generates a call to instance method: `$object->{$methodName}(<args>)`
**/
static function call<T>(object:AsVar<T>, methodName:String, args:Rest<Dynamic>):Dynamic;
/**
Generates a call to static method: `$className::{$methodName}(<args>)`
**/
static function staticCall(className:AsVar<EitherType<Class<Dynamic>, String>>, methodName:String, args:Rest<Dynamic>):Dynamic;
/**
```haxe
Syntax.arrayDecl(arg1, arg2, arg3);
```
Generates native array declaration:
```haxe
[$arg1, $arg2, $arg3]
```
**/
static function arrayDecl<T>(args:Rest<T>):NativeIndexedArray<T>;
/**
```haxe
Syntax.customArrayDecl([v1 => v2, v3 => v4]);
```
Generates native array declaration:
```haxe
[$v1 => $v2, $v3 => $v4]
```
**/
macro static function customArrayDecl<T>(decl:haxe.macro.Expr):haxe.macro.Expr.ExprOf<php.NativeArray>;
/**
```haxe
Syntax.assocDecl({field1:'first', field2:2}});
```
Generates native associative array declaration:
```haxe
["field1" => "first", "field2" => 2];
```
This method is not recursive.
Accepts object declarations only.
That means you can't pass an object stored in a variable to this method like `Syntax.assocDecl(someVar)`.
Use `php.Lib.associativeArrayOfObject(someVar)` instead.
**/
static function assocDecl<T:{}>(?arg:T):NativeAssocArray<Dynamic>;
/**
Don't let compiler to optimize away local var passed to this method.
**/
static function keepVar(localVar:Rest<Dynamic>):Void;
/**
Adds `...` operator before `args`
**/
static inline function splat(args:EitherType<NativeArray, Traversable>):Rest<Dynamic> {
return code('...{0}', args);
}
/**
Add errors suppression operator `@` before `expression`
**/
static function suppress<T>(expression:T):T;
/**
Generates `clone $value`.
@see http://php.net/manual/en/language.oop5.cloning.php
**/
static inline function clone<T>(value:T):T {
return Syntax.code('(clone {0})', value);
}
/**
Generates `yield $value`.
@see http://php.net/manual/en/language.generators.syntax.php
**/
static inline function yield(value:Dynamic):Dynamic {
return Syntax.code('yield {0}', value);
}
/**
Generates `yield $key => $value`.
@see http://php.net/manual/en/language.generators.syntax.php
**/
static inline function yieldPair(key:Dynamic, value:Dynamic):Dynamic {
return Syntax.code('yield {0} => {1}', key, value);
}
/**
Generates `yield for $value`.
@see http://php.net/manual/en/language.generators.syntax.php
**/
static inline function yieldFrom(value:Dynamic):Dynamic {
return Syntax.code('yield from {0}', value);
}
}