Skip to content

Commit 7cb7da3

Browse files
committed
glayzzle#141 - fix doc node locations
1 parent 7bb0afb commit 7cb7da3

File tree

9 files changed

+95
-83
lines changed

9 files changed

+95
-83
lines changed

dist/php-parser.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! php-parser - BSD3 License - 2018-03-26 */
1+
/*! php-parser - BSD3 License - 2018-03-27 */
22

33
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
44
/*!
@@ -438,10 +438,10 @@ AST.prototype.prepare = function (kind, docs, parser) {
438438
if (self.withPositions || self.withSource) {
439439
var src = null;
440440
if (self.withSource) {
441-
src = parser.lexer._input.substring(start.offset, parser.lexer.yylloc.prev_offset);
441+
src = parser.lexer._input.substring(start.offset, parser.prev[2]);
442442
}
443443
if (self.withPositions) {
444-
location = new Location(src, start, new Position(parser.lexer.yylloc.prev_line, parser.lexer.yylloc.prev_column, parser.lexer.yylloc.prev_offset));
444+
location = new Location(src, start, new Position(parser.prev[0], parser.prev[1], parser.prev[2]));
445445
} else {
446446
location = new Location(src, null, null);
447447
}
@@ -2321,7 +2321,7 @@ var KIND = "program";
23212321
* @property {Doc[]?} comments
23222322
* @property {String[]?} tokens
23232323
*/
2324-
var Program = Block.extends(function Program(children, errors, comments, docs, tokens, location) {
2324+
var Program = Block.extends(function Program(children, errors, comments, tokens, docs, location) {
23252325
Block.apply(this, [KIND, children, docs, location]);
23262326
this.errors = errors;
23272327
if (comments) {
@@ -4767,7 +4767,7 @@ parser.prototype.parse = function (code, filename) {
47674767
}
47684768
}
47694769
}
4770-
return program(childs, this._errors, this._docs, null, this._tokens);
4770+
return program(childs, this._errors, this._docs, this._tokens);
47714771
};
47724772

47734773
/**
@@ -4902,6 +4902,9 @@ parser.prototype.text = function () {
49024902

49034903
/** consume the next token **/
49044904
parser.prototype.next = function () {
4905+
// prepare the back command
4906+
this.prev = [this.lexer.yylloc.last_line, this.lexer.yylloc.last_column, this.lexer.offset];
4907+
49054908
// eating the token
49064909
this.lex();
49074910

@@ -4929,9 +4932,6 @@ parser.prototype.next = function () {
49294932
* Eating a token
49304933
*/
49314934
parser.prototype.lex = function () {
4932-
// prepare the back command
4933-
this.prev = [this.lexer.yylloc.first_line, this.lexer.yylloc.first_column, this.lexer.offset];
4934-
49354935
// append on token stack
49364936
if (this.extractTokens) {
49374937
do {
@@ -4947,16 +4947,15 @@ parser.prototype.lex = function () {
49474947
this._tokens.push(entry);
49484948
if (this.token === this.tok.T_CLOSE_TAG) {
49494949
// https://github.com/php/php-src/blob/7ff186434e82ee7be7c59d0db9a976641cf7b09c/Zend/zend_compile.c#L1680
4950-
this.token = ';';
4950+
this.token = ";";
49514951
return this;
49524952
} else if (this.token === this.tok.T_OPEN_TAG_WITH_ECHO) {
49534953
this.token = this.tok.T_ECHO;
49544954
return this;
49554955
}
49564956
} while (this.token === this.tok.T_WHITESPACE || // ignore white space
49574957
!this.extractDoc && (this.token === this.tok.T_COMMENT || // ignore single lines comments
4958-
this.token === this.tok.T_DOC_COMMENT // ignore doc comments
4959-
) ||
4958+
this.token === this.tok.T_DOC_COMMENT) || // ignore doc comments
49604959
// ignore open tags
49614960
this.token === this.tok.T_OPEN_TAG);
49624961
} else {
@@ -5535,17 +5534,26 @@ module.exports = {
55355534
read_comment: function read_comment() {
55365535
var text = this.text();
55375536
var result = this.ast.prepare(text.substring(0, 2) === "/*" ? "commentblock" : "commentline", null, this);
5537+
// handle location on comment
5538+
var prev = this.prev;
5539+
this.prev = [this.lexer.yylloc.last_line, this.lexer.yylloc.last_column, this.lexer.offset];
55385540
this.lex();
5539-
return result(text);
5541+
result = result(text);
5542+
this.prev = prev;
5543+
return result;
55405544
},
55415545
/**
55425546
* Comments with / ** ... * /
55435547
*/
55445548
read_doc_comment: function read_doc_comment() {
55455549
var result = this.ast.prepare("commentblock", null, this);
55465550
var text = this.text();
5551+
var prev = this.prev;
5552+
this.prev = [this.lexer.yylloc.last_line, this.lexer.yylloc.last_column, this.lexer.offset];
55475553
this.lex();
5548-
return result(text);
5554+
result = result(text);
5555+
this.prev = prev;
5556+
return result;
55495557
}
55505558
};
55515559

@@ -6307,7 +6315,7 @@ module.exports = {
63076315
var alternate = null;
63086316
var shortForm = false;
63096317
var test = null;
6310-
test = this.read_if_expr();
6318+
test = this.next().read_if_expr();
63116319

63126320
if (this.token === ":") {
63136321
shortForm = true;
@@ -6316,10 +6324,10 @@ module.exports = {
63166324
var items = [];
63176325
while (this.token !== this.EOF && this.token !== this.tok.T_ENDIF) {
63186326
if (this.token === this.tok.T_ELSEIF) {
6319-
alternate = this.next().read_elseif_short();
6327+
alternate = this.read_elseif_short();
63206328
break;
63216329
} else if (this.token === this.tok.T_ELSE) {
6322-
alternate = this.next().read_else_short();
6330+
alternate = this.read_else_short();
63236331
break;
63246332
}
63256333
items.push(this.read_inner_statement());
@@ -6330,7 +6338,7 @@ module.exports = {
63306338
} else {
63316339
body = this.read_statement();
63326340
if (this.token === this.tok.T_ELSEIF) {
6333-
alternate = this.next().read_if();
6341+
alternate = this.read_if();
63346342
} else if (this.token === this.tok.T_ELSE) {
63356343
alternate = this.next().read_statement();
63366344
}
@@ -6355,15 +6363,15 @@ module.exports = {
63556363
var test = null;
63566364
var body = null;
63576365
var items = [];
6358-
test = this.read_if_expr();
6366+
test = this.next().read_if_expr();
63596367
if (this.expect(":")) this.next();
63606368
body = this.node("block");
63616369
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
63626370
if (this.token === this.tok.T_ELSEIF) {
6363-
alternate = this.next().read_elseif_short();
6371+
alternate = this.read_elseif_short();
63646372
break;
63656373
} else if (this.token === this.tok.T_ELSE) {
6366-
alternate = this.next().read_else_short();
6374+
alternate = this.read_else_short();
63676375
break;
63686376
}
63696377
items.push(this.read_inner_statement());
@@ -6375,8 +6383,8 @@ module.exports = {
63756383
*
63766384
*/
63776385
read_else_short: function read_else_short() {
6378-
if (this.expect(":")) this.next();
63796386
var body = this.node("block");
6387+
if (this.next().expect(":")) this.next();
63806388
var items = [];
63816389
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
63826390
items.push(this.read_inner_statement());
@@ -7224,7 +7232,7 @@ module.exports = {
72247232
return this.read_code_block(false);
72257233

72267234
case this.tok.T_IF:
7227-
return this.next().read_if();
7235+
return this.read_if();
72287236

72297237
case this.tok.T_SWITCH:
72307238
return this.read_switch();

0 commit comments

Comments
 (0)