mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Test parser position information (syntax errors)
This commit is contained in:
parent
4defbc2174
commit
611fa5c7f1
@ -21,7 +21,10 @@ if (empty($files)) {
|
|||||||
showHelp("Must specify at least one file.");
|
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;
|
$dumper = new PhpParser\NodeDumper;
|
||||||
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
||||||
$serializer = new PhpParser\Serializer\XML;
|
$serializer = new PhpParser\Serializer\XML;
|
||||||
@ -45,13 +48,14 @@ foreach ($files as $file) {
|
|||||||
try {
|
try {
|
||||||
$stmts = $parser->parse($code);
|
$stmts = $parser->parse($code);
|
||||||
} catch (PhpParser\Error $e) {
|
} catch (PhpParser\Error $e) {
|
||||||
|
if ($attributes['with-column-info'] && $e->hasColumnInfo()) {
|
||||||
$message = $e->getMessage();
|
$startLine = $e->getStartLine();
|
||||||
|
$endLine = $e->getEndLine();
|
||||||
if($attributes['with-column-info'] && $e->hasTokenAttributes()){
|
$startColumn = $e->getStartColumn($code);
|
||||||
$beginColumn = $e->getBeginColumn();
|
$endColumn = $e->getEndColumn($code);
|
||||||
$endColumn = $e->getEndColumn();
|
$message .= $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
|
||||||
$message .= ", column {$beginColumn} to {$endColumn}";
|
} else {
|
||||||
|
$message = $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
die($message . "\n");
|
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
|
--serialize-xml Serialize nodes using Serializer\XML
|
||||||
--var-dump var_dump() nodes (for exact structure)
|
--var-dump var_dump() nodes (for exact structure)
|
||||||
--resolve-names -N Resolve names using NodeVisitor\NameResolver
|
--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:
|
Example:
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ abstract class ParserAbstract
|
|||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString,
|
'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString,
|
||||||
$startAttributes + $endAttributes
|
$startAttributes + $nextEndAttributes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,15 +30,24 @@ class ParserTest extends CodeTestAbstract
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideTestParseFail
|
* @dataProvider provideTestParseFail
|
||||||
*/
|
*/
|
||||||
public function testParseFail($name, $code, $msg) {
|
public function testParseFail($name, $code, $expectedMsg) {
|
||||||
$parser = new Parser(new Lexer\Emulative);
|
$lexer = new Lexer\Emulative(array('usedAttributes' => array(
|
||||||
|
'startLine', 'endLine', 'startFilePos', 'endFilePos'
|
||||||
|
)));
|
||||||
|
$parser = new Parser($lexer);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$parser->parse($code);
|
$parser->parse($code);
|
||||||
|
|
||||||
$this->fail(sprintf('"%s": Expected Error', $name));
|
$this->fail(sprintf('"%s": Expected Error', $name));
|
||||||
} catch (Error $e) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,4 +3,4 @@ New without a class
|
|||||||
<?php
|
<?php
|
||||||
new;
|
new;
|
||||||
-----
|
-----
|
||||||
Syntax error, unexpected ';' on line 2
|
Syntax error, unexpected ';' from 2:3 to 2:3
|
||||||
|
@ -26,7 +26,7 @@ Cannot use the final modifier on an abstract class member on line 1
|
|||||||
-----
|
-----
|
||||||
<?php abstract final class A { }
|
<?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; }
|
<?php class A { abstract $a; }
|
||||||
-----
|
-----
|
||||||
|
@ -10,7 +10,7 @@ Cannot use 'parent' as class name as it is reserved on line 1
|
|||||||
-----
|
-----
|
||||||
<?php class static {}
|
<?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 {}
|
<?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 {}
|
<?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 {}
|
<?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 {}
|
<?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 {}
|
<?php interface self {}
|
||||||
-----
|
-----
|
||||||
@ -46,7 +46,7 @@ Cannot use 'parent' as class name as it is reserved on line 1
|
|||||||
-----
|
-----
|
||||||
<?php interface static {}
|
<?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 {}
|
<?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 {}
|
<?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
|
||||||
|
@ -10,7 +10,7 @@ Cannot use 'parent' as namespace name on line 1
|
|||||||
-----
|
-----
|
||||||
<?php namespace static;
|
<?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;
|
<?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;
|
<?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
|
||||||
|
Loading…
Reference in New Issue
Block a user