Skip to content

Commit adb7f39

Browse files
committed
fix columns implementation
1 parent 0206c33 commit adb7f39

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/lexer.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
44
* @url http://glayzzle.com
55
*/
6-
6+
"use strict";
77
/**
88
* This is the php lexer. It will tokenize the string for helping the
99
* parser to build the AST from its grammar.
@@ -22,6 +22,7 @@ var lexer = function(engine) {
2222
this.engine = engine;
2323
this.tok = this.engine.tokens.names;
2424
this.EOF = 1;
25+
this.debug = false;
2526
this.all_tokens = true;
2627
this.comment_tokens = false;
2728
this.mode_eval = false;
@@ -134,6 +135,9 @@ lexer.prototype.setInput = function(input) {
134135
first_offset: 0,
135136
first_line: 1,
136137
first_column: 0,
138+
prev_offset: 0,
139+
prev_line: 1,
140+
prev_column: 0,
137141
last_line: 1,
138142
last_column: 0
139143
};
@@ -186,6 +190,8 @@ lexer.prototype.unput = function(size) {
186190
this.yylloc.last_line --;
187191
this.yylineno --;
188192
this.yylloc.last_column = this.yyprevcol;
193+
} else {
194+
this.yylloc.last_column --;
189195
}
190196
this.yytext = this.yytext.substring(0, this.yytext.length - size);
191197
} else if (size > 0) {
@@ -194,27 +200,27 @@ lexer.prototype.unput = function(size) {
194200
this.yytext = this.yytext.substring(0, this.yytext.length - size);
195201
// re-calculate position
196202
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;
198204
for(var i = 0; i < this.yytext.length; i++) {
199205
var c = this.yytext[i];
200206
if (c === '\r') {
201207
c = this.yytext[++i];
202-
this.yyprevcol = this.yylloc.last_col;
208+
this.yyprevcol = this.yylloc.last_column;
203209
this.yylloc.last_line ++;
204-
this.yylloc.last_col = 0;
210+
this.yylloc.last_column = 0;
205211
if (c !== '\n') {
206212
if (c === '\r') {
207213
this.yylloc.last_line ++;
208214
} else {
209-
this.yylloc.last_col ++;
215+
this.yylloc.last_column ++;
210216
}
211217
}
212218
} else if (c === '\n') {
213-
this.yyprevcol = this.yylloc.last_col;
219+
this.yyprevcol = this.yylloc.last_column;
214220
this.yylloc.last_line ++;
215-
this.yylloc.last_col = 0;
221+
this.yylloc.last_column = 0;
216222
} else {
217-
this.yylloc.last_col ++;
223+
this.yylloc.last_column ++;
218224
}
219225
}
220226
this.yylineno = this.yylloc.last_line;
@@ -310,6 +316,9 @@ lexer.prototype.appendToken = function(value, ahead) {
310316

311317
// return next match that has a token
312318
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;
313322
var token = this.next() || this.lex();
314323
if (!this.all_tokens) {
315324
while(
@@ -332,6 +341,11 @@ lexer.prototype.lex = function() {
332341
return this.tok.T_ECHO;
333342
}
334343
}
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+
}
335349
return token;
336350
};
337351

@@ -364,13 +378,16 @@ lexer.prototype.next = function () {
364378
if (!this._input) {
365379
this.done = true;
366380
}
367-
if (this.done) {
368-
return this.EOF;
369-
}
370381
this.yylloc.first_offset = this.offset;
371382
this.yylloc.first_line = this.yylloc.last_line;
372383
this.yylloc.first_column = this.yylloc.last_column;
373384
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+
}
374391
if (this.tokens.length > 0) {
375392
token = this.tokens.shift();
376393
if (typeof token[1] === 'object') {
@@ -385,6 +402,19 @@ lexer.prototype.next = function () {
385402
if (this.offset >= this.size && this.tokens.length === 0) {
386403
this.done = true;
387404
}
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+
}
388418
return token;
389419
};
390420

0 commit comments

Comments
 (0)