Skip to content

Commit dcb0db3

Browse files
committed
pre-release
1 parent 3112d3d commit dcb0db3

File tree

3 files changed

+115
-128
lines changed

3 files changed

+115
-128
lines changed

dist/php-parser.js

Lines changed: 68 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! php-parser - BSD3 License - 2016-12-17 */
1+
/*! php-parser - BSD3 License - 2016-12-20 */
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
// shim for using process in browser
@@ -1790,38 +1790,6 @@ function isNumber(n) {
17901790
}
17911791

17921792

1793-
/**
1794-
* Graceful decorator
1795-
*/
1796-
var _gracefulDecorator = function(fn) {
1797-
try {
1798-
this._currentNode = this._gracefulProxy[fn].apply(
1799-
this,
1800-
Array.prototype.slice.call(arguments, 1)
1801-
);
1802-
return this._currentNode;
1803-
} catch(e) {
1804-
if (this.lastError) {
1805-
this.next(); // ignore token & go next
1806-
var errorNode = [
1807-
'error',
1808-
this.lastError.tokenName,
1809-
this.lastError.expected,
1810-
this.lastError.line
1811-
];
1812-
// force to append the error node
1813-
if (this.ast.length < 3) {
1814-
this.ast.push([]);
1815-
}
1816-
this.ast[2].push(errorNode);
1817-
// return the node
1818-
return errorNode;
1819-
} else {
1820-
throw e; // not a parsing error
1821-
}
1822-
}
1823-
};
1824-
18251793
/**
18261794
* The PHP Parser class
18271795
*
@@ -1962,40 +1930,12 @@ parser.prototype.getTokenName = function(token) {
19621930
}
19631931
};
19641932

1965-
/**
1966-
* enable / disable the graceful mode
1967-
*/
1968-
parser.prototype.graceful = function(mode) {
1969-
if (this._graceful !== mode) {
1970-
if (mode) {
1971-
// enable the graceful mode
1972-
this._gracefulProxy = {};
1973-
for(var i in this) {
1974-
var cb = this[i];
1975-
if (typeof cb === 'function') {
1976-
this._gracefulProxy[i] = cb;
1977-
this[i] = _gracefulDecorator.bind(this, i);
1978-
}
1979-
}
1980-
} else {
1981-
// disable the graceful mode
1982-
for(var i in this._gracefulProxy) {
1983-
this[i] = this._gracefulProxy[i];
1984-
}
1985-
}
1986-
this._graceful = mode;
1987-
}
1988-
return this;
1989-
};
1990-
19911933
/**
19921934
* main entry point : converts a source code to AST
19931935
*/
19941936
parser.prototype.parse = function(code) {
1937+
this.firstError = false;
19951938
this.lastError = false;
1996-
if (this.suppressErrors) {
1997-
this.graceful(this.suppressErrors);
1998-
}
19991939
this.currentNamespace = [''];
20001940
this.lexer.setInput(code);
20011941
this.lexer.comment_tokens = this.extractDoc;
@@ -2030,9 +1970,24 @@ parser.prototype.raiseError = function(message, msgExpect, expect, token) {
20301970
message: message,
20311971
line: this.lexer.yylloc.first_line
20321972
};
1973+
if (!this.firstError) {
1974+
this.firstError = this.lastError;
1975+
}
20331976
if (!this.suppressErrors) {
2034-
throw new Error(this.lastError.message);
1977+
throw new Error(message);
1978+
}
1979+
if (this.ast.length === 2) {
1980+
this.ast.push([]);
20351981
}
1982+
// Error node :
1983+
var node = [
1984+
'error',
1985+
this.token,
1986+
message,
1987+
this.lexer.yylloc.first_line
1988+
];
1989+
this.ast[2].push(node);
1990+
return node;
20361991
};
20371992

20381993
/**
@@ -2133,7 +2088,7 @@ parser.prototype.expectEndOfStatement = function() {
21332088
};
21342089

21352090
/** outputs some debug information on current token **/
2136-
var ignoreStack = ['parser.next', '_gracefulDecorator'];
2091+
var ignoreStack = ['parser.next', 'parser.nextWithComments'];
21372092
parser.prototype.showlog = function() {
21382093
var stack = (new Error()).stack.split('\n');
21392094
var line;
@@ -2164,14 +2119,12 @@ parser.prototype.showlog = function() {
21642119

21652120
/** force to expect specified token **/
21662121
parser.prototype.expect = function(token) {
2167-
if (!this.lastError) {
2168-
if (Array.isArray(token)) {
2169-
if (token.indexOf(this.token) === -1) {
2170-
this.error(token);
2171-
}
2172-
} else if (this.token != token) {
2122+
if (Array.isArray(token)) {
2123+
if (token.indexOf(this.token) === -1) {
21732124
this.error(token);
21742125
}
2126+
} else if (this.token != token) {
2127+
this.error(token);
21752128
}
21762129
return this;
21772130
};
@@ -2544,6 +2497,7 @@ module.exports = {
25442497
this.tok.T_VARIABLE,
25452498
this.tok.T_FUNCTION
25462499
]);
2500+
this.next(); // ignore token
25472501
}
25482502
}
25492503
this.expect('}').nextWithComments();
@@ -2622,7 +2576,6 @@ module.exports = {
26222576
if (this.is('T_MEMBER_FLAGS')) {
26232577
var idx = 0, val = 0;
26242578
do {
2625-
26262579
switch(this.token) {
26272580
case this.tok.T_PUBLIC: idx = 0; val = 0; break;
26282581
case this.tok.T_PROTECTED: idx = 0; val = 1; break;
@@ -2633,13 +2586,21 @@ module.exports = {
26332586
}
26342587
if (asInterface) {
26352588
if (idx == 0 && val == 2) {
2589+
// an interface can't be private
26362590
this.expect([this.tok.T_PUBLIC, this.tok.T_PROTECTED]);
2591+
val = -1;
26372592
} else if (idx == 2 && val == 1) {
2593+
// an interface cant be abstract
26382594
this.error();
2595+
val = -1;
26392596
}
26402597
}
2641-
if (result[idx] != -1) this.error();
2642-
result[idx] = val;
2598+
if (result[idx] !== -1) {
2599+
// already defined flag
2600+
this.error();
2601+
} else if (val !== -1) {
2602+
result[idx] = val;
2603+
}
26432604
} while(this.next().is('T_MEMBER_FLAGS'));
26442605
}
26452606

@@ -2751,6 +2712,7 @@ module.exports = {
27512712
this.tok.T_CONST,
27522713
this.tok.T_FUNCTION
27532714
]);
2715+
this.next();
27542716
}
27552717
}
27562718
this.expect('}').next();
@@ -3210,7 +3172,8 @@ module.exports = {
32103172
}
32113173
}
32123174
} else {
3213-
this.error('EXPR');
3175+
expr = this.error('EXPR');
3176+
this.next();
32143177
}
32153178

