| 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 |
1
2
2
2
2
2
1
1
1
2
1
1
1
1
1
1
1
1
2
2
2
1
1
1
2
1
1
1
2
1
1
1
2
1
1
1
2
2
2
2
2
2
2
2
1
1
2
2
1
1
1
2
3
1
1
1
1
1
2
1
1
| /*!
* Copyright (C) 2017 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
/**
* Reads a while statement
* ```ebnf
* while ::= T_WHILE (statement | ':' inner_statement_list T_ENDWHILE ';')
* ```
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L587
* @return {While}
*/
read_while: function() {
var result = this.node('while'),
test = null,
body = null,
shortForm = false
;
Eif (this.expect('(')) this.next();
test = this.read_expr();
Eif (this.expect(')')) this.next();
if (this.token === ':') {
shortForm = true;
body = this.read_short_form(this.tok.T_ENDWHILE);
} else {
body = this.read_statement();
}
return result(test, body, shortForm);
}
/**
* Reads a do / while loop
* ```ebnf
* do ::= T_DO statement T_WHILE '(' expr ')' ';'
* ```
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L423
* @return {Do}
*/
,read_do: function() {
var result = this.node('do'),
test = null,
body = null
;
body = this.read_statement();
Eif (this.ignoreComments().expect(this.tok.T_WHILE)) {
Eif (this.next().expect('(')) this.next();
test = this.read_expr();
Eif (this.expect(')')) this.next();
Eif (this.expect(';')) this.next();
}
return result(test, body);
}
/**
* Read a for incremental loop
* ```ebnf
* for ::= T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
* for_statement ::= statement | ':' inner_statement_list T_ENDFOR ';'
* for_exprs ::= expr? (',' expr)*
* ```
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L425
* @return {For}
*/
,read_for: function() {
var result = this.node('for'),
init = [],
test = [],
increment = [],
body = null,
shortForm = false;
Eif (this.expect('(')) this.next();
if (this.token !== ';') {
init = this.read_list(this.read_expr, ',');
Eif (this.expect(';')) this.next();
} else {
this.next();
}
if (this.token !== ';') {
test = this.read_list(this.read_expr, ',');
Eif (this.expect(';')) this.next();
} else {
this.next();
}
if (this.token !== ')') {
increment = this.read_list(this.read_expr, ',');
Eif (this.expect(')')) this.next();
} else {
this.next();
}
if (this.token === ':') {
shortForm = true;
body = this.read_short_form(this.tok.T_ENDFOR);
} else {
body = this.read_statement();
}
return result(init, test, increment, body, shortForm);
}
/**
* Reads a foreach loop
* ```ebnf
* foreach ::= '(' expr T_AS foreach_variable (T_DOUBLE_ARROW foreach_variable)? ')' statement
* ```
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L438
* @return {Foreach}
*/
,read_foreach: function() {
var result = this.node('foreach'),
source = null,
key = null,
value = null,
body = null,
shortForm = false;
Eif (this.expect('(')) this.next();
source = this.read_expr();
Eif (this.ignoreComments().expect(this.tok.T_AS)) {
this.next();
value = this.read_foreach_variable();
if (this.token === this.tok.T_DOUBLE_ARROW) {
key = value;
value = this.next().read_foreach_variable();
}
}
Eif (this.expect(')')) this.next();
if (this.token === ':') {
shortForm = true;
body = this.read_short_form(this.tok.T_ENDFOREACH);
} else {
body = this.read_statement();
}
return result(source, key, value, body, shortForm);
}
/**
* Reads a foreach variable statement
* ```ebnf
* foreach_variable = variable |
* T_LIST '(' assignment_list ')' |
* '[' array_pair_list ']'
* ```
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L544
* @return {Expression}
*/
,read_foreach_variable: function() {
if (this.token === this.tok.T_LIST) {
var result = this.node('list');
Eif (this.next().expect('(')) this.next();
var assignList = this.read_assignment_list();
Eif (this.expect(')')) this.next();
return result(assignList);
} else if (this.token === '[' || this.token === this.tok.T_ARRAY) {
return this.read_array();
} else {
return this.read_variable(false, false, false);
}
}
};
|