Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit 1eb6cba

Browse files
committed
pre-release
1 parent ca007c8 commit 1eb6cba

15 files changed

+2220
-2134
lines changed

dist/php-parser.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* Package: php-parser
44
* Parse PHP code and returns its AST
5-
* Build: 800b5c0abc57980b33d2 - 2018-9-30
5+
* Build: aee66c9552f6f0b5f9e3 - 2018-10-7
66
* License: BSD-3-Clause
77
* Author: Ioan CHIRIAC
88
*
@@ -259,6 +259,8 @@ Declaration.prototype.parseFlags = function (flags) {
259259
if (this.kind !== "class") {
260260
if (flags[0] === -1) {
261261
this.visibility = IS_UNDEFINED;
262+
} else if (flags[0] === null) {
263+
this.visibility = null;
262264
} else if (flags[0] === 0) {
263265
this.visibility = IS_PUBLIC;
264266
} else if (flags[0] === 1) {
@@ -3066,6 +3068,9 @@ AST.prototype.prepare = function (kind, docs, parser) {
30663068
var location = null;
30673069
var args = Array.prototype.slice.call(arguments);
30683070
args.push(docs);
3071+
if (typeof result.preBuild === "function") {
3072+
result.preBuild(arguments);
3073+
}
30693074
if (self.withPositions || self.withSource) {
30703075
var src = null;
30713076
if (self.withSource) {
@@ -3088,9 +3093,13 @@ AST.prototype.prepare = function (kind, docs, parser) {
30883093
if (typeof node !== "function") {
30893094
throw new Error('Undefined node "' + kind + '"');
30903095
}
3091-
var result = Object.create(node.prototype);
3092-
node.apply(result, args);
3093-
return self.resolvePrecedence(result);
3096+
var astNode = Object.create(node.prototype);
3097+
node.apply(astNode, args);
3098+
result.instance = astNode;
3099+
if (result.trailingComments) {
3100+
astNode.trailingComments = result.trailingComments;
3101+
}
3102+
return self.resolvePrecedence(astNode);
30943103
};
30953104
/**
30963105
* Helper to change a node kind
@@ -3099,6 +3108,18 @@ AST.prototype.prepare = function (kind, docs, parser) {
30993108
result.setKind = function (newKind) {
31003109
kind = newKind;
31013110
};
3111+
/**
3112+
* Sets a list of trailing comments
3113+
* @param {*} docs
3114+
*/
3115+
result.setTrailingComments = function (docs) {
3116+
if (result.instance) {
3117+
// already created
3118+
result.instance.trailingComments = docs;
3119+
} else {
3120+
result.trailingComments = docs;
3121+
}
3122+
};
31023123
/**
31033124
* Release a node without using it on the AST
31043125
*/
@@ -4394,8 +4415,11 @@ module.exports = {
43944415

43954416
default:
43964417
// default fallback expr
4418+
//let statement = this.node('statement');
43974419
expr = this.read_expr();
43984420
this.expectEndOfStatement(expr);
4421+
/*statement = statement();
4422+
statement.expression = expr;*/
43994423
return expr;
44004424
}
44014425
},
@@ -6070,7 +6094,8 @@ module.exports = {
60706094
// jump over T_VAR then land on T_VARIABLE
60716095
if (this.token === this.tok.T_VAR) {
60726096
this.next().expect(this.tok.T_VARIABLE);
6073-
flags[0] = flags[1] = 0; // public & non static var
6097+
flags[0] = null; // public (as null)
6098+
flags[1] = 0; // non static var
60746099
}
60756100

60766101
if (this.token === this.tok.T_VARIABLE) {
@@ -6624,6 +6649,7 @@ parser.prototype.parse = function (code, filename) {
66246649
this.innerList = false;
66256650
this.innerListForm = false;
66266651
var program = this.ast.prepare("program", null, this);
6652+
this._nodeStack = [program];
66276653
var childs = [];
66286654
this.next();
66296655
while (this.token != this.EOF) {
@@ -6691,15 +6717,29 @@ parser.prototype.error = function (expect) {
66916717
*/
66926718
parser.prototype.node = function (name) {
66936719
if (this.extractDoc) {
6720+
var docs = null;
66946721
if (this._docIndex < this._docs.length) {
6695-
var docs = this._docs.slice(this._docIndex);
6722+
docs = this._docs.slice(this._docIndex);
66966723
this._docIndex = this._docs.length;
66976724
if (this.debug) {
66986725
console.log(new Error("Append docs on " + name));
66996726
console.log(docs);
67006727
}
6701-
return this.ast.prepare(name, docs, this);
67026728
}
6729+
var node = this.ast.prepare(name, docs, this);
6730+
this._nodeStack.push(node);
6731+
node.preBuild = function () {
6732+
var index = this._nodeStack.indexOf(node);
6733+
if (index > -1) {
6734+
this._nodeStack = this._nodeStack.slice(0, index);
6735+
}
6736+
// inject leading comment on current node
6737+
if (this._docIndex < this._docs.length) {
6738+
node.setTrailingComments(this._docs.slice(this._docIndex));
6739+
this._docIndex = this._docs.length;
6740+
}
6741+
}.bind(this);
6742+
return node;
67036743
}
67046744
return this.ast.prepare(name, null, this);
67056745
};

dist/php-parser.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/Array.html

Lines changed: 541 additions & 557 deletions
Large diffs are not rendered by default.

docs/Variable.html

Lines changed: 564 additions & 572 deletions
Large diffs are not rendered by default.

docs/ast.js.html

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ <h1 class="page-title">
357357
let location = null;
358358
const args = Array.prototype.slice.call(arguments);
359359
args.push(docs);
360+
if (typeof result.preBuild === "function") {
361+
result.preBuild(arguments);
362+
}
360363
if (self.withPositions || self.withSource) {
361364
let src = null;
362365
if (self.withSource) {
@@ -383,9 +386,13 @@ <h1 class="page-title">
383386
if (typeof node !== "function") {
384387
throw new Error('Undefined node "' + kind + '"');
385388
}
386-
const result = Object.create(node.prototype);
387-
node.apply(result, args);
388-
return self.resolvePrecedence(result);
389+
const astNode = Object.create(node.prototype);
390+
node.apply(astNode, args);
391+
result.instance = astNode;
392+
if (result.trailingComments) {
393+
astNode.trailingComments = result.trailingComments;
394+
}
395+
return self.resolvePrecedence(astNode);
389396
};
390397
/**
391398
* Helper to change a node kind
@@ -394,6 +401,35 @@ <h1 class="page-title">
394401
result.setKind = function(newKind) {
395402
kind = newKind;
396403
};
404+
/**
405+
* Sets a list of trailing comments
406+
* @param {*} docs
407+
*/
408+
result.setTrailingComments = function(docs) {
409+
if (result.instance) {
410+
// already created
411+
result.instance.trailingComments = docs;
412+
} else {
413+
result.trailingComments = docs;
414+
}
415+
};
416+
/**
417+
* Release a node without using it on the AST
418+
*/
419+
result.destroy = function(target) {
420+
if (docs) {
421+
// release current docs stack
422+
if (target) {
423+
if (!target.leadingComments) {
424+
target.leadingComments = docs;
425+
} else {
426+
target.leadingComments = docs.concat(target.leadingComments);
427+
}
428+
} else {
429+
parser._docIndex = parser._docs.length - docs.length;
430+
}
431+
}
432+
};
397433
return result;
398434
};
399435

docs/ast_declaration.js.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ <h1 class="page-title">
143143
if (this.kind !== "class") {
144144
if (flags[0] === -1) {
145145
this.visibility = IS_UNDEFINED;
146+
} else if (flags[0] === null) {
147+
this.visibility = null;
146148
} else if (flags[0] === 0) {
147149
this.visibility = IS_PUBLIC;
148150
} else if (flags[0] === 1) {

docs/engine.html

Lines changed: 954 additions & 972 deletions
Large diffs are not rendered by default.

docs/global.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ <h4 class="name" id="ignoreStack">
286286
<dd class="tag-source">
287287
<ul class="dummy">
288288
<li>
289-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line382">line 382</a>
289+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line401">line 401</a>
290290
</li>
291291
</ul>
292292
</dd>

docs/parser.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ <h4 class="name" id="error">
534534
<dd class="tag-source">
535535
<ul class="dummy">
536536
<li>
537-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line324">line 324</a>
537+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line325">line 325</a>
538538
</li>
539539
</ul>
540540
</dd>
@@ -690,7 +690,7 @@ <h5>Parameters:</h5>
690690
<dd class="tag-source">
691691
<ul class="dummy">
692692
<li>
693-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line427">line 427</a>
693+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line446">line 446</a>
694694
</li>
695695
</ul>
696696
</dd>
@@ -793,7 +793,7 @@ <h4 class="name" id="expectEndOfStatement">
793793
<dd class="tag-source">
794794
<ul class="dummy">
795795
<li>
796-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line366">line 366</a>
796+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line385">line 385</a>
797797
</li>
798798
</ul>
799799
</dd>
@@ -975,7 +975,7 @@ <h4 class="name" id="is">
975975
<dd class="tag-source">
976976
<ul class="dummy">
977977
<li>
978-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line541">line 541</a>
978+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line560">line 560</a>
979979
</li>
980980
</ul>
981981
</dd>
@@ -1066,7 +1066,7 @@ <h4 class="name" id="lex">
10661066
<dd class="tag-source">
10671067
<ul class="dummy">
10681068
<li>
1069-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line490">line 490</a>
1069+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line509">line 509</a>
10701070
</li>
10711071
</ul>
10721072
</dd>
@@ -1157,7 +1157,7 @@ <h4 class="name" id="next">
11571157
<dd class="tag-source">
11581158
<ul class="dummy">
11591159
<li>
1160-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line449">line 449</a>
1160+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line468">line 468</a>
11611161
</li>
11621162
</ul>
11631163
</dd>
@@ -1248,7 +1248,7 @@ <h4 class="name" id="node">
12481248
<dd class="tag-source">
12491249
<ul class="dummy">
12501250
<li>
1251-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line351">line 351</a>
1251+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line352">line 352</a>
12521252
</li>
12531253
</ul>
12541254
</dd>
@@ -1430,7 +1430,7 @@ <h4 class="name" id="raiseError">
14301430
<dd class="tag-source">
14311431
<ul class="dummy">
14321432
<li>
1433-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line297">line 297</a>
1433+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line298">line 298</a>
14341434
</li>
14351435
</ul>
14361436
</dd>
@@ -1521,7 +1521,7 @@ <h4 class="name" id="text">
15211521
<dd class="tag-source">
15221522
<ul class="dummy">
15231523
<li>
1524-
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line444">line 444</a>
1524+
<a href="parser.js.html">parser.js</a>, <a href="parser.js.html#line463">line 463</a>
15251525
</li>
15261526
</ul>
15271527
</dd>

docs/parser.js.html

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ <h1 class="page-title">
373373
this.innerList = false;
374374
this.innerListForm = false;
375375
const program = this.ast.prepare("program", null, this);
376+
this._nodeStack = [program];
376377
let childs = [];
377378
this.next();
378379
while (this.token != this.EOF) {
@@ -453,11 +454,29 @@ <h1 class="page-title">
453454
*/
454455
parser.prototype.node = function(name) {
455456
if (this.extractDoc) {
457+
let docs = null;
456458
if (this._docIndex &lt; this._docs.length) {
457-
const docs = this._docs.slice(this._docIndex);
459+
docs = this._docs.slice(this._docIndex);
458460
this._docIndex = this._docs.length;
459-
return this.ast.prepare(name, docs, this);
461+
if (this.debug) {
462+
console.log(new Error("Append docs on " + name));
463+
console.log(docs);
464+
}
460465
}
466+
const node = this.ast.prepare(name, docs, this);
467+
this._nodeStack.push(node);
468+
node.preBuild = function() {
469+
const index = this._nodeStack.indexOf(node);
470+
if (index > -1) {
471+
this._nodeStack = this._nodeStack.slice(0, index);
472+
}
473+
// inject leading comment on current node
474+
if (this._docIndex &lt; this._docs.length) {
475+
node.setTrailingComments(this._docs.slice(this._docIndex));
476+
this._docIndex = this._docs.length;
477+
}
478+
}.bind(this);
479+
return node;
461480
}
462481
return this.ast.prepare(name, null, this);
463482
};
@@ -482,7 +501,7 @@ <h1 class="page-title">
482501
};
483502

484503
/** outputs some debug information on current token **/
485-
const ignoreStack = ["parser.next"];
504+
const ignoreStack = ["parser.next", "parser.node", "parser.showlog"];
486505
parser.prototype.showlog = function() {
487506
const stack = new Error().stack.split("\n");
488507
let line;

docs/parser_class.js.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ <h1 class="page-title">
191191
// jump over T_VAR then land on T_VARIABLE
192192
if (this.token === this.tok.T_VAR) {
193193
this.next().expect(this.tok.T_VARIABLE);
194-
flags[0] = flags[1] = 0; // public &amp; non static var
194+
flags[0] = null; // public (as null)
195+
flags[1] = 0; // non static var
195196
}
196197

197198
if (this.token === this.tok.T_VARIABLE) {

0 commit comments

Comments
 (0)