| 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 |
1
1
121
15
391
2
389
64
64
64
64
64
64
64
64
7
3
3
3
3
1
1
2
3
3
3
3
4
26
2
2
2
270
270
270
270
270
20
2
2
2
2
2
2
2
104
104
57
57
57
47
8
8
3
3
3
3
3
1
1
1
1
5
8
8
39
13
13
26
26
26
5
5
5
5
26
3
3
3
3
3
3
104
45
45
13
32
28
4
45
104
45
45
45
4
45
2
2
2
2
| /*!
* Copyright (C) 2017 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
var specialChar = {
'\\r': "\r",
'\\n': "\n",
'\\t': "\t",
'\\v': String.fromCharCode(11),
'\\e': String.fromCharCode(27),
'\\f': String.fromCharCode(12),
"\\\\": "\\",
'\\$': "$",
'\\"': '"',
'\\\'': "'"
};
module.exports = {
/**
* Unescape special chars
*/
resolve_special_chars: function(text) {
return text.replace(
/\\[rntvef"'\\\$]/g,
function(seq) {
return specialChar[seq];
}
);
},
/**
* ```ebnf
* scalar ::= T_MAGIC_CONST
* | T_LNUMBER | T_DNUMBER
* | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE? T_END_HEREDOC
* | '"' encaps_list '"'
* | T_START_HEREDOC encaps_list T_END_HEREDOC
* | namespace_name (T_DOUBLE_COLON T_STRING)?
* ```
*/
read_scalar: function() {
if (this.is('T_MAGIC_CONST')) {
return this.get_magic_constant();
} else {
switch(this.token) {
// TEXTS
case this.tok.T_CONSTANT_ENCAPSED_STRING:
var value = this.node('string');
var text = this.text();
var isDoubleQuote = text[0] === '"';
text = text.substring(1, text.length - 1);
this.next();
value = value(isDoubleQuote, this.resolve_special_chars(text));
Iif (this.token === this.tok.T_DOUBLE_COLON) {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1151
return this.read_static_getter(value);
} else {
// dirrect string
return value;
}
case this.tok.T_START_HEREDOC:
if (this.lexer.curCondition === 'ST_NOWDOC') {
var node = this.node('nowdoc');
var value = this.next().text();
// strip the last line return char
var lastCh = value[value.length-1];
if (lastCh === '\n') {
Eif (value[value.length-2] === '\r') {
// windows style
value = value.substring(0, value.length - 2);
} else {
// linux style
value = value.substring(0, value.length - 1);
}
} else Iif (lastCh === '\r') {
// mac style
value = value.substring(0, value.length - 1);
}
this.expect(this.tok.T_ENCAPSED_AND_WHITESPACE) && this.next();
node = node(value, this.lexer.heredoc_label);
this.expect(this.tok.T_END_HEREDOC) && this.next();
return node;
} else {
return this.next().read_encapsed_string(
this.tok.T_END_HEREDOC
);
}
case '"':
return this.next().read_encapsed_string('"');
case 'b"':
case 'B"':
var node = this.node('cast');
var what = this.next().read_encapsed_string('"');
return node('binary', what);
// NUMERIC
case this.tok.T_LNUMBER: // long
case this.tok.T_DNUMBER: // double
var result = this.node('number');
var value = this.text();
this.next();
result = result(value);
return result;
// ARRAYS
case this.tok.T_ARRAY: // array parser
case '[': // short array format
return this.read_array();
default:
var err = this.error('SCALAR');
// graceful mode : ignore token & return error node
this.next();
return err;
}
}
}
/**
* Handles the dereferencing
*/
,read_dereferencable: function(expr) {
var result;
var node = this.node('offsetlookup');
Eif (this.token === '[') {
var offset = this.next().read_expr();
Eif (this.expect(']')) this.next();
result = node(expr, offset);
} else if (this.token === this.tok.T_DOLLAR_OPEN_CURLY_BRACES) {
var offset = this.read_encapsed_string_item();
result = node(expr, offset);
}
return result;
}
/**
* Reads and extracts an encapsed item
* ```ebnf
* encapsed_string_item ::= T_ENCAPSED_AND_WHITESPACE
* | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
* | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'
* | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
* | T_CURLY_OPEN variable '}'
* | variable
* | variable '[' expr ']'
* | variable T_OBJECT_OPERATOR T_STRING
* ```
* @return {String|Variable|Expr|Lookup}
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1219
*/
,read_encapsed_string_item: function() {
var result = this.node();
// plain text
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1222
if (this.token === this.tok.T_ENCAPSED_AND_WHITESPACE) {
var text = this.text();
this.next();
result = result(
'string', false, this.resolve_special_chars(text)
);
}
// dynamic variable name
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1239
else if (this.token === this.tok.T_DOLLAR_OPEN_CURLY_BRACES) {
var name = null;
if (this.next().token === this.tok.T_STRING_VARNAME) {
var varName = this.text();
name = this.node('variable');
this.next();
name = name(varName, false);
// check if lookup an offset
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1243
if (this.token === '[') {
var node = this.node('offsetlookup');
var offset = this.next().read_expr();
this.expect(']') && this.next();
name = node(name, offset);
}
} else {
name = this.read_expr();
}
this.expect('}') && this.next();
result = result('variable', name, false);
}
// expression
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1246
else if (this.token === this.tok.T_CURLY_OPEN) {
result = this.next().read_variable(false, false, false);
this.expect('}') && this.next();
}
// plain variable
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1231
else Eif (this.token === this.tok.T_VARIABLE) {
result = this.read_simple_variable(false);
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1233
if (this.token === '[') {
var node = this.node('offsetlookup');
var offset = this.next().read_encaps_var_offset();
this.expect(']') && this.next();
result = node(result, offset);
}
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1236
if (this.token === this.tok.T_OBJECT_OPERATOR) {
var node = this.node('propertylookup');
var what = this.node('constref');
this.next().expect(this.tok.T_STRING);
var name = this.text();
this.next();
result = node(result, what(name));
}
// error / fallback
} else {
this.expect(this.tok.T_ENCAPSED_AND_WHITESPACE);
var value = this.text();
this.next();
// consider it as string
result = result('string', false, value);
}
return result;
}
/**
* Reads an encapsed string
*/
,read_encapsed_string: function(expect) {
var node = this.node('encapsed'), value = [], type = null;
if (expect === '`') {
type = this.ast.encapsed.TYPE_SHELL;
} else if (expect === '"') {
type = this.ast.encapsed.TYPE_STRING;
} else {
type = this.ast.encapsed.TYPE_HEREDOC;
}
// reading encapsed parts
while(this.token !== expect && this.token !== this.EOF) {
value.push(this.read_encapsed_string_item());
}
this.expect(expect) && this.next();
node = node(value, type);
if (expect === this.tok.T_END_HEREDOC) {
node.label = this.lexer.heredoc_label;
}
return node;
}
/**
* Constant token
*/
,get_magic_constant: function() {
var result = this.node('magic');
var name = this.text();
this.next();
return result(name);
}
};
|