Skip to content

Commit 86ef553

Browse files
committed
Merge pull request #29 from danyaPostfactum/expectingtokens
Add expecting tokens info in parse error
2 parents 4a971dc + 1345c1f commit 86ef553

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

PHP.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7491,8 +7491,32 @@ PHP.Parser = function ( preprocessedTokens, eval ) {
74917491
} else {
74927492
/* error */
74937493
if (eval !== true) {
7494-
throw new PHP.ParseError("syntax error, unexpected " + terminals[ tokenId ] + ", expecting identifier", this.startAttributes['startLine']);
7495-
throw new Error('Unexpected token ' + terminals[ tokenId ] + ", tokenId " + tokenId + " line " + this.startAttributes['startLine']);
7494+
7495+
var expected = [];
7496+
7497+
for (var i = 0; i < this.TOKEN_MAP_SIZE; ++i) {
7498+
if ((yyn = yybase[ state ] + i) >= 0 && yyn < this.YYLAST && yycheck[ yyn ] == i
7499+
|| state < this.YY2TBLSTATE
7500+
&& (yyn = yybase[ state + this.YYNLSTATES] + i)
7501+
&& yyn < this.YYLAST && yycheck[ yyn ] == i
7502+
) {
7503+
if (yyaction[ yyn ] != this.YYUNEXPECTED) {
7504+
if (expected.length == 4) {
7505+
/* Too many expected tokens */
7506+
expected = [];
7507+
break;
7508+
}
7509+
7510+
expected.push( this.terminals[ i ] );
7511+
}
7512+
}
7513+
}
7514+
7515+
var expectedString = '';
7516+
if (expected.length) {
7517+
expectedString = ', expecting ' + expected.join(' or ');
7518+
}
7519+
throw new PHP.ParseError('syntax error, unexpected ' + terminals[ tokenId ] + expectedString, this.startAttributes['startLine']);
74967520
} else {
74977521
return this.startAttributes['startLine'];
74987522
}

src/parser/parser.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,32 @@ PHP.Parser = function ( preprocessedTokens, eval ) {
177177
} else {
178178
/* error */
179179
if (eval !== true) {
180-
throw new PHP.ParseError("syntax error, unexpected " + terminals[ tokenId ] + ", expecting identifier", this.startAttributes['startLine']);
181-
throw new Error('Unexpected token ' + terminals[ tokenId ] + ", tokenId " + tokenId + " line " + this.startAttributes['startLine']);
180+
181+
var expected = [];
182+
183+
for (var i = 0; i < this.TOKEN_MAP_SIZE; ++i) {
184+
if ((yyn = yybase[ state ] + i) >= 0 && yyn < this.YYLAST && yycheck[ yyn ] == i
185+
|| state < this.YY2TBLSTATE
186+
&& (yyn = yybase[ state + this.YYNLSTATES] + i)
187+
&& yyn < this.YYLAST && yycheck[ yyn ] == i
188+
) {
189+
if (yyaction[ yyn ] != this.YYUNEXPECTED) {
190+
if (expected.length == 4) {
191+
/* Too many expected tokens */
192+
expected = [];
193+
break;
194+
}
195+
196+
expected.push( this.terminals[ i ] );
197+
}
198+
}
199+
}
200+
201+
var expectedString = '';
202+
if (expected.length) {
203+
expectedString = ', expecting ' + expected.join(' or ');
204+
}
205+
throw new PHP.ParseError('syntax error, unexpected ' + terminals[ tokenId ] + expectedString, this.startAttributes['startLine']);
182206
} else {
183207
return this.startAttributes['startLine'];
184208
}

0 commit comments

Comments
 (0)