Test parser position information (syntax errors)

This commit is contained in:
Nikita Popov 2015-04-18 13:05:40 +02:00
parent 4defbc2174
commit 611fa5c7f1
7 changed files with 35 additions and 22 deletions

View File

@ -21,7 +21,10 @@ if (empty($files)) {
showHelp("Must specify at least one file.");
}
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos'
)));
$parser = new PhpParser\Parser($lexer);
$dumper = new PhpParser\NodeDumper;
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$serializer = new PhpParser\Serializer\XML;
@ -45,13 +48,14 @@ foreach ($files as $file) {
try {
$stmts = $parser->parse($code);
} catch (PhpParser\Error $e) {
if ($attributes['with-column-info'] && $e->hasColumnInfo()) {
$startLine = $e->getStartLine();
$endLine = $e->getEndLine();
$startColumn = $e->getStartColumn($code);
$endColumn = $e->getEndColumn($code);
$message .= $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
} else {
$message = $e->getMessage();
if($attributes['with-column-info'] && $e->hasTokenAttributes()){
$beginColumn = $e->getBeginColumn();
$endColumn = $e->getEndColumn();
$message .= ", column {$beginColumn} to {$endColumn}";
}
die($message . "\n");
@ -95,7 +99,7 @@ Operations is a list of the following options (--dump by default):
--serialize-xml Serialize nodes using Serializer\XML
--var-dump var_dump() nodes (for exact structure)
--resolve-names -N Resolve names using NodeVisitor\NameResolver
--with-column-info -c Show column-numbers for occoured errors. (if any)
--with-column-info -c Show column-numbers for errors (if available)
Example:

View File

@ -241,7 +241,7 @@ abstract class ParserAbstract
throw new Error(
'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString,
$startAttributes + $endAttributes
$startAttributes + $nextEndAttributes
);
}
}

View File

@ -30,15 +30,24 @@ class ParserTest extends CodeTestAbstract
/**
* @dataProvider provideTestParseFail
*/
public function testParseFail($name, $code, $msg) {
$parser = new Parser(new Lexer\Emulative);
public function testParseFail($name, $code, $expectedMsg) {
$lexer = new Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos'
)));
$parser = new Parser($lexer);
try {
$parser->parse($code);
$this->fail(sprintf('"%s": Expected Error', $name));
} catch (Error $e) {
$this->assertSame($msg, $e->getMessage(), $name);
if ($e->hasColumnInfo()) {
$msg = $e->getRawMessage() . ' from ' . $e->getStartLine() . ':' . $e->getStartColumn($code)
. ' to ' . $e->getEndLine() . ':' . $e->getEndColumn($code);
} else {
$msg = $e->getMessage();
}
$this->assertSame($expectedMsg, $msg, $name);
}
}

View File

@ -3,4 +3,4 @@ New without a class
<?php
new;
-----
Syntax error, unexpected ';' on line 2
Syntax error, unexpected ';' from 2:3 to 2:3

View File

@ -26,7 +26,7 @@ Cannot use the final modifier on an abstract class member on line 1
-----
<?php abstract final class A { }
-----
Syntax error, unexpected T_FINAL, expecting T_CLASS on line 1
Syntax error, unexpected T_FINAL, expecting T_CLASS from 1:15 to 1:19
-----
<?php class A { abstract $a; }
-----

View File

@ -10,7 +10,7 @@ Cannot use 'parent' as class name as it is reserved on line 1
-----
<?php class static {}
-----
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING from 1:12 to 1:17
-----
<?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 {}
-----
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR from 1:22 to 1:27
-----
<?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 {}
-----
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR from 1:25 to 1:30
-----
<?php interface self {}
-----
@ -46,7 +46,7 @@ Cannot use 'parent' as class name as it is reserved on line 1
-----
<?php interface static {}
-----
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING from 1:16 to 1:21
-----
<?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 {}
-----
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR from 1:26 to 1:31

View File

@ -10,7 +10,7 @@ Cannot use 'parent' as namespace name on line 1
-----
<?php namespace static;
-----
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NS_SEPARATOR or '{' on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NS_SEPARATOR or '{' from 1:16 to 1:21
-----
<?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;
-----
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
Syntax error, unexpected T_STATIC, expecting T_STRING from 1:15 to 1:20