| 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 |
1
137
137
137
137
137
137
137
137
137
137
137
137
1
141
141
141
141
141
141
141
141
141
141
141
136
5
141
1
11940
11940
11940
11940
11940
20
20
11940
414
414
414
11526
11940
1
2931
2819
2819
2819
16
16
16
2803
2819
112
101
101
101
101
101
101
281
281
5
5
5
5
5
5
5
276
276
101
2931
1
47
1
10
1
71
71
71
1
89
365
365
365
365
365
4
4
4
365
11
11
11
354
89
1
18
1
14
14
14
14
14
14
1
72
72
1
2961
2961
2961
2961
2961
2954
1411
2954
2
2959
156
156
156
2959
1
312
312
312
312
312
1
133
133
133
133
133
133
1
4372
4372
1
4372
4372
4372
4372
4372
143
143
143
143
4229
86
86
14
72
86
4143
4229
137
4229
4229
1
8
34
1
| /*!
* Copyright (C) 2017 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
/**
* This is the php lexer. It will tokenize the string for helping the
* parser to build the AST from its grammar.
*
* @public @constructor {Lexer}
* @property {Integer} EOF
* @property {Boolean} all_tokens defines if all tokens must be retrieved (used by token_get_all only)
* @property {Boolean} comment_tokens extracts comments tokens
* @property {Boolean} mode_eval enables the evald mode (ignore opening tags)
* @property {Boolean} asp_tags disables by default asp tags mode
* @property {Boolean} short_tags enables by default short tags mode
* @property {Object} keywords List of php keyword
* @property {Object} castKeywords List of php keywords for type casting
*/
var lexer = function(engine) {
this.engine = engine;
this.tok = this.engine.tokens.names;
this.EOF = 1;
this.debug = false;
this.all_tokens = true;
this.comment_tokens = false;
this.mode_eval = false;
this.asp_tags = false;
this.short_tags = true;
this.yyprevcol = 0;
this.keywords = {
"__class__": this.tok.T_CLASS_C,
"__trait__": this.tok.T_TRAIT_C,
"__function__": this.tok.T_FUNC_C,
"__method__": this.tok.T_METHOD_C,
"__line__": this.tok.T_LINE,
"__file__": this.tok.T_FILE,
"__dir__": this.tok.T_DIR,
"__namespace__": this.tok.T_NS_C,
'exit': this.tok.T_EXIT,
'die': this.tok.T_EXIT,
'function': this.tok.T_FUNCTION,
"const": this.tok.T_CONST,
"return": this.tok.T_RETURN,
"try": this.tok.T_TRY,
"catch": this.tok.T_CATCH,
"finally": this.tok.T_FINALLY,
"throw": this.tok.T_THROW,
"if": this.tok.T_IF,
"elseif": this.tok.T_ELSEIF,
"endif": this.tok.T_ENDIF,
"else": this.tok.T_ELSE,
"while": this.tok.T_WHILE,
"endwhile": this.tok.T_ENDWHILE,
"do": this.tok.T_DO,
"for": this.tok.T_FOR,
"endfor": this.tok.T_ENDFOR,
"foreach": this.tok.T_FOREACH,
"endforeach": this.tok.T_ENDFOREACH,
"declare": this.tok.T_DECLARE,
"enddeclare": this.tok.T_ENDDECLARE,
"instanceof": this.tok.T_INSTANCEOF,
"as": this.tok.T_AS,
"switch": this.tok.T_SWITCH,
"endswitch": this.tok.T_ENDSWITCH,
"case": this.tok.T_CASE,
"default": this.tok.T_DEFAULT,
"break": this.tok.T_BREAK,
"continue": this.tok.T_CONTINUE,
"goto": this.tok.T_GOTO,
"echo": this.tok.T_ECHO,
"print": this.tok.T_PRINT,
"class": this.tok.T_CLASS,
"interface": this.tok.T_INTERFACE,
"trait": this.tok.T_TRAIT,
"extends": this.tok.T_EXTENDS,
"implements": this.tok.T_IMPLEMENTS,
"new": this.tok.T_NEW,
"clone": this.tok.T_CLONE,
"var": this.tok.T_VAR,
"eval": this.tok.T_EVAL,
"include": this.tok.T_INCLUDE,
"include_once": this.tok.T_INCLUDE_ONCE,
"require": this.tok.T_REQUIRE,
"require_once": this.tok.T_REQUIRE_ONCE,
"namespace": this.tok.T_NAMESPACE,
"use": this.tok.T_USE,
"insteadof": this.tok.T_INSTEADOF,
"global": this.tok.T_GLOBAL,
"isset": this.tok.T_ISSET,
"empty": this.tok.T_EMPTY,
"__halt_compiler": this.tok.T_HALT_COMPILER,
"static": this.tok.T_STATIC,
"abstract": this.tok.T_ABSTRACT,
"final": this.tok.T_FINAL,
"private": this.tok.T_PRIVATE,
"protected": this.tok.T_PROTECTED,
"public": this.tok.T_PUBLIC,
"unset": this.tok.T_UNSET,
"list": this.tok.T_LIST,
"array": this.tok.T_ARRAY,
"callable": this.tok.T_CALLABLE,
"or": this.tok.T_LOGICAL_OR,
"and": this.tok.T_LOGICAL_AND,
"xor": this.tok.T_LOGICAL_XOR
};
this.castKeywords = {
'int': this.tok.T_INT_CAST,
'integer': this.tok.T_INT_CAST,
"real": this.tok.T_DOUBLE_CAST,
"double": this.tok.T_DOUBLE_CAST,
"float": this.tok.T_DOUBLE_CAST,
"string": this.tok.T_STRING_CAST,
"binary": this.tok.T_STRING_CAST,
"array": this.tok.T_ARRAY_CAST,
"object": this.tok.T_OBJECT_CAST,
"bool": this.tok.T_BOOL_CAST,
"boolean": this.tok.T_BOOL_CAST,
"unset": this.tok.T_UNSET_CAST
};
};
/**
* Initialize the lexer with the specified input
*/
lexer.prototype.setInput = function(input) {
this._input = input;
this.size = input.length;
this.yylineno = 1;
this.offset = 0;
this.yyprevcol = 0;
this.yytext = '';
this.yylloc = {
first_offset: 0,
first_line: 1,
first_column: 0,
prev_offset: 0,
prev_line: 1,
prev_column: 0,
last_line: 1,
last_column: 0
};
this.tokens = [];
this.conditionStack = [];
this.done = this.offset >= this.size;
if (!this.all_tokens && this.mode_eval) {
this.begin('ST_IN_SCRIPTING');
} else {
this.begin('INITIAL');
}
return this;
};
/**
* consumes and returns one char from the input
*/
lexer.prototype.input = function(size) {
var ch = this._input[this.offset];
Iif (!ch) return '';
this.yytext += ch;
this.offset ++;
if ( ch === '\r' && this._input[this.offset] === '\n' ) {
this.yytext += '\n';
this.offset++;
}
if (ch === '\n' || ch === '\r') {
this.yylloc.last_line = ++this.yylineno;
this.yyprevcol = this.yylloc.last_column;
this.yylloc.last_column = 0;
} else {
this.yylloc.last_column++;
}
return ch;
};
/**
* revert eating specified size
*/
lexer.prototype.unput = function(size) {
if (size === 1) {
// 1 char unput (most cases)
this.offset --;
Iif (this._input[this.offset] === '\n' && this._input[this.offset - 1] === '\r') {
this.offset --;
size ++;
}
if (this._input[this.offset] === '\r' || this._input[this.offset] === '\n') {
this.yylloc.last_line --;
this.yylineno --;
this.yylloc.last_column = this.yyprevcol;
} else {
this.yylloc.last_column --;
}
this.yytext = this.yytext.substring(0, this.yytext.length - size);
} else if (size > 0) {
this.offset -= size;
Eif (size < this.yytext.length) {
this.yytext = this.yytext.substring(0, this.yytext.length - size);
// re-calculate position
this.yylloc.last_line = this.yylloc.first_line;
this.yylloc.last_column = this.yyprevcol = this.yylloc.first_column;
for(var i = 0; i < this.yytext.length; i++) {
var c = this.yytext[i];
if (c === '\r') {
c = this.yytext[++i];
this.yyprevcol = this.yylloc.last_column;
this.yylloc.last_line ++;
this.yylloc.last_column = 0;
Eif (c !== '\n') {
Iif (c === '\r') {
this.yylloc.last_line ++;
} else {
this.yylloc.last_column ++;
}
}
} else Iif (c === '\n') {
this.yyprevcol = this.yylloc.last_column;
this.yylloc.last_line ++;
this.yylloc.last_column = 0;
} else {
this.yylloc.last_column ++;
}
}
this.yylineno = this.yylloc.last_line;
} else {
// reset full text
this.yytext = "";
this.yylloc.last_line = this.yylineno = this.yylloc.first_line;
this.yylloc.last_column = this.yylloc.first_column;
}
}
return this;
};
// check if the text matches
lexer.prototype.tryMatch = function(text) {
return text === this.ahead(text.length);
};
// check if the text matches
lexer.prototype.tryMatchCaseless = function(text) {
return text === this.ahead(text.length).toLowerCase();
};
// look ahead
lexer.prototype.ahead = function(size) {
var text = this._input.substring(this.offset, this.offset + size);
Iif (text[text.length - 1] === '\r' && this._input[this.offset + size + 1] === '\n') {
text += '\n';
}
return text;
};
// consume the specified size
lexer.prototype.consume = function(size) {
for(var i = 0; i < size; i++) {
var ch = this._input[this.offset];
Iif (!ch) break;
this.yytext += ch;
this.offset ++;
if ( ch === '\r' && this._input[this.offset] === '\n' ) {
this.yytext += '\n';
this.offset++;
i++;
}
if (ch === '\n' || ch === '\r') {
this.yylloc.last_line = ++this.yylineno;
this.yyprevcol = this.yylloc.last_column;
this.yylloc.last_column = 0;
} else {
this.yylloc.last_column++;
}
}
return this;
};
/**
* Gets the current state
*/
lexer.prototype.getState = function() {
return {
yytext: this.yytext,
offset: this.offset,
yylineno: this.yylineno,
yyprevcol: this.yyprevcol,
yylloc: {
first_offset: this.yylloc.first_offset,
first_line: this.yylloc.first_line,
first_column: this.yylloc.first_column,
last_line: this.yylloc.last_line,
last_column: this.yylloc.last_column
}
};
};
/**
* Sets the current lexer state
*/
lexer.prototype.setState = function(state) {
this.yytext = state.yytext;
this.offset = state.offset;
this.yylineno = state.yylineno;
this.yyprevcol = state.yyprevcol;
this.yylloc = state.yylloc;
return this;
};
// prepend next token
lexer.prototype.appendToken = function(value, ahead) {
this.tokens.push([value, ahead]);
return this;
};
// return next match that has a token
lexer.prototype.lex = function() {
this.yylloc.prev_offset = this.offset;
this.yylloc.prev_line = this.yylloc.last_line;
this.yylloc.prev_column = this.yylloc.last_column;
var token = this.next() || this.lex();
if (!this.all_tokens) {
while(
token === this.tok.T_WHITESPACE // ignore white space
|| (
!this.comment_tokens && (
token === this.tok.T_COMMENT // ignore single lines comments
|| token === this.tok.T_DOC_COMMENT // ignore doc comments
)
)
|| (
// ignore open tags
token === this.tok.T_OPEN_TAG
)
) {
token = this.next() || this.lex();
}
if (!this.mode_eval && token == this.tok.T_OPEN_TAG_WITH_ECHO) {
// open tag with echo statement
return this.tok.T_ECHO;
}
}
if (!this.yylloc.prev_offset) {
this.yylloc.prev_offset = this.yylloc.first_offset;
this.yylloc.prev_line = this.yylloc.first_line;
this.yylloc.prev_column = this.yylloc.first_column;
}
/*else if (this.yylloc.prev_offset === this.offset && this.offset !== this.size) {
throw new Error('Infinite loop @ ' + this.offset + ' / ' + this.size);
}*/
return token;
};
// activates a new lexer condition state (pushes the new lexer condition state onto the condition stack)
lexer.prototype.begin = function(condition) {
this.conditionStack.push(condition);
this.curCondition = condition;
this.stateCb = this['match' + condition];
Iif (typeof this.stateCb !== 'function') {
throw new Error('Undefined condition state "'+condition+'"');
}
return this;
};
// pop the previously active lexer condition state off the condition stack
lexer.prototype.popState = function() {
var n = this.conditionStack.length - 1;
var condition = (n > 0) ? this.conditionStack.pop() : this.conditionStack[0];
this.curCondition = this.conditionStack[this.conditionStack.length - 1];
this.stateCb = this['match' + this.curCondition];
Iif (typeof this.stateCb !== 'function') {
throw new Error('Undefined condition state "'+this.curCondition+'"');
}
return condition;
};
// return next match in input
lexer.prototype.next = function () {
var token;
if (!this._input) {
this.done = true;
}
this.yylloc.first_offset = this.offset;
this.yylloc.first_line = this.yylloc.last_line;
this.yylloc.first_column = this.yylloc.last_column;
this.yytext = '';
if (this.done) {
this.yylloc.prev_offset = this.yylloc.first_offset;
this.yylloc.prev_line = this.yylloc.first_line;
this.yylloc.prev_column = this.yylloc.first_column;
return this.EOF;
}
if (this.tokens.length > 0) {
token = this.tokens.shift();
if (typeof token[1] === 'object') {
this.setState(token[1]);
} else {
this.consume(token[1]);
}
token = token[0];
} else {
token = this.stateCb.apply(this, []);
}
if (this.offset >= this.size && this.tokens.length === 0) {
this.done = true;
}
Iif (this.debug) {
var tName = token;
if (typeof tName === 'number') {
tName = this.engine.tokens.values[tName];
} else {
tName = '"'+tName+'"';
}
var e = new Error(
tName +
'\tfrom ' + this.yylloc.first_line + ',' + this.yylloc.first_column +
'\t - to ' + this.yylloc.last_line + ',' + this.yylloc.last_column +
'\t"'+this.yytext+'"'
);
console.log(e.stack);
}
return token;
};
// extends the lexer with states
[
require('./lexer/comments.js'),
require('./lexer/initial.js'),
require('./lexer/numbers.js'),
require('./lexer/property.js'),
require('./lexer/scripting.js'),
require('./lexer/strings.js'),
require('./lexer/tokens.js'),
require('./lexer/utils.js')
].forEach(function (ext) {
for(var k in ext) {
lexer.prototype[k] = ext[k];
}
});
module.exports = lexer;
|