This commit is contained in:
Sara Itani 2016-12-28 16:46:00 -05:00
Родитель d0ac8bdc00
Коммит de9393f006
7 изменённых файлов: 22 добавлений и 21 удалений

Просмотреть файл

@ -57,9 +57,10 @@ and be on your way!
```php
<?php
$parser = new \PhpParser\Parser($myFilename);
$ast = $parser->parseSourceFile(); # returns an AST representing source file
$errors = $parser->getErrors($ast); # get errors from AST Node
$parser = new \PhpParser\Parser();
$fileContents = file_get_contents($myFilename);
$ast = $parser->parseSourceFile($fileContents); # returns an AST representing source file
$errors = \PhpParser\Utilities::getDiagnostics($ast); # get errors from AST Node
```
## Play around with the AST!

Просмотреть файл

@ -129,9 +129,7 @@ class Parser {
private $parameterTypeDeclarationTokens;
private $returnTypeDeclarationTokens;
public function __construct($content) {
$this->lexer = new Lexer($content);
public function __construct() {
$this->reservedWordTokens = array_values(RESERVED_WORDS);
$this->keywordTokens = array_values(KEYWORDS);
$this->nameOrKeywordOrReservedWordTokens = array_merge([TokenKind::Name], $this->keywordTokens, $this->reservedWordTokens);
@ -144,7 +142,9 @@ class Parser {
$this->returnTypeDeclarationTokens = array_merge([TokenKind::VoidReservedWord], $this->parameterTypeDeclarationTokens);
}
public function parseSourceFile() : Script {
public function parseSourceFile($contents) : Script {
$this->lexer = new Lexer($contents);
$this->reset();
$sourceFile = new Script();

Просмотреть файл

@ -9,8 +9,8 @@ use PhpParser\Parser;
use PhpParser\Utilities;
$contents = file_get_contents($argv[1]);
$parser = new Parser($contents);
$sourceFile = $parser->parseSourceFile();
$parser = new Parser();
$sourceFile = $parser->parseSourceFile($contents);
file_put_contents($argv[1] . ".ast", json_encode($sourceFile, JSON_PRETTY_PRINT));

Просмотреть файл

@ -17,14 +17,14 @@ class NodeApiTest extends TestCase {
const FILENAME_PATTERN = __dir__ . "/cases/{parser,}/*.php";
public function testSourceFileNodePosition() {
$parser = new \PhpParser\Parser(<<<'EOT'
$parser = new \PhpParser\Parser();
$node = $parser->parseSourceFile(<<<'EOT'
<?php
function a () {
$a = 3;
}
EOT
);
$node = $parser->parseSourceFile();
$this->assertEquals(\PhpParser\NodeKind::FunctionNode, $node->getNodeAtPosition(15)->kind);
$this->assertEquals(\PhpParser\NodeKind::Variable, $node->getNodeAtPosition(28)->kind);
}

Просмотреть файл

@ -39,8 +39,8 @@ class ParserFrameworkValidationTests extends TestCase {
*/
public function testFramworkErrors($testCaseFile, $frameworkName) {
$fileContents = file_get_contents($testCaseFile);
$parser = new \PhpParser\Parser($fileContents);
$sourceFile = $parser->parseSourceFile();
$parser = new \PhpParser\Parser();
$sourceFile = $parser->parseSourceFile($fileContents);
$directory = __DIR__ . "/output/$frameworkName/";
if (!file_exists($dir = __DIR__ . "/output")) {

Просмотреть файл

@ -41,9 +41,9 @@ class ParserGrammarTest extends TestCase {
$expectedTokens = str_replace("\r\n", "\n", file_get_contents($expectedTokensFile));
$fileContents = file_get_contents($testCaseFile);
$parser = new \PhpParser\Parser($fileContents);
$parser = new \PhpParser\Parser();
$GLOBALS["SHORT_TOKEN_SERIALIZE"] = true;
$tokens = str_replace("\r\n", "\n", json_encode($parser->parseSourceFile(), JSON_PRETTY_PRINT));
$tokens = str_replace("\r\n", "\n", json_encode($parser->parseSourceFile($fileContents), JSON_PRETTY_PRINT));
$GLOBALS["SHORT_TOKEN_SERIALIZE"] = false;
$this->tokens = $tokens;
@ -74,8 +74,8 @@ class ParserGrammarTest extends TestCase {
* @dataProvider outTreeProvider
*/
public function testSpecOutputTreeClassificationAndLength($testCaseFile, $expectedTreeFile) {
$parser = new \PhpParser\Parser(file_get_contents($testCaseFile));
$sourceFile = $parser->parseSourceFile();
$parser = new \PhpParser\Parser();
$sourceFile = $parser->parseSourceFile(file_get_contents($testCaseFile));
$tokens = str_replace("\r\n", "\n", json_encode($sourceFile, JSON_PRETTY_PRINT));
file_put_contents($expectedTreeFile, $tokens);

Просмотреть файл

@ -22,8 +22,8 @@ class ParserInvariantsTest extends LexerInvariantsTest {
$testCases = glob(self::FILENAME_PATTERN, GLOB_BRACE);
foreach ($testCases as $filename) {
$parser = new \PhpParser\Parser(file_get_contents($filename));
$testFiles[basename($filename)] = [$filename, $parser->parseSourceFile()];
$parser = new \PhpParser\Parser();
$testFiles[basename($filename)] = [$filename, $parser->parseSourceFile(file_get_contents($filename))];
}
return $testFiles;
}
@ -33,8 +33,8 @@ class ParserInvariantsTest extends LexerInvariantsTest {
$testCases = glob(self::FILENAME_PATTERN, GLOB_BRACE);
foreach ($testCases as $filename) {
$parser = new \PhpParser\Parser(file_get_contents($filename));
$sourceFileNode = $parser->parseSourceFile();
$parser = new \PhpParser\Parser();
$sourceFileNode = $parser->parseSourceFile(file_get_contents($filename));
$tokensArray = array();
foreach ($sourceFileNode->getDescendantNodesAndTokens() as $child) {
if ($child instanceof \PhpParser\Token) {