mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-07-13 02:16:37 +02:00
Compare commits
260 Commits
v4.10.2
...
v5.0.0alph
Author | SHA1 | Date | |
---|---|---|---|
da65ae474d | |||
8fb716841e | |||
b9974596e0 | |||
64484a4979 | |||
a0ed229b31 | |||
1286059998 | |||
698ff1ca46 | |||
7877e0302d | |||
b3158e0b53 | |||
bbec9db626 | |||
a4728678fc | |||
6649012e6c | |||
2217f14d6e | |||
c62dda9507 | |||
4c3e759a51 | |||
f8fea0997c | |||
57d4a02659 | |||
8e100f1e69 | |||
fcd5934dae | |||
cc34c2450c | |||
68eb1ca9c1 | |||
cb60eda774 | |||
1cb460ae38 | |||
5ad02d8a2c | |||
3bd38c5b2c | |||
e9416a0eae | |||
7b4a8c1ebd | |||
ee3c80f90b | |||
9476cff37d | |||
6a88bdb05a | |||
1eb6b5653e | |||
d24745ddbc | |||
cd3c0c11e4 | |||
f6ddde6428 | |||
47626c74ec | |||
ce3337b0c2 | |||
d83562e6fe | |||
55cc60c286 | |||
2df8878f5d | |||
8ad4129442 | |||
4c4af21df8 | |||
21a3e8cac5 | |||
950bf8f1d1 | |||
4ce9781260 | |||
f077f76557 | |||
b0edd4c411 | |||
bad10e1618 | |||
4bcdf74b8b | |||
46558ed9a5 | |||
5573afc06e | |||
0dd85ebd34 | |||
132690f2f8 | |||
f7b448fa15 | |||
b3ad14b938 | |||
9f9c2ea81b | |||
f98341f688 | |||
fc6b4890ef | |||
032b102146 | |||
40c89cf924 | |||
a099803d01 | |||
c595989e4d | |||
6af204467c | |||
48f470eac7 | |||
43d6332dce | |||
9b46dffb12 | |||
205bd75aa8 | |||
031c5e6ed0 | |||
36b2a996ca | |||
b9fe3449e8 | |||
e9800cf7d3 | |||
2070cb7cb2 | |||
9b5a2c8991 | |||
8dfce13d77 | |||
f59f226f65 | |||
468c0ef6bc | |||
b4b60c8460 | |||
636f066b76 | |||
7362f2b2d0 | |||
96037b3d33 | |||
8d02d37e42 | |||
e68b17cc3b | |||
f63081a57c | |||
44c6a97705 | |||
9a230cd333 | |||
a772853307 | |||
c585a2d766 | |||
8ed76726aa | |||
5f3ad31501 | |||
4917c71a91 | |||
e1345f0c09 | |||
03ccfa3dd4 | |||
a3b0541c71 | |||
c42290ae42 | |||
a44faa6328 | |||
11caa3b9cc | |||
f4ec6a1e53 | |||
23835d20ef | |||
66b20bd6bc | |||
2b562b72a8 | |||
f5b56a5c4c | |||
a2608f0b74 | |||
9dca6f1d37 | |||
035c1c7cd2 | |||
8be56afd2d | |||
0933986293 | |||
bf39f6a4e0 | |||
892b07c428 | |||
9857581ee8 | |||
09c6048df1 | |||
3c3bcd3125 | |||
a5033e3860 | |||
a2753c8218 | |||
dd63ddbc24 | |||
f62b2bfdec | |||
68fc1ba4cb | |||
53b907d405 | |||
f828b76972 | |||
0201a7ee3f | |||
9b2a01aa0c | |||
652fb0c6c1 | |||
ea9d6b2238 | |||
34d8681488 | |||
1f504d2c7d | |||
c55c7a2ac3 | |||
9ef528f3f5 | |||
e61bb11989 | |||
cf0cd6003e | |||
646b490735 | |||
c218db3e16 | |||
84813dc1e4 | |||
59145a4443 | |||
7bf6348240 | |||
a73c8ee03b | |||
1e89658cae | |||
050342b5df | |||
a3f2bb634d | |||
de4ac93023 | |||
4021a63cef | |||
653757bec6 | |||
572af7fff2 | |||
0086a261d0 | |||
5aae65e627 | |||
0ea134a507 | |||
a38a60b7dd | |||
c878d7195d | |||
6e0eec807e | |||
71ed641cd4 | |||
e3ff8cf035 | |||
b0469d127e | |||
23be1f9bd1 | |||
3fd30f64bc | |||
55f29b152c | |||
d3d1297c0d | |||
b4902cefe4 | |||
b6d11da541 | |||
bdd131d3ec | |||
472e163ffa | |||
fe9db376a1 | |||
aff98bbf16 | |||
7c445bb608 | |||
5af93eee52 | |||
27fe7a68c0 | |||
6c0b63d9af | |||
87387caf8f | |||
9c5eb3ccba | |||
e70541f136 | |||
2d589921f2 | |||
5466ee365f | |||
1f416d9052 | |||
cdb731fa8b | |||
e727475d08 | |||
34bea19b6e | |||
678ccbe072 | |||
5d83adcc0e | |||
3bf0082455 | |||
d3eb10aca1 | |||
a6e34665fd | |||
f4b835f7d8 | |||
210577fe3c | |||
f09f22760e | |||
d4cb98ae38 | |||
68d2a52b42 | |||
63f8699143 | |||
4122ff3a91 | |||
6f1f206862 | |||
99a24b6a55 | |||
63a79e8daa | |||
6a21234e58 | |||
7064539974 | |||
4bfc4595ed | |||
54f19a0a66 | |||
8da6d7ac62 | |||
f6e1fbf3a2 | |||
50953a2691 | |||
0a20979a62 | |||
a45fb2a621 | |||
08501991d4 | |||
b5234eacd0 | |||
632ead3a82 | |||
13549aa794 | |||
d2c645f163 | |||
def24f2224 | |||
cfeb195205 | |||
ace6c67a8a | |||
0483391aca | |||
9aebf377fc | |||
5a43015499 | |||
6608f01670 | |||
55c4269232 | |||
c4304c76bd | |||
b099e8fc76 | |||
acf16edc8e | |||
a8b5ed4306 | |||
c758510a37 | |||
feed91cf0f | |||
fe14cf3672 | |||
3fb73520c1 | |||
c35cc4b2cb | |||
e69ebbbfd9 | |||
0b258d9a9e | |||
2d193bb0e4 | |||
49e9951f2c | |||
eccf1bf464 | |||
b67560d388 | |||
4432ba399e | |||
37ac4ea9c2 | |||
4848a0d734 | |||
8eb194ea1f | |||
e03d63cffb | |||
ce91d139b5 | |||
33d7c8d3d8 | |||
6b409b96bb | |||
f68e1a43ff | |||
f767b9fd9f | |||
2a4bb7ef2c | |||
4abdcde5f1 | |||
d46f261ef9 | |||
38aa0920c9 | |||
a8223f228a | |||
8165cf69fa | |||
46221a0914 | |||
3b87eb721c | |||
e3471d94d3 | |||
c6d052fc58 | |||
8008d07bef | |||
7284a4d019 | |||
d3d1ee470a | |||
893a5bce3f | |||
bec74aceda | |||
7c09e096c9 | |||
0f64504317 | |||
5e36ef732e | |||
dbe56d23de | |||
c64986fa55 | |||
bc7a9bf9c2 | |||
2816485126 | |||
1d1bc8a364 | |||
d520bc9e1d | |||
51e0b30843 | |||
eff72eeffa |
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*.y]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
19
.gitattributes
vendored
19
.gitattributes
vendored
@ -1,9 +1,12 @@
|
|||||||
/doc export-ignore
|
/.github export-ignore
|
||||||
/test export-ignore
|
/doc export-ignore
|
||||||
/test_old export-ignore
|
/test export-ignore
|
||||||
.gitattributes export-ignore
|
/test_old export-ignore
|
||||||
.gitignore export-ignore
|
/tools export-ignore
|
||||||
.travis.yml export-ignore
|
.editorconfig export-ignore
|
||||||
CHANGELOG.md export-ignore
|
.gitattributes export-ignore
|
||||||
|
.gitignore export-ignore
|
||||||
|
CHANGELOG.md export-ignore
|
||||||
|
CONTRIBUTING.md export-ignore
|
||||||
phpunit.xml.dist export-ignore
|
phpunit.xml.dist export-ignore
|
||||||
UPGRADE-*.md export-ignore
|
UPGRADE-*.md export-ignore
|
||||||
|
108
.github/workflows/main.yml
vendored
Normal file
108
.github/workflows/main.yml
vendored
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
|
||||||
|
name: Main
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests_coverage:
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
name: "PHP 7.1 Unit Tests (with coverage)"
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: "actions/checkout@v2"
|
||||||
|
- name: "Install PHP"
|
||||||
|
uses: "shivammathur/setup-php@v2"
|
||||||
|
with:
|
||||||
|
coverage: "xdebug"
|
||||||
|
php-version: "7.1"
|
||||||
|
tools: composer:v2
|
||||||
|
- name: "Install dependencies"
|
||||||
|
run: |
|
||||||
|
composer require php-coveralls/php-coveralls:^2.2 --dev --no-update
|
||||||
|
composer update --no-progress --prefer-dist
|
||||||
|
- name: "Tests"
|
||||||
|
run: "php vendor/bin/phpunit --coverage-clover build/logs/clover.xml"
|
||||||
|
- name: Coveralls
|
||||||
|
env:
|
||||||
|
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: "php vendor/bin/php-coveralls"
|
||||||
|
if: ${{ success() }}
|
||||||
|
tests:
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
name: "PHP ${{ matrix.php-version }} Unit Tests"
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
php-version:
|
||||||
|
- "7.2"
|
||||||
|
- "7.3"
|
||||||
|
- "7.4"
|
||||||
|
- "8.0"
|
||||||
|
- "8.1"
|
||||||
|
- "8.2"
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: "actions/checkout@v2"
|
||||||
|
- name: "Install PHP"
|
||||||
|
uses: "shivammathur/setup-php@v2"
|
||||||
|
with:
|
||||||
|
coverage: "none"
|
||||||
|
php-version: "${{ matrix.php-version }}"
|
||||||
|
tools: composer:v2
|
||||||
|
- name: "Install dependencies"
|
||||||
|
run: "composer update --no-progress --prefer-dist ${{ matrix.flags }}"
|
||||||
|
- name: "PHPUnit"
|
||||||
|
run: "php vendor/bin/phpunit"
|
||||||
|
test_old_73_80:
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
name: "PHP 7.3 Code on PHP 8.0 Integration Tests"
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: "actions/checkout@v2"
|
||||||
|
- name: "Install PHP"
|
||||||
|
uses: "shivammathur/setup-php@v2"
|
||||||
|
with:
|
||||||
|
coverage: "none"
|
||||||
|
php-version: "8.0"
|
||||||
|
tools: composer:v2
|
||||||
|
- name: "Install PHP 8 dependencies"
|
||||||
|
run: "composer update --no-progress --prefer-dist"
|
||||||
|
- name: "Tests"
|
||||||
|
run: "test_old/run-php-src.sh 7.3.21"
|
||||||
|
test_old_80_70:
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
name: "PHP 8.2 Code on PHP 7.1 Integration Tests"
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: "actions/checkout@v2"
|
||||||
|
- name: "Install PHP"
|
||||||
|
uses: "shivammathur/setup-php@v2"
|
||||||
|
with:
|
||||||
|
coverage: "none"
|
||||||
|
php-version: "7.1"
|
||||||
|
tools: composer:v2
|
||||||
|
- name: "Install PHP 8 dependencies"
|
||||||
|
run: "composer update --no-progress --prefer-dist"
|
||||||
|
- name: "Tests"
|
||||||
|
run: "test_old/run-php-src.sh 8.2.3"
|
||||||
|
phpstan:
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
name: "PHP ${{ matrix.php-version }} PHPStan"
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
php-version:
|
||||||
|
- "8.2"
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: "actions/checkout@v2"
|
||||||
|
- name: "Install PHP"
|
||||||
|
uses: "shivammathur/setup-php@v2"
|
||||||
|
with:
|
||||||
|
coverage: "none"
|
||||||
|
php-version: "${{ matrix.php-version }}"
|
||||||
|
tools: composer:v2
|
||||||
|
- name: "Install dependencies"
|
||||||
|
run: |
|
||||||
|
cd tools && composer install
|
||||||
|
- name: "PHPStan"
|
||||||
|
run: "php tools/vendor/bin/phpstan"
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,7 @@
|
|||||||
|
.idea/
|
||||||
vendor/
|
vendor/
|
||||||
composer.lock
|
composer.lock
|
||||||
grammar/kmyacc.exe
|
grammar/kmyacc.exe
|
||||||
grammar/y.output
|
grammar/y.output
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
|
.php-cs-fixer.cache
|
||||||
|
26
.php-cs-fixer.dist.php
Normal file
26
.php-cs-fixer.dist.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$finder = PhpCsFixer\Finder::create()
|
||||||
|
->exclude('PhpParser/Parser')
|
||||||
|
->in(__DIR__ . '/lib')
|
||||||
|
->in(__DIR__ . '/test')
|
||||||
|
->in(__DIR__ . '/grammar')
|
||||||
|
;
|
||||||
|
|
||||||
|
$config = new PhpCsFixer\Config();
|
||||||
|
return $config->setRiskyAllowed(true)
|
||||||
|
->setRules([
|
||||||
|
'@PSR12' => true,
|
||||||
|
// We use PSR12 with consistent brace placement.
|
||||||
|
'curly_braces_position' => [
|
||||||
|
'functions_opening_brace' => 'same_line',
|
||||||
|
'classes_opening_brace' => 'same_line',
|
||||||
|
],
|
||||||
|
// declare(strict_types=1) on the same line as <?php.
|
||||||
|
'blank_line_after_opening_tag' => false,
|
||||||
|
'declare_strict_types' => true,
|
||||||
|
// Keep argument formatting for now.
|
||||||
|
'method_argument_space' => ['on_multiline' => 'ignore'],
|
||||||
|
])
|
||||||
|
->setFinder($finder)
|
||||||
|
;
|
43
.travis.yml
43
.travis.yml
@ -1,43 +0,0 @@
|
|||||||
language: php
|
|
||||||
dist: xenial
|
|
||||||
|
|
||||||
before_install: composer self-update --2
|
|
||||||
|
|
||||||
install: composer update --no-progress --prefer-dist
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- name: PHP 7.0 Unit Tests
|
|
||||||
php: 7.0
|
|
||||||
install:
|
|
||||||
- composer require php-coveralls/php-coveralls:^2.2 --dev --no-update
|
|
||||||
- composer update --no-progress --prefer-dist
|
|
||||||
script: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
|
|
||||||
after_success: php vendor/bin/coveralls
|
|
||||||
- name: PHP 7.1 Unit Tests
|
|
||||||
php: 7.1
|
|
||||||
- name: PHP 7.2 Unit Tests
|
|
||||||
php: 7.2
|
|
||||||
- name: PHP 7.3 Unit Tests
|
|
||||||
php: 7.3
|
|
||||||
- name: PHP 7.4 Unit Tests
|
|
||||||
php: 7.4
|
|
||||||
- name: PHP 8.0 Unit Tests
|
|
||||||
php: nightly
|
|
||||||
install:
|
|
||||||
- composer update --ignore-platform-req=php --no-progress --prefer-dist
|
|
||||||
- name: PHP 7.3 Code on PHP 8.0 Integration Tests
|
|
||||||
php: nightly
|
|
||||||
install:
|
|
||||||
- composer update --ignore-platform-req=php --no-progress --prefer-dist
|
|
||||||
script:
|
|
||||||
- test_old/run-php-src.sh 7.3.21
|
|
||||||
- name: PHP 8.0 Code on PHP 7.0 Integration Tests
|
|
||||||
php: 7.0
|
|
||||||
script:
|
|
||||||
- test_old/run-php-src.sh 8.0.0beta4
|
|
||||||
allow_failures:
|
|
||||||
- name: PHP 8.0 Code on PHP 7.0 Integration Tests
|
|
||||||
fast_finish: true
|
|
||||||
|
|
||||||
script: vendor/bin/phpunit
|
|
215
CHANGELOG.md
215
CHANGELOG.md
@ -1,7 +1,216 @@
|
|||||||
Version 4.10.3-dev
|
Version 5.0.0-alpha2 (2023-03-05)
|
||||||
------------------
|
---------------------------------
|
||||||
|
|
||||||
|
See UPGRADE-5.0 for detailed migration instructions.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* [PHP 8.3] Added support for dynamic class constant fetch.
|
||||||
|
* Added many additional type annotations. PhpStan is now used.
|
||||||
|
* Added a fuzzing target for PHP-Fuzzer, which was how a lot of pretty printer bugs were found.
|
||||||
|
* Added `isPromoted()`, `isPublic()`, `isProtected()`, `isPrivate()` and `isReadonly()` methods
|
||||||
|
on `Param`.
|
||||||
|
* Added support for class constants in trait builder.
|
||||||
|
* Added `PrettyPrinter` interface.
|
||||||
|
* Added support for formatting preservation when toggling static modifiers.
|
||||||
|
* The `php-parse` binary now accepts `-` as the file name, in which case it will read from stdin.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* The pretty printer now uses a more accurate treatment of unary operator precedence, and will only
|
||||||
|
wrap them in parentheses if required. This allowed fixing a number of other precedence related
|
||||||
|
bugs.
|
||||||
|
* The pretty printer now respects the precedence of `clone`, `throw` and arrow functions.
|
||||||
|
* The pretty printer no longer unconditionally wraps `yield` in parentheses, unless the target
|
||||||
|
version is set to older than PHP 7.0.
|
||||||
|
* Fixed formatting preservation for alternative elseif/else syntax.
|
||||||
|
* Fixed checks for when it is safe to print strings as heredoc/nowdoc to accommodate flexible
|
||||||
|
doc string semantics.
|
||||||
|
* The pretty printer now prints parentheses around new/instanceof operands in all required
|
||||||
|
situations.
|
||||||
|
* Similar, differences in allowed expressions on the LHS of `->` and `::` are now taken into account.
|
||||||
|
* Fixed various cases where `\r` at the end of a doc string could be incorrectly merged into a CRLF
|
||||||
|
sequence with a following `\n`.
|
||||||
|
* `__halt_compiler` is no longer recognized as a semi-reserved keyword, in line with PHP behavior.
|
||||||
|
* `<?=` is no longer recognized as a semi-reserved keyword.
|
||||||
|
* Fix handling of very large overflowing `\u` escape sequences.
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
* Removed deprecated `Error` constructor taking a line number instead of an attributes array.
|
||||||
|
|
||||||
|
Version 5.0.0-alpha1 (2022-09-04)
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
See UPGRADE-5.0 for detailed migration instructions.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* PHP 7.1 is now required to run PHP-Parser.
|
||||||
|
* Formatting of the standard pretty printer has been adjusted to match PSR-12 more closely.
|
||||||
|
* The internal token representation now uses a `PhpParser\Token` class, which is compatible with
|
||||||
|
PHP 8 token representation (`PhpToken`).
|
||||||
|
* Destructuring is now always represented using `Expr\List_` nodes, even if it uses `[]` syntax.
|
||||||
|
* Renamed a number of node classes, and moved things that were not real expressions/statements
|
||||||
|
outside the `Expr`/`Stmt` hierarchy. Compatibility shims for the old names have been retained.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Added `PhpVersion` class, which is accepted in a number of places (e.g. ParserFactory, Parser,
|
||||||
|
Lexer, PrettyPrinter) and gives more precise control over the PHP version being targeted.
|
||||||
|
* Added PHP 8 parser though it only differs from the PHP 7 parser in concatenation precedence.
|
||||||
|
* Added `Parser::getLexer()` method.
|
||||||
|
* Added a `Modifiers` class, as a replacement for `Stmt\Class_::MODIFIER_*`.
|
||||||
|
* Added support for returning an array or `REMOVE_NODE` from `NodeVisitor::enterNode()`.
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
* The PHP 5 parser has been removed. The PHP 7 parser has been adjusted to deal with PHP 5 code
|
||||||
|
more gracefully.
|
||||||
|
|
||||||
|
Version 4.15.1 (2022-09-04)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed formatting preservation when adding *multiple* attributes to a class/method/etc that
|
||||||
|
previously had none. This fixes a regression in the 4.15.0 release.
|
||||||
|
|
||||||
|
Version 4.15.0 (2022-09-03)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* PHP 8.2: Added support for `true` type.
|
||||||
|
* PHP 8.2: Added support for DNF types.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Support `readonly` as a function name.
|
||||||
|
* Added `__serialize` and `__unserialize` to magic method list.
|
||||||
|
* Fixed bounds check in `Name::slice()`.
|
||||||
|
* Fixed formatting preservation when adding attributes to a class/method/etc that previously had none.
|
||||||
|
|
||||||
|
Version 4.14.0 (2022-05-31)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Added support for readonly classes.
|
||||||
|
* Added `rawValue` attribute to `LNumber`, `DNumber` and `String_` nodes, which stores the unparsed
|
||||||
|
value of the literal (e.g. `"1_000"` rather than `1000`).
|
||||||
|
|
||||||
|
Version 4.13.2 (2021-11-30)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Added builders for enums and enum cases.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* NullsafeMethodCall now extends from CallLike.
|
||||||
|
* The `namespacedName` property populated by the `NameResolver` is now declared on relevant nodes,
|
||||||
|
to avoid a dynamic property deprecation warning with PHP 8.2.
|
||||||
|
|
||||||
|
Version 4.13.1 (2021-11-03)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Support reserved keywords as enum cases.
|
||||||
|
* Support array unpacking in constant expression evaluator.
|
||||||
|
|
||||||
|
Version 4.13.0 (2021-09-20)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* [PHP 8.1] Added support for intersection types using a new `IntersectionType` node. Additionally
|
||||||
|
a `ComplexType` parent class for `NullableType`, `UnionType` and `IntersectionType` has been
|
||||||
|
added.
|
||||||
|
* [PHP 8.1] Added support for explicit octal literals.
|
||||||
|
* [PHP 8.1] Added support for first-class callables. These are represented using a call whose first
|
||||||
|
argument is a `VariadicPlaceholder`. The representation is intended to be forward-compatible with
|
||||||
|
partial function application, just like the PHP feature itself. Call nodes now extend from
|
||||||
|
`Expr\CallLike`, which provides an `isFirstClassCallable()` method to determine whether a
|
||||||
|
placeholder id present. `getArgs()` can be used to assert that the call is not a first-class
|
||||||
|
callable and returns `Arg[]` rather than `array<Arg|VariadicPlaceholder>`.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Multiple modifiers for promoted properties are now accepted. In particular this allows something
|
||||||
|
like `public readonly` for promoted properties.
|
||||||
|
* Formatting-preserving pretty printing for comments in array literals has been fixed.
|
||||||
|
|
||||||
|
Version 4.12.0 (2021-07-21)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* [PHP 8.1] Added support for readonly properties (through a new `MODIFIER_READONLY`).
|
||||||
|
* [PHP 8.1] Added support for final class constants.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed compatibility with PHP 8.1. `&` tokens are now canonicalized to the
|
||||||
|
`T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG` and `T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG` tokens
|
||||||
|
used in PHP 8.1. This happens unconditionally, regardless of whether the emulative lexer is used.
|
||||||
|
|
||||||
|
Version 4.11.0 (2021-07-03)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* `BuilderFactory::args()` now accepts named arguments.
|
||||||
|
* `BuilderFactory::attribute()` has been added.
|
||||||
|
* An `addAttribute()` method accepting an `Attribute` or `AttributeGroup` has been adde to all
|
||||||
|
builders that accept attributes, such as `Builder\Class_`.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* `NameResolver` now handles enums.
|
||||||
|
* `PrettyPrinter` now prints backing enum type.
|
||||||
|
* Builder methods for types now property handle `never` type.
|
||||||
|
|
||||||
|
Version 4.10.5 (2021-05-03)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* [PHP 8.1] Added support for enums. These are represented using the `Stmt\Enum_` and
|
||||||
|
`Stmt\EnumCase` nodes.
|
||||||
|
* [PHP 8.1] Added support for never type. This type will now be returned as an `Identifier` rather
|
||||||
|
than `Name`.
|
||||||
|
* Added `ClassConst` builder.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Non-UTF-8 code units in strings will now be hex-encoded.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed precedence of arrow functions.
|
||||||
|
|
||||||
|
Version 4.10.4 (2020-12-20)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed position information for variable-variables (#741).
|
||||||
|
* Fixed position information for traits/interfaces preceded by if statement (#738).
|
||||||
|
|
||||||
|
Version 4.10.3 (2020-12-03)
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed formatting-preserving pretty printing for `"{$x}"`.
|
||||||
|
* Ternary expressions are now treated as non-associative in the pretty printer, in order to
|
||||||
|
generate code that is compatible with the parentheses requirement introduced in PHP 8.
|
||||||
|
* Removed no longer necessary `error_clear_last()` call in lexer, which may interfere with fatal
|
||||||
|
error handlers if invoked during shutdown.
|
||||||
|
|
||||||
Nothing yet.
|
|
||||||
|
|
||||||
Version 4.10.2 (2020-09-26)
|
Version 4.10.2 (2020-09-26)
|
||||||
------------------
|
------------------
|
||||||
|
4
CONTRIBUTING.md
Normal file
4
CONTRIBUTING.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
## Coding Style
|
||||||
|
|
||||||
|
This project uses PSR-12 with consistent brace placement. This means that the opening brace is
|
||||||
|
always on the same line, even for class and method declarations.
|
17
README.md
17
README.md
@ -1,12 +1,14 @@
|
|||||||
PHP Parser
|
PHP Parser
|
||||||
==========
|
==========
|
||||||
|
|
||||||
[](https://travis-ci.org/nikic/PHP-Parser) [](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
|
[](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
|
||||||
|
|
||||||
This is a PHP 5.2 to PHP 7.4 parser written in PHP. Its purpose is to simplify static code analysis and
|
This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and
|
||||||
manipulation.
|
manipulation.
|
||||||
|
|
||||||
[**Documentation for version 4.x**][doc_master] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.4).
|
[Documentation for version 5.x][doc_master] (in development; for running on PHP >= 7.1; for parsing PHP 7.0 to PHP 8.2, with limited support for parsing PHP 5.x).
|
||||||
|
|
||||||
|
[**Documentation for version 4.x**][doc_4_x] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.2).
|
||||||
|
|
||||||
[Documentation for version 3.x][doc_3_x] (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
|
[Documentation for version 3.x][doc_3_x] (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
|
||||||
|
|
||||||
@ -15,12 +17,12 @@ Features
|
|||||||
|
|
||||||
The main features provided by this library are:
|
The main features provided by this library are:
|
||||||
|
|
||||||
* Parsing PHP 5 and PHP 7 code into an abstract syntax tree (AST).
|
* Parsing PHP 7, and PHP 8 code into an abstract syntax tree (AST).
|
||||||
* Invalid code can be parsed into a partial AST.
|
* Invalid code can be parsed into a partial AST.
|
||||||
* The AST contains accurate location information.
|
* The AST contains accurate location information.
|
||||||
* Dumping the AST in human-readable form.
|
* Dumping the AST in human-readable form.
|
||||||
* Converting an AST back to PHP code.
|
* Converting an AST back to PHP code.
|
||||||
* Experimental: Formatting can be preserved for partially changed ASTs.
|
* Formatting can be preserved for partially changed ASTs.
|
||||||
* Infrastructure to traverse and modify ASTs.
|
* Infrastructure to traverse and modify ASTs.
|
||||||
* Resolution of namespaced names.
|
* Resolution of namespaced names.
|
||||||
* Evaluation of constant expressions.
|
* Evaluation of constant expressions.
|
||||||
@ -51,7 +53,7 @@ function test($foo)
|
|||||||
}
|
}
|
||||||
CODE;
|
CODE;
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||||
try {
|
try {
|
||||||
$ast = $parser->parse($code);
|
$ast = $parser->parse($code);
|
||||||
} catch (Error $error) {
|
} catch (Error $error) {
|
||||||
@ -215,11 +217,12 @@ Component documentation:
|
|||||||
* [JSON representation](doc/component/JSON_representation.markdown)
|
* [JSON representation](doc/component/JSON_representation.markdown)
|
||||||
* JSON encoding and decoding of ASTs
|
* JSON encoding and decoding of ASTs
|
||||||
* [Performance](doc/component/Performance.markdown)
|
* [Performance](doc/component/Performance.markdown)
|
||||||
* Disabling XDebug
|
* Disabling Xdebug
|
||||||
* Reusing objects
|
* Reusing objects
|
||||||
* Garbage collection impact
|
* Garbage collection impact
|
||||||
* [Frequently asked questions](doc/component/FAQ.markdown)
|
* [Frequently asked questions](doc/component/FAQ.markdown)
|
||||||
* Parent and sibling references
|
* Parent and sibling references
|
||||||
|
|
||||||
[doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
|
[doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
|
||||||
|
[doc_4_x]: https://github.com/nikic/PHP-Parser/tree/4.x/doc
|
||||||
[doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc
|
[doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc
|
||||||
|
223
UPGRADE-5.0.md
Normal file
223
UPGRADE-5.0.md
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
Upgrading from PHP-Parser 4.x to 5.0
|
||||||
|
====================================
|
||||||
|
|
||||||
|
### PHP version requirements
|
||||||
|
|
||||||
|
PHP-Parser now requires PHP 7.1 or newer to run. It is however still possible to *parse* code for older versions, while running on a newer version.
|
||||||
|
|
||||||
|
### PHP 5 parsing support
|
||||||
|
|
||||||
|
The dedicated parser for PHP 5 has been removed. The PHP 7 parser now accepts a `PhpVersion` argument, which can be used to improve compatibility with older PHP versions.
|
||||||
|
|
||||||
|
In particular, if an older `PhpVersion` is specified, then:
|
||||||
|
|
||||||
|
* For versions before PHP 7.0, `$foo =& new Bar()` assignments are allowed without error.
|
||||||
|
* For versions before PHP 7.0, invalid octal literals `089` are allowed without error.
|
||||||
|
* Type hints are interpreted as a class `Name` or as a built-in `Identifier` depending on PHP
|
||||||
|
version, for example `int` is treated as a class name on PHP 5.6 and as a built-in on PHP 7.0.
|
||||||
|
|
||||||
|
However, some aspects of PHP 5 parsing are no longer supported:
|
||||||
|
|
||||||
|
* Some variables like `$$foo[0]` are valid in both PHP 5 and PHP 7, but have different interpretation. In that case, the PHP 7 AST will always be constructed (`($$foo)[0]` rather than `${$foo[0]}`).
|
||||||
|
* Declarations of the form `global $$var[0]` are not supported in PHP 7 and will cause a parse error. In error recovery mode, it is possible to continue parsing after such declarations.
|
||||||
|
* The PHP 7 parser will accept many constructs that are not valid in PHP 5. However, this was also true of the dedicated PHP 5 parser.
|
||||||
|
|
||||||
|
The following symbols are affected by this removal:
|
||||||
|
|
||||||
|
* The `PhpParser\Parser\Php5` class has been removed.
|
||||||
|
* The `PhpParser\Parser\Multiple` class has been removed. While not strictly related to PHP 5 support, this functionality is no longer useful without it.
|
||||||
|
* The `PhpParser\ParserFactory::ONLY_PHP5` and `PREFER_PHP5` options have been removed.
|
||||||
|
* The `PhpParser\ParserFactory::PREFER_PHP7` option is now equivalent to `ONLY_PHP7`.
|
||||||
|
|
||||||
|
### Changes to the parser factory
|
||||||
|
|
||||||
|
The `ParserFactory::create()` method is deprecated in favor of three new methods that provide more fine-grained control over the PHP version being targeted:
|
||||||
|
|
||||||
|
* `createForNewestSupportedVersion()`: Use this if you don't know the PHP version of the code you're parsing. It's better to assume a too new version than a too old one.
|
||||||
|
* `createForHostVersion()`: Use this if you're parsing code for the PHP version you're running on.
|
||||||
|
* `createForVersion()`: Use this if you know the PHP version of the code you want to parse.
|
||||||
|
|
||||||
|
In all cases, the PHP version is a fairly weak hint that is only used on a best-effort basis. The parser will usually accept code for newer versions if it does not have any backwards-compatibility implications.
|
||||||
|
|
||||||
|
For example, if you specify version `"8.0"`, then `class ReadOnly {}` is treated as a valid class declaration, while using `public readonly int $prop` will lead to a parse error. However, `final public const X = Y;` will be accepted in both cases.
|
||||||
|
|
||||||
|
```php
|
||||||
|
use PhpParser\ParserFactory;
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
|
$factory = new ParserFactory;
|
||||||
|
|
||||||
|
# Before
|
||||||
|
$parser = $factory->create(ParserFactory::PREFER_PHP7);
|
||||||
|
|
||||||
|
# After (this is roughly equivalent to PREFER_PHP7 behavior)
|
||||||
|
$parser = $factory->createForNewestSupportedVersion();
|
||||||
|
# Or
|
||||||
|
$parser = $factory->createForHostVersion();
|
||||||
|
|
||||||
|
# Before
|
||||||
|
$parser = $factory->create(ParserFactory::ONLY_PHP5);
|
||||||
|
# After (supported on a best-effort basis)
|
||||||
|
$parser = $factory->createForVersion(PhpVersion::fromString("5.6"));
|
||||||
|
```
|
||||||
|
|
||||||
|
### Changes to the array destructuring representation
|
||||||
|
|
||||||
|
Previously, the `list($x) = $y` destructuring syntax was represented using a `Node\Expr\List_`
|
||||||
|
node, while `[$x] = $y` used a `Node\Expr\Array_` node, the same used for the creation (rather than
|
||||||
|
destructuring) of arrays.
|
||||||
|
|
||||||
|
Now, destructuring is always represented using `Node\Expr\List_`. The `kind` attribute with value
|
||||||
|
`Node\Expr\List_::KIND_LIST` or `Node\Expr\List_::KIND_ARRAY` specifies which syntax was actually
|
||||||
|
used.
|
||||||
|
|
||||||
|
### Renamed nodes
|
||||||
|
|
||||||
|
A number of AST nodes have been renamed or moved in the AST hierarchy:
|
||||||
|
|
||||||
|
* `Node\Scalar\LNumber` is now `Node\Scalar\Int_`.
|
||||||
|
* `Node\Scalar\DNumber` is now `Node\Scalar\Float_`.
|
||||||
|
* `Node\Scalar\Encapsed` is now `Node\Scalar\InterpolatedString`.
|
||||||
|
* `Node\Scalar\EncapsedStringPart` is now `Node\InterpolatedStringPart` and no longer extends
|
||||||
|
`Node\Scalar` or `Node\Expr`.
|
||||||
|
* `Node\Expr\ArrayItem` is now `Node\ArrayItem` and no longer extends `Node\Expr`.
|
||||||
|
* `Node\Expr\ClosureUse` is now `Node\ClosureUse` and no longer extends `Node\Expr`.
|
||||||
|
* `Node\Stmt\DeclareDeclare` is now `Node\DeclareItem` and no longer extends `Node\Stmt`.
|
||||||
|
* `Node\Stmt\PropertyProperty` is now `Node\PropertyItem` and no longer extends `Node\Stmt`.
|
||||||
|
* `Node\Stmt\StaticVar` is now `Node\StaticVar` and no longer extends `Node\Stmt`.
|
||||||
|
* `Node\Stmt\UseUse` is now `Node\UseItem` and no longer extends `Node\Stmt`.
|
||||||
|
|
||||||
|
The old class names have been retained as aliases for backwards compatibility. However, the `Node::getType()` method will now always return the new name (e.g. `ClosureUse` instead of `Expr_ClosureUse`).
|
||||||
|
|
||||||
|
### Modifiers
|
||||||
|
|
||||||
|
Modifier flags (as used by the `$flags` subnode of `Class_`, `ClassMethod`, `Property`, etc.) are now available as class constants on a separate `PhpParser\Modifiers` class, instead of being part of `PhpParser\Node\Stmt\Class_`, to make it clearer that these are used by many different nodes. The old constants are deprecated, but are still available.
|
||||||
|
|
||||||
|
```
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC -> PhpParser\Modifiers::PUBLIC
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_PROTECTED -> PhpParser\Modifiers::PROTECTED
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE -> PhpParser\Modifiers::PRIVATE
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_STATIC -> PhpParser\Modifiers::STATIC
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_ABSTRACT -> PhpParser\Modifiers::ABSTRACT
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_FINAL -> PhpParser\Modifiers::FINAL
|
||||||
|
PhpParser\Node\Stmt\Class_::MODIFIER_READONLY -> PhpParser\Modifiers::READONLY
|
||||||
|
PhpParser\Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK -> PhpParser\Modifiers::VISIBILITY_MASK
|
||||||
|
```
|
||||||
|
|
||||||
|
### Changes to the pretty printer
|
||||||
|
|
||||||
|
A number of changes to the standard pretty printer have been made, to make it match contemporary coding style conventions (and in particular PSR-12). Options to restore the previous behavior are not provided, but it is possible to override the formatting methods (such as `pStmt_ClassMethod`) with your preferred formatting.
|
||||||
|
|
||||||
|
Return types are now formatted without a space before the `:`:
|
||||||
|
|
||||||
|
```php
|
||||||
|
# Before
|
||||||
|
function test() : Type
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
# After
|
||||||
|
function test(): Type
|
||||||
|
{
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`abstract` and `final` are now printed before visibility modifiers:
|
||||||
|
|
||||||
|
```php
|
||||||
|
# Before
|
||||||
|
public abstract function test();
|
||||||
|
|
||||||
|
# After
|
||||||
|
abstract public function test();
|
||||||
|
```
|
||||||
|
|
||||||
|
A space is now printed between `use` and the following `(` for closures:
|
||||||
|
|
||||||
|
```php
|
||||||
|
# Before
|
||||||
|
function () use($var) {
|
||||||
|
};
|
||||||
|
|
||||||
|
# After
|
||||||
|
function () use ($var) {
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Backslashes in single-quoted strings are now only printed if they are necessary:
|
||||||
|
|
||||||
|
```php
|
||||||
|
# Before
|
||||||
|
'Foo\\Bar';
|
||||||
|
'\\\\';
|
||||||
|
|
||||||
|
# After
|
||||||
|
'Foo\Bar';
|
||||||
|
'\\\\';
|
||||||
|
```
|
||||||
|
|
||||||
|
The pretty printer now accepts a `phpVersion` option, which accepts a `PhpVersion` object and defaults to PHP 7.0. The pretty printer will make formatting choices to make the code valid for that version. It currently controls the following behavior:
|
||||||
|
|
||||||
|
* For PHP >= 7.0 (default), short array syntax `[]` will be used by default. This does not affect nodes that specify an explicit array syntax using the `kind` attribute.
|
||||||
|
* For PHP >= 7.0 (default), parentheses around `yield` expressions will only be printed when necessary. Previously, parentheses were always printed, even if `yield` was used as a statement.
|
||||||
|
* For PHP >= 7.1, the short array syntax `[]` will be used for destructuring by default (instead of
|
||||||
|
`list()`). This does not affect nodes that specify and explicit syntax using the `kind` attribute.
|
||||||
|
* For PHP >= 7.3, a newline is no longer forced after heredoc/nowdoc strings, as the requirement for this has been removed with the introduction of flexible heredoc/nowdoc strings.
|
||||||
|
|
||||||
|
### Changes to precedence handling in the pretty printer
|
||||||
|
|
||||||
|
The pretty printer now more accurately models operator precedence. Especially for unary operators, less unnecessary parentheses will be printed. Conversely, many bugs where semantically meaningful parentheses were omitted have been fixed.
|
||||||
|
|
||||||
|
To support these changes, precedence is now handled differently in the pretty printer. The internal `p()` method, which is used to recursively print nodes, now has the following signature:
|
||||||
|
```php
|
||||||
|
protected function p(
|
||||||
|
Node $node, int $precedence = self::MAX_PRECEDENCE, int $lhsPrecedence = self::MAX_PRECEDENCE,
|
||||||
|
bool $parentFormatPreserved = false
|
||||||
|
): string;
|
||||||
|
```
|
||||||
|
|
||||||
|
The `$precedence` is the precedence of the direct parent operator (if any), while `$lhsPrecedence` is that precedence of the nearest binary operator on whose left-hand-side the node occurs. For unary operators, only the `$lhsPrecedence` is relevant.
|
||||||
|
|
||||||
|
Recursive calls in pretty-printer methods should generally continue calling `p()` without additional parameters. However, pretty-printer methods for operators that participate in precedence resolution need to be adjusted. For example, typical implementations for operators looks as follows now:
|
||||||
|
|
||||||
|
```php
|
||||||
|
protected function pExpr_BinaryOp_Plus(
|
||||||
|
BinaryOp\Plus $node, int $precedence, int $lhsPrecedence
|
||||||
|
): string {
|
||||||
|
return $this->pInfixOp(
|
||||||
|
BinaryOp\Plus::class, $node->left, ' + ', $node->right, $precedence, $lhsPrecedence);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function pExpr_UnaryPlus(
|
||||||
|
Expr\UnaryPlus $node, int $precedence, int $lhsPrecedence
|
||||||
|
): string {
|
||||||
|
return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr, $precedence, $lhsPrecedence);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The new `$precedence` and `$lhsPrecedence` arguments need to be passed down to the `pInfixOp()`, `pPrefixOp()` and `pPostfixOp()` methods.
|
||||||
|
|
||||||
|
### Changes to token representation
|
||||||
|
|
||||||
|
Tokens are now internally represented using the `PhpParser\Token` class, which exposes the same base interface as
|
||||||
|
the `PhpToken` class introduced in PHP 8.0. On PHP 8.0 or newer, `PhpParser\Token` extends from `PhpToken`, otherwise
|
||||||
|
it extends from a polyfill implementation. The most important parts of the interface may be summarized as follows:
|
||||||
|
|
||||||
|
```php
|
||||||
|
class Token {
|
||||||
|
public int $id;
|
||||||
|
public string $text;
|
||||||
|
public int $line;
|
||||||
|
public int $pos;
|
||||||
|
|
||||||
|
public function is(int|string|array $kind): bool;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `Lexer::getTokens()` method will now return an array of `Token`s, rather than an array of arrays and strings.
|
||||||
|
Additionally, the token array is now terminated by a sentinel token with ID 0.
|
||||||
|
|
||||||
|
### Other removed functionality
|
||||||
|
|
||||||
|
* The deprecated `Builder\Param::setTypeHint()` method has been removed in favor of `Builder\Param::setType()`.
|
||||||
|
* The deprecated `Error` constructor taking a start line has been removed. Pass `['startLine' => $startLine]` attributes instead.
|
@ -10,7 +10,7 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
|
|||||||
|
|
||||||
ini_set('xdebug.max_nesting_level', 3000);
|
ini_set('xdebug.max_nesting_level', 3000);
|
||||||
|
|
||||||
// Disable XDebug var_dump() output truncation
|
// Disable Xdebug var_dump() output truncation
|
||||||
ini_set('xdebug.var_display_max_children', -1);
|
ini_set('xdebug.var_display_max_children', -1);
|
||||||
ini_set('xdebug.var_display_max_data', -1);
|
ini_set('xdebug.var_display_max_data', -1);
|
||||||
ini_set('xdebug.var_display_max_depth', -1);
|
ini_set('xdebug.var_display_max_depth', -1);
|
||||||
@ -26,13 +26,10 @@ if (empty($files)) {
|
|||||||
showHelp("Must specify at least one file.");
|
showHelp("Must specify at least one file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
|
$lexerOptions = ['usedAttributes' => [
|
||||||
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
|
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
|
||||||
]]);
|
]];
|
||||||
$parser = (new PhpParser\ParserFactory)->create(
|
$parser = (new PhpParser\ParserFactory())->createForVersion($attributes['version'], $lexerOptions);
|
||||||
PhpParser\ParserFactory::PREFER_PHP7,
|
|
||||||
$lexer
|
|
||||||
);
|
|
||||||
$dumper = new PhpParser\NodeDumper([
|
$dumper = new PhpParser\NodeDumper([
|
||||||
'dumpComments' => true,
|
'dumpComments' => true,
|
||||||
'dumpPositions' => $attributes['with-positions'],
|
'dumpPositions' => $attributes['with-positions'],
|
||||||
@ -43,7 +40,10 @@ $traverser = new PhpParser\NodeTraverser();
|
|||||||
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
|
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
|
||||||
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
if (strpos($file, '<?php') === 0) {
|
if ($file === '-') {
|
||||||
|
$code = file_get_contents('php://stdin');
|
||||||
|
fwrite(STDERR, "====> Stdin:\n");
|
||||||
|
} else if (strpos($file, '<?php') === 0) {
|
||||||
$code = $file;
|
$code = $file;
|
||||||
fwrite(STDERR, "====> Code $code\n");
|
fwrite(STDERR, "====> Code $code\n");
|
||||||
} else {
|
} else {
|
||||||
@ -108,7 +108,7 @@ function showHelp($error = '') {
|
|||||||
if ($error) {
|
if ($error) {
|
||||||
fwrite(STDERR, $error . "\n\n");
|
fwrite(STDERR, $error . "\n\n");
|
||||||
}
|
}
|
||||||
fwrite($error ? STDERR : STDOUT, <<<OUTPUT
|
fwrite($error ? STDERR : STDOUT, <<<'OUTPUT'
|
||||||
Usage: php-parse [operations] file1.php [file2.php ...]
|
Usage: php-parse [operations] file1.php [file2.php ...]
|
||||||
or: php-parse [operations] "<?php code"
|
or: php-parse [operations] "<?php code"
|
||||||
Turn PHP source code into an abstract syntax tree.
|
Turn PHP source code into an abstract syntax tree.
|
||||||
@ -123,6 +123,7 @@ Operations is a list of the following options (--dump by default):
|
|||||||
-c, --with-column-info Show column-numbers for errors (if available)
|
-c, --with-column-info Show column-numbers for errors (if available)
|
||||||
-P, --with-positions Show positions in node dumps
|
-P, --with-positions Show positions in node dumps
|
||||||
-r, --with-recovery Use parsing with error recovery
|
-r, --with-recovery Use parsing with error recovery
|
||||||
|
--version=VERSION Target specific PHP version (default: newest)
|
||||||
-h, --help Display this page
|
-h, --help Display this page
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
@ -143,6 +144,7 @@ function parseArgs($args) {
|
|||||||
'with-column-info' => false,
|
'with-column-info' => false,
|
||||||
'with-positions' => false,
|
'with-positions' => false,
|
||||||
'with-recovery' => false,
|
'with-recovery' => false,
|
||||||
|
'version' => PhpParser\PhpVersion::getNewestSupported(),
|
||||||
];
|
];
|
||||||
|
|
||||||
array_shift($args);
|
array_shift($args);
|
||||||
@ -193,7 +195,9 @@ function parseArgs($args) {
|
|||||||
$parseOptions = false;
|
$parseOptions = false;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if ($arg[0] === '-') {
|
if (preg_match('/^--version=(.*)$/', $arg, $matches)) {
|
||||||
|
$attributes['version'] = PhpParser\PhpVersion::fromString($matches[1]);
|
||||||
|
} elseif ($arg[0] === '-' && \strlen($arg[0]) > 1) {
|
||||||
showHelp("Invalid operation $arg.");
|
showHelp("Invalid operation $arg.");
|
||||||
} else {
|
} else {
|
||||||
$files[] = $arg;
|
$files[] = $arg;
|
||||||
|
@ -13,16 +13,18 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.0",
|
"php": ">=7.1",
|
||||||
"ext-tokenizer": "*"
|
"ext-tokenizer": "*",
|
||||||
|
"ext-json": "*",
|
||||||
|
"ext-ctype": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0",
|
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
|
||||||
"ircmaxell/php-yacc": "^0.0.7"
|
"ircmaxell/php-yacc": "^0.0.7"
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "4.9-dev"
|
"dev-master": "5.0-dev"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
Introduction
|
Introduction
|
||||||
============
|
============
|
||||||
|
|
||||||
This project is a PHP 5.2 to PHP 7.4 parser **written in PHP itself**.
|
This project is a PHP parser **written in PHP itself**.
|
||||||
|
|
||||||
What is this for?
|
What is this for?
|
||||||
-----------------
|
-----------------
|
||||||
@ -26,12 +26,29 @@ programmatic PHP code analysis are incidentally PHP developers, not C developers
|
|||||||
What can it parse?
|
What can it parse?
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
The parser supports parsing PHP 5.2-7.4.
|
The parser supports parsing PHP 7 and PHP 8 code, with the following exceptions:
|
||||||
|
|
||||||
|
* Namespaced names containing whitespace (e.g. `Foo \ Bar` instead of `Foo\Bar`) are not supported.
|
||||||
|
These are illegal in PHP 8, but are legal in earlier versions. However, PHP-Parser does not
|
||||||
|
support them for any version.
|
||||||
|
|
||||||
|
PHP-Parser 4.x had full support for parsing PHP 5. PHP-Parser 5.x has only limited support, with the
|
||||||
|
following caveats:
|
||||||
|
|
||||||
|
* Some variable expressions like `$$foo[0]` are valid in both PHP 5 and PHP 7, but have different
|
||||||
|
interpretation. In such cases, the PHP 7 AST will always be constructed (using `($$foo)[0]`
|
||||||
|
rather than `${$foo[0]}`).
|
||||||
|
* Declarations of the form `global $$var[0]` are not supported in PHP 7 and will cause a parse
|
||||||
|
error. In error recovery mode, it is possible to continue parsing after such declarations.
|
||||||
|
|
||||||
As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP
|
As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP
|
||||||
version it runs on), additionally a wrapper for emulating tokens from newer versions is provided.
|
version it runs on), additionally a wrapper for emulating tokens from newer versions is provided.
|
||||||
This allows to parse PHP 7.4 source code running on PHP 7.0, for example. This emulation is somewhat
|
This allows to parse PHP 8.0 source code running on PHP 7.1, for example. This emulation is not
|
||||||
hacky and not perfect, but it should work well on any sane code.
|
perfect, but works well in practice.
|
||||||
|
|
||||||
|
Finally, it should be noted that the parser aims to accept all valid code, not reject all invalid
|
||||||
|
code. It will generally accept code that is only valid in newer versions (even when targeting an
|
||||||
|
older one), and accept code that is syntactically correct, but would result in a compiler error.
|
||||||
|
|
||||||
What output does it produce?
|
What output does it produce?
|
||||||
----------------------------
|
----------------------------
|
||||||
@ -59,7 +76,7 @@ This matches the structure of the code: An echo statement, which takes two strin
|
|||||||
with the values `Hi` and `World`.
|
with the values `Hi` and `World`.
|
||||||
|
|
||||||
You can also see that the AST does not contain any whitespace information (but most comments are saved).
|
You can also see that the AST does not contain any whitespace information (but most comments are saved).
|
||||||
So using it for formatting analysis is not possible.
|
However, it does retain accurate position information, which can be used to inspect precise formatting.
|
||||||
|
|
||||||
What else can it do?
|
What else can it do?
|
||||||
--------------------
|
--------------------
|
||||||
@ -70,7 +87,7 @@ Apart from the parser itself this package also bundles support for some other, r
|
|||||||
that "pretty printing" does not imply that the output is especially pretty. It's just how it's
|
that "pretty printing" does not imply that the output is especially pretty. It's just how it's
|
||||||
called ;)
|
called ;)
|
||||||
* Support for serializing and unserializing the node tree to JSON
|
* Support for serializing and unserializing the node tree to JSON
|
||||||
* Support for dumping the node tree in a human readable form (see the section above for an
|
* Support for dumping the node tree in a human-readable form (see the section above for an
|
||||||
example of how the output looks like)
|
example of how the output looks like)
|
||||||
* Infrastructure for traversing and changing the AST (node traverser and node visitors)
|
* Infrastructure for traversing and changing the AST (node traverser and node visitors)
|
||||||
* A node visitor for resolving namespaced names
|
* A node visitor for resolving namespaced names
|
||||||
|
@ -12,14 +12,14 @@ To bootstrap the library, include the autoloader generated by composer:
|
|||||||
require 'path/to/vendor/autoload.php';
|
require 'path/to/vendor/autoload.php';
|
||||||
```
|
```
|
||||||
|
|
||||||
Additionally you may want to set the `xdebug.max_nesting_level` ini option to a higher value:
|
Additionally, you may want to set the `xdebug.max_nesting_level` ini option to a higher value:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
ini_set('xdebug.max_nesting_level', 3000);
|
ini_set('xdebug.max_nesting_level', 3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
This ensures that there will be no errors when traversing highly nested node trees. However, it is
|
This ensures that there will be no errors when traversing highly nested node trees. However, it is
|
||||||
preferable to disable XDebug completely, as it can easily make this library more than five times
|
preferable to disable Xdebug completely, as it can easily make this library more than five times
|
||||||
slower.
|
slower.
|
||||||
|
|
||||||
Parsing
|
Parsing
|
||||||
@ -29,25 +29,29 @@ In order to parse code, you first have to create a parser instance:
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
use PhpParser\ParserFactory;
|
use PhpParser\ParserFactory;
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
|
// Parser for the version you are running on.
|
||||||
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
|
|
||||||
|
// Parser for the newest PHP version supported by the PHP-Parser library.
|
||||||
|
$parser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||||
|
|
||||||
|
// Parser for a specific PHP version.
|
||||||
|
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromString('8.1'));
|
||||||
```
|
```
|
||||||
|
|
||||||
The factory accepts a kind argument, that determines how different PHP versions are treated:
|
Which version you should target depends on your use case. In many cases you will want to use the
|
||||||
|
host version, as people typically analyze code for the version they are running on. However, when
|
||||||
|
analyzing arbitrary code you are usually best off using the newest supported version, which tends
|
||||||
|
to accept the widest range of code (unless there are breaking changes in PHP).
|
||||||
|
|
||||||
Kind | Behavior
|
The `createXYZ()` methods optionally accept an array of lexer options. Some use cases that require
|
||||||
-----|---------
|
customized lexer options are discussed in the [lexer documentation](component/Lexer.markdown).
|
||||||
`ParserFactory::PREFER_PHP7` | Try to parse code as PHP 7. If this fails, try to parse it as PHP 5.
|
|
||||||
`ParserFactory::PREFER_PHP5` | Try to parse code as PHP 5. If this fails, try to parse it as PHP 7.
|
|
||||||
`ParserFactory::ONLY_PHP7` | Parse code as PHP 7.
|
|
||||||
`ParserFactory::ONLY_PHP5` | Parse code as PHP 5.
|
|
||||||
|
|
||||||
Unless you have a strong reason to use something else, `PREFER_PHP7` is a reasonable default.
|
Subsequently, you can pass PHP code (including the opening `<?php` tag) to the `parse()` method in
|
||||||
|
order to create a syntax tree. If a syntax error is encountered, an `PhpParser\Error` exception will
|
||||||
The `create()` method optionally accepts a `Lexer` instance as the second argument. Some use cases
|
be thrown by default:
|
||||||
that require customized lexers are discussed in the [lexer documentation](component/Lexer.markdown).
|
|
||||||
|
|
||||||
Subsequently you can pass PHP code (including the opening `<?php` tag) to the `parse` method in order to
|
|
||||||
create a syntax tree. If a syntax error is encountered, an `PhpParser\Error` exception will be thrown:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
@ -62,7 +66,7 @@ function printLine($msg) {
|
|||||||
printLine('Hello World!!!');
|
printLine('Hello World!!!');
|
||||||
CODE;
|
CODE;
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$stmts = $parser->parse($code);
|
$stmts = $parser->parse($code);
|
||||||
@ -77,7 +81,7 @@ A parser instance can be reused to parse multiple files.
|
|||||||
Node dumping
|
Node dumping
|
||||||
------------
|
------------
|
||||||
|
|
||||||
To dump the abstract syntax tree in human readable form, a `NodeDumper` can be used:
|
To dump the abstract syntax tree in human-readable form, a `NodeDumper` can be used:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
@ -171,7 +175,7 @@ with them easier they are grouped into three categories:
|
|||||||
|
|
||||||
* `PhpParser\Node\Stmt`s are statement nodes, i.e. language constructs that do not return
|
* `PhpParser\Node\Stmt`s are statement nodes, i.e. language constructs that do not return
|
||||||
a value and can not occur in an expression. For example a class definition is a statement.
|
a value and can not occur in an expression. For example a class definition is a statement.
|
||||||
It doesn't return a value and you can't write something like `func(class A {});`.
|
It doesn't return a value, and you can't write something like `func(class A {});`.
|
||||||
* `PhpParser\Node\Expr`s are expression nodes, i.e. language constructs that return a value
|
* `PhpParser\Node\Expr`s are expression nodes, i.e. language constructs that return a value
|
||||||
and thus can occur in other expressions. Examples of expressions are `$var`
|
and thus can occur in other expressions. Examples of expressions are `$var`
|
||||||
(`PhpParser\Node\Expr\Variable`) and `func()` (`PhpParser\Node\Expr\FuncCall`).
|
(`PhpParser\Node\Expr\Variable`) and `func()` (`PhpParser\Node\Expr\FuncCall`).
|
||||||
@ -201,15 +205,14 @@ can then be retrieved using `hasAttribute()`, `getAttribute()` and `getAttribute
|
|||||||
By default the lexer adds the `startLine`, `endLine` and `comments` attributes. `comments` is an array
|
By default the lexer adds the `startLine`, `endLine` and `comments` attributes. `comments` is an array
|
||||||
of `PhpParser\Comment[\Doc]` instances.
|
of `PhpParser\Comment[\Doc]` instances.
|
||||||
|
|
||||||
The start line can also be accessed using `getLine()`/`setLine()` (instead of `getAttribute('startLine')`).
|
The start line can also be accessed using `getStartLine()` (instead of `getAttribute('startLine')`).
|
||||||
The last doc comment from the `comments` attribute can be obtained using `getDocComment()`.
|
The last doc comment from the `comments` attribute can be obtained using `getDocComment()`.
|
||||||
|
|
||||||
Pretty printer
|
Pretty printer
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
The pretty printer component compiles the AST back to PHP code. As the parser does not retain formatting
|
The pretty printer component compiles the AST back to PHP code according to a specified scheme.
|
||||||
information the formatting is done using a specified scheme. Currently there is only one scheme available,
|
Currently, there is only one scheme available, namely `PhpParser\PrettyPrinter\Standard`.
|
||||||
namely `PhpParser\PrettyPrinter\Standard`.
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use PhpParser\Error;
|
use PhpParser\Error;
|
||||||
@ -218,8 +221,8 @@ use PhpParser\PrettyPrinter;
|
|||||||
|
|
||||||
$code = "<?php echo 'Hi ', hi\\getTarget();";
|
$code = "<?php echo 'Hi ', hi\\getTarget();";
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
$prettyPrinter = new PrettyPrinter\Standard;
|
$prettyPrinter = new PrettyPrinter\Standard();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// parse
|
// parse
|
||||||
@ -254,10 +257,13 @@ single expression using `prettyPrintExpr()`.
|
|||||||
The `prettyPrintFile()` method can be used to print an entire file. This will include the opening `<?php` tag
|
The `prettyPrintFile()` method can be used to print an entire file. This will include the opening `<?php` tag
|
||||||
and handle inline HTML as the first/last statement more gracefully.
|
and handle inline HTML as the first/last statement more gracefully.
|
||||||
|
|
||||||
|
There is also a pretty-printing mode which retains formatting for parts of the AST that have not
|
||||||
|
been changed, which requires additional setup.
|
||||||
|
|
||||||
> Read more: [Pretty printing documentation](component/Pretty_printing.markdown)
|
> Read more: [Pretty printing documentation](component/Pretty_printing.markdown)
|
||||||
|
|
||||||
Node traversation
|
Node traversal
|
||||||
-----------------
|
--------------
|
||||||
|
|
||||||
The above pretty printing example used the fact that the source code was known and thus it was easy to
|
The above pretty printing example used the fact that the source code was known and thus it was easy to
|
||||||
write code that accesses a certain part of a node tree and changes it. Normally this is not the case.
|
write code that accesses a certain part of a node tree and changes it. Normally this is not the case.
|
||||||
@ -272,7 +278,7 @@ use PhpParser\NodeTraverser;
|
|||||||
use PhpParser\ParserFactory;
|
use PhpParser\ParserFactory;
|
||||||
use PhpParser\PrettyPrinter;
|
use PhpParser\PrettyPrinter;
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$prettyPrinter = new PrettyPrinter\Standard;
|
$prettyPrinter = new PrettyPrinter\Standard;
|
||||||
|
|
||||||
@ -303,8 +309,7 @@ The corresponding node visitor might look like this:
|
|||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\NodeVisitorAbstract;
|
use PhpParser\NodeVisitorAbstract;
|
||||||
|
|
||||||
class MyNodeVisitor extends NodeVisitorAbstract
|
class MyNodeVisitor extends NodeVisitorAbstract {
|
||||||
{
|
|
||||||
public function leaveNode(Node $node) {
|
public function leaveNode(Node $node) {
|
||||||
if ($node instanceof Node\Scalar\String_) {
|
if ($node instanceof Node\Scalar\String_) {
|
||||||
$node->value = 'foo';
|
$node->value = 'foo';
|
||||||
@ -326,7 +331,7 @@ public function afterTraverse(array $nodes);
|
|||||||
```
|
```
|
||||||
|
|
||||||
The `beforeTraverse()` method is called once before the traversal begins and is passed the nodes the
|
The `beforeTraverse()` method is called once before the traversal begins and is passed the nodes the
|
||||||
traverser was called with. This method can be used for resetting values before traversation or
|
traverser was called with. This method can be used for resetting values before traversal or
|
||||||
preparing the tree for traversal.
|
preparing the tree for traversal.
|
||||||
|
|
||||||
The `afterTraverse()` method is similar to the `beforeTraverse()` method, with the only difference that
|
The `afterTraverse()` method is similar to the `beforeTraverse()` method, with the only difference that
|
||||||
@ -342,8 +347,8 @@ The `enterNode()` method can additionally return the value `NodeTraverser::DONT_
|
|||||||
which instructs the traverser to skip all children of the current node. To furthermore prevent subsequent
|
which instructs the traverser to skip all children of the current node. To furthermore prevent subsequent
|
||||||
visitors from visiting the current node, `NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN` can be used instead.
|
visitors from visiting the current node, `NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN` can be used instead.
|
||||||
|
|
||||||
The `leaveNode()` method can additionally return the value `NodeTraverser::REMOVE_NODE`, in which
|
Both methods can additionally return the value `NodeTraverser::REMOVE_NODE`, in which
|
||||||
case the current node will be removed from the parent array. Furthermore it is possible to return
|
case the current node will be removed from the parent array. Furthermore, it is possible to return
|
||||||
an array of nodes, which will be merged into the parent array at the offset of the current node.
|
an array of nodes, which will be merged into the parent array at the offset of the current node.
|
||||||
I.e. if in `array(A, B, C)` the node `B` should be replaced with `array(X, Y, Z)` the result will
|
I.e. if in `array(A, B, C)` the node `B` should be replaced with `array(X, Y, Z)` the result will
|
||||||
be `array(A, X, Y, Z, C)`.
|
be `array(A, X, Y, Z, C)`.
|
||||||
@ -372,8 +377,9 @@ unqualified function and constant names. These are resolved at runtime and thus
|
|||||||
know which function they are referring to. In most cases this is a non-issue as the global functions
|
know which function they are referring to. In most cases this is a non-issue as the global functions
|
||||||
are meant.
|
are meant.
|
||||||
|
|
||||||
Also the `NameResolver` adds a `namespacedName` subnode to class, function and constant declarations
|
Additionally, the `NameResolver` adds a `namespacedName` subnode to class, function and constant
|
||||||
that contains the namespaced name instead of only the shortname that is available via `name`.
|
declarations that contains the namespaced name instead of only the shortname that is available via
|
||||||
|
`name`.
|
||||||
|
|
||||||
> Read more: [Name resolution documentation](component/Name_resolution.markdown)
|
> Read more: [Name resolution documentation](component/Name_resolution.markdown)
|
||||||
|
|
||||||
@ -396,7 +402,7 @@ use PhpParser\NodeVisitor\NameResolver;
|
|||||||
$inDir = '/some/path';
|
$inDir = '/some/path';
|
||||||
$outDir = '/some/other/path';
|
$outDir = '/some/other/path';
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$prettyPrinter = new PrettyPrinter\Standard;
|
$prettyPrinter = new PrettyPrinter\Standard;
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ Component documentation
|
|||||||
* [JSON representation](component/JSON_representation.markdown)
|
* [JSON representation](component/JSON_representation.markdown)
|
||||||
* JSON encoding and decoding of ASTs
|
* JSON encoding and decoding of ASTs
|
||||||
* [Performance](component/Performance.markdown)
|
* [Performance](component/Performance.markdown)
|
||||||
* Disabling XDebug
|
* Disabling Xdebug
|
||||||
* Reusing objects
|
* Reusing objects
|
||||||
* Garbage collection impact
|
* Garbage collection impact
|
||||||
* [Frequently asked questions](component/FAQ.markdown)
|
* [Frequently asked questions](component/FAQ.markdown)
|
||||||
|
@ -101,7 +101,7 @@ abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
|
|||||||
*
|
*
|
||||||
* @param SomeClass And takes a parameter
|
* @param SomeClass And takes a parameter
|
||||||
*/
|
*/
|
||||||
public abstract function someMethod(SomeClass $someParam) : bool;
|
public abstract function someMethod(SomeClass $someParam): bool;
|
||||||
protected function anotherMethod($someParam = 'test')
|
protected function anotherMethod($someParam = 'test')
|
||||||
{
|
{
|
||||||
print $someParam;
|
print $someParam;
|
||||||
|
@ -19,9 +19,9 @@ PHP-Parser supports evaluation of such constant expressions through the `ConstEx
|
|||||||
|
|
||||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||||
|
|
||||||
$evalutator = new ConstExprEvaluator();
|
$evaluator = new ConstExprEvaluator();
|
||||||
try {
|
try {
|
||||||
$value = $evalutator->evaluateSilently($someExpr);
|
$value = $evaluator->evaluateSilently($someExpr);
|
||||||
} catch (ConstExprEvaluationException $e) {
|
} catch (ConstExprEvaluationException $e) {
|
||||||
// Either the expression contains unsupported expression types,
|
// Either the expression contains unsupported expression types,
|
||||||
// or an error occurred during evaluation
|
// or an error occurred during evaluation
|
||||||
@ -45,7 +45,7 @@ use PhpParser\Node\{Expr, Scalar};
|
|||||||
$evaluator = new ConstExprEvaluator();
|
$evaluator = new ConstExprEvaluator();
|
||||||
|
|
||||||
// 10 / 0
|
// 10 / 0
|
||||||
$expr = new Expr\BinaryOp\Div(new Scalar\LNumber(10), new Scalar\LNumber(0));
|
$expr = new Expr\BinaryOp\Div(new Scalar\Int_(10), new Scalar\Int_(0));
|
||||||
|
|
||||||
var_dump($evaluator->evaluateDirectly($expr)); // float(INF)
|
var_dump($evaluator->evaluateDirectly($expr)); // float(INF)
|
||||||
// Warning: Division by zero
|
// Warning: Division by zero
|
||||||
@ -69,6 +69,7 @@ expressions, apart from the following:
|
|||||||
* `Scalar\MagicConst\*`
|
* `Scalar\MagicConst\*`
|
||||||
* `Expr\ConstFetch` (only null/false/true are handled)
|
* `Expr\ConstFetch` (only null/false/true are handled)
|
||||||
* `Expr\ClassConstFetch`
|
* `Expr\ClassConstFetch`
|
||||||
|
* `Expr\New_` (since PHP 8.1)
|
||||||
|
|
||||||
Handling these expression types requires non-local information, such as which global constants are
|
Handling these expression types requires non-local information, such as which global constants are
|
||||||
defined. By default, the evaluator will throw a `ConstExprEvaluationException` when it encounters
|
defined. By default, the evaluator will throw a `ConstExprEvaluationException` when it encounters
|
||||||
@ -112,4 +113,4 @@ class Test {
|
|||||||
const A = self::B;
|
const A = self::B;
|
||||||
const B = self::A;
|
const B = self::A;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -14,10 +14,10 @@ In order to receive information about not only the line, but also the column spa
|
|||||||
position attributes in the lexer need to be enabled:
|
position attributes in the lexer need to be enabled:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$lexer = new PhpParser\Lexer(array(
|
$lexerOptions = array(
|
||||||
'usedAttributes' => array('comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'),
|
'usedAttributes' => array('comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'),
|
||||||
));
|
);
|
||||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
|
$parser = (new PhpParser\ParserFactory())->createForHostVersion($lexerOptions);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$stmts = $parser->parse($code);
|
$stmts = $parser->parse($code);
|
||||||
@ -36,7 +36,7 @@ if ($e->hasColumnInfo()) {
|
|||||||
echo $e->getRawMessage() . ' from ' . $e->getStartLine() . ':' . $e->getStartColumn($code)
|
echo $e->getRawMessage() . ' from ' . $e->getStartLine() . ':' . $e->getStartColumn($code)
|
||||||
. ' to ' . $e->getEndLine() . ':' . $e->getEndColumn($code);
|
. ' to ' . $e->getEndLine() . ':' . $e->getEndColumn($code);
|
||||||
// or:
|
// or:
|
||||||
echo $e->getMessageWithColumnInfo();
|
echo $e->getMessageWithColumnInfo($code);
|
||||||
} else {
|
} else {
|
||||||
echo $e->getMessage();
|
echo $e->getMessage();
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ To instead collect all encountered errors into an array, while trying to continu
|
|||||||
an instance of `ErrorHandler\Collecting` can be passed to the `Parser::parse()` method. A usage example:
|
an instance of `ErrorHandler\Collecting` can be passed to the `Parser::parse()` method. A usage example:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::ONLY_PHP7);
|
$parser = (new PhpParser\ParserFactory())->createForHostVersion();
|
||||||
$errorHandler = new PhpParser\ErrorHandler\Collecting;
|
$errorHandler = new PhpParser\ErrorHandler\Collecting;
|
||||||
|
|
||||||
$stmts = $parser->parse($code, $errorHandler);
|
$stmts = $parser->parse($code, $errorHandler);
|
||||||
@ -72,4 +72,6 @@ if (null !== $stmts) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `NameResolver` visitor also accepts an `ErrorHandler` as a constructor argument.
|
The partial AST may contain `Expr\Error` nodes that indicate that an error occurred while parsing an expression.
|
||||||
|
|
||||||
|
The `NameResolver` visitor also accepts an `ErrorHandler` as a constructor argument.
|
||||||
|
@ -19,7 +19,7 @@ $code = '...';
|
|||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$traverser->addVisitor(new ParentConnectingVisitor);
|
$traverser->addVisitor(new ParentConnectingVisitor);
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
$ast = $parser->parse($code);
|
$ast = $parser->parse($code);
|
||||||
$ast = $traverser->traverse($ast);
|
$ast = $traverser->traverse($ast);
|
||||||
```
|
```
|
||||||
@ -42,7 +42,7 @@ $code = '...';
|
|||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$traverser->addVisitor(new NodeConnectingVisitor);
|
$traverser->addVisitor(new NodeConnectingVisitor);
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
$ast = $parser->parse($code);
|
$ast = $parser->parse($code);
|
||||||
$ast = $traverser->traverse($ast);
|
$ast = $traverser->traverse($ast);
|
||||||
```
|
```
|
||||||
|
@ -18,7 +18,7 @@ function printLine($msg) {
|
|||||||
}
|
}
|
||||||
CODE;
|
CODE;
|
||||||
|
|
||||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
$parser = (new ParserFactory())->createForHostVersion();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$stmts = $parser->parse($code);
|
$stmts = $parser->parse($code);
|
||||||
|
@ -42,16 +42,17 @@ The attributes used in this example match the default behavior of the lexer. The
|
|||||||
> **Note:** The example in this section is outdated in that this information is directly available in the AST: While
|
> **Note:** The example in this section is outdated in that this information is directly available in the AST: While
|
||||||
> `$property->isPublic()` does not distinguish between `public` and `var`, directly checking `$property->flags` for
|
> `$property->isPublic()` does not distinguish between `public` and `var`, directly checking `$property->flags` for
|
||||||
> the `$property->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0` allows making this distinction without resorting to
|
> the `$property->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0` allows making this distinction without resorting to
|
||||||
> tokens. However the general idea behind the example still applies in other cases.
|
> tokens. However, the general idea behind the example still applies in other cases.
|
||||||
|
|
||||||
The token offset information is useful if you wish to examine the exact formatting used for a node. For example the AST
|
The token offset information is useful if you wish to examine the exact formatting used for a node. For example the AST
|
||||||
does not distinguish whether a property was declared using `public` or using `var`, but you can retrieve this
|
does not distinguish whether a property was declared using `public` or using `var`, but you can retrieve this
|
||||||
information based on the token position:
|
information based on the token position:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
/** @param PhpParser\Token[] $tokens */
|
||||||
function isDeclaredUsingVar(array $tokens, PhpParser\Node\Stmt\Property $prop) {
|
function isDeclaredUsingVar(array $tokens, PhpParser\Node\Stmt\Property $prop) {
|
||||||
$i = $prop->getAttribute('startTokenPos');
|
$i = $prop->getStartTokenPos();
|
||||||
return $tokens[$i][0] === T_VAR;
|
return $tokens[$i]->id === T_VAR;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -72,12 +73,12 @@ class MyNodeVisitor extends PhpParser\NodeVisitorAbstract {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$lexer = new PhpParser\Lexer(array(
|
$lexerOptions = array(
|
||||||
'usedAttributes' => array(
|
'usedAttributes' => array(
|
||||||
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
|
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
|
||||||
)
|
)
|
||||||
));
|
);
|
||||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::ONLY_PHP7, $lexer);
|
$parser = (new PhpParser\ParserFactory())->createForHostVersion($lexerOptions);
|
||||||
|
|
||||||
$visitor = new MyNodeVisitor();
|
$visitor = new MyNodeVisitor();
|
||||||
$traverser = new PhpParser\NodeTraverser();
|
$traverser = new PhpParser\NodeTraverser();
|
||||||
@ -111,14 +112,15 @@ The `startLexing()` method is invoked whenever the `parse()` method of the parse
|
|||||||
code that is to be lexed (including the opening tag). It can be used to reset state or preprocess the source code or tokens. The
|
code that is to be lexed (including the opening tag). It can be used to reset state or preprocess the source code or tokens. The
|
||||||
passed `ErrorHandler` should be used to report lexing errors.
|
passed `ErrorHandler` should be used to report lexing errors.
|
||||||
|
|
||||||
The `getTokens()` method returns the current token array, in the usual `token_get_all()` format. This method is not
|
The `getTokens()` method returns the current array of `PhpParser\Token`s, which are compatible with the PHP 8 `PhpToken`
|
||||||
used by the parser (which uses `getNextToken()`), but is useful in combination with the token position attributes.
|
class. This method is not used by the parser (which uses `getNextToken()`), but is useful in combination with the token
|
||||||
|
position attributes.
|
||||||
|
|
||||||
The `handleHaltCompiler()` method is called whenever a `T_HALT_COMPILER` token is encountered. It has to return the
|
The `handleHaltCompiler()` method is called whenever a `T_HALT_COMPILER` token is encountered. It has to return the
|
||||||
remaining string after the construct (not including `();`).
|
remaining string after the construct (not including `();`).
|
||||||
|
|
||||||
The `getNextToken()` method returns the ID of the next token (as defined by the `Parser::T_*` constants). If no more
|
The `getNextToken()` method returns the ID of the next token (in the sense of `Token::$id`). If no more
|
||||||
tokens are available it must return `0`, which is the ID of the `EOF` token. Furthermore the string content of the
|
tokens are available it must return `0`, which is the ID of the `EOF` token. Furthermore, the string content of the
|
||||||
token should be written into the by-reference `$value` parameter (which will then be available as `$n` in the parser).
|
token should be written into the by-reference `$value` parameter (which will then be available as `$n` in the parser).
|
||||||
|
|
||||||
### Attribute handling
|
### Attribute handling
|
||||||
@ -144,10 +146,10 @@ class KeepOriginalValueLexer extends Lexer // or Lexer\Emulative
|
|||||||
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
||||||
$tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
|
$tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
|
||||||
|
|
||||||
if ($tokenId == Tokens::T_CONSTANT_ENCAPSED_STRING // non-interpolated string
|
if ($tokenId == \T_CONSTANT_ENCAPSED_STRING // non-interpolated string
|
||||||
|| $tokenId == Tokens::T_ENCAPSED_AND_WHITESPACE // interpolated string
|
|| $tokenId == \T_ENCAPSED_AND_WHITESPACE // interpolated string
|
||||||
|| $tokenId == Tokens::T_LNUMBER // integer
|
|| $tokenId == \T_LNUMBER // integer
|
||||||
|| $tokenId == Tokens::T_DNUMBER // floating point number
|
|| $tokenId == \T_DNUMBER // floating point number
|
||||||
) {
|
) {
|
||||||
// could also use $startAttributes, doesn't really matter here
|
// could also use $startAttributes, doesn't really matter here
|
||||||
$endAttributes['originalValue'] = $value;
|
$endAttributes['originalValue'] = $value;
|
||||||
|
@ -10,7 +10,7 @@ visitor (NameResolver) based on it.
|
|||||||
The NameResolver visitor
|
The NameResolver visitor
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
The `NameResolver` visitor can (and for nearly all uses of the AST, is) be applied to resolve names
|
The `NameResolver` visitor can (and for nearly all uses of the AST, should) be applied to resolve names
|
||||||
to their fully-qualified form, to the degree that this is possible.
|
to their fully-qualified form, to the degree that this is possible.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@ -53,7 +53,7 @@ name. Once again, if an unqualified function or constant name cannot be resolved
|
|||||||
`resolvedName` attribute will not be present, and instead a `namespacedName` attribute is added.
|
`resolvedName` attribute will not be present, and instead a `namespacedName` attribute is added.
|
||||||
|
|
||||||
The `replaceNodes` attribute is useful if you wish to perform modifications on the AST, as you
|
The `replaceNodes` attribute is useful if you wish to perform modifications on the AST, as you
|
||||||
probably do not wish the resoluting code to have fully resolved names as a side-effect.
|
probably do not wish the resulting code to have fully resolved names as a side-effect.
|
||||||
|
|
||||||
The NameContext
|
The NameContext
|
||||||
---------------
|
---------------
|
||||||
@ -84,4 +84,4 @@ representation of a name given the current name resolution environment.
|
|||||||
The name context is intended to be used for name resolution operations outside the AST itself, such
|
The name context is intended to be used for name resolution operations outside the AST itself, such
|
||||||
as class names inside doc comments. A visitor running in parallel with the name resolver can access
|
as class names inside doc comments. A visitor running in parallel with the name resolver can access
|
||||||
the name context using `$nameResolver->getNameContext()`. Alternatively a visitor can use an
|
the name context using `$nameResolver->getNameContext()`. Alternatively a visitor can use an
|
||||||
independent context and explicitly feed `Namespace` and `Use` nodes to it.
|
independent context and explicitly feed `Namespace` and `Use` nodes to it.
|
||||||
|
@ -8,20 +8,20 @@ described in the following.
|
|||||||
Xdebug
|
Xdebug
|
||||||
------
|
------
|
||||||
|
|
||||||
Running PHP with XDebug adds a lot of overhead, especially for code that performs many method calls.
|
Running PHP with Xdebug adds a lot of overhead, especially for code that performs many method calls.
|
||||||
Just by loading XDebug (without enabling profiling or other more intrusive XDebug features), you
|
Just by loading Xdebug (without enabling profiling or other more intrusive Xdebug features), you
|
||||||
can expect that code using PHP-Parser will be approximately *five times slower*.
|
can expect that code using PHP-Parser will be approximately *five times slower*.
|
||||||
|
|
||||||
As such, you should make sure that XDebug is not loaded when using this library. Note that setting
|
As such, you should make sure that Xdebug is not loaded when using this library. Note that setting
|
||||||
the `xdebug.default_enable=0` ini option does *not* disable XDebug. The *only* way to disable
|
the `xdebug.default_enable=0` ini option does *not* disable Xdebug. The *only* way to disable
|
||||||
XDebug is to not load the extension in the first place.
|
Xdebug is to not load the extension in the first place.
|
||||||
|
|
||||||
If you are building a command-line utility for use by developers (who often have XDebug enabled),
|
If you are building a command-line utility for use by developers (who often have Xdebug enabled),
|
||||||
you may want to consider automatically restarting PHP with XDebug unloaded. The
|
you may want to consider automatically restarting PHP with Xdebug unloaded. The
|
||||||
[composer/xdebug-handler](https://github.com/composer/xdebug-handler) package can be used to do
|
[composer/xdebug-handler](https://github.com/composer/xdebug-handler) package can be used to do
|
||||||
this.
|
this.
|
||||||
|
|
||||||
If you do run with XDebug, you may need to increase the `xdebug.max_nesting_level` option to a
|
If you do run with Xdebug, you may need to increase the `xdebug.max_nesting_level` option to a
|
||||||
higher level, such as 3000. While the parser itself is recursion free, most other code working on
|
higher level, such as 3000. While the parser itself is recursion free, most other code working on
|
||||||
the AST uses recursion and will generate an error if the value of this option is too low.
|
the AST uses recursion and will generate an error if the value of this option is too low.
|
||||||
|
|
||||||
@ -62,4 +62,4 @@ disproportionally (and are usually generated anyway).
|
|||||||
Of course, you can also try to (temporarily) disable GC. By design the AST generated by PHP-Parser
|
Of course, you can also try to (temporarily) disable GC. By design the AST generated by PHP-Parser
|
||||||
is cycle-free, so the AST itself will never cause leaks with GC disabled. However, other code
|
is cycle-free, so the AST itself will never cause leaks with GC disabled. However, other code
|
||||||
(including for example the parser object itself) may hold cycles, so disabling of GC should be
|
(including for example the parser object itself) may hold cycles, so disabling of GC should be
|
||||||
approached with care.
|
approached with care.
|
||||||
|
@ -31,10 +31,16 @@ expression.
|
|||||||
Customizing the formatting
|
Customizing the formatting
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
Apart from an `shortArraySyntax` option, the default pretty printer does not provide any
|
The pretty printer respects a number of `kind` attributes used by some notes (e.g., whether an
|
||||||
functionality to customize the formatting of the generated code. The pretty printer does respect a
|
integer should be printed as decimal, hexadecimal, etc). Additionally, it supports two options:
|
||||||
number of `kind` attributes used by some notes (e.g., whether an integer should be printed as
|
|
||||||
decimal, hexadecimal, etc), but there are no options to control brace placement or similar.
|
* `phpVersion` (defaults to 7.0) allows opting into formatting that is not supported by older PHP
|
||||||
|
versions.
|
||||||
|
* `shortArraySyntax` determines the used array syntax if the `kind` attribute is not set. This is
|
||||||
|
a legacy option, and `phpVersion` should be used to control this behavior instead.
|
||||||
|
|
||||||
|
However, the default pretty printer does not provide any functionality for fine-grained
|
||||||
|
customization of code formatting.
|
||||||
|
|
||||||
If you want to make minor changes to the formatting, the easiest way is to extend the pretty printer
|
If you want to make minor changes to the formatting, the easiest way is to extend the pretty printer
|
||||||
and override the methods responsible for the node types you are interested in.
|
and override the methods responsible for the node types you are interested in.
|
||||||
@ -46,29 +52,27 @@ default pretty printer with an existing library for code reformatting, such as
|
|||||||
Formatting-preserving pretty printing
|
Formatting-preserving pretty printing
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
> **Note:** This functionality is **experimental** and not yet complete.
|
|
||||||
|
|
||||||
For automated code refactoring, migration and similar, you will usually only want to modify a small
|
For automated code refactoring, migration and similar, you will usually only want to modify a small
|
||||||
portion of the code and leave the remainder alone. The basic pretty printer is not suitable for
|
portion of the code and leave the remainder alone. The basic pretty printer is not suitable for
|
||||||
this, because it will also reformat parts of the code which have not been modified.
|
this, because it will also reformat parts of the code which have not been modified.
|
||||||
|
|
||||||
Since PHP-Parser 4.0, an experimental formatting-preserving pretty-printing mode is available, which
|
Since PHP-Parser 4.0, a formatting-preserving pretty-printing mode is available, which
|
||||||
attempts to preserve the formatting of code (those AST nodes that have not changed) and only reformat
|
attempts to preserve the formatting of code (those AST nodes that have not changed) and only reformat
|
||||||
code which has been modified or newly inserted.
|
code which has been modified or newly inserted.
|
||||||
|
|
||||||
Use of the formatting-preservation functionality requires some additional preparatory steps:
|
Use of the formatting-preservation functionality requires some additional preparatory steps:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use PhpParser\{Lexer, NodeTraverser, NodeVisitor, Parser, PrettyPrinter};
|
use PhpParser\{Lexer, NodeTraverser, NodeVisitor, ParserFactory, PrettyPrinter};
|
||||||
|
|
||||||
$lexer = new Lexer\Emulative([
|
$lexerOptions = new [
|
||||||
'usedAttributes' => [
|
'usedAttributes' => [
|
||||||
'comments',
|
'comments',
|
||||||
'startLine', 'endLine',
|
'startLine', 'endLine',
|
||||||
'startTokenPos', 'endTokenPos',
|
'startTokenPos', 'endTokenPos',
|
||||||
],
|
],
|
||||||
]);
|
];
|
||||||
$parser = new Parser\Php7($lexer);
|
$parser = (new ParserFactory())->createForHostVersion($lexerOptions);
|
||||||
|
|
||||||
$traverser = new NodeTraverser();
|
$traverser = new NodeTraverser();
|
||||||
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
||||||
@ -76,7 +80,7 @@ $traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
|||||||
$printer = new PrettyPrinter\Standard();
|
$printer = new PrettyPrinter\Standard();
|
||||||
|
|
||||||
$oldStmts = $parser->parse($code);
|
$oldStmts = $parser->parse($code);
|
||||||
$oldTokens = $lexer->getTokens();
|
$oldTokens = $parser->getLexer()->getTokens();
|
||||||
|
|
||||||
$newStmts = $traverser->traverse($oldStmts);
|
$newStmts = $traverser->traverse($oldStmts);
|
||||||
|
|
||||||
@ -86,11 +90,9 @@ $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
|||||||
```
|
```
|
||||||
|
|
||||||
If you make use of the name resolution functionality, you will likely want to disable the
|
If you make use of the name resolution functionality, you will likely want to disable the
|
||||||
`replaceNodes` option. This will add resolved names as attributes, instead of directlying modifying
|
`replaceNodes` option. This will add resolved names as attributes, instead of directly modifying
|
||||||
the AST and causing spurious changes to the pretty printed code. For more information, see the
|
the AST and causing spurious changes to the pretty printed code. For more information, see the
|
||||||
[name resolution documentation](Name_resolution.markdown).
|
[name resolution documentation](Name_resolution.markdown).
|
||||||
|
|
||||||
This functionality is experimental and not yet fully implemented. It should not provide incorrect
|
The formatting-preservation works on a best-effort basis and may sometimes reformat more code tha
|
||||||
code, but it may sometimes reformat more code than necessary. Open issues are tracked in
|
necessary. If you encounter problems while using this functionality, please open an issue.
|
||||||
[issue #344](https://github.com/nikic/PHP-Parser/issues/344). If you encounter problems while using
|
|
||||||
this functionality, please open an issue, so we know what to prioritize.
|
|
||||||
|
@ -11,7 +11,7 @@ use PhpParser\{Node, NodeTraverser, NodeVisitorAbstract};
|
|||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$traverser->addVisitor(new class extends NodeVisitorAbstract {
|
$traverser->addVisitor(new class extends NodeVisitorAbstract {
|
||||||
public function leaveNode(Node $node) {
|
public function leaveNode(Node $node) {
|
||||||
if ($node instanceof Node\Scalar\LNumber) {
|
if ($node instanceof Node\Scalar\Int_) {
|
||||||
return new Node\Scalar\String_((string) $node->value);
|
return new Node\Scalar\String_((string) $node->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,8 +129,7 @@ Now `$a && $b` will be replaced by `!($a && $b)`. Then the traverser will go int
|
|||||||
only) child of `!($a && $b)`, which is `$a && $b`. The transformation applies again and we end up
|
only) child of `!($a && $b)`, which is `$a && $b`. The transformation applies again and we end up
|
||||||
with `!!($a && $b)`. This will continue until PHP hits the memory limit.
|
with `!!($a && $b)`. This will continue until PHP hits the memory limit.
|
||||||
|
|
||||||
Finally, two special replacement types are supported only by leaveNode. The first is removal of a
|
Finally, there are two special replacement types. The first is removal of a node:
|
||||||
node:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
public function leaveNode(Node $node) {
|
public function leaveNode(Node $node) {
|
||||||
@ -165,8 +164,8 @@ This example will remove all calls to `var_dump()` which occur as expression sta
|
|||||||
that `var_dump($a);` will be removed, but `if (var_dump($a))` will not be removed (and there is no
|
that `var_dump($a);` will be removed, but `if (var_dump($a))` will not be removed (and there is no
|
||||||
obvious way in which it can be removed).
|
obvious way in which it can be removed).
|
||||||
|
|
||||||
Next to removing nodes, it is also possible to replace one node with multiple nodes. Again, this
|
Next to removing nodes, it is also possible to replace one node with multiple nodes. This
|
||||||
only works inside leaveNode and only if the parent structure is an array.
|
only works if the parent structure is an array.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
public function leaveNode(Node $node) {
|
public function leaveNode(Node $node) {
|
||||||
@ -193,7 +192,7 @@ anonymous classes), you know that once you've seen a class declaration, there is
|
|||||||
checking all it's child nodes, because PHP does not allow nesting classes. In this case, you can
|
checking all it's child nodes, because PHP does not allow nesting classes. In this case, you can
|
||||||
instruct the traverser to not recurse into the class node:
|
instruct the traverser to not recurse into the class node:
|
||||||
|
|
||||||
```
|
```php
|
||||||
private $classes = [];
|
private $classes = [];
|
||||||
public function enterNode(Node $node) {
|
public function enterNode(Node $node) {
|
||||||
if ($node instanceof Node\Stmt\Class_) {
|
if ($node instanceof Node\Stmt\Class_) {
|
||||||
@ -211,7 +210,7 @@ after finding it. For example, if you are looking for the node of a class with a
|
|||||||
discounting exotic cases like conditionally defining a class two times), you can stop traversal
|
discounting exotic cases like conditionally defining a class two times), you can stop traversal
|
||||||
once you found it:
|
once you found it:
|
||||||
|
|
||||||
```
|
```php
|
||||||
private $class = null;
|
private $class = null;
|
||||||
public function enterNode(Node $node) {
|
public function enterNode(Node $node) {
|
||||||
if ($node instanceof Node\Stmt\Class_ &&
|
if ($node instanceof Node\Stmt\Class_ &&
|
||||||
@ -251,7 +250,7 @@ Stmt_Return(
|
|||||||
|
|
||||||
the following method calls will be performed:
|
the following method calls will be performed:
|
||||||
|
|
||||||
```
|
```php
|
||||||
$visitorA->enterNode(Stmt_Return)
|
$visitorA->enterNode(Stmt_Return)
|
||||||
$visitorB->enterNode(Stmt_Return)
|
$visitorB->enterNode(Stmt_Return)
|
||||||
$visitorA->enterNode(Expr_Variable)
|
$visitorA->enterNode(Expr_Variable)
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
What do all those files mean?
|
What do all those files mean?
|
||||||
=============================
|
=============================
|
||||||
|
|
||||||
* `php5.y`: PHP 5 grammar written in a pseudo language
|
* `php.y`: PHP 5-8 grammar written in a pseudo language
|
||||||
* `php7.y`: PHP 7 grammar written in a pseudo language
|
|
||||||
* `tokens.y`: Tokens definition shared between PHP 5 and PHP 7 grammars
|
|
||||||
* `parser.template`: A `kmyacc` parser prototype file for PHP
|
* `parser.template`: A `kmyacc` parser prototype file for PHP
|
||||||
* `tokens.template`: A `kmyacc` prototype file for the `Tokens` class
|
|
||||||
* `rebuildParsers.php`: Preprocesses the grammar and builds the parser using `kmyacc`
|
* `rebuildParsers.php`: Preprocesses the grammar and builds the parser using `kmyacc`
|
||||||
|
|
||||||
.phpy pseudo language
|
.phpy pseudo language
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<?php
|
<?php declare(strict_types=1);
|
||||||
$meta #
|
$meta #
|
||||||
#semval($) $this->semValue
|
#semval($) $this->semValue
|
||||||
#semval($,%t) $this->semValue
|
#semval($,%t) $this->semValue
|
||||||
@ -8,6 +8,7 @@ $meta #
|
|||||||
namespace PhpParser\Parser;
|
namespace PhpParser\Parser;
|
||||||
|
|
||||||
use PhpParser\Error;
|
use PhpParser\Error;
|
||||||
|
use PhpParser\Modifiers;
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
use PhpParser\Node\Name;
|
use PhpParser\Node\Name;
|
||||||
@ -17,12 +18,16 @@ use PhpParser\Node\Stmt;
|
|||||||
|
|
||||||
/* This is an automatically GENERATED file, which should not be manually edited.
|
/* This is an automatically GENERATED file, which should not be manually edited.
|
||||||
* Instead edit one of the following:
|
* Instead edit one of the following:
|
||||||
* * the grammar files grammar/php5.y or grammar/php7.y
|
* * the grammar file grammar/php.y
|
||||||
* * the skeleton file grammar/parser.template
|
* * the skeleton file grammar/parser.template
|
||||||
* * the preprocessing script grammar/rebuildParsers.php
|
* * the preprocessing script grammar/rebuildParsers.php
|
||||||
*/
|
*/
|
||||||
class #(-p) extends \PhpParser\ParserAbstract
|
class #(-p) extends \PhpParser\ParserAbstract
|
||||||
{
|
{
|
||||||
|
#tokenval
|
||||||
|
public const %s = %n;
|
||||||
|
#endtokenval
|
||||||
|
|
||||||
protected $tokenToSymbolMapSize = #(YYMAXLEX);
|
protected $tokenToSymbolMapSize = #(YYMAXLEX);
|
||||||
protected $actionTableSize = #(YYLAST);
|
protected $actionTableSize = #(YYLAST);
|
||||||
protected $gotoTableSize = #(YYGLAST);
|
protected $gotoTableSize = #(YYGLAST);
|
||||||
@ -89,7 +94,7 @@ class #(-p) extends \PhpParser\ParserAbstract
|
|||||||
);
|
);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected function initReduceCallbacks() {
|
protected function initReduceCallbacks(): void {
|
||||||
$this->reduceCallbacks = [
|
$this->reduceCallbacks = [
|
||||||
#reduce
|
#reduce
|
||||||
%n => function ($stackPos) {
|
%n => function ($stackPos) {
|
||||||
|
@ -1,7 +1,125 @@
|
|||||||
%pure_parser
|
%pure_parser
|
||||||
%expect 2
|
%expect 2
|
||||||
|
|
||||||
%tokens
|
%right T_THROW
|
||||||
|
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
|
||||||
|
%left ','
|
||||||
|
%left T_LOGICAL_OR
|
||||||
|
%left T_LOGICAL_XOR
|
||||||
|
%left T_LOGICAL_AND
|
||||||
|
%right T_PRINT
|
||||||
|
%right T_YIELD
|
||||||
|
%right T_DOUBLE_ARROW
|
||||||
|
%right T_YIELD_FROM
|
||||||
|
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL T_COALESCE_EQUAL
|
||||||
|
%left '?' ':'
|
||||||
|
%right T_COALESCE
|
||||||
|
%left T_BOOLEAN_OR
|
||||||
|
%left T_BOOLEAN_AND
|
||||||
|
%left '|'
|
||||||
|
%left '^'
|
||||||
|
%left T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
|
||||||
|
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
|
||||||
|
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
|
||||||
|
#if PHP7
|
||||||
|
%left T_SL T_SR
|
||||||
|
%left '+' '-' '.'
|
||||||
|
#endif
|
||||||
|
#if PHP8
|
||||||
|
%left '.'
|
||||||
|
%left T_SL T_SR
|
||||||
|
%left '+' '-'
|
||||||
|
#endif
|
||||||
|
%left '*' '/' '%'
|
||||||
|
%right '!'
|
||||||
|
%nonassoc T_INSTANCEOF
|
||||||
|
%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
|
||||||
|
%right T_POW
|
||||||
|
%right '['
|
||||||
|
%nonassoc T_NEW T_CLONE
|
||||||
|
%token T_EXIT
|
||||||
|
%token T_IF
|
||||||
|
%left T_ELSEIF
|
||||||
|
%left T_ELSE
|
||||||
|
%left T_ENDIF
|
||||||
|
%token T_LNUMBER
|
||||||
|
%token T_DNUMBER
|
||||||
|
%token T_STRING
|
||||||
|
%token T_STRING_VARNAME
|
||||||
|
%token T_VARIABLE
|
||||||
|
%token T_NUM_STRING
|
||||||
|
%token T_INLINE_HTML
|
||||||
|
%token T_ENCAPSED_AND_WHITESPACE
|
||||||
|
%token T_CONSTANT_ENCAPSED_STRING
|
||||||
|
%token T_ECHO
|
||||||
|
%token T_DO
|
||||||
|
%token T_WHILE
|
||||||
|
%token T_ENDWHILE
|
||||||
|
%token T_FOR
|
||||||
|
%token T_ENDFOR
|
||||||
|
%token T_FOREACH
|
||||||
|
%token T_ENDFOREACH
|
||||||
|
%token T_DECLARE
|
||||||
|
%token T_ENDDECLARE
|
||||||
|
%token T_AS
|
||||||
|
%token T_SWITCH
|
||||||
|
%token T_MATCH
|
||||||
|
%token T_ENDSWITCH
|
||||||
|
%token T_CASE
|
||||||
|
%token T_DEFAULT
|
||||||
|
%token T_BREAK
|
||||||
|
%token T_CONTINUE
|
||||||
|
%token T_GOTO
|
||||||
|
%token T_FUNCTION
|
||||||
|
%token T_FN
|
||||||
|
%token T_CONST
|
||||||
|
%token T_RETURN
|
||||||
|
%token T_TRY
|
||||||
|
%token T_CATCH
|
||||||
|
%token T_FINALLY
|
||||||
|
%token T_THROW
|
||||||
|
%token T_USE
|
||||||
|
%token T_INSTEADOF
|
||||||
|
%token T_GLOBAL
|
||||||
|
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC T_READONLY
|
||||||
|
%token T_VAR
|
||||||
|
%token T_UNSET
|
||||||
|
%token T_ISSET
|
||||||
|
%token T_EMPTY
|
||||||
|
%token T_HALT_COMPILER
|
||||||
|
%token T_CLASS
|
||||||
|
%token T_TRAIT
|
||||||
|
%token T_INTERFACE
|
||||||
|
%token T_ENUM
|
||||||
|
%token T_EXTENDS
|
||||||
|
%token T_IMPLEMENTS
|
||||||
|
%token T_OBJECT_OPERATOR
|
||||||
|
%token T_NULLSAFE_OBJECT_OPERATOR
|
||||||
|
%token T_DOUBLE_ARROW
|
||||||
|
%token T_LIST
|
||||||
|
%token T_ARRAY
|
||||||
|
%token T_CALLABLE
|
||||||
|
%token T_CLASS_C
|
||||||
|
%token T_TRAIT_C
|
||||||
|
%token T_METHOD_C
|
||||||
|
%token T_FUNC_C
|
||||||
|
%token T_LINE
|
||||||
|
%token T_FILE
|
||||||
|
%token T_START_HEREDOC
|
||||||
|
%token T_END_HEREDOC
|
||||||
|
%token T_DOLLAR_OPEN_CURLY_BRACES
|
||||||
|
%token T_CURLY_OPEN
|
||||||
|
%token T_PAAMAYIM_NEKUDOTAYIM
|
||||||
|
%token T_NAMESPACE
|
||||||
|
%token T_NS_C
|
||||||
|
%token T_DIR
|
||||||
|
%token T_NS_SEPARATOR
|
||||||
|
%token T_ELLIPSIS
|
||||||
|
%token T_NAME_FULLY_QUALIFIED
|
||||||
|
%token T_NAME_QUALIFIED
|
||||||
|
%token T_NAME_RELATIVE
|
||||||
|
%token T_ATTRIBUTE
|
||||||
|
%token T_ENUM
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
@ -20,28 +138,34 @@ top_statement_list:
|
|||||||
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
ampersand:
|
||||||
|
T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
|
||||||
|
| T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG
|
||||||
|
;
|
||||||
|
|
||||||
reserved_non_modifiers:
|
reserved_non_modifiers:
|
||||||
T_INCLUDE | T_INCLUDE_ONCE | T_EVAL | T_REQUIRE | T_REQUIRE_ONCE | T_LOGICAL_OR | T_LOGICAL_XOR | T_LOGICAL_AND
|
T_INCLUDE | T_INCLUDE_ONCE | T_EVAL | T_REQUIRE | T_REQUIRE_ONCE | T_LOGICAL_OR | T_LOGICAL_XOR | T_LOGICAL_AND
|
||||||
| T_INSTANCEOF | T_NEW | T_CLONE | T_EXIT | T_IF | T_ELSEIF | T_ELSE | T_ENDIF | T_ECHO | T_DO | T_WHILE
|
| T_INSTANCEOF | T_NEW | T_CLONE | T_EXIT | T_IF | T_ELSEIF | T_ELSE | T_ENDIF | T_DO | T_WHILE
|
||||||
| T_ENDWHILE | T_FOR | T_ENDFOR | T_FOREACH | T_ENDFOREACH | T_DECLARE | T_ENDDECLARE | T_AS | T_TRY | T_CATCH
|
| T_ENDWHILE | T_FOR | T_ENDFOR | T_FOREACH | T_ENDFOREACH | T_DECLARE | T_ENDDECLARE | T_AS | T_TRY | T_CATCH
|
||||||
| T_FINALLY | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
|
| T_FINALLY | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
|
||||||
| T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT
|
| T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT
|
||||||
| T_BREAK | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
|
| T_BREAK | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
|
||||||
| T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER | T_FN
|
| T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_FN
|
||||||
| T_MATCH
|
| T_MATCH | T_ENUM
|
||||||
|
| T_ECHO { $$ = $1; if ($$ === "<?=") $this->emitError(new Error('Cannot use "<?=" as an identifier', attributes())); }
|
||||||
;
|
;
|
||||||
|
|
||||||
semi_reserved:
|
semi_reserved:
|
||||||
reserved_non_modifiers
|
reserved_non_modifiers
|
||||||
| T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC
|
| T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC | T_READONLY
|
||||||
;
|
;
|
||||||
|
|
||||||
identifier_ex:
|
identifier_maybe_reserved:
|
||||||
T_STRING { $$ = Node\Identifier[$1]; }
|
T_STRING { $$ = Node\Identifier[$1]; }
|
||||||
| semi_reserved { $$ = Node\Identifier[$1]; }
|
| semi_reserved { $$ = Node\Identifier[$1]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
identifier:
|
identifier_not_reserved:
|
||||||
T_STRING { $$ = Node\Identifier[$1]; }
|
T_STRING { $$ = Node\Identifier[$1]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -112,7 +236,7 @@ top_statement:
|
|||||||
statement { $$ = $1; }
|
statement { $$ = $1; }
|
||||||
| function_declaration_statement { $$ = $1; }
|
| function_declaration_statement { $$ = $1; }
|
||||||
| class_declaration_statement { $$ = $1; }
|
| class_declaration_statement { $$ = $1; }
|
||||||
| T_HALT_COMPILER
|
| T_HALT_COMPILER '(' ')' ';'
|
||||||
{ $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; }
|
{ $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; }
|
||||||
| T_NAMESPACE namespace_declaration_name semi
|
| T_NAMESPACE namespace_declaration_name semi
|
||||||
{ $$ = Stmt\Namespace_[$2, null];
|
{ $$ = Stmt\Namespace_[$2, null];
|
||||||
@ -175,16 +299,16 @@ non_empty_inline_use_declarations:
|
|||||||
|
|
||||||
unprefixed_use_declaration:
|
unprefixed_use_declaration:
|
||||||
namespace_name
|
namespace_name
|
||||||
{ $$ = Stmt\UseUse[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
|
{ $$ = Node\UseItem[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
|
||||||
| namespace_name T_AS identifier
|
| namespace_name T_AS identifier_not_reserved
|
||||||
{ $$ = Stmt\UseUse[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
|
{ $$ = Node\UseItem[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
|
||||||
;
|
;
|
||||||
|
|
||||||
use_declaration:
|
use_declaration:
|
||||||
legacy_namespace_name
|
legacy_namespace_name
|
||||||
{ $$ = Stmt\UseUse[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
|
{ $$ = Node\UseItem[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
|
||||||
| legacy_namespace_name T_AS identifier
|
| legacy_namespace_name T_AS identifier_not_reserved
|
||||||
{ $$ = Stmt\UseUse[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
|
{ $$ = Node\UseItem[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
|
||||||
;
|
;
|
||||||
|
|
||||||
inline_use_declaration:
|
inline_use_declaration:
|
||||||
@ -203,7 +327,7 @@ non_empty_constant_declaration_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
constant_declaration:
|
constant_declaration:
|
||||||
identifier '=' expr { $$ = Node\Const_[$1, $3]; }
|
identifier_not_reserved '=' expr { $$ = Node\Const_[$1, $3]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
class_const_list:
|
class_const_list:
|
||||||
@ -216,7 +340,7 @@ non_empty_class_const_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
class_const:
|
class_const:
|
||||||
identifier_ex '=' expr { $$ = Node\Const_[$1, $3]; }
|
identifier_maybe_reserved '=' expr { $$ = Node\Const_[$1, $3]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
inner_statement_list_ex:
|
inner_statement_list_ex:
|
||||||
@ -284,8 +408,8 @@ non_empty_statement:
|
|||||||
| T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt\Declare_[$3, $5]; }
|
| T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt\Declare_[$3, $5]; }
|
||||||
| T_TRY '{' inner_statement_list '}' catches optional_finally
|
| T_TRY '{' inner_statement_list '}' catches optional_finally
|
||||||
{ $$ = Stmt\TryCatch[$3, $5, $6]; $this->checkTryCatch($$); }
|
{ $$ = Stmt\TryCatch[$3, $5, $6]; $this->checkTryCatch($$); }
|
||||||
| T_GOTO identifier semi { $$ = Stmt\Goto_[$2]; }
|
| T_GOTO identifier_not_reserved semi { $$ = Stmt\Goto_[$2]; }
|
||||||
| identifier ':' { $$ = Stmt\Label[$1]; }
|
| identifier_not_reserved ':' { $$ = Stmt\Label[$1]; }
|
||||||
| error { $$ = array(); /* means: no statement */ }
|
| error { $$ = array(); /* means: no statement */ }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -327,7 +451,12 @@ non_empty_variables_list:
|
|||||||
|
|
||||||
optional_ref:
|
optional_ref:
|
||||||
/* empty */ { $$ = false; }
|
/* empty */ { $$ = false; }
|
||||||
| '&' { $$ = true; }
|
| ampersand { $$ = true; }
|
||||||
|
;
|
||||||
|
|
||||||
|
optional_arg_ref:
|
||||||
|
/* empty */ { $$ = false; }
|
||||||
|
| T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG { $$ = true; }
|
||||||
;
|
;
|
||||||
|
|
||||||
optional_ellipsis:
|
optional_ellipsis:
|
||||||
@ -340,31 +469,58 @@ block_or_error:
|
|||||||
| error { $$ = []; }
|
| error { $$ = []; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
identifier_maybe_readonly:
|
||||||
|
identifier_not_reserved { $$ = $1; }
|
||||||
|
| T_READONLY { $$ = Node\Identifier[$1]; }
|
||||||
|
;
|
||||||
|
|
||||||
function_declaration_statement:
|
function_declaration_statement:
|
||||||
T_FUNCTION optional_ref identifier '(' parameter_list ')' optional_return_type block_or_error
|
T_FUNCTION optional_ref identifier_maybe_readonly '(' parameter_list ')' optional_return_type block_or_error
|
||||||
{ $$ = Stmt\Function_[$3, ['byRef' => $2, 'params' => $5, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
|
{ $$ = Stmt\Function_[$3, ['byRef' => $2, 'params' => $5, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
|
||||||
| attributes T_FUNCTION optional_ref identifier '(' parameter_list ')' optional_return_type block_or_error
|
| attributes T_FUNCTION optional_ref identifier_maybe_readonly '(' parameter_list ')' optional_return_type block_or_error
|
||||||
{ $$ = Stmt\Function_[$4, ['byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
|
{ $$ = Stmt\Function_[$4, ['byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
class_declaration_statement:
|
class_declaration_statement:
|
||||||
class_entry_type identifier extends_from implements_list '{' class_statement_list '}'
|
class_entry_type identifier_not_reserved extends_from implements_list '{' class_statement_list '}'
|
||||||
{ $$ = Stmt\Class_[$2, ['type' => $1, 'extends' => $3, 'implements' => $4, 'stmts' => $6, 'attrGroups' => []]];
|
{ $$ = Stmt\Class_[$2, ['type' => $1, 'extends' => $3, 'implements' => $4, 'stmts' => $6, 'attrGroups' => []]];
|
||||||
$this->checkClass($$, #2); }
|
$this->checkClass($$, #2); }
|
||||||
| attributes class_entry_type identifier extends_from implements_list '{' class_statement_list '}'
|
| attributes class_entry_type identifier_not_reserved extends_from implements_list '{' class_statement_list '}'
|
||||||
{ $$ = Stmt\Class_[$3, ['type' => $2, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
|
{ $$ = Stmt\Class_[$3, ['type' => $2, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
|
||||||
$this->checkClass($$, #3); }
|
$this->checkClass($$, #3); }
|
||||||
| optional_attributes T_INTERFACE identifier interface_extends_list '{' class_statement_list '}'
|
| optional_attributes T_INTERFACE identifier_not_reserved interface_extends_list '{' class_statement_list '}'
|
||||||
{ $$ = Stmt\Interface_[$3, ['extends' => $4, 'stmts' => $6, 'attrGroups' => $1]];
|
{ $$ = Stmt\Interface_[$3, ['extends' => $4, 'stmts' => $6, 'attrGroups' => $1]];
|
||||||
$this->checkInterface($$, #3); }
|
$this->checkInterface($$, #3); }
|
||||||
| optional_attributes T_TRAIT identifier '{' class_statement_list '}'
|
| optional_attributes T_TRAIT identifier_not_reserved '{' class_statement_list '}'
|
||||||
{ $$ = Stmt\Trait_[$3, ['stmts' => $5, 'attrGroups' => $1]]; }
|
{ $$ = Stmt\Trait_[$3, ['stmts' => $5, 'attrGroups' => $1]]; }
|
||||||
|
| optional_attributes T_ENUM identifier_not_reserved enum_scalar_type implements_list '{' class_statement_list '}'
|
||||||
|
{ $$ = Stmt\Enum_[$3, ['scalarType' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
|
||||||
|
$this->checkEnum($$, #3); }
|
||||||
|
;
|
||||||
|
|
||||||
|
enum_scalar_type:
|
||||||
|
/* empty */ { $$ = null; }
|
||||||
|
| ':' type { $$ = $2; }
|
||||||
|
|
||||||
|
enum_case_expr:
|
||||||
|
/* empty */ { $$ = null; }
|
||||||
|
| '=' expr { $$ = $2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
class_entry_type:
|
class_entry_type:
|
||||||
T_CLASS { $$ = 0; }
|
T_CLASS { $$ = 0; }
|
||||||
| T_ABSTRACT T_CLASS { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
|
| class_modifiers T_CLASS { $$ = $1; }
|
||||||
| T_FINAL T_CLASS { $$ = Stmt\Class_::MODIFIER_FINAL; }
|
;
|
||||||
|
|
||||||
|
class_modifiers:
|
||||||
|
class_modifier { $$ = $1; }
|
||||||
|
| class_modifiers class_modifier { $this->checkClassModifier($1, $2, #2); $$ = $1 | $2; }
|
||||||
|
;
|
||||||
|
|
||||||
|
class_modifier:
|
||||||
|
T_ABSTRACT { $$ = Modifiers::ABSTRACT; }
|
||||||
|
| T_FINAL { $$ = Modifiers::FINAL; }
|
||||||
|
| T_READONLY { $$ = Modifiers::READONLY; }
|
||||||
;
|
;
|
||||||
|
|
||||||
extends_from:
|
extends_from:
|
||||||
@ -417,7 +573,7 @@ non_empty_declare_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
declare_list_element:
|
declare_list_element:
|
||||||
identifier '=' expr { $$ = Stmt\DeclareDeclare[$1, $3]; }
|
identifier_not_reserved '=' expr { $$ = Node\DeclareItem[$1, $3]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
switch_case_list:
|
switch_case_list:
|
||||||
@ -481,7 +637,8 @@ new_elseif_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
new_elseif:
|
new_elseif:
|
||||||
T_ELSEIF '(' expr ')' ':' inner_statement_list { $$ = Stmt\ElseIf_[$3, $6]; }
|
T_ELSEIF '(' expr ')' ':' inner_statement_list
|
||||||
|
{ $$ = Stmt\ElseIf_[$3, $6]; $this->fixupAlternativeElse($$); }
|
||||||
;
|
;
|
||||||
|
|
||||||
else_single:
|
else_single:
|
||||||
@ -491,14 +648,16 @@ else_single:
|
|||||||
|
|
||||||
new_else_single:
|
new_else_single:
|
||||||
/* empty */ { $$ = null; }
|
/* empty */ { $$ = null; }
|
||||||
| T_ELSE ':' inner_statement_list { $$ = Stmt\Else_[$3]; }
|
| T_ELSE ':' inner_statement_list
|
||||||
|
{ $$ = Stmt\Else_[$3]; $this->fixupAlternativeElse($$); }
|
||||||
;
|
;
|
||||||
|
|
||||||
foreach_variable:
|
foreach_variable:
|
||||||
variable { $$ = array($1, false); }
|
variable { $$ = array($1, false); }
|
||||||
| '&' variable { $$ = array($2, true); }
|
| ampersand variable { $$ = array($2, true); }
|
||||||
| list_expr { $$ = array($1, false); }
|
| list_expr { $$ = array($1, false); }
|
||||||
| array_short_syntax { $$ = array($1, false); }
|
| array_short_syntax
|
||||||
|
{ $$ = array($this->fixupArrayDestructuring($1), false); }
|
||||||
;
|
;
|
||||||
|
|
||||||
parameter_list:
|
parameter_list:
|
||||||
@ -511,21 +670,30 @@ non_empty_parameter_list:
|
|||||||
| non_empty_parameter_list ',' parameter { push($1, $3); }
|
| non_empty_parameter_list ',' parameter { push($1, $3); }
|
||||||
;
|
;
|
||||||
|
|
||||||
optional_visibility_modifier:
|
optional_property_modifiers:
|
||||||
/* empty */ { $$ = 0; }
|
/* empty */ { $$ = 0; }
|
||||||
| T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
|
| optional_property_modifiers property_modifier
|
||||||
| T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
|
{ $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
|
||||||
| T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
|
;
|
||||||
|
|
||||||
|
property_modifier:
|
||||||
|
T_PUBLIC { $$ = Modifiers::PUBLIC; }
|
||||||
|
| T_PROTECTED { $$ = Modifiers::PROTECTED; }
|
||||||
|
| T_PRIVATE { $$ = Modifiers::PRIVATE; }
|
||||||
|
| T_READONLY { $$ = Modifiers::READONLY; }
|
||||||
;
|
;
|
||||||
|
|
||||||
parameter:
|
parameter:
|
||||||
optional_attributes optional_visibility_modifier optional_type_without_static optional_ref optional_ellipsis plain_variable
|
optional_attributes optional_property_modifiers optional_type_without_static
|
||||||
|
optional_arg_ref optional_ellipsis plain_variable
|
||||||
{ $$ = new Node\Param($6, null, $3, $4, $5, attributes(), $2, $1);
|
{ $$ = new Node\Param($6, null, $3, $4, $5, attributes(), $2, $1);
|
||||||
$this->checkParam($$); }
|
$this->checkParam($$); }
|
||||||
| optional_attributes optional_visibility_modifier optional_type_without_static optional_ref optional_ellipsis plain_variable '=' expr
|
| optional_attributes optional_property_modifiers optional_type_without_static
|
||||||
|
optional_arg_ref optional_ellipsis plain_variable '=' expr
|
||||||
{ $$ = new Node\Param($6, $8, $3, $4, $5, attributes(), $2, $1);
|
{ $$ = new Node\Param($6, $8, $3, $4, $5, attributes(), $2, $1);
|
||||||
$this->checkParam($$); }
|
$this->checkParam($$); }
|
||||||
| optional_attributes optional_visibility_modifier optional_type_without_static optional_ref optional_ellipsis error
|
| optional_attributes optional_property_modifiers optional_type_without_static
|
||||||
|
optional_arg_ref optional_ellipsis error
|
||||||
{ $$ = new Node\Param(Expr\Error[], null, $3, $4, $5, attributes(), $2, $1); }
|
{ $$ = new Node\Param(Expr\Error[], null, $3, $4, $5, attributes(), $2, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -533,6 +701,7 @@ type_expr:
|
|||||||
type { $$ = $1; }
|
type { $$ = $1; }
|
||||||
| '?' type { $$ = Node\NullableType[$2]; }
|
| '?' type { $$ = Node\NullableType[$2]; }
|
||||||
| union_type { $$ = Node\UnionType[$1]; }
|
| union_type { $$ = Node\UnionType[$1]; }
|
||||||
|
| intersection_type { $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
type:
|
type:
|
||||||
@ -546,20 +715,52 @@ type_without_static:
|
|||||||
| T_CALLABLE { $$ = Node\Identifier['callable']; }
|
| T_CALLABLE { $$ = Node\Identifier['callable']; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
union_type_element:
|
||||||
|
type { $$ = $1; }
|
||||||
|
| '(' intersection_type ')' { $$ = $2; }
|
||||||
|
;
|
||||||
|
|
||||||
union_type:
|
union_type:
|
||||||
type '|' type { init($1, $3); }
|
union_type_element '|' union_type_element { init($1, $3); }
|
||||||
| union_type '|' type { push($1, $3); }
|
| union_type '|' union_type_element { push($1, $3); }
|
||||||
|
;
|
||||||
|
|
||||||
|
union_type_without_static_element:
|
||||||
|
type_without_static { $$ = $1; }
|
||||||
|
| '(' intersection_type_without_static ')' { $$ = $2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
union_type_without_static:
|
union_type_without_static:
|
||||||
type_without_static '|' type_without_static { init($1, $3); }
|
union_type_without_static_element '|' union_type_without_static_element { init($1, $3); }
|
||||||
| union_type_without_static '|' type_without_static { push($1, $3); }
|
| union_type_without_static '|' union_type_without_static_element { push($1, $3); }
|
||||||
|
;
|
||||||
|
|
||||||
|
intersection_type_list:
|
||||||
|
type T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type { init($1, $3); }
|
||||||
|
| intersection_type_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type
|
||||||
|
{ push($1, $3); }
|
||||||
|
;
|
||||||
|
|
||||||
|
intersection_type:
|
||||||
|
intersection_type_list { $$ = Node\IntersectionType[$1]; }
|
||||||
|
;
|
||||||
|
|
||||||
|
intersection_type_without_static_list:
|
||||||
|
type_without_static T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
|
||||||
|
{ init($1, $3); }
|
||||||
|
| intersection_type_without_static_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
|
||||||
|
{ push($1, $3); }
|
||||||
|
;
|
||||||
|
|
||||||
|
intersection_type_without_static:
|
||||||
|
intersection_type_without_static_list { $$ = Node\IntersectionType[$1]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
type_expr_without_static:
|
type_expr_without_static:
|
||||||
type_without_static { $$ = $1; }
|
type_without_static { $$ = $1; }
|
||||||
| '?' type_without_static { $$ = Node\NullableType[$2]; }
|
| '?' type_without_static { $$ = Node\NullableType[$2]; }
|
||||||
| union_type_without_static { $$ = Node\UnionType[$1]; }
|
| union_type_without_static { $$ = Node\UnionType[$1]; }
|
||||||
|
| intersection_type_without_static { $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
optional_type_without_static:
|
optional_type_without_static:
|
||||||
@ -576,6 +777,11 @@ optional_return_type:
|
|||||||
argument_list:
|
argument_list:
|
||||||
'(' ')' { $$ = array(); }
|
'(' ')' { $$ = array(); }
|
||||||
| '(' non_empty_argument_list optional_comma ')' { $$ = $2; }
|
| '(' non_empty_argument_list optional_comma ')' { $$ = $2; }
|
||||||
|
| '(' variadic_placeholder ')' { init($2); }
|
||||||
|
;
|
||||||
|
|
||||||
|
variadic_placeholder:
|
||||||
|
T_ELLIPSIS { $$ = Node\VariadicPlaceholder[]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
non_empty_argument_list:
|
non_empty_argument_list:
|
||||||
@ -585,9 +791,9 @@ non_empty_argument_list:
|
|||||||
|
|
||||||
argument:
|
argument:
|
||||||
expr { $$ = Node\Arg[$1, false, false]; }
|
expr { $$ = Node\Arg[$1, false, false]; }
|
||||||
| '&' variable { $$ = Node\Arg[$2, true, false]; }
|
| ampersand variable { $$ = Node\Arg[$2, true, false]; }
|
||||||
| T_ELLIPSIS expr { $$ = Node\Arg[$2, false, true]; }
|
| T_ELLIPSIS expr { $$ = Node\Arg[$2, false, true]; }
|
||||||
| identifier_ex ':' expr
|
| identifier_maybe_reserved ':' expr
|
||||||
{ $$ = new Node\Arg($3, false, false, attributes(), $1); }
|
{ $$ = new Node\Arg($3, false, false, attributes(), $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -601,7 +807,7 @@ non_empty_global_var_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
global_var:
|
global_var:
|
||||||
simple_variable { $$ = Expr\Variable[$1]; }
|
simple_variable { $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
static_var_list:
|
static_var_list:
|
||||||
@ -614,8 +820,8 @@ non_empty_static_var_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
static_var:
|
static_var:
|
||||||
plain_variable { $$ = Stmt\StaticVar[$1, null]; }
|
plain_variable { $$ = Node\StaticVar[$1, null]; }
|
||||||
| plain_variable '=' expr { $$ = Stmt\StaticVar[$1, $3]; }
|
| plain_variable '=' expr { $$ = Node\StaticVar[$1, $3]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
class_statement_list_ex:
|
class_statement_list_ex:
|
||||||
@ -636,10 +842,13 @@ class_statement:
|
|||||||
| optional_attributes method_modifiers T_CONST class_const_list semi
|
| optional_attributes method_modifiers T_CONST class_const_list semi
|
||||||
{ $$ = new Stmt\ClassConst($4, $2, attributes(), $1);
|
{ $$ = new Stmt\ClassConst($4, $2, attributes(), $1);
|
||||||
$this->checkClassConst($$, #2); }
|
$this->checkClassConst($$, #2); }
|
||||||
| optional_attributes method_modifiers T_FUNCTION optional_ref identifier_ex '(' parameter_list ')' optional_return_type method_body
|
| optional_attributes method_modifiers T_FUNCTION optional_ref identifier_maybe_reserved '(' parameter_list ')'
|
||||||
|
optional_return_type method_body
|
||||||
{ $$ = Stmt\ClassMethod[$5, ['type' => $2, 'byRef' => $4, 'params' => $7, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]];
|
{ $$ = Stmt\ClassMethod[$5, ['type' => $2, 'byRef' => $4, 'params' => $7, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]];
|
||||||
$this->checkClassMethod($$, #2); }
|
$this->checkClassMethod($$, #2); }
|
||||||
| T_USE class_name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
| T_USE class_name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
|
||||||
|
| optional_attributes T_CASE identifier_maybe_reserved enum_case_expr semi
|
||||||
|
{ $$ = Stmt\EnumCase[$3, $4, $1]; }
|
||||||
| error { $$ = null; /* will be skipped */ }
|
| error { $$ = null; /* will be skipped */ }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -656,22 +865,22 @@ trait_adaptation_list:
|
|||||||
trait_adaptation:
|
trait_adaptation:
|
||||||
trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
|
trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
|
||||||
{ $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
|
{ $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
|
||||||
| trait_method_reference T_AS member_modifier identifier_ex ';'
|
| trait_method_reference T_AS member_modifier identifier_maybe_reserved ';'
|
||||||
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
|
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
|
||||||
| trait_method_reference T_AS member_modifier ';'
|
| trait_method_reference T_AS member_modifier ';'
|
||||||
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, null]; }
|
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, null]; }
|
||||||
| trait_method_reference T_AS identifier ';'
|
| trait_method_reference T_AS identifier_not_reserved ';'
|
||||||
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
|
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
|
||||||
| trait_method_reference T_AS reserved_non_modifiers_identifier ';'
|
| trait_method_reference T_AS reserved_non_modifiers_identifier ';'
|
||||||
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
|
{ $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
trait_method_reference_fully_qualified:
|
trait_method_reference_fully_qualified:
|
||||||
name T_PAAMAYIM_NEKUDOTAYIM identifier_ex { $$ = array($1, $3); }
|
name T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved { $$ = array($1, $3); }
|
||||||
;
|
;
|
||||||
trait_method_reference:
|
trait_method_reference:
|
||||||
trait_method_reference_fully_qualified { $$ = $1; }
|
trait_method_reference_fully_qualified { $$ = $1; }
|
||||||
| identifier_ex { $$ = array(null, $1); }
|
| identifier_maybe_reserved { $$ = array(null, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
method_body:
|
method_body:
|
||||||
@ -695,12 +904,13 @@ non_empty_member_modifiers:
|
|||||||
;
|
;
|
||||||
|
|
||||||
member_modifier:
|
member_modifier:
|
||||||
T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
|
T_PUBLIC { $$ = Modifiers::PUBLIC; }
|
||||||
| T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
|
| T_PROTECTED { $$ = Modifiers::PROTECTED; }
|
||||||
| T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
|
| T_PRIVATE { $$ = Modifiers::PRIVATE; }
|
||||||
| T_STATIC { $$ = Stmt\Class_::MODIFIER_STATIC; }
|
| T_STATIC { $$ = Modifiers::STATIC; }
|
||||||
| T_ABSTRACT { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
|
| T_ABSTRACT { $$ = Modifiers::ABSTRACT; }
|
||||||
| T_FINAL { $$ = Stmt\Class_::MODIFIER_FINAL; }
|
| T_FINAL { $$ = Modifiers::FINAL; }
|
||||||
|
| T_READONLY { $$ = Modifiers::READONLY; }
|
||||||
;
|
;
|
||||||
|
|
||||||
property_declaration_list:
|
property_declaration_list:
|
||||||
@ -718,8 +928,8 @@ property_decl_name:
|
|||||||
;
|
;
|
||||||
|
|
||||||
property_declaration:
|
property_declaration:
|
||||||
property_decl_name { $$ = Stmt\PropertyProperty[$1, null]; }
|
property_decl_name { $$ = Node\PropertyItem[$1, null]; }
|
||||||
| property_decl_name '=' expr { $$ = Stmt\PropertyProperty[$1, $3]; }
|
| property_decl_name '=' expr { $$ = Node\PropertyItem[$1, $3]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
expr_list_forbid_comma:
|
expr_list_forbid_comma:
|
||||||
@ -743,9 +953,16 @@ for_expr:
|
|||||||
expr:
|
expr:
|
||||||
variable { $$ = $1; }
|
variable { $$ = $1; }
|
||||||
| list_expr '=' expr { $$ = Expr\Assign[$1, $3]; }
|
| list_expr '=' expr { $$ = Expr\Assign[$1, $3]; }
|
||||||
| array_short_syntax '=' expr { $$ = Expr\Assign[$1, $3]; }
|
| array_short_syntax '=' expr
|
||||||
|
{ $$ = Expr\Assign[$this->fixupArrayDestructuring($1), $3]; }
|
||||||
| variable '=' expr { $$ = Expr\Assign[$1, $3]; }
|
| variable '=' expr { $$ = Expr\Assign[$1, $3]; }
|
||||||
| variable '=' '&' variable { $$ = Expr\AssignRef[$1, $4]; }
|
| variable '=' ampersand variable { $$ = Expr\AssignRef[$1, $4]; }
|
||||||
|
| variable '=' ampersand new_expr
|
||||||
|
{ $$ = Expr\AssignRef[$1, $4];
|
||||||
|
if (!$this->phpVersion->allowsAssignNewByReference()) {
|
||||||
|
$this->emitError(new Error('Cannot assign new by reference', attributes()));
|
||||||
|
}
|
||||||
|
}
|
||||||
| new_expr { $$ = $1; }
|
| new_expr { $$ = $1; }
|
||||||
| match { $$ = $1; }
|
| match { $$ = $1; }
|
||||||
| T_CLONE expr { $$ = Expr\Clone_[$2]; }
|
| T_CLONE expr { $$ = Expr\Clone_[$2]; }
|
||||||
@ -772,7 +989,8 @@ expr:
|
|||||||
| expr T_LOGICAL_AND expr { $$ = Expr\BinaryOp\LogicalAnd[$1, $3]; }
|
| expr T_LOGICAL_AND expr { $$ = Expr\BinaryOp\LogicalAnd[$1, $3]; }
|
||||||
| expr T_LOGICAL_XOR expr { $$ = Expr\BinaryOp\LogicalXor[$1, $3]; }
|
| expr T_LOGICAL_XOR expr { $$ = Expr\BinaryOp\LogicalXor[$1, $3]; }
|
||||||
| expr '|' expr { $$ = Expr\BinaryOp\BitwiseOr [$1, $3]; }
|
| expr '|' expr { $$ = Expr\BinaryOp\BitwiseOr [$1, $3]; }
|
||||||
| expr '&' expr { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
|
| expr T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG expr { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
|
||||||
|
| expr T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG expr { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
|
||||||
| expr '^' expr { $$ = Expr\BinaryOp\BitwiseXor[$1, $3]; }
|
| expr '^' expr { $$ = Expr\BinaryOp\BitwiseXor[$1, $3]; }
|
||||||
| expr '.' expr { $$ = Expr\BinaryOp\Concat [$1, $3]; }
|
| expr '.' expr { $$ = Expr\BinaryOp\Concat [$1, $3]; }
|
||||||
| expr '+' expr { $$ = Expr\BinaryOp\Plus [$1, $3]; }
|
| expr '+' expr { $$ = Expr\BinaryOp\Plus [$1, $3]; }
|
||||||
@ -832,18 +1050,18 @@ expr:
|
|||||||
| T_YIELD_FROM expr { $$ = Expr\YieldFrom[$2]; }
|
| T_YIELD_FROM expr { $$ = Expr\YieldFrom[$2]; }
|
||||||
| T_THROW expr { $$ = Expr\Throw_[$2]; }
|
| T_THROW expr { $$ = Expr\Throw_[$2]; }
|
||||||
|
|
||||||
| T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr
|
| T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
|
||||||
{ $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $2, 'params' => $4, 'returnType' => $6, 'expr' => $8, 'attrGroups' => []]]; }
|
{ $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $2, 'params' => $4, 'returnType' => $6, 'expr' => $8, 'attrGroups' => []]]; }
|
||||||
| T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr
|
| T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
|
||||||
{ $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => []]]; }
|
{ $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => []]]; }
|
||||||
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
|
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
|
||||||
{ $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
|
{ $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
|
||||||
| T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
|
| T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
|
||||||
{ $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => []]]; }
|
{ $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => []]]; }
|
||||||
|
|
||||||
| attributes T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr
|
| attributes T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
|
||||||
{ $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => $1]]; }
|
{ $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => $1]]; }
|
||||||
| attributes T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr
|
| attributes T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
|
||||||
{ $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $4, 'params' => $6, 'returnType' => $8, 'expr' => $10, 'attrGroups' => $1]]; }
|
{ $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $4, 'params' => $6, 'returnType' => $8, 'expr' => $10, 'attrGroups' => $1]]; }
|
||||||
| attributes T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
|
| attributes T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
|
||||||
{ $$ = Expr\Closure[['static' => false, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
|
{ $$ = Expr\Closure[['static' => false, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
|
||||||
@ -878,11 +1096,16 @@ non_empty_lexical_var_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
lexical_var:
|
lexical_var:
|
||||||
optional_ref plain_variable { $$ = Expr\ClosureUse[$2, $1]; }
|
optional_ref plain_variable { $$ = Node\ClosureUse[$2, $1]; }
|
||||||
|
;
|
||||||
|
|
||||||
|
name_readonly:
|
||||||
|
T_READONLY { $$ = Name[$1]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
function_call:
|
function_call:
|
||||||
name argument_list { $$ = Expr\FuncCall[$1, $2]; }
|
name argument_list { $$ = Expr\FuncCall[$1, $2]; }
|
||||||
|
| name_readonly argument_list { $$ = Expr\FuncCall[$1, $2]; }
|
||||||
| callable_expr argument_list { $$ = Expr\FuncCall[$1, $2]; }
|
| callable_expr argument_list { $$ = Expr\FuncCall[$1, $2]; }
|
||||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
|
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
|
||||||
{ $$ = Expr\StaticCall[$1, $3, $4]; }
|
{ $$ = Expr\StaticCall[$1, $3, $4]; }
|
||||||
@ -920,7 +1143,7 @@ exit_expr:
|
|||||||
backticks_expr:
|
backticks_expr:
|
||||||
/* empty */ { $$ = array(); }
|
/* empty */ { $$ = array(); }
|
||||||
| T_ENCAPSED_AND_WHITESPACE
|
| T_ENCAPSED_AND_WHITESPACE
|
||||||
{ $$ = array(Scalar\EncapsedStringPart[Scalar\String_::parseEscapeSequences($1, '`')]); }
|
{ $$ = array(Node\InterpolatedStringPart[Scalar\String_::parseEscapeSequences($1, '`')]); }
|
||||||
| encaps_list { parseEncapsed($1, '`', true); $$ = $1; }
|
| encaps_list { parseEncapsed($1, '`', true); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -942,8 +1165,10 @@ constant:
|
|||||||
;
|
;
|
||||||
|
|
||||||
class_constant:
|
class_constant:
|
||||||
class_name_or_var T_PAAMAYIM_NEKUDOTAYIM identifier_ex
|
class_name_or_var T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved
|
||||||
{ $$ = Expr\ClassConstFetch[$1, $3]; }
|
{ $$ = Expr\ClassConstFetch[$1, $3]; }
|
||||||
|
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '{' expr '}'
|
||||||
|
{ $$ = Expr\ClassConstFetch[$1, $4]; }
|
||||||
/* We interpret an isolated FOO:: as an unfinished class constant fetch. It could also be
|
/* We interpret an isolated FOO:: as an unfinished class constant fetch. It could also be
|
||||||
an unfinished static property fetch or unfinished scoped call. */
|
an unfinished static property fetch or unfinished scoped call. */
|
||||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM error
|
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM error
|
||||||
@ -959,19 +1184,19 @@ array_short_syntax:
|
|||||||
dereferencable_scalar:
|
dereferencable_scalar:
|
||||||
T_ARRAY '(' array_pair_list ')'
|
T_ARRAY '(' array_pair_list ')'
|
||||||
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
|
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
|
||||||
$$ = new Expr\Array_($3, $attrs); }
|
$$ = new Expr\Array_($3, $attrs);
|
||||||
| array_short_syntax { $$ = $1; }
|
$this->createdArrays->attach($$); }
|
||||||
| T_CONSTANT_ENCAPSED_STRING
|
| array_short_syntax { $$ = $1; $this->createdArrays->attach($$); }
|
||||||
{ $attrs = attributes(); $attrs['kind'] = strKind($1);
|
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar\String_::fromString($1, attributes()); }
|
||||||
$$ = new Scalar\String_(Scalar\String_::parse($1), $attrs); }
|
|
||||||
| '"' encaps_list '"'
|
| '"' encaps_list '"'
|
||||||
{ $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
|
{ $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
|
||||||
parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
|
parseEncapsed($2, '"', true); $$ = new Scalar\InterpolatedString($2, $attrs); }
|
||||||
;
|
;
|
||||||
|
|
||||||
scalar:
|
scalar:
|
||||||
T_LNUMBER { $$ = $this->parseLNumber($1, attributes()); }
|
T_LNUMBER
|
||||||
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
|
{ $$ = $this->parseLNumber($1, attributes(), $this->phpVersion->allowsInvalidOctals()); }
|
||||||
|
| T_DNUMBER { $$ = Scalar\Float_::fromString($1, attributes()); }
|
||||||
| dereferencable_scalar { $$ = $1; }
|
| dereferencable_scalar { $$ = $1; }
|
||||||
| constant { $$ = $1; }
|
| constant { $$ = $1; }
|
||||||
| class_constant { $$ = $1; }
|
| class_constant { $$ = $1; }
|
||||||
@ -1007,7 +1232,7 @@ callable_expr:
|
|||||||
;
|
;
|
||||||
|
|
||||||
callable_variable:
|
callable_variable:
|
||||||
simple_variable { $$ = Expr\Variable[$1]; }
|
simple_variable { $$ = $1; }
|
||||||
| array_object_dereferencable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
| array_object_dereferencable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
||||||
| array_object_dereferencable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
| array_object_dereferencable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
||||||
| function_call { $$ = $1; }
|
| function_call { $$ = $1; }
|
||||||
@ -1032,15 +1257,15 @@ variable:
|
|||||||
;
|
;
|
||||||
|
|
||||||
simple_variable:
|
simple_variable:
|
||||||
T_VARIABLE { $$ = parseVar($1); }
|
plain_variable { $$ = $1; }
|
||||||
| '$' '{' expr '}' { $$ = $3; }
|
| '$' '{' expr '}' { $$ = Expr\Variable[$3]; }
|
||||||
| '$' simple_variable { $$ = Expr\Variable[$2]; }
|
| '$' simple_variable { $$ = Expr\Variable[$2]; }
|
||||||
| '$' error { $$ = Expr\Error[]; $this->errorState = 2; }
|
| '$' error { $$ = Expr\Variable[Expr\Error[]]; $this->errorState = 2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
static_member_prop_name:
|
static_member_prop_name:
|
||||||
simple_variable
|
simple_variable
|
||||||
{ $var = $1; $$ = \is_string($var) ? Node\VarLikeIdentifier[$var] : $var; }
|
{ $var = $1->name; $$ = \is_string($var) ? Node\VarLikeIdentifier[$var] : $var; }
|
||||||
;
|
;
|
||||||
|
|
||||||
static_member:
|
static_member:
|
||||||
@ -1049,7 +1274,7 @@ static_member:
|
|||||||
;
|
;
|
||||||
|
|
||||||
new_variable:
|
new_variable:
|
||||||
simple_variable { $$ = Expr\Variable[$1]; }
|
simple_variable { $$ = $1; }
|
||||||
| new_variable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
| new_variable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
||||||
| new_variable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
| new_variable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
||||||
| new_variable T_OBJECT_OPERATOR property_name { $$ = Expr\PropertyFetch[$1, $3]; }
|
| new_variable T_OBJECT_OPERATOR property_name { $$ = Expr\PropertyFetch[$1, $3]; }
|
||||||
@ -1061,25 +1286,27 @@ new_variable:
|
|||||||
;
|
;
|
||||||
|
|
||||||
member_name:
|
member_name:
|
||||||
identifier_ex { $$ = $1; }
|
identifier_maybe_reserved { $$ = $1; }
|
||||||
| '{' expr '}' { $$ = $2; }
|
| '{' expr '}' { $$ = $2; }
|
||||||
| simple_variable { $$ = Expr\Variable[$1]; }
|
| simple_variable { $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
property_name:
|
property_name:
|
||||||
identifier { $$ = $1; }
|
identifier_not_reserved { $$ = $1; }
|
||||||
| '{' expr '}' { $$ = $2; }
|
| '{' expr '}' { $$ = $2; }
|
||||||
| simple_variable { $$ = Expr\Variable[$1]; }
|
| simple_variable { $$ = $1; }
|
||||||
| error { $$ = Expr\Error[]; $this->errorState = 2; }
|
| error { $$ = Expr\Error[]; $this->errorState = 2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
list_expr:
|
list_expr:
|
||||||
T_LIST '(' inner_array_pair_list ')' { $$ = Expr\List_[$3]; }
|
T_LIST '(' inner_array_pair_list ')'
|
||||||
|
{ $$ = Expr\List_[$3]; $$->setAttribute('kind', Expr\List_::KIND_LIST);
|
||||||
|
$this->postprocessList($$); }
|
||||||
;
|
;
|
||||||
|
|
||||||
array_pair_list:
|
array_pair_list:
|
||||||
inner_array_pair_list
|
inner_array_pair_list
|
||||||
{ $$ = $1; $end = count($$)-1; if ($$[$end] === null) array_pop($$); }
|
{ $$ = $1; $end = count($$)-1; if ($$[$end]->value instanceof Expr\Error) array_pop($$); }
|
||||||
;
|
;
|
||||||
|
|
||||||
comma_or_error:
|
comma_or_error:
|
||||||
@ -1094,14 +1321,18 @@ inner_array_pair_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
array_pair:
|
array_pair:
|
||||||
expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
expr { $$ = Node\ArrayItem[$1, null, false]; }
|
||||||
| '&' variable { $$ = Expr\ArrayItem[$2, null, true]; }
|
| ampersand variable { $$ = Node\ArrayItem[$2, null, true]; }
|
||||||
| list_expr { $$ = Expr\ArrayItem[$1, null, false]; }
|
| list_expr { $$ = Node\ArrayItem[$1, null, false]; }
|
||||||
| expr T_DOUBLE_ARROW expr { $$ = Expr\ArrayItem[$3, $1, false]; }
|
| expr T_DOUBLE_ARROW expr { $$ = Node\ArrayItem[$3, $1, false]; }
|
||||||
| expr T_DOUBLE_ARROW '&' variable { $$ = Expr\ArrayItem[$4, $1, true]; }
|
| expr T_DOUBLE_ARROW ampersand variable { $$ = Node\ArrayItem[$4, $1, true]; }
|
||||||
| expr T_DOUBLE_ARROW list_expr { $$ = Expr\ArrayItem[$3, $1, false]; }
|
| expr T_DOUBLE_ARROW list_expr { $$ = Node\ArrayItem[$3, $1, false]; }
|
||||||
| T_ELLIPSIS expr { $$ = Expr\ArrayItem[$2, null, false, attributes(), true]; }
|
| T_ELLIPSIS expr { $$ = new Node\ArrayItem($2, null, false, attributes(), true); }
|
||||||
| /* empty */ { $$ = null; }
|
| /* empty */
|
||||||
|
{ /* Create an Error node now to remember the position. We'll later either report an error,
|
||||||
|
or convert this into a null element, depending on whether this is a creation or destructuring context. */
|
||||||
|
$attrs = $this->createEmptyElemAttributes($this->lookaheadStartAttributes);
|
||||||
|
$$ = new Node\ArrayItem(new Expr\Error($attrs), null, false, $attrs); }
|
||||||
;
|
;
|
||||||
|
|
||||||
encaps_list:
|
encaps_list:
|
||||||
@ -1112,7 +1343,7 @@ encaps_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
encaps_string_part:
|
encaps_string_part:
|
||||||
T_ENCAPSED_AND_WHITESPACE { $$ = Scalar\EncapsedStringPart[$1]; }
|
T_ENCAPSED_AND_WHITESPACE { $$ = Node\InterpolatedStringPart[$1]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
encaps_str_varname:
|
encaps_str_varname:
|
||||||
@ -1122,8 +1353,10 @@ encaps_str_varname:
|
|||||||
encaps_var:
|
encaps_var:
|
||||||
plain_variable { $$ = $1; }
|
plain_variable { $$ = $1; }
|
||||||
| plain_variable '[' encaps_var_offset ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
| plain_variable '[' encaps_var_offset ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
|
||||||
| plain_variable T_OBJECT_OPERATOR identifier { $$ = Expr\PropertyFetch[$1, $3]; }
|
| plain_variable T_OBJECT_OPERATOR identifier_not_reserved
|
||||||
| plain_variable T_NULLSAFE_OBJECT_OPERATOR identifier { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
|
{ $$ = Expr\PropertyFetch[$1, $3]; }
|
||||||
|
| plain_variable T_NULLSAFE_OBJECT_OPERATOR identifier_not_reserved
|
||||||
|
{ $$ = Expr\NullsafePropertyFetch[$1, $3]; }
|
||||||
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = Expr\Variable[$2]; }
|
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = Expr\Variable[$2]; }
|
||||||
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = Expr\Variable[$2]; }
|
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = Expr\Variable[$2]; }
|
||||||
| T_DOLLAR_OPEN_CURLY_BRACES encaps_str_varname '[' expr ']' '}'
|
| T_DOLLAR_OPEN_CURLY_BRACES encaps_str_varname '[' expr ']' '}'
|
1026
grammar/php5.y
1026
grammar/php5.y
File diff suppressed because it is too large
Load Diff
184
grammar/phpyLang.php
Normal file
184
grammar/phpyLang.php
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
///////////////////////////////
|
||||||
|
/// Utility regex constants ///
|
||||||
|
///////////////////////////////
|
||||||
|
|
||||||
|
const LIB = '(?(DEFINE)
|
||||||
|
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
|
||||||
|
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
|
||||||
|
(?<string>(?&singleQuotedString)|(?&doubleQuotedString))
|
||||||
|
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
|
||||||
|
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
|
||||||
|
)';
|
||||||
|
|
||||||
|
const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?¶ms)\][^[\]]*+)*+)\]';
|
||||||
|
const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
|
||||||
|
|
||||||
|
///////////////////////////////
|
||||||
|
/// Preprocessing functions ///
|
||||||
|
///////////////////////////////
|
||||||
|
|
||||||
|
function preprocessGrammar($code) {
|
||||||
|
$code = resolveNodes($code);
|
||||||
|
$code = resolveMacros($code);
|
||||||
|
$code = resolveStackAccess($code);
|
||||||
|
|
||||||
|
return $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveNodes($code) {
|
||||||
|
return preg_replace_callback(
|
||||||
|
'~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
|
||||||
|
function ($matches) {
|
||||||
|
// recurse
|
||||||
|
$matches['params'] = resolveNodes($matches['params']);
|
||||||
|
|
||||||
|
$params = magicSplit(
|
||||||
|
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
||||||
|
$matches['params']
|
||||||
|
);
|
||||||
|
|
||||||
|
$paramCode = '';
|
||||||
|
foreach ($params as $param) {
|
||||||
|
$paramCode .= $param . ', ';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
|
||||||
|
},
|
||||||
|
$code
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveMacros($code) {
|
||||||
|
return preg_replace_callback(
|
||||||
|
'~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
|
||||||
|
function ($matches) {
|
||||||
|
// recurse
|
||||||
|
$matches['args'] = resolveMacros($matches['args']);
|
||||||
|
|
||||||
|
$name = $matches['name'];
|
||||||
|
$args = magicSplit(
|
||||||
|
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
||||||
|
$matches['args']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ('attributes' === $name) {
|
||||||
|
assertArgs(0, $args, $name);
|
||||||
|
return '$this->startAttributeStack[#1] + $this->endAttributes';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('stackAttributes' === $name) {
|
||||||
|
assertArgs(1, $args, $name);
|
||||||
|
return '$this->startAttributeStack[' . $args[0] . ']'
|
||||||
|
. ' + $this->endAttributeStack[' . $args[0] . ']';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('init' === $name) {
|
||||||
|
return '$$ = array(' . implode(', ', $args) . ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('push' === $name) {
|
||||||
|
assertArgs(2, $args, $name);
|
||||||
|
|
||||||
|
return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('pushNormalizing' === $name) {
|
||||||
|
assertArgs(2, $args, $name);
|
||||||
|
|
||||||
|
return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
|
||||||
|
. ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('toArray' == $name) {
|
||||||
|
assertArgs(1, $args, $name);
|
||||||
|
|
||||||
|
return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('parseVar' === $name) {
|
||||||
|
assertArgs(1, $args, $name);
|
||||||
|
|
||||||
|
return 'substr(' . $args[0] . ', 1)';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('parseEncapsed' === $name) {
|
||||||
|
assertArgs(3, $args, $name);
|
||||||
|
|
||||||
|
return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\InterpolatedStringPart) {'
|
||||||
|
. ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('makeNop' === $name) {
|
||||||
|
assertArgs(3, $args, $name);
|
||||||
|
|
||||||
|
return '$startAttributes = ' . $args[1] . ';'
|
||||||
|
. ' if (isset($startAttributes[\'comments\']))'
|
||||||
|
. ' { ' . $args[0] . ' = new Stmt\Nop($startAttributes + ' . $args[2] . '); }'
|
||||||
|
. ' else { ' . $args[0] . ' = null; }';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('makeZeroLengthNop' == $name) {
|
||||||
|
assertArgs(2, $args, $name);
|
||||||
|
|
||||||
|
return '$startAttributes = ' . $args[1] . ';'
|
||||||
|
. ' if (isset($startAttributes[\'comments\']))'
|
||||||
|
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createCommentNopAttributes($startAttributes[\'comments\'])); }'
|
||||||
|
. ' else { ' . $args[0] . ' = null; }';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('prependLeadingComments' === $name) {
|
||||||
|
assertArgs(1, $args, $name);
|
||||||
|
|
||||||
|
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
||||||
|
. 'if (!empty($attrs[\'comments\'])) {'
|
||||||
|
. '$stmts[0]->setAttribute(\'comments\', '
|
||||||
|
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $matches[0];
|
||||||
|
},
|
||||||
|
$code
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertArgs($num, $args, $name) {
|
||||||
|
if ($num != count($args)) {
|
||||||
|
die('Wrong argument count for ' . $name . '().');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveStackAccess($code) {
|
||||||
|
$code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
|
||||||
|
$code = preg_replace('/#(\d+)/', '$$1', $code);
|
||||||
|
return $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeTrailingWhitespace($code) {
|
||||||
|
$lines = explode("\n", $code);
|
||||||
|
$lines = array_map('rtrim', $lines);
|
||||||
|
return implode("\n", $lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////
|
||||||
|
/// Regex helper functions ///
|
||||||
|
//////////////////////////////
|
||||||
|
|
||||||
|
function regex($regex) {
|
||||||
|
return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
|
||||||
|
}
|
||||||
|
|
||||||
|
function magicSplit($regex, $string) {
|
||||||
|
$pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
|
||||||
|
|
||||||
|
foreach ($pieces as &$piece) {
|
||||||
|
$piece = trim($piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pieces === ['']) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pieces;
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
<?php
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
$grammarFileToName = [
|
require __DIR__ . '/phpyLang.php';
|
||||||
__DIR__ . '/php5.y' => 'Php5',
|
|
||||||
__DIR__ . '/php7.y' => 'Php7',
|
$parserToDefines = [
|
||||||
|
'Php7' => ['PHP7' => true],
|
||||||
|
'Php8' => ['PHP8' => true],
|
||||||
];
|
];
|
||||||
|
|
||||||
$tokensFile = __DIR__ . '/tokens.y';
|
$grammarFile = __DIR__ . '/php.y';
|
||||||
$tokensTemplate = __DIR__ . '/tokens.template';
|
|
||||||
$skeletonFile = __DIR__ . '/parser.template';
|
$skeletonFile = __DIR__ . '/parser.template';
|
||||||
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
||||||
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
||||||
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
||||||
$tokensResultsFile = $resultDir . '/Tokens.php';
|
|
||||||
|
|
||||||
$kmyacc = getenv('KMYACC');
|
$kmyacc = getenv('KMYACC');
|
||||||
if (!$kmyacc) {
|
if (!$kmyacc) {
|
||||||
@ -23,36 +23,16 @@ $options = array_flip($argv);
|
|||||||
$optionDebug = isset($options['--debug']);
|
$optionDebug = isset($options['--debug']);
|
||||||
$optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
|
$optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
|
||||||
|
|
||||||
///////////////////////////////
|
|
||||||
/// Utility regex constants ///
|
|
||||||
///////////////////////////////
|
|
||||||
|
|
||||||
const LIB = '(?(DEFINE)
|
|
||||||
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
|
|
||||||
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
|
|
||||||
(?<string>(?&singleQuotedString)|(?&doubleQuotedString))
|
|
||||||
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
|
|
||||||
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
|
|
||||||
)';
|
|
||||||
|
|
||||||
const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?¶ms)\][^[\]]*+)*+)\]';
|
|
||||||
const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
|
|
||||||
|
|
||||||
///////////////////
|
///////////////////
|
||||||
/// Main script ///
|
/// Main script ///
|
||||||
///////////////////
|
///////////////////
|
||||||
|
|
||||||
$tokens = file_get_contents($tokensFile);
|
foreach ($parserToDefines as $name => $defines) {
|
||||||
|
|
||||||
foreach ($grammarFileToName as $grammarFile => $name) {
|
|
||||||
echo "Building temporary $name grammar file.\n";
|
echo "Building temporary $name grammar file.\n";
|
||||||
|
|
||||||
$grammarCode = file_get_contents($grammarFile);
|
$grammarCode = file_get_contents($grammarFile);
|
||||||
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);
|
$grammarCode = replaceIfBlocks($grammarCode, $defines);
|
||||||
|
$grammarCode = preprocessGrammar($grammarCode);
|
||||||
$grammarCode = resolveNodes($grammarCode);
|
|
||||||
$grammarCode = resolveMacros($grammarCode);
|
|
||||||
$grammarCode = resolveStackAccess($grammarCode);
|
|
||||||
|
|
||||||
file_put_contents($tmpGrammarFile, $grammarCode);
|
file_put_contents($tmpGrammarFile, $grammarCode);
|
||||||
|
|
||||||
@ -68,160 +48,14 @@ foreach ($grammarFileToName as $grammarFile => $name) {
|
|||||||
file_put_contents("$resultDir/$name.php", $resultCode);
|
file_put_contents("$resultDir/$name.php", $resultCode);
|
||||||
unlink($tmpResultFile);
|
unlink($tmpResultFile);
|
||||||
|
|
||||||
echo "Building token definition.\n";
|
|
||||||
$output = execCmd("$kmyacc -m $tokensTemplate $tmpGrammarFile");
|
|
||||||
rename($tmpResultFile, $tokensResultsFile);
|
|
||||||
|
|
||||||
if (!$optionKeepTmpGrammar) {
|
if (!$optionKeepTmpGrammar) {
|
||||||
unlink($tmpGrammarFile);
|
unlink($tmpGrammarFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////
|
////////////////////////////////
|
||||||
/// Preprocessing functions ///
|
/// Utility helper functions ///
|
||||||
///////////////////////////////
|
////////////////////////////////
|
||||||
|
|
||||||
function resolveNodes($code) {
|
|
||||||
return preg_replace_callback(
|
|
||||||
'~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
|
|
||||||
function($matches) {
|
|
||||||
// recurse
|
|
||||||
$matches['params'] = resolveNodes($matches['params']);
|
|
||||||
|
|
||||||
$params = magicSplit(
|
|
||||||
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
|
||||||
$matches['params']
|
|
||||||
);
|
|
||||||
|
|
||||||
$paramCode = '';
|
|
||||||
foreach ($params as $param) {
|
|
||||||
$paramCode .= $param . ', ';
|
|
||||||
}
|
|
||||||
|
|
||||||
return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
|
|
||||||
},
|
|
||||||
$code
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveMacros($code) {
|
|
||||||
return preg_replace_callback(
|
|
||||||
'~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
|
|
||||||
function($matches) {
|
|
||||||
// recurse
|
|
||||||
$matches['args'] = resolveMacros($matches['args']);
|
|
||||||
|
|
||||||
$name = $matches['name'];
|
|
||||||
$args = magicSplit(
|
|
||||||
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
|
||||||
$matches['args']
|
|
||||||
);
|
|
||||||
|
|
||||||
if ('attributes' === $name) {
|
|
||||||
assertArgs(0, $args, $name);
|
|
||||||
return '$this->startAttributeStack[#1] + $this->endAttributes';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('stackAttributes' === $name) {
|
|
||||||
assertArgs(1, $args, $name);
|
|
||||||
return '$this->startAttributeStack[' . $args[0] . ']'
|
|
||||||
. ' + $this->endAttributeStack[' . $args[0] . ']';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('init' === $name) {
|
|
||||||
return '$$ = array(' . implode(', ', $args) . ')';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('push' === $name) {
|
|
||||||
assertArgs(2, $args, $name);
|
|
||||||
|
|
||||||
return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('pushNormalizing' === $name) {
|
|
||||||
assertArgs(2, $args, $name);
|
|
||||||
|
|
||||||
return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
|
|
||||||
. ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('toArray' == $name) {
|
|
||||||
assertArgs(1, $args, $name);
|
|
||||||
|
|
||||||
return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('parseVar' === $name) {
|
|
||||||
assertArgs(1, $args, $name);
|
|
||||||
|
|
||||||
return 'substr(' . $args[0] . ', 1)';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('parseEncapsed' === $name) {
|
|
||||||
assertArgs(3, $args, $name);
|
|
||||||
|
|
||||||
return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
|
|
||||||
. ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('makeNop' === $name) {
|
|
||||||
assertArgs(3, $args, $name);
|
|
||||||
|
|
||||||
return '$startAttributes = ' . $args[1] . ';'
|
|
||||||
. ' if (isset($startAttributes[\'comments\']))'
|
|
||||||
. ' { ' . $args[0] . ' = new Stmt\Nop($startAttributes + ' . $args[2] . '); }'
|
|
||||||
. ' else { ' . $args[0] . ' = null; }';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('makeZeroLengthNop' == $name) {
|
|
||||||
assertArgs(2, $args, $name);
|
|
||||||
|
|
||||||
return '$startAttributes = ' . $args[1] . ';'
|
|
||||||
. ' if (isset($startAttributes[\'comments\']))'
|
|
||||||
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createCommentNopAttributes($startAttributes[\'comments\'])); }'
|
|
||||||
. ' else { ' . $args[0] . ' = null; }';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('strKind' === $name) {
|
|
||||||
assertArgs(1, $args, $name);
|
|
||||||
|
|
||||||
return '(' . $args[0] . '[0] === "\'" || (' . $args[0] . '[1] === "\'" && '
|
|
||||||
. '(' . $args[0] . '[0] === \'b\' || ' . $args[0] . '[0] === \'B\')) '
|
|
||||||
. '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('prependLeadingComments' === $name) {
|
|
||||||
assertArgs(1, $args, $name);
|
|
||||||
|
|
||||||
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
|
||||||
. 'if (!empty($attrs[\'comments\'])) {'
|
|
||||||
. '$stmts[0]->setAttribute(\'comments\', '
|
|
||||||
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $matches[0];
|
|
||||||
},
|
|
||||||
$code
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function assertArgs($num, $args, $name) {
|
|
||||||
if ($num != count($args)) {
|
|
||||||
die('Wrong argument count for ' . $name . '().');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveStackAccess($code) {
|
|
||||||
$code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
|
|
||||||
$code = preg_replace('/#(\d+)/', '$$1', $code);
|
|
||||||
return $code;
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeTrailingWhitespace($code) {
|
|
||||||
$lines = explode("\n", $code);
|
|
||||||
$lines = array_map('rtrim', $lines);
|
|
||||||
return implode("\n", $lines);
|
|
||||||
}
|
|
||||||
|
|
||||||
function ensureDirExists($dir) {
|
function ensureDirExists($dir) {
|
||||||
if (!is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
@ -230,7 +64,7 @@ function ensureDirExists($dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function execCmd($cmd) {
|
function execCmd($cmd) {
|
||||||
$output = trim(shell_exec("$cmd 2>&1"));
|
$output = trim(shell_exec("$cmd 2>&1") ?? '');
|
||||||
if ($output !== "") {
|
if ($output !== "") {
|
||||||
echo "> " . $cmd . "\n";
|
echo "> " . $cmd . "\n";
|
||||||
echo $output;
|
echo $output;
|
||||||
@ -238,24 +72,9 @@ function execCmd($cmd) {
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////
|
function replaceIfBlocks(string $code, array $defines): string {
|
||||||
/// Regex helper functions ///
|
return preg_replace_callback('/\n#if\s+(\w+)\n(.*?)\n#endif/s', function ($matches) use ($defines) {
|
||||||
//////////////////////////////
|
$value = $defines[$matches[1]] ?? false;
|
||||||
|
return $value ? $matches[2] : '';
|
||||||
function regex($regex) {
|
}, $code);
|
||||||
return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
|
|
||||||
}
|
|
||||||
|
|
||||||
function magicSplit($regex, $string) {
|
|
||||||
$pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
|
|
||||||
|
|
||||||
foreach ($pieces as &$piece) {
|
|
||||||
$piece = trim($piece);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($pieces === ['']) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $pieces;
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
$meta #
|
|
||||||
#semval($) $this->semValue
|
|
||||||
#semval($,%t) $this->semValue
|
|
||||||
#semval(%n) $this->stackPos-(%l-%n)
|
|
||||||
#semval(%n,%t) $this->stackPos-(%l-%n)
|
|
||||||
|
|
||||||
namespace PhpParser\Parser;
|
|
||||||
#include;
|
|
||||||
|
|
||||||
/* GENERATED file based on grammar/tokens.y */
|
|
||||||
final class Tokens
|
|
||||||
{
|
|
||||||
#tokenval
|
|
||||||
const %s = %n;
|
|
||||||
#endtokenval
|
|
||||||
}
|
|
113
grammar/tokens.y
113
grammar/tokens.y
@ -1,113 +0,0 @@
|
|||||||
/* We currently rely on the token ID mapping to be the same between PHP 5 and PHP 7 - so the same lexer can be used for
|
|
||||||
* both. This is enforced by sharing this token file. */
|
|
||||||
|
|
||||||
%right T_THROW
|
|
||||||
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
|
|
||||||
%left ','
|
|
||||||
%left T_LOGICAL_OR
|
|
||||||
%left T_LOGICAL_XOR
|
|
||||||
%left T_LOGICAL_AND
|
|
||||||
%right T_PRINT
|
|
||||||
%right T_YIELD
|
|
||||||
%right T_DOUBLE_ARROW
|
|
||||||
%right T_YIELD_FROM
|
|
||||||
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL T_COALESCE_EQUAL
|
|
||||||
%left '?' ':'
|
|
||||||
%right T_COALESCE
|
|
||||||
%left T_BOOLEAN_OR
|
|
||||||
%left T_BOOLEAN_AND
|
|
||||||
%left '|'
|
|
||||||
%left '^'
|
|
||||||
%left '&'
|
|
||||||
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
|
|
||||||
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
|
|
||||||
%left T_SL T_SR
|
|
||||||
%left '+' '-' '.'
|
|
||||||
%left '*' '/' '%'
|
|
||||||
%right '!'
|
|
||||||
%nonassoc T_INSTANCEOF
|
|
||||||
%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
|
|
||||||
%right T_POW
|
|
||||||
%right '['
|
|
||||||
%nonassoc T_NEW T_CLONE
|
|
||||||
%token T_EXIT
|
|
||||||
%token T_IF
|
|
||||||
%left T_ELSEIF
|
|
||||||
%left T_ELSE
|
|
||||||
%left T_ENDIF
|
|
||||||
%token T_LNUMBER
|
|
||||||
%token T_DNUMBER
|
|
||||||
%token T_STRING
|
|
||||||
%token T_STRING_VARNAME
|
|
||||||
%token T_VARIABLE
|
|
||||||
%token T_NUM_STRING
|
|
||||||
%token T_INLINE_HTML
|
|
||||||
%token T_ENCAPSED_AND_WHITESPACE
|
|
||||||
%token T_CONSTANT_ENCAPSED_STRING
|
|
||||||
%token T_ECHO
|
|
||||||
%token T_DO
|
|
||||||
%token T_WHILE
|
|
||||||
%token T_ENDWHILE
|
|
||||||
%token T_FOR
|
|
||||||
%token T_ENDFOR
|
|
||||||
%token T_FOREACH
|
|
||||||
%token T_ENDFOREACH
|
|
||||||
%token T_DECLARE
|
|
||||||
%token T_ENDDECLARE
|
|
||||||
%token T_AS
|
|
||||||
%token T_SWITCH
|
|
||||||
%token T_MATCH
|
|
||||||
%token T_ENDSWITCH
|
|
||||||
%token T_CASE
|
|
||||||
%token T_DEFAULT
|
|
||||||
%token T_BREAK
|
|
||||||
%token T_CONTINUE
|
|
||||||
%token T_GOTO
|
|
||||||
%token T_FUNCTION
|
|
||||||
%token T_FN
|
|
||||||
%token T_CONST
|
|
||||||
%token T_RETURN
|
|
||||||
%token T_TRY
|
|
||||||
%token T_CATCH
|
|
||||||
%token T_FINALLY
|
|
||||||
%token T_THROW
|
|
||||||
%token T_USE
|
|
||||||
%token T_INSTEADOF
|
|
||||||
%token T_GLOBAL
|
|
||||||
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
|
|
||||||
%token T_VAR
|
|
||||||
%token T_UNSET
|
|
||||||
%token T_ISSET
|
|
||||||
%token T_EMPTY
|
|
||||||
%token T_HALT_COMPILER
|
|
||||||
%token T_CLASS
|
|
||||||
%token T_TRAIT
|
|
||||||
%token T_INTERFACE
|
|
||||||
%token T_EXTENDS
|
|
||||||
%token T_IMPLEMENTS
|
|
||||||
%token T_OBJECT_OPERATOR
|
|
||||||
%token T_NULLSAFE_OBJECT_OPERATOR
|
|
||||||
%token T_DOUBLE_ARROW
|
|
||||||
%token T_LIST
|
|
||||||
%token T_ARRAY
|
|
||||||
%token T_CALLABLE
|
|
||||||
%token T_CLASS_C
|
|
||||||
%token T_TRAIT_C
|
|
||||||
%token T_METHOD_C
|
|
||||||
%token T_FUNC_C
|
|
||||||
%token T_LINE
|
|
||||||
%token T_FILE
|
|
||||||
%token T_START_HEREDOC
|
|
||||||
%token T_END_HEREDOC
|
|
||||||
%token T_DOLLAR_OPEN_CURLY_BRACES
|
|
||||||
%token T_CURLY_OPEN
|
|
||||||
%token T_PAAMAYIM_NEKUDOTAYIM
|
|
||||||
%token T_NAMESPACE
|
|
||||||
%token T_NS_C
|
|
||||||
%token T_DIR
|
|
||||||
%token T_NS_SEPARATOR
|
|
||||||
%token T_ELLIPSIS
|
|
||||||
%token T_NAME_FULLY_QUALIFIED
|
|
||||||
%token T_NAME_QUALIFIED
|
|
||||||
%token T_NAME_RELATIVE
|
|
||||||
%token T_ATTRIBUTE
|
|
@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
interface Builder
|
interface Builder {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Returns the built node.
|
* Returns the built node.
|
||||||
*
|
*
|
||||||
* @return Node The built node
|
* @return Node The built node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node;
|
public function getNode(): Node;
|
||||||
}
|
}
|
||||||
|
135
lib/PhpParser/Builder/ClassConst.php
Normal file
135
lib/PhpParser/Builder/ClassConst.php
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Builder;
|
||||||
|
|
||||||
|
use PhpParser;
|
||||||
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Modifiers;
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Const_;
|
||||||
|
use PhpParser\Node\Identifier;
|
||||||
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
|
class ClassConst implements PhpParser\Builder {
|
||||||
|
/** @var int */
|
||||||
|
protected $flags = 0;
|
||||||
|
/** @var array<string, mixed> */
|
||||||
|
protected $attributes = [];
|
||||||
|
/** @var list<Const_> */
|
||||||
|
protected $constants = [];
|
||||||
|
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a class constant builder
|
||||||
|
*
|
||||||
|
* @param string|Identifier $name Name
|
||||||
|
* @param Node\Expr|bool|null|int|float|string|array $value Value
|
||||||
|
*/
|
||||||
|
public function __construct($name, $value) {
|
||||||
|
$this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add another constant to const group
|
||||||
|
*
|
||||||
|
* @param string|Identifier $name Name
|
||||||
|
* @param Node\Expr|bool|null|int|float|string|array $value Value
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addConst($name, $value) {
|
||||||
|
$this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value));
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes the constant public.
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function makePublic() {
|
||||||
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes the constant protected.
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function makeProtected() {
|
||||||
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes the constant private.
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function makePrivate() {
|
||||||
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes the constant final.
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function makeFinal() {
|
||||||
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets doc comment for the constant.
|
||||||
|
*
|
||||||
|
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function setDocComment($docComment) {
|
||||||
|
$this->attributes = [
|
||||||
|
'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the built class node.
|
||||||
|
*
|
||||||
|
* @return Stmt\ClassConst The built constant node
|
||||||
|
*/
|
||||||
|
public function getNode(): PhpParser\Node {
|
||||||
|
return new Stmt\ClassConst(
|
||||||
|
$this->constants,
|
||||||
|
$this->flags,
|
||||||
|
$this->attributes,
|
||||||
|
$this->attributeGroups
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -4,21 +4,32 @@ namespace PhpParser\Builder;
|
|||||||
|
|
||||||
use PhpParser;
|
use PhpParser;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Modifiers;
|
||||||
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Name;
|
use PhpParser\Node\Name;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Class_ extends Declaration
|
class Class_ extends Declaration {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
|
/** @var Name|null */
|
||||||
protected $extends = null;
|
protected $extends = null;
|
||||||
|
/** @var list<Name> */
|
||||||
protected $implements = [];
|
protected $implements = [];
|
||||||
|
/** @var int */
|
||||||
protected $flags = 0;
|
protected $flags = 0;
|
||||||
|
|
||||||
|
/** @var list<Stmt\TraitUse> */
|
||||||
protected $uses = [];
|
protected $uses = [];
|
||||||
|
/** @var list<Stmt\ClassConst> */
|
||||||
protected $constants = [];
|
protected $constants = [];
|
||||||
|
/** @var list<Stmt\Property> */
|
||||||
protected $properties = [];
|
protected $properties = [];
|
||||||
|
/** @var list<Stmt\ClassMethod> */
|
||||||
protected $methods = [];
|
protected $methods = [];
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a class builder.
|
* Creates a class builder.
|
||||||
@ -63,7 +74,7 @@ class Class_ extends Declaration
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeAbstract() {
|
public function makeAbstract() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
|
$this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::ABSTRACT);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -74,7 +85,18 @@ class Class_ extends Declaration
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeFinal() {
|
public function makeFinal() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
|
$this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::FINAL);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes the class readonly.
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function makeReadonly() {
|
||||||
|
$this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::READONLY);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -89,19 +111,30 @@ class Class_ extends Declaration
|
|||||||
public function addStmt($stmt) {
|
public function addStmt($stmt) {
|
||||||
$stmt = BuilderHelpers::normalizeNode($stmt);
|
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||||
|
|
||||||
$targets = [
|
if ($stmt instanceof Stmt\Property) {
|
||||||
Stmt\TraitUse::class => &$this->uses,
|
$this->properties[] = $stmt;
|
||||||
Stmt\ClassConst::class => &$this->constants,
|
} elseif ($stmt instanceof Stmt\ClassMethod) {
|
||||||
Stmt\Property::class => &$this->properties,
|
$this->methods[] = $stmt;
|
||||||
Stmt\ClassMethod::class => &$this->methods,
|
} elseif ($stmt instanceof Stmt\TraitUse) {
|
||||||
];
|
$this->uses[] = $stmt;
|
||||||
|
} elseif ($stmt instanceof Stmt\ClassConst) {
|
||||||
$class = \get_class($stmt);
|
$this->constants[] = $stmt;
|
||||||
if (!isset($targets[$class])) {
|
} else {
|
||||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
$targets[$class][] = $stmt;
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -111,12 +144,13 @@ class Class_ extends Declaration
|
|||||||
*
|
*
|
||||||
* @return Stmt\Class_ The built class node
|
* @return Stmt\Class_ The built class node
|
||||||
*/
|
*/
|
||||||
public function getNode() : PhpParser\Node {
|
public function getNode(): PhpParser\Node {
|
||||||
return new Stmt\Class_($this->name, [
|
return new Stmt\Class_($this->name, [
|
||||||
'flags' => $this->flags,
|
'flags' => $this->flags,
|
||||||
'extends' => $this->extends,
|
'extends' => $this->extends,
|
||||||
'implements' => $this->implements,
|
'implements' => $this->implements,
|
||||||
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
|
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
|
||||||
|
'attrGroups' => $this->attributeGroups,
|
||||||
], $this->attributes);
|
], $this->attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,23 @@ namespace PhpParser\Builder;
|
|||||||
use PhpParser;
|
use PhpParser;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
|
||||||
abstract class Declaration implements PhpParser\Builder
|
abstract class Declaration implements PhpParser\Builder {
|
||||||
{
|
/** @var array<string, mixed> */
|
||||||
protected $attributes = [];
|
protected $attributes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a statement.
|
||||||
|
*
|
||||||
|
* @param PhpParser\Node\Stmt|PhpParser\Builder $stmt The statement to add
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
abstract public function addStmt($stmt);
|
abstract public function addStmt($stmt);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds multiple statements.
|
* Adds multiple statements.
|
||||||
*
|
*
|
||||||
* @param array $stmts The statements to add
|
* @param (PhpParser\Node\Stmt|PhpParser\Builder)[] $stmts The statements to add
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
|
87
lib/PhpParser/Builder/EnumCase.php
Normal file
87
lib/PhpParser/Builder/EnumCase.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Builder;
|
||||||
|
|
||||||
|
use PhpParser;
|
||||||
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Identifier;
|
||||||
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
|
class EnumCase implements PhpParser\Builder {
|
||||||
|
/** @var Identifier|string */
|
||||||
|
protected $name;
|
||||||
|
/** @var ?Node\Expr */
|
||||||
|
protected $value = null;
|
||||||
|
/** @var array<string, mixed> */
|
||||||
|
protected $attributes = [];
|
||||||
|
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an enum case builder.
|
||||||
|
*
|
||||||
|
* @param string|Identifier $name Name
|
||||||
|
*/
|
||||||
|
public function __construct($name) {
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value.
|
||||||
|
*
|
||||||
|
* @param Node\Expr|string|int $value
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setValue($value) {
|
||||||
|
$this->value = BuilderHelpers::normalizeValue($value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets doc comment for the constant.
|
||||||
|
*
|
||||||
|
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function setDocComment($docComment) {
|
||||||
|
$this->attributes = [
|
||||||
|
'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the built enum case node.
|
||||||
|
*
|
||||||
|
* @return Stmt\EnumCase The built constant node
|
||||||
|
*/
|
||||||
|
public function getNode(): PhpParser\Node {
|
||||||
|
return new Stmt\EnumCase(
|
||||||
|
$this->name,
|
||||||
|
$this->value,
|
||||||
|
$this->attributeGroups,
|
||||||
|
$this->attributes
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
118
lib/PhpParser/Builder/Enum_.php
Normal file
118
lib/PhpParser/Builder/Enum_.php
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Builder;
|
||||||
|
|
||||||
|
use PhpParser;
|
||||||
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Identifier;
|
||||||
|
use PhpParser\Node\Name;
|
||||||
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
|
class Enum_ extends Declaration {
|
||||||
|
/** @var string */
|
||||||
|
protected $name;
|
||||||
|
/** @var Identifier|null */
|
||||||
|
protected $scalarType = null;
|
||||||
|
/** @var list<Name> */
|
||||||
|
protected $implements = [];
|
||||||
|
/** @var list<Stmt\TraitUse> */
|
||||||
|
protected $uses = [];
|
||||||
|
/** @var list<Stmt\EnumCase> */
|
||||||
|
protected $enumCases = [];
|
||||||
|
/** @var list<Stmt\ClassConst> */
|
||||||
|
protected $constants = [];
|
||||||
|
/** @var list<Stmt\ClassMethod> */
|
||||||
|
protected $methods = [];
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an enum builder.
|
||||||
|
*
|
||||||
|
* @param string $name Name of the enum
|
||||||
|
*/
|
||||||
|
public function __construct(string $name) {
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the scalar type.
|
||||||
|
*
|
||||||
|
* @param string|Identifier $scalarType
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setScalarType($scalarType) {
|
||||||
|
$this->scalarType = BuilderHelpers::normalizeType($scalarType);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements one or more interfaces.
|
||||||
|
*
|
||||||
|
* @param Name|string ...$interfaces Names of interfaces to implement
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function implement(...$interfaces) {
|
||||||
|
foreach ($interfaces as $interface) {
|
||||||
|
$this->implements[] = BuilderHelpers::normalizeName($interface);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a statement.
|
||||||
|
*
|
||||||
|
* @param Stmt|PhpParser\Builder $stmt The statement to add
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addStmt($stmt) {
|
||||||
|
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||||
|
|
||||||
|
if ($stmt instanceof Stmt\EnumCase) {
|
||||||
|
$this->enumCases[] = $stmt;
|
||||||
|
} elseif ($stmt instanceof Stmt\ClassMethod) {
|
||||||
|
$this->methods[] = $stmt;
|
||||||
|
} elseif ($stmt instanceof Stmt\TraitUse) {
|
||||||
|
$this->uses[] = $stmt;
|
||||||
|
} elseif ($stmt instanceof Stmt\ClassConst) {
|
||||||
|
$this->constants[] = $stmt;
|
||||||
|
} else {
|
||||||
|
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the built class node.
|
||||||
|
*
|
||||||
|
* @return Stmt\Enum_ The built enum node
|
||||||
|
*/
|
||||||
|
public function getNode(): PhpParser\Node {
|
||||||
|
return new Stmt\Enum_($this->name, [
|
||||||
|
'scalarType' => $this->scalarType,
|
||||||
|
'implements' => $this->implements,
|
||||||
|
'stmts' => array_merge($this->uses, $this->enumCases, $this->constants, $this->methods),
|
||||||
|
'attrGroups' => $this->attributeGroups,
|
||||||
|
], $this->attributes);
|
||||||
|
}
|
||||||
|
}
|
@ -5,12 +5,13 @@ namespace PhpParser\Builder;
|
|||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
|
|
||||||
abstract class FunctionLike extends Declaration
|
abstract class FunctionLike extends Declaration {
|
||||||
{
|
/** @var bool */
|
||||||
protected $returnByRef = false;
|
protected $returnByRef = false;
|
||||||
|
/** @var Node\Param[] */
|
||||||
protected $params = [];
|
protected $params = [];
|
||||||
|
|
||||||
/** @var string|Node\Name|Node\NullableType|null */
|
/** @var Node\Identifier|Node\Name|Node\ComplexType|null */
|
||||||
protected $returnType = null;
|
protected $returnType = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,7 +47,7 @@ abstract class FunctionLike extends Declaration
|
|||||||
/**
|
/**
|
||||||
* Adds multiple parameters.
|
* Adds multiple parameters.
|
||||||
*
|
*
|
||||||
* @param array $params The parameters to add
|
* @param (Node\Param|Param)[] $params The parameters to add
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
@ -61,8 +62,7 @@ abstract class FunctionLike extends Declaration
|
|||||||
/**
|
/**
|
||||||
* Sets the return type for PHP 7.
|
* Sets the return type for PHP 7.
|
||||||
*
|
*
|
||||||
* @param string|Node\Name|Node\NullableType $type One of array, callable, string, int, float,
|
* @param string|Node\Name|Node\Identifier|Node\ComplexType $type
|
||||||
* bool, iterable, or a class/interface name.
|
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
|
@ -7,11 +7,15 @@ use PhpParser\BuilderHelpers;
|
|||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Function_ extends FunctionLike
|
class Function_ extends FunctionLike {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var list<Stmt> */
|
||||||
protected $stmts = [];
|
protected $stmts = [];
|
||||||
|
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function builder.
|
* Creates a function builder.
|
||||||
*
|
*
|
||||||
@ -34,17 +38,31 @@ class Function_ extends FunctionLike
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the built function node.
|
* Returns the built function node.
|
||||||
*
|
*
|
||||||
* @return Stmt\Function_ The built function node
|
* @return Stmt\Function_ The built function node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
return new Stmt\Function_($this->name, [
|
return new Stmt\Function_($this->name, [
|
||||||
'byRef' => $this->returnByRef,
|
'byRef' => $this->returnByRef,
|
||||||
'params' => $this->params,
|
'params' => $this->params,
|
||||||
'returnType' => $this->returnType,
|
'returnType' => $this->returnType,
|
||||||
'stmts' => $this->stmts,
|
'stmts' => $this->stmts,
|
||||||
|
'attrGroups' => $this->attributeGroups,
|
||||||
], $this->attributes);
|
], $this->attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,15 +4,21 @@ namespace PhpParser\Builder;
|
|||||||
|
|
||||||
use PhpParser;
|
use PhpParser;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Name;
|
use PhpParser\Node\Name;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Interface_ extends Declaration
|
class Interface_ extends Declaration {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var list<Name> */
|
||||||
protected $extends = [];
|
protected $extends = [];
|
||||||
|
/** @var list<Stmt\ClassConst> */
|
||||||
protected $constants = [];
|
protected $constants = [];
|
||||||
|
/** @var list<Stmt\ClassMethod> */
|
||||||
protected $methods = [];
|
protected $methods = [];
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an interface builder.
|
* Creates an interface builder.
|
||||||
@ -61,15 +67,29 @@ class Interface_ extends Declaration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the built interface node.
|
* Returns the built interface node.
|
||||||
*
|
*
|
||||||
* @return Stmt\Interface_ The built interface node
|
* @return Stmt\Interface_ The built interface node
|
||||||
*/
|
*/
|
||||||
public function getNode() : PhpParser\Node {
|
public function getNode(): PhpParser\Node {
|
||||||
return new Stmt\Interface_($this->name, [
|
return new Stmt\Interface_($this->name, [
|
||||||
'extends' => $this->extends,
|
'extends' => $this->extends,
|
||||||
'stmts' => array_merge($this->constants, $this->methods),
|
'stmts' => array_merge($this->constants, $this->methods),
|
||||||
|
'attrGroups' => $this->attributeGroups,
|
||||||
], $this->attributes);
|
], $this->attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,22 @@ namespace PhpParser\Builder;
|
|||||||
|
|
||||||
use PhpParser;
|
use PhpParser;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Modifiers;
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Method extends FunctionLike
|
class Method extends FunctionLike {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var int */
|
||||||
protected $flags = 0;
|
protected $flags = 0;
|
||||||
|
|
||||||
/** @var array|null */
|
/** @var list<Stmt>|null */
|
||||||
protected $stmts = [];
|
protected $stmts = [];
|
||||||
|
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a method builder.
|
* Creates a method builder.
|
||||||
*
|
*
|
||||||
@ -30,7 +35,7 @@ class Method extends FunctionLike
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makePublic() {
|
public function makePublic() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -41,7 +46,7 @@ class Method extends FunctionLike
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeProtected() {
|
public function makeProtected() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -52,7 +57,7 @@ class Method extends FunctionLike
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makePrivate() {
|
public function makePrivate() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -63,7 +68,7 @@ class Method extends FunctionLike
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeStatic() {
|
public function makeStatic() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -78,7 +83,7 @@ class Method extends FunctionLike
|
|||||||
throw new \LogicException('Cannot make method with statements abstract');
|
throw new \LogicException('Cannot make method with statements abstract');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::ABSTRACT);
|
||||||
$this->stmts = null; // abstract methods don't have statements
|
$this->stmts = null; // abstract methods don't have statements
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -90,7 +95,7 @@ class Method extends FunctionLike
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeFinal() {
|
public function makeFinal() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -112,18 +117,32 @@ class Method extends FunctionLike
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the built method node.
|
* Returns the built method node.
|
||||||
*
|
*
|
||||||
* @return Stmt\ClassMethod The built method node
|
* @return Stmt\ClassMethod The built method node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
return new Stmt\ClassMethod($this->name, [
|
return new Stmt\ClassMethod($this->name, [
|
||||||
'flags' => $this->flags,
|
'flags' => $this->flags,
|
||||||
'byRef' => $this->returnByRef,
|
'byRef' => $this->returnByRef,
|
||||||
'params' => $this->params,
|
'params' => $this->params,
|
||||||
'returnType' => $this->returnType,
|
'returnType' => $this->returnType,
|
||||||
'stmts' => $this->stmts,
|
'stmts' => $this->stmts,
|
||||||
|
'attrGroups' => $this->attributeGroups,
|
||||||
], $this->attributes);
|
], $this->attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ use PhpParser\BuilderHelpers;
|
|||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Namespace_ extends Declaration
|
class Namespace_ extends Declaration {
|
||||||
{
|
/** @var Node\Name|null */
|
||||||
private $name;
|
private $name;
|
||||||
|
/** @var Stmt[] */
|
||||||
private $stmts = [];
|
private $stmts = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,9 +38,9 @@ class Namespace_ extends Declaration
|
|||||||
/**
|
/**
|
||||||
* Returns the built node.
|
* Returns the built node.
|
||||||
*
|
*
|
||||||
* @return Node The built node
|
* @return Stmt\Namespace_ The built node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
return new Stmt\Namespace_($this->name, $this->stmts, $this->attributes);
|
return new Stmt\Namespace_($this->name, $this->stmts, $this->attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,19 @@ use PhpParser;
|
|||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
|
|
||||||
class Param implements PhpParser\Builder
|
class Param implements PhpParser\Builder {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var Node\Expr|null */
|
||||||
protected $default = null;
|
protected $default = null;
|
||||||
|
/** @var Node\Identifier|Node\Name|Node\ComplexType|null */
|
||||||
/** @var Node\Identifier|Node\Name|Node\NullableType|null */
|
|
||||||
protected $type = null;
|
protected $type = null;
|
||||||
|
/** @var bool */
|
||||||
protected $byRef = false;
|
protected $byRef = false;
|
||||||
|
/** @var bool */
|
||||||
protected $variadic = false;
|
protected $variadic = false;
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a parameter builder.
|
* Creates a parameter builder.
|
||||||
@ -44,7 +45,7 @@ class Param implements PhpParser\Builder
|
|||||||
/**
|
/**
|
||||||
* Sets type for the parameter.
|
* Sets type for the parameter.
|
||||||
*
|
*
|
||||||
* @param string|Node\Name|Node\NullableType|Node\UnionType $type Parameter type
|
* @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
@ -57,19 +58,6 @@ class Param implements PhpParser\Builder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets type for the parameter.
|
|
||||||
*
|
|
||||||
* @param string|Node\Name|Node\NullableType|Node\UnionType $type Parameter type
|
|
||||||
*
|
|
||||||
* @return $this The builder instance (for fluid interface)
|
|
||||||
*
|
|
||||||
* @deprecated Use setType() instead
|
|
||||||
*/
|
|
||||||
public function setTypeHint($type) {
|
|
||||||
return $this->setType($type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the parameter accept the value by reference.
|
* Make the parameter accept the value by reference.
|
||||||
*
|
*
|
||||||
@ -92,15 +80,28 @@ class Param implements PhpParser\Builder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the built parameter node.
|
* Returns the built parameter node.
|
||||||
*
|
*
|
||||||
* @return Node\Param The built parameter node
|
* @return Node\Param The built parameter node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
return new Node\Param(
|
return new Node\Param(
|
||||||
new Node\Expr\Variable($this->name),
|
new Node\Expr\Variable($this->name),
|
||||||
$this->default, $this->type, $this->byRef, $this->variadic
|
$this->default, $this->type, $this->byRef, $this->variadic, [], 0, $this->attributeGroups
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,21 +4,26 @@ namespace PhpParser\Builder;
|
|||||||
|
|
||||||
use PhpParser;
|
use PhpParser;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Modifiers;
|
||||||
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Identifier;
|
use PhpParser\Node\Identifier;
|
||||||
use PhpParser\Node\Name;
|
use PhpParser\Node\Name;
|
||||||
use PhpParser\Node\NullableType;
|
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
use PhpParser\Node\ComplexType;
|
||||||
|
|
||||||
class Property implements PhpParser\Builder
|
class Property implements PhpParser\Builder {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var int */
|
||||||
protected $flags = 0;
|
protected $flags = 0;
|
||||||
|
/** @var Node\Expr|null */
|
||||||
protected $default = null;
|
protected $default = null;
|
||||||
|
/** @var array<string, mixed> */
|
||||||
protected $attributes = [];
|
protected $attributes = [];
|
||||||
|
/** @var null|Identifier|Name|ComplexType */
|
||||||
/** @var null|Identifier|Name|NullableType */
|
|
||||||
protected $type;
|
protected $type;
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a property builder.
|
* Creates a property builder.
|
||||||
@ -35,7 +40,7 @@ class Property implements PhpParser\Builder
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makePublic() {
|
public function makePublic() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -46,7 +51,7 @@ class Property implements PhpParser\Builder
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeProtected() {
|
public function makeProtected() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -57,7 +62,7 @@ class Property implements PhpParser\Builder
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makePrivate() {
|
public function makePrivate() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -68,7 +73,18 @@ class Property implements PhpParser\Builder
|
|||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeStatic() {
|
public function makeStatic() {
|
||||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes the property readonly.
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function makeReadonly() {
|
||||||
|
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -104,7 +120,7 @@ class Property implements PhpParser\Builder
|
|||||||
/**
|
/**
|
||||||
* Sets the property type for PHP 7.4+.
|
* Sets the property type for PHP 7.4+.
|
||||||
*
|
*
|
||||||
* @param string|Name|NullableType|Identifier $type
|
* @param string|Name|Identifier|ComplexType $type
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@ -114,19 +130,33 @@ class Property implements PhpParser\Builder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the built class node.
|
* Returns the built class node.
|
||||||
*
|
*
|
||||||
* @return Stmt\Property The built property node
|
* @return Stmt\Property The built property node
|
||||||
*/
|
*/
|
||||||
public function getNode() : PhpParser\Node {
|
public function getNode(): PhpParser\Node {
|
||||||
return new Stmt\Property(
|
return new Stmt\Property(
|
||||||
$this->flags !== 0 ? $this->flags : Stmt\Class_::MODIFIER_PUBLIC,
|
$this->flags !== 0 ? $this->flags : Modifiers::PUBLIC,
|
||||||
[
|
[
|
||||||
new Stmt\PropertyProperty($this->name, $this->default)
|
new Node\PropertyItem($this->name, $this->default)
|
||||||
],
|
],
|
||||||
$this->attributes,
|
$this->attributes,
|
||||||
$this->type
|
$this->type,
|
||||||
|
$this->attributeGroups
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ use PhpParser\BuilderHelpers;
|
|||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class TraitUse implements Builder
|
class TraitUse implements Builder {
|
||||||
{
|
/** @var Node\Name[] */
|
||||||
protected $traits = [];
|
protected $traits = [];
|
||||||
|
/** @var Stmt\TraitUseAdaptation[] */
|
||||||
protected $adaptations = [];
|
protected $adaptations = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,7 +59,7 @@ class TraitUse implements Builder
|
|||||||
*
|
*
|
||||||
* @return Node The built node
|
* @return Node The built node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
return new Stmt\TraitUse($this->traits, $this->adaptations);
|
return new Stmt\TraitUse($this->traits, $this->adaptations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,43 +4,45 @@ namespace PhpParser\Builder;
|
|||||||
|
|
||||||
use PhpParser\Builder;
|
use PhpParser\Builder;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Modifiers;
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class TraitUseAdaptation implements Builder
|
class TraitUseAdaptation implements Builder {
|
||||||
{
|
private const TYPE_UNDEFINED = 0;
|
||||||
const TYPE_UNDEFINED = 0;
|
private const TYPE_ALIAS = 1;
|
||||||
const TYPE_ALIAS = 1;
|
private const TYPE_PRECEDENCE = 2;
|
||||||
const TYPE_PRECEDENCE = 2;
|
|
||||||
|
|
||||||
/** @var int Type of building adaptation */
|
/** @var int Type of building adaptation */
|
||||||
protected $type;
|
protected $type;
|
||||||
|
/** @var Node\Name|null */
|
||||||
protected $trait;
|
protected $trait;
|
||||||
|
/** @var Node\Identifier */
|
||||||
protected $method;
|
protected $method;
|
||||||
|
/** @var int|null */
|
||||||
protected $modifier = null;
|
protected $modifier = null;
|
||||||
|
/** @var Node\Identifier|null */
|
||||||
protected $alias = null;
|
protected $alias = null;
|
||||||
|
/** @var Node\Name[] */
|
||||||
protected $insteadof = [];
|
protected $insteadof = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a trait use adaptation builder.
|
* Creates a trait use adaptation builder.
|
||||||
*
|
*
|
||||||
* @param Node\Name|string|null $trait Name of adaptated trait
|
* @param Node\Name|string|null $trait Name of adapted trait
|
||||||
* @param Node\Identifier|string $method Name of adaptated method
|
* @param Node\Identifier|string $method Name of adapted method
|
||||||
*/
|
*/
|
||||||
public function __construct($trait, $method) {
|
public function __construct($trait, $method) {
|
||||||
$this->type = self::TYPE_UNDEFINED;
|
$this->type = self::TYPE_UNDEFINED;
|
||||||
|
|
||||||
$this->trait = is_null($trait)? null: BuilderHelpers::normalizeName($trait);
|
$this->trait = is_null($trait) ? null : BuilderHelpers::normalizeName($trait);
|
||||||
$this->method = BuilderHelpers::normalizeIdentifier($method);
|
$this->method = BuilderHelpers::normalizeIdentifier($method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets alias of method.
|
* Sets alias of method.
|
||||||
*
|
*
|
||||||
* @param Node\Identifier|string $alias Alias for adaptated method
|
* @param Node\Identifier|string $alias Alias for adapted method
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
@ -53,37 +55,37 @@ class TraitUseAdaptation implements Builder
|
|||||||
throw new \LogicException('Cannot set alias for not alias adaptation buider');
|
throw new \LogicException('Cannot set alias for not alias adaptation buider');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->alias = $alias;
|
$this->alias = BuilderHelpers::normalizeIdentifier($alias);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets adaptated method public.
|
* Sets adapted method public.
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makePublic() {
|
public function makePublic() {
|
||||||
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
|
$this->setModifier(Modifiers::PUBLIC);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets adaptated method protected.
|
* Sets adapted method protected.
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makeProtected() {
|
public function makeProtected() {
|
||||||
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
|
$this->setModifier(Modifiers::PROTECTED);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets adaptated method private.
|
* Sets adapted method private.
|
||||||
*
|
*
|
||||||
* @return $this The builder instance (for fluid interface)
|
* @return $this The builder instance (for fluid interface)
|
||||||
*/
|
*/
|
||||||
public function makePrivate() {
|
public function makePrivate() {
|
||||||
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
|
$this->setModifier(Modifiers::PRIVATE);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +116,7 @@ class TraitUseAdaptation implements Builder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setModifier(int $modifier) {
|
protected function setModifier(int $modifier): void {
|
||||||
if ($this->type === self::TYPE_UNDEFINED) {
|
if ($this->type === self::TYPE_UNDEFINED) {
|
||||||
$this->type = self::TYPE_ALIAS;
|
$this->type = self::TYPE_ALIAS;
|
||||||
}
|
}
|
||||||
@ -135,7 +137,7 @@ class TraitUseAdaptation implements Builder
|
|||||||
*
|
*
|
||||||
* @return Node The built node
|
* @return Node The built node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
switch ($this->type) {
|
switch ($this->type) {
|
||||||
case self::TYPE_ALIAS:
|
case self::TYPE_ALIAS:
|
||||||
return new Stmt\TraitUseAdaptation\Alias($this->trait, $this->method, $this->modifier, $this->alias);
|
return new Stmt\TraitUseAdaptation\Alias($this->trait, $this->method, $this->modifier, $this->alias);
|
||||||
|
@ -4,14 +4,22 @@ namespace PhpParser\Builder;
|
|||||||
|
|
||||||
use PhpParser;
|
use PhpParser;
|
||||||
use PhpParser\BuilderHelpers;
|
use PhpParser\BuilderHelpers;
|
||||||
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Trait_ extends Declaration
|
class Trait_ extends Declaration {
|
||||||
{
|
/** @var string */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var list<Stmt\TraitUse> */
|
||||||
protected $uses = [];
|
protected $uses = [];
|
||||||
|
/** @var list<Stmt\ClassConst> */
|
||||||
|
protected $constants = [];
|
||||||
|
/** @var list<Stmt\Property> */
|
||||||
protected $properties = [];
|
protected $properties = [];
|
||||||
|
/** @var list<Stmt\ClassMethod> */
|
||||||
protected $methods = [];
|
protected $methods = [];
|
||||||
|
/** @var list<Node\AttributeGroup> */
|
||||||
|
protected $attributeGroups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an interface builder.
|
* Creates an interface builder.
|
||||||
@ -38,6 +46,8 @@ class Trait_ extends Declaration
|
|||||||
$this->methods[] = $stmt;
|
$this->methods[] = $stmt;
|
||||||
} elseif ($stmt instanceof Stmt\TraitUse) {
|
} elseif ($stmt instanceof Stmt\TraitUse) {
|
||||||
$this->uses[] = $stmt;
|
$this->uses[] = $stmt;
|
||||||
|
} elseif ($stmt instanceof Stmt\ClassConst) {
|
||||||
|
$this->constants[] = $stmt;
|
||||||
} else {
|
} else {
|
||||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||||
}
|
}
|
||||||
@ -45,15 +55,29 @@ class Trait_ extends Declaration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an attribute group.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addAttribute($attribute) {
|
||||||
|
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the built trait node.
|
* Returns the built trait node.
|
||||||
*
|
*
|
||||||
* @return Stmt\Trait_ The built interface node
|
* @return Stmt\Trait_ The built interface node
|
||||||
*/
|
*/
|
||||||
public function getNode() : PhpParser\Node {
|
public function getNode(): PhpParser\Node {
|
||||||
return new Stmt\Trait_(
|
return new Stmt\Trait_(
|
||||||
$this->name, [
|
$this->name, [
|
||||||
'stmts' => array_merge($this->uses, $this->properties, $this->methods)
|
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
|
||||||
|
'attrGroups' => $this->attributeGroups,
|
||||||
], $this->attributes
|
], $this->attributes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,12 @@ use PhpParser\BuilderHelpers;
|
|||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class Use_ implements Builder
|
class Use_ implements Builder {
|
||||||
{
|
/** @var Node\Name */
|
||||||
protected $name;
|
protected $name;
|
||||||
|
/** @var int */
|
||||||
protected $type;
|
protected $type;
|
||||||
|
/** @var string|null */
|
||||||
protected $alias = null;
|
protected $alias = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,11 +41,11 @@ class Use_ implements Builder
|
|||||||
/**
|
/**
|
||||||
* Returns the built node.
|
* Returns the built node.
|
||||||
*
|
*
|
||||||
* @return Node The built node
|
* @return Stmt\Use_ The built node
|
||||||
*/
|
*/
|
||||||
public function getNode() : Node {
|
public function getNode(): Node {
|
||||||
return new Stmt\Use_([
|
return new Stmt\Use_([
|
||||||
new Stmt\UseUse($this->name, $this->alias)
|
new Node\UseItem($this->name, $this->alias)
|
||||||
], $this->type);
|
], $this->type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,22 @@ use PhpParser\Node\Name;
|
|||||||
use PhpParser\Node\Scalar\String_;
|
use PhpParser\Node\Scalar\String_;
|
||||||
use PhpParser\Node\Stmt\Use_;
|
use PhpParser\Node\Stmt\Use_;
|
||||||
|
|
||||||
class BuilderFactory
|
class BuilderFactory {
|
||||||
{
|
/**
|
||||||
|
* Creates an attribute node.
|
||||||
|
*
|
||||||
|
* @param string|Name $name Name of the attribute
|
||||||
|
* @param array $args Attribute named arguments
|
||||||
|
*
|
||||||
|
* @return Node\Attribute
|
||||||
|
*/
|
||||||
|
public function attribute($name, array $args = []): Node\Attribute {
|
||||||
|
return new Node\Attribute(
|
||||||
|
BuilderHelpers::normalizeName($name),
|
||||||
|
$this->args($args)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a namespace builder.
|
* Creates a namespace builder.
|
||||||
*
|
*
|
||||||
@ -19,7 +33,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Namespace_ The created namespace builder
|
* @return Builder\Namespace_ The created namespace builder
|
||||||
*/
|
*/
|
||||||
public function namespace($name) : Builder\Namespace_ {
|
public function namespace($name): Builder\Namespace_ {
|
||||||
return new Builder\Namespace_($name);
|
return new Builder\Namespace_($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +44,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Class_ The created class builder
|
* @return Builder\Class_ The created class builder
|
||||||
*/
|
*/
|
||||||
public function class(string $name) : Builder\Class_ {
|
public function class(string $name): Builder\Class_ {
|
||||||
return new Builder\Class_($name);
|
return new Builder\Class_($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +55,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Interface_ The created interface builder
|
* @return Builder\Interface_ The created interface builder
|
||||||
*/
|
*/
|
||||||
public function interface(string $name) : Builder\Interface_ {
|
public function interface(string $name): Builder\Interface_ {
|
||||||
return new Builder\Interface_($name);
|
return new Builder\Interface_($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,18 +66,29 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Trait_ The created trait builder
|
* @return Builder\Trait_ The created trait builder
|
||||||
*/
|
*/
|
||||||
public function trait(string $name) : Builder\Trait_ {
|
public function trait(string $name): Builder\Trait_ {
|
||||||
return new Builder\Trait_($name);
|
return new Builder\Trait_($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an enum builder.
|
||||||
|
*
|
||||||
|
* @param string $name Name of the enum
|
||||||
|
*
|
||||||
|
* @return Builder\Enum_ The created enum builder
|
||||||
|
*/
|
||||||
|
public function enum(string $name): Builder\Enum_ {
|
||||||
|
return new Builder\Enum_($name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a trait use builder.
|
* Creates a trait use builder.
|
||||||
*
|
*
|
||||||
* @param Node\Name|string ...$traits Trait names
|
* @param Node\Name|string ...$traits Trait names
|
||||||
*
|
*
|
||||||
* @return Builder\TraitUse The create trait use builder
|
* @return Builder\TraitUse The created trait use builder
|
||||||
*/
|
*/
|
||||||
public function useTrait(...$traits) : Builder\TraitUse {
|
public function useTrait(...$traits): Builder\TraitUse {
|
||||||
return new Builder\TraitUse(...$traits);
|
return new Builder\TraitUse(...$traits);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,9 +98,9 @@ class BuilderFactory
|
|||||||
* @param Node\Name|string|null $trait Trait name
|
* @param Node\Name|string|null $trait Trait name
|
||||||
* @param Node\Identifier|string $method Method name
|
* @param Node\Identifier|string $method Method name
|
||||||
*
|
*
|
||||||
* @return Builder\TraitUseAdaptation The create trait use adaptation builder
|
* @return Builder\TraitUseAdaptation The created trait use adaptation builder
|
||||||
*/
|
*/
|
||||||
public function traitUseAdaptation($trait, $method = null) : Builder\TraitUseAdaptation {
|
public function traitUseAdaptation($trait, $method = null): Builder\TraitUseAdaptation {
|
||||||
if ($method === null) {
|
if ($method === null) {
|
||||||
$method = $trait;
|
$method = $trait;
|
||||||
$trait = null;
|
$trait = null;
|
||||||
@ -91,7 +116,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Method The created method builder
|
* @return Builder\Method The created method builder
|
||||||
*/
|
*/
|
||||||
public function method(string $name) : Builder\Method {
|
public function method(string $name): Builder\Method {
|
||||||
return new Builder\Method($name);
|
return new Builder\Method($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +127,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Param The created parameter builder
|
* @return Builder\Param The created parameter builder
|
||||||
*/
|
*/
|
||||||
public function param(string $name) : Builder\Param {
|
public function param(string $name): Builder\Param {
|
||||||
return new Builder\Param($name);
|
return new Builder\Param($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +138,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Property The created property builder
|
* @return Builder\Property The created property builder
|
||||||
*/
|
*/
|
||||||
public function property(string $name) : Builder\Property {
|
public function property(string $name): Builder\Property {
|
||||||
return new Builder\Property($name);
|
return new Builder\Property($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +149,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Function_ The created function builder
|
* @return Builder\Function_ The created function builder
|
||||||
*/
|
*/
|
||||||
public function function(string $name) : Builder\Function_ {
|
public function function(string $name): Builder\Function_ {
|
||||||
return new Builder\Function_($name);
|
return new Builder\Function_($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +160,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Use_ The created use builder
|
* @return Builder\Use_ The created use builder
|
||||||
*/
|
*/
|
||||||
public function use($name) : Builder\Use_ {
|
public function use($name): Builder\Use_ {
|
||||||
return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +171,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Use_ The created use function builder
|
* @return Builder\Use_ The created use function builder
|
||||||
*/
|
*/
|
||||||
public function useFunction($name) : Builder\Use_ {
|
public function useFunction($name): Builder\Use_ {
|
||||||
return new Builder\Use_($name, Use_::TYPE_FUNCTION);
|
return new Builder\Use_($name, Use_::TYPE_FUNCTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,10 +182,33 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Builder\Use_ The created use const builder
|
* @return Builder\Use_ The created use const builder
|
||||||
*/
|
*/
|
||||||
public function useConst($name) : Builder\Use_ {
|
public function useConst($name): Builder\Use_ {
|
||||||
return new Builder\Use_($name, Use_::TYPE_CONSTANT);
|
return new Builder\Use_($name, Use_::TYPE_CONSTANT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a class constant builder.
|
||||||
|
*
|
||||||
|
* @param string|Identifier $name Name
|
||||||
|
* @param Node\Expr|bool|null|int|float|string|array $value Value
|
||||||
|
*
|
||||||
|
* @return Builder\ClassConst The created use const builder
|
||||||
|
*/
|
||||||
|
public function classConst($name, $value): Builder\ClassConst {
|
||||||
|
return new Builder\ClassConst($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an enum case builder.
|
||||||
|
*
|
||||||
|
* @param string|Identifier $name Name
|
||||||
|
*
|
||||||
|
* @return Builder\EnumCase The created use const builder
|
||||||
|
*/
|
||||||
|
public function enumCase($name): Builder\EnumCase {
|
||||||
|
return new Builder\EnumCase($name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates node a for a literal value.
|
* Creates node a for a literal value.
|
||||||
*
|
*
|
||||||
@ -168,7 +216,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr
|
* @return Expr
|
||||||
*/
|
*/
|
||||||
public function val($value) : Expr {
|
public function val($value): Expr {
|
||||||
return BuilderHelpers::normalizeValue($value);
|
return BuilderHelpers::normalizeValue($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +227,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\Variable
|
* @return Expr\Variable
|
||||||
*/
|
*/
|
||||||
public function var($name) : Expr\Variable {
|
public function var($name): Expr\Variable {
|
||||||
if (!\is_string($name) && !$name instanceof Expr) {
|
if (!\is_string($name) && !$name instanceof Expr) {
|
||||||
throw new \LogicException('Variable name must be string or Expr');
|
throw new \LogicException('Variable name must be string or Expr');
|
||||||
}
|
}
|
||||||
@ -194,16 +242,18 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @param array $args List of arguments to normalize
|
* @param array $args List of arguments to normalize
|
||||||
*
|
*
|
||||||
* @return Arg[]
|
* @return list<Arg>
|
||||||
*/
|
*/
|
||||||
public function args(array $args) : array {
|
public function args(array $args): array {
|
||||||
$normalizedArgs = [];
|
$normalizedArgs = [];
|
||||||
foreach ($args as $arg) {
|
foreach ($args as $key => $arg) {
|
||||||
if ($arg instanceof Arg) {
|
if (!($arg instanceof Arg)) {
|
||||||
$normalizedArgs[] = $arg;
|
$arg = new Arg(BuilderHelpers::normalizeValue($arg));
|
||||||
} else {
|
|
||||||
$normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
|
|
||||||
}
|
}
|
||||||
|
if (\is_string($key)) {
|
||||||
|
$arg->name = BuilderHelpers::normalizeIdentifier($key);
|
||||||
|
}
|
||||||
|
$normalizedArgs[] = $arg;
|
||||||
}
|
}
|
||||||
return $normalizedArgs;
|
return $normalizedArgs;
|
||||||
}
|
}
|
||||||
@ -216,7 +266,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\FuncCall
|
* @return Expr\FuncCall
|
||||||
*/
|
*/
|
||||||
public function funcCall($name, array $args = []) : Expr\FuncCall {
|
public function funcCall($name, array $args = []): Expr\FuncCall {
|
||||||
return new Expr\FuncCall(
|
return new Expr\FuncCall(
|
||||||
BuilderHelpers::normalizeNameOrExpr($name),
|
BuilderHelpers::normalizeNameOrExpr($name),
|
||||||
$this->args($args)
|
$this->args($args)
|
||||||
@ -232,7 +282,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\MethodCall
|
* @return Expr\MethodCall
|
||||||
*/
|
*/
|
||||||
public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
|
public function methodCall(Expr $var, $name, array $args = []): Expr\MethodCall {
|
||||||
return new Expr\MethodCall(
|
return new Expr\MethodCall(
|
||||||
$var,
|
$var,
|
||||||
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
||||||
@ -249,7 +299,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\StaticCall
|
* @return Expr\StaticCall
|
||||||
*/
|
*/
|
||||||
public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
|
public function staticCall($class, $name, array $args = []): Expr\StaticCall {
|
||||||
return new Expr\StaticCall(
|
return new Expr\StaticCall(
|
||||||
BuilderHelpers::normalizeNameOrExpr($class),
|
BuilderHelpers::normalizeNameOrExpr($class),
|
||||||
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
||||||
@ -265,7 +315,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\New_
|
* @return Expr\New_
|
||||||
*/
|
*/
|
||||||
public function new($class, array $args = []) : Expr\New_ {
|
public function new($class, array $args = []): Expr\New_ {
|
||||||
return new Expr\New_(
|
return new Expr\New_(
|
||||||
BuilderHelpers::normalizeNameOrExpr($class),
|
BuilderHelpers::normalizeNameOrExpr($class),
|
||||||
$this->args($args)
|
$this->args($args)
|
||||||
@ -279,10 +329,10 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\ConstFetch
|
* @return Expr\ConstFetch
|
||||||
*/
|
*/
|
||||||
public function constFetch($name) : Expr\ConstFetch {
|
public function constFetch($name): Expr\ConstFetch {
|
||||||
return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
|
return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a property fetch node.
|
* Creates a property fetch node.
|
||||||
*
|
*
|
||||||
@ -291,22 +341,22 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Expr\PropertyFetch
|
* @return Expr\PropertyFetch
|
||||||
*/
|
*/
|
||||||
public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch {
|
public function propertyFetch(Expr $var, $name): Expr\PropertyFetch {
|
||||||
return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
|
return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a class constant fetch node.
|
* Creates a class constant fetch node.
|
||||||
*
|
*
|
||||||
* @param string|Name|Expr $class Class name
|
* @param string|Name|Expr $class Class name
|
||||||
* @param string|Identifier $name Constant name
|
* @param string|Identifier|Expr $name Constant name
|
||||||
*
|
*
|
||||||
* @return Expr\ClassConstFetch
|
* @return Expr\ClassConstFetch
|
||||||
*/
|
*/
|
||||||
public function classConstFetch($class, $name): Expr\ClassConstFetch {
|
public function classConstFetch($class, $name): Expr\ClassConstFetch {
|
||||||
return new Expr\ClassConstFetch(
|
return new Expr\ClassConstFetch(
|
||||||
BuilderHelpers::normalizeNameOrExpr($class),
|
BuilderHelpers::normalizeNameOrExpr($class),
|
||||||
BuilderHelpers::normalizeIdentifier($name)
|
BuilderHelpers::normalizeIdentifierOrExpr($name)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,7 +367,7 @@ class BuilderFactory
|
|||||||
*
|
*
|
||||||
* @return Concat
|
* @return Concat
|
||||||
*/
|
*/
|
||||||
public function concat(...$exprs) : Concat {
|
public function concat(...$exprs): Concat {
|
||||||
$numExprs = count($exprs);
|
$numExprs = count($exprs);
|
||||||
if ($numExprs < 2) {
|
if ($numExprs < 2) {
|
||||||
throw new \LogicException('Expected at least two expressions');
|
throw new \LogicException('Expected at least two expressions');
|
||||||
@ -334,7 +384,7 @@ class BuilderFactory
|
|||||||
* @param string|Expr $expr
|
* @param string|Expr $expr
|
||||||
* @return Expr
|
* @return Expr
|
||||||
*/
|
*/
|
||||||
private function normalizeStringExpr($expr) : Expr {
|
private function normalizeStringExpr($expr): Expr {
|
||||||
if ($expr instanceof Expr) {
|
if ($expr instanceof Expr) {
|
||||||
return $expr;
|
return $expr;
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,20 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
|
use PhpParser\Node\ComplexType;
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
use PhpParser\Node\Identifier;
|
use PhpParser\Node\Identifier;
|
||||||
use PhpParser\Node\Name;
|
use PhpParser\Node\Name;
|
||||||
use PhpParser\Node\NullableType;
|
use PhpParser\Node\NullableType;
|
||||||
use PhpParser\Node\Scalar;
|
use PhpParser\Node\Scalar;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
use PhpParser\Node\UnionType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class defines helpers used in the implementation of builders. Don't use it directly.
|
* This class defines helpers used in the implementation of builders. Don't use it directly.
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
final class BuilderHelpers
|
final class BuilderHelpers {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Normalizes a node: Converts builder objects to nodes.
|
* Normalizes a node: Converts builder objects to nodes.
|
||||||
*
|
*
|
||||||
@ -24,10 +23,12 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return Node The normalized node
|
* @return Node The normalized node
|
||||||
*/
|
*/
|
||||||
public static function normalizeNode($node) : Node {
|
public static function normalizeNode($node): Node {
|
||||||
if ($node instanceof Builder) {
|
if ($node instanceof Builder) {
|
||||||
return $node->getNode();
|
return $node->getNode();
|
||||||
} elseif ($node instanceof Node) {
|
}
|
||||||
|
|
||||||
|
if ($node instanceof Node) {
|
||||||
return $node;
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return Stmt The normalized statement node
|
* @return Stmt The normalized statement node
|
||||||
*/
|
*/
|
||||||
public static function normalizeStmt($node) : Stmt {
|
public static function normalizeStmt($node): Stmt {
|
||||||
$node = self::normalizeNode($node);
|
$node = self::normalizeNode($node);
|
||||||
if ($node instanceof Stmt) {
|
if ($node instanceof Stmt) {
|
||||||
return $node;
|
return $node;
|
||||||
@ -63,7 +64,7 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return Identifier The normalized identifier
|
* @return Identifier The normalized identifier
|
||||||
*/
|
*/
|
||||||
public static function normalizeIdentifier($name) : Identifier {
|
public static function normalizeIdentifier($name): Identifier {
|
||||||
if ($name instanceof Identifier) {
|
if ($name instanceof Identifier) {
|
||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
@ -101,8 +102,28 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return Name The normalized name
|
* @return Name The normalized name
|
||||||
*/
|
*/
|
||||||
public static function normalizeName($name) : Name {
|
public static function normalizeName($name): Name {
|
||||||
return self::normalizeNameCommon($name, false);
|
if ($name instanceof Name) {
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($name)) {
|
||||||
|
if (!$name) {
|
||||||
|
throw new \LogicException('Name cannot be empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($name[0] === '\\') {
|
||||||
|
return new Name\FullyQualified(substr($name, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 === strpos($name, 'namespace\\')) {
|
||||||
|
return new Name\Relative(substr($name, strlen('namespace\\')));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Name($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \LogicException('Name must be a string or an instance of Node\Name');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,44 +134,17 @@ final class BuilderHelpers
|
|||||||
* @return Name|Expr The normalized name or expression
|
* @return Name|Expr The normalized name or expression
|
||||||
*/
|
*/
|
||||||
public static function normalizeNameOrExpr($name) {
|
public static function normalizeNameOrExpr($name) {
|
||||||
return self::normalizeNameCommon($name, true);
|
if ($name instanceof Expr) {
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalizes a name: Converts string names to Name nodes, optionally allowing expressions.
|
|
||||||
*
|
|
||||||
* @param Expr|Name|string $name The name to normalize
|
|
||||||
* @param bool $allowExpr Whether to also allow expressions
|
|
||||||
*
|
|
||||||
* @return Name|Expr The normalized name, or expression (if allowed)
|
|
||||||
*/
|
|
||||||
private static function normalizeNameCommon($name, bool $allowExpr) {
|
|
||||||
if ($name instanceof Name) {
|
|
||||||
return $name;
|
return $name;
|
||||||
} elseif (is_string($name)) {
|
|
||||||
if (!$name) {
|
|
||||||
throw new \LogicException('Name cannot be empty');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($name[0] === '\\') {
|
|
||||||
return new Name\FullyQualified(substr($name, 1));
|
|
||||||
} elseif (0 === strpos($name, 'namespace\\')) {
|
|
||||||
return new Name\Relative(substr($name, strlen('namespace\\')));
|
|
||||||
} else {
|
|
||||||
return new Name($name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($allowExpr) {
|
if (!is_string($name) && !($name instanceof Name)) {
|
||||||
if ($name instanceof Expr) {
|
|
||||||
return $name;
|
|
||||||
}
|
|
||||||
throw new \LogicException(
|
throw new \LogicException(
|
||||||
'Name must be a string or an instance of Node\Name or Node\Expr'
|
'Name must be a string or an instance of Node\Name or Node\Expr'
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
throw new \LogicException('Name must be a string or an instance of Node\Name');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return self::normalizeName($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -159,18 +153,18 @@ final class BuilderHelpers
|
|||||||
* In particular, builtin types become Identifiers, custom types become Names and nullables
|
* In particular, builtin types become Identifiers, custom types become Names and nullables
|
||||||
* are wrapped in NullableType nodes.
|
* are wrapped in NullableType nodes.
|
||||||
*
|
*
|
||||||
* @param string|Name|Identifier|NullableType|UnionType $type The type to normalize
|
* @param string|Name|Identifier|ComplexType $type The type to normalize
|
||||||
*
|
*
|
||||||
* @return Name|Identifier|NullableType|UnionType The normalized type
|
* @return Name|Identifier|ComplexType The normalized type
|
||||||
*/
|
*/
|
||||||
public static function normalizeType($type) {
|
public static function normalizeType($type) {
|
||||||
if (!is_string($type)) {
|
if (!is_string($type)) {
|
||||||
if (
|
if (
|
||||||
!$type instanceof Name && !$type instanceof Identifier &&
|
!$type instanceof Name && !$type instanceof Identifier &&
|
||||||
!$type instanceof NullableType && !$type instanceof UnionType
|
!$type instanceof ComplexType
|
||||||
) {
|
) {
|
||||||
throw new \LogicException(
|
throw new \LogicException(
|
||||||
'Type must be a string, or an instance of Name, Identifier, NullableType or UnionType'
|
'Type must be a string, or an instance of Name, Identifier or ComplexType'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return $type;
|
return $type;
|
||||||
@ -183,7 +177,20 @@ final class BuilderHelpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
$builtinTypes = [
|
$builtinTypes = [
|
||||||
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed'
|
'array',
|
||||||
|
'callable',
|
||||||
|
'bool',
|
||||||
|
'int',
|
||||||
|
'float',
|
||||||
|
'string',
|
||||||
|
'iterable',
|
||||||
|
'void',
|
||||||
|
'object',
|
||||||
|
'null',
|
||||||
|
'false',
|
||||||
|
'mixed',
|
||||||
|
'never',
|
||||||
|
'true',
|
||||||
];
|
];
|
||||||
|
|
||||||
$lowerType = strtolower($type);
|
$lowerType = strtolower($type);
|
||||||
@ -193,12 +200,11 @@ final class BuilderHelpers
|
|||||||
$type = self::normalizeName($type);
|
$type = self::normalizeName($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($nullable && (string) $type === 'void') {
|
$notNullableTypes = [
|
||||||
throw new \LogicException('void type cannot be nullable');
|
'void', 'mixed', 'never',
|
||||||
}
|
];
|
||||||
|
if ($nullable && in_array((string) $type, $notNullableTypes)) {
|
||||||
if ($nullable && (string) $type === 'mixed') {
|
throw new \LogicException(sprintf('%s type cannot be nullable', $type));
|
||||||
throw new \LogicException('mixed type cannot be nullable');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $nullable ? new NullableType($type) : $type;
|
return $nullable ? new NullableType($type) : $type;
|
||||||
@ -212,35 +218,47 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return Expr The normalized value
|
* @return Expr The normalized value
|
||||||
*/
|
*/
|
||||||
public static function normalizeValue($value) : Expr {
|
public static function normalizeValue($value): Expr {
|
||||||
if ($value instanceof Node\Expr) {
|
if ($value instanceof Node\Expr) {
|
||||||
return $value;
|
return $value;
|
||||||
} elseif (is_null($value)) {
|
}
|
||||||
|
|
||||||
|
if (is_null($value)) {
|
||||||
return new Expr\ConstFetch(
|
return new Expr\ConstFetch(
|
||||||
new Name('null')
|
new Name('null')
|
||||||
);
|
);
|
||||||
} elseif (is_bool($value)) {
|
}
|
||||||
|
|
||||||
|
if (is_bool($value)) {
|
||||||
return new Expr\ConstFetch(
|
return new Expr\ConstFetch(
|
||||||
new Name($value ? 'true' : 'false')
|
new Name($value ? 'true' : 'false')
|
||||||
);
|
);
|
||||||
} elseif (is_int($value)) {
|
}
|
||||||
return new Scalar\LNumber($value);
|
|
||||||
} elseif (is_float($value)) {
|
if (is_int($value)) {
|
||||||
return new Scalar\DNumber($value);
|
return new Scalar\Int_($value);
|
||||||
} elseif (is_string($value)) {
|
}
|
||||||
|
|
||||||
|
if (is_float($value)) {
|
||||||
|
return new Scalar\Float_($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($value)) {
|
||||||
return new Scalar\String_($value);
|
return new Scalar\String_($value);
|
||||||
} elseif (is_array($value)) {
|
}
|
||||||
|
|
||||||
|
if (is_array($value)) {
|
||||||
$items = [];
|
$items = [];
|
||||||
$lastKey = -1;
|
$lastKey = -1;
|
||||||
foreach ($value as $itemKey => $itemValue) {
|
foreach ($value as $itemKey => $itemValue) {
|
||||||
// for consecutive, numeric keys don't generate keys
|
// for consecutive, numeric keys don't generate keys
|
||||||
if (null !== $lastKey && ++$lastKey === $itemKey) {
|
if (null !== $lastKey && ++$lastKey === $itemKey) {
|
||||||
$items[] = new Expr\ArrayItem(
|
$items[] = new Node\ArrayItem(
|
||||||
self::normalizeValue($itemValue)
|
self::normalizeValue($itemValue)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$lastKey = null;
|
$lastKey = null;
|
||||||
$items[] = new Expr\ArrayItem(
|
$items[] = new Node\ArrayItem(
|
||||||
self::normalizeValue($itemValue),
|
self::normalizeValue($itemValue),
|
||||||
self::normalizeValue($itemKey)
|
self::normalizeValue($itemKey)
|
||||||
);
|
);
|
||||||
@ -248,9 +266,9 @@ final class BuilderHelpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Expr\Array_($items);
|
return new Expr\Array_($items);
|
||||||
} else {
|
|
||||||
throw new \LogicException('Invalid value');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new \LogicException('Invalid value');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -260,14 +278,35 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return Comment\Doc The normalized doc comment
|
* @return Comment\Doc The normalized doc comment
|
||||||
*/
|
*/
|
||||||
public static function normalizeDocComment($docComment) : Comment\Doc {
|
public static function normalizeDocComment($docComment): Comment\Doc {
|
||||||
if ($docComment instanceof Comment\Doc) {
|
if ($docComment instanceof Comment\Doc) {
|
||||||
return $docComment;
|
return $docComment;
|
||||||
} elseif (is_string($docComment)) {
|
|
||||||
return new Comment\Doc($docComment);
|
|
||||||
} else {
|
|
||||||
throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_string($docComment)) {
|
||||||
|
return new Comment\Doc($docComment);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalizes a attribute: Converts attribute to the Attribute Group if needed.
|
||||||
|
*
|
||||||
|
* @param Node\Attribute|Node\AttributeGroup $attribute
|
||||||
|
*
|
||||||
|
* @return Node\AttributeGroup The Attribute Group
|
||||||
|
*/
|
||||||
|
public static function normalizeAttribute($attribute): Node\AttributeGroup {
|
||||||
|
if ($attribute instanceof Node\AttributeGroup) {
|
||||||
|
return $attribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!($attribute instanceof Node\Attribute)) {
|
||||||
|
throw new \LogicException('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Node\AttributeGroup([$attribute]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -278,8 +317,17 @@ final class BuilderHelpers
|
|||||||
*
|
*
|
||||||
* @return int New modifiers
|
* @return int New modifiers
|
||||||
*/
|
*/
|
||||||
public static function addModifier(int $modifiers, int $modifier) : int {
|
public static function addModifier(int $modifiers, int $modifier): int {
|
||||||
Stmt\Class_::verifyModifier($modifiers, $modifier);
|
Modifiers::verifyModifier($modifiers, $modifier);
|
||||||
return $modifiers | $modifier;
|
return $modifiers | $modifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a modifier and returns new modifier bitmask.
|
||||||
|
* @return int New modifiers
|
||||||
|
*/
|
||||||
|
public static function addClassModifier(int $existingModifiers, int $modifierToSet): int {
|
||||||
|
Modifiers::verifyClassModifier($existingModifiers, $modifierToSet);
|
||||||
|
return $existingModifiers | $modifierToSet;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,20 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
class Comment implements \JsonSerializable
|
class Comment implements \JsonSerializable {
|
||||||
{
|
/** @var string string */
|
||||||
protected $text;
|
protected $text;
|
||||||
|
/** @var int */
|
||||||
protected $startLine;
|
protected $startLine;
|
||||||
|
/** @var int */
|
||||||
protected $startFilePos;
|
protected $startFilePos;
|
||||||
|
/** @var int */
|
||||||
protected $startTokenPos;
|
protected $startTokenPos;
|
||||||
|
/** @var int */
|
||||||
protected $endLine;
|
protected $endLine;
|
||||||
|
/** @var int */
|
||||||
protected $endFilePos;
|
protected $endFilePos;
|
||||||
|
/** @var int */
|
||||||
protected $endTokenPos;
|
protected $endTokenPos;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,7 +45,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return string The comment text (including comment delimiters like /*)
|
* @return string The comment text (including comment delimiters like /*)
|
||||||
*/
|
*/
|
||||||
public function getText() : string {
|
public function getText(): string {
|
||||||
return $this->text;
|
return $this->text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +54,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int Line number (or -1 if not available)
|
* @return int Line number (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getStartLine() : int {
|
public function getStartLine(): int {
|
||||||
return $this->startLine;
|
return $this->startLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +63,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int File offset (or -1 if not available)
|
* @return int File offset (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getStartFilePos() : int {
|
public function getStartFilePos(): int {
|
||||||
return $this->startFilePos;
|
return $this->startFilePos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +72,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int Token offset (or -1 if not available)
|
* @return int Token offset (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getStartTokenPos() : int {
|
public function getStartTokenPos(): int {
|
||||||
return $this->startTokenPos;
|
return $this->startTokenPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +81,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int Line number (or -1 if not available)
|
* @return int Line number (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getEndLine() : int {
|
public function getEndLine(): int {
|
||||||
return $this->endLine;
|
return $this->endLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +90,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int File offset (or -1 if not available)
|
* @return int File offset (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getEndFilePos() : int {
|
public function getEndFilePos(): int {
|
||||||
return $this->endFilePos;
|
return $this->endFilePos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +99,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int Token offset (or -1 if not available)
|
* @return int Token offset (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getEndTokenPos() : int {
|
public function getEndTokenPos(): int {
|
||||||
return $this->endTokenPos;
|
return $this->endTokenPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +110,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int Line number
|
* @return int Line number
|
||||||
*/
|
*/
|
||||||
public function getLine() : int {
|
public function getLine(): int {
|
||||||
return $this->startLine;
|
return $this->startLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +121,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int File offset
|
* @return int File offset
|
||||||
*/
|
*/
|
||||||
public function getFilePos() : int {
|
public function getFilePos(): int {
|
||||||
return $this->startFilePos;
|
return $this->startFilePos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +132,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return int Token offset
|
* @return int Token offset
|
||||||
*/
|
*/
|
||||||
public function getTokenPos() : int {
|
public function getTokenPos(): int {
|
||||||
return $this->startTokenPos;
|
return $this->startTokenPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +141,7 @@ class Comment implements \JsonSerializable
|
|||||||
*
|
*
|
||||||
* @return string The comment text (including comment delimiters like /*)
|
* @return string The comment text (including comment delimiters like /*)
|
||||||
*/
|
*/
|
||||||
public function __toString() : string {
|
public function __toString(): string {
|
||||||
return $this->text;
|
return $this->text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,12 +156,13 @@ class Comment implements \JsonSerializable
|
|||||||
* @return mixed|string
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getReformattedText() {
|
public function getReformattedText() {
|
||||||
$text = trim($this->text);
|
$text = $this->text;
|
||||||
$newlinePos = strpos($text, "\n");
|
$newlinePos = strpos($text, "\n");
|
||||||
if (false === $newlinePos) {
|
if (false === $newlinePos) {
|
||||||
// Single line comments don't need further processing
|
// Single line comments don't need further processing
|
||||||
return $text;
|
return $text;
|
||||||
} elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
|
}
|
||||||
|
if (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
|
||||||
// Multi line comment of the type
|
// Multi line comment of the type
|
||||||
//
|
//
|
||||||
// /*
|
// /*
|
||||||
@ -165,7 +172,8 @@ class Comment implements \JsonSerializable
|
|||||||
//
|
//
|
||||||
// is handled by replacing the whitespace sequences before the * by a single space
|
// is handled by replacing the whitespace sequences before the * by a single space
|
||||||
return preg_replace('(^\s+\*)m', ' *', $this->text);
|
return preg_replace('(^\s+\*)m', ' *', $this->text);
|
||||||
} elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
|
}
|
||||||
|
if (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
|
||||||
// Multi line comment of the type
|
// Multi line comment of the type
|
||||||
//
|
//
|
||||||
// /*
|
// /*
|
||||||
@ -177,7 +185,8 @@ class Comment implements \JsonSerializable
|
|||||||
// */ on all lines. So if the last line is " */", then " " is removed at the
|
// */ on all lines. So if the last line is " */", then " " is removed at the
|
||||||
// start of all lines.
|
// start of all lines.
|
||||||
return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
|
return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
|
||||||
} elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
|
}
|
||||||
|
if (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
|
||||||
// Multi line comment of the type
|
// Multi line comment of the type
|
||||||
//
|
//
|
||||||
// /* Some text.
|
// /* Some text.
|
||||||
@ -204,9 +213,9 @@ class Comment implements \JsonSerializable
|
|||||||
* @param string $str String to check
|
* @param string $str String to check
|
||||||
* @return int Length in characters. Tabs count as single characters.
|
* @return int Length in characters. Tabs count as single characters.
|
||||||
*/
|
*/
|
||||||
private function getShortestWhitespacePrefixLen(string $str) : int {
|
private function getShortestWhitespacePrefixLen(string $str): int {
|
||||||
$lines = explode("\n", $str);
|
$lines = explode("\n", $str);
|
||||||
$shortestPrefixLen = \INF;
|
$shortestPrefixLen = \PHP_INT_MAX;
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
preg_match('(^\s*)', $line, $matches);
|
preg_match('(^\s*)', $line, $matches);
|
||||||
$prefixLen = strlen($matches[0]);
|
$prefixLen = strlen($matches[0]);
|
||||||
@ -221,7 +230,7 @@ class Comment implements \JsonSerializable
|
|||||||
* @return array
|
* @return array
|
||||||
* @psalm-return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
|
* @psalm-return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
|
||||||
*/
|
*/
|
||||||
public function jsonSerialize() : array {
|
public function jsonSerialize(): array {
|
||||||
// Technically not a node, but we make it look like one anyway
|
// Technically not a node, but we make it look like one anyway
|
||||||
$type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
|
$type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
|
||||||
return [
|
return [
|
||||||
|
@ -2,6 +2,5 @@
|
|||||||
|
|
||||||
namespace PhpParser\Comment;
|
namespace PhpParser\Comment;
|
||||||
|
|
||||||
class Doc extends \PhpParser\Comment
|
class Doc extends \PhpParser\Comment {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
class ConstExprEvaluationException extends \Exception
|
class ConstExprEvaluationException extends \Exception {
|
||||||
{}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
<?php
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
use PhpParser\Node\Scalar;
|
use PhpParser\Node\Scalar;
|
||||||
|
|
||||||
|
use function array_merge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluates constant expressions.
|
* Evaluates constant expressions.
|
||||||
*
|
*
|
||||||
@ -24,8 +26,8 @@ use PhpParser\Node\Scalar;
|
|||||||
* point to string conversions are affected by the precision ini setting. Secondly, they are also
|
* point to string conversions are affected by the precision ini setting. Secondly, they are also
|
||||||
* affected by the LC_NUMERIC locale.
|
* affected by the LC_NUMERIC locale.
|
||||||
*/
|
*/
|
||||||
class ConstExprEvaluator
|
class ConstExprEvaluator {
|
||||||
{
|
/** @var callable|null */
|
||||||
private $fallbackEvaluator;
|
private $fallbackEvaluator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,8 +38,8 @@ class ConstExprEvaluator
|
|||||||
*
|
*
|
||||||
* @param callable|null $fallbackEvaluator To call if subexpression cannot be evaluated
|
* @param callable|null $fallbackEvaluator To call if subexpression cannot be evaluated
|
||||||
*/
|
*/
|
||||||
public function __construct(callable $fallbackEvaluator = null) {
|
public function __construct(?callable $fallbackEvaluator = null) {
|
||||||
$this->fallbackEvaluator = $fallbackEvaluator ?? function(Expr $expr) {
|
$this->fallbackEvaluator = $fallbackEvaluator ?? function (Expr $expr) {
|
||||||
throw new ConstExprEvaluationException(
|
throw new ConstExprEvaluationException(
|
||||||
"Expression of type {$expr->getType()} cannot be evaluated"
|
"Expression of type {$expr->getType()} cannot be evaluated"
|
||||||
);
|
);
|
||||||
@ -62,7 +64,7 @@ class ConstExprEvaluator
|
|||||||
* @throws ConstExprEvaluationException if the expression cannot be evaluated or an error occurred
|
* @throws ConstExprEvaluationException if the expression cannot be evaluated or an error occurred
|
||||||
*/
|
*/
|
||||||
public function evaluateSilently(Expr $expr) {
|
public function evaluateSilently(Expr $expr) {
|
||||||
set_error_handler(function($num, $str, $file, $line) {
|
set_error_handler(function ($num, $str, $file, $line) {
|
||||||
throw new \ErrorException($str, 0, $num, $file, $line);
|
throw new \ErrorException($str, 0, $num, $file, $line);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -100,9 +102,10 @@ class ConstExprEvaluator
|
|||||||
return $this->evaluate($expr);
|
return $this->evaluate($expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return mixed */
|
||||||
private function evaluate(Expr $expr) {
|
private function evaluate(Expr $expr) {
|
||||||
if ($expr instanceof Scalar\LNumber
|
if ($expr instanceof Scalar\Int_
|
||||||
|| $expr instanceof Scalar\DNumber
|
|| $expr instanceof Scalar\Float_
|
||||||
|| $expr instanceof Scalar\String_
|
|| $expr instanceof Scalar\String_
|
||||||
) {
|
) {
|
||||||
return $expr->value;
|
return $expr->value;
|
||||||
@ -145,11 +148,13 @@ class ConstExprEvaluator
|
|||||||
return ($this->fallbackEvaluator)($expr);
|
return ($this->fallbackEvaluator)($expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function evaluateArray(Expr\Array_ $expr) {
|
private function evaluateArray(Expr\Array_ $expr): array {
|
||||||
$array = [];
|
$array = [];
|
||||||
foreach ($expr->items as $item) {
|
foreach ($expr->items as $item) {
|
||||||
if (null !== $item->key) {
|
if (null !== $item->key) {
|
||||||
$array[$this->evaluate($item->key)] = $this->evaluate($item->value);
|
$array[$this->evaluate($item->key)] = $this->evaluate($item->value);
|
||||||
|
} elseif ($item->unpack) {
|
||||||
|
$array = array_merge($array, $this->evaluate($item->value));
|
||||||
} else {
|
} else {
|
||||||
$array[] = $this->evaluate($item->value);
|
$array[] = $this->evaluate($item->value);
|
||||||
}
|
}
|
||||||
@ -157,6 +162,7 @@ class ConstExprEvaluator
|
|||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return mixed */
|
||||||
private function evaluateTernary(Expr\Ternary $expr) {
|
private function evaluateTernary(Expr\Ternary $expr) {
|
||||||
if (null === $expr->if) {
|
if (null === $expr->if) {
|
||||||
return $this->evaluate($expr->cond) ?: $this->evaluate($expr->else);
|
return $this->evaluate($expr->cond) ?: $this->evaluate($expr->else);
|
||||||
@ -167,6 +173,7 @@ class ConstExprEvaluator
|
|||||||
: $this->evaluate($expr->else);
|
: $this->evaluate($expr->else);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return mixed */
|
||||||
private function evaluateBinaryOp(Expr\BinaryOp $expr) {
|
private function evaluateBinaryOp(Expr\BinaryOp $expr) {
|
||||||
if ($expr instanceof Expr\BinaryOp\Coalesce
|
if ($expr instanceof Expr\BinaryOp\Coalesce
|
||||||
&& $expr->left instanceof Expr\ArrayDimFetch
|
&& $expr->left instanceof Expr\ArrayDimFetch
|
||||||
@ -213,6 +220,7 @@ class ConstExprEvaluator
|
|||||||
throw new \Exception('Should not happen');
|
throw new \Exception('Should not happen');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return mixed */
|
||||||
private function evaluateConstFetch(Expr\ConstFetch $expr) {
|
private function evaluateConstFetch(Expr\ConstFetch $expr) {
|
||||||
$name = $expr->name->toLowerString();
|
$name = $expr->name->toLowerString();
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
|
@ -2,25 +2,21 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
class Error extends \RuntimeException
|
class Error extends \RuntimeException {
|
||||||
{
|
/** @var string */
|
||||||
protected $rawMessage;
|
protected $rawMessage;
|
||||||
|
/** @var array<string, mixed> */
|
||||||
protected $attributes;
|
protected $attributes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an Exception signifying a parse error.
|
* Creates an Exception signifying a parse error.
|
||||||
*
|
*
|
||||||
* @param string $message Error message
|
* @param string $message Error message
|
||||||
* @param array|int $attributes Attributes of node/token where error occurred
|
* @param array<string, mixed> $attributes Attributes of node/token where error occurred
|
||||||
* (or start line of error -- deprecated)
|
|
||||||
*/
|
*/
|
||||||
public function __construct(string $message, $attributes = []) {
|
public function __construct(string $message, array $attributes = []) {
|
||||||
$this->rawMessage = $message;
|
$this->rawMessage = $message;
|
||||||
if (is_array($attributes)) {
|
$this->attributes = $attributes;
|
||||||
$this->attributes = $attributes;
|
|
||||||
} else {
|
|
||||||
$this->attributes = ['startLine' => $attributes];
|
|
||||||
}
|
|
||||||
$this->updateMessage();
|
$this->updateMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +25,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @return string Error message
|
* @return string Error message
|
||||||
*/
|
*/
|
||||||
public function getRawMessage() : string {
|
public function getRawMessage(): string {
|
||||||
return $this->rawMessage;
|
return $this->rawMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +34,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @return int Error start line
|
* @return int Error start line
|
||||||
*/
|
*/
|
||||||
public function getStartLine() : int {
|
public function getStartLine(): int {
|
||||||
return $this->attributes['startLine'] ?? -1;
|
return $this->attributes['startLine'] ?? -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,25 +43,25 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @return int Error end line
|
* @return int Error end line
|
||||||
*/
|
*/
|
||||||
public function getEndLine() : int {
|
public function getEndLine(): int {
|
||||||
return $this->attributes['endLine'] ?? -1;
|
return $this->attributes['endLine'] ?? -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the attributes of the node/token the error occurred at.
|
* Gets the attributes of the node/token the error occurred at.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function getAttributes() : array {
|
public function getAttributes(): array {
|
||||||
return $this->attributes;
|
return $this->attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the attributes of the node/token the error occurred at.
|
* Sets the attributes of the node/token the error occurred at.
|
||||||
*
|
*
|
||||||
* @param array $attributes
|
* @param array<string, mixed> $attributes
|
||||||
*/
|
*/
|
||||||
public function setAttributes(array $attributes) {
|
public function setAttributes(array $attributes): void {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
$this->updateMessage();
|
$this->updateMessage();
|
||||||
}
|
}
|
||||||
@ -75,7 +71,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @param string $message Error message
|
* @param string $message Error message
|
||||||
*/
|
*/
|
||||||
public function setRawMessage(string $message) {
|
public function setRawMessage(string $message): void {
|
||||||
$this->rawMessage = $message;
|
$this->rawMessage = $message;
|
||||||
$this->updateMessage();
|
$this->updateMessage();
|
||||||
}
|
}
|
||||||
@ -85,7 +81,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @param int $line Error start line
|
* @param int $line Error start line
|
||||||
*/
|
*/
|
||||||
public function setStartLine(int $line) {
|
public function setStartLine(int $line): void {
|
||||||
$this->attributes['startLine'] = $line;
|
$this->attributes['startLine'] = $line;
|
||||||
$this->updateMessage();
|
$this->updateMessage();
|
||||||
}
|
}
|
||||||
@ -97,7 +93,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasColumnInfo() : bool {
|
public function hasColumnInfo(): bool {
|
||||||
return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
|
return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +103,7 @@ class Error extends \RuntimeException
|
|||||||
* @param string $code Source code of the file
|
* @param string $code Source code of the file
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getStartColumn(string $code) : int {
|
public function getStartColumn(string $code): int {
|
||||||
if (!$this->hasColumnInfo()) {
|
if (!$this->hasColumnInfo()) {
|
||||||
throw new \RuntimeException('Error does not have column information');
|
throw new \RuntimeException('Error does not have column information');
|
||||||
}
|
}
|
||||||
@ -121,7 +117,7 @@ class Error extends \RuntimeException
|
|||||||
* @param string $code Source code of the file
|
* @param string $code Source code of the file
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getEndColumn(string $code) : int {
|
public function getEndColumn(string $code): int {
|
||||||
if (!$this->hasColumnInfo()) {
|
if (!$this->hasColumnInfo()) {
|
||||||
throw new \RuntimeException('Error does not have column information');
|
throw new \RuntimeException('Error does not have column information');
|
||||||
}
|
}
|
||||||
@ -136,7 +132,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @return string Formatted message
|
* @return string Formatted message
|
||||||
*/
|
*/
|
||||||
public function getMessageWithColumnInfo(string $code) : string {
|
public function getMessageWithColumnInfo(string $code): string {
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'%s from %d:%d to %d:%d', $this->getRawMessage(),
|
'%s from %d:%d to %d:%d', $this->getRawMessage(),
|
||||||
$this->getStartLine(), $this->getStartColumn($code),
|
$this->getStartLine(), $this->getStartColumn($code),
|
||||||
@ -152,7 +148,7 @@ class Error extends \RuntimeException
|
|||||||
*
|
*
|
||||||
* @return int 1-based column (relative to start of line)
|
* @return int 1-based column (relative to start of line)
|
||||||
*/
|
*/
|
||||||
private function toColumn(string $code, int $pos) : int {
|
private function toColumn(string $code, int $pos): int {
|
||||||
if ($pos > strlen($code)) {
|
if ($pos > strlen($code)) {
|
||||||
throw new \RuntimeException('Invalid position information');
|
throw new \RuntimeException('Invalid position information');
|
||||||
}
|
}
|
||||||
@ -168,7 +164,7 @@ class Error extends \RuntimeException
|
|||||||
/**
|
/**
|
||||||
* Updates the exception message after a change to rawMessage or rawLine.
|
* Updates the exception message after a change to rawMessage or rawLine.
|
||||||
*/
|
*/
|
||||||
protected function updateMessage() {
|
protected function updateMessage(): void {
|
||||||
$this->message = $this->rawMessage;
|
$this->message = $this->rawMessage;
|
||||||
|
|
||||||
if (-1 === $this->getStartLine()) {
|
if (-1 === $this->getStartLine()) {
|
||||||
|
@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
interface ErrorHandler
|
interface ErrorHandler {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Handle an error generated during lexing, parsing or some other operation.
|
* Handle an error generated during lexing, parsing or some other operation.
|
||||||
*
|
*
|
||||||
* @param Error $error The error that needs to be handled
|
* @param Error $error The error that needs to be handled
|
||||||
*/
|
*/
|
||||||
public function handleError(Error $error);
|
public function handleError(Error $error): void;
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,11 @@ use PhpParser\ErrorHandler;
|
|||||||
*
|
*
|
||||||
* This allows graceful handling of errors.
|
* This allows graceful handling of errors.
|
||||||
*/
|
*/
|
||||||
class Collecting implements ErrorHandler
|
class Collecting implements ErrorHandler {
|
||||||
{
|
|
||||||
/** @var Error[] Collected errors */
|
/** @var Error[] Collected errors */
|
||||||
private $errors = [];
|
private $errors = [];
|
||||||
|
|
||||||
public function handleError(Error $error) {
|
public function handleError(Error $error): void {
|
||||||
$this->errors[] = $error;
|
$this->errors[] = $error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +23,7 @@ class Collecting implements ErrorHandler
|
|||||||
*
|
*
|
||||||
* @return Error[]
|
* @return Error[]
|
||||||
*/
|
*/
|
||||||
public function getErrors() : array {
|
public function getErrors(): array {
|
||||||
return $this->errors;
|
return $this->errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,14 +32,14 @@ class Collecting implements ErrorHandler
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasErrors() : bool {
|
public function hasErrors(): bool {
|
||||||
return !empty($this->errors);
|
return !empty($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset/clear collected errors.
|
* Reset/clear collected errors.
|
||||||
*/
|
*/
|
||||||
public function clearErrors() {
|
public function clearErrors(): void {
|
||||||
$this->errors = [];
|
$this->errors = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,8 @@ use PhpParser\ErrorHandler;
|
|||||||
*
|
*
|
||||||
* This is the default strategy used by all components.
|
* This is the default strategy used by all components.
|
||||||
*/
|
*/
|
||||||
class Throwing implements ErrorHandler
|
class Throwing implements ErrorHandler {
|
||||||
{
|
public function handleError(Error $error): void {
|
||||||
public function handleError(Error $error) {
|
|
||||||
throw $error;
|
throw $error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,11 @@ namespace PhpParser\Internal;
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class DiffElem
|
class DiffElem {
|
||||||
{
|
public const TYPE_KEEP = 0;
|
||||||
const TYPE_KEEP = 0;
|
public const TYPE_REMOVE = 1;
|
||||||
const TYPE_REMOVE = 1;
|
public const TYPE_ADD = 2;
|
||||||
const TYPE_ADD = 2;
|
public const TYPE_REPLACE = 3;
|
||||||
const TYPE_REPLACE = 3;
|
|
||||||
|
|
||||||
/** @var int One of the TYPE_* constants */
|
/** @var int One of the TYPE_* constants */
|
||||||
public $type;
|
public $type;
|
||||||
@ -19,6 +18,11 @@ class DiffElem
|
|||||||
/** @var mixed Is null for remove operations */
|
/** @var mixed Is null for remove operations */
|
||||||
public $new;
|
public $new;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $type One of the TYPE_* constants
|
||||||
|
* @param mixed $old Is null for add operations
|
||||||
|
* @param mixed $new Is null for remove operations
|
||||||
|
*/
|
||||||
public function __construct(int $type, $old, $new) {
|
public function __construct(int $type, $old, $new) {
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
$this->old = $old;
|
$this->old = $old;
|
||||||
|
@ -8,16 +8,17 @@ namespace PhpParser\Internal;
|
|||||||
* Myers, Eugene W. "An O (ND) difference algorithm and its variations."
|
* Myers, Eugene W. "An O (ND) difference algorithm and its variations."
|
||||||
* Algorithmica 1.1 (1986): 251-266.
|
* Algorithmica 1.1 (1986): 251-266.
|
||||||
*
|
*
|
||||||
|
* @template T
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class Differ
|
class Differ {
|
||||||
{
|
/** @var callable(T, T): bool */
|
||||||
private $isEqual;
|
private $isEqual;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create differ over the given equality relation.
|
* Create differ over the given equality relation.
|
||||||
*
|
*
|
||||||
* @param callable $isEqual Equality relation with signature function($a, $b) : bool
|
* @param callable(T, T): bool $isEqual Equality relation
|
||||||
*/
|
*/
|
||||||
public function __construct(callable $isEqual) {
|
public function __construct(callable $isEqual) {
|
||||||
$this->isEqual = $isEqual;
|
$this->isEqual = $isEqual;
|
||||||
@ -26,12 +27,12 @@ class Differ
|
|||||||
/**
|
/**
|
||||||
* Calculate diff (edit script) from $old to $new.
|
* Calculate diff (edit script) from $old to $new.
|
||||||
*
|
*
|
||||||
* @param array $old Original array
|
* @param T[] $old Original array
|
||||||
* @param array $new New array
|
* @param T[] $new New array
|
||||||
*
|
*
|
||||||
* @return DiffElem[] Diff (edit script)
|
* @return DiffElem[] Diff (edit script)
|
||||||
*/
|
*/
|
||||||
public function diff(array $old, array $new) {
|
public function diff(array $old, array $new): array {
|
||||||
list($trace, $x, $y) = $this->calculateTrace($old, $new);
|
list($trace, $x, $y) = $this->calculateTrace($old, $new);
|
||||||
return $this->extractDiff($trace, $x, $y, $old, $new);
|
return $this->extractDiff($trace, $x, $y, $old, $new);
|
||||||
}
|
}
|
||||||
@ -42,18 +43,23 @@ class Differ
|
|||||||
* If a sequence of remove operations is followed by the same number of add operations, these
|
* If a sequence of remove operations is followed by the same number of add operations, these
|
||||||
* will be coalesced into replace operations.
|
* will be coalesced into replace operations.
|
||||||
*
|
*
|
||||||
* @param array $old Original array
|
* @param T[] $old Original array
|
||||||
* @param array $new New array
|
* @param T[] $new New array
|
||||||
*
|
*
|
||||||
* @return DiffElem[] Diff (edit script), including replace operations
|
* @return DiffElem[] Diff (edit script), including replace operations
|
||||||
*/
|
*/
|
||||||
public function diffWithReplacements(array $old, array $new) {
|
public function diffWithReplacements(array $old, array $new): array {
|
||||||
return $this->coalesceReplacements($this->diff($old, $new));
|
return $this->coalesceReplacements($this->diff($old, $new));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateTrace(array $a, array $b) {
|
/**
|
||||||
$n = \count($a);
|
* @param T[] $old
|
||||||
$m = \count($b);
|
* @param T[] $new
|
||||||
|
* @return array{array<int, array<int, int>>, int, int}
|
||||||
|
*/
|
||||||
|
private function calculateTrace(array $old, array $new): array {
|
||||||
|
$n = \count($old);
|
||||||
|
$m = \count($new);
|
||||||
$max = $n + $m;
|
$max = $n + $m;
|
||||||
$v = [1 => 0];
|
$v = [1 => 0];
|
||||||
$trace = [];
|
$trace = [];
|
||||||
@ -67,7 +73,7 @@ class Differ
|
|||||||
}
|
}
|
||||||
|
|
||||||
$y = $x - $k;
|
$y = $x - $k;
|
||||||
while ($x < $n && $y < $m && ($this->isEqual)($a[$x], $b[$y])) {
|
while ($x < $n && $y < $m && ($this->isEqual)($old[$x], $new[$y])) {
|
||||||
$x++;
|
$x++;
|
||||||
$y++;
|
$y++;
|
||||||
}
|
}
|
||||||
@ -81,7 +87,15 @@ class Differ
|
|||||||
throw new \Exception('Should not happen');
|
throw new \Exception('Should not happen');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extractDiff(array $trace, int $x, int $y, array $a, array $b) {
|
/**
|
||||||
|
* @param array<int, array<int, int>> $trace
|
||||||
|
* @param int $x
|
||||||
|
* @param int $y
|
||||||
|
* @param T[] $old
|
||||||
|
* @param T[] $new
|
||||||
|
* @return DiffElem[]
|
||||||
|
*/
|
||||||
|
private function extractDiff(array $trace, int $x, int $y, array $old, array $new): array {
|
||||||
$result = [];
|
$result = [];
|
||||||
for ($d = \count($trace) - 1; $d >= 0; $d--) {
|
for ($d = \count($trace) - 1; $d >= 0; $d--) {
|
||||||
$v = $trace[$d];
|
$v = $trace[$d];
|
||||||
@ -97,7 +111,7 @@ class Differ
|
|||||||
$prevY = $prevX - $prevK;
|
$prevY = $prevX - $prevK;
|
||||||
|
|
||||||
while ($x > $prevX && $y > $prevY) {
|
while ($x > $prevX && $y > $prevY) {
|
||||||
$result[] = new DiffElem(DiffElem::TYPE_KEEP, $a[$x-1], $b[$y-1]);
|
$result[] = new DiffElem(DiffElem::TYPE_KEEP, $old[$x-1], $new[$y-1]);
|
||||||
$x--;
|
$x--;
|
||||||
$y--;
|
$y--;
|
||||||
}
|
}
|
||||||
@ -107,12 +121,12 @@ class Differ
|
|||||||
}
|
}
|
||||||
|
|
||||||
while ($x > $prevX) {
|
while ($x > $prevX) {
|
||||||
$result[] = new DiffElem(DiffElem::TYPE_REMOVE, $a[$x-1], null);
|
$result[] = new DiffElem(DiffElem::TYPE_REMOVE, $old[$x-1], null);
|
||||||
$x--;
|
$x--;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ($y > $prevY) {
|
while ($y > $prevY) {
|
||||||
$result[] = new DiffElem(DiffElem::TYPE_ADD, null, $b[$y-1]);
|
$result[] = new DiffElem(DiffElem::TYPE_ADD, null, $new[$y-1]);
|
||||||
$y--;
|
$y--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,7 +139,7 @@ class Differ
|
|||||||
* @param DiffElem[] $diff
|
* @param DiffElem[] $diff
|
||||||
* @return DiffElem[]
|
* @return DiffElem[]
|
||||||
*/
|
*/
|
||||||
private function coalesceReplacements(array $diff) {
|
private function coalesceReplacements(array $diff): array {
|
||||||
$newDiff = [];
|
$newDiff = [];
|
||||||
$c = \count($diff);
|
$c = \count($diff);
|
||||||
for ($i = 0; $i < $c; $i++) {
|
for ($i = 0; $i < $c; $i++) {
|
||||||
|
@ -15,11 +15,10 @@ use PhpParser\Node\Expr;
|
|||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class PrintableNewAnonClassNode extends Expr
|
class PrintableNewAnonClassNode extends Expr {
|
||||||
{
|
|
||||||
/** @var Node\AttributeGroup[] PHP attribute groups */
|
/** @var Node\AttributeGroup[] PHP attribute groups */
|
||||||
public $attrGroups;
|
public $attrGroups;
|
||||||
/** @var Node\Arg[] Arguments */
|
/** @var (Node\Arg|Node\VariadicPlaceholder)[] Arguments */
|
||||||
public $args;
|
public $args;
|
||||||
/** @var null|Node\Name Name of extended class */
|
/** @var null|Node\Name Name of extended class */
|
||||||
public $extends;
|
public $extends;
|
||||||
@ -28,8 +27,16 @@ class PrintableNewAnonClassNode extends Expr
|
|||||||
/** @var Node\Stmt[] Statements */
|
/** @var Node\Stmt[] Statements */
|
||||||
public $stmts;
|
public $stmts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Node\AttributeGroup[] $attrGroups PHP attribute groups
|
||||||
|
* @param (Node\Arg|Node\VariadicPlaceholder)[] $args Arguments
|
||||||
|
* @param Node\Name|null $extends Name of extended class
|
||||||
|
* @param Node\Name[] $implements Names of implemented interfaces
|
||||||
|
* @param Node\Stmt[] $stmts Statements
|
||||||
|
* @param array<string, mixed> $attributes Attributes
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
array $attrGroups, array $args, Node\Name $extends = null, array $implements,
|
array $attrGroups, array $args, ?Node\Name $extends, array $implements,
|
||||||
array $stmts, array $attributes
|
array $stmts, array $attributes
|
||||||
) {
|
) {
|
||||||
parent::__construct($attributes);
|
parent::__construct($attributes);
|
||||||
@ -40,7 +47,7 @@ class PrintableNewAnonClassNode extends Expr
|
|||||||
$this->stmts = $stmts;
|
$this->stmts = $stmts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromNewNode(Expr\New_ $newNode) {
|
public static function fromNewNode(Expr\New_ $newNode): self {
|
||||||
$class = $newNode->class;
|
$class = $newNode->class;
|
||||||
assert($class instanceof Node\Stmt\Class_);
|
assert($class instanceof Node\Stmt\Class_);
|
||||||
// We don't assert that $class->name is null here, to allow consumers to assign unique names
|
// We don't assert that $class->name is null here, to allow consumers to assign unique names
|
||||||
@ -51,11 +58,11 @@ class PrintableNewAnonClassNode extends Expr
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Expr_PrintableNewAnonClass';
|
return 'Expr_PrintableNewAnonClass';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['attrGroups', 'args', 'extends', 'implements', 'stmts'];
|
return ['attrGroups', 'args', 'extends', 'implements', 'stmts'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
272
lib/PhpParser/Internal/TokenPolyfill.php
Normal file
272
lib/PhpParser/Internal/TokenPolyfill.php
Normal file
@ -0,0 +1,272 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Internal;
|
||||||
|
|
||||||
|
if (\PHP_VERSION_ID >= 80000) {
|
||||||
|
class TokenPolyfill extends \PhpToken {
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a polyfill for the PhpToken class introduced in PHP 8.0. We do not actually polyfill
|
||||||
|
* PhpToken, because composer might end up picking a different polyfill implementation, which does
|
||||||
|
* not meet our requirements.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class TokenPolyfill {
|
||||||
|
/** @var int The ID of the token. Either a T_* constant of a character code < 256. */
|
||||||
|
public $id;
|
||||||
|
/** @var string The textual content of the token. */
|
||||||
|
public $text;
|
||||||
|
/** @var int The 1-based starting line of the token (or -1 if unknown). */
|
||||||
|
public $line;
|
||||||
|
/** @var int The 0-based starting position of the token (or -1 if unknown). */
|
||||||
|
public $pos;
|
||||||
|
|
||||||
|
/** @var array<int, bool> Tokens ignored by the PHP parser. */
|
||||||
|
private const IGNORABLE_TOKENS = [
|
||||||
|
\T_WHITESPACE => true,
|
||||||
|
\T_COMMENT => true,
|
||||||
|
\T_DOC_COMMENT => true,
|
||||||
|
\T_OPEN_TAG => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
/** @var array<int, bool>|null Tokens that may be part of a T_NAME_* identifier. */
|
||||||
|
private static $identifierTokens;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Token with the given ID and text, as well optional line and position information.
|
||||||
|
*/
|
||||||
|
final public function __construct(int $id, string $text, int $line = -1, int $pos = -1) {
|
||||||
|
$this->id = $id;
|
||||||
|
$this->text = $text;
|
||||||
|
$this->line = $line;
|
||||||
|
$this->pos = $pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the token. For single-char tokens this will be the token character.
|
||||||
|
* Otherwise it will be a T_* style name, or null if the token ID is unknown.
|
||||||
|
*/
|
||||||
|
public function getTokenName(): ?string {
|
||||||
|
if ($this->id < 256) {
|
||||||
|
return \chr($this->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$name = token_name($this->id);
|
||||||
|
return $name === 'UNKNOWN' ? null : $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the token is of the given kind. The kind may be either an integer that matches
|
||||||
|
* the token ID, a string that matches the token text, or an array of integers/strings. In the
|
||||||
|
* latter case, the function returns true if any of the kinds in the array match.
|
||||||
|
*
|
||||||
|
* @param int|string|(int|string)[] $kind
|
||||||
|
*/
|
||||||
|
public function is($kind): bool {
|
||||||
|
if (\is_int($kind)) {
|
||||||
|
return $this->id === $kind;
|
||||||
|
}
|
||||||
|
if (\is_string($kind)) {
|
||||||
|
return $this->text === $kind;
|
||||||
|
}
|
||||||
|
if (\is_array($kind)) {
|
||||||
|
foreach ($kind as $entry) {
|
||||||
|
if (\is_int($entry)) {
|
||||||
|
if ($this->id === $entry) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} elseif (\is_string($entry)) {
|
||||||
|
if ($this->text === $entry) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new \TypeError(
|
||||||
|
'Argument #1 ($kind) must only have elements of type string|int, ' .
|
||||||
|
gettype($entry) . ' given');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
throw new \TypeError(
|
||||||
|
'Argument #1 ($kind) must be of type string|int|array, ' .gettype($kind) . ' given');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether this token would be ignored by the PHP parser. Returns true for T_WHITESPACE,
|
||||||
|
* T_COMMENT, T_DOC_COMMENT and T_OPEN_TAG, and false for everything else.
|
||||||
|
*/
|
||||||
|
public function isIgnorable(): bool {
|
||||||
|
return isset(self::IGNORABLE_TOKENS[$this->id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the textual content of the token.
|
||||||
|
*/
|
||||||
|
public function __toString(): string {
|
||||||
|
return $this->text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tokenize the given source code and return an array of tokens.
|
||||||
|
*
|
||||||
|
* This performs certain canonicalizations to match the PHP 8.0 token format:
|
||||||
|
* * Bad characters are represented using T_BAD_CHARACTER rather than omitted.
|
||||||
|
* * T_COMMENT does not include trailing newlines, instead the newline is part of a following
|
||||||
|
* T_WHITESPACE token.
|
||||||
|
* * Namespaced names are represented using T_NAME_* tokens.
|
||||||
|
*
|
||||||
|
* @return static[]
|
||||||
|
*/
|
||||||
|
public static function tokenize(string $code, int $flags = 0): array {
|
||||||
|
self::init();
|
||||||
|
|
||||||
|
$tokens = [];
|
||||||
|
$line = 1;
|
||||||
|
$pos = 0;
|
||||||
|
$origTokens = \token_get_all($code, $flags);
|
||||||
|
if (\PHP_VERSION_ID < 70400) {
|
||||||
|
$origTokens = self::fixupBadCharacters($code, $origTokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
$numTokens = \count($origTokens);
|
||||||
|
for ($i = 0; $i < $numTokens; $i++) {
|
||||||
|
$token = $origTokens[$i];
|
||||||
|
if (\is_string($token)) {
|
||||||
|
if (\strlen($token) === 2) {
|
||||||
|
// b" and B" are tokenized as single-char tokens, even though they aren't.
|
||||||
|
$tokens[] = new static(\ord('"'), $token, $line, $pos);
|
||||||
|
$pos += 2;
|
||||||
|
} else {
|
||||||
|
$tokens[] = new static(\ord($token), $token, $line, $pos);
|
||||||
|
$pos++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$id = $token[0];
|
||||||
|
$text = $token[1];
|
||||||
|
|
||||||
|
// Emulate PHP 8.0 comment format, which does not include trailing whitespace anymore.
|
||||||
|
if ($id === \T_COMMENT && \substr($text, 0, 2) !== '/*' &&
|
||||||
|
\preg_match('/(\r\n|\n|\r)$/D', $text, $matches)
|
||||||
|
) {
|
||||||
|
$trailingNewline = $matches[0];
|
||||||
|
$text = \substr($text, 0, -\strlen($trailingNewline));
|
||||||
|
$tokens[] = new static($id, $text, $line, $pos);
|
||||||
|
$pos += \strlen($text);
|
||||||
|
|
||||||
|
if ($i + 1 < $numTokens && $origTokens[$i + 1][0] === \T_WHITESPACE) {
|
||||||
|
// Move trailing newline into following T_WHITESPACE token, if it already exists.
|
||||||
|
$origTokens[$i + 1][1] = $trailingNewline . $origTokens[$i + 1][1];
|
||||||
|
$origTokens[$i + 1][2]--;
|
||||||
|
} else {
|
||||||
|
// Otherwise, we need to create a new T_WHITESPACE token.
|
||||||
|
$tokens[] = new static(\T_WHITESPACE, $trailingNewline, $line, $pos);
|
||||||
|
$line++;
|
||||||
|
$pos += \strlen($trailingNewline);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emulate PHP 8.0 T_NAME_* tokens, by combining sequences of T_NS_SEPARATOR and
|
||||||
|
// T_STRING into a single token.
|
||||||
|
if (($id === \T_NS_SEPARATOR || isset(self::$identifierTokens[$id]))) {
|
||||||
|
$newText = $text;
|
||||||
|
$lastWasSeparator = $id === \T_NS_SEPARATOR;
|
||||||
|
for ($j = $i + 1; $j < $numTokens; $j++) {
|
||||||
|
if ($lastWasSeparator) {
|
||||||
|
if (!isset(self::$identifierTokens[$origTokens[$j][0]])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$lastWasSeparator = false;
|
||||||
|
} else {
|
||||||
|
if ($origTokens[$j][0] !== \T_NS_SEPARATOR) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$lastWasSeparator = true;
|
||||||
|
}
|
||||||
|
$newText .= $origTokens[$j][1];
|
||||||
|
}
|
||||||
|
if ($lastWasSeparator) {
|
||||||
|
// Trailing separator is not part of the name.
|
||||||
|
$j--;
|
||||||
|
$newText = \substr($newText, 0, -1);
|
||||||
|
}
|
||||||
|
if ($j > $i + 1) {
|
||||||
|
if ($id === \T_NS_SEPARATOR) {
|
||||||
|
$id = \T_NAME_FULLY_QUALIFIED;
|
||||||
|
} elseif ($id === \T_NAMESPACE) {
|
||||||
|
$id = \T_NAME_RELATIVE;
|
||||||
|
} else {
|
||||||
|
$id = \T_NAME_QUALIFIED;
|
||||||
|
}
|
||||||
|
$tokens[] = new static($id, $newText, $line, $pos);
|
||||||
|
$pos += \strlen($newText);
|
||||||
|
$i = $j - 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$tokens[] = new static($id, $text, $line, $pos);
|
||||||
|
$line += \substr_count($text, "\n");
|
||||||
|
$pos += \strlen($text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prior to PHP 7.4, token_get_all() simply dropped invalid characters from the token stream.
|
||||||
|
* Detect such cases and replace them with T_BAD_CHARACTER.
|
||||||
|
*/
|
||||||
|
private static function fixupBadCharacters(string $code, array $origTokens): array {
|
||||||
|
$newTokens = [];
|
||||||
|
$pos = 0;
|
||||||
|
foreach ($origTokens as $token) {
|
||||||
|
$text = \is_string($token) ? $token : $token[1];
|
||||||
|
$len = \strlen($text);
|
||||||
|
if (substr($code, $pos, $len) !== $text) {
|
||||||
|
$nextPos = strpos($code, $text, $pos);
|
||||||
|
for ($i = $pos; $i < $nextPos; $i++) {
|
||||||
|
// Don't bother including the line, we're not going to use it anyway.
|
||||||
|
$newTokens[] = [\T_BAD_CHARACTER, $code[$i]];
|
||||||
|
}
|
||||||
|
$pos = $nextPos;
|
||||||
|
}
|
||||||
|
$pos += $len;
|
||||||
|
$newTokens[] = $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle trailing invalid characters.
|
||||||
|
$codeLen = \strlen($code);
|
||||||
|
if ($pos !== $codeLen) {
|
||||||
|
for ($i = $pos; $i < $codeLen; $i++) {
|
||||||
|
$newTokens[] = [\T_BAD_CHARACTER, $code[$i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $newTokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Initialize private static state needed by tokenize(). */
|
||||||
|
private static function init(): void {
|
||||||
|
if (isset(self::$identifierTokens)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Based on semi_reserved production.
|
||||||
|
self::$identifierTokens = \array_fill_keys([
|
||||||
|
\T_STRING,
|
||||||
|
\T_STATIC, \T_ABSTRACT, \T_FINAL, \T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_READONLY,
|
||||||
|
\T_INCLUDE, \T_INCLUDE_ONCE, \T_EVAL, \T_REQUIRE, \T_REQUIRE_ONCE, \T_LOGICAL_OR, \T_LOGICAL_XOR, \T_LOGICAL_AND,
|
||||||
|
\T_INSTANCEOF, \T_NEW, \T_CLONE, \T_EXIT, \T_IF, \T_ELSEIF, \T_ELSE, \T_ENDIF, \T_ECHO, \T_DO, \T_WHILE,
|
||||||
|
\T_ENDWHILE, \T_FOR, \T_ENDFOR, \T_FOREACH, \T_ENDFOREACH, \T_DECLARE, \T_ENDDECLARE, \T_AS, \T_TRY, \T_CATCH,
|
||||||
|
\T_FINALLY, \T_THROW, \T_USE, \T_INSTEADOF, \T_GLOBAL, \T_VAR, \T_UNSET, \T_ISSET, \T_EMPTY, \T_CONTINUE, \T_GOTO,
|
||||||
|
\T_FUNCTION, \T_CONST, \T_RETURN, \T_PRINT, \T_YIELD, \T_LIST, \T_SWITCH, \T_ENDSWITCH, \T_CASE, \T_DEFAULT,
|
||||||
|
\T_BREAK, \T_ARRAY, \T_CALLABLE, \T_EXTENDS, \T_IMPLEMENTS, \T_NAMESPACE, \T_TRAIT, \T_INTERFACE, \T_CLASS,
|
||||||
|
\T_CLASS_C, \T_TRAIT_C, \T_FUNC_C, \T_METHOD_C, \T_LINE, \T_FILE, \T_DIR, \T_NS_C, \T_HALT_COMPILER, \T_FN,
|
||||||
|
\T_MATCH,
|
||||||
|
], true);
|
||||||
|
}
|
||||||
|
}
|
@ -2,14 +2,15 @@
|
|||||||
|
|
||||||
namespace PhpParser\Internal;
|
namespace PhpParser\Internal;
|
||||||
|
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides operations on token streams, for use by pretty printer.
|
* Provides operations on token streams, for use by pretty printer.
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class TokenStream
|
class TokenStream {
|
||||||
{
|
/** @var Token[] Tokens (in PhpToken::tokenize() format) */
|
||||||
/** @var array Tokens (in token_get_all format) */
|
|
||||||
private $tokens;
|
private $tokens;
|
||||||
/** @var int[] Map from position to indentation */
|
/** @var int[] Map from position to indentation */
|
||||||
private $indentMap;
|
private $indentMap;
|
||||||
@ -17,7 +18,7 @@ class TokenStream
|
|||||||
/**
|
/**
|
||||||
* Create token stream instance.
|
* Create token stream instance.
|
||||||
*
|
*
|
||||||
* @param array $tokens Tokens in token_get_all() format
|
* @param Token[] $tokens Tokens in PhpToken::tokenize() format
|
||||||
*/
|
*/
|
||||||
public function __construct(array $tokens) {
|
public function __construct(array $tokens) {
|
||||||
$this->tokens = $tokens;
|
$this->tokens = $tokens;
|
||||||
@ -32,7 +33,7 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function haveParens(int $startPos, int $endPos) : bool {
|
public function haveParens(int $startPos, int $endPos): bool {
|
||||||
return $this->haveTokenImmediatelyBefore($startPos, '(')
|
return $this->haveTokenImmediatelyBefore($startPos, '(')
|
||||||
&& $this->haveTokenImmediatelyAfter($endPos, ')');
|
&& $this->haveTokenImmediatelyAfter($endPos, ')');
|
||||||
}
|
}
|
||||||
@ -45,8 +46,9 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function haveBraces(int $startPos, int $endPos) : bool {
|
public function haveBraces(int $startPos, int $endPos): bool {
|
||||||
return $this->haveTokenImmediatelyBefore($startPos, '{')
|
return ($this->haveTokenImmediatelyBefore($startPos, '{')
|
||||||
|
|| $this->haveTokenImmediatelyBefore($startPos, T_CURLY_OPEN))
|
||||||
&& $this->haveTokenImmediatelyAfter($endPos, '}');
|
&& $this->haveTokenImmediatelyAfter($endPos, '}');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,16 +62,15 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return bool Whether the expected token was found
|
* @return bool Whether the expected token was found
|
||||||
*/
|
*/
|
||||||
public function haveTokenImmediatelyBefore(int $pos, $expectedTokenType) : bool {
|
public function haveTokenImmediatelyBefore(int $pos, $expectedTokenType): bool {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
$pos--;
|
$pos--;
|
||||||
for (; $pos >= 0; $pos--) {
|
for (; $pos >= 0; $pos--) {
|
||||||
$tokenType = $tokens[$pos][0];
|
$token = $tokens[$pos];
|
||||||
if ($tokenType === $expectedTokenType) {
|
if ($token->is($expectedTokenType)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ($tokenType !== \T_WHITESPACE
|
if (!$token->isIgnorable()) {
|
||||||
&& $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,23 +87,23 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return bool Whether the expected token was found
|
* @return bool Whether the expected token was found
|
||||||
*/
|
*/
|
||||||
public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType) : bool {
|
public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType): bool {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
$pos++;
|
$pos++;
|
||||||
for (; $pos < \count($tokens); $pos++) {
|
for ($c = \count($tokens); $pos < $c; $pos++) {
|
||||||
$tokenType = $tokens[$pos][0];
|
$token = $tokens[$pos];
|
||||||
if ($tokenType === $expectedTokenType) {
|
if ($token->is($expectedTokenType)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ($tokenType !== \T_WHITESPACE
|
if (!$token->isIgnorable()) {
|
||||||
&& $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function skipLeft(int $pos, $skipTokenType) {
|
/** @param int|string|(int|string)[] $skipTokenType */
|
||||||
|
public function skipLeft(int $pos, $skipTokenType): int {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
|
|
||||||
$pos = $this->skipLeftWhitespace($pos);
|
$pos = $this->skipLeftWhitespace($pos);
|
||||||
@ -110,7 +111,7 @@ class TokenStream
|
|||||||
return $pos;
|
return $pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($tokens[$pos][0] !== $skipTokenType) {
|
if (!$tokens[$pos]->is($skipTokenType)) {
|
||||||
// Shouldn't happen. The skip token MUST be there
|
// Shouldn't happen. The skip token MUST be there
|
||||||
throw new \Exception('Encountered unexpected token');
|
throw new \Exception('Encountered unexpected token');
|
||||||
}
|
}
|
||||||
@ -119,7 +120,8 @@ class TokenStream
|
|||||||
return $this->skipLeftWhitespace($pos);
|
return $this->skipLeftWhitespace($pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function skipRight(int $pos, $skipTokenType) {
|
/** @param int|string|(int|string)[] $skipTokenType */
|
||||||
|
public function skipRight(int $pos, $skipTokenType): int {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
|
|
||||||
$pos = $this->skipRightWhitespace($pos);
|
$pos = $this->skipRightWhitespace($pos);
|
||||||
@ -127,7 +129,7 @@ class TokenStream
|
|||||||
return $pos;
|
return $pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($tokens[$pos][0] !== $skipTokenType) {
|
if (!$tokens[$pos]->is($skipTokenType)) {
|
||||||
// Shouldn't happen. The skip token MUST be there
|
// Shouldn't happen. The skip token MUST be there
|
||||||
throw new \Exception('Encountered unexpected token');
|
throw new \Exception('Encountered unexpected token');
|
||||||
}
|
}
|
||||||
@ -142,11 +144,10 @@ class TokenStream
|
|||||||
* @param int $pos Token position
|
* @param int $pos Token position
|
||||||
* @return int Non-whitespace token position
|
* @return int Non-whitespace token position
|
||||||
*/
|
*/
|
||||||
public function skipLeftWhitespace(int $pos) {
|
public function skipLeftWhitespace(int $pos): int {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
for (; $pos >= 0; $pos--) {
|
for (; $pos >= 0; $pos--) {
|
||||||
$type = $tokens[$pos][0];
|
if (!$tokens[$pos]->isIgnorable()) {
|
||||||
if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,22 +160,21 @@ class TokenStream
|
|||||||
* @param int $pos Token position
|
* @param int $pos Token position
|
||||||
* @return int Non-whitespace token position
|
* @return int Non-whitespace token position
|
||||||
*/
|
*/
|
||||||
public function skipRightWhitespace(int $pos) {
|
public function skipRightWhitespace(int $pos): int {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
for ($count = \count($tokens); $pos < $count; $pos++) {
|
for ($count = \count($tokens); $pos < $count; $pos++) {
|
||||||
$type = $tokens[$pos][0];
|
if (!$tokens[$pos]->isIgnorable()) {
|
||||||
if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $pos;
|
return $pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findRight(int $pos, $findTokenType) {
|
/** @param int|string|(int|string)[] $findTokenType */
|
||||||
|
public function findRight(int $pos, $findTokenType): int {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
for ($count = \count($tokens); $pos < $count; $pos++) {
|
for ($count = \count($tokens); $pos < $count; $pos++) {
|
||||||
$type = $tokens[$pos][0];
|
if ($tokens[$pos]->is($findTokenType)) {
|
||||||
if ($type === $findTokenType) {
|
|
||||||
return $pos;
|
return $pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,21 +189,27 @@ class TokenStream
|
|||||||
* @param int|string $tokenType Token type to look for
|
* @param int|string $tokenType Token type to look for
|
||||||
* @return bool Whether the token occurs in the given range
|
* @return bool Whether the token occurs in the given range
|
||||||
*/
|
*/
|
||||||
public function haveTokenInRange(int $startPos, int $endPos, $tokenType) {
|
public function haveTokenInRange(int $startPos, int $endPos, $tokenType): bool {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
for ($pos = $startPos; $pos < $endPos; $pos++) {
|
for ($pos = $startPos; $pos < $endPos; $pos++) {
|
||||||
if ($tokens[$pos][0] === $tokenType) {
|
if ($tokens[$pos]->is($tokenType)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function haveBracesInRange(int $startPos, int $endPos) {
|
public function haveBracesInRange(int $startPos, int $endPos): bool {
|
||||||
return $this->haveTokenInRange($startPos, $endPos, '{')
|
return $this->haveTokenInRange($startPos, $endPos, '{')
|
||||||
|
|| $this->haveTokenInRange($startPos, $endPos, T_CURLY_OPEN)
|
||||||
|| $this->haveTokenInRange($startPos, $endPos, '}');
|
|| $this->haveTokenInRange($startPos, $endPos, '}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function haveTagInRange(int $startPos, int $endPos): bool {
|
||||||
|
return $this->haveTokenInRange($startPos, $endPos, \T_OPEN_TAG)
|
||||||
|
|| $this->haveTokenInRange($startPos, $endPos, \T_CLOSE_TAG);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get indentation before token position.
|
* Get indentation before token position.
|
||||||
*
|
*
|
||||||
@ -211,7 +217,7 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return int Indentation depth (in spaces)
|
* @return int Indentation depth (in spaces)
|
||||||
*/
|
*/
|
||||||
public function getIndentationBefore(int $pos) : int {
|
public function getIndentationBefore(int $pos): int {
|
||||||
return $this->indentMap[$pos];
|
return $this->indentMap[$pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,28 +230,24 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return string Code corresponding to token range, adjusted for indentation
|
* @return string Code corresponding to token range, adjusted for indentation
|
||||||
*/
|
*/
|
||||||
public function getTokenCode(int $from, int $to, int $indent) : string {
|
public function getTokenCode(int $from, int $to, int $indent): string {
|
||||||
$tokens = $this->tokens;
|
$tokens = $this->tokens;
|
||||||
$result = '';
|
$result = '';
|
||||||
for ($pos = $from; $pos < $to; $pos++) {
|
for ($pos = $from; $pos < $to; $pos++) {
|
||||||
$token = $tokens[$pos];
|
$token = $tokens[$pos];
|
||||||
if (\is_array($token)) {
|
$id = $token->id;
|
||||||
$type = $token[0];
|
$text = $token->text;
|
||||||
$content = $token[1];
|
if ($id === \T_CONSTANT_ENCAPSED_STRING || $id === \T_ENCAPSED_AND_WHITESPACE) {
|
||||||
if ($type === \T_CONSTANT_ENCAPSED_STRING || $type === \T_ENCAPSED_AND_WHITESPACE) {
|
$result .= $text;
|
||||||
$result .= $content;
|
|
||||||
} else {
|
|
||||||
// TODO Handle non-space indentation
|
|
||||||
if ($indent < 0) {
|
|
||||||
$result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $content);
|
|
||||||
} elseif ($indent > 0) {
|
|
||||||
$result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $content);
|
|
||||||
} else {
|
|
||||||
$result .= $content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$result .= $token;
|
// TODO Handle non-space indentation
|
||||||
|
if ($indent < 0) {
|
||||||
|
$result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $text);
|
||||||
|
} elseif ($indent > 0) {
|
||||||
|
$result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $text);
|
||||||
|
} else {
|
||||||
|
$result .= $text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
@ -256,14 +258,14 @@ class TokenStream
|
|||||||
*
|
*
|
||||||
* @return int[] Token position to indentation map
|
* @return int[] Token position to indentation map
|
||||||
*/
|
*/
|
||||||
private function calcIndentMap() {
|
private function calcIndentMap(): array {
|
||||||
$indentMap = [];
|
$indentMap = [];
|
||||||
$indent = 0;
|
$indent = 0;
|
||||||
foreach ($this->tokens as $token) {
|
foreach ($this->tokens as $token) {
|
||||||
$indentMap[] = $indent;
|
$indentMap[] = $indent;
|
||||||
|
|
||||||
if ($token[0] === \T_WHITESPACE) {
|
if ($token->id === \T_WHITESPACE) {
|
||||||
$content = $token[1];
|
$content = $token->text;
|
||||||
$newlinePos = \strrpos($content, "\n");
|
$newlinePos = \strrpos($content, "\n");
|
||||||
if (false !== $newlinePos) {
|
if (false !== $newlinePos) {
|
||||||
$indent = \strlen($content) - $newlinePos - 1;
|
$indent = \strlen($content) - $newlinePos - 1;
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
class JsonDecoder
|
class JsonDecoder {
|
||||||
{
|
/** @var \ReflectionClass<Node>[] Node type to reflection class map */
|
||||||
/** @var \ReflectionClass[] Node type to reflection class map */
|
|
||||||
private $reflectionClassCache;
|
private $reflectionClassCache;
|
||||||
|
|
||||||
|
/** @return mixed */
|
||||||
public function decode(string $json) {
|
public function decode(string $json) {
|
||||||
$value = json_decode($json, true);
|
$value = json_decode($json, true);
|
||||||
if (json_last_error()) {
|
if (json_last_error()) {
|
||||||
@ -16,6 +16,10 @@ class JsonDecoder
|
|||||||
return $this->decodeRecursive($value);
|
return $this->decodeRecursive($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
private function decodeRecursive($value) {
|
private function decodeRecursive($value) {
|
||||||
if (\is_array($value)) {
|
if (\is_array($value)) {
|
||||||
if (isset($value['nodeType'])) {
|
if (isset($value['nodeType'])) {
|
||||||
@ -29,7 +33,7 @@ class JsonDecoder
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function decodeArray(array $array) : array {
|
private function decodeArray(array $array): array {
|
||||||
$decodedArray = [];
|
$decodedArray = [];
|
||||||
foreach ($array as $key => $value) {
|
foreach ($array as $key => $value) {
|
||||||
$decodedArray[$key] = $this->decodeRecursive($value);
|
$decodedArray[$key] = $this->decodeRecursive($value);
|
||||||
@ -37,14 +41,13 @@ class JsonDecoder
|
|||||||
return $decodedArray;
|
return $decodedArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function decodeNode(array $value) : Node {
|
private function decodeNode(array $value): Node {
|
||||||
$nodeType = $value['nodeType'];
|
$nodeType = $value['nodeType'];
|
||||||
if (!\is_string($nodeType)) {
|
if (!\is_string($nodeType)) {
|
||||||
throw new \RuntimeException('Node type must be a string');
|
throw new \RuntimeException('Node type must be a string');
|
||||||
}
|
}
|
||||||
|
|
||||||
$reflectionClass = $this->reflectionClassFromNodeType($nodeType);
|
$reflectionClass = $this->reflectionClassFromNodeType($nodeType);
|
||||||
/** @var Node $node */
|
|
||||||
$node = $reflectionClass->newInstanceWithoutConstructor();
|
$node = $reflectionClass->newInstanceWithoutConstructor();
|
||||||
|
|
||||||
if (isset($value['attributes'])) {
|
if (isset($value['attributes'])) {
|
||||||
@ -66,7 +69,7 @@ class JsonDecoder
|
|||||||
return $node;
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function decodeComment(array $value) : Comment {
|
private function decodeComment(array $value): Comment {
|
||||||
$className = $value['nodeType'] === 'Comment' ? Comment::class : Comment\Doc::class;
|
$className = $value['nodeType'] === 'Comment' ? Comment::class : Comment\Doc::class;
|
||||||
if (!isset($value['text'])) {
|
if (!isset($value['text'])) {
|
||||||
throw new \RuntimeException('Comment must have text');
|
throw new \RuntimeException('Comment must have text');
|
||||||
@ -79,7 +82,8 @@ class JsonDecoder
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function reflectionClassFromNodeType(string $nodeType) : \ReflectionClass {
|
/** @return \ReflectionClass<Node> */
|
||||||
|
private function reflectionClassFromNodeType(string $nodeType): \ReflectionClass {
|
||||||
if (!isset($this->reflectionClassCache[$nodeType])) {
|
if (!isset($this->reflectionClassCache[$nodeType])) {
|
||||||
$className = $this->classNameFromNodeType($nodeType);
|
$className = $this->classNameFromNodeType($nodeType);
|
||||||
$this->reflectionClassCache[$nodeType] = new \ReflectionClass($className);
|
$this->reflectionClassCache[$nodeType] = new \ReflectionClass($className);
|
||||||
@ -87,7 +91,8 @@ class JsonDecoder
|
|||||||
return $this->reflectionClassCache[$nodeType];
|
return $this->reflectionClassCache[$nodeType];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function classNameFromNodeType(string $nodeType) : string {
|
/** @return class-string<Node> */
|
||||||
|
private function classNameFromNodeType(string $nodeType): string {
|
||||||
$className = 'PhpParser\\Node\\' . strtr($nodeType, '_', '\\');
|
$className = 'PhpParser\\Node\\' . strtr($nodeType, '_', '\\');
|
||||||
if (class_exists($className)) {
|
if (class_exists($className)) {
|
||||||
return $className;
|
return $className;
|
||||||
|
@ -2,44 +2,45 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
use PhpParser\Parser\Tokens;
|
require __DIR__ . '/compatibility_tokens.php';
|
||||||
|
|
||||||
class Lexer
|
class Lexer {
|
||||||
{
|
/** @var string Code being tokenized */
|
||||||
protected $code;
|
protected $code;
|
||||||
|
/** @var list<Token> List of tokens */
|
||||||
protected $tokens;
|
protected $tokens;
|
||||||
|
/** @var int Current position in the token array */
|
||||||
protected $pos;
|
protected $pos;
|
||||||
protected $line;
|
/** @var bool Whether the preceding closing PHP tag has a trailing newline */
|
||||||
protected $filePos;
|
|
||||||
protected $prevCloseTagHasNewline;
|
protected $prevCloseTagHasNewline;
|
||||||
|
/** @var array<int, int> Map of tokens that should be dropped (like T_WHITESPACE) */
|
||||||
protected $tokenMap;
|
|
||||||
protected $dropTokens;
|
protected $dropTokens;
|
||||||
protected $identifierTokens;
|
|
||||||
|
|
||||||
|
/** @var bool Whether to use the startLine attribute */
|
||||||
private $attributeStartLineUsed;
|
private $attributeStartLineUsed;
|
||||||
|
/** @var bool Whether to use the endLine attribute */
|
||||||
private $attributeEndLineUsed;
|
private $attributeEndLineUsed;
|
||||||
|
/** @var bool Whether to use the startTokenPos attribute */
|
||||||
private $attributeStartTokenPosUsed;
|
private $attributeStartTokenPosUsed;
|
||||||
|
/** @var bool Whether to use the endTokenPos attribute */
|
||||||
private $attributeEndTokenPosUsed;
|
private $attributeEndTokenPosUsed;
|
||||||
|
/** @var bool Whether to use the startFilePos attribute */
|
||||||
private $attributeStartFilePosUsed;
|
private $attributeStartFilePosUsed;
|
||||||
|
/** @var bool Whether to use the endFilePos attribute */
|
||||||
private $attributeEndFilePosUsed;
|
private $attributeEndFilePosUsed;
|
||||||
|
/** @var bool Whether to use the comments attribute */
|
||||||
private $attributeCommentsUsed;
|
private $attributeCommentsUsed;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Lexer.
|
* Creates a Lexer.
|
||||||
*
|
*
|
||||||
* @param array $options Options array. Currently only the 'usedAttributes' option is supported,
|
* @param array{usedAttributes?: string[]} $options Options array. Currently only the
|
||||||
* which is an array of attributes to add to the AST nodes. Possible
|
* 'usedAttributes' option is supported, which is an array of attributes to add to the
|
||||||
* attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
|
* AST nodes. Possible attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
|
||||||
* 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the
|
* 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the first three.
|
||||||
* first three. For more info see getNextToken() docs.
|
* For more info see getNextToken() docs.
|
||||||
*/
|
*/
|
||||||
public function __construct(array $options = []) {
|
public function __construct(array $options = []) {
|
||||||
// Create Map from internal tokens to PhpParser tokens.
|
|
||||||
$this->defineCompatibilityTokens();
|
|
||||||
$this->tokenMap = $this->createTokenMap();
|
|
||||||
$this->identifierTokens = $this->createIdentifierTokenMap();
|
|
||||||
|
|
||||||
// map of tokens to drop while lexing (the map is only used for isset lookup,
|
// map of tokens to drop while lexing (the map is only used for isset lookup,
|
||||||
// that's why the value is simply set to 1; the value is never actually used.)
|
// that's why the value is simply set to 1; the value is never actually used.)
|
||||||
$this->dropTokens = array_fill_keys(
|
$this->dropTokens = array_fill_keys(
|
||||||
@ -69,15 +70,13 @@ class Lexer
|
|||||||
* @param ErrorHandler|null $errorHandler Error handler to use for lexing errors. Defaults to
|
* @param ErrorHandler|null $errorHandler Error handler to use for lexing errors. Defaults to
|
||||||
* ErrorHandler\Throwing
|
* ErrorHandler\Throwing
|
||||||
*/
|
*/
|
||||||
public function startLexing(string $code, ErrorHandler $errorHandler = null) {
|
public function startLexing(string $code, ?ErrorHandler $errorHandler = null): void {
|
||||||
if (null === $errorHandler) {
|
if (null === $errorHandler) {
|
||||||
$errorHandler = new ErrorHandler\Throwing();
|
$errorHandler = new ErrorHandler\Throwing();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->code = $code; // keep the code around for __halt_compiler() handling
|
$this->code = $code; // keep the code around for __halt_compiler() handling
|
||||||
$this->pos = -1;
|
$this->pos = -1;
|
||||||
$this->line = 1;
|
|
||||||
$this->filePos = 0;
|
|
||||||
|
|
||||||
// If inline HTML occurs without preceding code, treat it as if it had a leading newline.
|
// If inline HTML occurs without preceding code, treat it as if it had a leading newline.
|
||||||
// This ensures proper composability, because having a newline is the "safe" assumption.
|
// This ensures proper composability, because having a newline is the "safe" assumption.
|
||||||
@ -85,8 +84,7 @@ class Lexer
|
|||||||
|
|
||||||
$scream = ini_set('xdebug.scream', '0');
|
$scream = ini_set('xdebug.scream', '0');
|
||||||
|
|
||||||
error_clear_last();
|
$this->tokens = @Token::tokenize($code);
|
||||||
$this->tokens = @token_get_all($code);
|
|
||||||
$this->postprocessTokens($errorHandler);
|
$this->postprocessTokens($errorHandler);
|
||||||
|
|
||||||
if (false !== $scream) {
|
if (false !== $scream) {
|
||||||
@ -94,175 +92,77 @@ class Lexer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handleInvalidCharacterRange($start, $end, $line, ErrorHandler $errorHandler) {
|
private function handleInvalidCharacter(Token $token, ErrorHandler $errorHandler): void {
|
||||||
$tokens = [];
|
$chr = $token->text;
|
||||||
for ($i = $start; $i < $end; $i++) {
|
if ($chr === "\0") {
|
||||||
$chr = $this->code[$i];
|
// PHP cuts error message after null byte, so need special case
|
||||||
if ($chr === "\0") {
|
$errorMsg = 'Unexpected null byte';
|
||||||
// PHP cuts error message after null byte, so need special case
|
} else {
|
||||||
$errorMsg = 'Unexpected null byte';
|
$errorMsg = sprintf(
|
||||||
} else {
|
'Unexpected character "%s" (ASCII %d)', $chr, ord($chr)
|
||||||
$errorMsg = sprintf(
|
);
|
||||||
'Unexpected character "%s" (ASCII %d)', $chr, ord($chr)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$tokens[] = [\T_BAD_CHARACTER, $chr, $line];
|
|
||||||
$errorHandler->handleError(new Error($errorMsg, [
|
|
||||||
'startLine' => $line,
|
|
||||||
'endLine' => $line,
|
|
||||||
'startFilePos' => $i,
|
|
||||||
'endFilePos' => $i,
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
return $tokens;
|
|
||||||
|
$errorHandler->handleError(new Error($errorMsg, [
|
||||||
|
'startLine' => $token->line,
|
||||||
|
'endLine' => $token->line,
|
||||||
|
'startFilePos' => $token->pos,
|
||||||
|
'endFilePos' => $token->pos,
|
||||||
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function isUnterminatedComment(Token $token): bool {
|
||||||
* Check whether comment token is unterminated.
|
return $token->is([\T_COMMENT, \T_DOC_COMMENT])
|
||||||
*
|
&& substr($token->text, 0, 2) === '/*'
|
||||||
* @return bool
|
&& substr($token->text, -2) !== '*/';
|
||||||
*/
|
|
||||||
private function isUnterminatedComment($token) : bool {
|
|
||||||
return ($token[0] === \T_COMMENT || $token[0] === \T_DOC_COMMENT)
|
|
||||||
&& substr($token[1], 0, 2) === '/*'
|
|
||||||
&& substr($token[1], -2) !== '*/';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function postprocessTokens(ErrorHandler $errorHandler) {
|
protected function postprocessTokens(ErrorHandler $errorHandler): void {
|
||||||
// PHP's error handling for token_get_all() is rather bad, so if we want detailed
|
// This function reports errors (bad characters and unterminated comments) in the token
|
||||||
// error information we need to compute it ourselves. Invalid character errors are
|
// array, and performs certain canonicalizations:
|
||||||
// detected by finding "gaps" in the token array. Unterminated comments are detected
|
// * Use PHP 8.1 T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG and
|
||||||
// by checking if a trailing comment has a "*/" at the end.
|
// T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG tokens used to disambiguate intersection types.
|
||||||
//
|
// * Add a sentinel token with ID 0.
|
||||||
// Additionally, we canonicalize to the PHP 8 comment format here, which does not include
|
|
||||||
// the trailing whitespace anymore.
|
|
||||||
//
|
|
||||||
// We also canonicalize to the PHP 8 T_NAME_* tokens.
|
|
||||||
|
|
||||||
$filePos = 0;
|
|
||||||
$line = 1;
|
|
||||||
$numTokens = \count($this->tokens);
|
$numTokens = \count($this->tokens);
|
||||||
for ($i = 0; $i < $numTokens; $i++) {
|
if ($numTokens === 0) {
|
||||||
$token = $this->tokens[$i];
|
// Empty input edge case: Just add the sentinel token.
|
||||||
|
$this->tokens[] = new Token(0, "\0", 1, 0);
|
||||||
// Since PHP 7.4 invalid characters are represented by a T_BAD_CHARACTER token.
|
|
||||||
// In this case we only need to emit an error.
|
|
||||||
if ($token[0] === \T_BAD_CHARACTER) {
|
|
||||||
$this->handleInvalidCharacterRange($filePos, $filePos + 1, $line, $errorHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($token[0] === \T_COMMENT && substr($token[1], 0, 2) !== '/*'
|
|
||||||
&& preg_match('/(\r\n|\n|\r)$/D', $token[1], $matches)) {
|
|
||||||
$trailingNewline = $matches[0];
|
|
||||||
$token[1] = substr($token[1], 0, -strlen($trailingNewline));
|
|
||||||
$this->tokens[$i] = $token;
|
|
||||||
if (isset($this->tokens[$i + 1]) && $this->tokens[$i + 1][0] === \T_WHITESPACE) {
|
|
||||||
// Move trailing newline into following T_WHITESPACE token, if it already exists.
|
|
||||||
$this->tokens[$i + 1][1] = $trailingNewline . $this->tokens[$i + 1][1];
|
|
||||||
$this->tokens[$i + 1][2]--;
|
|
||||||
} else {
|
|
||||||
// Otherwise, we need to create a new T_WHITESPACE token.
|
|
||||||
array_splice($this->tokens, $i + 1, 0, [
|
|
||||||
[\T_WHITESPACE, $trailingNewline, $line],
|
|
||||||
]);
|
|
||||||
$numTokens++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emulate PHP 8 T_NAME_* tokens, by combining sequences of T_NS_SEPARATOR and T_STRING
|
|
||||||
// into a single token.
|
|
||||||
if (\is_array($token)
|
|
||||||
&& ($token[0] === \T_NS_SEPARATOR || isset($this->identifierTokens[$token[0]]))) {
|
|
||||||
$lastWasSeparator = $token[0] === \T_NS_SEPARATOR;
|
|
||||||
$text = $token[1];
|
|
||||||
for ($j = $i + 1; isset($this->tokens[$j]); $j++) {
|
|
||||||
if ($lastWasSeparator) {
|
|
||||||
if (!isset($this->identifierTokens[$this->tokens[$j][0]])) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$lastWasSeparator = false;
|
|
||||||
} else {
|
|
||||||
if ($this->tokens[$j][0] !== \T_NS_SEPARATOR) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$lastWasSeparator = true;
|
|
||||||
}
|
|
||||||
$text .= $this->tokens[$j][1];
|
|
||||||
}
|
|
||||||
if ($lastWasSeparator) {
|
|
||||||
// Trailing separator is not part of the name.
|
|
||||||
$j--;
|
|
||||||
$text = substr($text, 0, -1);
|
|
||||||
}
|
|
||||||
if ($j > $i + 1) {
|
|
||||||
if ($token[0] === \T_NS_SEPARATOR) {
|
|
||||||
$type = \T_NAME_FULLY_QUALIFIED;
|
|
||||||
} else if ($token[0] === \T_NAMESPACE) {
|
|
||||||
$type = \T_NAME_RELATIVE;
|
|
||||||
} else {
|
|
||||||
$type = \T_NAME_QUALIFIED;
|
|
||||||
}
|
|
||||||
$token = [$type, $text, $line];
|
|
||||||
array_splice($this->tokens, $i, $j - $i, [$token]);
|
|
||||||
$numTokens -= $j - $i - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$tokenValue = \is_string($token) ? $token : $token[1];
|
|
||||||
$tokenLen = \strlen($tokenValue);
|
|
||||||
|
|
||||||
if (substr($this->code, $filePos, $tokenLen) !== $tokenValue) {
|
|
||||||
// Something is missing, must be an invalid character
|
|
||||||
$nextFilePos = strpos($this->code, $tokenValue, $filePos);
|
|
||||||
$badCharTokens = $this->handleInvalidCharacterRange(
|
|
||||||
$filePos, $nextFilePos, $line, $errorHandler);
|
|
||||||
$filePos = (int) $nextFilePos;
|
|
||||||
|
|
||||||
array_splice($this->tokens, $i, 0, $badCharTokens);
|
|
||||||
$numTokens += \count($badCharTokens);
|
|
||||||
$i += \count($badCharTokens);
|
|
||||||
}
|
|
||||||
|
|
||||||
$filePos += $tokenLen;
|
|
||||||
$line += substr_count($tokenValue, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($filePos !== \strlen($this->code)) {
|
|
||||||
if (substr($this->code, $filePos, 2) === '/*') {
|
|
||||||
// Unlike PHP, HHVM will drop unterminated comments entirely
|
|
||||||
$comment = substr($this->code, $filePos);
|
|
||||||
$errorHandler->handleError(new Error('Unterminated comment', [
|
|
||||||
'startLine' => $line,
|
|
||||||
'endLine' => $line + substr_count($comment, "\n"),
|
|
||||||
'startFilePos' => $filePos,
|
|
||||||
'endFilePos' => $filePos + \strlen($comment),
|
|
||||||
]));
|
|
||||||
|
|
||||||
// Emulate the PHP behavior
|
|
||||||
$isDocComment = isset($comment[3]) && $comment[3] === '*';
|
|
||||||
$this->tokens[] = [$isDocComment ? \T_DOC_COMMENT : \T_COMMENT, $comment, $line];
|
|
||||||
} else {
|
|
||||||
// Invalid characters at the end of the input
|
|
||||||
$badCharTokens = $this->handleInvalidCharacterRange(
|
|
||||||
$filePos, \strlen($this->code), $line, $errorHandler);
|
|
||||||
$this->tokens = array_merge($this->tokens, $badCharTokens);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($this->tokens) > 0) {
|
for ($i = 0; $i < $numTokens; $i++) {
|
||||||
// Check for unterminated comment
|
$token = $this->tokens[$i];
|
||||||
$lastToken = $this->tokens[count($this->tokens) - 1];
|
if ($token->id === \T_BAD_CHARACTER) {
|
||||||
if ($this->isUnterminatedComment($lastToken)) {
|
$this->handleInvalidCharacter($token, $errorHandler);
|
||||||
$errorHandler->handleError(new Error('Unterminated comment', [
|
}
|
||||||
'startLine' => $line - substr_count($lastToken[1], "\n"),
|
|
||||||
'endLine' => $line,
|
if ($token->id === \ord('&')) {
|
||||||
'startFilePos' => $filePos - \strlen($lastToken[1]),
|
$next = $i + 1;
|
||||||
'endFilePos' => $filePos,
|
while (isset($this->tokens[$next]) && $this->tokens[$next]->id === \T_WHITESPACE) {
|
||||||
]));
|
$next++;
|
||||||
|
}
|
||||||
|
$followedByVarOrVarArg = isset($this->tokens[$next]) &&
|
||||||
|
$this->tokens[$next]->is([\T_VARIABLE, \T_ELLIPSIS]);
|
||||||
|
$token->id = $followedByVarOrVarArg
|
||||||
|
? \T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
|
||||||
|
: \T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for unterminated comment
|
||||||
|
$lastToken = $this->tokens[$numTokens - 1];
|
||||||
|
if ($this->isUnterminatedComment($lastToken)) {
|
||||||
|
$errorHandler->handleError(new Error('Unterminated comment', [
|
||||||
|
'startLine' => $lastToken->line,
|
||||||
|
'endLine' => $lastToken->getEndLine(),
|
||||||
|
'startFilePos' => $lastToken->pos,
|
||||||
|
'endFilePos' => $lastToken->getEndPos(),
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add sentinel token.
|
||||||
|
$this->tokens[] = new Token(0, "\0", $lastToken->getEndLine(), $lastToken->getEndPos());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -287,97 +187,76 @@ class Lexer
|
|||||||
*
|
*
|
||||||
* @return int Token id
|
* @return int Token id
|
||||||
*/
|
*/
|
||||||
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) : int {
|
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null): int {
|
||||||
$startAttributes = [];
|
$startAttributes = [];
|
||||||
$endAttributes = [];
|
$endAttributes = [];
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (isset($this->tokens[++$this->pos])) {
|
$token = $this->tokens[++$this->pos];
|
||||||
$token = $this->tokens[$this->pos];
|
|
||||||
} else {
|
|
||||||
// EOF token with ID 0
|
|
||||||
$token = "\0";
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->attributeStartLineUsed) {
|
if ($this->attributeStartLineUsed) {
|
||||||
$startAttributes['startLine'] = $this->line;
|
$startAttributes['startLine'] = $token->line;
|
||||||
}
|
}
|
||||||
if ($this->attributeStartTokenPosUsed) {
|
if ($this->attributeStartTokenPosUsed) {
|
||||||
$startAttributes['startTokenPos'] = $this->pos;
|
$startAttributes['startTokenPos'] = $this->pos;
|
||||||
}
|
}
|
||||||
if ($this->attributeStartFilePosUsed) {
|
if ($this->attributeStartFilePosUsed) {
|
||||||
$startAttributes['startFilePos'] = $this->filePos;
|
$startAttributes['startFilePos'] = $token->pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\is_string($token)) {
|
$id = $token->id;
|
||||||
$value = $token;
|
if (isset($this->dropTokens[$id])) {
|
||||||
if (isset($token[1])) {
|
if (\T_COMMENT === $id || \T_DOC_COMMENT === $id) {
|
||||||
// bug in token_get_all
|
|
||||||
$this->filePos += 2;
|
|
||||||
$id = ord('"');
|
|
||||||
} else {
|
|
||||||
$this->filePos += 1;
|
|
||||||
$id = ord($token);
|
|
||||||
}
|
|
||||||
} elseif (!isset($this->dropTokens[$token[0]])) {
|
|
||||||
$value = $token[1];
|
|
||||||
$id = $this->tokenMap[$token[0]];
|
|
||||||
if (\T_CLOSE_TAG === $token[0]) {
|
|
||||||
$this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
|
|
||||||
} elseif (\T_INLINE_HTML === $token[0]) {
|
|
||||||
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->line += substr_count($value, "\n");
|
|
||||||
$this->filePos += \strlen($value);
|
|
||||||
} else {
|
|
||||||
$origLine = $this->line;
|
|
||||||
$origFilePos = $this->filePos;
|
|
||||||
$this->line += substr_count($token[1], "\n");
|
|
||||||
$this->filePos += \strlen($token[1]);
|
|
||||||
|
|
||||||
if (\T_COMMENT === $token[0] || \T_DOC_COMMENT === $token[0]) {
|
|
||||||
if ($this->attributeCommentsUsed) {
|
if ($this->attributeCommentsUsed) {
|
||||||
$comment = \T_DOC_COMMENT === $token[0]
|
$comment = \T_DOC_COMMENT === $id
|
||||||
? new Comment\Doc($token[1],
|
? new Comment\Doc($token->text, $token->line, $token->pos, $this->pos,
|
||||||
$origLine, $origFilePos, $this->pos,
|
$token->getEndLine(), $token->getEndPos() - 1, $this->pos)
|
||||||
$this->line, $this->filePos - 1, $this->pos)
|
: new Comment($token->text, $token->line, $token->pos, $this->pos,
|
||||||
: new Comment($token[1],
|
$token->getEndLine(), $token->getEndPos() - 1, $this->pos);
|
||||||
$origLine, $origFilePos, $this->pos,
|
|
||||||
$this->line, $this->filePos - 1, $this->pos);
|
|
||||||
$startAttributes['comments'][] = $comment;
|
$startAttributes['comments'][] = $comment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$value = $token->text;
|
||||||
|
if (\T_CLOSE_TAG === $token->id) {
|
||||||
|
$this->prevCloseTagHasNewline = false !== strpos($value, "\n")
|
||||||
|
|| false !== strpos($value, "\r");
|
||||||
|
} elseif (\T_INLINE_HTML === $token->id) {
|
||||||
|
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the end line/pos from the next token (if available) instead of recomputing it.
|
||||||
|
$nextToken = $this->tokens[$this->pos + 1] ?? null;
|
||||||
if ($this->attributeEndLineUsed) {
|
if ($this->attributeEndLineUsed) {
|
||||||
$endAttributes['endLine'] = $this->line;
|
$endAttributes['endLine'] = $nextToken ? $nextToken->line : $token->getEndLine();
|
||||||
}
|
}
|
||||||
if ($this->attributeEndTokenPosUsed) {
|
if ($this->attributeEndTokenPosUsed) {
|
||||||
$endAttributes['endTokenPos'] = $this->pos;
|
$endAttributes['endTokenPos'] = $this->pos;
|
||||||
}
|
}
|
||||||
if ($this->attributeEndFilePosUsed) {
|
if ($this->attributeEndFilePosUsed) {
|
||||||
$endAttributes['endFilePos'] = $this->filePos - 1;
|
$endAttributes['endFilePos'] = ($nextToken ? $nextToken->pos : $token->getEndPos()) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \RuntimeException('Reached end of lexer loop');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the token array for current code.
|
* Returns the token array for current code.
|
||||||
*
|
*
|
||||||
* The token array is in the same format as provided by the
|
* The token array is in the same format as provided by the PhpToken::tokenize() method in
|
||||||
* token_get_all() function and does not discard tokens (i.e.
|
* PHP 8.0. The tokens are instances of PhpParser\Token, to abstract over a polyfill
|
||||||
* whitespace and comments are included). The token position
|
* implementation in earlier PHP version.
|
||||||
* attributes are against this token array.
|
|
||||||
*
|
*
|
||||||
* @return array Array of tokens in token_get_all() format
|
* The token array is terminated by a sentinel token with token ID 0.
|
||||||
|
* The token array does not discard any tokens (i.e. whitespace and comments are included).
|
||||||
|
* The token position attributes are against this token array.
|
||||||
|
*
|
||||||
|
* @return Token[] Array of tokens
|
||||||
*/
|
*/
|
||||||
public function getTokens() : array {
|
public function getTokens(): array {
|
||||||
return $this->tokens;
|
return $this->tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,149 +265,12 @@ class Lexer
|
|||||||
*
|
*
|
||||||
* @return string Remaining text
|
* @return string Remaining text
|
||||||
*/
|
*/
|
||||||
public function handleHaltCompiler() : string {
|
public function handleHaltCompiler(): string {
|
||||||
// text after T_HALT_COMPILER, still including ();
|
// Prevent the lexer from returning any further tokens.
|
||||||
$textAfter = substr($this->code, $this->filePos);
|
$nextToken = $this->tokens[$this->pos + 1];
|
||||||
|
$this->pos = \count($this->tokens) - 2;
|
||||||
|
|
||||||
// ensure that it is followed by ();
|
// Return text after __halt_compiler.
|
||||||
// this simplifies the situation, by not allowing any comments
|
return $nextToken->id === \T_INLINE_HTML ? $nextToken->text : '';
|
||||||
// in between of the tokens.
|
|
||||||
if (!preg_match('~^\s*\(\s*\)\s*(?:;|\?>\r?\n?)~', $textAfter, $matches)) {
|
|
||||||
throw new Error('__HALT_COMPILER must be followed by "();"');
|
|
||||||
}
|
|
||||||
|
|
||||||
// prevent the lexer from returning any further tokens
|
|
||||||
$this->pos = count($this->tokens);
|
|
||||||
|
|
||||||
// return with (); removed
|
|
||||||
return substr($textAfter, strlen($matches[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private function defineCompatibilityTokens() {
|
|
||||||
static $compatTokensDefined = false;
|
|
||||||
if ($compatTokensDefined) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$compatTokens = [
|
|
||||||
// PHP 7.4
|
|
||||||
'T_BAD_CHARACTER',
|
|
||||||
'T_FN',
|
|
||||||
'T_COALESCE_EQUAL',
|
|
||||||
// PHP 8.0
|
|
||||||
'T_NAME_QUALIFIED',
|
|
||||||
'T_NAME_FULLY_QUALIFIED',
|
|
||||||
'T_NAME_RELATIVE',
|
|
||||||
'T_MATCH',
|
|
||||||
'T_NULLSAFE_OBJECT_OPERATOR',
|
|
||||||
'T_ATTRIBUTE',
|
|
||||||
];
|
|
||||||
|
|
||||||
// PHP-Parser might be used together with another library that also emulates some or all
|
|
||||||
// of these tokens. Perform a sanity-check that all already defined tokens have been
|
|
||||||
// assigned a unique ID.
|
|
||||||
$usedTokenIds = [];
|
|
||||||
foreach ($compatTokens as $token) {
|
|
||||||
if (\defined($token)) {
|
|
||||||
$tokenId = \constant($token);
|
|
||||||
$clashingToken = $usedTokenIds[$tokenId] ?? null;
|
|
||||||
if ($clashingToken !== null) {
|
|
||||||
throw new \Error(sprintf(
|
|
||||||
'Token %s has same ID as token %s, ' .
|
|
||||||
'you may be using a library with broken token emulation',
|
|
||||||
$token, $clashingToken
|
|
||||||
));
|
|
||||||
}
|
|
||||||
$usedTokenIds[$tokenId] = $token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now define any tokens that have not yet been emulated. Try to assign IDs from -1
|
|
||||||
// downwards, but skip any IDs that may already be in use.
|
|
||||||
$newTokenId = -1;
|
|
||||||
foreach ($compatTokens as $token) {
|
|
||||||
if (!\defined($token)) {
|
|
||||||
while (isset($usedTokenIds[$newTokenId])) {
|
|
||||||
$newTokenId--;
|
|
||||||
}
|
|
||||||
\define($token, $newTokenId);
|
|
||||||
$newTokenId--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$compatTokensDefined = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the token map.
|
|
||||||
*
|
|
||||||
* The token map maps the PHP internal token identifiers
|
|
||||||
* to the identifiers used by the Parser. Additionally it
|
|
||||||
* maps T_OPEN_TAG_WITH_ECHO to T_ECHO and T_CLOSE_TAG to ';'.
|
|
||||||
*
|
|
||||||
* @return array The token map
|
|
||||||
*/
|
|
||||||
protected function createTokenMap() : array {
|
|
||||||
$tokenMap = [];
|
|
||||||
|
|
||||||
// 256 is the minimum possible token number, as everything below
|
|
||||||
// it is an ASCII value
|
|
||||||
for ($i = 256; $i < 1000; ++$i) {
|
|
||||||
if (\T_DOUBLE_COLON === $i) {
|
|
||||||
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
|
|
||||||
$tokenMap[$i] = Tokens::T_PAAMAYIM_NEKUDOTAYIM;
|
|
||||||
} elseif(\T_OPEN_TAG_WITH_ECHO === $i) {
|
|
||||||
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
|
|
||||||
$tokenMap[$i] = Tokens::T_ECHO;
|
|
||||||
} elseif(\T_CLOSE_TAG === $i) {
|
|
||||||
// T_CLOSE_TAG is equivalent to ';'
|
|
||||||
$tokenMap[$i] = ord(';');
|
|
||||||
} elseif ('UNKNOWN' !== $name = token_name($i)) {
|
|
||||||
if ('T_HASHBANG' === $name) {
|
|
||||||
// HHVM uses a special token for #! hashbang lines
|
|
||||||
$tokenMap[$i] = Tokens::T_INLINE_HTML;
|
|
||||||
} elseif (defined($name = Tokens::class . '::' . $name)) {
|
|
||||||
// Other tokens can be mapped directly
|
|
||||||
$tokenMap[$i] = constant($name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HHVM uses a special token for numbers that overflow to double
|
|
||||||
if (defined('T_ONUMBER')) {
|
|
||||||
$tokenMap[\T_ONUMBER] = Tokens::T_DNUMBER;
|
|
||||||
}
|
|
||||||
// HHVM also has a separate token for the __COMPILER_HALT_OFFSET__ constant
|
|
||||||
if (defined('T_COMPILER_HALT_OFFSET')) {
|
|
||||||
$tokenMap[\T_COMPILER_HALT_OFFSET] = Tokens::T_STRING;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign tokens for which we define compatibility constants, as token_name() does not know them.
|
|
||||||
$tokenMap[\T_FN] = Tokens::T_FN;
|
|
||||||
$tokenMap[\T_COALESCE_EQUAL] = Tokens::T_COALESCE_EQUAL;
|
|
||||||
$tokenMap[\T_NAME_QUALIFIED] = Tokens::T_NAME_QUALIFIED;
|
|
||||||
$tokenMap[\T_NAME_FULLY_QUALIFIED] = Tokens::T_NAME_FULLY_QUALIFIED;
|
|
||||||
$tokenMap[\T_NAME_RELATIVE] = Tokens::T_NAME_RELATIVE;
|
|
||||||
$tokenMap[\T_MATCH] = Tokens::T_MATCH;
|
|
||||||
$tokenMap[\T_NULLSAFE_OBJECT_OPERATOR] = Tokens::T_NULLSAFE_OBJECT_OPERATOR;
|
|
||||||
$tokenMap[\T_ATTRIBUTE] = Tokens::T_ATTRIBUTE;
|
|
||||||
|
|
||||||
return $tokenMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createIdentifierTokenMap(): array {
|
|
||||||
// Based on semi_reserved production.
|
|
||||||
return array_fill_keys([
|
|
||||||
\T_STRING,
|
|
||||||
\T_STATIC, \T_ABSTRACT, \T_FINAL, \T_PRIVATE, \T_PROTECTED, \T_PUBLIC,
|
|
||||||
\T_INCLUDE, \T_INCLUDE_ONCE, \T_EVAL, \T_REQUIRE, \T_REQUIRE_ONCE, \T_LOGICAL_OR, \T_LOGICAL_XOR, \T_LOGICAL_AND,
|
|
||||||
\T_INSTANCEOF, \T_NEW, \T_CLONE, \T_EXIT, \T_IF, \T_ELSEIF, \T_ELSE, \T_ENDIF, \T_ECHO, \T_DO, \T_WHILE,
|
|
||||||
\T_ENDWHILE, \T_FOR, \T_ENDFOR, \T_FOREACH, \T_ENDFOREACH, \T_DECLARE, \T_ENDDECLARE, \T_AS, \T_TRY, \T_CATCH,
|
|
||||||
\T_FINALLY, \T_THROW, \T_USE, \T_INSTEADOF, \T_GLOBAL, \T_VAR, \T_UNSET, \T_ISSET, \T_EMPTY, \T_CONTINUE, \T_GOTO,
|
|
||||||
\T_FUNCTION, \T_CONST, \T_RETURN, \T_PRINT, \T_YIELD, \T_LIST, \T_SWITCH, \T_ENDSWITCH, \T_CASE, \T_DEFAULT,
|
|
||||||
\T_BREAK, \T_ARRAY, \T_CALLABLE, \T_EXTENDS, \T_IMPLEMENTS, \T_NAMESPACE, \T_TRAIT, \T_INTERFACE, \T_CLASS,
|
|
||||||
\T_CLASS_C, \T_TRAIT_C, \T_FUNC_C, \T_METHOD_C, \T_LINE, \T_FILE, \T_DIR, \T_NS_C, \T_HALT_COMPILER, \T_FN,
|
|
||||||
\T_MATCH,
|
|
||||||
], true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,39 +6,44 @@ use PhpParser\Error;
|
|||||||
use PhpParser\ErrorHandler;
|
use PhpParser\ErrorHandler;
|
||||||
use PhpParser\Lexer;
|
use PhpParser\Lexer;
|
||||||
use PhpParser\Lexer\TokenEmulator\AttributeEmulator;
|
use PhpParser\Lexer\TokenEmulator\AttributeEmulator;
|
||||||
|
use PhpParser\Lexer\TokenEmulator\EnumTokenEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
|
use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
|
||||||
|
use PhpParser\Lexer\TokenEmulator\ExplicitOctalEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\FlexibleDocStringEmulator;
|
use PhpParser\Lexer\TokenEmulator\FlexibleDocStringEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
|
use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\MatchTokenEmulator;
|
use PhpParser\Lexer\TokenEmulator\MatchTokenEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\NullsafeTokenEmulator;
|
use PhpParser\Lexer\TokenEmulator\NullsafeTokenEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\NumericLiteralSeparatorEmulator;
|
use PhpParser\Lexer\TokenEmulator\NumericLiteralSeparatorEmulator;
|
||||||
|
use PhpParser\Lexer\TokenEmulator\ReadonlyFunctionTokenEmulator;
|
||||||
|
use PhpParser\Lexer\TokenEmulator\ReadonlyTokenEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\ReverseEmulator;
|
use PhpParser\Lexer\TokenEmulator\ReverseEmulator;
|
||||||
use PhpParser\Lexer\TokenEmulator\TokenEmulator;
|
use PhpParser\Lexer\TokenEmulator\TokenEmulator;
|
||||||
use PhpParser\Parser\Tokens;
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
class Emulative extends Lexer
|
class Emulative extends Lexer {
|
||||||
{
|
/** @var array{int, string, string}[] Patches used to reverse changes introduced in the code */
|
||||||
const PHP_7_3 = '7.3dev';
|
|
||||||
const PHP_7_4 = '7.4dev';
|
|
||||||
const PHP_8_0 = '8.0dev';
|
|
||||||
|
|
||||||
/** @var mixed[] Patches used to reverse changes introduced in the code */
|
|
||||||
private $patches = [];
|
private $patches = [];
|
||||||
|
|
||||||
/** @var TokenEmulator[] */
|
/** @var list<TokenEmulator> */
|
||||||
private $emulators = [];
|
private $emulators = [];
|
||||||
|
|
||||||
/** @var string */
|
/** @var PhpVersion */
|
||||||
private $targetPhpVersion;
|
private $targetPhpVersion;
|
||||||
|
/** @var PhpVersion */
|
||||||
|
private $hostPhpVersion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed[] $options Lexer options. In addition to the usual options,
|
* @param array{usedAttributes?: string[], phpVersion?: PhpVersion|string} $options Lexer options.
|
||||||
* accepts a 'phpVersion' string that specifies the
|
* In addition to the usual options, accepts a 'phpVersion' (PhpVersion object or string)
|
||||||
* version to emulated. Defaults to newest supported.
|
* that specifies the version to emulate. Defaults to newest supported.
|
||||||
*/
|
*/
|
||||||
public function __construct(array $options = [])
|
public function __construct(array $options = []) {
|
||||||
{
|
$version = $options['phpVersion'] ?? PhpVersion::getNewestSupported();
|
||||||
$this->targetPhpVersion = $options['phpVersion'] ?? Emulative::PHP_8_0;
|
if (!$version instanceof PhpVersion) {
|
||||||
|
$version = PhpVersion::fromString($version);
|
||||||
|
}
|
||||||
|
$this->targetPhpVersion = $version;
|
||||||
|
$this->hostPhpVersion = PhpVersion::getHostVersion();
|
||||||
unset($options['phpVersion']);
|
unset($options['phpVersion']);
|
||||||
|
|
||||||
parent::__construct($options);
|
parent::__construct($options);
|
||||||
@ -51,6 +56,10 @@ class Emulative extends Lexer
|
|||||||
new NumericLiteralSeparatorEmulator(),
|
new NumericLiteralSeparatorEmulator(),
|
||||||
new NullsafeTokenEmulator(),
|
new NullsafeTokenEmulator(),
|
||||||
new AttributeEmulator(),
|
new AttributeEmulator(),
|
||||||
|
new EnumTokenEmulator(),
|
||||||
|
new ReadonlyTokenEmulator(),
|
||||||
|
new ExplicitOctalEmulator(),
|
||||||
|
new ReadonlyFunctionTokenEmulator(),
|
||||||
];
|
];
|
||||||
|
|
||||||
// Collect emulators that are relevant for the PHP version we're running
|
// Collect emulators that are relevant for the PHP version we're running
|
||||||
@ -59,14 +68,14 @@ class Emulative extends Lexer
|
|||||||
$emulatorPhpVersion = $emulator->getPhpVersion();
|
$emulatorPhpVersion = $emulator->getPhpVersion();
|
||||||
if ($this->isForwardEmulationNeeded($emulatorPhpVersion)) {
|
if ($this->isForwardEmulationNeeded($emulatorPhpVersion)) {
|
||||||
$this->emulators[] = $emulator;
|
$this->emulators[] = $emulator;
|
||||||
} else if ($this->isReverseEmulationNeeded($emulatorPhpVersion)) {
|
} elseif ($this->isReverseEmulationNeeded($emulatorPhpVersion)) {
|
||||||
$this->emulators[] = new ReverseEmulator($emulator);
|
$this->emulators[] = new ReverseEmulator($emulator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function startLexing(string $code, ErrorHandler $errorHandler = null) {
|
public function startLexing(string $code, ?ErrorHandler $errorHandler = null): void {
|
||||||
$emulators = array_filter($this->emulators, function($emulator) use($code) {
|
$emulators = array_filter($this->emulators, function ($emulator) use ($code) {
|
||||||
return $emulator->isEmulationNeeded($code);
|
return $emulator->isEmulationNeeded($code);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -99,61 +108,41 @@ class Emulative extends Lexer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isForwardEmulationNeeded(string $emulatorPhpVersion): bool {
|
private function isForwardEmulationNeeded(PhpVersion $emulatorPhpVersion): bool {
|
||||||
return version_compare(\PHP_VERSION, $emulatorPhpVersion, '<')
|
return $this->hostPhpVersion->older($emulatorPhpVersion)
|
||||||
&& version_compare($this->targetPhpVersion, $emulatorPhpVersion, '>=');
|
&& $this->targetPhpVersion->newerOrEqual($emulatorPhpVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isReverseEmulationNeeded(string $emulatorPhpVersion): bool {
|
private function isReverseEmulationNeeded(PhpVersion $emulatorPhpVersion): bool {
|
||||||
return version_compare(\PHP_VERSION, $emulatorPhpVersion, '>=')
|
return $this->hostPhpVersion->newerOrEqual($emulatorPhpVersion)
|
||||||
&& version_compare($this->targetPhpVersion, $emulatorPhpVersion, '<');
|
&& $this->targetPhpVersion->older($emulatorPhpVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sortPatches()
|
private function sortPatches(): void {
|
||||||
{
|
|
||||||
// Patches may be contributed by different emulators.
|
// Patches may be contributed by different emulators.
|
||||||
// Make sure they are sorted by increasing patch position.
|
// Make sure they are sorted by increasing patch position.
|
||||||
usort($this->patches, function($p1, $p2) {
|
usort($this->patches, function ($p1, $p2) {
|
||||||
return $p1[0] <=> $p2[0];
|
return $p1[0] <=> $p2[0];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private function fixupTokens()
|
private function fixupTokens(): void {
|
||||||
{
|
|
||||||
if (\count($this->patches) === 0) {
|
if (\count($this->patches) === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load first patch
|
// Load first patch
|
||||||
$patchIdx = 0;
|
$patchIdx = 0;
|
||||||
|
|
||||||
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
||||||
|
|
||||||
// We use a manual loop over the tokens, because we modify the array on the fly
|
// We use a manual loop over the tokens, because we modify the array on the fly
|
||||||
$pos = 0;
|
$posDelta = 0;
|
||||||
for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
|
for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
|
||||||
$token = $this->tokens[$i];
|
$token = $this->tokens[$i];
|
||||||
if (\is_string($token)) {
|
$pos = $token->pos;
|
||||||
if ($patchPos === $pos) {
|
$token->pos += $posDelta;
|
||||||
// Only support replacement for string tokens.
|
$localPosDelta = 0;
|
||||||
assert($patchType === 'replace');
|
$len = \strlen($token->text);
|
||||||
$this->tokens[$i] = $patchText;
|
|
||||||
|
|
||||||
// Fetch the next patch
|
|
||||||
$patchIdx++;
|
|
||||||
if ($patchIdx >= \count($this->patches)) {
|
|
||||||
// No more patches, we're done
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
|
||||||
}
|
|
||||||
|
|
||||||
$pos += \strlen($token);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$len = \strlen($token[1]);
|
|
||||||
$posDelta = 0;
|
|
||||||
while ($patchPos >= $pos && $patchPos < $pos + $len) {
|
while ($patchPos >= $pos && $patchPos < $pos + $len) {
|
||||||
$patchTextLen = \strlen($patchText);
|
$patchTextLen = \strlen($patchText);
|
||||||
if ($patchType === 'remove') {
|
if ($patchType === 'remove') {
|
||||||
@ -164,21 +153,21 @@ class Emulative extends Lexer
|
|||||||
$c--;
|
$c--;
|
||||||
} else {
|
} else {
|
||||||
// Remove from token string
|
// Remove from token string
|
||||||
$this->tokens[$i][1] = substr_replace(
|
$token->text = substr_replace(
|
||||||
$token[1], '', $patchPos - $pos + $posDelta, $patchTextLen
|
$token->text, '', $patchPos - $pos + $localPosDelta, $patchTextLen
|
||||||
);
|
);
|
||||||
$posDelta -= $patchTextLen;
|
$localPosDelta -= $patchTextLen;
|
||||||
}
|
}
|
||||||
} elseif ($patchType === 'add') {
|
} elseif ($patchType === 'add') {
|
||||||
// Insert into the token string
|
// Insert into the token string
|
||||||
$this->tokens[$i][1] = substr_replace(
|
$token->text = substr_replace(
|
||||||
$token[1], $patchText, $patchPos - $pos + $posDelta, 0
|
$token->text, $patchText, $patchPos - $pos + $localPosDelta, 0
|
||||||
);
|
);
|
||||||
$posDelta += $patchTextLen;
|
$localPosDelta += $patchTextLen;
|
||||||
} else if ($patchType === 'replace') {
|
} elseif ($patchType === 'replace') {
|
||||||
// Replace inside the token string
|
// Replace inside the token string
|
||||||
$this->tokens[$i][1] = substr_replace(
|
$token->text = substr_replace(
|
||||||
$token[1], $patchText, $patchPos - $pos + $posDelta, $patchTextLen
|
$token->text, $patchText, $patchPos - $pos + $localPosDelta, $patchTextLen
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
assert(false);
|
assert(false);
|
||||||
@ -187,22 +176,16 @@ class Emulative extends Lexer
|
|||||||
// Fetch the next patch
|
// Fetch the next patch
|
||||||
$patchIdx++;
|
$patchIdx++;
|
||||||
if ($patchIdx >= \count($this->patches)) {
|
if ($patchIdx >= \count($this->patches)) {
|
||||||
// No more patches, we're done
|
// No more patches. However, we still need to adjust position.
|
||||||
return;
|
$patchPos = \PHP_INT_MAX;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
||||||
|
|
||||||
// Multiple patches may apply to the same token. Reload the current one to check
|
|
||||||
// If the new patch applies
|
|
||||||
$token = $this->tokens[$i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$pos += $len;
|
$posDelta += $localPosDelta;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A patch did not apply
|
|
||||||
assert(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -210,7 +193,7 @@ class Emulative extends Lexer
|
|||||||
*
|
*
|
||||||
* @param Error[] $errors
|
* @param Error[] $errors
|
||||||
*/
|
*/
|
||||||
private function fixupErrors(array $errors) {
|
private function fixupErrors(array $errors): void {
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
$attrs = $error->getAttributes();
|
$attrs = $error->getAttributes();
|
||||||
|
|
||||||
@ -226,7 +209,7 @@ class Emulative extends Lexer
|
|||||||
if ($patchType === 'add') {
|
if ($patchType === 'add') {
|
||||||
$posDelta += strlen($patchText);
|
$posDelta += strlen($patchText);
|
||||||
$lineDelta += substr_count($patchText, "\n");
|
$lineDelta += substr_count($patchText, "\n");
|
||||||
} else if ($patchType === 'remove') {
|
} elseif ($patchType === 'remove') {
|
||||||
$posDelta -= strlen($patchText);
|
$posDelta -= strlen($patchText);
|
||||||
$lineDelta -= substr_count($patchText, "\n");
|
$lineDelta -= substr_count($patchText, "\n");
|
||||||
}
|
}
|
||||||
|
@ -2,43 +2,36 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
final class AttributeEmulator extends TokenEmulator
|
final class AttributeEmulator extends TokenEmulator {
|
||||||
{
|
public function getPhpVersion(): PhpVersion {
|
||||||
public function getPhpVersion(): string
|
return PhpVersion::fromComponents(8, 0);
|
||||||
{
|
|
||||||
return Emulative::PHP_8_0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmulationNeeded(string $code) : bool
|
public function isEmulationNeeded(string $code): bool {
|
||||||
{
|
|
||||||
return strpos($code, '#[') !== false;
|
return strpos($code, '#[') !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function emulate(string $code, array $tokens): array
|
public function emulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// We need to manually iterate and manage a count because we'll change
|
// We need to manually iterate and manage a count because we'll change
|
||||||
// the tokens array on the way.
|
// the tokens array on the way.
|
||||||
$line = 1;
|
|
||||||
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
||||||
if ($tokens[$i] === '#' && isset($tokens[$i + 1]) && $tokens[$i + 1] === '[') {
|
$token = $tokens[$i];
|
||||||
|
if ($token->text === '#' && isset($tokens[$i + 1]) && $tokens[$i + 1]->text === '[') {
|
||||||
array_splice($tokens, $i, 2, [
|
array_splice($tokens, $i, 2, [
|
||||||
[\T_ATTRIBUTE, '#[', $line]
|
new Token(\T_ATTRIBUTE, '#[', $token->line, $token->pos),
|
||||||
]);
|
]);
|
||||||
$c--;
|
$c--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (\is_array($tokens[$i])) {
|
|
||||||
$line += substr_count($tokens[$i][1], "\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseEmulate(string $code, array $tokens): array
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// TODO
|
// TODO
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
@ -2,45 +2,38 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
final class CoaleseEqualTokenEmulator extends TokenEmulator
|
final class CoaleseEqualTokenEmulator extends TokenEmulator {
|
||||||
{
|
public function getPhpVersion(): PhpVersion {
|
||||||
public function getPhpVersion(): string
|
return PhpVersion::fromComponents(7, 4);
|
||||||
{
|
|
||||||
return Emulative::PHP_7_4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmulationNeeded(string $code): bool
|
public function isEmulationNeeded(string $code): bool {
|
||||||
{
|
|
||||||
return strpos($code, '??=') !== false;
|
return strpos($code, '??=') !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function emulate(string $code, array $tokens): array
|
public function emulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// We need to manually iterate and manage a count because we'll change
|
// We need to manually iterate and manage a count because we'll change
|
||||||
// the tokens array on the way
|
// the tokens array on the way
|
||||||
$line = 1;
|
|
||||||
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
||||||
|
$token = $tokens[$i];
|
||||||
if (isset($tokens[$i + 1])) {
|
if (isset($tokens[$i + 1])) {
|
||||||
if ($tokens[$i][0] === T_COALESCE && $tokens[$i + 1] === '=') {
|
if ($token->id === T_COALESCE && $tokens[$i + 1]->text === '=') {
|
||||||
array_splice($tokens, $i, 2, [
|
array_splice($tokens, $i, 2, [
|
||||||
[\T_COALESCE_EQUAL, '??=', $line]
|
new Token(\T_COALESCE_EQUAL, '??=', $token->line, $token->pos),
|
||||||
]);
|
]);
|
||||||
$c--;
|
$c--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (\is_array($tokens[$i])) {
|
|
||||||
$line += substr_count($tokens[$i][1], "\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseEmulate(string $code, array $tokens): array
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// ??= was not valid code previously, don't bother.
|
// ??= was not valid code previously, don't bother.
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
26
lib/PhpParser/Lexer/TokenEmulator/EnumTokenEmulator.php
Normal file
26
lib/PhpParser/Lexer/TokenEmulator/EnumTokenEmulator.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
|
final class EnumTokenEmulator extends KeywordEmulator {
|
||||||
|
public function getPhpVersion(): PhpVersion {
|
||||||
|
return PhpVersion::fromComponents(8, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKeywordString(): string {
|
||||||
|
return 'enum';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKeywordToken(): int {
|
||||||
|
return \T_ENUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function isKeywordContext(array $tokens, int $pos): bool {
|
||||||
|
return parent::isKeywordContext($tokens, $pos)
|
||||||
|
&& isset($tokens[$pos + 2])
|
||||||
|
&& $tokens[$pos + 1]->id === \T_WHITESPACE
|
||||||
|
&& $tokens[$pos + 2]->id === \T_STRING;
|
||||||
|
}
|
||||||
|
}
|
45
lib/PhpParser/Lexer/TokenEmulator/ExplicitOctalEmulator.php
Normal file
45
lib/PhpParser/Lexer/TokenEmulator/ExplicitOctalEmulator.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
|
class ExplicitOctalEmulator extends TokenEmulator {
|
||||||
|
public function getPhpVersion(): PhpVersion {
|
||||||
|
return PhpVersion::fromComponents(8, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isEmulationNeeded(string $code): bool {
|
||||||
|
return strpos($code, '0o') !== false || strpos($code, '0O') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function emulate(string $code, array $tokens): array {
|
||||||
|
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
||||||
|
$token = $tokens[$i];
|
||||||
|
if ($token->id == \T_LNUMBER && $token->text === '0' &&
|
||||||
|
isset($tokens[$i + 1]) && $tokens[$i + 1]->id == \T_STRING &&
|
||||||
|
preg_match('/[oO][0-7]+(?:_[0-7]+)*/', $tokens[$i + 1]->text)
|
||||||
|
) {
|
||||||
|
$tokenKind = $this->resolveIntegerOrFloatToken($tokens[$i + 1]->text);
|
||||||
|
array_splice($tokens, $i, 2, [
|
||||||
|
new Token($tokenKind, '0' . $tokens[$i + 1]->text, $token->line, $token->pos),
|
||||||
|
]);
|
||||||
|
$c--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveIntegerOrFloatToken(string $str): int {
|
||||||
|
$str = substr($str, 1);
|
||||||
|
$str = str_replace('_', '', $str);
|
||||||
|
$num = octdec($str);
|
||||||
|
return is_float($num) ? \T_DNUMBER : \T_LNUMBER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
|
// Explicit octals were not legal code previously, don't bother.
|
||||||
|
return $tokens;
|
||||||
|
}
|
||||||
|
}
|
@ -2,34 +2,29 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
final class FlexibleDocStringEmulator extends TokenEmulator
|
final class FlexibleDocStringEmulator extends TokenEmulator {
|
||||||
{
|
private const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX'
|
||||||
const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX'
|
|
||||||
/<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
|
/<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
|
||||||
(?:.*\r?\n)*?
|
(?:.*\r?\n)*?
|
||||||
(?<indentation>\h*)\2(?![a-zA-Z0-9_\x80-\xff])(?<separator>(?:;?[\r\n])?)/x
|
(?<indentation>\h*)\2(?![a-zA-Z0-9_\x80-\xff])(?<separator>(?:;?[\r\n])?)/x
|
||||||
REGEX;
|
REGEX;
|
||||||
|
|
||||||
public function getPhpVersion(): string
|
public function getPhpVersion(): PhpVersion {
|
||||||
{
|
return PhpVersion::fromComponents(7, 3);
|
||||||
return Emulative::PHP_7_3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmulationNeeded(string $code) : bool
|
public function isEmulationNeeded(string $code): bool {
|
||||||
{
|
|
||||||
return strpos($code, '<<<') !== false;
|
return strpos($code, '<<<') !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function emulate(string $code, array $tokens): array
|
public function emulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// Handled by preprocessing + fixup.
|
// Handled by preprocessing + fixup.
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseEmulate(string $code, array $tokens): array
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// Not supported.
|
// Not supported.
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
@ -2,22 +2,18 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
final class FnTokenEmulator extends KeywordEmulator
|
final class FnTokenEmulator extends KeywordEmulator {
|
||||||
{
|
public function getPhpVersion(): PhpVersion {
|
||||||
public function getPhpVersion(): string
|
return PhpVersion::fromComponents(7, 4);
|
||||||
{
|
|
||||||
return Emulative::PHP_7_4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKeywordString(): string
|
public function getKeywordString(): string {
|
||||||
{
|
|
||||||
return 'fn';
|
return 'fn';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKeywordToken(): int
|
public function getKeywordToken(): int {
|
||||||
{
|
|
||||||
return \T_FN;
|
return \T_FN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,41 +2,38 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
abstract class KeywordEmulator extends TokenEmulator
|
use PhpParser\Token;
|
||||||
{
|
|
||||||
abstract function getKeywordString(): string;
|
|
||||||
abstract function getKeywordToken(): int;
|
|
||||||
|
|
||||||
public function isEmulationNeeded(string $code): bool
|
abstract class KeywordEmulator extends TokenEmulator {
|
||||||
{
|
abstract public function getKeywordString(): string;
|
||||||
|
abstract public function getKeywordToken(): int;
|
||||||
|
|
||||||
|
public function isEmulationNeeded(string $code): bool {
|
||||||
return strpos(strtolower($code), $this->getKeywordString()) !== false;
|
return strpos(strtolower($code), $this->getKeywordString()) !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function emulate(string $code, array $tokens): array
|
/** @param Token[] $tokens */
|
||||||
{
|
protected function isKeywordContext(array $tokens, int $pos): bool {
|
||||||
|
$previousNonSpaceToken = $this->getPreviousNonSpaceToken($tokens, $pos);
|
||||||
|
return $previousNonSpaceToken === null || $previousNonSpaceToken->id !== \T_OBJECT_OPERATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function emulate(string $code, array $tokens): array {
|
||||||
$keywordString = $this->getKeywordString();
|
$keywordString = $this->getKeywordString();
|
||||||
foreach ($tokens as $i => $token) {
|
foreach ($tokens as $i => $token) {
|
||||||
if ($token[0] === T_STRING && strtolower($token[1]) === $keywordString) {
|
if ($token->id === T_STRING && strtolower($token->text) === $keywordString
|
||||||
$previousNonSpaceToken = $this->getPreviousNonSpaceToken($tokens, $i);
|
&& $this->isKeywordContext($tokens, $i)) {
|
||||||
if ($previousNonSpaceToken !== null && $previousNonSpaceToken[0] === \T_OBJECT_OPERATOR) {
|
$token->id = $this->getKeywordToken();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tokens[$i][0] = $this->getKeywordToken();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @param Token[] $tokens */
|
||||||
* @param mixed[] $tokens
|
private function getPreviousNonSpaceToken(array $tokens, int $start): ?Token {
|
||||||
* @return mixed[]|null
|
|
||||||
*/
|
|
||||||
private function getPreviousNonSpaceToken(array $tokens, int $start)
|
|
||||||
{
|
|
||||||
for ($i = $start - 1; $i >= 0; --$i) {
|
for ($i = $start - 1; $i >= 0; --$i) {
|
||||||
if ($tokens[$i][0] === T_WHITESPACE) {
|
if ($tokens[$i]->id === T_WHITESPACE) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,12 +43,11 @@ abstract class KeywordEmulator extends TokenEmulator
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseEmulate(string $code, array $tokens): array
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
$keywordToken = $this->getKeywordToken();
|
$keywordToken = $this->getKeywordToken();
|
||||||
foreach ($tokens as $i => $token) {
|
foreach ($tokens as $i => $token) {
|
||||||
if ($token[0] === $keywordToken) {
|
if ($token->id === $keywordToken) {
|
||||||
$tokens[$i][0] = \T_STRING;
|
$token->id = \T_STRING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,22 +2,18 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
final class MatchTokenEmulator extends KeywordEmulator
|
final class MatchTokenEmulator extends KeywordEmulator {
|
||||||
{
|
public function getPhpVersion(): PhpVersion {
|
||||||
public function getPhpVersion(): string
|
return PhpVersion::fromComponents(8, 0);
|
||||||
{
|
|
||||||
return Emulative::PHP_8_0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKeywordString(): string
|
public function getKeywordString(): string {
|
||||||
{
|
|
||||||
return 'match';
|
return 'match';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKeywordToken(): int
|
public function getKeywordToken(): int {
|
||||||
{
|
|
||||||
return \T_MATCH;
|
return \T_MATCH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,65 +2,58 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
final class NullsafeTokenEmulator extends TokenEmulator
|
final class NullsafeTokenEmulator extends TokenEmulator {
|
||||||
{
|
public function getPhpVersion(): PhpVersion {
|
||||||
public function getPhpVersion(): string
|
return PhpVersion::fromComponents(8, 0);
|
||||||
{
|
|
||||||
return Emulative::PHP_8_0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmulationNeeded(string $code): bool
|
public function isEmulationNeeded(string $code): bool {
|
||||||
{
|
|
||||||
return strpos($code, '?->') !== false;
|
return strpos($code, '?->') !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function emulate(string $code, array $tokens): array
|
public function emulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// We need to manually iterate and manage a count because we'll change
|
// We need to manually iterate and manage a count because we'll change
|
||||||
// the tokens array on the way
|
// the tokens array on the way
|
||||||
$line = 1;
|
|
||||||
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
||||||
if ($tokens[$i] === '?' && isset($tokens[$i + 1]) && $tokens[$i + 1][0] === \T_OBJECT_OPERATOR) {
|
$token = $tokens[$i];
|
||||||
|
if ($token->text === '?' && isset($tokens[$i + 1]) && $tokens[$i + 1]->id === \T_OBJECT_OPERATOR) {
|
||||||
array_splice($tokens, $i, 2, [
|
array_splice($tokens, $i, 2, [
|
||||||
[\T_NULLSAFE_OBJECT_OPERATOR, '?->', $line]
|
new Token(\T_NULLSAFE_OBJECT_OPERATOR, '?->', $token->line, $token->pos),
|
||||||
]);
|
]);
|
||||||
$c--;
|
$c--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle ?-> inside encapsed string.
|
// Handle ?-> inside encapsed string.
|
||||||
if ($tokens[$i][0] === \T_ENCAPSED_AND_WHITESPACE && isset($tokens[$i - 1])
|
if ($token->id === \T_ENCAPSED_AND_WHITESPACE && isset($tokens[$i - 1])
|
||||||
&& $tokens[$i - 1][0] === \T_VARIABLE
|
&& $tokens[$i - 1]->id === \T_VARIABLE
|
||||||
&& preg_match('/^\?->([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)/', $tokens[$i][1], $matches)
|
&& preg_match('/^\?->([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)/', $token->text, $matches)
|
||||||
) {
|
) {
|
||||||
$replacement = [
|
$replacement = [
|
||||||
[\T_NULLSAFE_OBJECT_OPERATOR, '?->', $line],
|
new Token(\T_NULLSAFE_OBJECT_OPERATOR, '?->', $token->line, $token->pos),
|
||||||
[\T_STRING, $matches[1], $line],
|
new Token(\T_STRING, $matches[1], $token->line, $token->pos + 3),
|
||||||
];
|
];
|
||||||
if (\strlen($matches[0]) !== \strlen($tokens[$i][1])) {
|
$matchLen = \strlen($matches[0]);
|
||||||
$replacement[] = [
|
if ($matchLen !== \strlen($token->text)) {
|
||||||
|
$replacement[] = new Token(
|
||||||
\T_ENCAPSED_AND_WHITESPACE,
|
\T_ENCAPSED_AND_WHITESPACE,
|
||||||
\substr($tokens[$i][1], \strlen($matches[0])),
|
\substr($token->text, $matchLen),
|
||||||
$line
|
$token->line, $token->pos + $matchLen
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
array_splice($tokens, $i, 1, $replacement);
|
array_splice($tokens, $i, 1, $replacement);
|
||||||
$c += \count($replacement) - 1;
|
$c += \count($replacement) - 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\is_array($tokens[$i])) {
|
|
||||||
$line += substr_count($tokens[$i][1], "\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseEmulate(string $code, array $tokens): array
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// ?-> was not valid code previously, don't bother.
|
// ?-> was not valid code previously, don't bother.
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
@ -2,70 +2,63 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
use PhpParser\Lexer\Emulative;
|
use PhpParser\PhpVersion;
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
final class NumericLiteralSeparatorEmulator extends TokenEmulator
|
final class NumericLiteralSeparatorEmulator extends TokenEmulator {
|
||||||
{
|
private const BIN = '(?:0b[01]+(?:_[01]+)*)';
|
||||||
const BIN = '(?:0b[01]+(?:_[01]+)*)';
|
private const HEX = '(?:0x[0-9a-f]+(?:_[0-9a-f]+)*)';
|
||||||
const HEX = '(?:0x[0-9a-f]+(?:_[0-9a-f]+)*)';
|
private const DEC = '(?:[0-9]+(?:_[0-9]+)*)';
|
||||||
const DEC = '(?:[0-9]+(?:_[0-9]+)*)';
|
private const SIMPLE_FLOAT = '(?:' . self::DEC . '\.' . self::DEC . '?|\.' . self::DEC . ')';
|
||||||
const SIMPLE_FLOAT = '(?:' . self::DEC . '\.' . self::DEC . '?|\.' . self::DEC . ')';
|
private const EXP = '(?:e[+-]?' . self::DEC . ')';
|
||||||
const EXP = '(?:e[+-]?' . self::DEC . ')';
|
private const FLOAT = '(?:' . self::SIMPLE_FLOAT . self::EXP . '?|' . self::DEC . self::EXP . ')';
|
||||||
const FLOAT = '(?:' . self::SIMPLE_FLOAT . self::EXP . '?|' . self::DEC . self::EXP . ')';
|
private const NUMBER = '~' . self::FLOAT . '|' . self::BIN . '|' . self::HEX . '|' . self::DEC . '~iA';
|
||||||
const NUMBER = '~' . self::FLOAT . '|' . self::BIN . '|' . self::HEX . '|' . self::DEC . '~iA';
|
|
||||||
|
|
||||||
public function getPhpVersion(): string
|
public function getPhpVersion(): PhpVersion {
|
||||||
{
|
return PhpVersion::fromComponents(7, 4);
|
||||||
return Emulative::PHP_7_4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmulationNeeded(string $code) : bool
|
public function isEmulationNeeded(string $code): bool {
|
||||||
{
|
|
||||||
return preg_match('~[0-9]_[0-9]~', $code)
|
return preg_match('~[0-9]_[0-9]~', $code)
|
||||||
|| preg_match('~0x[0-9a-f]+_[0-9a-f]~i', $code);
|
|| preg_match('~0x[0-9a-f]+_[0-9a-f]~i', $code);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function emulate(string $code, array $tokens): array
|
public function emulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// We need to manually iterate and manage a count because we'll change
|
// We need to manually iterate and manage a count because we'll change
|
||||||
// the tokens array on the way
|
// the tokens array on the way
|
||||||
$codeOffset = 0;
|
|
||||||
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
||||||
$token = $tokens[$i];
|
$token = $tokens[$i];
|
||||||
$tokenLen = \strlen(\is_array($token) ? $token[1] : $token);
|
$tokenLen = \strlen($token->text);
|
||||||
|
|
||||||
if ($token[0] !== T_LNUMBER && $token[0] !== T_DNUMBER) {
|
if ($token->id !== \T_LNUMBER && $token->id !== \T_DNUMBER) {
|
||||||
$codeOffset += $tokenLen;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$res = preg_match(self::NUMBER, $code, $matches, 0, $codeOffset);
|
$res = preg_match(self::NUMBER, $code, $matches, 0, $token->pos);
|
||||||
assert($res, "No number at number token position");
|
assert($res, "No number at number token position");
|
||||||
|
|
||||||
$match = $matches[0];
|
$match = $matches[0];
|
||||||
$matchLen = \strlen($match);
|
$matchLen = \strlen($match);
|
||||||
if ($matchLen === $tokenLen) {
|
if ($matchLen === $tokenLen) {
|
||||||
// Original token already holds the full number.
|
// Original token already holds the full number.
|
||||||
$codeOffset += $tokenLen;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$tokenKind = $this->resolveIntegerOrFloatToken($match);
|
$tokenKind = $this->resolveIntegerOrFloatToken($match);
|
||||||
$newTokens = [[$tokenKind, $match, $token[2]]];
|
$newTokens = [new Token($tokenKind, $match, $token->line, $token->pos)];
|
||||||
|
|
||||||
$numTokens = 1;
|
$numTokens = 1;
|
||||||
$len = $tokenLen;
|
$len = $tokenLen;
|
||||||
while ($matchLen > $len) {
|
while ($matchLen > $len) {
|
||||||
$nextToken = $tokens[$i + $numTokens];
|
$nextToken = $tokens[$i + $numTokens];
|
||||||
$nextTokenText = \is_array($nextToken) ? $nextToken[1] : $nextToken;
|
$nextTokenText = $nextToken->text;
|
||||||
$nextTokenLen = \strlen($nextTokenText);
|
$nextTokenLen = \strlen($nextTokenText);
|
||||||
|
|
||||||
$numTokens++;
|
$numTokens++;
|
||||||
if ($matchLen < $len + $nextTokenLen) {
|
if ($matchLen < $len + $nextTokenLen) {
|
||||||
// Split trailing characters into a partial token.
|
// Split trailing characters into a partial token.
|
||||||
assert(is_array($nextToken), "Partial token should be an array token");
|
|
||||||
$partialText = substr($nextTokenText, $matchLen - $len);
|
$partialText = substr($nextTokenText, $matchLen - $len);
|
||||||
$newTokens[] = [$nextToken[0], $partialText, $nextToken[2]];
|
$newTokens[] = new Token($nextToken->id, $partialText, $nextToken->line, $nextToken->pos);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,14 +67,12 @@ final class NumericLiteralSeparatorEmulator extends TokenEmulator
|
|||||||
|
|
||||||
array_splice($tokens, $i, $numTokens, $newTokens);
|
array_splice($tokens, $i, $numTokens, $newTokens);
|
||||||
$c -= $numTokens - \count($newTokens);
|
$c -= $numTokens - \count($newTokens);
|
||||||
$codeOffset += $matchLen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function resolveIntegerOrFloatToken(string $str): int
|
private function resolveIntegerOrFloatToken(string $str): int {
|
||||||
{
|
|
||||||
$str = str_replace('_', '', $str);
|
$str = str_replace('_', '', $str);
|
||||||
|
|
||||||
if (stripos($str, '0b') === 0) {
|
if (stripos($str, '0b') === 0) {
|
||||||
@ -97,8 +88,7 @@ final class NumericLiteralSeparatorEmulator extends TokenEmulator
|
|||||||
return is_float($num) ? T_DNUMBER : T_LNUMBER;
|
return is_float($num) ? T_DNUMBER : T_LNUMBER;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseEmulate(string $code, array $tokens): array
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
{
|
|
||||||
// Numeric separators were not legal code previously, don't bother.
|
// Numeric separators were not legal code previously, don't bother.
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* In PHP 8.1, "readonly(" was special cased in the lexer in order to support functions with
|
||||||
|
* name readonly. In PHP 8.2, this may conflict with readonly properties having a DNF type. For
|
||||||
|
* this reason, PHP 8.2 instead treats this as T_READONLY and then handles it specially in the
|
||||||
|
* parser. This emulator only exists to handle this special case, which is skipped by the
|
||||||
|
* PHP 8.1 ReadonlyTokenEmulator.
|
||||||
|
*/
|
||||||
|
class ReadonlyFunctionTokenEmulator extends KeywordEmulator {
|
||||||
|
public function getKeywordString(): string {
|
||||||
|
return 'readonly';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKeywordToken(): int {
|
||||||
|
return \T_READONLY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPhpVersion(): PhpVersion {
|
||||||
|
return PhpVersion::fromComponents(8, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reverseEmulate(string $code, array $tokens): array {
|
||||||
|
// Don't bother
|
||||||
|
return $tokens;
|
||||||
|
}
|
||||||
|
}
|
31
lib/PhpParser/Lexer/TokenEmulator/ReadonlyTokenEmulator.php
Normal file
31
lib/PhpParser/Lexer/TokenEmulator/ReadonlyTokenEmulator.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
|
final class ReadonlyTokenEmulator extends KeywordEmulator {
|
||||||
|
public function getPhpVersion(): PhpVersion {
|
||||||
|
return PhpVersion::fromComponents(8, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKeywordString(): string {
|
||||||
|
return 'readonly';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKeywordToken(): int {
|
||||||
|
return \T_READONLY;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function isKeywordContext(array $tokens, int $pos): bool {
|
||||||
|
if (!parent::isKeywordContext($tokens, $pos)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Support "function readonly("
|
||||||
|
return !(isset($tokens[$pos + 1]) &&
|
||||||
|
($tokens[$pos + 1]->text === '(' ||
|
||||||
|
($tokens[$pos + 1]->id === \T_WHITESPACE &&
|
||||||
|
isset($tokens[$pos + 2]) &&
|
||||||
|
$tokens[$pos + 2]->text === '(')));
|
||||||
|
}
|
||||||
|
}
|
@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverses emulation direction of the inner emulator.
|
* Reverses emulation direction of the inner emulator.
|
||||||
*/
|
*/
|
||||||
final class ReverseEmulator extends TokenEmulator
|
final class ReverseEmulator extends TokenEmulator {
|
||||||
{
|
|
||||||
/** @var TokenEmulator Inner emulator */
|
/** @var TokenEmulator Inner emulator */
|
||||||
private $emulator;
|
private $emulator;
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ final class ReverseEmulator extends TokenEmulator
|
|||||||
$this->emulator = $emulator;
|
$this->emulator = $emulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPhpVersion(): string {
|
public function getPhpVersion(): PhpVersion {
|
||||||
return $this->emulator->getPhpVersion();
|
return $this->emulator->getPhpVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,4 +34,4 @@ final class ReverseEmulator extends TokenEmulator
|
|||||||
public function preprocessCode(string $code, array &$patches): string {
|
public function preprocessCode(string $code, array &$patches): string {
|
||||||
return $code;
|
return $code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,23 +2,28 @@
|
|||||||
|
|
||||||
namespace PhpParser\Lexer\TokenEmulator;
|
namespace PhpParser\Lexer\TokenEmulator;
|
||||||
|
|
||||||
|
use PhpParser\PhpVersion;
|
||||||
|
use PhpParser\Token;
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
abstract class TokenEmulator
|
abstract class TokenEmulator {
|
||||||
{
|
abstract public function getPhpVersion(): PhpVersion;
|
||||||
abstract public function getPhpVersion(): string;
|
|
||||||
|
|
||||||
abstract public function isEmulationNeeded(string $code): bool;
|
abstract public function isEmulationNeeded(string $code): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array Modified Tokens
|
* @param Token[] $tokens Original tokens
|
||||||
|
* @return Token[] Modified Tokens
|
||||||
*/
|
*/
|
||||||
abstract public function emulate(string $code, array $tokens): array;
|
abstract public function emulate(string $code, array $tokens): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array Modified Tokens
|
* @param Token[] $tokens Original tokens
|
||||||
|
* @return Token[] Modified Tokens
|
||||||
*/
|
*/
|
||||||
abstract public function reverseEmulate(string $code, array $tokens): array;
|
abstract public function reverseEmulate(string $code, array $tokens): array;
|
||||||
|
|
||||||
|
/** @param array{int, string, string}[] $patches */
|
||||||
public function preprocessCode(string $code, array &$patches): string {
|
public function preprocessCode(string $code, array &$patches): string {
|
||||||
return $code;
|
return $code;
|
||||||
}
|
}
|
||||||
|
69
lib/PhpParser/Modifiers.php
Normal file
69
lib/PhpParser/Modifiers.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modifiers used (as a bit mask) by various flags subnodes, for example on classes, functions,
|
||||||
|
* properties and constants.
|
||||||
|
*/
|
||||||
|
final class Modifiers {
|
||||||
|
public const PUBLIC = 1;
|
||||||
|
public const PROTECTED = 2;
|
||||||
|
public const PRIVATE = 4;
|
||||||
|
public const STATIC = 8;
|
||||||
|
public const ABSTRACT = 16;
|
||||||
|
public const FINAL = 32;
|
||||||
|
public const READONLY = 64;
|
||||||
|
|
||||||
|
public const VISIBILITY_MASK = 1 | 2 | 4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public static function verifyClassModifier(int $a, int $b): void {
|
||||||
|
if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
|
||||||
|
throw new Error('Multiple abstract modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & Modifiers::FINAL && $b & Modifiers::FINAL) {
|
||||||
|
throw new Error('Multiple final modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & Modifiers::READONLY && $b & Modifiers::READONLY) {
|
||||||
|
throw new Error('Multiple readonly modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & 48 && $b & 48) {
|
||||||
|
throw new Error('Cannot use the final modifier on an abstract class');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public static function verifyModifier(int $a, int $b): void {
|
||||||
|
if ($a & Modifiers::VISIBILITY_MASK && $b & Modifiers::VISIBILITY_MASK) {
|
||||||
|
throw new Error('Multiple access type modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
|
||||||
|
throw new Error('Multiple abstract modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & Modifiers::STATIC && $b & Modifiers::STATIC) {
|
||||||
|
throw new Error('Multiple static modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & Modifiers::FINAL && $b & Modifiers::FINAL) {
|
||||||
|
throw new Error('Multiple final modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & Modifiers::READONLY && $b & Modifiers::READONLY) {
|
||||||
|
throw new Error('Multiple readonly modifiers are not allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a & 48 && $b & 48) {
|
||||||
|
throw new Error('Cannot use the final modifier on an abstract class member');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,8 +6,7 @@ use PhpParser\Node\Name;
|
|||||||
use PhpParser\Node\Name\FullyQualified;
|
use PhpParser\Node\Name\FullyQualified;
|
||||||
use PhpParser\Node\Stmt;
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
class NameContext
|
class NameContext {
|
||||||
{
|
|
||||||
/** @var null|Name Current namespace */
|
/** @var null|Name Current namespace */
|
||||||
protected $namespace;
|
protected $namespace;
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ class NameContext
|
|||||||
*
|
*
|
||||||
* @param Name|null $namespace Null is the global namespace
|
* @param Name|null $namespace Null is the global namespace
|
||||||
*/
|
*/
|
||||||
public function startNamespace(Name $namespace = null) {
|
public function startNamespace(?Name $namespace = null): void {
|
||||||
$this->namespace = $namespace;
|
$this->namespace = $namespace;
|
||||||
$this->origAliases = $this->aliases = [
|
$this->origAliases = $this->aliases = [
|
||||||
Stmt\Use_::TYPE_NORMAL => [],
|
Stmt\Use_::TYPE_NORMAL => [],
|
||||||
@ -51,9 +50,9 @@ class NameContext
|
|||||||
* @param Name $name Original name
|
* @param Name $name Original name
|
||||||
* @param string $aliasName Aliased name
|
* @param string $aliasName Aliased name
|
||||||
* @param int $type One of Stmt\Use_::TYPE_*
|
* @param int $type One of Stmt\Use_::TYPE_*
|
||||||
* @param array $errorAttrs Attributes to use to report an error
|
* @param array<string, mixed> $errorAttrs Attributes to use to report an error
|
||||||
*/
|
*/
|
||||||
public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []) {
|
public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []): void {
|
||||||
// Constant names are case sensitive, everything else case insensitive
|
// Constant names are case sensitive, everything else case insensitive
|
||||||
if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
||||||
$aliasLookupName = $aliasName;
|
$aliasLookupName = $aliasName;
|
||||||
@ -87,7 +86,7 @@ class NameContext
|
|||||||
*
|
*
|
||||||
* @return null|Name Namespace (or null if global namespace)
|
* @return null|Name Namespace (or null if global namespace)
|
||||||
*/
|
*/
|
||||||
public function getNamespace() {
|
public function getNamespace(): ?Name {
|
||||||
return $this->namespace;
|
return $this->namespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +98,7 @@ class NameContext
|
|||||||
*
|
*
|
||||||
* @return null|Name Resolved name, or null if static resolution is not possible
|
* @return null|Name Resolved name, or null if static resolution is not possible
|
||||||
*/
|
*/
|
||||||
public function getResolvedName(Name $name, int $type) {
|
public function getResolvedName(Name $name, int $type): ?Name {
|
||||||
// don't resolve special class names
|
// don't resolve special class names
|
||||||
if ($type === Stmt\Use_::TYPE_NORMAL && $name->isSpecialClassName()) {
|
if ($type === Stmt\Use_::TYPE_NORMAL && $name->isSpecialClassName()) {
|
||||||
if (!$name->isUnqualified()) {
|
if (!$name->isUnqualified()) {
|
||||||
@ -142,7 +141,7 @@ class NameContext
|
|||||||
*
|
*
|
||||||
* @return Name Resolved name
|
* @return Name Resolved name
|
||||||
*/
|
*/
|
||||||
public function getResolvedClassName(Name $name) : Name {
|
public function getResolvedClassName(Name $name): Name {
|
||||||
return $this->getResolvedName($name, Stmt\Use_::TYPE_NORMAL);
|
return $this->getResolvedName($name, Stmt\Use_::TYPE_NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +153,7 @@ class NameContext
|
|||||||
*
|
*
|
||||||
* @return Name[] Possible representations of the name
|
* @return Name[] Possible representations of the name
|
||||||
*/
|
*/
|
||||||
public function getPossibleNames(string $name, int $type) : array {
|
public function getPossibleNames(string $name, int $type): array {
|
||||||
$lcName = strtolower($name);
|
$lcName = strtolower($name);
|
||||||
|
|
||||||
if ($type === Stmt\Use_::TYPE_NORMAL) {
|
if ($type === Stmt\Use_::TYPE_NORMAL) {
|
||||||
@ -210,7 +209,7 @@ class NameContext
|
|||||||
*
|
*
|
||||||
* @return Name Shortest representation
|
* @return Name Shortest representation
|
||||||
*/
|
*/
|
||||||
public function getShortName(string $name, int $type) : Name {
|
public function getShortName(string $name, int $type): Name {
|
||||||
$possibleNames = $this->getPossibleNames($name, $type);
|
$possibleNames = $this->getPossibleNames($name, $type);
|
||||||
|
|
||||||
// Find shortest name
|
// Find shortest name
|
||||||
@ -224,10 +223,10 @@ class NameContext
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $shortestName;
|
return $shortestName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function resolveAlias(Name $name, $type) {
|
private function resolveAlias(Name $name, int $type): ?FullyQualified {
|
||||||
$firstPart = $name->getFirst();
|
$firstPart = $name->getFirst();
|
||||||
|
|
||||||
if ($name->isQualified()) {
|
if ($name->isQualified()) {
|
||||||
@ -250,7 +249,7 @@ class NameContext
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getNamespaceRelativeName(string $name, string $lcName, int $type) {
|
private function getNamespaceRelativeName(string $name, string $lcName, int $type): ?Name {
|
||||||
if (null === $this->namespace) {
|
if (null === $this->namespace) {
|
||||||
return new Name($name);
|
return new Name($name);
|
||||||
}
|
}
|
||||||
@ -271,7 +270,7 @@ class NameContext
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function normalizeConstName(string $name) {
|
private function normalizeConstName(string $name): string {
|
||||||
$nsSep = strrpos($name, '\\');
|
$nsSep = strrpos($name, '\\');
|
||||||
if (false === $nsSep) {
|
if (false === $nsSep) {
|
||||||
return $name;
|
return $name;
|
||||||
|
@ -2,28 +2,27 @@
|
|||||||
|
|
||||||
namespace PhpParser;
|
namespace PhpParser;
|
||||||
|
|
||||||
interface Node
|
interface Node {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Gets the type of the node.
|
* Gets the type of the node.
|
||||||
*
|
*
|
||||||
* @return string Type of the node
|
* @return string Type of the node
|
||||||
*/
|
*/
|
||||||
public function getType() : string;
|
public function getType(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the names of the sub nodes.
|
* Gets the names of the sub nodes.
|
||||||
*
|
*
|
||||||
* @return array Names of sub nodes
|
* @return string[] Names of sub nodes
|
||||||
*/
|
*/
|
||||||
public function getSubNodeNames() : array;
|
public function getSubNodeNames(): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets line the node started in (alias of getStartLine).
|
* Gets line the node started in (alias of getStartLine).
|
||||||
*
|
*
|
||||||
* @return int Start line (or -1 if not available)
|
* @return int Start line (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getLine() : int;
|
public function getLine(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets line the node started in.
|
* Gets line the node started in.
|
||||||
@ -32,7 +31,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return int Start line (or -1 if not available)
|
* @return int Start line (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getStartLine() : int;
|
public function getStartLine(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the line the node ended in.
|
* Gets the line the node ended in.
|
||||||
@ -41,7 +40,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return int End line (or -1 if not available)
|
* @return int End line (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getEndLine() : int;
|
public function getEndLine(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the token offset of the first token that is part of this node.
|
* Gets the token offset of the first token that is part of this node.
|
||||||
@ -52,7 +51,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return int Token start position (or -1 if not available)
|
* @return int Token start position (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getStartTokenPos() : int;
|
public function getStartTokenPos(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the token offset of the last token that is part of this node.
|
* Gets the token offset of the last token that is part of this node.
|
||||||
@ -63,7 +62,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return int Token end position (or -1 if not available)
|
* @return int Token end position (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getEndTokenPos() : int;
|
public function getEndTokenPos(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the file offset of the first character that is part of this node.
|
* Gets the file offset of the first character that is part of this node.
|
||||||
@ -72,7 +71,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return int File start position (or -1 if not available)
|
* @return int File start position (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getStartFilePos() : int;
|
public function getStartFilePos(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the file offset of the last character that is part of this node.
|
* Gets the file offset of the last character that is part of this node.
|
||||||
@ -81,7 +80,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return int File end position (or -1 if not available)
|
* @return int File end position (or -1 if not available)
|
||||||
*/
|
*/
|
||||||
public function getEndFilePos() : int;
|
public function getEndFilePos(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all comments directly preceding this node.
|
* Gets all comments directly preceding this node.
|
||||||
@ -90,14 +89,14 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return Comment[]
|
* @return Comment[]
|
||||||
*/
|
*/
|
||||||
public function getComments() : array;
|
public function getComments(): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the doc comment of the node.
|
* Gets the doc comment of the node.
|
||||||
*
|
*
|
||||||
* @return null|Comment\Doc Doc comment object or null
|
* @return null|Comment\Doc Doc comment object or null
|
||||||
*/
|
*/
|
||||||
public function getDocComment();
|
public function getDocComment(): ?Comment\Doc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the doc comment of the node.
|
* Sets the doc comment of the node.
|
||||||
@ -106,7 +105,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @param Comment\Doc $docComment Doc comment to set
|
* @param Comment\Doc $docComment Doc comment to set
|
||||||
*/
|
*/
|
||||||
public function setDocComment(Comment\Doc $docComment);
|
public function setDocComment(Comment\Doc $docComment): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets an attribute on a node.
|
* Sets an attribute on a node.
|
||||||
@ -114,7 +113,7 @@ interface Node
|
|||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
public function setAttribute(string $key, $value);
|
public function setAttribute(string $key, $value): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether an attribute exists.
|
* Returns whether an attribute exists.
|
||||||
@ -123,7 +122,7 @@ interface Node
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasAttribute(string $key) : bool;
|
public function hasAttribute(string $key): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value of an attribute.
|
* Returns the value of an attribute.
|
||||||
@ -138,14 +137,14 @@ interface Node
|
|||||||
/**
|
/**
|
||||||
* Returns all the attributes of this node.
|
* Returns all the attributes of this node.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function getAttributes() : array;
|
public function getAttributes(): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces all the attributes of this node.
|
* Replaces all the attributes of this node.
|
||||||
*
|
*
|
||||||
* @param array $attributes
|
* @param array<string, mixed> $attributes
|
||||||
*/
|
*/
|
||||||
public function setAttributes(array $attributes);
|
public function setAttributes(array $attributes): void;
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ namespace PhpParser\Node;
|
|||||||
|
|
||||||
use PhpParser\NodeAbstract;
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
class Arg extends NodeAbstract
|
class Arg extends NodeAbstract {
|
||||||
{
|
|
||||||
/** @var Identifier|null Parameter name (for named parameters) */
|
/** @var Identifier|null Parameter name (for named parameters) */
|
||||||
public $name;
|
public $name;
|
||||||
/** @var Expr Value to pass */
|
/** @var Expr Value to pass */
|
||||||
@ -21,12 +20,12 @@ class Arg extends NodeAbstract
|
|||||||
* @param Expr $value Value to pass
|
* @param Expr $value Value to pass
|
||||||
* @param bool $byRef Whether to pass by ref
|
* @param bool $byRef Whether to pass by ref
|
||||||
* @param bool $unpack Whether to unpack the argument
|
* @param bool $unpack Whether to unpack the argument
|
||||||
* @param array $attributes Additional attributes
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
* @param Identifier|null $name Parameter name (for named parameters)
|
* @param Identifier|null $name Parameter name (for named parameters)
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = [],
|
Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = [],
|
||||||
Identifier $name = null
|
?Identifier $name = null
|
||||||
) {
|
) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
@ -35,11 +34,11 @@ class Arg extends NodeAbstract
|
|||||||
$this->unpack = $unpack;
|
$this->unpack = $unpack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['name', 'value', 'byRef', 'unpack'];
|
return ['name', 'value', 'byRef', 'unpack'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Arg';
|
return 'Arg';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
lib/PhpParser/Node/ArrayItem.php
Normal file
43
lib/PhpParser/Node/ArrayItem.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Node;
|
||||||
|
|
||||||
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
|
class ArrayItem extends NodeAbstract {
|
||||||
|
/** @var null|Expr Key */
|
||||||
|
public $key;
|
||||||
|
/** @var Expr Value */
|
||||||
|
public $value;
|
||||||
|
/** @var bool Whether to assign by reference */
|
||||||
|
public $byRef;
|
||||||
|
/** @var bool Whether to unpack the argument */
|
||||||
|
public $unpack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an array item node.
|
||||||
|
*
|
||||||
|
* @param Expr $value Value
|
||||||
|
* @param null|Expr $key Key
|
||||||
|
* @param bool $byRef Whether to assign by reference
|
||||||
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
|
*/
|
||||||
|
public function __construct(Expr $value, ?Expr $key = null, bool $byRef = false, array $attributes = [], bool $unpack = false) {
|
||||||
|
$this->attributes = $attributes;
|
||||||
|
$this->key = $key;
|
||||||
|
$this->value = $value;
|
||||||
|
$this->byRef = $byRef;
|
||||||
|
$this->unpack = $unpack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSubNodeNames(): array {
|
||||||
|
return ['key', 'value', 'byRef', 'unpack'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getType(): string {
|
||||||
|
return 'ArrayItem';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @deprecated compatibility alias
|
||||||
|
class_alias(ArrayItem::class, Expr\ArrayItem::class);
|
@ -5,18 +5,17 @@ namespace PhpParser\Node;
|
|||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
use PhpParser\NodeAbstract;
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
class Attribute extends NodeAbstract
|
class Attribute extends NodeAbstract {
|
||||||
{
|
|
||||||
/** @var Name Attribute name */
|
/** @var Name Attribute name */
|
||||||
public $name;
|
public $name;
|
||||||
|
|
||||||
/** @var Arg[] Attribute arguments */
|
/** @var list<Arg> Attribute arguments */
|
||||||
public $args;
|
public $args;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Node\Name $name Attribute name
|
* @param Node\Name $name Attribute name
|
||||||
* @param Arg[] $args Attribute arguments
|
* @param list<Arg> $args Attribute arguments
|
||||||
* @param array $attributes Additional node attributes
|
* @param array<string, mixed> $attributes Additional node attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(Name $name, array $args = [], array $attributes = []) {
|
public function __construct(Name $name, array $args = [], array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
@ -24,11 +23,11 @@ class Attribute extends NodeAbstract
|
|||||||
$this->args = $args;
|
$this->args = $args;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['name', 'args'];
|
return ['name', 'args'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Attribute';
|
return 'Attribute';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,28 +2,26 @@
|
|||||||
|
|
||||||
namespace PhpParser\Node;
|
namespace PhpParser\Node;
|
||||||
|
|
||||||
use PhpParser\Node;
|
|
||||||
use PhpParser\NodeAbstract;
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
class AttributeGroup extends NodeAbstract
|
class AttributeGroup extends NodeAbstract {
|
||||||
{
|
|
||||||
/** @var Attribute[] Attributes */
|
/** @var Attribute[] Attributes */
|
||||||
public $attrs;
|
public $attrs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Attribute[] $attrs PHP attributes
|
* @param Attribute[] $attrs PHP attributes
|
||||||
* @param array $attributes Additional node attributes
|
* @param array<string, mixed> $attributes Additional node attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(array $attrs, array $attributes = []) {
|
public function __construct(array $attrs, array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
$this->attrs = $attrs;
|
$this->attrs = $attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['attrs'];
|
return ['attrs'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'AttributeGroup';
|
return 'AttributeGroup';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
36
lib/PhpParser/Node/ClosureUse.php
Normal file
36
lib/PhpParser/Node/ClosureUse.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Node;
|
||||||
|
|
||||||
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
|
class ClosureUse extends NodeAbstract {
|
||||||
|
/** @var Expr\Variable Variable to use */
|
||||||
|
public $var;
|
||||||
|
/** @var bool Whether to use by reference */
|
||||||
|
public $byRef;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a closure use node.
|
||||||
|
*
|
||||||
|
* @param Expr\Variable $var Variable to use
|
||||||
|
* @param bool $byRef Whether to use by reference
|
||||||
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
|
*/
|
||||||
|
public function __construct(Expr\Variable $var, bool $byRef = false, array $attributes = []) {
|
||||||
|
$this->attributes = $attributes;
|
||||||
|
$this->var = $var;
|
||||||
|
$this->byRef = $byRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSubNodeNames(): array {
|
||||||
|
return ['var', 'byRef'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getType(): string {
|
||||||
|
return 'ClosureUse';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @deprecated compatibility alias
|
||||||
|
class_alias(ClosureUse::class, Expr\ClosureUse::class);
|
13
lib/PhpParser/Node/ComplexType.php
Normal file
13
lib/PhpParser/Node/ComplexType.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Node;
|
||||||
|
|
||||||
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a base class for complex types, including nullable types and union types.
|
||||||
|
*
|
||||||
|
* It does not provide any shared behavior and exists only for type-checking purposes.
|
||||||
|
*/
|
||||||
|
abstract class ComplexType extends NodeAbstract {
|
||||||
|
}
|
@ -4,22 +4,21 @@ namespace PhpParser\Node;
|
|||||||
|
|
||||||
use PhpParser\NodeAbstract;
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
/**
|
class Const_ extends NodeAbstract {
|
||||||
* @property Name $namespacedName Namespaced name (for global constants, if using NameResolver)
|
|
||||||
*/
|
|
||||||
class Const_ extends NodeAbstract
|
|
||||||
{
|
|
||||||
/** @var Identifier Name */
|
/** @var Identifier Name */
|
||||||
public $name;
|
public $name;
|
||||||
/** @var Expr Value */
|
/** @var Expr Value */
|
||||||
public $value;
|
public $value;
|
||||||
|
|
||||||
|
/** @var Name|null Namespaced name (if using NameResolver) */
|
||||||
|
public $namespacedName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a const node for use in class const and const statements.
|
* Constructs a const node for use in class const and const statements.
|
||||||
*
|
*
|
||||||
* @param string|Identifier $name Name
|
* @param string|Identifier $name Name
|
||||||
* @param Expr $value Value
|
* @param Expr $value Value
|
||||||
* @param array $attributes Additional attributes
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
*/
|
*/
|
||||||
public function __construct($name, Expr $value, array $attributes = []) {
|
public function __construct($name, Expr $value, array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
@ -27,11 +26,11 @@ class Const_ extends NodeAbstract
|
|||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['name', 'value'];
|
return ['name', 'value'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Const';
|
return 'Const';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
37
lib/PhpParser/Node/DeclareItem.php
Normal file
37
lib/PhpParser/Node/DeclareItem.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace PhpParser\Node;
|
||||||
|
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
|
class DeclareItem extends NodeAbstract {
|
||||||
|
/** @var Node\Identifier Key */
|
||||||
|
public $key;
|
||||||
|
/** @var Node\Expr Value */
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a declare key=>value pair node.
|
||||||
|
*
|
||||||
|
* @param string|Node\Identifier $key Key
|
||||||
|
* @param Node\Expr $value Value
|
||||||
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
|
*/
|
||||||
|
public function __construct($key, Node\Expr $value, array $attributes = []) {
|
||||||
|
$this->attributes = $attributes;
|
||||||
|
$this->key = \is_string($key) ? new Node\Identifier($key) : $key;
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSubNodeNames(): array {
|
||||||
|
return ['key', 'value'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getType(): string {
|
||||||
|
return 'DeclareItem';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @deprecated compatibility alias
|
||||||
|
class_alias(DeclareItem::class, Stmt\DeclareDeclare::class);
|
@ -4,6 +4,5 @@ namespace PhpParser\Node;
|
|||||||
|
|
||||||
use PhpParser\NodeAbstract;
|
use PhpParser\NodeAbstract;
|
||||||
|
|
||||||
abstract class Expr extends NodeAbstract
|
abstract class Expr extends NodeAbstract {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ namespace PhpParser\Node\Expr;
|
|||||||
|
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
|
|
||||||
class ArrayDimFetch extends Expr
|
class ArrayDimFetch extends Expr {
|
||||||
{
|
|
||||||
/** @var Expr Variable */
|
/** @var Expr Variable */
|
||||||
public $var;
|
public $var;
|
||||||
/** @var null|Expr Array index / dim */
|
/** @var null|Expr Array index / dim */
|
||||||
@ -16,19 +15,19 @@ class ArrayDimFetch extends Expr
|
|||||||
*
|
*
|
||||||
* @param Expr $var Variable
|
* @param Expr $var Variable
|
||||||
* @param null|Expr $dim Array index / dim
|
* @param null|Expr $dim Array index / dim
|
||||||
* @param array $attributes Additional attributes
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(Expr $var, Expr $dim = null, array $attributes = []) {
|
public function __construct(Expr $var, ?Expr $dim = null, array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
$this->var = $var;
|
$this->var = $var;
|
||||||
$this->dim = $dim;
|
$this->dim = $dim;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['var', 'dim'];
|
return ['var', 'dim'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Expr_ArrayDimFetch';
|
return 'Expr_ArrayDimFetch';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,41 +1,3 @@
|
|||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
namespace PhpParser\Node\Expr;
|
require __DIR__ . '/../ArrayItem.php';
|
||||||
|
|
||||||
use PhpParser\Node\Expr;
|
|
||||||
|
|
||||||
class ArrayItem extends Expr
|
|
||||||
{
|
|
||||||
/** @var null|Expr Key */
|
|
||||||
public $key;
|
|
||||||
/** @var Expr Value */
|
|
||||||
public $value;
|
|
||||||
/** @var bool Whether to assign by reference */
|
|
||||||
public $byRef;
|
|
||||||
/** @var bool Whether to unpack the argument */
|
|
||||||
public $unpack;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs an array item node.
|
|
||||||
*
|
|
||||||
* @param Expr $value Value
|
|
||||||
* @param null|Expr $key Key
|
|
||||||
* @param bool $byRef Whether to assign by reference
|
|
||||||
* @param array $attributes Additional attributes
|
|
||||||
*/
|
|
||||||
public function __construct(Expr $value, Expr $key = null, bool $byRef = false, array $attributes = [], bool $unpack = false) {
|
|
||||||
$this->attributes = $attributes;
|
|
||||||
$this->key = $key;
|
|
||||||
$this->value = $value;
|
|
||||||
$this->byRef = $byRef;
|
|
||||||
$this->unpack = $unpack;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
|
||||||
return ['key', 'value', 'byRef', 'unpack'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getType() : string {
|
|
||||||
return 'Expr_ArrayItem';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -2,33 +2,33 @@
|
|||||||
|
|
||||||
namespace PhpParser\Node\Expr;
|
namespace PhpParser\Node\Expr;
|
||||||
|
|
||||||
|
use PhpParser\Node\ArrayItem;
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
|
|
||||||
class Array_ extends Expr
|
class Array_ extends Expr {
|
||||||
{
|
|
||||||
// For use in "kind" attribute
|
// For use in "kind" attribute
|
||||||
const KIND_LONG = 1; // array() syntax
|
public const KIND_LONG = 1; // array() syntax
|
||||||
const KIND_SHORT = 2; // [] syntax
|
public const KIND_SHORT = 2; // [] syntax
|
||||||
|
|
||||||
/** @var (ArrayItem|null)[] Items */
|
/** @var ArrayItem[] Items */
|
||||||
public $items;
|
public $items;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an array node.
|
* Constructs an array node.
|
||||||
*
|
*
|
||||||
* @param (ArrayItem|null)[] $items Items of the array
|
* @param ArrayItem[] $items Items of the array
|
||||||
* @param array $attributes Additional attributes
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(array $items = [], array $attributes = []) {
|
public function __construct(array $items = [], array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
$this->items = $items;
|
$this->items = $items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['items'];
|
return ['items'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Expr_Array';
|
return 'Expr_Array';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,7 @@ use PhpParser\Node;
|
|||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
use PhpParser\Node\FunctionLike;
|
use PhpParser\Node\FunctionLike;
|
||||||
|
|
||||||
class ArrowFunction extends Expr implements FunctionLike
|
class ArrowFunction extends Expr implements FunctionLike {
|
||||||
{
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public $static;
|
public $static;
|
||||||
|
|
||||||
@ -17,7 +16,7 @@ class ArrowFunction extends Expr implements FunctionLike
|
|||||||
/** @var Node\Param[] */
|
/** @var Node\Param[] */
|
||||||
public $params = [];
|
public $params = [];
|
||||||
|
|
||||||
/** @var null|Node\Identifier|Node\Name|Node\NullableType|Node\UnionType */
|
/** @var null|Node\Identifier|Node\Name|Node\ComplexType */
|
||||||
public $returnType;
|
public $returnType;
|
||||||
|
|
||||||
/** @var Expr */
|
/** @var Expr */
|
||||||
@ -26,35 +25,42 @@ class ArrowFunction extends Expr implements FunctionLike
|
|||||||
public $attrGroups;
|
public $attrGroups;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $subNodes Array of the following optional subnodes:
|
* @param array{
|
||||||
* 'static' => false : Whether the closure is static
|
* expr: Expr,
|
||||||
* 'byRef' => false : Whether to return by reference
|
* static?: bool,
|
||||||
* 'params' => array() : Parameters
|
* byRef?: bool,
|
||||||
* 'returnType' => null : Return type
|
* params?: Node\Param[],
|
||||||
* 'expr' => Expr : Expression body
|
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
|
||||||
* 'attrGroups' => array() : PHP attribute groups
|
* attrGroups?: Node\AttributeGroup[]
|
||||||
* @param array $attributes Additional attributes
|
* } $subNodes Array of the following subnodes:
|
||||||
|
* 'expr' : Expression body
|
||||||
|
* 'static' => false : Whether the closure is static
|
||||||
|
* 'byRef' => false : Whether to return by reference
|
||||||
|
* 'params' => array() : Parameters
|
||||||
|
* 'returnType' => null : Return type
|
||||||
|
* 'attrGroups' => array() : PHP attribute groups
|
||||||
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(array $subNodes = [], array $attributes = []) {
|
public function __construct(array $subNodes, array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
$this->static = $subNodes['static'] ?? false;
|
$this->static = $subNodes['static'] ?? false;
|
||||||
$this->byRef = $subNodes['byRef'] ?? false;
|
$this->byRef = $subNodes['byRef'] ?? false;
|
||||||
$this->params = $subNodes['params'] ?? [];
|
$this->params = $subNodes['params'] ?? [];
|
||||||
$returnType = $subNodes['returnType'] ?? null;
|
$returnType = $subNodes['returnType'] ?? null;
|
||||||
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
|
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
|
||||||
$this->expr = $subNodes['expr'] ?? null;
|
$this->expr = $subNodes['expr'];
|
||||||
$this->attrGroups = $subNodes['attrGroups'] ?? [];
|
$this->attrGroups = $subNodes['attrGroups'] ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['attrGroups', 'static', 'byRef', 'params', 'returnType', 'expr'];
|
return ['attrGroups', 'static', 'byRef', 'params', 'returnType', 'expr'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function returnsByRef() : bool {
|
public function returnsByRef(): bool {
|
||||||
return $this->byRef;
|
return $this->byRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getParams() : array {
|
public function getParams(): array {
|
||||||
return $this->params;
|
return $this->params;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,18 +68,18 @@ class ArrowFunction extends Expr implements FunctionLike
|
|||||||
return $this->returnType;
|
return $this->returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAttrGroups() : array {
|
public function getAttrGroups(): array {
|
||||||
return $this->attrGroups;
|
return $this->attrGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Node\Stmt\Return_[]
|
* @return Node\Stmt\Return_[]
|
||||||
*/
|
*/
|
||||||
public function getStmts() : array {
|
public function getStmts(): array {
|
||||||
return [new Node\Stmt\Return_($this->expr)];
|
return [new Node\Stmt\Return_($this->expr)];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Expr_ArrowFunction';
|
return 'Expr_ArrowFunction';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ namespace PhpParser\Node\Expr;
|
|||||||
|
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
|
|
||||||
class Assign extends Expr
|
class Assign extends Expr {
|
||||||
{
|
|
||||||
/** @var Expr Variable */
|
/** @var Expr Variable */
|
||||||
public $var;
|
public $var;
|
||||||
/** @var Expr Expression */
|
/** @var Expr Expression */
|
||||||
@ -16,7 +15,7 @@ class Assign extends Expr
|
|||||||
*
|
*
|
||||||
* @param Expr $var Variable
|
* @param Expr $var Variable
|
||||||
* @param Expr $expr Expression
|
* @param Expr $expr Expression
|
||||||
* @param array $attributes Additional attributes
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
@ -24,11 +23,11 @@ class Assign extends Expr
|
|||||||
$this->expr = $expr;
|
$this->expr = $expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['var', 'expr'];
|
return ['var', 'expr'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType() : string {
|
public function getType(): string {
|
||||||
return 'Expr_Assign';
|
return 'Expr_Assign';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ namespace PhpParser\Node\Expr;
|
|||||||
|
|
||||||
use PhpParser\Node\Expr;
|
use PhpParser\Node\Expr;
|
||||||
|
|
||||||
abstract class AssignOp extends Expr
|
abstract class AssignOp extends Expr {
|
||||||
{
|
|
||||||
/** @var Expr Variable */
|
/** @var Expr Variable */
|
||||||
public $var;
|
public $var;
|
||||||
/** @var Expr Expression */
|
/** @var Expr Expression */
|
||||||
@ -16,7 +15,7 @@ abstract class AssignOp extends Expr
|
|||||||
*
|
*
|
||||||
* @param Expr $var Variable
|
* @param Expr $var Variable
|
||||||
* @param Expr $expr Expression
|
* @param Expr $expr Expression
|
||||||
* @param array $attributes Additional attributes
|
* @param array<string, mixed> $attributes Additional attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
public function __construct(Expr $var, Expr $expr, array $attributes = []) {
|
||||||
$this->attributes = $attributes;
|
$this->attributes = $attributes;
|
||||||
@ -24,7 +23,7 @@ abstract class AssignOp extends Expr
|
|||||||
$this->expr = $expr;
|
$this->expr = $expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubNodeNames() : array {
|
public function getSubNodeNames(): array {
|
||||||
return ['var', 'expr'];
|
return ['var', 'expr'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user