From de9393f006baacb1348be03ea9a244d33bec3401 Mon Sep 17 00:00:00 2001 From: Sara Itani Date: Wed, 28 Dec 2016 16:46:00 -0500 Subject: [PATCH] update parser contructor --- GettingStarted.md | 7 ++++--- parser.php | 8 ++++---- playground/server/src/parse.php | 4 ++-- tests/NodeApiTest.php | 4 ++-- tests/ParserFrameworkValidationTests.php | 4 ++-- tests/ParserGrammarTest.php | 8 ++++---- tests/ParserInvariantsTest.php | 8 ++++---- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/GettingStarted.md b/GettingStarted.md index c3c6279..c31c707 100644 --- a/GettingStarted.md +++ b/GettingStarted.md @@ -57,9 +57,10 @@ and be on your way! ```php 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! diff --git a/parser.php b/parser.php index 38f6caa..1a5c127 100644 --- a/parser.php +++ b/parser.php @@ -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(); diff --git a/playground/server/src/parse.php b/playground/server/src/parse.php index dbd530f..9736e5c 100644 --- a/playground/server/src/parse.php +++ b/playground/server/src/parse.php @@ -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)); diff --git a/tests/NodeApiTest.php b/tests/NodeApiTest.php index 3f7f130..adc10b9 100644 --- a/tests/NodeApiTest.php +++ b/tests/NodeApiTest.php @@ -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' parseSourceFile(); $this->assertEquals(\PhpParser\NodeKind::FunctionNode, $node->getNodeAtPosition(15)->kind); $this->assertEquals(\PhpParser\NodeKind::Variable, $node->getNodeAtPosition(28)->kind); } diff --git a/tests/ParserFrameworkValidationTests.php b/tests/ParserFrameworkValidationTests.php index 6e08edb..396f5d1 100644 --- a/tests/ParserFrameworkValidationTests.php +++ b/tests/ParserFrameworkValidationTests.php @@ -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")) { diff --git a/tests/ParserGrammarTest.php b/tests/ParserGrammarTest.php index 78b54eb..3cc0f20 100644 --- a/tests/ParserGrammarTest.php +++ b/tests/ParserGrammarTest.php @@ -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); diff --git a/tests/ParserInvariantsTest.php b/tests/ParserInvariantsTest.php index 5d4df0c..b5f8ca4 100644 --- a/tests/ParserInvariantsTest.php +++ b/tests/ParserInvariantsTest.php @@ -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) {