X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Fnikic%2Fphp-parser%2Ftest%2FPhpParser%2FCodeParsingTest.php;fp=vendor%2Fnikic%2Fphp-parser%2Ftest%2FPhpParser%2FCodeParsingTest.php;h=3bdab812e32999d0d612ac508441e4bdc98c17a0;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=1a4a9ad1dfa479604fb68c60b2cbeb035eb4e428;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/vendor/nikic/php-parser/test/PhpParser/CodeParsingTest.php b/vendor/nikic/php-parser/test/PhpParser/CodeParsingTest.php index 1a4a9ad1d..3bdab812e 100644 --- a/vendor/nikic/php-parser/test/PhpParser/CodeParsingTest.php +++ b/vendor/nikic/php-parser/test/PhpParser/CodeParsingTest.php @@ -1,7 +1,10 @@ - array( - 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments' - ))); - $parser5 = new Parser\Php5($lexer); - $parser7 = new Parser\Php7($lexer); - - $dumpPositions = isset($modes['positions']); - $output5 = $this->getParseOutput($parser5, $code, $dumpPositions); - $output7 = $this->getParseOutput($parser7, $code, $dumpPositions); + list($parser5, $parser7) = $this->createParsers($modes); + list($stmts5, $output5) = $this->getParseOutput($parser5, $code, $modes); + list($stmts7, $output7) = $this->getParseOutput($parser7, $code, $modes); if (isset($modes['php5'])) { $this->assertSame($expected, $output5, $name); $this->assertNotSame($expected, $output7, $name); - } else if (isset($modes['php7'])) { + } elseif (isset($modes['php7'])) { $this->assertNotSame($expected, $output5, $name); $this->assertSame($expected, $output7, $name); } else { $this->assertSame($expected, $output5, $name); $this->assertSame($expected, $output7, $name); } + + $this->checkAttributes($stmts5); + $this->checkAttributes($stmts7); + } + + public function createParsers(array $modes) { + $lexer = new Lexer\Emulative(['usedAttributes' => [ + 'startLine', 'endLine', + 'startFilePos', 'endFilePos', + 'startTokenPos', 'endTokenPos', + 'comments' + ]]); + + return [ + new Parser\Php5($lexer), + new Parser\Php7($lexer), + ]; } - private function getParseOutput(Parser $parser, $code, $dumpPositions) { + private function getParseOutput(Parser $parser, $code, array $modes) { + $dumpPositions = isset($modes['positions']); + $errors = new ErrorHandler\Collecting; $stmts = $parser->parse($code, $errors); @@ -53,7 +68,7 @@ class CodeParsingTest extends CodeTestAbstract $output .= $dumper->dump($stmts, $code); } - return canonicalize($output); + return [$stmts, canonicalize($output)]; } public function provideTestParse() { @@ -67,4 +82,39 @@ class CodeParsingTest extends CodeTestAbstract return $e->getMessage(); } } + + private function checkAttributes($stmts) { + if ($stmts === null) { + return; + } + + $traverser = new NodeTraverser(); + $traverser->addVisitor(new class extends NodeVisitorAbstract { + public function enterNode(Node $node) { + $startLine = $node->getStartLine(); + $endLine = $node->getEndLine(); + $startFilePos = $node->getStartFilePos(); + $endFilePos = $node->getEndFilePos(); + $startTokenPos = $node->getStartTokenPos(); + $endTokenPos = $node->getEndTokenPos(); + if ($startLine < 0 || $endLine < 0 || + $startFilePos < 0 || $endFilePos < 0 || + $startTokenPos < 0 || $endTokenPos < 0 + ) { + throw new \Exception('Missing location information on ' . $node->getType()); + } + + if ($endLine < $startLine || + $endFilePos < $startFilePos || + $endTokenPos < $startTokenPos + ) { + // Nops and error can have inverted order, if they are empty + if (!$node instanceof Stmt\Nop && !$node instanceof Expr\Error) { + throw new \Exception('End < start on ' . $node->getType()); + } + } + } + }); + $traverser->traverse($stmts); + } }