mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-06-23 08:32:26 +02:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
e57b3a0978 | |||
d01fafcb40 | |||
67df02c844 | |||
94c715d97e | |||
579f4ce846 | |||
94ca9a7ab9 | |||
bac91b426e | |||
08131e7ff2 | |||
0ba710affa | |||
72231abe6d | |||
d418bf3951 | |||
5a9fbca54a |
30
CHANGELOG.md
30
CHANGELOG.md
@ -1,8 +1,36 @@
|
||||
Version 3.1.2-dev
|
||||
Version 3.1.5-dev
|
||||
-----------------
|
||||
|
||||
Nothing yet.
|
||||
|
||||
Version 3.1.4 (2018-01-25)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed pretty printing of `-(-$x)` and `+(+$x)`. (#459)
|
||||
|
||||
Version 3.1.3 (2017-12-26)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Improve compatibility with php-scoper, by supporting prefixed namespaces in
|
||||
`NodeAbstract::getType()`.
|
||||
|
||||
Version 3.1.2 (2017-11-04)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Comments on empty blocks are now preserved on a `Stmt\Nop` node. (#382)
|
||||
|
||||
### Added
|
||||
|
||||
* Added `kind` attribute for `Stmt\Namespace_` node, which is one of `KIND_SEMICOLON` or
|
||||
`KIND_BRACED`. (#417)
|
||||
* Added `setDocComment()` method to namespace builder. (#437)
|
||||
|
||||
Version 3.1.1 (2017-09-02)
|
||||
--------------------------
|
||||
|
||||
|
4
LICENSE
4
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright (c) 2011 by Nikita Popov.
|
||||
Copyright (c) 2011-2018 by Nikita Popov.
|
||||
|
||||
Some rights reserved.
|
||||
|
||||
@ -28,4 +28,4 @@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
@ -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; }
|
||||
@ -155,7 +161,15 @@ inner_statement:
|
||||
;
|
||||
|
||||
non_empty_statement:
|
||||
'{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); }
|
||||
'{' inner_statement_list '}'
|
||||
{
|
||||
if ($2) {
|
||||
$$ = $2; prependLeadingComments($$);
|
||||
} else {
|
||||
makeNop($$, $this->startAttributeStack[#1]);
|
||||
if (null === $$) { $$ = array(); }
|
||||
}
|
||||
}
|
||||
| T_IF parentheses_expr statement elseif_list else_single
|
||||
{ $$ = Stmt\If_[$2, ['stmts' => toArray($3), 'elseifs' => $4, 'else' => $5]]; }
|
||||
| T_IF parentheses_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
|
||||
|
@ -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; }
|
||||
@ -191,7 +197,15 @@ inner_statement:
|
||||
;
|
||||
|
||||
non_empty_statement:
|
||||
'{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); }
|
||||
'{' inner_statement_list '}'
|
||||
{
|
||||
if ($2) {
|
||||
$$ = $2; prependLeadingComments($$);
|
||||
} else {
|
||||
makeNop($$, $this->startAttributeStack[#1]);
|
||||
if (null === $$) { $$ = array(); }
|
||||
}
|
||||
}
|
||||
| T_IF '(' expr ')' statement elseif_list else_single
|
||||
{ $$ = Stmt\If_[$3, ['stmts' => toArray($5), 'elseifs' => $6, 'else' => $7]]; }
|
||||
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
|
||||
|
@ -205,7 +205,7 @@ function resolveMacros($code) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
||||
. 'if (!empty($attrs[\'comments\']) && isset($stmts[0])) {'
|
||||
. 'if (!empty($attrs[\'comments\'])) {'
|
||||
. '$stmts[0]->setAttribute(\'comments\', '
|
||||
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use PhpParser;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Namespace_ extends PhpParser\BuilderAbstract
|
||||
class Namespace_ extends Declaration
|
||||
{
|
||||
private $name;
|
||||
private $stmts = array();
|
||||
@ -33,27 +33,12 @@ class Namespace_ extends PhpParser\BuilderAbstract
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple statements.
|
||||
*
|
||||
* @param array $stmts The statements to add
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmts(array $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
$this->addStmt($stmt);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built node.
|
||||
*
|
||||
* @return Node The built node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new Stmt\Namespace_($this->name, $this->stmts);
|
||||
return new Stmt\Namespace_($this->name, $this->stmts, $this->attributes);
|
||||
}
|
||||
}
|
||||
|
@ -360,7 +360,7 @@ class Lexer
|
||||
if ('T_HASHBANG' === $name) {
|
||||
// HHVM uses a special token for #! hashbang lines
|
||||
$tokenMap[$i] = Tokens::T_INLINE_HTML;
|
||||
} else if (defined($name = 'PhpParser\Parser\Tokens::' . $name)) {
|
||||
} else if (defined($name = Tokens::class . '::' . $name)) {
|
||||
// Other tokens can be mapped directly
|
||||
$tokenMap[$i] = constant($name);
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ class ClassMethod extends Node\Stmt implements FunctionLike
|
||||
public $params;
|
||||
/** @var null|string|Node\Name|Node\NullableType Return type */
|
||||
public $returnType;
|
||||
/** @var Node[] Statements */
|
||||
/** @var Node[]|null Statements */
|
||||
public $stmts;
|
||||
|
||||
/** @deprecated Use $flags instead */
|
||||
|
@ -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 */
|
||||
|
@ -21,7 +21,15 @@ abstract class NodeAbstract implements Node, \JsonSerializable
|
||||
* @return string Type of the node
|
||||
*/
|
||||
public function getType() {
|
||||
return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
|
||||
$className = rtrim(get_class($this), '_');
|
||||
return strtr(
|
||||
substr(
|
||||
$className,
|
||||
strpos($className, 'PhpParser\Node') + 15
|
||||
),
|
||||
'\\',
|
||||
'_'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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() {
|
||||
@ -1410,7 +1416,14 @@ class Php5 extends \PhpParser\ParserAbstract
|
||||
}
|
||||
|
||||
protected function reduceRule127() {
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments']) && isset($stmts[0])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); };
|
||||
|
||||
if ($this->semStack[$this->stackPos-(3-2)]) {
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments'])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); };
|
||||
} else {
|
||||
$startAttributes = $this->startAttributeStack[$this->stackPos-(3-1)]; if (isset($startAttributes['comments'])) { $this->semValue = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $this->semValue = null; };
|
||||
if (null === $this->semValue) { $this->semValue = array(); }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function reduceRule128() {
|
||||
|
@ -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() {
|
||||
@ -1364,7 +1370,14 @@ class Php7 extends \PhpParser\ParserAbstract
|
||||
}
|
||||
|
||||
protected function reduceRule138() {
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments']) && isset($stmts[0])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); };
|
||||
|
||||
if ($this->semStack[$this->stackPos-(3-2)]) {
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments'])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); };
|
||||
} else {
|
||||
$startAttributes = $this->startAttributeStack[$this->stackPos-(3-1)]; if (isset($startAttributes['comments'])) { $this->semValue = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $this->semValue = null; };
|
||||
if (null === $this->semValue) { $this->semValue = array(); }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function reduceRule139() {
|
||||
|
@ -374,10 +374,18 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) {
|
||||
if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) {
|
||||
// Enforce -(-$expr) instead of --$expr
|
||||
return '-(' . $this->p($node->expr) . ')';
|
||||
}
|
||||
return $this->pPrefixOp('Expr_UnaryMinus', '-', $node->expr);
|
||||
}
|
||||
|
||||
protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) {
|
||||
if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) {
|
||||
// Enforce +(+$expr) instead of ++$expr
|
||||
return '+(' . $this->p($node->expr) . ')';
|
||||
}
|
||||
return $this->pPrefixOp('Expr_UnaryPlus', '+', $node->expr);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser\Comment\Doc;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
@ -15,19 +16,23 @@ class NamespaceTest extends \PHPUnit_Framework_TestCase
|
||||
$stmt1 = new Stmt\Class_('SomeClass');
|
||||
$stmt2 = new Stmt\Interface_('SomeInterface');
|
||||
$stmt3 = new Stmt\Function_('someFunction');
|
||||
$docComment = new Doc('/** Test */');
|
||||
$expected = new Stmt\Namespace_(
|
||||
new Node\Name('Name\Space'),
|
||||
array($stmt1, $stmt2, $stmt3)
|
||||
array($stmt1, $stmt2, $stmt3),
|
||||
array('comments' => array($docComment))
|
||||
);
|
||||
|
||||
$node = $this->createNamespaceBuilder('Name\Space')
|
||||
->addStmt($stmt1)
|
||||
->addStmts(array($stmt2, $stmt3))
|
||||
->setDocComment($docComment)
|
||||
->getNode()
|
||||
;
|
||||
$this->assertEquals($expected, $node);
|
||||
|
||||
$node = $this->createNamespaceBuilder(new Node\Name(array('Name', 'Space')))
|
||||
->setDocComment($docComment)
|
||||
->addStmts(array($stmt1, $stmt2))
|
||||
->addStmt($stmt3)
|
||||
->getNode()
|
||||
|
@ -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]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,9 @@ Comments on blocks
|
||||
$a;
|
||||
}
|
||||
}
|
||||
|
||||
// empty
|
||||
{}
|
||||
-----
|
||||
array(
|
||||
0: Expr_Variable(
|
||||
@ -20,4 +23,9 @@ array(
|
||||
2: // baz
|
||||
)
|
||||
)
|
||||
1: Stmt_Nop(
|
||||
comments: array(
|
||||
0: // empty
|
||||
)
|
||||
)
|
||||
)
|
@ -38,6 +38,11 @@ yield from ($a and yield from $b);
|
||||
|
||||
print ($a and print $b);
|
||||
|
||||
-(-$a);
|
||||
+(+$a);
|
||||
-(--$a);
|
||||
+(++$a);
|
||||
|
||||
// The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment
|
||||
// and incdec only work on variables.
|
||||
!$a = $b;
|
||||
@ -70,6 +75,10 @@ $a ** $b ** $c;
|
||||
yield from $a and yield from $b;
|
||||
yield from ($a and yield from $b);
|
||||
print ($a and print $b);
|
||||
-(-$a);
|
||||
+(+$a);
|
||||
-(--$a);
|
||||
+(++$a);
|
||||
// The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment
|
||||
// and incdec only work on variables.
|
||||
!($a = $b);
|
||||
|
Reference in New Issue
Block a user