diff --git a/grammar/php5.y b/grammar/php5.y index ce1b3ff..346eecb 100644 --- a/grammar/php5.y +++ b/grammar/php5.y @@ -183,8 +183,8 @@ non_empty_statement: | T_STATIC static_var_list ';' { $$ = Stmt\Static_[$2]; } | T_ECHO expr_list ';' { $$ = Stmt\Echo_[$2]; } | T_INLINE_HTML { $$ = Stmt\InlineHTML[$1]; } - | yield_expr ';' { $$ = maybeMakeExprStmt($1); } - | expr ';' { $$ = maybeMakeExprStmt($1); } + | yield_expr ';' { $$ = Stmt\Expression[$1]; } + | expr ';' { $$ = Stmt\Expression[$1]; } | T_UNSET '(' variables_list ')' ';' { $$ = Stmt\Unset_[$3]; } | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement { $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; } @@ -196,7 +196,7 @@ non_empty_statement: | T_THROW expr ';' { $$ = Stmt\Throw_[$2]; } | T_GOTO identifier ';' { $$ = Stmt\Goto_[$2]; } | identifier ':' { $$ = Stmt\Label[$1]; } - | expr error { $$ = maybeMakeExprStmt($1); } + | expr error { $$ = Stmt\Expression[$1]; } | error { $$ = array(); /* means: no statement */ } ; diff --git a/grammar/php7.y b/grammar/php7.y index 83e569d..f9dd8dd 100644 --- a/grammar/php7.y +++ b/grammar/php7.y @@ -180,7 +180,7 @@ non_empty_statement: | T_STATIC static_var_list ';' { $$ = Stmt\Static_[$2]; } | T_ECHO expr_list ';' { $$ = Stmt\Echo_[$2]; } | T_INLINE_HTML { $$ = Stmt\InlineHTML[$1]; } - | expr ';' { $$ = maybeMakeExprStmt($1); } + | expr ';' { $$ = Stmt\Expression[$1]; } | T_UNSET '(' variables_list ')' ';' { $$ = Stmt\Unset_[$3]; } | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement { $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; } @@ -192,7 +192,7 @@ non_empty_statement: | T_THROW expr ';' { $$ = Stmt\Throw_[$2]; } | T_GOTO identifier ';' { $$ = Stmt\Goto_[$2]; } | identifier ':' { $$ = Stmt\Label[$1]; } - | expr error { $$ = maybeMakeExprStmt($1); } + | expr error { $$ = Stmt\Expression[$1]; } | error { $$ = array(); /* means: no statement */ } ; diff --git a/grammar/rebuildParsers.php b/grammar/rebuildParsers.php index b353d39..9eea0d9 100644 --- a/grammar/rebuildParsers.php +++ b/grammar/rebuildParsers.php @@ -231,13 +231,6 @@ function resolveMacros($code) { . '$this->startAttributeStack[#1] + $this->endAttributes) : ' . $args[0] . ')'; } - if ('maybeMakeExprStmt' == $name) { - assertArgs(1, $args, $name); - - return '($this->useExpressionStatements ? new Stmt\Expression(' . $args[0] . ', ' - . '$this->startAttributeStack[#1] + $this->endAttributes) : ' . $args[0] . ')'; - } - return $matches[0]; }, $code diff --git a/lib/PhpParser/Parser/Php5.php b/lib/PhpParser/Parser/Php5.php index e85e968..48cfa52 100644 --- a/lib/PhpParser/Parser/Php5.php +++ b/lib/PhpParser/Parser/Php5.php @@ -1496,11 +1496,11 @@ class Php5 extends \PhpParser\ParserAbstract } protected function reduceRule146() { - $this->semValue = ($this->useExpressionStatements ? new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes) : $this->semStack[$this->stackPos-(2-1)]); + $this->semValue = new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes); } protected function reduceRule147() { - $this->semValue = ($this->useExpressionStatements ? new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes) : $this->semStack[$this->stackPos-(2-1)]); + $this->semValue = new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes); } protected function reduceRule148() { @@ -1536,7 +1536,7 @@ class Php5 extends \PhpParser\ParserAbstract } protected function reduceRule156() { - $this->semValue = ($this->useExpressionStatements ? new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes) : $this->semStack[$this->stackPos-(2-1)]); + $this->semValue = new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes); } protected function reduceRule157() { diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index 62e1b36..773ccd9 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -1387,7 +1387,7 @@ class Php7 extends \PhpParser\ParserAbstract } protected function reduceRule143() { - $this->semValue = ($this->useExpressionStatements ? new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes) : $this->semStack[$this->stackPos-(2-1)]); + $this->semValue = new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes); } protected function reduceRule144() { @@ -1423,7 +1423,7 @@ class Php7 extends \PhpParser\ParserAbstract } protected function reduceRule152() { - $this->semValue = ($this->useExpressionStatements ? new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes) : $this->semStack[$this->stackPos-(2-1)]); + $this->semValue = new Stmt\Expression($this->semStack[$this->stackPos-(2-1)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes); } protected function reduceRule153() { diff --git a/lib/PhpParser/ParserAbstract.php b/lib/PhpParser/ParserAbstract.php index 4eac350..fbc53fc 100644 --- a/lib/PhpParser/ParserAbstract.php +++ b/lib/PhpParser/ParserAbstract.php @@ -116,8 +116,6 @@ abstract class ParserAbstract implements Parser protected $useIdentifierNodes; /** @var bool Whether to consistently use Variable nodes */ protected $useConsistentVariableNodes; - /** @var bool Whether to use Stmt\Expr nodes */ - protected $useExpressionStatements; /** @var bool Whether to use Stmt\Nop nodes */ protected $useNopStatements; @@ -129,8 +127,6 @@ abstract class ParserAbstract implements Parser * Otherwise plain strings will be used. * * useConsistentVariableNodes (default false): Create Variable nodes in more places (like * function parameters, catch clause variables, etc.) - * * useExpressionStatements (default false): Create Stmt\Expression nodes for statements of - * type "expr;". Otherwise the expression is directly included in the statement list * * useNopStatements (default true): Create Stmt\Nop nodes for dangling comments at the end of * statement lists. * @@ -142,7 +138,6 @@ abstract class ParserAbstract implements Parser $this->errors = array(); $this->useIdentifierNodes = !empty($options['useIdentifierNodes']); $this->useConsistentVariableNodes = !empty($options['useConsistentVariableNodes']); - $this->useExpressionStatements = !empty($options['useExpressionStatements']); $this->useNopStatements = isset($options['useNopStatements']) ? $options['useNopStatements'] : true; diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 4904485..8404258 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -221,7 +221,7 @@ abstract class PrettyPrinterAbstract } } - $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : ''); + $result .= "\n" . $this->p($node); } if ($indent) { @@ -343,8 +343,8 @@ abstract class PrettyPrinterAbstract * * In order to use this method a number of prerequisites must be satisfied: * * The startTokenPos and endTokenPos attributes in the lexer must be enabled. - * * The parser must be run with the options useIdentifierNodes, useConsistentVariableNodes, - * useExpressionStatements ENABLED and useNopStatements DISABLED. + * * The parser must be run with the options useIdentifierNodes, useConsistentVariableNodes + * ENABLED and useNopStatements DISABLED. * * The CloningVisitor must be run on the AST prior to modification. * * The original tokens must be provided, using the getTokens() method on the lexer. * diff --git a/test/PhpParser/CodeParsingTest.php b/test/PhpParser/CodeParsingTest.php index ee5b488..881c54f 100644 --- a/test/PhpParser/CodeParsingTest.php +++ b/test/PhpParser/CodeParsingTest.php @@ -19,7 +19,6 @@ class CodeParsingTest extends CodeTestAbstract $parserOptions = [ 'useIdentifierNodes' => isset($modes['ident']), 'useConsistentVariableNodes' => isset($modes['consistentVars']), - 'useExpressionStatements' => isset($modes['exprStmts']), ]; $lexer = new Lexer\Emulative(array('usedAttributes' => array( diff --git a/test/PhpParser/NodeDumperTest.php b/test/PhpParser/NodeDumperTest.php index 9bb65a7..479c003 100644 --- a/test/PhpParser/NodeDumperTest.php +++ b/test/PhpParser/NodeDumperTest.php @@ -70,12 +70,14 @@ class NodeDumperTest extends \PHPUnit_Framework_TestCase $code = "traverse($stmts); $stmt = $stmts[0]; - $this->assertSame(array('Bar', 'Baz'), $stmt->stmts[1]->expr->class->parts); + $assign = $stmt->stmts[1]->expr; + $this->assertSame(array('Bar', 'Baz'), $assign->expr->class->parts); } public function testSpecialClassNamesAreCaseInsensitive() { @@ -442,9 +443,9 @@ EOC; $classStmt = $stmts[0]; $methodStmt = $classStmt->stmts[0]->stmts[0]; - $this->assertSame('SELF', (string)$methodStmt->stmts[0]->class); - $this->assertSame('PARENT', (string)$methodStmt->stmts[1]->class); - $this->assertSame('STATIC', (string)$methodStmt->stmts[2]->class); + $this->assertSame('SELF', (string)$methodStmt->stmts[0]->expr->class); + $this->assertSame('PARENT', (string)$methodStmt->stmts[1]->expr->class); + $this->assertSame('STATIC', (string)$methodStmt->stmts[2]->expr->class); } public function testAddOriginalNames() { diff --git a/test/PhpParser/Parser/MultipleTest.php b/test/PhpParser/Parser/MultipleTest.php index 01d3b60..dd4c02e 100644 --- a/test/PhpParser/Parser/MultipleTest.php +++ b/test/PhpParser/Parser/MultipleTest.php @@ -59,9 +59,9 @@ class MultipleTest extends ParserTest { 'getPrefer5(), [ - new Expr\Variable( + new Stmt\Expression(new Expr\Variable( new Expr\ArrayDimFetch(new Expr\Variable('a'), LNumber::fromString('0')) - ) + )) ] ], [ @@ -69,9 +69,9 @@ class MultipleTest extends ParserTest { 'getPrefer7(), [ - new Expr\ArrayDimFetch( + new Stmt\Expression(new Expr\ArrayDimFetch( new Expr\Variable(new Expr\Variable('a')), LNumber::fromString('0') - ) + )) ] ], ]; diff --git a/test/PhpParser/ParserTest.php b/test/PhpParser/ParserTest.php index 1295ae0..602f0b4 100644 --- a/test/PhpParser/ParserTest.php +++ b/test/PhpParser/ParserTest.php @@ -124,7 +124,8 @@ EOC; public function testExtraAttributes($code, $expectedAttributes) { $parser = $this->getParser(new Lexer); $stmts = $parser->parse("getAttributes(); + $node = $stmts[0] instanceof Node\Stmt\Expression ? $stmts[0]->expr : $stmts[0]; + $attributes = $node->getAttributes(); foreach ($expectedAttributes as $name => $value) { $this->assertSame($value, $attributes[$name]); } diff --git a/test/PhpParser/PrettyPrinterTest.php b/test/PhpParser/PrettyPrinterTest.php index 4f63048..6e8d3c0 100644 --- a/test/PhpParser/PrettyPrinterTest.php +++ b/test/PhpParser/PrettyPrinterTest.php @@ -24,7 +24,6 @@ class PrettyPrinterTest extends CodeTestAbstract $parser7 = new Parser\Php7($lexer, [ 'useIdentifierNodes' => true, 'useConsistentVariableNodes' => true, - 'useExpressionStatements' => true, ]); list($version, $options) = $this->parseModeLine($modeLine); @@ -206,7 +205,6 @@ class PrettyPrinterTest extends CodeTestAbstract $parser = new Parser\Php7($lexer, [ 'useIdentifierNodes' => true, 'useConsistentVariableNodes' => true, - 'useExpressionStatements' => true, 'useNopStatements' => false, ]); @@ -268,7 +266,6 @@ CODE $parser = new $parserClass($lexer, [ 'useIdentifierNodes' => true, 'useConsistentVariableNodes' => true, - 'useExpressionStatements' => true, 'useNopStatements' => false, ]);