From 611fa5c7f1ffe9611757fb837e785fd96270862d Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 18 Apr 2015 13:05:40 +0200 Subject: [PATCH] Test parser position information (syntax errors) --- bin/php-parse.php | 22 +++++++++++-------- lib/PhpParser/ParserAbstract.php | 2 +- test/PhpParser/ParserTest.php | 15 ++++++++++--- .../parser/expr/newWithoutClass.test-fail | 2 +- .../code/parser/stmt/class/modifier.test-fail | 2 +- test/code/parser/stmt/class/name.test-fail | 10 ++++----- .../code/parser/stmt/namespace/name.test-fail | 4 ++-- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/bin/php-parse.php b/bin/php-parse.php index 6f98974..7e83ac0 100755 --- a/bin/php-parse.php +++ b/bin/php-parse.php @@ -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) { - - $message = $e->getMessage(); - - if($attributes['with-column-info'] && $e->hasTokenAttributes()){ - $beginColumn = $e->getBeginColumn(); - $endColumn = $e->getEndColumn(); - $message .= ", column {$beginColumn} to {$endColumn}"; + 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(); } 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: diff --git a/lib/PhpParser/ParserAbstract.php b/lib/PhpParser/ParserAbstract.php index 2b3f5e4..352aeb8 100644 --- a/lib/PhpParser/ParserAbstract.php +++ b/lib/PhpParser/ParserAbstract.php @@ -241,7 +241,7 @@ abstract class ParserAbstract throw new Error( 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString, - $startAttributes + $endAttributes + $startAttributes + $nextEndAttributes ); } } diff --git a/test/PhpParser/ParserTest.php b/test/PhpParser/ParserTest.php index 4d4db0e..f6b00a1 100644 --- a/test/PhpParser/ParserTest.php +++ b/test/PhpParser/ParserTest.php @@ -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); } } diff --git a/test/code/parser/expr/newWithoutClass.test-fail b/test/code/parser/expr/newWithoutClass.test-fail index 0708934..4e444a1 100644 --- a/test/code/parser/expr/newWithoutClass.test-fail +++ b/test/code/parser/expr/newWithoutClass.test-fail @@ -3,4 +3,4 @@ New without a class