Skip to content

Commit 7bb0afb

Browse files
committed
glayzzle#141 - fix the location retrieval (ignoring comments & whitespace tokens)
1 parent ce9c657 commit 7bb0afb

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

src/ast.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,17 @@ AST.prototype.prepare = function(kind, docs, parser) {
250250
if (self.withSource) {
251251
src = parser.lexer._input.substring(
252252
start.offset,
253-
parser.lexer.yylloc.prev_offset
253+
parser.prev[2]
254254
);
255255
}
256256
if (self.withPositions) {
257257
location = new Location(
258258
src,
259259
start,
260260
new Position(
261-
parser.lexer.yylloc.prev_line,
262-
parser.lexer.yylloc.prev_column,
263-
parser.lexer.yylloc.prev_offset
261+
parser.prev[0],
262+
parser.prev[1],
263+
parser.prev[2]
264264
)
265265
);
266266
} else {

src/parser.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,14 @@ parser.prototype.text = function() {
432432

433433
/** consume the next token **/
434434
parser.prototype.next = function() {
435+
436+
// prepare the back command
437+
this.prev = [
438+
this.lexer.yylloc.last_line,
439+
this.lexer.yylloc.last_column,
440+
this.lexer.offset
441+
];
442+
435443
// eating the token
436444
this.lex();
437445

@@ -462,13 +470,6 @@ parser.prototype.next = function() {
462470
* Eating a token
463471
*/
464472
parser.prototype.lex = function() {
465-
// prepare the back command
466-
this.prev = [
467-
this.lexer.yylloc.first_line,
468-
this.lexer.yylloc.first_column,
469-
this.lexer.offset
470-
];
471-
472473
// append on token stack
473474
if (this.extractTokens) {
474475
do {

src/parser/if.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ module.exports = {
1313
* ```
1414
*/
1515
read_if: function() {
16-
const result = this.node("if");
16+
let result = this.node("if");
1717
let body = null;
1818
let alternate = null;
1919
let shortForm = false;
2020
let test = null;
21-
test = this.read_if_expr();
21+
test = this.next().read_if_expr();
2222

2323
if (this.token === ":") {
2424
shortForm = true;
@@ -27,10 +27,10 @@ module.exports = {
2727
const items = [];
2828
while (this.token !== this.EOF && this.token !== this.tok.T_ENDIF) {
2929
if (this.token === this.tok.T_ELSEIF) {
30-
alternate = this.next().read_elseif_short();
30+
alternate = this.read_elseif_short();
3131
break;
3232
} else if (this.token === this.tok.T_ELSE) {
33-
alternate = this.next().read_else_short();
33+
alternate = this.read_else_short();
3434
break;
3535
}
3636
items.push(this.read_inner_statement());
@@ -41,7 +41,7 @@ module.exports = {
4141
} else {
4242
body = this.read_statement();
4343
if (this.token === this.tok.T_ELSEIF) {
44-
alternate = this.next().read_if();
44+
alternate = this.read_if();
4545
} else if (this.token === this.tok.T_ELSE) {
4646
alternate = this.next().read_statement();
4747
}
@@ -66,15 +66,15 @@ module.exports = {
6666
let test = null;
6767
let body = null;
6868
const items = [];
69-
test = this.read_if_expr();
69+
test = this.next().read_if_expr();
7070
if (this.expect(":")) this.next();
7171
body = this.node("block");
7272
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
7373
if (this.token === this.tok.T_ELSEIF) {
74-
alternate = this.next().read_elseif_short();
74+
alternate = this.read_elseif_short();
7575
break;
7676
} else if (this.token === this.tok.T_ELSE) {
77-
alternate = this.next().read_else_short();
77+
alternate = this.read_else_short();
7878
break;
7979
}
8080
items.push(this.read_inner_statement());
@@ -86,8 +86,8 @@ module.exports = {
8686
*
8787
*/
8888
read_else_short: function() {
89-
if (this.expect(":")) this.next();
9089
const body = this.node("block");
90+
if (this.next().expect(":")) this.next();
9191
const items = [];
9292
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
9393
items.push(this.read_inner_statement());

src/parser/statement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ module.exports = {
196196
return this.read_code_block(false);
197197

198198
case this.tok.T_IF:
199-
return this.next().read_if();
199+
return this.read_if();
200200

201201
case this.tok.T_SWITCH:
202202
return this.read_switch();
@@ -394,7 +394,7 @@ module.exports = {
394394
* ```
395395
*/
396396
read_code_block: function(top) {
397-
const result = this.node("block");
397+
let result = this.node("block");
398398
this.expect("{") && this.next();
399399
const body = top
400400
? this.read_top_statements()

test/locationTests.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,19 @@ describe("Test offsets", function() {
3636
});
3737

3838
describe("to comment node", function() {
39+
console.log(ast.comments[0].loc);
3940
it("test line", function() {
4041
ast.comments[0].loc.start.line.should.be.exactly(1);
41-
ast.comments[0].loc.end.line.should.be.exactly(2);
42+
ast.comments[0].loc.end.line.should.be.exactly(1);
4243
});
4344
it("test column", function() {
4445
ast.comments[0].loc.start.column.should.be.exactly(0);
45-
ast.comments[0].loc.end.column.should.be.exactly(0);
46+
ast.comments[0].loc.end.column.should.be.exactly(12);
4647
});
4748
it("test offsets", function() {
4849
ast.comments[0].loc.start.offset.should.be.exactly(0);
49-
ast.comments[0].loc.end.offset.should.be.exactly(
50-
lines[0].length + 2 // first line + \r\n
51-
);
52-
ast.comments[0].loc.source.should.be.exactly(lines[0] + "\r\n");
50+
ast.comments[0].loc.end.offset.should.be.exactly(lines[0].length);
51+
ast.comments[0].loc.source.should.be.exactly(lines[0]);
5352
});
5453
});
5554

@@ -129,4 +128,14 @@ describe("Test offsets", function() {
129128
node.loc.source.should.be.exactly("true");
130129
});
131130
});
131+
132+
describe("test block statements", function() {
133+
it("test if", function() {
134+
const ast = test.parseEval('if(true) {}\n//foo\necho $bar;');
135+
ast.children[0].loc.start.line.should.be.exactly(1);
136+
ast.children[0].loc.end.line.should.be.exactly(1);
137+
ast.children[1].loc.start.line.should.be.exactly(3);
138+
ast.children[1].loc.end.line.should.be.exactly(3);
139+
});
140+
})
132141
});

0 commit comments

Comments
 (0)