32163179
// returns variable | scalar
@@ -3415,6 +3378,7 @@ module.exports = {
34153378
break;
34163379
} else {
34173380
this.error([',', ')']);
3381+
break;
34183382
}
34193383
}
34203384
}
@@ -3744,7 +3708,10 @@ module.exports = {
37443708
this.currentNamespace = [''];
37453709
return result([''], this.read_code_block(true));
37463710
} else {
3747-
if(this.token === this.tok.T_NAMESPACE) this.error(['{', this.tok.T_STRING]);
3711+
if(this.token === this.tok.T_NAMESPACE) {
3712+
this.error(['{', this.tok.T_STRING]);
3713+
this.next(); // ignore namespace token
3714+
}
37483715
var name = this.read_namespace_name();
37493716
if (this.token == ';') {
37503717
this.currentNamespace = name;
@@ -3762,6 +3729,11 @@ module.exports = {
37623729
);
37633730
} else {
37643731
this.error(['{', ';']);
3732+
// graceful mode :
3733+
this.currentNamespace = name;
3734+
var body = this.read_top_statements();
3735+
this.expect(this.EOF);
3736+
return result(name, body);
37653737
}
37663738
}
37673739
}
@@ -3983,7 +3955,10 @@ module.exports = {
39833955
case '[': // short array format
39843956
return this.read_array();
39853957
default:
3986-
this.error('SCALAR');
3958+
var err = this.error('SCALAR');
3959+
// graceful mode : ignore token & return error node
3960+
this.next();
3961+
return err;
39873962
}
39883963
}
39893964
}
@@ -4131,7 +4106,9 @@ module.exports = {
41314106
case this.tok.T_INTERFACE:
41324107
return this.read_interface(flag);
41334108
default:
4134-
this.error([this.tok.T_CLASS, this.tok.T_INTERFACE]);
4109+
var err = this.error([this.tok.T_CLASS, this.tok.T_INTERFACE]);
4110+
this.next();
4111+
return err;
41354112
}
41364113
case this.tok.T_CLASS:
41374114
return this.read_class(0);
@@ -4228,7 +4205,10 @@ module.exports = {
42284205
case this.tok.T_INTERFACE:
42294206
return this.read_interface(flag);
42304207
default:
4231-
this.error([this.tok.T_CLASS, this.tok.T_INTERFACE]);
4208+
var err = this.error([this.tok.T_CLASS, this.tok.T_INTERFACE]);
4209+
// graceful mode : ignore token & go next
4210+
this.next();
4211+
return err;
42324212
}
42334213
case this.tok.T_CLASS:
42344214
return this.read_class(0);
@@ -4597,7 +4577,9 @@ module.exports = {
45974577
getter = this.text();
45984578
this.next();
45994579
} else {
4600-
this.error([this.tok.T_VARIABLE, this.tok.T_STRING]);
4580+
getter = this.error([this.tok.T_VARIABLE, this.tok.T_STRING]);
4581+
// graceful mode : set getter as error node and continue
4582+
this.next();
46014583
}
46024584
if (from[0] != 'ns') {
46034585
from = ['lookup', 'class', from];
@@ -4669,7 +4651,10 @@ module.exports = {
46694651
this.expect('}').next();
46704652
break;
46714653
default:
4672-
this.error([this.tok.T_STRING, this.tok.T_VARIABLE]);
4654+
what = this.error([this.tok.T_STRING, this.tok.T_VARIABLE]);
4655+
// graceful mode : set what as error mode & continue
4656+
this.next();
4657+
break;
46734658
}
46744659
result = ['prop', result, what];
46754660
break;
@@ -4755,7 +4740,9 @@ module.exports = {
47554740
this.next();
47564741
break;
47574742
default:
4758-
this.error(['{', '$', this.tok.T_VARIABLE]);
4743+
result = this.error(['{', '$', this.tok.T_VARIABLE]);
4744+
// graceful mode
4745+
this.next();
47594746
}
47604747
result = ['lookup', 'var', result];
47614748
}

0 commit comments

Comments
 (0)