3
3
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
4
4
* @url http://glayzzle.com
5
5
*/
6
-
6
+ "use strict" ;
7
7
/**
8
8
* This is the php lexer. It will tokenize the string for helping the
9
9
* parser to build the AST from its grammar.
@@ -22,6 +22,7 @@ var lexer = function(engine) {
22
22
this . engine = engine ;
23
23
this . tok = this . engine . tokens . names ;
24
24
this . EOF = 1 ;
25
+ this . debug = false ;
25
26
this . all_tokens = true ;
26
27
this . comment_tokens = false ;
27
28
this . mode_eval = false ;
@@ -134,6 +135,9 @@ lexer.prototype.setInput = function(input) {
134
135
first_offset : 0 ,
135
136
first_line : 1 ,
136
137
first_column : 0 ,
138
+ prev_offset : 0 ,
139
+ prev_line : 1 ,
140
+ prev_column : 0 ,
137
141
last_line : 1 ,
138
142
last_column : 0
139
143
} ;
@@ -186,6 +190,8 @@ lexer.prototype.unput = function(size) {
186
190
this . yylloc . last_line -- ;
187
191
this . yylineno -- ;
188
192
this . yylloc . last_column = this . yyprevcol ;
193
+ } else {
194
+ this . yylloc . last_column -- ;
189
195
}
190
196
this . yytext = this . yytext . substring ( 0 , this . yytext . length - size ) ;
191
197
} else if ( size > 0 ) {
@@ -194,27 +200,27 @@ lexer.prototype.unput = function(size) {
194
200
this . yytext = this . yytext . substring ( 0 , this . yytext . length - size ) ;
195
201
// re-calculate position
196
202
this . yylloc . last_line = this . yylloc . first_line ;
197
- this . yylloc . last_col = this . yyprevcol = this . yylloc . first_col ;
203
+ this . yylloc . last_column = this . yyprevcol = this . yylloc . first_column ;
198
204
for ( var i = 0 ; i < this . yytext . length ; i ++ ) {
199
205
var c = this . yytext [ i ] ;
200
206
if ( c === '\r' ) {
201
207
c = this . yytext [ ++ i ] ;
202
- this . yyprevcol = this . yylloc . last_col ;
208
+ this . yyprevcol = this . yylloc . last_column ;
203
209
this . yylloc . last_line ++ ;
204
- this . yylloc . last_col = 0 ;
210
+ this . yylloc . last_column = 0 ;
205
211
if ( c !== '\n' ) {
206
212
if ( c === '\r' ) {
207
213
this . yylloc . last_line ++ ;
208
214
} else {
209
- this . yylloc . last_col ++ ;
215
+ this . yylloc . last_column ++ ;
210
216
}
211
217
}
212
218
} else if ( c === '\n' ) {
213
- this . yyprevcol = this . yylloc . last_col ;
219
+ this . yyprevcol = this . yylloc . last_column ;
214
220
this . yylloc . last_line ++ ;
215
- this . yylloc . last_col = 0 ;
221
+ this . yylloc . last_column = 0 ;
216
222
} else {
217
- this . yylloc . last_col ++ ;
223
+ this . yylloc . last_column ++ ;
218
224
}
219
225
}
220
226
this . yylineno = this . yylloc . last_line ;
@@ -310,6 +316,9 @@ lexer.prototype.appendToken = function(value, ahead) {
310
316
311
317
// return next match that has a token
312
318
lexer . prototype . lex = function ( ) {
319
+ this . yylloc . prev_offset = this . offset ;
320
+ this . yylloc . prev_line = this . yylloc . last_line ;
321
+ this . yylloc . prev_column = this . yylloc . last_column ;
313
322
var token = this . next ( ) || this . lex ( ) ;
314
323
if ( ! this . all_tokens ) {
315
324
while (
@@ -332,6 +341,11 @@ lexer.prototype.lex = function() {
332
341
return this . tok . T_ECHO ;
333
342
}
334
343
}
344
+ if ( ! this . yylloc . prev_offset ) {
345
+ this . yylloc . prev_offset = this . yylloc . first_offset ;
346
+ this . yylloc . prev_line = this . yylloc . first_line ;
347
+ this . yylloc . prev_column = this . yylloc . first_column ;
348
+ }
335
349
return token ;
336
350
} ;
337
351
@@ -364,13 +378,16 @@ lexer.prototype.next = function () {
364
378
if ( ! this . _input ) {
365
379
this . done = true ;
366
380
}
367
- if ( this . done ) {
368
- return this . EOF ;
369
- }
370
381
this . yylloc . first_offset = this . offset ;
371
382
this . yylloc . first_line = this . yylloc . last_line ;
372
383
this . yylloc . first_column = this . yylloc . last_column ;
373
384
this . yytext = '' ;
385
+ if ( this . done ) {
386
+ this . yylloc . prev_offset = this . yylloc . first_offset ;
387
+ this . yylloc . prev_line = this . yylloc . first_line ;
388
+ this . yylloc . prev_column = this . yylloc . first_column ;
389
+ return this . EOF ;
390
+ }
374
391
if ( this . tokens . length > 0 ) {
375
392
token = this . tokens . shift ( ) ;
376
393
if ( typeof token [ 1 ] === 'object' ) {
@@ -385,6 +402,19 @@ lexer.prototype.next = function () {
385
402
if ( this . offset >= this . size && this . tokens . length === 0 ) {
386
403
this . done = true ;
387
404
}
405
+ if ( this . debug ) {
406
+ var tName = token ;
407
+ if ( typeof tName === 'number' ) {
408
+ tName = this . engine . tokens . values [ tName ] ;
409
+ } else {
410
+ tName = '"' + tName + '"' ;
411
+ }
412
+ console . log (
413
+ tName ,
414
+ 'from ' + this . yylloc . first_line + ',' + this . yylloc . first_column ,
415
+ ' - to ' + this . yylloc . last_line + ',' + this . yylloc . last_column
416
+ ) ;
417
+ }
388
418
return token ;
389
419
} ;
390
420
0 commit comments