From 5a9fbca54a5edba0be673b85f0bc824caddb6567 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 26 Sep 2017 18:42:52 +0200 Subject: [PATCH] Add attribute for namespace kinds (#417) One of KIND_SEMICOLON or KIND_BRACED. --- CHANGELOG.md | 5 ++++- grammar/php5.y | 12 +++++++++--- grammar/php7.y | 12 +++++++++--- lib/PhpParser/Node/Stmt/Namespace_.php | 4 ++++ lib/PhpParser/Parser/Php5.php | 12 +++++++++--- lib/PhpParser/Parser/Php7.php | 12 +++++++++--- test/PhpParser/ParserTest.php | 3 +++ 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fd7b094..a524e1d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ Version 3.1.2-dev ----------------- -Nothing yet. +### Added + +* Added `kind` attribute for `Stmt\Namespace_` node, which is one of `KIND_SEMICOLON` or + `KIND_BRACED`. (#417) Version 3.1.1 (2017-09-02) -------------------------- diff --git a/grammar/php5.y b/grammar/php5.y index 8c51e6ce..19651d8a 100644 --- a/grammar/php5.y +++ b/grammar/php5.y @@ -56,11 +56,17 @@ top_statement: | T_HALT_COMPILER { $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; } | T_NAMESPACE namespace_name ';' - { $$ = Stmt\Namespace_[$2, null]; $this->checkNamespace($$); } + { $$ = Stmt\Namespace_[$2, null]; + $$->setAttribute('kind', Stmt\Namespace_::KIND_SEMICOLON); + $this->checkNamespace($$); } | T_NAMESPACE namespace_name '{' top_statement_list '}' - { $$ = Stmt\Namespace_[$2, $4]; $this->checkNamespace($$); } + { $$ = Stmt\Namespace_[$2, $4]; + $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($$); } | T_NAMESPACE '{' top_statement_list '}' - { $$ = Stmt\Namespace_[null, $3]; $this->checkNamespace($$); } + { $$ = Stmt\Namespace_[null, $3]; + $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($$); } | T_USE use_declarations ';' { $$ = Stmt\Use_[$2, Stmt\Use_::TYPE_NORMAL]; } | T_USE use_type use_declarations ';' { $$ = Stmt\Use_[$3, $2]; } | group_use_declaration ';' { $$ = $1; } diff --git a/grammar/php7.y b/grammar/php7.y index 6794da83..f24a98ed 100644 --- a/grammar/php7.y +++ b/grammar/php7.y @@ -70,11 +70,17 @@ top_statement: | T_HALT_COMPILER { $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; } | T_NAMESPACE namespace_name semi - { $$ = Stmt\Namespace_[$2, null]; $this->checkNamespace($$); } + { $$ = Stmt\Namespace_[$2, null]; + $$->setAttribute('kind', Stmt\Namespace_::KIND_SEMICOLON); + $this->checkNamespace($$); } | T_NAMESPACE namespace_name '{' top_statement_list '}' - { $$ = Stmt\Namespace_[$2, $4]; $this->checkNamespace($$); } + { $$ = Stmt\Namespace_[$2, $4]; + $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($$); } | T_NAMESPACE '{' top_statement_list '}' - { $$ = Stmt\Namespace_[null, $3]; $this->checkNamespace($$); } + { $$ = Stmt\Namespace_[null, $3]; + $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($$); } | T_USE use_declarations semi { $$ = Stmt\Use_[$2, Stmt\Use_::TYPE_NORMAL]; } | T_USE use_type use_declarations semi { $$ = Stmt\Use_[$3, $2]; } | group_use_declaration semi { $$ = $1; } diff --git a/lib/PhpParser/Node/Stmt/Namespace_.php b/lib/PhpParser/Node/Stmt/Namespace_.php index 8b2d48eb..444855c3 100644 --- a/lib/PhpParser/Node/Stmt/Namespace_.php +++ b/lib/PhpParser/Node/Stmt/Namespace_.php @@ -6,6 +6,10 @@ use PhpParser\Node; class Namespace_ extends Node\Stmt { + /* For use in the "kind" attribute */ + const KIND_SEMICOLON = 1; + const KIND_BRACED = 2; + /** @var null|Node\Name Name */ public $name; /** @var Node[] Statements */ diff --git a/lib/PhpParser/Parser/Php5.php b/lib/PhpParser/Parser/Php5.php index 4837b085..c8ee06fe 100644 --- a/lib/PhpParser/Parser/Php5.php +++ b/lib/PhpParser/Parser/Php5.php @@ -1257,15 +1257,21 @@ class Php5 extends \PhpParser\ParserAbstract } protected function reduceRule89() { - $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(3-2)], null, $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes); $this->checkNamespace($this->semValue); + $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(3-2)], null, $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes); + $this->semValue->setAttribute('kind', Stmt\Namespace_::KIND_SEMICOLON); + $this->checkNamespace($this->semValue); } protected function reduceRule90() { - $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(5-2)], $this->semStack[$this->stackPos-(5-4)], $this->startAttributeStack[$this->stackPos-(5-1)] + $this->endAttributes); $this->checkNamespace($this->semValue); + $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(5-2)], $this->semStack[$this->stackPos-(5-4)], $this->startAttributeStack[$this->stackPos-(5-1)] + $this->endAttributes); + $this->semValue->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($this->semValue); } protected function reduceRule91() { - $this->semValue = new Stmt\Namespace_(null, $this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes); $this->checkNamespace($this->semValue); + $this->semValue = new Stmt\Namespace_(null, $this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes); + $this->semValue->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($this->semValue); } protected function reduceRule92() { diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index 7d032b5a..eeaa637b 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -1191,15 +1191,21 @@ class Php7 extends \PhpParser\ParserAbstract } protected function reduceRule95() { - $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(3-2)], null, $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes); $this->checkNamespace($this->semValue); + $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(3-2)], null, $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes); + $this->semValue->setAttribute('kind', Stmt\Namespace_::KIND_SEMICOLON); + $this->checkNamespace($this->semValue); } protected function reduceRule96() { - $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(5-2)], $this->semStack[$this->stackPos-(5-4)], $this->startAttributeStack[$this->stackPos-(5-1)] + $this->endAttributes); $this->checkNamespace($this->semValue); + $this->semValue = new Stmt\Namespace_($this->semStack[$this->stackPos-(5-2)], $this->semStack[$this->stackPos-(5-4)], $this->startAttributeStack[$this->stackPos-(5-1)] + $this->endAttributes); + $this->semValue->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($this->semValue); } protected function reduceRule97() { - $this->semValue = new Stmt\Namespace_(null, $this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes); $this->checkNamespace($this->semValue); + $this->semValue = new Stmt\Namespace_(null, $this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes); + $this->semValue->setAttribute('kind', Stmt\Namespace_::KIND_BRACED); + $this->checkNamespace($this->semValue); } protected function reduceRule98() { diff --git a/test/PhpParser/ParserTest.php b/test/PhpParser/ParserTest.php index 1295ae07..0dabba21 100644 --- a/test/PhpParser/ParserTest.php +++ b/test/PhpParser/ParserTest.php @@ -169,6 +169,9 @@ EOC; array("exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]), array("?>Foo", ['hasLeadingNewline' => false]), array("?>\nFoo", ['hasLeadingNewline' => true]), + array("namespace Foo;", ['kind' => Node\Stmt\Namespace_::KIND_SEMICOLON]), + array("namespace Foo {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]), + array("namespace {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]), ); } }