forked from dan-da/php2js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php2js2.php
520 lines (431 loc) · 15.6 KB
/
php2js2.php
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
main($argv);
function assert_cb($file, $line, $message) {
echo "Assert failed: " . $message . "\n\n";
debug_print_backtrace();
exit;
}
function main($argv) {
ini_set( 'error_reporting', E_ALL | E_STRICT);
ini_set( 'display_errors', 'on');
ini_set( 'memory_limit', '-1');
assert_options( ASSERT_CALLBACK, 'assert_cb');
$proc = new phpjs();
$proc->translate_file( $argv[1] );
}
class phpjs {
var $xpath;
var $jsbuf;
var $pretty = true;
var $need_closing_paren = 0;
var $in_quoted_string = false;
var $local_scope = array();
var $context = array('global_scope');
var $array_brace_count = 0;
var $array_assoc_count = array();
function __construct() {
$this->local_scope[] = array(); // the global scope.
}
function translate_file($filename) {
$buf = file_get_contents( $filename );
return $this->translate( $buf );
}
function translate( $code_buf ) {
$tokens = token_get_all( $code_buf );
// $this->_print_tokens( $tokens ); //exit;
$this->boilerplate();
$this->_translate( $tokens );
echo "\n" . $this->jsbuf . "\n";
}
function boilerplate() {
$this->jsbuf .= "
function ___array() {
var arr = [];
for (var i = 0; i < ___array.arguments.length; ++i) {
var item = ___array.arguments[i];
if(item instanceof Array) {
arr[item[0]] = item[1];
}
else {
arr.push( item );
}
}
return arr;
}
";
}
function _print_tokens( $tokens ) {
foreach( $tokens as $token ) {
if(is_array($token))
{
print(token_name($token[0]) . ": '" . $token[1] . "'\n");
}
else if( is_long( $token )) {
echo token_name( $token ) . "\n";
}
else {
echo $token . "\n";
}
}
}
function _translate( $tokens ) {
$this->tokens = $tokens;
$this->idx_current_token = 0;
$this->in_quoted_string = false;
for( $this->idx_current_token = 0; $this->idx_current_token < count( $this->tokens ); $this->idx_current_token ++) {
$token = $this->tokens[$this->idx_current_token];
if(is_array($token))
{
$token_name = token_name($token[0]);
$token_value = $token[1];
}
else if( is_long( $token )) {
$token_name = token_name( $token );
}
else {
$token_name = $token;
$token_value = $token;
}
$method_name = strtolower($token_name);
if( $this->array_brace_count ) {
if( $this->get_token_name($tokens[ $this->idx_current_token + 2 ] ) == 'T_DOUBLE_ARROW' ) {
$this->jsbuf .= '[';
if( !isset($this->array_assoc_count[$this->array_brace_count] )) {
$this->array_assoc_count[$this->array_brace_count] = 0;
}
$this->array_assoc_count[$this->array_brace_count] ++;
}
}
if( strlen( $method_name) == 1) {
$this->handle_char( $method_name );
}
else if( method_exists($this, $method_name )) {
$token[2] = token_name( $token[0] );
$skip_ahead = $this->$method_name( $token );
}
else {
$this->jsbuf .= $token_value;
}
if( $skip_ahead ) {
$this->idx_current_token += $skip_ahead;
}
// $this->idx_current_token ++;
}
}
function context($depth) {
$idx = count($this->context) - 1 - $depth;
if( $idx > -1 ) {
return $this->context[$idx];
}
return null;
}
function get_token( $token ) {
$token_name = null;
if(is_array($token))
{
return array(token_name($token[0]), $token[1]);
}
else {
return array( $token, $token );
}
}
function get_token_by_idx( $idx ) {
return $this->get_token( $this->tokens[$idx] );
}
function get_token_name( $token ) {
$tok = $this->get_token( $token );
return $tok[0];
}
function get_token_value( $token ) {
$tok = $this->get_token( $token );
return $tok[1];
}
function get_token_name_by_idx( $idx ) {
return $this->get_token_name( $this->tokens[$idx] );
}
function get_token_value_by_idx( $idx ) {
return $this->get_token_value( $this->tokens[$idx] );
}
function t_echo( $token ) {
$this->jsbuf .= 'document.write(';
$this->need_closing_paren++;
}
function t_open_tag( $token ) {
}
function t_close_tag( $token ) {
}
function t_whitespace( $token ) {
if( $this->pretty ) {
$this->jsbuf .= $token[1];
}
}
function t_array( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= '___array(';
$this->array_brace_count ++;
}
function t_global( $token ) {
$this->context[] = $token[1];
}
function t_variable( $token, $is_decl = null ) {
if( $this->in_quoted_string ) {
$this->jsbuf .= '"+';
}
$var_name = str_replace( '$', '', $token[1] );
if( $is_decl === null ) {
$prev_id = $this->consume_whitespace_reverse( $this->idx_current_token -1 );
$prev_name = $this->get_token_name_by_idx( $prev_id );
if( in_array($prev_name, array('{',')', '}', ';') ) ) {
$next_id = $this->consume_whitespace( $this->idx_current_token +1 );
$next_name = $this->get_token_name_by_idx( $next_id );
if( $next_name == ';' || $next_name == '=') {
$is_decl = true;
}
}
}
//echo $var_name . ' : ' . $is_decl . "\n";
if( $is_decl ) {
$scope_idx = count($this->local_scope)-1;
$declared_in_scope = false;
do {
if( isset( $this->local_scope[$scope_idx][$var_name])) {
$declared_in_scope = true;
break;
}
if( isset($this->local_scope[$scope_idx]['___scope_func'])) {
break;
}
} while ( --$scope_idx > 0 );
if( !$declared_in_scope ) {
if( !$this->in_function_decl() ) {
$this->jsbuf .= 'var ';
}
$this->local_scope[count($this->local_scope)-1][$var_name] = 1;
}
}
if( $this->in_global_decl() ) {
// variables are global by default in JS. no need to declare them. but we do want it in local scope so we don't try to redeclare later.
$this->local_scope[count($this->local_scope)-1][$var_name] = 1;
return;
}
// echo $var_name . "\n";
// print_r( $this->local_scope );
// echo "\n";
$this->jsbuf .= $var_name;
if( $this->in_quoted_string ) {
$this->jsbuf .= '+"';
}
}
function in_function_decl() {
return $this->context(0) == '(' && $this->context(1) == 'function';
}
function in_global_decl() {
return $this->context(0) == 'global';
}
function t_function( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= 'function';
}
function t_while( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= $token[1];
}
function t_if( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= $token[1];
}
function t_elseif( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= 'else if';
}
function t_else( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= $token[1];
}
function t_for( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= $token[1];
}
function t_switch( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= $token[1];
}
function t_do( $token ) {
$this->context[] = $token[1];
$this->jsbuf .= $token[1];
}
// foreach( $foo as $key => val)
function t_foreach( $token ) {
$this->context[] = $token[1];
/*
for( $i = 0; $i < 20; $i ++ ) {
echo sprintf("i = $i, name=%s, value=%s\n", $this->get_token_name_by_idx($this->idx_current_token+$i), $this->get_token_value_by_idx($this->idx_current_token+$i));
}
*/
// advance to opening paren
$idx = $this->consume_whitespace( $this->idx_current_token + 1 );
// advance to variable
$idx = $this->consume_whitespace( $idx + 1 );
$arr = $this->get_token_value_by_idx( $idx );
// advance to 'as'
$idx = $this->consume_whitespace( $idx + 1 );
// advance to key (or val)
$idx = $this->consume_whitespace( $idx + 1 );
$key_or_val = $this->get_token_value_by_idx( $idx );
// advance to closing paren (or =>)
$idx = $this->consume_whitespace( $idx + 1 );
$paren_or_double_arrow = $this->get_token_name_by_idx( $idx );
$is_assoc = $paren_or_double_arrow == 'T_DOUBLE_ARROW';
if( $is_assoc ) {
$key = $key_or_val;
// advance to value
$idx = $this->consume_whitespace( $idx + 1 );
$value = $this->get_token_value_by_idx( $idx );
// advance to closing paren
$idx = $this->consume_whitespace( $idx + 1 );
}
else {
$key = '___key';
$value = $key_or_val;
}
$key = str_replace( '$', '',$key);
$value = str_replace( '$', '', $value);
$arr = str_replace( '$', '', $arr);
$this->jsbuf .= 'for(';
$this->t_variable( array( T_VARIABLE, $key), true);
$this->jsbuf .= sprintf( ' in %s)', $arr );
$this->handle_char( '{');
$this->t_variable( array( T_VARIABLE, $value ), true );
$this->jsbuf .= sprintf(' = %s[%s];', $arr, $key );
// advance to next token. may be an opening brace.
$idx = $this->consume_whitespace( $idx + 1 );
$open_brace_or_other = $this->get_token_name_by_idx( $idx );
if( $open_brace_or_other == '{') {
$idx ++;
}
else { // single statement foreach, no braces used. So we need to add a closing brace at end of statement.
$idx2 = $this->advance_to_token( $idx + 1, ';' );
$this->tokens = array_insert( $this->tokens, $idx2+1, '}');
$idx --;
// $this->_print_tokens($this->tokens); exit;
}
return $idx - $this->idx_current_token;
}
function consume_whitespace($idx) {
$token_name = $this->get_token_name_by_idx( $idx );
while( $token_name == 'T_WHITESPACE' ) {
$idx ++;
$token_name = $this->get_token_name_by_idx( $idx );
}
return $idx;
}
function consume_whitespace_reverse($idx) {
$token_name = $this->get_token_name_by_idx( $idx );
while( $token_name == 'T_WHITESPACE' ) {
$idx --;
$token_name = $this->get_token_name_by_idx( $idx );
}
return $idx;
}
function advance_to_token($idx, $token_name_target) {
$token_name = $this->get_token_name_by_idx( $idx );
while( $token_name != $token_name_target ) {
$idx ++;
$token_name = $this->get_token_name_by_idx( $idx );
}
return $idx;
}
function handle_char( $char ) {
switch( $char ) {
case ';':
$in_global_decl = $this->in_global_decl();
if( !in_array( $this->context(0), array('(','{','global_scope') ) ) {
array_pop( $this->context );
}
// avoid printing 'global $var;' lines.
if( $in_global_decl ) {
break;
}
if( $this->need_closing_paren ) {
$this->jsbuf .= ')';
$this->need_closing_paren--;
}
$this->jsbuf .= $char;
break;
case '"':
$this->in_quoted_string = !$this->in_quoted_string;
$this->jsbuf .= $char;
break;
case '.':
$this->jsbuf .= '+';
break;
case ',':
if( $this->in_global_decl() ) {
// variables are global by default in JS. no need to declare them.
break;
}
if( $this->array_brace_count ) {
if( aget( $this->array_assoc_count, $this->array_brace_count ) ) {
$this->array_assoc_count[$this->array_brace_count] --;
$this->jsbuf .= ']';
}
}
$this->jsbuf .= ',';
break;
case '{':
$this->context[] = $char;
// create new variable scope
$this->local_scope[] = array();
if( $this->context(1) == 'function' ) {
// specify that this is function scope, which is special.
$this->local_scope[count($this->local_scope)-1]['___scope_func'] = true;
}
// print_r( $this->local_scope );
$this->jsbuf .= $char;
break;
case '}':
array_pop( $this->context );
if( !in_array($this->context(0), array('(','{') ) ) {
array_pop( $this->context );
}
array_pop( $this->local_scope );
$this->jsbuf .= $char;
break;
case '(':
$this->context[] = $char;
if( $this->array_brace_count ) {
break;
}
$this->jsbuf .= $char;
break;
case ')':
array_pop( $this->context );
if( $this->array_brace_count ) {
if( aget( $this->array_assoc_count, $this->array_brace_count ) ) {
$this->array_assoc_count[$this->array_brace_count] --;
$this->jsbuf .= ']';
}
$this->array_brace_count --;
}
$this->jsbuf .= ')';
break;
default:
$this->jsbuf .= $char;
break;
}
}
function t_double_arrow( $token ) {
$this->jsbuf .= ',';
}
}
function aget( $arr, $key ) {
return isset( $arr[$key]) ? $arr[$key] : null;
}
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
?>