1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00

Add information on expected tokens to syntax errors

This now mimics the error messages provided by PHP itself (pre 5.4).
This commit is contained in:
nikic 2013-01-15 17:26:20 +01:00
parent 222c9612ab
commit fbaa1e5fc3
6 changed files with 69 additions and 11 deletions

View File

@ -1,7 +1,11 @@
Version 0.9.4-dev
-----------------
Nothing yet.
* Syntax errors now include information on expected tokens and mimic the format of PHP's own (pre 5.4) error messages.
Example:
Old: Unexpected token T_STATIC on line 1
New: Syntax error, unexpected T_STATIC, expecting T_STRING or T_NS_SEPARATOR or '{'
Version 0.9.3 (22.11.2012)
--------------------------

View File

@ -304,8 +304,35 @@ class #(-p)
$attributeStack[$this->stackPos] = $startAttributes;
} else {
/* error */
$expected = array();
$base = self::$yybase[$state];
for ($i = 0; $i < self::TOKEN_MAP_SIZE; ++$i) {
$n = $base + $i;
if ($n >= 0 && $n < self::YYLAST && self::$yycheck[$n] == $i
|| $state < self::YY2TBLSTATE
&& ($n = self::$yybase[$state + self::YYNLSTATES] + $i)
&& $n < self::YYLAST && self::$yycheck[$n] == $i
) {
if (self::$yyaction[$n] != self::YYUNEXPECTED) {
if (count($expected) == 4) {
/* Too many expected tokens */
$expected = array();
break;
}
$expected[] = self::$terminals[$i];
}
}
}
$expectedString = '';
if ($expected) {
$expectedString = ', expecting ' . implode(' or ', $expected);
}
throw new PHPParser_Error(
'Unexpected token ' . self::$terminals[$tokenId],
'Syntax error, unexpected ' . self::$terminals[$tokenId] . $expectedString,
$startAttributes['startLine']
);
}

View File

@ -1068,8 +1068,35 @@ class PHPParser_Parser
$attributeStack[$this->stackPos] = $startAttributes;
} else {
/* error */
$expected = array();
$base = self::$yybase[$state];
for ($i = 0; $i < self::TOKEN_MAP_SIZE; ++$i) {
$n = $base + $i;
if ($n >= 0 && $n < self::YYLAST && self::$yycheck[$n] == $i
|| $state < self::YY2TBLSTATE
&& ($n = self::$yybase[$state + self::YYNLSTATES] + $i)
&& $n < self::YYLAST && self::$yycheck[$n] == $i
) {
if (self::$yyaction[$n] != self::YYUNEXPECTED) {
if (count($expected) == 4) {
/* Too many expected tokens */
$expected = array();
break;
}
$expected[] = self::$terminals[$i];
}
}
}
$expectedString = '';
if ($expected) {
$expectedString = ', expecting ' . implode(' or ', $expected);
}
throw new PHPParser_Error(
'Unexpected token ' . self::$terminals[$tokenId],
'Syntax error, unexpected ' . self::$terminals[$tokenId] . $expectedString,
$startAttributes['startLine']
);
}

View File

@ -26,4 +26,4 @@ Cannot use the final and abstract modifier at the same time on line 1
-----
<?php abstract final class A { }
-----
Unexpected token T_FINAL on line 1
Syntax error, unexpected T_FINAL, expecting T_CLASS on line 1

View File

@ -10,7 +10,7 @@ Cannot use "parent" as class name as it is reserved on line 1
-----
<?php class static {}
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
-----
<?php class A extends self {}
-----
@ -22,7 +22,7 @@ Cannot use "parent" as class name as it is reserved on line 1
-----
<?php class A extends static {}
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1
-----
<?php class A implements self {}
-----
@ -34,7 +34,7 @@ Cannot use "parent" as interface name as it is reserved on line 1
-----
<?php class A implements static {}
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1
-----
<?php interface self {}
-----
@ -46,7 +46,7 @@ Cannot use "parent" as interface name as it is reserved on line 1
-----
<?php interface static {}
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
-----
<?php interface A extends self {}
-----
@ -58,4 +58,4 @@ Cannot use "parent" as interface name as it is reserved on line 1
-----
<?php interface A extends static {}
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1

View File

@ -10,7 +10,7 @@ Cannot use "parent" as namespace name as it is reserved on line 1
-----
<?php namespace static;
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NS_SEPARATOR or '{' on line 1
-----
<?php use A as self;
-----
@ -22,4 +22,4 @@ Cannot use "B" as "parent" because "parent" is a special class name on line 1
-----
<?php use C as static;
-----
Unexpected token T_STATIC on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1