/** * Parses the given file, and visits all nodes in the file and includes them too. * * @param string $path The path to the file to parse. * @return \PhpParser\Node[] The parse tree for the file, with all the includes expanded. */ private function parse($path) { $includer = new NodeTraverser(); $includer->addVisitor($this); return $includer->traverse($this->parser->parseFile($path)); }
public function testErrorPrinting() { $this->expectOutputString("[31m[Error] [0mTest at [33mline 1[0m" . PHP_EOL); $parser = new Parser(); $this->cli->getClass()->printResults(array(new Error("Test", $parser->parse('<?php $x = 3;')[0]))); }
/** * Parses the given file. * * @param string $file The path to the file. * @return array The statements in all included files file, and the complete parse tree of the * program after following requires and includes. * @throws Exception\ParseErrorException When there is a syntax error in the input source. * @throws IOException When the file cannot be opened. */ private static function parseFile($file) { $realfile = realpath($file); if ($file === false || $realfile === false) { throw new IOException($file); } $parser = new Parser(); try { $statements = $parser->parseFile($realfile); // Parse requires $includer = new NodeTraverser(); $includeResolver = new IncludeResolverVisitor($parser); $includer->addVisitor($includeResolver); $parseTree = $includer->traverse(array_slice($statements, 0)); // Convert to fully qualified names $traverser = new NodeTraverser(); $traverser->addVisitor(new NameResolver()); $statements = $traverser->traverse($statements); // Merge all the raw statements $files = array($realfile => $statements); $files = array_merge($files, $includeResolver->getFiles()); return array($files, $parseTree); } catch (Error $e) { throw new Exception\ParseErrorException($e->getMessage(), $e->getLine(), $e); } }