1--TEST--
2Test token_get_all() function : usage variations - with control structure tokens
3--SKIPIF--
4<?php if (!extension_loaded("tokenizer")) print "skip"; ?>
5--FILE--
6<?php
7/* Prototype  : array token_get_all(string $source)
8 * Description: splits the given source into an array of PHP languange tokens
9 * Source code: ext/tokenizer/tokenizer.c
10*/
11
12/*
13 * Using different control structure keywords
14 *   if..else, elseif - T_IF(301), T_ELSEIF(302), T_ELSE(303)
15 *   while - T_WHILE(318)
16 *   do...while - T_DO(317)
17 *   for - T_ENDFOR(320)
18 *   foreach - T_ENDFOREACH(322)
19 *   switch...case - T_ENDSWITCH(327), T_CASE(329)
20 *   break - T_BREAK(331)
21 *   continue - T_CONTINUE(332)
22*/
23
24echo "*** Testing token_get_all() : for control structure tokens ***\n";
25
26// if..elseif....else
27echo "-- with if..elseif..else..tokens --\n";
28
29$source = '<?php
30if($a == true) {
31     echo "$a = true";
32}
33elseif($a == false) {
34  echo false;
35}
36else
37  echo 1;
38?>';
39
40var_dump( token_get_all($source));
41
42// while..., do..while, break, continue
43echo "-- with while..., do..while, switch & continue tokens --\n";
44
45$source = "<?php while(true) {
46echo 'True';
47break;
48}
49do {
50continue;
51}while(false); ?>";
52
53var_dump( token_get_all($source));
54
55// for..., foreach( as )
56echo "-- with for..foreach( as ) tokens --\n";
57
58$source = '<?php for($count=0; $count < 5; $count++) {
59echo $count;
60}
61foreach($values as $index) {
62continue;
63} ?>';
64
65var_dump( token_get_all($source));
66
67// switch..case, default
68echo "-- with switch...case tokens --\n";
69
70$source = '<?php switch($var)
71case 1: break;
72default: echo "default"; ?>';
73
74var_dump( token_get_all($source));
75
76echo "Done"
77?>
78--EXPECTF--
79*** Testing token_get_all() : for control structure tokens ***
80-- with if..elseif..else..tokens --
81array(49) {
82  [0]=>
83  array(3) {
84    [0]=>
85    int(%d)
86    [1]=>
87    string(6) "<?php "
88    [2]=>
89    int(1)
90  }
91  [1]=>
92  array(3) {
93    [0]=>
94    int(%d)
95    [1]=>
96    string(1) "
97"
98    [2]=>
99    int(1)
100  }
101  [2]=>
102  array(3) {
103    [0]=>
104    int(%d)
105    [1]=>
106    string(2) "if"
107    [2]=>
108    int(2)
109  }
110  [3]=>
111  string(1) "("
112  [4]=>
113  array(3) {
114    [0]=>
115    int(%d)
116    [1]=>
117    string(2) "$a"
118    [2]=>
119    int(2)
120  }
121  [5]=>
122  array(3) {
123    [0]=>
124    int(%d)
125    [1]=>
126    string(1) " "
127    [2]=>
128    int(2)
129  }
130  [6]=>
131  array(3) {
132    [0]=>
133    int(283)
134    [1]=>
135    string(2) "=="
136    [2]=>
137    int(2)
138  }
139  [7]=>
140  array(3) {
141    [0]=>
142    int(%d)
143    [1]=>
144    string(1) " "
145    [2]=>
146    int(2)
147  }
148  [8]=>
149  array(3) {
150    [0]=>
151    int(%d)
152    [1]=>
153    string(4) "true"
154    [2]=>
155    int(2)
156  }
157  [9]=>
158  string(1) ")"
159  [10]=>
160  array(3) {
161    [0]=>
162    int(%d)
163    [1]=>
164    string(1) " "
165    [2]=>
166    int(2)
167  }
168  [11]=>
169  string(1) "{"
170  [12]=>
171  array(3) {
172    [0]=>
173    int(%d)
174    [1]=>
175    string(6) "
176     "
177    [2]=>
178    int(2)
179  }
180  [13]=>
181  array(3) {
182    [0]=>
183    int(%d)
184    [1]=>
185    string(4) "echo"
186    [2]=>
187    int(%d)
188  }
189  [14]=>
190  array(3) {
191    [0]=>
192    int(%d)
193    [1]=>
194    string(1) " "
195    [2]=>
196    int(%d)
197  }
198  [15]=>
199  string(1) """
200  [16]=>
201  array(3) {
202    [0]=>
203    int(%d)
204    [1]=>
205    string(2) "$a"
206    [2]=>
207    int(%d)
208  }
209  [17]=>
210  array(3) {
211    [0]=>
212    int(%d)
213    [1]=>
214    string(7) " = true"
215    [2]=>
216    int(%d)
217  }
218  [18]=>
219  string(1) """
220  [19]=>
221  string(1) ";"
222  [20]=>
223  array(3) {
224    [0]=>
225    int(%d)
226    [1]=>
227    string(1) "
228"
229    [2]=>
230    int(%d)
231  }
232  [21]=>
233  string(1) "}"
234  [22]=>
235  array(3) {
236    [0]=>
237    int(%d)
238    [1]=>
239    string(1) "
240"
241    [2]=>
242    int(4)
243  }
244  [23]=>
245  array(3) {
246    [0]=>
247    int(%d)
248    [1]=>
249    string(6) "elseif"
250    [2]=>
251    int(5)
252  }
253  [24]=>
254  string(1) "("
255  [25]=>
256  array(3) {
257    [0]=>
258    int(%d)
259    [1]=>
260    string(2) "$a"
261    [2]=>
262    int(5)
263  }
264  [26]=>
265  array(3) {
266    [0]=>
267    int(%d)
268    [1]=>
269    string(1) " "
270    [2]=>
271    int(5)
272  }
273  [27]=>
274  array(3) {
275    [0]=>
276    int(283)
277    [1]=>
278    string(2) "=="
279    [2]=>
280    int(5)
281  }
282  [28]=>
283  array(3) {
284    [0]=>
285    int(%d)
286    [1]=>
287    string(1) " "
288    [2]=>
289    int(5)
290  }
291  [29]=>
292  array(3) {
293    [0]=>
294    int(%d)
295    [1]=>
296    string(5) "false"
297    [2]=>
298    int(5)
299  }
300  [30]=>
301  string(1) ")"
302  [31]=>
303  array(3) {
304    [0]=>
305    int(%d)
306    [1]=>
307    string(1) " "
308    [2]=>
309    int(5)
310  }
311  [32]=>
312  string(1) "{"
313  [33]=>
314  array(3) {
315    [0]=>
316    int(%d)
317    [1]=>
318    string(3) "
319  "
320    [2]=>
321    int(5)
322  }
323  [34]=>
324  array(3) {
325    [0]=>
326    int(%d)
327    [1]=>
328    string(4) "echo"
329    [2]=>
330    int(6)
331  }
332  [35]=>
333  array(3) {
334    [0]=>
335    int(%d)
336    [1]=>
337    string(1) " "
338    [2]=>
339    int(6)
340  }
341  [36]=>
342  array(3) {
343    [0]=>
344    int(%d)
345    [1]=>
346    string(5) "false"
347    [2]=>
348    int(6)
349  }
350  [37]=>
351  string(1) ";"
352  [38]=>
353  array(3) {
354    [0]=>
355    int(%d)
356    [1]=>
357    string(1) "
358"
359    [2]=>
360    int(6)
361  }
362  [39]=>
363  string(1) "}"
364  [40]=>
365  array(3) {
366    [0]=>
367    int(%d)
368    [1]=>
369    string(1) "
370"
371    [2]=>
372    int(7)
373  }
374  [41]=>
375  array(3) {
376    [0]=>
377    int(%d)
378    [1]=>
379    string(4) "else"
380    [2]=>
381    int(8)
382  }
383  [42]=>
384  array(3) {
385    [0]=>
386    int(%d)
387    [1]=>
388    string(3) "
389  "
390    [2]=>
391    int(8)
392  }
393  [43]=>
394  array(3) {
395    [0]=>
396    int(%d)
397    [1]=>
398    string(4) "echo"
399    [2]=>
400    int(9)
401  }
402  [44]=>
403  array(3) {
404    [0]=>
405    int(%d)
406    [1]=>
407    string(1) " "
408    [2]=>
409    int(9)
410  }
411  [45]=>
412  array(3) {
413    [0]=>
414    int(%d)
415    [1]=>
416    string(1) "1"
417    [2]=>
418    int(9)
419  }
420  [46]=>
421  string(1) ";"
422  [47]=>
423  array(3) {
424    [0]=>
425    int(%d)
426    [1]=>
427    string(1) "
428"
429    [2]=>
430    int(9)
431  }
432  [48]=>
433  array(3) {
434    [0]=>
435    int(%d)
436    [1]=>
437    string(2) "?>"
438    [2]=>
439    int(10)
440  }
441}
442-- with while..., do..while, switch & continue tokens --
443array(33) {
444  [0]=>
445  array(3) {
446    [0]=>
447    int(%d)
448    [1]=>
449    string(6) "<?php "
450    [2]=>
451    int(1)
452  }
453  [1]=>
454  array(3) {
455    [0]=>
456    int(%d)
457    [1]=>
458    string(5) "while"
459    [2]=>
460    int(1)
461  }
462  [2]=>
463  string(1) "("
464  [3]=>
465  array(3) {
466    [0]=>
467    int(%d)
468    [1]=>
469    string(4) "true"
470    [2]=>
471    int(1)
472  }
473  [4]=>
474  string(1) ")"
475  [5]=>
476  array(3) {
477    [0]=>
478    int(%d)
479    [1]=>
480    string(1) " "
481    [2]=>
482    int(1)
483  }
484  [6]=>
485  string(1) "{"
486  [7]=>
487  array(3) {
488    [0]=>
489    int(%d)
490    [1]=>
491    string(1) "
492"
493    [2]=>
494    int(1)
495  }
496  [8]=>
497  array(3) {
498    [0]=>
499    int(%d)
500    [1]=>
501    string(4) "echo"
502    [2]=>
503    int(2)
504  }
505  [9]=>
506  array(3) {
507    [0]=>
508    int(%d)
509    [1]=>
510    string(1) " "
511    [2]=>
512    int(2)
513  }
514  [10]=>
515  array(3) {
516    [0]=>
517    int(%d)
518    [1]=>
519    string(6) "'True'"
520    [2]=>
521    int(2)
522  }
523  [11]=>
524  string(1) ";"
525  [12]=>
526  array(3) {
527    [0]=>
528    int(%d)
529    [1]=>
530    string(1) "
531"
532    [2]=>
533    int(2)
534  }
535  [13]=>
536  array(3) {
537    [0]=>
538    int(%d)
539    [1]=>
540    string(5) "break"
541    [2]=>
542    int(%d)
543  }
544  [14]=>
545  string(1) ";"
546  [15]=>
547  array(3) {
548    [0]=>
549    int(%d)
550    [1]=>
551    string(1) "
552"
553    [2]=>
554    int(%d)
555  }
556  [16]=>
557  string(1) "}"
558  [17]=>
559  array(3) {
560    [0]=>
561    int(%d)
562    [1]=>
563    string(1) "
564"
565    [2]=>
566    int(4)
567  }
568  [18]=>
569  array(3) {
570    [0]=>
571    int(%d)
572    [1]=>
573    string(2) "do"
574    [2]=>
575    int(5)
576  }
577  [19]=>
578  array(3) {
579    [0]=>
580    int(%d)
581    [1]=>
582    string(1) " "
583    [2]=>
584    int(5)
585  }
586  [20]=>
587  string(1) "{"
588  [21]=>
589  array(3) {
590    [0]=>
591    int(%d)
592    [1]=>
593    string(1) "
594"
595    [2]=>
596    int(5)
597  }
598  [22]=>
599  array(3) {
600    [0]=>
601    int(%d)
602    [1]=>
603    string(8) "continue"
604    [2]=>
605    int(6)
606  }
607  [23]=>
608  string(1) ";"
609  [24]=>
610  array(3) {
611    [0]=>
612    int(%d)
613    [1]=>
614    string(1) "
615"
616    [2]=>
617    int(6)
618  }
619  [25]=>
620  string(1) "}"
621  [26]=>
622  array(3) {
623    [0]=>
624    int(%d)
625    [1]=>
626    string(5) "while"
627    [2]=>
628    int(7)
629  }
630  [27]=>
631  string(1) "("
632  [28]=>
633  array(3) {
634    [0]=>
635    int(%d)
636    [1]=>
637    string(5) "false"
638    [2]=>
639    int(7)
640  }
641  [29]=>
642  string(1) ")"
643  [30]=>
644  string(1) ";"
645  [31]=>
646  array(3) {
647    [0]=>
648    int(%d)
649    [1]=>
650    string(1) " "
651    [2]=>
652    int(7)
653  }
654  [32]=>
655  array(3) {
656    [0]=>
657    int(%d)
658    [1]=>
659    string(2) "?>"
660    [2]=>
661    int(7)
662  }
663}
664-- with for..foreach( as ) tokens --
665array(45) {
666  [0]=>
667  array(3) {
668    [0]=>
669    int(%d)
670    [1]=>
671    string(6) "<?php "
672    [2]=>
673    int(1)
674  }
675  [1]=>
676  array(3) {
677    [0]=>
678    int(%d)
679    [1]=>
680    string(3) "for"
681    [2]=>
682    int(1)
683  }
684  [2]=>
685  string(1) "("
686  [3]=>
687  array(3) {
688    [0]=>
689    int(%d)
690    [1]=>
691    string(6) "$count"
692    [2]=>
693    int(1)
694  }
695  [4]=>
696  string(1) "="
697  [5]=>
698  array(3) {
699    [0]=>
700    int(%d)
701    [1]=>
702    string(1) "0"
703    [2]=>
704    int(1)
705  }
706  [6]=>
707  string(1) ";"
708  [7]=>
709  array(3) {
710    [0]=>
711    int(%d)
712    [1]=>
713    string(1) " "
714    [2]=>
715    int(1)
716  }
717  [8]=>
718  array(3) {
719    [0]=>
720    int(%d)
721    [1]=>
722    string(6) "$count"
723    [2]=>
724    int(1)
725  }
726  [9]=>
727  array(3) {
728    [0]=>
729    int(%d)
730    [1]=>
731    string(1) " "
732    [2]=>
733    int(1)
734  }
735  [10]=>
736  string(1) "<"
737  [11]=>
738  array(3) {
739    [0]=>
740    int(%d)
741    [1]=>
742    string(1) " "
743    [2]=>
744    int(1)
745  }
746  [12]=>
747  array(3) {
748    [0]=>
749    int(%d)
750    [1]=>
751    string(1) "5"
752    [2]=>
753    int(1)
754  }
755  [13]=>
756  string(1) ";"
757  [14]=>
758  array(3) {
759    [0]=>
760    int(%d)
761    [1]=>
762    string(1) " "
763    [2]=>
764    int(1)
765  }
766  [15]=>
767  array(3) {
768    [0]=>
769    int(%d)
770    [1]=>
771    string(6) "$count"
772    [2]=>
773    int(1)
774  }
775  [16]=>
776  array(3) {
777    [0]=>
778    int(%d)
779    [1]=>
780    string(2) "++"
781    [2]=>
782    int(1)
783  }
784  [17]=>
785  string(1) ")"
786  [18]=>
787  array(3) {
788    [0]=>
789    int(%d)
790    [1]=>
791    string(1) " "
792    [2]=>
793    int(1)
794  }
795  [19]=>
796  string(1) "{"
797  [20]=>
798  array(3) {
799    [0]=>
800    int(%d)
801    [1]=>
802    string(1) "
803"
804    [2]=>
805    int(1)
806  }
807  [21]=>
808  array(3) {
809    [0]=>
810    int(%d)
811    [1]=>
812    string(4) "echo"
813    [2]=>
814    int(2)
815  }
816  [22]=>
817  array(3) {
818    [0]=>
819    int(%d)
820    [1]=>
821    string(1) " "
822    [2]=>
823    int(2)
824  }
825  [23]=>
826  array(3) {
827    [0]=>
828    int(%d)
829    [1]=>
830    string(6) "$count"
831    [2]=>
832    int(2)
833  }
834  [24]=>
835  string(1) ";"
836  [25]=>
837  array(3) {
838    [0]=>
839    int(%d)
840    [1]=>
841    string(1) "
842"
843    [2]=>
844    int(2)
845  }
846  [26]=>
847  string(1) "}"
848  [27]=>
849  array(3) {
850    [0]=>
851    int(%d)
852    [1]=>
853    string(1) "
854"
855    [2]=>
856    int(%d)
857  }
858  [28]=>
859  array(3) {
860    [0]=>
861    int(%d)
862    [1]=>
863    string(7) "foreach"
864    [2]=>
865    int(4)
866  }
867  [29]=>
868  string(1) "("
869  [30]=>
870  array(3) {
871    [0]=>
872    int(%d)
873    [1]=>
874    string(7) "$values"
875    [2]=>
876    int(4)
877  }
878  [31]=>
879  array(3) {
880    [0]=>
881    int(%d)
882    [1]=>
883    string(1) " "
884    [2]=>
885    int(4)
886  }
887  [32]=>
888  array(3) {
889    [0]=>
890    int(%d)
891    [1]=>
892    string(2) "as"
893    [2]=>
894    int(4)
895  }
896  [33]=>
897  array(3) {
898    [0]=>
899    int(%d)
900    [1]=>
901    string(1) " "
902    [2]=>
903    int(4)
904  }
905  [34]=>
906  array(3) {
907    [0]=>
908    int(%d)
909    [1]=>
910    string(6) "$index"
911    [2]=>
912    int(4)
913  }
914  [35]=>
915  string(1) ")"
916  [36]=>
917  array(3) {
918    [0]=>
919    int(%d)
920    [1]=>
921    string(1) " "
922    [2]=>
923    int(4)
924  }
925  [37]=>
926  string(1) "{"
927  [38]=>
928  array(3) {
929    [0]=>
930    int(%d)
931    [1]=>
932    string(1) "
933"
934    [2]=>
935    int(4)
936  }
937  [39]=>
938  array(3) {
939    [0]=>
940    int(%d)
941    [1]=>
942    string(8) "continue"
943    [2]=>
944    int(5)
945  }
946  [40]=>
947  string(1) ";"
948  [41]=>
949  array(3) {
950    [0]=>
951    int(%d)
952    [1]=>
953    string(1) "
954"
955    [2]=>
956    int(5)
957  }
958  [42]=>
959  string(1) "}"
960  [43]=>
961  array(3) {
962    [0]=>
963    int(%d)
964    [1]=>
965    string(1) " "
966    [2]=>
967    int(6)
968  }
969  [44]=>
970  array(3) {
971    [0]=>
972    int(%d)
973    [1]=>
974    string(2) "?>"
975    [2]=>
976    int(6)
977  }
978}
979-- with switch...case tokens --
980array(23) {
981  [0]=>
982  array(3) {
983    [0]=>
984    int(%d)
985    [1]=>
986    string(6) "<?php "
987    [2]=>
988    int(1)
989  }
990  [1]=>
991  array(3) {
992    [0]=>
993    int(%d)
994    [1]=>
995    string(6) "switch"
996    [2]=>
997    int(1)
998  }
999  [2]=>
1000  string(1) "("
1001  [3]=>
1002  array(3) {
1003    [0]=>
1004    int(%d)
1005    [1]=>
1006    string(4) "$var"
1007    [2]=>
1008    int(1)
1009  }
1010  [4]=>
1011  string(1) ")"
1012  [5]=>
1013  array(3) {
1014    [0]=>
1015    int(%d)
1016    [1]=>
1017    string(1) "
1018"
1019    [2]=>
1020    int(1)
1021  }
1022  [6]=>
1023  array(3) {
1024    [0]=>
1025    int(%d)
1026    [1]=>
1027    string(4) "case"
1028    [2]=>
1029    int(2)
1030  }
1031  [7]=>
1032  array(3) {
1033    [0]=>
1034    int(%d)
1035    [1]=>
1036    string(1) " "
1037    [2]=>
1038    int(2)
1039  }
1040  [8]=>
1041  array(3) {
1042    [0]=>
1043    int(%d)
1044    [1]=>
1045    string(1) "1"
1046    [2]=>
1047    int(2)
1048  }
1049  [9]=>
1050  string(1) ":"
1051  [10]=>
1052  array(3) {
1053    [0]=>
1054    int(%d)
1055    [1]=>
1056    string(1) " "
1057    [2]=>
1058    int(2)
1059  }
1060  [11]=>
1061  array(3) {
1062    [0]=>
1063    int(%d)
1064    [1]=>
1065    string(5) "break"
1066    [2]=>
1067    int(2)
1068  }
1069  [12]=>
1070  string(1) ";"
1071  [13]=>
1072  array(3) {
1073    [0]=>
1074    int(%d)
1075    [1]=>
1076    string(1) "
1077"
1078    [2]=>
1079    int(2)
1080  }
1081  [14]=>
1082  array(3) {
1083    [0]=>
1084    int(%d)
1085    [1]=>
1086    string(7) "default"
1087    [2]=>
1088    int(%d)
1089  }
1090  [15]=>
1091  string(1) ":"
1092  [16]=>
1093  array(3) {
1094    [0]=>
1095    int(%d)
1096    [1]=>
1097    string(1) " "
1098    [2]=>
1099    int(%d)
1100  }
1101  [17]=>
1102  array(3) {
1103    [0]=>
1104    int(%d)
1105    [1]=>
1106    string(4) "echo"
1107    [2]=>
1108    int(%d)
1109  }
1110  [18]=>
1111  array(3) {
1112    [0]=>
1113    int(%d)
1114    [1]=>
1115    string(1) " "
1116    [2]=>
1117    int(%d)
1118  }
1119  [19]=>
1120  array(3) {
1121    [0]=>
1122    int(%d)
1123    [1]=>
1124    string(9) ""default""
1125    [2]=>
1126    int(%d)
1127  }
1128  [20]=>
1129  string(1) ";"
1130  [21]=>
1131  array(3) {
1132    [0]=>
1133    int(%d)
1134    [1]=>
1135    string(1) " "
1136    [2]=>
1137    int(%d)
1138  }
1139  [22]=>
1140  array(3) {
1141    [0]=>
1142    int(%d)
1143    [1]=>
1144    string(2) "?>"
1145    [2]=>
1146    int(%d)
1147  }
1148}
1149Done
1150