mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-07-14 10:56:55 +02:00
Compare commits
64 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb87e28e7d | |||
1c3eabb000 | |||
08215e7646 | |||
e57b3a0978 | |||
d01fafcb40 | |||
67df02c844 | |||
94c715d97e | |||
579f4ce846 | |||
94ca9a7ab9 | |||
bac91b426e | |||
08131e7ff2 | |||
0ba710affa | |||
72231abe6d | |||
d418bf3951 | |||
5a9fbca54a | |||
a1e8e1a30e | |||
d77e6cd6e9 | |||
a10780ca0d | |||
4d4896e553 | |||
6fa073879e | |||
5a3a1ec25c | |||
4a7d011317 | |||
7f862ac21c | |||
0808939f81 | |||
7bfc320bda | |||
bc0bff3f87 | |||
c28b8556f5 | |||
3da86df48f | |||
901b895c02 | |||
c877c1a64f | |||
c3cbf07946 | |||
2b9e2f71b7 | |||
d5873b177b | |||
48ec654d0c | |||
c12a4c8239 | |||
86ea6fe8c4 | |||
1b59e918f7 | |||
0bf561dfe7 | |||
df98b0417b | |||
a8eb2fc675 | |||
d18ccfeec7 | |||
4e55897059 | |||
41facc02ef | |||
62877b5d14 | |||
5b8182cc0a | |||
af8b17bd75 | |||
42f046ec19 | |||
d287c167bc | |||
58e7881e98 | |||
fd7ac25108 | |||
ba57202ed7 | |||
3e8c8d248d | |||
301c34373d | |||
58970e2a37 | |||
c1e0bab4f8 | |||
d5eebf7214 | |||
70319e27ee | |||
8a97065e30 | |||
adf44419c0 | |||
5219f75719 | |||
a485ecd7ba | |||
030de805e1 | |||
aa6aec90e1 | |||
3e158a2313 |
@ -1,5 +1,5 @@
|
||||
language: php
|
||||
|
||||
dist: trusty
|
||||
sudo: false
|
||||
|
||||
cache:
|
||||
|
148
CHANGELOG.md
148
CHANGELOG.md
@ -1,8 +1,154 @@
|
||||
Version 3.0.1-dev
|
||||
Version 3.1.6-dev
|
||||
-----------------
|
||||
|
||||
Nothing yet.
|
||||
|
||||
Version 3.1.5 (2018-02-28)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed duplicate comment assignment in switch statements. (#469)
|
||||
* Improve compatibility with PHP-Scoper. (#477)
|
||||
|
||||
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)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed syntax error on comment after brace-style namespace declaration. (#412)
|
||||
* Added support for TraitUse statements in trait builder. (#413)
|
||||
|
||||
Version 3.1.0 (2017-07-28)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.2] Added support for trailing comma in group use statements.
|
||||
* [PHP 7.2] Added support for `object` type. This means `object` types will now be represented as a
|
||||
builtin type (a simple `"object"` string), rather than a class `Name`.
|
||||
|
||||
### Fixed
|
||||
|
||||
* Floating-point numbers are now printed correctly if the LC_NUMERIC locale uses a comma as decimal
|
||||
separator.
|
||||
|
||||
### Changed
|
||||
|
||||
* `Name::$parts` is no longer deprecated.
|
||||
|
||||
Version 3.0.6 (2017-06-28)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed the spelling of `Class_::VISIBILITY_MODIFIER_MASK`. The previous spelling of
|
||||
`Class_::VISIBILITY_MODIFER_MASK` is preserved for backwards compatibility.
|
||||
* The pretty printing will now preserve comments inside array literals and function calls by
|
||||
printing the array items / function arguments on separate lines. Array literals and functions that
|
||||
do not contain comments are not affected.
|
||||
|
||||
### Added
|
||||
|
||||
* Added `Builder\Param::makeVariadic()`.
|
||||
|
||||
### Deprecated
|
||||
|
||||
* The `Node::setLine()` method has been deprecated.
|
||||
|
||||
Version 3.0.5 (2017-03-05)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Name resolution of `NullableType`s is now performed earlier, so that a fully resolved signature is
|
||||
available when a function is entered. (#360)
|
||||
* `Error` nodes are now considered empty, while previously they extended until the token where the
|
||||
error occurred. This made some nodes larger than expected. (#359)
|
||||
* Fixed notices being thrown during error recovery in some situations. (#362)
|
||||
|
||||
Version 3.0.4 (2017-02-10)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed some extensibility issues in pretty printer (`pUseType()` is now public and `pPrec()` calls
|
||||
into `p()`, instead of directly dispatching to the type-specific printing method).
|
||||
* Fixed notice in `bin/php-parse` script.
|
||||
|
||||
### Added
|
||||
|
||||
* Error recovery from missing semicolons is now supported in more cases.
|
||||
* Error recovery from trailing commas in positions where PHP does not support them is now supported.
|
||||
|
||||
Version 3.0.3 (2017-02-03)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* In `"$foo[0]"` the `0` is now parsed as an `LNumber` rather than `String`. (#325)
|
||||
* Ensure integers and floats are always pretty printed preserving semantics, even if the particular
|
||||
value can only be manually constructed.
|
||||
* Throw a `LogicException` when trying to pretty-print an `Error` node. Previously this resulted in
|
||||
an undefined method exception or fatal error.
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.1] Added support for negative interpolated offsets: `"$foo[-1]"`
|
||||
* Added `preserveOriginalNames` option to `NameResolver`. If this option is enabled, an
|
||||
`originalName` attribute, containing the unresolved name, will be added to each resolved name.
|
||||
* Added `php-parse --with-positions` option, which dumps nodes with position information.
|
||||
|
||||
### Deprecated
|
||||
|
||||
* The XML serializer has been deprecated. In particular, the classes `Serializer\XML`,
|
||||
`Unserializer\XML`, as well as the interfaces `Serializer` and `Unserializer` are deprecated.
|
||||
|
||||
Version 3.0.2 (2016-12-06)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed name resolution of nullable types. (#324)
|
||||
* Fixed pretty-printing of nullable types.
|
||||
|
||||
Version 3.0.1 (2016-12-01)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed handling of nested `list()`s: If the nested list was unkeyed, it was directly included in
|
||||
the list items. If it was keyed, it was wrapped in `ArrayItem`. Now nested `List_` nodes are
|
||||
always wrapped in `ArrayItem`s. (#321)
|
||||
|
||||
Version 3.0.0 (2016-11-30)
|
||||
--------------------------
|
||||
|
||||
|
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.
|
||||
|
@ -144,6 +144,10 @@ The following methods, arguments or options have been removed:
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
* The `NameResolver` will now resolve unqualified function and constant names in the global
|
||||
namespace into fully qualified names. For example `foo()` in the global namespace resolves to
|
||||
`\foo()`. For names where no static resolution is possible, a `namespacedName` attribute is
|
||||
added now, containing the namespaced variant of the name.
|
||||
* All methods on `PrettyPrinter\Standard` are now protected. Previoulsy most of them were public.
|
||||
The pretty printer should only be invoked using the `prettyPrint()`, `prettyPrintFile()` and
|
||||
`prettyPrintExpr()` methods.
|
||||
|
@ -33,7 +33,10 @@ $parser = (new PhpParser\ParserFactory)->create(
|
||||
PhpParser\ParserFactory::PREFER_PHP7,
|
||||
$lexer
|
||||
);
|
||||
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
|
||||
$dumper = new PhpParser\NodeDumper([
|
||||
'dumpComments' => true,
|
||||
'dumpPositions' => $attributes['with-positions'],
|
||||
]);
|
||||
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
||||
$serializer = new PhpParser\Serializer\XML;
|
||||
|
||||
@ -75,7 +78,7 @@ foreach ($files as $file) {
|
||||
foreach ($operations as $operation) {
|
||||
if ('dump' === $operation) {
|
||||
echo "==> Node dump:\n";
|
||||
echo $dumper->dump($stmts), "\n";
|
||||
echo $dumper->dump($stmts, $code), "\n";
|
||||
} elseif ('pretty-print' === $operation) {
|
||||
echo "==> Pretty print:\n";
|
||||
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
|
||||
@ -117,6 +120,7 @@ Operations is a list of the following options (--dump by default):
|
||||
--var-dump var_dump() nodes (for exact structure)
|
||||
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
|
||||
-c, --with-column-info Show column-numbers for errors (if available)
|
||||
-P, --with-positions Show positions in node dumps
|
||||
-r, --with-recovery Use parsing with error recovery
|
||||
-h, --help Display this page
|
||||
|
||||
@ -134,7 +138,8 @@ function parseArgs($args) {
|
||||
$operations = array();
|
||||
$files = array();
|
||||
$attributes = array(
|
||||
'with-column-info' => false,
|
||||
'with-column-info' => false,
|
||||
'with-positions' => false,
|
||||
'with-recovery' => false,
|
||||
);
|
||||
|
||||
@ -169,6 +174,10 @@ function parseArgs($args) {
|
||||
case '-c';
|
||||
$attributes['with-column-info'] = true;
|
||||
break;
|
||||
case '--with-positions':
|
||||
case '-P':
|
||||
$attributes['with-positions'] = true;
|
||||
break;
|
||||
case '--with-recovery':
|
||||
case '-r':
|
||||
$attributes['with-recovery'] = true;
|
||||
|
@ -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 ';'
|
||||
@ -253,22 +267,22 @@ class_entry_type:
|
||||
|
||||
extends_from:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_EXTENDS name { $$ = $2; }
|
||||
| T_EXTENDS class_name { $$ = $2; }
|
||||
;
|
||||
|
||||
interface_extends_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_EXTENDS name_list { $$ = $2; }
|
||||
| T_EXTENDS class_name_list { $$ = $2; }
|
||||
;
|
||||
|
||||
implements_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_IMPLEMENTS name_list { $$ = $2; }
|
||||
| T_IMPLEMENTS class_name_list { $$ = $2; }
|
||||
;
|
||||
|
||||
name_list:
|
||||
name { init($1); }
|
||||
| name_list ',' name { push($1, $3); }
|
||||
class_name_list:
|
||||
class_name { init($1); }
|
||||
| class_name_list ',' class_name { push($1, $3); }
|
||||
;
|
||||
|
||||
for_statement:
|
||||
@ -309,8 +323,8 @@ case_list:
|
||||
;
|
||||
|
||||
case:
|
||||
T_CASE expr case_separator inner_statement_list { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list { $$ = Stmt\Case_[null, $3]; }
|
||||
T_CASE expr case_separator inner_statement_list_ex { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list_ex { $$ = Stmt\Case_[null, $3]; }
|
||||
;
|
||||
|
||||
case_separator:
|
||||
@ -440,7 +454,7 @@ class_statement:
|
||||
| method_modifiers T_FUNCTION optional_ref identifier '(' parameter_list ')' optional_return_type method_body
|
||||
{ $$ = Stmt\ClassMethod[$4, ['type' => $1, 'byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9]];
|
||||
$this->checkClassMethod($$, #1); }
|
||||
| T_USE name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
||||
| T_USE class_name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
||||
;
|
||||
|
||||
trait_adaptations:
|
||||
@ -454,7 +468,7 @@ trait_adaptation_list:
|
||||
;
|
||||
|
||||
trait_adaptation:
|
||||
trait_method_reference_fully_qualified T_INSTEADOF name_list ';'
|
||||
trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
|
||||
{ $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
|
||||
| trait_method_reference T_AS member_modifier identifier ';'
|
||||
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
|
||||
@ -939,7 +953,7 @@ list_expr_elements:
|
||||
|
||||
list_expr_element:
|
||||
variable { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| list_expr { $$ = $1; }
|
||||
| list_expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| /* empty */ { $$ = null; }
|
||||
;
|
||||
|
||||
@ -988,7 +1002,7 @@ encaps_var:
|
||||
|
||||
encaps_var_offset:
|
||||
T_STRING { $$ = Scalar\String_[$1]; }
|
||||
| T_NUM_STRING { $$ = Scalar\String_[$1]; }
|
||||
| T_NUM_STRING { $$ = $this->parseNumString($1, attributes()); }
|
||||
| T_VARIABLE { $$ = Expr\Variable[parseVar($1)]; }
|
||||
;
|
||||
|
||||
|
177
grammar/php7.y
177
grammar/php7.y
@ -49,22 +49,42 @@ namespace_name:
|
||||
namespace_name_parts { $$ = Name[$1]; }
|
||||
;
|
||||
|
||||
semi:
|
||||
';' { /* nothing */ }
|
||||
| error { /* nothing */ }
|
||||
;
|
||||
|
||||
no_comma:
|
||||
/* empty */ { /* nothing */ }
|
||||
| ',' { $this->emitError(new Error('A trailing comma is not allowed here', attributes())); }
|
||||
;
|
||||
|
||||
optional_comma:
|
||||
/* empty */
|
||||
| ','
|
||||
|
||||
top_statement:
|
||||
statement { $$ = $1; }
|
||||
| function_declaration_statement { $$ = $1; }
|
||||
| class_declaration_statement { $$ = $1; }
|
||||
| T_HALT_COMPILER
|
||||
{ $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; }
|
||||
| T_NAMESPACE namespace_name ';'
|
||||
{ $$ = Stmt\Namespace_[$2, null]; $this->checkNamespace($$); }
|
||||
| T_NAMESPACE namespace_name semi
|
||||
{ $$ = 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($$); }
|
||||
| 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; }
|
||||
| T_CONST constant_declaration_list ';' { $$ = Stmt\Const_[$2]; }
|
||||
{ $$ = 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; }
|
||||
| T_CONST constant_declaration_list semi { $$ = Stmt\Const_[$2]; }
|
||||
;
|
||||
|
||||
use_type:
|
||||
@ -85,18 +105,31 @@ group_use_declaration:
|
||||
;
|
||||
|
||||
unprefixed_use_declarations:
|
||||
unprefixed_use_declarations ',' unprefixed_use_declaration
|
||||
non_empty_unprefixed_use_declarations optional_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_unprefixed_use_declarations:
|
||||
non_empty_unprefixed_use_declarations ',' unprefixed_use_declaration
|
||||
{ push($1, $3); }
|
||||
| unprefixed_use_declaration { init($1); }
|
||||
;
|
||||
|
||||
use_declarations:
|
||||
use_declarations ',' use_declaration { push($1, $3); }
|
||||
non_empty_use_declarations no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_use_declarations:
|
||||
non_empty_use_declarations ',' use_declaration { push($1, $3); }
|
||||
| use_declaration { init($1); }
|
||||
;
|
||||
|
||||
inline_use_declarations:
|
||||
inline_use_declarations ',' inline_use_declaration { push($1, $3); }
|
||||
non_empty_inline_use_declarations optional_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_inline_use_declarations:
|
||||
non_empty_inline_use_declarations ',' inline_use_declaration
|
||||
{ push($1, $3); }
|
||||
| inline_use_declaration { init($1); }
|
||||
;
|
||||
|
||||
@ -118,7 +151,12 @@ inline_use_declaration:
|
||||
;
|
||||
|
||||
constant_declaration_list:
|
||||
constant_declaration_list ',' constant_declaration { push($1, $3); }
|
||||
non_empty_constant_declaration_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_constant_declaration_list:
|
||||
non_empty_constant_declaration_list ',' constant_declaration
|
||||
{ push($1, $3); }
|
||||
| constant_declaration { init($1); }
|
||||
;
|
||||
|
||||
@ -127,7 +165,11 @@ constant_declaration:
|
||||
;
|
||||
|
||||
class_const_list:
|
||||
class_const_list ',' class_const { push($1, $3); }
|
||||
non_empty_class_const_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_class_const_list:
|
||||
non_empty_class_const_list ',' class_const { push($1, $3); }
|
||||
| class_const { init($1); }
|
||||
;
|
||||
|
||||
@ -155,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 ';'
|
||||
@ -165,15 +215,15 @@ non_empty_statement:
|
||||
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
|
||||
{ $$ = Stmt\For_[['init' => $3, 'cond' => $5, 'loop' => $7, 'stmts' => $9]]; }
|
||||
| T_SWITCH '(' expr ')' switch_case_list { $$ = Stmt\Switch_[$3, $5]; }
|
||||
| T_BREAK optional_expr ';' { $$ = Stmt\Break_[$2]; }
|
||||
| T_CONTINUE optional_expr ';' { $$ = Stmt\Continue_[$2]; }
|
||||
| T_RETURN optional_expr ';' { $$ = Stmt\Return_[$2]; }
|
||||
| T_GLOBAL global_var_list ';' { $$ = Stmt\Global_[$2]; }
|
||||
| T_STATIC static_var_list ';' { $$ = Stmt\Static_[$2]; }
|
||||
| T_ECHO expr_list ';' { $$ = Stmt\Echo_[$2]; }
|
||||
| T_BREAK optional_expr semi { $$ = Stmt\Break_[$2]; }
|
||||
| T_CONTINUE optional_expr semi { $$ = Stmt\Continue_[$2]; }
|
||||
| T_RETURN optional_expr semi { $$ = Stmt\Return_[$2]; }
|
||||
| T_GLOBAL global_var_list semi { $$ = Stmt\Global_[$2]; }
|
||||
| T_STATIC static_var_list semi { $$ = Stmt\Static_[$2]; }
|
||||
| T_ECHO expr_list semi { $$ = Stmt\Echo_[$2]; }
|
||||
| T_INLINE_HTML { $$ = Stmt\InlineHTML[$1]; }
|
||||
| expr ';' { $$ = $1; }
|
||||
| T_UNSET '(' variables_list ')' ';' { $$ = Stmt\Unset_[$3]; }
|
||||
| expr semi { $$ = $1; }
|
||||
| T_UNSET '(' variables_list ')' semi { $$ = Stmt\Unset_[$3]; }
|
||||
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
|
||||
{ $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; }
|
||||
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
|
||||
@ -181,10 +231,9 @@ non_empty_statement:
|
||||
| T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt\Declare_[$3, $5]; }
|
||||
| T_TRY '{' inner_statement_list '}' catches optional_finally
|
||||
{ $$ = Stmt\TryCatch[$3, $5, $6]; $this->checkTryCatch($$); }
|
||||
| T_THROW expr ';' { $$ = Stmt\Throw_[$2]; }
|
||||
| T_GOTO T_STRING ';' { $$ = Stmt\Goto_[$2]; }
|
||||
| T_THROW expr semi { $$ = Stmt\Throw_[$2]; }
|
||||
| T_GOTO T_STRING semi { $$ = Stmt\Goto_[$2]; }
|
||||
| T_STRING ':' { $$ = Stmt\Label[$1]; }
|
||||
| expr error { $$ = $1; }
|
||||
| error { $$ = array(); /* means: no statement */ }
|
||||
;
|
||||
|
||||
@ -216,8 +265,12 @@ optional_finally:
|
||||
;
|
||||
|
||||
variables_list:
|
||||
non_empty_variables_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_variables_list:
|
||||
variable { init($1); }
|
||||
| variables_list ',' variable { push($1, $3); }
|
||||
| non_empty_variables_list ',' variable { push($1, $3); }
|
||||
;
|
||||
|
||||
optional_ref:
|
||||
@ -254,22 +307,26 @@ class_entry_type:
|
||||
|
||||
extends_from:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_EXTENDS name { $$ = $2; }
|
||||
| T_EXTENDS class_name { $$ = $2; }
|
||||
;
|
||||
|
||||
interface_extends_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_EXTENDS name_list { $$ = $2; }
|
||||
| T_EXTENDS class_name_list { $$ = $2; }
|
||||
;
|
||||
|
||||
implements_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_IMPLEMENTS name_list { $$ = $2; }
|
||||
| T_IMPLEMENTS class_name_list { $$ = $2; }
|
||||
;
|
||||
|
||||
name_list:
|
||||
name { init($1); }
|
||||
| name_list ',' name { push($1, $3); }
|
||||
class_name_list:
|
||||
non_empty_class_name_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_class_name_list:
|
||||
class_name { init($1); }
|
||||
| non_empty_class_name_list ',' class_name { push($1, $3); }
|
||||
;
|
||||
|
||||
for_statement:
|
||||
@ -289,8 +346,12 @@ declare_statement:
|
||||
;
|
||||
|
||||
declare_list:
|
||||
non_empty_declare_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_declare_list:
|
||||
declare_list_element { init($1); }
|
||||
| declare_list ',' declare_list_element { push($1, $3); }
|
||||
| non_empty_declare_list ',' declare_list_element { push($1, $3); }
|
||||
;
|
||||
|
||||
declare_list_element:
|
||||
@ -310,8 +371,8 @@ case_list:
|
||||
;
|
||||
|
||||
case:
|
||||
T_CASE expr case_separator inner_statement_list { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list { $$ = Stmt\Case_[null, $3]; }
|
||||
T_CASE expr case_separator inner_statement_list_ex { $$ = Stmt\Case_[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list_ex { $$ = Stmt\Case_[null, $3]; }
|
||||
;
|
||||
|
||||
case_separator:
|
||||
@ -360,7 +421,7 @@ foreach_variable:
|
||||
;
|
||||
|
||||
parameter_list:
|
||||
non_empty_parameter_list { $$ = $1; }
|
||||
non_empty_parameter_list no_comma { $$ = $1; }
|
||||
| /* empty */ { $$ = array(); }
|
||||
;
|
||||
|
||||
@ -399,7 +460,7 @@ optional_return_type:
|
||||
|
||||
argument_list:
|
||||
'(' ')' { $$ = array(); }
|
||||
| '(' non_empty_argument_list ')' { $$ = $2; }
|
||||
| '(' non_empty_argument_list no_comma ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
non_empty_argument_list:
|
||||
@ -414,7 +475,11 @@ argument:
|
||||
;
|
||||
|
||||
global_var_list:
|
||||
global_var_list ',' global_var { push($1, $3); }
|
||||
non_empty_global_var_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_global_var_list:
|
||||
non_empty_global_var_list ',' global_var { push($1, $3); }
|
||||
| global_var { init($1); }
|
||||
;
|
||||
|
||||
@ -423,7 +488,11 @@ global_var:
|
||||
;
|
||||
|
||||
static_var_list:
|
||||
static_var_list ',' static_var { push($1, $3); }
|
||||
non_empty_static_var_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_static_var_list:
|
||||
non_empty_static_var_list ',' static_var { push($1, $3); }
|
||||
| static_var { init($1); }
|
||||
;
|
||||
|
||||
@ -445,7 +514,7 @@ class_statement:
|
||||
| method_modifiers T_FUNCTION optional_ref identifier '(' parameter_list ')' optional_return_type method_body
|
||||
{ $$ = Stmt\ClassMethod[$4, ['type' => $1, 'byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9]];
|
||||
$this->checkClassMethod($$, #1); }
|
||||
| T_USE name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
||||
| T_USE class_name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
||||
;
|
||||
|
||||
trait_adaptations:
|
||||
@ -459,7 +528,7 @@ trait_adaptation_list:
|
||||
;
|
||||
|
||||
trait_adaptation:
|
||||
trait_method_reference_fully_qualified T_INSTEADOF name_list ';'
|
||||
trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
|
||||
{ $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
|
||||
| trait_method_reference T_AS member_modifier identifier ';'
|
||||
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
|
||||
@ -509,8 +578,13 @@ member_modifier:
|
||||
;
|
||||
|
||||
property_declaration_list:
|
||||
non_empty_property_declaration_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_property_declaration_list:
|
||||
property_declaration { init($1); }
|
||||
| property_declaration_list ',' property_declaration { push($1, $3); }
|
||||
| non_empty_property_declaration_list ',' property_declaration
|
||||
{ push($1, $3); }
|
||||
;
|
||||
|
||||
property_declaration:
|
||||
@ -519,7 +593,11 @@ property_declaration:
|
||||
;
|
||||
|
||||
expr_list:
|
||||
expr_list ',' expr { push($1, $3); }
|
||||
non_empty_expr_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_expr_list:
|
||||
non_empty_expr_list ',' expr { push($1, $3); }
|
||||
| expr { init($1); }
|
||||
;
|
||||
|
||||
@ -638,8 +716,12 @@ lexical_vars:
|
||||
;
|
||||
|
||||
lexical_var_list:
|
||||
non_empty_lexical_var_list no_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_lexical_var_list:
|
||||
lexical_var { init($1); }
|
||||
| lexical_var_list ',' lexical_var { push($1, $3); }
|
||||
| non_empty_lexical_var_list ',' lexical_var { push($1, $3); }
|
||||
;
|
||||
|
||||
lexical_var:
|
||||
@ -699,7 +781,7 @@ constant:
|
||||
/* We interpret and isolated FOO:: as an unfinished class constant fetch. It could also be
|
||||
an unfinished static property fetch or unfinished scoped call. */
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM error
|
||||
{ $$ = Expr\ClassConstFetch[$1, Expr\Error[]]; $this->errorState = 2; }
|
||||
{ $$ = Expr\ClassConstFetch[$1, new Expr\Error(stackAttributes(#3))]; $this->errorState = 2; }
|
||||
;
|
||||
|
||||
array_short_syntax:
|
||||
@ -823,7 +905,7 @@ list_expr_elements:
|
||||
|
||||
list_expr_element:
|
||||
variable { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| list_expr { $$ = $1; }
|
||||
| list_expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
||||
| expr T_DOUBLE_ARROW variable { $$ = Expr\ArrayItem[$3, $1, false]; }
|
||||
| expr T_DOUBLE_ARROW list_expr { $$ = Expr\ArrayItem[$3, $1, false]; }
|
||||
| /* empty */ { $$ = null; }
|
||||
@ -875,7 +957,8 @@ encaps_var:
|
||||
|
||||
encaps_var_offset:
|
||||
T_STRING { $$ = Scalar\String_[$1]; }
|
||||
| T_NUM_STRING { $$ = Scalar\String_[$1]; }
|
||||
| T_NUM_STRING { $$ = $this->parseNumString($1, attributes()); }
|
||||
| '-' T_NUM_STRING { $$ = $this->parseNumString('-' . $2, attributes()); }
|
||||
| T_VARIABLE { $$ = Expr\Variable[parseVar($1)]; }
|
||||
;
|
||||
|
||||
|
@ -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\', []))); }';
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ abstract class FunctionLike extends Declaration
|
||||
{
|
||||
protected $returnByRef = false;
|
||||
protected $params = array();
|
||||
|
||||
/** @var string|Node\Name|Node\NullableType|null */
|
||||
protected $returnType = null;
|
||||
|
||||
/**
|
||||
@ -59,7 +61,7 @@ abstract class FunctionLike extends Declaration
|
||||
/**
|
||||
* Sets the return type for PHP 7.
|
||||
*
|
||||
* @param string|Node\Name $type One of array, callable, string, int, float, bool, iterable,
|
||||
* @param string|Node\Name|Node\NullableType $type One of array, callable, string, int, float, bool, iterable,
|
||||
* or a class/interface name.
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
|
@ -10,6 +10,8 @@ class Method extends FunctionLike
|
||||
{
|
||||
protected $name;
|
||||
protected $flags = 0;
|
||||
|
||||
/** @var array|null */
|
||||
protected $stmts = array();
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,14 @@ class Param extends PhpParser\BuilderAbstract
|
||||
protected $name;
|
||||
|
||||
protected $default = null;
|
||||
|
||||
/** @var string|Node\Name|Node\NullableType|null */
|
||||
protected $type = null;
|
||||
|
||||
protected $byRef = false;
|
||||
|
||||
protected $variadic = false;
|
||||
|
||||
/**
|
||||
* Creates a parameter builder.
|
||||
*
|
||||
@ -38,7 +43,7 @@ class Param extends PhpParser\BuilderAbstract
|
||||
/**
|
||||
* Sets type hint for the parameter.
|
||||
*
|
||||
* @param string|Node\Name $type Type hint to use
|
||||
* @param string|Node\Name|Node\NullableType $type Type hint to use
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
@ -62,6 +67,17 @@ class Param extends PhpParser\BuilderAbstract
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the parameter variadic
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeVariadic() {
|
||||
$this->variadic = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built parameter node.
|
||||
*
|
||||
@ -69,7 +85,7 @@ class Param extends PhpParser\BuilderAbstract
|
||||
*/
|
||||
public function getNode() {
|
||||
return new Node\Param(
|
||||
$this->name, $this->default, $this->type, $this->byRef
|
||||
$this->name, $this->default, $this->type, $this->byRef, $this->variadic
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node\Stmt;
|
||||
class Trait_ extends Declaration
|
||||
{
|
||||
protected $name;
|
||||
protected $uses = array();
|
||||
protected $properties = array();
|
||||
protected $methods = array();
|
||||
|
||||
@ -34,6 +35,8 @@ class Trait_ extends Declaration
|
||||
$this->properties[] = $stmt;
|
||||
} else if ($stmt instanceof Stmt\ClassMethod) {
|
||||
$this->methods[] = $stmt;
|
||||
} else if ($stmt instanceof Stmt\TraitUse) {
|
||||
$this->uses[] = $stmt;
|
||||
} else {
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||
}
|
||||
@ -49,7 +52,7 @@ class Trait_ extends Declaration
|
||||
public function getNode() {
|
||||
return new Stmt\Trait_(
|
||||
$this->name, array(
|
||||
'stmts' => array_merge($this->properties, $this->methods)
|
||||
'stmts' => array_merge($this->uses, $this->properties, $this->methods)
|
||||
), $this->attributes
|
||||
);
|
||||
}
|
||||
|
@ -60,9 +60,9 @@ abstract class BuilderAbstract implements Builder {
|
||||
* In particular, builtin types are left as strings, custom types become Names and nullables
|
||||
* are wrapped in NullableType nodes.
|
||||
*
|
||||
* @param Name|string $type The type to normalize
|
||||
* @param Name|string|NullableType $type The type to normalize
|
||||
*
|
||||
* @return Name|string The normalized type
|
||||
* @return Name|string|NullableType The normalized type
|
||||
*/
|
||||
protected function normalizeType($type) {
|
||||
if (!is_string($type)) {
|
||||
@ -80,7 +80,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
}
|
||||
|
||||
$builtinTypes = array(
|
||||
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void'
|
||||
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object'
|
||||
);
|
||||
|
||||
$lowerType = strtolower($type);
|
||||
|
@ -185,15 +185,17 @@ class Lexer
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for unterminated comment
|
||||
$lastToken = $this->tokens[count($this->tokens) - 1];
|
||||
if ($this->isUnterminatedComment($lastToken)) {
|
||||
$errorHandler->handleError(new Error('Unterminated comment', [
|
||||
'startLine' => $line - substr_count($lastToken[1], "\n"),
|
||||
'endLine' => $line,
|
||||
'startFilePos' => $filePos - \strlen($lastToken[1]),
|
||||
'endFilePos' => $filePos,
|
||||
]));
|
||||
if (count($this->tokens) > 0) {
|
||||
// Check for unterminated comment
|
||||
$lastToken = $this->tokens[count($this->tokens) - 1];
|
||||
if ($this->isUnterminatedComment($lastToken)) {
|
||||
$errorHandler->handleError(new Error('Unterminated comment', [
|
||||
'startLine' => $line - substr_count($lastToken[1], "\n"),
|
||||
'endLine' => $line,
|
||||
'startFilePos' => $filePos - \strlen($lastToken[1]),
|
||||
'endFilePos' => $filePos,
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -358,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);
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ interface Node
|
||||
* Sets line the node started in.
|
||||
*
|
||||
* @param int $line Line
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function setLine($line);
|
||||
|
||||
|
@ -24,7 +24,7 @@ class ArrayDimFetch extends Expr
|
||||
$this->dim = $dim;
|
||||
}
|
||||
|
||||
public function getSubnodeNames() {
|
||||
public function getSubNodeNames() {
|
||||
return array('var', 'dim');
|
||||
}
|
||||
}
|
||||
|
@ -9,15 +9,15 @@ class ClassConstFetch extends Expr
|
||||
{
|
||||
/** @var Name|Expr Class name */
|
||||
public $class;
|
||||
/** @var string Constant name */
|
||||
/** @var string|Error Constant name */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* Constructs a class const fetch node.
|
||||
*
|
||||
* @param Name|Expr $class Class name
|
||||
* @param string $name Constant name
|
||||
* @param array $attributes Additional attributes
|
||||
* @param Name|Expr $class Class name
|
||||
* @param string|Error $name Constant name
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct($class, $name, array $attributes = array()) {
|
||||
parent::__construct($attributes);
|
||||
|
@ -16,7 +16,7 @@ class Closure extends Expr implements FunctionLike
|
||||
public $params;
|
||||
/** @var ClosureUse[] use()s */
|
||||
public $uses;
|
||||
/** @var null|string|Node\Name Return type */
|
||||
/** @var null|string|Node\Name|Node\NullableType Return type */
|
||||
public $returnType;
|
||||
/** @var Node[] Statements */
|
||||
public $stmts;
|
||||
|
@ -23,7 +23,7 @@ interface FunctionLike extends Node
|
||||
/**
|
||||
* Get the declared return type or null
|
||||
*
|
||||
* @return null|string|Node\Name
|
||||
* @return null|string|Node\Name|Node\NullableType
|
||||
*/
|
||||
public function getReturnType();
|
||||
|
||||
|
@ -8,7 +8,6 @@ class Name extends NodeAbstract
|
||||
{
|
||||
/**
|
||||
* @var string[] Parts of the name
|
||||
* @deprecated Avoid directly accessing $parts, use methods instead.
|
||||
*/
|
||||
public $parts;
|
||||
|
||||
|
@ -6,7 +6,7 @@ use PhpParser\NodeAbstract;
|
||||
|
||||
class Param extends NodeAbstract
|
||||
{
|
||||
/** @var null|string|Name Typehint */
|
||||
/** @var null|string|Name|NullableType Typehint */
|
||||
public $type;
|
||||
/** @var bool Whether parameter is passed by reference */
|
||||
public $byRef;
|
||||
@ -20,12 +20,12 @@ class Param extends NodeAbstract
|
||||
/**
|
||||
* Constructs a parameter node.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param null|Expr $default Default value
|
||||
* @param null|string|Name $type Typehint
|
||||
* @param bool $byRef Whether is passed by reference
|
||||
* @param bool $variadic Whether this is a variadic argument
|
||||
* @param array $attributes Additional attributes
|
||||
* @param string $name Name
|
||||
* @param null|Expr $default Default value
|
||||
* @param null|string|Name|NullableType $type Typehint
|
||||
* @param bool $byRef Whether is passed by reference
|
||||
* @param bool $variadic Whether this is a variadic argument
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct($name, Expr $default = null, $type = null, $byRef = false, $variadic = false, array $attributes = array()) {
|
||||
parent::__construct($attributes);
|
||||
|
@ -30,7 +30,7 @@ class ClassConst extends Node\Stmt
|
||||
|
||||
public function isPublic() {
|
||||
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|
||||
|| ($this->flags & Class_::VISIBILITY_MODIFER_MASK) === 0;
|
||||
|| ($this->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0;
|
||||
}
|
||||
|
||||
public function isProtected() {
|
||||
|
@ -15,9 +15,9 @@ class ClassMethod extends Node\Stmt implements FunctionLike
|
||||
public $name;
|
||||
/** @var Node\Param[] Parameters */
|
||||
public $params;
|
||||
/** @var null|string|Node\Name Return type */
|
||||
/** @var null|string|Node\Name|Node\NullableType Return type */
|
||||
public $returnType;
|
||||
/** @var Node[] Statements */
|
||||
/** @var Node[]|null Statements */
|
||||
public $stmts;
|
||||
|
||||
/** @deprecated Use $flags instead */
|
||||
@ -69,7 +69,7 @@ class ClassMethod extends Node\Stmt implements FunctionLike
|
||||
|
||||
public function isPublic() {
|
||||
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|
||||
|| ($this->flags & Class_::VISIBILITY_MODIFER_MASK) === 0;
|
||||
|| ($this->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0;
|
||||
}
|
||||
|
||||
public function isProtected() {
|
||||
|
@ -14,7 +14,9 @@ class Class_ extends ClassLike
|
||||
const MODIFIER_ABSTRACT = 16;
|
||||
const MODIFIER_FINAL = 32;
|
||||
|
||||
const VISIBILITY_MODIFER_MASK = 7; // 1 | 2 | 4
|
||||
const VISIBILITY_MODIFIER_MASK = 7; // 1 | 2 | 4
|
||||
/** @deprecated */
|
||||
const VISIBILITY_MODIFER_MASK = self::VISIBILITY_MODIFIER_MASK;
|
||||
|
||||
/** @var int Type */
|
||||
public $flags;
|
||||
@ -74,7 +76,7 @@ class Class_ extends ClassLike
|
||||
* @internal
|
||||
*/
|
||||
public static function verifyModifier($a, $b) {
|
||||
if ($a & self::VISIBILITY_MODIFER_MASK && $b & self::VISIBILITY_MODIFER_MASK) {
|
||||
if ($a & self::VISIBILITY_MODIFIER_MASK && $b & self::VISIBILITY_MODIFIER_MASK) {
|
||||
throw new Error('Multiple access type modifiers are not allowed');
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ class Function_ extends Node\Stmt implements FunctionLike
|
||||
public $name;
|
||||
/** @var Node\Param[] Parameters */
|
||||
public $params;
|
||||
/** @var null|string|Node\Name Return type */
|
||||
/** @var null|string|Node\Name|Node\NullableType Return type */
|
||||
public $returnType;
|
||||
/** @var Node[] Statements */
|
||||
public $stmts;
|
||||
|
@ -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 */
|
||||
|
@ -34,7 +34,7 @@ class Property extends Node\Stmt
|
||||
|
||||
public function isPublic() {
|
||||
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|
||||
|| ($this->flags & Class_::VISIBILITY_MODIFER_MASK) === 0;
|
||||
|| ($this->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0;
|
||||
}
|
||||
|
||||
public function isProtected() {
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
use PhpParser\Node;
|
||||
|
||||
abstract class NodeAbstract implements Node, \JsonSerializable
|
||||
{
|
||||
protected $attributes;
|
||||
@ -21,7 +23,12 @@ 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, strlen(Node::class) + 1),
|
||||
'\\',
|
||||
'_'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,6 +44,8 @@ abstract class NodeAbstract implements Node, \JsonSerializable
|
||||
* Sets line the node started in.
|
||||
*
|
||||
* @param int $line Line
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function setLine($line) {
|
||||
$this->setAttribute('startLine', (int) $line);
|
||||
|
@ -13,6 +13,14 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
*/
|
||||
const DONT_TRAVERSE_CHILDREN = 1;
|
||||
|
||||
/**
|
||||
* If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns
|
||||
* STOP_TRAVERSAL, traversal is aborted.
|
||||
*
|
||||
* The afterTraverse() method will still be invoked.
|
||||
*/
|
||||
const STOP_TRAVERSAL = 2;
|
||||
|
||||
/**
|
||||
* If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs
|
||||
* in an array, it will be removed from the array.
|
||||
@ -25,6 +33,9 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
/** @var NodeVisitor[] Visitors */
|
||||
protected $visitors;
|
||||
|
||||
/** @var bool Whether traversal should be stopped */
|
||||
protected $stopTraversal;
|
||||
|
||||
/**
|
||||
* Constructs a node traverser.
|
||||
*/
|
||||
@ -63,6 +74,8 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
* @return Node[] Traversed array of nodes
|
||||
*/
|
||||
public function traverse(array $nodes) {
|
||||
$this->stopTraversal = false;
|
||||
|
||||
foreach ($this->visitors as $visitor) {
|
||||
if (null !== $return = $visitor->beforeTraverse($nodes)) {
|
||||
$nodes = $return;
|
||||
@ -86,12 +99,18 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
|
||||
if (is_array($subNode)) {
|
||||
$subNode = $this->traverseArray($subNode);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
} elseif ($subNode instanceof Node) {
|
||||
$traverseChildren = true;
|
||||
foreach ($this->visitors as $visitor) {
|
||||
$return = $visitor->enterNode($subNode);
|
||||
if (self::DONT_TRAVERSE_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
} else if (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} else if (null !== $return) {
|
||||
$subNode = $return;
|
||||
}
|
||||
@ -99,10 +118,17 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
|
||||
if ($traverseChildren) {
|
||||
$subNode = $this->traverseNode($subNode);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->visitors as $visitor) {
|
||||
if (null !== $return = $visitor->leaveNode($subNode)) {
|
||||
$return = $visitor->leaveNode($subNode);
|
||||
if (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} else if (null !== $return) {
|
||||
if (is_array($return)) {
|
||||
throw new \LogicException(
|
||||
'leaveNode() may only return an array ' .
|
||||
@ -124,12 +150,18 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
foreach ($nodes as $i => &$node) {
|
||||
if (is_array($node)) {
|
||||
$node = $this->traverseArray($node);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
} elseif ($node instanceof Node) {
|
||||
$traverseChildren = true;
|
||||
foreach ($this->visitors as $visitor) {
|
||||
$return = $visitor->enterNode($node);
|
||||
if (self::DONT_TRAVERSE_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
} else if (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} else if (null !== $return) {
|
||||
$node = $return;
|
||||
}
|
||||
@ -137,6 +169,9 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
|
||||
if ($traverseChildren) {
|
||||
$node = $this->traverseNode($node);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->visitors as $visitor) {
|
||||
@ -145,6 +180,9 @@ class NodeTraverser implements NodeTraverserInterface
|
||||
if (self::REMOVE_NODE === $return) {
|
||||
$doNodes[] = array($i, array());
|
||||
break;
|
||||
} else if (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (is_array($return)) {
|
||||
$doNodes[] = array($i, $return);
|
||||
break;
|
||||
|
@ -21,12 +21,18 @@ interface NodeVisitor
|
||||
* Called when entering a node.
|
||||
*
|
||||
* Return value semantics:
|
||||
* * null: $node stays as-is
|
||||
* * otherwise: $node is set to the return value
|
||||
* * null
|
||||
* => $node stays as-is
|
||||
* * NodeTraverser::DONT_TRAVERSE_CHILDREN
|
||||
* => Children of $node are not traversed. $node stays as-is
|
||||
* * NodeTraverser::STOP_TRAVERSAL
|
||||
* => Traversal is aborted. $node stays as-is
|
||||
* * otherwise
|
||||
* => $node is set to the return value
|
||||
*
|
||||
* @param Node $node Node
|
||||
*
|
||||
* @return null|Node Node
|
||||
* @return null|int|Node Node
|
||||
*/
|
||||
public function enterNode(Node $node);
|
||||
|
||||
@ -34,14 +40,20 @@ interface NodeVisitor
|
||||
* Called when leaving a node.
|
||||
*
|
||||
* Return value semantics:
|
||||
* * null: $node stays as-is
|
||||
* * false: $node is removed from the parent array
|
||||
* * array: The return value is merged into the parent array (at the position of the $node)
|
||||
* * otherwise: $node is set to the return value
|
||||
* * null
|
||||
* => $node stays as-is
|
||||
* * NodeTraverser::REMOVE_NODE
|
||||
* => $node is removed from the parent array
|
||||
* * NodeTraverser::STOP_TRAVERSAL
|
||||
* => Traversal is aborted. $node stays as-is
|
||||
* * array (of Nodes)
|
||||
* => The return value is merged into the parent array (at the position of the $node)
|
||||
* * otherwise
|
||||
* => $node is set to the return value
|
||||
*
|
||||
* @param Node $node Node
|
||||
*
|
||||
* @return null|Node|false|Node[] Node
|
||||
* @return null|false|int|Node|Node[] Node
|
||||
*/
|
||||
public function leaveNode(Node $node);
|
||||
|
||||
|
@ -22,13 +22,21 @@ class NameResolver extends NodeVisitorAbstract
|
||||
/** @var ErrorHandler Error handler */
|
||||
protected $errorHandler;
|
||||
|
||||
/** @var bool Whether to preserve original names */
|
||||
protected $preserveOriginalNames;
|
||||
|
||||
/**
|
||||
* Constructs a name resolution visitor.
|
||||
*
|
||||
* Options: If "preserveOriginalNames" is enabled, an "originalName" attribute will be added to
|
||||
* all name nodes that underwent resolution.
|
||||
*
|
||||
* @param ErrorHandler|null $errorHandler Error handler
|
||||
* @param array $options Options
|
||||
*/
|
||||
public function __construct(ErrorHandler $errorHandler = null) {
|
||||
public function __construct(ErrorHandler $errorHandler = null, array $options = []) {
|
||||
$this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing;
|
||||
$this->preserveOriginalNames = !empty($options['preserveOriginalNames']);
|
||||
}
|
||||
|
||||
public function beforeTraverse(array $nodes) {
|
||||
@ -112,7 +120,6 @@ class NameResolver extends NodeVisitorAbstract
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,16 +168,30 @@ class NameResolver extends NodeVisitorAbstract
|
||||
/** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */
|
||||
private function resolveSignature($node) {
|
||||
foreach ($node->params as $param) {
|
||||
if ($param->type instanceof Name) {
|
||||
$param->type = $this->resolveClassName($param->type);
|
||||
}
|
||||
$param->type = $this->resolveType($param->type);
|
||||
}
|
||||
if ($node->returnType instanceof Name) {
|
||||
$node->returnType = $this->resolveClassName($node->returnType);
|
||||
$node->returnType = $this->resolveType($node->returnType);
|
||||
}
|
||||
|
||||
private function resolveType($node) {
|
||||
if ($node instanceof Node\NullableType) {
|
||||
$node->type = $this->resolveType($node->type);
|
||||
return $node;
|
||||
}
|
||||
if ($node instanceof Name) {
|
||||
return $this->resolveClassName($node);
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
protected function resolveClassName(Name $name) {
|
||||
if ($this->preserveOriginalNames) {
|
||||
// Save the original name
|
||||
$originalName = $name;
|
||||
$name = clone $originalName;
|
||||
$name->setAttribute('originalName', $originalName);
|
||||
}
|
||||
|
||||
// don't resolve special class names
|
||||
if (in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) {
|
||||
if (!$name->isUnqualified()) {
|
||||
@ -179,7 +200,6 @@ class NameResolver extends NodeVisitorAbstract
|
||||
$name->getAttributes()
|
||||
));
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
@ -200,6 +220,13 @@ class NameResolver extends NodeVisitorAbstract
|
||||
}
|
||||
|
||||
protected function resolveOtherName(Name $name, $type) {
|
||||
if ($this->preserveOriginalNames) {
|
||||
// Save the original name
|
||||
$originalName = $name;
|
||||
$name = clone $originalName;
|
||||
$name->setAttribute('originalName', $originalName);
|
||||
}
|
||||
|
||||
// fully qualified names are already resolved
|
||||
if ($name->isFullyQualified()) {
|
||||
return $name;
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -9,6 +9,7 @@ namespace PhpParser;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
@ -314,7 +315,11 @@ abstract class ParserAbstract implements Parser
|
||||
//$this->traceShift($this->errorSymbol);
|
||||
++$this->stackPos;
|
||||
$stateStack[$this->stackPos] = $state = $action;
|
||||
$this->endAttributes = $this->endAttributeStack[$this->stackPos];
|
||||
|
||||
// We treat the error symbol as being empty, so we reset the end attributes
|
||||
// to the end attributes of the last non-error symbol
|
||||
$this->endAttributeStack[$this->stackPos] = $this->endAttributeStack[$this->stackPos - 1];
|
||||
$this->endAttributes = $this->endAttributeStack[$this->stackPos - 1];
|
||||
break;
|
||||
|
||||
case 3:
|
||||
@ -440,6 +445,7 @@ abstract class ParserAbstract implements Parser
|
||||
if ($stmt instanceof Node\Stmt\Namespace_) {
|
||||
$afterFirstNamespace = true;
|
||||
} elseif (!$stmt instanceof Node\Stmt\HaltCompiler
|
||||
&& !$stmt instanceof Node\Stmt\Nop
|
||||
&& $afterFirstNamespace && !$hasErrored) {
|
||||
$this->emitError(new Error(
|
||||
'No code may exist outside of namespace {}', $stmt->getAttributes()));
|
||||
@ -524,6 +530,7 @@ abstract class ParserAbstract implements Parser
|
||||
'string' => true,
|
||||
'iterable' => true,
|
||||
'void' => true,
|
||||
'object' => true,
|
||||
];
|
||||
|
||||
if (!$name->isUnqualified()) {
|
||||
@ -554,6 +561,19 @@ abstract class ParserAbstract implements Parser
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseNumString($str, $attributes) {
|
||||
if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
|
||||
return new String_($str, $attributes);
|
||||
}
|
||||
|
||||
$num = +$str;
|
||||
if (!is_int($num)) {
|
||||
return new String_($str, $attributes);
|
||||
}
|
||||
|
||||
return new LNumber($num, $attributes);
|
||||
}
|
||||
|
||||
protected function checkModifier($a, $b, $modifierPos) {
|
||||
// Jumping through some hoops here because verifyModifier() is also used elsewhere
|
||||
try {
|
||||
|
@ -34,7 +34,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
protected function pNullableType(Node\NullableType $node) {
|
||||
return '?' . $node->type;
|
||||
return '?' . $this->pType($node->type);
|
||||
}
|
||||
|
||||
// Names
|
||||
@ -141,26 +141,52 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
protected function pScalar_LNumber(Scalar\LNumber $node) {
|
||||
if ($node->value === -\PHP_INT_MAX-1) {
|
||||
// PHP_INT_MIN cannot be represented as a literal,
|
||||
// because the sign is not part of the literal
|
||||
return '(-' . \PHP_INT_MAX . '-1)';
|
||||
}
|
||||
|
||||
$kind = $node->getAttribute('kind', Scalar\LNumber::KIND_DEC);
|
||||
if (Scalar\LNumber::KIND_DEC === $kind) {
|
||||
return (string) $node->value;
|
||||
}
|
||||
|
||||
$sign = $node->value < 0 ? '-' : '';
|
||||
$str = (string) $node->value;
|
||||
switch ($node->getAttribute('kind', Scalar\LNumber::KIND_DEC)) {
|
||||
switch ($kind) {
|
||||
case Scalar\LNumber::KIND_BIN:
|
||||
return '0b' . base_convert($str, 10, 2);
|
||||
return $sign . '0b' . base_convert($str, 10, 2);
|
||||
case Scalar\LNumber::KIND_OCT:
|
||||
return '0' . base_convert($str, 10, 8);
|
||||
case Scalar\LNumber::KIND_DEC:
|
||||
return $str;
|
||||
return $sign . '0' . base_convert($str, 10, 8);
|
||||
case Scalar\LNumber::KIND_HEX:
|
||||
return '0x' . base_convert($str, 10, 16);
|
||||
return $sign . '0x' . base_convert($str, 10, 16);
|
||||
}
|
||||
throw new \Exception('Invalid number kind');
|
||||
}
|
||||
|
||||
protected function pScalar_DNumber(Scalar\DNumber $node) {
|
||||
if (!is_finite($node->value)) {
|
||||
if ($node->value === \INF) {
|
||||
return '\INF';
|
||||
} elseif ($node->value === -\INF) {
|
||||
return '-\INF';
|
||||
} else {
|
||||
return '\NAN';
|
||||
}
|
||||
}
|
||||
|
||||
// Try to find a short full-precision representation
|
||||
$stringValue = sprintf('%.16G', $node->value);
|
||||
if ($node->value !== (double) $stringValue) {
|
||||
$stringValue = sprintf('%.17G', $node->value);
|
||||
}
|
||||
|
||||
// %G is locale dependent and there exists no locale-independent alternative. We don't want
|
||||
// mess with switching locales here, so let's assume that a comma is the only non-standard
|
||||
// decimal separator we may encounter...
|
||||
$stringValue = str_replace(',', '.', $stringValue);
|
||||
|
||||
// ensure that number is really printed as float
|
||||
return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
|
||||
}
|
||||
@ -348,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);
|
||||
}
|
||||
|
||||
@ -417,12 +451,12 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
protected function pExpr_FuncCall(Expr\FuncCall $node) {
|
||||
return $this->pCallLhs($node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_MethodCall(Expr\MethodCall $node) {
|
||||
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_StaticCall(Expr\StaticCall $node) {
|
||||
@ -432,7 +466,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
? $this->p($node->name)
|
||||
: '{' . $this->p($node->name) . '}')
|
||||
: $node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_Empty(Expr\Empty_ $node) {
|
||||
@ -464,6 +498,10 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
// Other
|
||||
|
||||
protected function pExpr_Error(Expr\Error $node) {
|
||||
throw new \LogicException('Cannot pretty-print AST with Error nodes');
|
||||
}
|
||||
|
||||
protected function pExpr_Variable(Expr\Variable $node) {
|
||||
if ($node->name instanceof Expr) {
|
||||
return '${' . $this->p($node->name) . '}';
|
||||
@ -476,9 +514,9 @@ class Standard extends PrettyPrinterAbstract
|
||||
$syntax = $node->getAttribute('kind',
|
||||
$this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
|
||||
if ($syntax === Expr\Array_::KIND_SHORT) {
|
||||
return '[' . $this->pCommaSeparated($node->items) . ']';
|
||||
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
|
||||
} else {
|
||||
return 'array(' . $this->pCommaSeparated($node->items) . ')';
|
||||
return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
@ -497,7 +535,8 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) {
|
||||
return $this->p($node->class) . '::' . $node->name;
|
||||
return $this->p($node->class) . '::'
|
||||
. (is_string($node->name) ? $node->name : $this->p($node->name));
|
||||
}
|
||||
|
||||
protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) {
|
||||
@ -527,10 +566,10 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
protected function pExpr_New(Expr\New_ $node) {
|
||||
if ($node->class instanceof Stmt\Class_) {
|
||||
$args = $node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '';
|
||||
$args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
|
||||
return 'new ' . $this->pClassCommon($node->class, $args);
|
||||
}
|
||||
return 'new ' . $this->p($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
return 'new ' . $this->p($node->class) . '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_Clone(Expr\Clone_ $node) {
|
||||
@ -589,7 +628,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
. ($node->name->getLast() !== $node->alias ? ' as ' . $node->alias : '');
|
||||
}
|
||||
|
||||
private function pUseType($type) {
|
||||
protected function pUseType($type) {
|
||||
return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
|
||||
: ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
|
||||
}
|
||||
@ -918,4 +957,21 @@ class Standard extends PrettyPrinterAbstract
|
||||
return '(' . $this->p($node) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
private function hasNodeWithComments(array $nodes) {
|
||||
foreach ($nodes as $node) {
|
||||
if ($node && $node->getAttribute('comments')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function pMaybeMultiline(array $nodes, $trailingComma = false) {
|
||||
if (!$this->hasNodeWithComments($nodes)) {
|
||||
return $this->pCommaSeparated($nodes);
|
||||
} else {
|
||||
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -247,11 +247,11 @@ abstract class PrettyPrinterAbstract
|
||||
if ($childPrecedence > $parentPrecedence
|
||||
|| ($parentPrecedence == $childPrecedence && $parentAssociativity != $childPosition)
|
||||
) {
|
||||
return '(' . $this->{'p' . $type}($node) . ')';
|
||||
return '(' . $this->p($node) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
return $this->{'p' . $type}($node);
|
||||
return $this->p($node);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -286,6 +286,38 @@ abstract class PrettyPrinterAbstract
|
||||
return $this->pImplode($nodes, ', ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Pretty prints a comma-separated list of nodes in multiline style, including comments.
|
||||
*
|
||||
* The result includes a leading newline and one level of indentation (same as pStmts).
|
||||
*
|
||||
* @param Node[] $nodes Array of Nodes to be printed
|
||||
* @param bool $trailingComma Whether to use a trailing comma
|
||||
*
|
||||
* @return string Comma separated pretty printed nodes in multiline style
|
||||
*/
|
||||
protected function pCommaSeparatedMultiline(array $nodes, $trailingComma) {
|
||||
$result = '';
|
||||
$lastIdx = count($nodes) - 1;
|
||||
foreach ($nodes as $idx => $node) {
|
||||
if ($node !== null) {
|
||||
$comments = $node->getAttribute('comments', array());
|
||||
if ($comments) {
|
||||
$result .= "\n" . $this->pComments($comments);
|
||||
}
|
||||
|
||||
$result .= "\n" . $this->p($node);
|
||||
} else {
|
||||
$result .= "\n";
|
||||
}
|
||||
if ($trailingComma || $idx !== $lastIdx) {
|
||||
$result .= ',';
|
||||
}
|
||||
}
|
||||
|
||||
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Signals the pretty printer that a string shall not be indented.
|
||||
*
|
||||
|
@ -2,6 +2,9 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
interface Serializer
|
||||
{
|
||||
/**
|
||||
|
@ -7,6 +7,9 @@ use PhpParser\Node;
|
||||
use PhpParser\Serializer;
|
||||
use XMLWriter;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class XML implements Serializer
|
||||
{
|
||||
protected $writer;
|
||||
|
@ -2,6 +2,9 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
interface Unserializer
|
||||
{
|
||||
/**
|
||||
|
@ -6,6 +6,9 @@ use DomainException;
|
||||
use PhpParser\Unserializer;
|
||||
use XMLReader;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class XML implements Unserializer
|
||||
{
|
||||
protected $reader;
|
||||
|
@ -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()
|
||||
|
@ -112,6 +112,7 @@ class ParamTest extends \PHPUnit_Framework_TestCase
|
||||
array('float', 'float'),
|
||||
array('string', 'string'),
|
||||
array('iterable', 'iterable'),
|
||||
array('object', 'object'),
|
||||
array('Array', 'array'),
|
||||
array('CALLABLE', 'callable'),
|
||||
array('Some\Class', new Node\Name('Some\Class')),
|
||||
@ -155,4 +156,16 @@ class ParamTest extends \PHPUnit_Framework_TestCase
|
||||
$node
|
||||
);
|
||||
}
|
||||
|
||||
public function testVariadic() {
|
||||
$node = $this->createParamBuilder('test')
|
||||
->makeVariadic()
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
new Node\Param('test', null, null, false, true),
|
||||
$node
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser\Comment;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class TraitTest extends \PHPUnit_Framework_TestCase
|
||||
@ -18,19 +19,21 @@ class TraitTest extends \PHPUnit_Framework_TestCase
|
||||
$prop = new Stmt\Property(Stmt\Class_::MODIFIER_PUBLIC, array(
|
||||
new Stmt\PropertyProperty('test')
|
||||
));
|
||||
$use = new Stmt\TraitUse([new Name('OtherTrait')]);
|
||||
$trait = $this->createTraitBuilder('TestTrait')
|
||||
->setDocComment('/** Nice trait */')
|
||||
->addStmt($method1)
|
||||
->addStmts(array($method2, $method3))
|
||||
->addStmts([$method2, $method3])
|
||||
->addStmt($prop)
|
||||
->addStmt($use)
|
||||
->getNode();
|
||||
$this->assertEquals(new Stmt\Trait_('TestTrait', array(
|
||||
'stmts' => array($prop, $method1, $method2, $method3)
|
||||
), array(
|
||||
'comments' => array(
|
||||
$this->assertEquals(new Stmt\Trait_('TestTrait', [
|
||||
'stmts' => [$use, $prop, $method1, $method2, $method3]
|
||||
], [
|
||||
'comments' => [
|
||||
new Comment\Doc('/** Nice trait */')
|
||||
)
|
||||
)), $trait);
|
||||
]
|
||||
]), $trait);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -201,7 +201,13 @@ class LexerTest extends \PHPUnit_Framework_TestCase
|
||||
array(), array()
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
// tests no tokens
|
||||
array(
|
||||
'',
|
||||
array(),
|
||||
array()
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -166,6 +166,63 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
||||
}
|
||||
|
||||
public function testStopTraversal() {
|
||||
$varNode1 = new Expr\Variable('a');
|
||||
$varNode2 = new Expr\Variable('b');
|
||||
$varNode3 = new Expr\Variable('c');
|
||||
$mulNode = new Expr\BinaryOp\Mul($varNode1, $varNode2);
|
||||
$printNode = new Expr\Print_($varNode3);
|
||||
$stmts = [$mulNode, $printNode];
|
||||
|
||||
// From enterNode() with array parent
|
||||
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
$visitor->expects($this->at(1))->method('enterNode')->with($mulNode)
|
||||
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
|
||||
$visitor->expects($this->at(2))->method('afterTraverse');
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitor);
|
||||
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
||||
|
||||
// From enterNode with Node parent
|
||||
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
$visitor->expects($this->at(2))->method('enterNode')->with($varNode1)
|
||||
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
|
||||
$visitor->expects($this->at(3))->method('afterTraverse');
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitor);
|
||||
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
||||
|
||||
// From leaveNode with Node parent
|
||||
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
$visitor->expects($this->at(3))->method('leaveNode')->with($varNode1)
|
||||
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
|
||||
$visitor->expects($this->at(4))->method('afterTraverse');
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitor);
|
||||
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
||||
|
||||
// From leaveNode with array parent
|
||||
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
$visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
|
||||
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
|
||||
$visitor->expects($this->at(7))->method('afterTraverse');
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitor);
|
||||
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
||||
|
||||
// Check that pending array modifications are still carried out
|
||||
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
$visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
|
||||
->will($this->returnValue(NodeTraverser::REMOVE_NODE));
|
||||
$visitor->expects($this->at(7))->method('enterNode')->with($printNode)
|
||||
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
|
||||
$visitor->expects($this->at(8))->method('afterTraverse');
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitor);
|
||||
$this->assertEquals([$printNode], $traverser->traverse($stmts));
|
||||
|
||||
}
|
||||
|
||||
public function testRemovingVisitor() {
|
||||
$visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
$visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
|
||||
|
@ -199,9 +199,12 @@ interface A extends C, D {
|
||||
public function a(A $a) : A;
|
||||
}
|
||||
|
||||
function fn() : A {}
|
||||
function fn2() : array {}
|
||||
function() : A {};
|
||||
function fn(A $a) : A {}
|
||||
function fn2(array $a) : array {}
|
||||
function(A $a) : A {};
|
||||
|
||||
function fn3(?A $a) : ?A {}
|
||||
function fn4(?array $a) : ?array {}
|
||||
|
||||
A::b();
|
||||
A::$b;
|
||||
@ -233,14 +236,20 @@ interface A extends \NS\C, \NS\D
|
||||
{
|
||||
public function a(\NS\A $a) : \NS\A;
|
||||
}
|
||||
function fn() : \NS\A
|
||||
function fn(\NS\A $a) : \NS\A
|
||||
{
|
||||
}
|
||||
function fn2() : array
|
||||
function fn2(array $a) : array
|
||||
{
|
||||
}
|
||||
function () : \NS\A {
|
||||
function (\NS\A $a) : \NS\A {
|
||||
};
|
||||
function fn3(?\NS\A $a) : ?\NS\A
|
||||
{
|
||||
}
|
||||
function fn4(?array $a) : ?array
|
||||
{
|
||||
}
|
||||
\NS\A::b();
|
||||
\NS\A::$b;
|
||||
\NS\A::B;
|
||||
@ -437,4 +446,23 @@ EOC;
|
||||
$this->assertSame('PARENT', (string)$methodStmt->stmts[1]->class);
|
||||
$this->assertSame('STATIC', (string)$methodStmt->stmts[2]->class);
|
||||
}
|
||||
|
||||
public function testAddOriginalNames() {
|
||||
$traverser = new PhpParser\NodeTraverser;
|
||||
$traverser->addVisitor(new NameResolver(null, ['preserveOriginalNames' => true]));
|
||||
|
||||
$n1 = new Name('Bar');
|
||||
$n2 = new Name('bar');
|
||||
$origStmts = [
|
||||
new Stmt\Namespace_(new Name('Foo'), [
|
||||
new Expr\ClassConstFetch($n1, 'FOO'),
|
||||
new Expr\FuncCall($n2),
|
||||
])
|
||||
];
|
||||
|
||||
$stmts = $traverser->traverse($origStmts);
|
||||
|
||||
$this->assertSame($n1, $stmts[0]->stmts[0]->class->getAttribute('originalName'));
|
||||
$this->assertSame($n2, $stmts[0]->stmts[1]->name->getAttribute('originalName'));
|
||||
}
|
||||
}
|
||||
|
@ -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]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,11 @@ namespace PhpParser;
|
||||
|
||||
use PhpParser\Comment;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\DNumber;
|
||||
use PhpParser\Node\Scalar\Encapsed;
|
||||
use PhpParser\Node\Scalar\EncapsedStringPart;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\PrettyPrinter\Standard;
|
||||
@ -161,4 +164,44 @@ class PrettyPrinterTest extends CodeTestAbstract
|
||||
[new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider provideTestUnnaturalLiterals */
|
||||
public function testUnnaturalLiterals($node, $expected) {
|
||||
$prttyPrinter = new PrettyPrinter\Standard;
|
||||
$result = $prttyPrinter->prettyPrintExpr($node);
|
||||
$this->assertSame($expected, $result);
|
||||
}
|
||||
|
||||
public function provideTestUnnaturalLiterals() {
|
||||
return [
|
||||
[new LNumber(-1), '-1'],
|
||||
[new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
|
||||
[new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
|
||||
[new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
|
||||
[new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
|
||||
[new DNumber(\INF), '\INF'],
|
||||
[new DNumber(-\INF), '-\INF'],
|
||||
[new DNumber(-\NAN), '\NAN'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Cannot pretty-print AST with Error nodes
|
||||
*/
|
||||
public function testPrettyPrintWithError() {
|
||||
$stmts = [new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())];
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
$prettyPrinter->prettyPrint($stmts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Cannot pretty-print AST with Error nodes
|
||||
*/
|
||||
public function testPrettyPrintWithErrorInClassConstFetch() {
|
||||
$stmts = [new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())];
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
$prettyPrinter->prettyPrint($stmts);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
)
|
||||
)
|
||||
)
|
@ -248,11 +248,11 @@ $foo->
|
||||
!!positions
|
||||
Syntax error, unexpected ';', expecting T_STRING or T_VARIABLE or '{' or '$' from 3:1 to 3:1
|
||||
array(
|
||||
0: Expr_PropertyFetch[2:1 - 3:1](
|
||||
0: Expr_PropertyFetch[2:1 - 2:6](
|
||||
var: Expr_Variable[2:1 - 2:4](
|
||||
name: foo
|
||||
)
|
||||
name: Expr_Error[3:1 - 3:1](
|
||||
name: Expr_Error[3:1 - 2:6](
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -272,11 +272,11 @@ array(
|
||||
)
|
||||
returnType: null
|
||||
stmts: array(
|
||||
0: Expr_PropertyFetch[3:5 - 4:1](
|
||||
0: Expr_PropertyFetch[3:5 - 3:10](
|
||||
var: Expr_Variable[3:5 - 3:8](
|
||||
name: bar
|
||||
)
|
||||
name: Expr_Error[4:1 - 4:1](
|
||||
name: Expr_Error[4:1 - 3:10](
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -305,8 +305,8 @@ new
|
||||
!!php7,positions
|
||||
Syntax error, unexpected EOF from 2:4 to 2:4
|
||||
array(
|
||||
0: Expr_New[2:1 - 2:4](
|
||||
class: Expr_Error[2:4 - 2:4](
|
||||
0: Expr_New[2:1 - 2:3](
|
||||
class: Expr_Error[2:4 - 2:3](
|
||||
)
|
||||
args: array(
|
||||
)
|
||||
@ -372,4 +372,495 @@ array(
|
||||
name: Expr_Error(
|
||||
)
|
||||
)
|
||||
)
|
||||
-----
|
||||
<?php
|
||||
namespace Foo
|
||||
use A
|
||||
use function a
|
||||
use A\{B}
|
||||
const A = 1
|
||||
break
|
||||
break 2
|
||||
continue
|
||||
continue 2
|
||||
return
|
||||
return 2
|
||||
echo $a
|
||||
unset($a)
|
||||
throw $x
|
||||
goto label
|
||||
-----
|
||||
!!php7
|
||||
Syntax error, unexpected T_USE, expecting ';' or '{' from 3:1 to 3:3
|
||||
Syntax error, unexpected T_USE, expecting ';' from 5:1 to 5:3
|
||||
Syntax error, unexpected T_CONST, expecting ';' from 6:1 to 6:5
|
||||
Syntax error, unexpected T_BREAK, expecting ';' from 7:1 to 7:5
|
||||
Syntax error, unexpected T_THROW, expecting ';' from 15:1 to 15:5
|
||||
array(
|
||||
0: Stmt_Namespace(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: Foo
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
0: Stmt_Use(
|
||||
type: TYPE_NORMAL (1)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
alias: A
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Stmt_Use(
|
||||
type: TYPE_FUNCTION (2)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: a
|
||||
)
|
||||
)
|
||||
alias: a
|
||||
)
|
||||
)
|
||||
)
|
||||
2: Stmt_GroupUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_NORMAL (1)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: B
|
||||
)
|
||||
)
|
||||
alias: B
|
||||
)
|
||||
)
|
||||
)
|
||||
3: Stmt_Const(
|
||||
consts: array(
|
||||
0: Const(
|
||||
name: A
|
||||
value: Scalar_LNumber(
|
||||
value: 1
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
4: Stmt_Break(
|
||||
num: null
|
||||
)
|
||||
5: Stmt_Break(
|
||||
num: Scalar_LNumber(
|
||||
value: 2
|
||||
)
|
||||
)
|
||||
6: Stmt_Continue(
|
||||
num: null
|
||||
)
|
||||
7: Stmt_Continue(
|
||||
num: Scalar_LNumber(
|
||||
value: 2
|
||||
)
|
||||
)
|
||||
8: Stmt_Return(
|
||||
expr: null
|
||||
)
|
||||
9: Stmt_Return(
|
||||
expr: Scalar_LNumber(
|
||||
value: 2
|
||||
)
|
||||
)
|
||||
10: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
)
|
||||
)
|
||||
11: Stmt_Unset(
|
||||
vars: array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
)
|
||||
)
|
||||
12: Stmt_Throw(
|
||||
expr: Expr_Variable(
|
||||
name: x
|
||||
)
|
||||
)
|
||||
13: Stmt_Goto(
|
||||
name: label
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
-----
|
||||
<?php
|
||||
|
||||
use A\{B, };
|
||||
use function A\{b, };
|
||||
use A, ;
|
||||
const A = 42, ;
|
||||
|
||||
class X implements Y, {
|
||||
use A, ;
|
||||
use A, {
|
||||
A::b insteadof C, ;
|
||||
}
|
||||
const A = 42, ;
|
||||
public $x, ;
|
||||
}
|
||||
interface I extends J, {}
|
||||
|
||||
unset($x, );
|
||||
isset($x, );
|
||||
|
||||
declare(a=42, );
|
||||
|
||||
function foo($a, ) {}
|
||||
foo($a, );
|
||||
global $a, ;
|
||||
static $a, ;
|
||||
echo $a, ;
|
||||
|
||||
for ($a, ; $b, ; $c, );
|
||||
function ($a, ) use ($b, ) {};
|
||||
-----
|
||||
!!php7
|
||||
A trailing comma is not allowed here from 5:6 to 5:6
|
||||
A trailing comma is not allowed here from 6:13 to 6:13
|
||||
A trailing comma is not allowed here from 8:21 to 8:21
|
||||
A trailing comma is not allowed here from 9:10 to 9:10
|
||||
A trailing comma is not allowed here from 10:10 to 10:10
|
||||
A trailing comma is not allowed here from 11:25 to 11:25
|
||||
A trailing comma is not allowed here from 13:17 to 13:17
|
||||
A trailing comma is not allowed here from 14:14 to 14:14
|
||||
A trailing comma is not allowed here from 16:22 to 16:22
|
||||
A trailing comma is not allowed here from 18:9 to 18:9
|
||||
A trailing comma is not allowed here from 19:9 to 19:9
|
||||
A trailing comma is not allowed here from 21:13 to 21:13
|
||||
A trailing comma is not allowed here from 23:16 to 23:16
|
||||
A trailing comma is not allowed here from 24:7 to 24:7
|
||||
A trailing comma is not allowed here from 25:10 to 25:10
|
||||
A trailing comma is not allowed here from 26:10 to 26:10
|
||||
A trailing comma is not allowed here from 27:8 to 27:8
|
||||
A trailing comma is not allowed here from 29:8 to 29:8
|
||||
A trailing comma is not allowed here from 29:14 to 29:14
|
||||
A trailing comma is not allowed here from 29:20 to 29:20
|
||||
A trailing comma is not allowed here from 30:13 to 30:13
|
||||
A trailing comma is not allowed here from 30:24 to 30:24
|
||||
array(
|
||||
0: Stmt_GroupUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_NORMAL (1)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: B
|
||||
)
|
||||
)
|
||||
alias: B
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Stmt_GroupUse(
|
||||
type: TYPE_FUNCTION (2)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: b
|
||||
)
|
||||
)
|
||||
alias: b
|
||||
)
|
||||
)
|
||||
)
|
||||
2: Stmt_Use(
|
||||
type: TYPE_NORMAL (1)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
alias: A
|
||||
)
|
||||
)
|
||||
)
|
||||
3: Stmt_Const(
|
||||
consts: array(
|
||||
0: Const(
|
||||
name: A
|
||||
value: Scalar_LNumber(
|
||||
value: 42
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
4: Stmt_Class(
|
||||
flags: 0
|
||||
name: X
|
||||
extends: null
|
||||
implements: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: Y
|
||||
)
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
0: Stmt_TraitUse(
|
||||
traits: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
)
|
||||
adaptations: array(
|
||||
)
|
||||
)
|
||||
1: Stmt_TraitUse(
|
||||
traits: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
)
|
||||
adaptations: array(
|
||||
0: Stmt_TraitUseAdaptation_Precedence(
|
||||
trait: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
method: b
|
||||
insteadof: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: C
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
2: Stmt_ClassConst(
|
||||
flags: 0
|
||||
consts: array(
|
||||
0: Const(
|
||||
name: A
|
||||
value: Scalar_LNumber(
|
||||
value: 42
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
3: Stmt_Property(
|
||||
flags: MODIFIER_PUBLIC (1)
|
||||
props: array(
|
||||
0: Stmt_PropertyProperty(
|
||||
name: x
|
||||
default: null
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
5: Stmt_Interface(
|
||||
name: I
|
||||
extends: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: J
|
||||
)
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
6: Stmt_Unset(
|
||||
vars: array(
|
||||
0: Expr_Variable(
|
||||
name: x
|
||||
)
|
||||
)
|
||||
)
|
||||
7: Expr_Isset(
|
||||
vars: array(
|
||||
0: Expr_Variable(
|
||||
name: x
|
||||
)
|
||||
)
|
||||
)
|
||||
8: Stmt_Declare(
|
||||
declares: array(
|
||||
0: Stmt_DeclareDeclare(
|
||||
key: a
|
||||
value: Scalar_LNumber(
|
||||
value: 42
|
||||
)
|
||||
)
|
||||
)
|
||||
stmts: null
|
||||
)
|
||||
9: Stmt_Function(
|
||||
byRef: false
|
||||
name: foo
|
||||
params: array(
|
||||
0: Param(
|
||||
type: null
|
||||
byRef: false
|
||||
variadic: false
|
||||
name: a
|
||||
default: null
|
||||
)
|
||||
)
|
||||
returnType: null
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
10: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: foo
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
11: Stmt_Global(
|
||||
vars: array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
)
|
||||
)
|
||||
12: Stmt_Static(
|
||||
vars: array(
|
||||
0: Stmt_StaticVar(
|
||||
name: a
|
||||
default: null
|
||||
)
|
||||
)
|
||||
)
|
||||
13: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
)
|
||||
)
|
||||
14: Stmt_For(
|
||||
init: array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
)
|
||||
cond: array(
|
||||
0: Expr_Variable(
|
||||
name: b
|
||||
)
|
||||
)
|
||||
loop: array(
|
||||
0: Expr_Variable(
|
||||
name: c
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
15: Expr_Closure(
|
||||
static: false
|
||||
byRef: false
|
||||
params: array(
|
||||
0: Param(
|
||||
type: null
|
||||
byRef: false
|
||||
variadic: false
|
||||
name: a
|
||||
default: null
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Expr_ClosureUse(
|
||||
var: b
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
returnType: null
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
-----
|
||||
<?php
|
||||
|
||||
foo(Bar::);
|
||||
-----
|
||||
!!php7,positions
|
||||
Syntax error, unexpected ')' from 3:10 to 3:10
|
||||
array(
|
||||
0: Expr_FuncCall[3:1 - 3:10](
|
||||
name: Name[3:1 - 3:3](
|
||||
parts: array(
|
||||
0: foo
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg[3:5 - 3:9](
|
||||
value: Expr_ClassConstFetch[3:5 - 3:9](
|
||||
class: Name[3:5 - 3:7](
|
||||
parts: array(
|
||||
0: Bar
|
||||
)
|
||||
)
|
||||
name: Expr_Error[3:10 - 3:9](
|
||||
)
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
@ -246,17 +246,21 @@ array(
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
1: Expr_List(
|
||||
items: array(
|
||||
0: null
|
||||
1: Expr_ArrayItem(
|
||||
key: null
|
||||
value: Expr_Variable(
|
||||
name: c
|
||||
1: Expr_ArrayItem(
|
||||
key: null
|
||||
value: Expr_List(
|
||||
items: array(
|
||||
0: null
|
||||
1: Expr_ArrayItem(
|
||||
key: null
|
||||
value: Expr_Variable(
|
||||
name: c
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
2: Expr_ArrayItem(
|
||||
key: null
|
||||
|
@ -4,13 +4,22 @@ Non-simple variables are forbidden in PHP 7
|
||||
global $$foo->bar;
|
||||
-----
|
||||
!!php7
|
||||
Syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' from 2:13 to 2:14
|
||||
Syntax error, unexpected T_OBJECT_OPERATOR, expecting ';' from 2:13 to 2:14
|
||||
array(
|
||||
0: Expr_ConstFetch(
|
||||
0: Stmt_Global(
|
||||
vars: array(
|
||||
0: Expr_Variable(
|
||||
name: Expr_Variable(
|
||||
name: foo
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Expr_ConstFetch(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: bar
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
72
test/code/parser/scalar/encapsedNegVarOffset.test
Normal file
72
test/code/parser/scalar/encapsedNegVarOffset.test
Normal file
@ -0,0 +1,72 @@
|
||||
Encapsed string negative var offsets
|
||||
-----
|
||||
<?php
|
||||
"$a[-0]";
|
||||
"$a[-1]";
|
||||
"$a[-0x0]";
|
||||
"$a[-00]";
|
||||
"$a[@@{ -PHP_INT_MAX - 1 }@@]";
|
||||
-----
|
||||
!!php7
|
||||
array(
|
||||
0: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: -0
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
dim: Scalar_LNumber(
|
||||
value: -1
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
2: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: -0x0
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
3: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: -00
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
4: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
dim: Scalar_LNumber(
|
||||
value: @@{ -PHP_INT_MAX - 1 }@@
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
@ -6,7 +6,11 @@ Encapsed strings
|
||||
"$A->B";
|
||||
"$A[B]";
|
||||
"$A[0]";
|
||||
"$A[1234]";
|
||||
"$A[9223372036854775808]";
|
||||
"$A[000]";
|
||||
"$A[0x0]";
|
||||
"$A[0b0]";
|
||||
"$A[$B]";
|
||||
"{$A}";
|
||||
"{$A['B']}";
|
||||
@ -59,13 +63,49 @@ array(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
dim: Scalar_LNumber(
|
||||
value: 0
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
4: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_LNumber(
|
||||
value: 1234
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
5: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: 9223372036854775808
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
6: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: 000
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
7: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
@ -77,7 +117,19 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
5: Scalar_Encapsed(
|
||||
8: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: 0b0
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
9: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
@ -89,45 +141,45 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
6: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
)
|
||||
)
|
||||
7: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: B
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
8: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
)
|
||||
)
|
||||
9: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: B
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
10: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
)
|
||||
)
|
||||
11: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: B
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
12: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
)
|
||||
)
|
||||
13: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_ArrayDimFetch(
|
||||
var: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
dim: Scalar_String(
|
||||
value: B
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
14: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: Expr_Variable(
|
||||
@ -136,7 +188,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
11: Scalar_Encapsed(
|
||||
15: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Scalar_EncapsedStringPart(
|
||||
value: \{
|
||||
@ -149,7 +201,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
12: Scalar_Encapsed(
|
||||
16: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Scalar_EncapsedStringPart(
|
||||
value: \{
|
||||
@ -162,7 +214,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
13: Scalar_Encapsed(
|
||||
17: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Scalar_EncapsedStringPart(
|
||||
value: \
|
||||
@ -172,7 +224,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
14: Scalar_Encapsed(
|
||||
18: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Scalar_EncapsedStringPart(
|
||||
value: \{
|
||||
@ -185,7 +237,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
15: Scalar_Encapsed(
|
||||
19: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: Expr_Variable(
|
||||
@ -197,7 +249,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
16: Scalar_Encapsed(
|
||||
20: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Scalar_EncapsedStringPart(
|
||||
value: $
|
||||
@ -212,7 +264,7 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
17: Scalar_Encapsed(
|
||||
21: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Scalar_EncapsedStringPart(
|
||||
value: A
|
||||
@ -225,14 +277,14 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
18: Scalar_Encapsed(
|
||||
22: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: A
|
||||
)
|
||||
)
|
||||
)
|
||||
19: Scalar_Encapsed(
|
||||
23: Scalar_Encapsed(
|
||||
parts: array(
|
||||
0: Expr_Variable(
|
||||
name: A
|
||||
|
@ -76,8 +76,21 @@ array(
|
||||
-----
|
||||
<?php class A extends static {}
|
||||
-----
|
||||
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR from 1:23 to 1:28
|
||||
Cannot use 'static' as class name as it is reserved from 1:23 to 1:28
|
||||
array(
|
||||
0: Stmt_Class(
|
||||
flags: 0
|
||||
name: A
|
||||
extends: Name(
|
||||
parts: array(
|
||||
0: static
|
||||
)
|
||||
)
|
||||
implements: array(
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
-----
|
||||
<?php class A implements self {}
|
||||
@ -122,8 +135,22 @@ array(
|
||||
-----
|
||||
<?php class A implements static {}
|
||||
-----
|
||||
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR from 1:26 to 1:31
|
||||
Cannot use 'static' as interface name as it is reserved from 1:26 to 1:31
|
||||
array(
|
||||
0: Stmt_Class(
|
||||
flags: 0
|
||||
name: A
|
||||
extends: null
|
||||
implements: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: static
|
||||
)
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
-----
|
||||
<?php interface self {}
|
||||
@ -196,6 +223,18 @@ array(
|
||||
-----
|
||||
<?php interface A extends static {}
|
||||
-----
|
||||
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR from 1:27 to 1:32
|
||||
Cannot use 'static' as interface name as it is reserved from 1:27 to 1:32
|
||||
array(
|
||||
)
|
||||
0: Stmt_Interface(
|
||||
name: A
|
||||
extends: array(
|
||||
0: Name(
|
||||
parts: array(
|
||||
0: static
|
||||
)
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
)
|
@ -1,7 +1,7 @@
|
||||
Scalar type declarations
|
||||
-----
|
||||
<?php
|
||||
function test(bool $a, Int $b, FLOAT $c, StRiNg $d, iterable $e) : void {}
|
||||
function test(bool $a, Int $b, FLOAT $c, StRiNg $d, iterable $e, object $f) : void {}
|
||||
-----
|
||||
!!php7
|
||||
array(
|
||||
@ -44,9 +44,16 @@ array(
|
||||
name: e
|
||||
default: null
|
||||
)
|
||||
5: Param(
|
||||
type: object
|
||||
byRef: false
|
||||
variadic: false
|
||||
name: f
|
||||
default: null
|
||||
)
|
||||
)
|
||||
returnType: void
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
22
test/code/parser/stmt/namespace/commentAfterNamespace.test
Normal file
22
test/code/parser/stmt/namespace/commentAfterNamespace.test
Normal file
@ -0,0 +1,22 @@
|
||||
Trailing comment after braced namespace declaration
|
||||
-----
|
||||
<?php
|
||||
namespace Foo {}
|
||||
// Comment
|
||||
-----
|
||||
array(
|
||||
0: Stmt_Namespace(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: Foo
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
1: Stmt_Nop(
|
||||
comments: array(
|
||||
0: // Comment
|
||||
)
|
||||
)
|
||||
)
|
@ -5,9 +5,32 @@ Invalid group use syntax
|
||||
use Foo\{Bar}
|
||||
use Bar\{Foo};
|
||||
-----
|
||||
!!php7
|
||||
Syntax error, unexpected T_USE, expecting ';' from 4:1 to 4:3
|
||||
array(
|
||||
0: Stmt_GroupUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
0: Foo
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_NORMAL (1)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: Bar
|
||||
)
|
||||
)
|
||||
alias: Bar
|
||||
)
|
||||
)
|
||||
comments: array(
|
||||
0: // Missing semicolon
|
||||
)
|
||||
)
|
||||
1: Stmt_GroupUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
@ -32,16 +55,34 @@ array(
|
||||
// Missing NS separator
|
||||
use Foo {Bar, Baz};
|
||||
-----
|
||||
Syntax error, unexpected '{', expecting ',' or ';' from 3:9 to 3:9
|
||||
!!php7
|
||||
Syntax error, unexpected '{', expecting ';' from 3:9 to 3:9
|
||||
array(
|
||||
0: Expr_ConstFetch(
|
||||
0: Stmt_Use(
|
||||
type: TYPE_NORMAL (1)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: Foo
|
||||
)
|
||||
)
|
||||
alias: Foo
|
||||
)
|
||||
)
|
||||
comments: array(
|
||||
0: // Missing NS separator
|
||||
)
|
||||
)
|
||||
1: Expr_ConstFetch(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: Bar
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Expr_ConstFetch(
|
||||
2: Expr_ConstFetch(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: Baz
|
||||
|
47
test/code/parser/stmt/namespace/groupUseTrailingComma.test
Normal file
47
test/code/parser/stmt/namespace/groupUseTrailingComma.test
Normal file
@ -0,0 +1,47 @@
|
||||
Group use can have trailing comma
|
||||
-----
|
||||
<?php
|
||||
use A\{B,};
|
||||
use function A\{b,};
|
||||
-----
|
||||
!!php7
|
||||
array(
|
||||
0: Stmt_GroupUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_NORMAL (1)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: B
|
||||
)
|
||||
)
|
||||
alias: B
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Stmt_GroupUse(
|
||||
type: TYPE_FUNCTION (2)
|
||||
prefix: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
type: TYPE_UNKNOWN (0)
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: b
|
||||
)
|
||||
)
|
||||
alias: b
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
@ -4,6 +4,8 @@ Switch
|
||||
|
||||
switch ($a) {
|
||||
case 0:
|
||||
break;
|
||||
// Comment
|
||||
case 1;
|
||||
default:
|
||||
}
|
||||
@ -27,6 +29,9 @@ array(
|
||||
value: 0
|
||||
)
|
||||
stmts: array(
|
||||
0: Stmt_Break(
|
||||
num: null
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Stmt_Case(
|
||||
@ -35,6 +40,9 @@ array(
|
||||
)
|
||||
stmts: array(
|
||||
)
|
||||
comments: array(
|
||||
0: // Comment
|
||||
)
|
||||
)
|
||||
2: Stmt_Case(
|
||||
cond: null
|
||||
|
53
test/code/prettyPrinter/commentsInCommaList.test
Normal file
53
test/code/prettyPrinter/commentsInCommaList.test
Normal file
@ -0,0 +1,53 @@
|
||||
Comments in arrays and function calls
|
||||
-----
|
||||
<?php
|
||||
|
||||
$arr = [
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar,
|
||||
// Discarded
|
||||
];
|
||||
[
|
||||
// Foo
|
||||
$foo,
|
||||
,
|
||||
// Bar
|
||||
$bar,
|
||||
] = $arr;
|
||||
foo(
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar
|
||||
);
|
||||
new Foo(
|
||||
// Foo
|
||||
$foo
|
||||
);
|
||||
-----
|
||||
!!php7
|
||||
$arr = [
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar,
|
||||
];
|
||||
[
|
||||
// Foo
|
||||
$foo,
|
||||
,
|
||||
// Bar
|
||||
$bar,
|
||||
] = $arr;
|
||||
foo(
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar
|
||||
);
|
||||
new Foo(
|
||||
// Foo
|
||||
$foo
|
||||
);
|
@ -110,7 +110,7 @@ FALSE;
|
||||
0.0;
|
||||
1.0;
|
||||
1.0E+100;
|
||||
INF;
|
||||
\INF;
|
||||
1.0E-100;
|
||||
1.0E+84;
|
||||
378282246310005.0;
|
||||
|
@ -31,5 +31,5 @@ Number literals
|
||||
-42.5;
|
||||
1.0E+42;
|
||||
-1.0E+42;
|
||||
INF;
|
||||
-INF;
|
||||
\INF;
|
||||
-\INF;
|
@ -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);
|
||||
|
@ -1,11 +1,11 @@
|
||||
Nullable types
|
||||
-----
|
||||
<?php
|
||||
function test(?Foo $bar, ?string $foo) : ?Baz
|
||||
function test(?Foo $bar, ?string $foo, ?\Xyz $zyx) : ?Baz
|
||||
{
|
||||
}
|
||||
-----
|
||||
!!php7
|
||||
function test(?Foo $bar, ?string $foo) : ?Baz
|
||||
function test(?Foo $bar, ?string $foo, ?\Xyz $zyx) : ?Baz
|
||||
{
|
||||
}
|
@ -13,6 +13,7 @@ switch ($expr) {
|
||||
case 4:
|
||||
echo 'Third case, return instead of break';
|
||||
return;
|
||||
// Comment
|
||||
default:
|
||||
echo 'Default case';
|
||||
break;
|
||||
@ -29,6 +30,7 @@ switch ($expr) {
|
||||
case 4:
|
||||
echo 'Third case, return instead of break';
|
||||
return;
|
||||
// Comment
|
||||
default:
|
||||
echo 'Default case';
|
||||
break;
|
||||
|
@ -1,4 +1,4 @@
|
||||
wget -q https://github.com/php/php-src/archive/php-7.0.5.tar.gz
|
||||
wget -q https://github.com/php/php-src/archive/php-7.1.0.tar.gz
|
||||
mkdir -p ./data/php-src
|
||||
tar -xzf ./php-7.0.5.tar.gz -C ./data/php-src --strip-components=1
|
||||
tar -xzf ./php-7.1.0.tar.gz -C ./data/php-src --strip-components=1
|
||||
php -n test_old/run.php --verbose --no-progress PHP7 ./data/php-src
|
||||
|
@ -83,18 +83,15 @@ switch ($testType) {
|
||||
| Zend.tests.multibyte.multibyte_encoding_001
|
||||
| Zend.tests.multibyte.multibyte_encoding_004
|
||||
| Zend.tests.multibyte.multibyte_encoding_005
|
||||
# token_get_all bug (https://bugs.php.net/bug.php?id=60097)
|
||||
| Zend.tests.bug47516
|
||||
# pretty print difference due to INF vs 1e1000
|
||||
| ext.standard.tests.general_functions.bug27678
|
||||
| tests.lang.bug24640
|
||||
# pretty print differences due to negative LNumbers
|
||||
| Zend.tests.neg_num_string
|
||||
| Zend.tests.bug72918
|
||||
# pretty print difference due to nop statements
|
||||
| ext.mbstring.tests.htmlent
|
||||
| ext.standard.tests.file.fread_basic
|
||||
# tests using __halt_compiler as semi reserved keyword
|
||||
| Zend.tests.grammar.semi_reserved_001
|
||||
| Zend.tests.grammar.semi_reserved_002
|
||||
| Zend.tests.grammar.semi_reserved_005
|
||||
)\.phpt$~x', $file)) {
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user