mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-06-25 17:43:47 +02:00
Compare commits
1310 Commits
v0.9.4
...
v5.0.0beta
Author | SHA1 | Date | |
---|---|---|---|
edee19a5d2 | |||
ea5ba26749 | |||
ab51e9d35a | |||
1873020bf7 | |||
1b346f7935 | |||
2d3dd4e23e | |||
3c52ea9b6d | |||
5a7753a930 | |||
c91c8633a4 | |||
e395f042d2 | |||
21ead39056 | |||
b11fca0310 | |||
06c7ab51b7 | |||
263fa80b81 | |||
5da5231fde | |||
8d58380108 | |||
efe93a171b | |||
ea77807592 | |||
9a68468fda | |||
502b090900 | |||
3c0432b09d | |||
ee3e7db3fc | |||
ba851243f4 | |||
d1d784a5c6 | |||
62853b179c | |||
4b497045e0 | |||
b20267c5ad | |||
748aab3365 | |||
c48ee36f54 | |||
8b1371990c | |||
eaa1d91b4e | |||
3fb4b92f59 | |||
571ca90b7e | |||
16c766eae1 | |||
53f6717329 | |||
5b65f9fc92 | |||
69993a181a | |||
23647573e8 | |||
df3a7057ab | |||
d43edfbb31 | |||
ad8daa12b2 | |||
5883189d61 | |||
afe1628a72 | |||
289756d056 | |||
8490c0e82d | |||
8bc698248d | |||
fb2c3ac97c | |||
c23976a299 | |||
a9dad5c54e | |||
74caed6446 | |||
a5d4c1005c | |||
93731c5cfa | |||
0a8a333a4a | |||
91da19147b | |||
5c267f55c9 | |||
9a5d5c112c | |||
779b6950c3 | |||
b68fb76f14 | |||
36a6dcd04e | |||
bb4263ea1a | |||
4ce0de2d12 | |||
a3bc900a41 | |||
7785d2b887 | |||
aa721520f9 | |||
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 | |||
658f1be311 | |||
b9b65a2996 | |||
1b479e7592 | |||
b5351f883a | |||
88be6127fa | |||
8a97fa157f | |||
1c13d05035 | |||
c7dc3ce552 | |||
9f6ad686a7 | |||
1899471f80 | |||
8505acd151 | |||
c3e20d9970 | |||
4c22c62783 | |||
f66a32e2df | |||
75abbbd2d4 | |||
39b046007d | |||
e3872b8906 | |||
4a40a84cf6 | |||
88e519766f | |||
35306de32f | |||
ad365b1beb | |||
4bc82432de | |||
fd6052e37d | |||
bd722809f7 | |||
56356e4aec | |||
aaee038b91 | |||
f9d35fe11e | |||
8bcaa4261e | |||
feb6bf7a0c | |||
0cee2088ea | |||
78e08fc178 | |||
3aadc15e2e | |||
a98350581c | |||
7f398769a0 | |||
98f7f39d1c | |||
722119502f | |||
544aee1671 | |||
8c58eb4cd4 | |||
23d9c17770 | |||
31be7b4ed9 | |||
f6bf041583 | |||
1bf073a76c | |||
1721ae155c | |||
54fc023b25 | |||
f845568e1d | |||
b4ca4ce0fe | |||
303342630d | |||
1a1300aa2a | |||
3698f4b727 | |||
7b2ec6703f | |||
61328f89da | |||
21dce06dfb | |||
acaf3fecad | |||
a63b495fe6 | |||
2d1998938c | |||
739b4b4c60 | |||
17f478107a | |||
cd7c058e76 | |||
d8b19d7963 | |||
69c5d48afd | |||
6ec527bce7 | |||
fc25609ecb | |||
f545f18a87 | |||
c346bbfafe | |||
4abc531213 | |||
b58b19ed1d | |||
0d2d8f95a1 | |||
244db65dd1 | |||
53c2753d75 | |||
b5f5313d73 | |||
32f89662f3 | |||
f33f081c8f | |||
bd43ec7152 | |||
d86ca0f745 | |||
c8d1216531 | |||
8999a13cb2 | |||
c1eaa0d5cd | |||
ca5f7c9761 | |||
ad696ee75d | |||
6770deda3d | |||
568236a305 | |||
9dda080a9d | |||
3ec87ef757 | |||
a2443aaefa | |||
64f4d5b619 | |||
f56d90d4f8 | |||
40aa2282df | |||
46cbd9393a | |||
602af9060d | |||
bf086d9833 | |||
521addec91 | |||
88f3a669c1 | |||
ba9cf39999 | |||
f862853987 | |||
9a9981c347 | |||
2c42f64475 | |||
664c10121e | |||
5b1cd2e4f2 | |||
b76bbc3c51 | |||
eacc5dbe19 | |||
3226eb4086 | |||
54c37f6b3b | |||
0a80b2d8ee | |||
2f45e05042 | |||
69c105dde1 | |||
603203177e | |||
97e59c7a16 | |||
006acba066 | |||
005bb1dba7 | |||
40e7b67d69 | |||
5644a916bc | |||
e612609022 | |||
4fd36b9946 | |||
a1f72690ef | |||
2e2954ccdf | |||
3f718ee2c3 | |||
b9b45dd2bc | |||
a4b43edb03 | |||
3cf61fdd26 | |||
9484baf8f8 | |||
aad0e2896f | |||
624f71fa6f | |||
1bd73cc04c | |||
94d93f27a5 | |||
a167aa2061 | |||
993f29906b | |||
9d44edf85d | |||
aa97a9bb69 | |||
aa72c5d674 | |||
60d025a914 | |||
b2cecec6bc | |||
8012faea54 | |||
f3b19c19ef | |||
78d9985d11 | |||
57b8673ea7 | |||
5221f49a60 | |||
ae4e90d558 | |||
9de96821f7 | |||
b7e6361536 | |||
594bcae1fc | |||
d5180f0d95 | |||
1f95f9215c | |||
6b9dd7afe7 | |||
ba092652fe | |||
ec0d834c5f | |||
ea3657fc5f | |||
af8c729603 | |||
0ef61b49bb | |||
90ee36a7fc | |||
8aae5b59b8 | |||
49d73e829f | |||
80ead71da2 | |||
294b93fbca | |||
bc3ac5e5ea | |||
382ca0128f | |||
f42bbc2403 | |||
ebf6b1c33b | |||
a74b54ce8b | |||
8e4f972036 | |||
3d0f7843d4 | |||
69068722b7 | |||
fb3d89e463 | |||
d0230c5c77 | |||
dc323458b4 | |||
674c5610fb | |||
4fb48c0e18 | |||
d638dd9b25 | |||
c5316487a4 | |||
eebaa94647 | |||
0ed9065b4c | |||
5f73c4de80 | |||
146411bb86 | |||
e12891a9db | |||
a5c726bfbf | |||
fd93690705 | |||
83766c8c0e | |||
fa6ee28600 | |||
16c5d107ca | |||
d7d581c1d2 | |||
1973b6633a | |||
7f96481c80 | |||
41408081d7 | |||
4d6825c460 | |||
d4a5a6e84b | |||
c55798ccc6 | |||
82e8b33814 | |||
8d9ae46597 | |||
f6fc248ca3 | |||
84dcd179d2 | |||
3e02eb7108 | |||
2f67429ec9 | |||
0cd7207ca6 | |||
6751ac3c9d | |||
5aeb884ab5 | |||
b855c1ca23 | |||
53c732a96d | |||
bd088dc940 | |||
8b1c9c0409 | |||
df64d86cf4 | |||
aac539ef0a | |||
f967b867d5 | |||
6bbdaf6c16 | |||
35b8caf75e | |||
6526ea2497 | |||
3ce5e628a7 | |||
ee870e3464 | |||
21d27527ed | |||
7b201b63d2 | |||
01e85a26c6 | |||
e6452e8d15 | |||
9202d87f53 | |||
fa99c3fbfb | |||
2b0bd657bb | |||
81b7be3ba6 | |||
6121001576 | |||
7208b1c7ac | |||
e4a54fa90a | |||
3125b54e5e | |||
7f8ff1b9a4 | |||
91a462ce76 | |||
610617fe93 | |||
ff2d85dc6b | |||
b998d1e9b5 | |||
6aba7624ed | |||
9c18e3db49 | |||
ae52aadb43 | |||
9cea94000a | |||
bb87e28e7d | |||
7484acb88b | |||
bc5ba47b28 | |||
b9996315a6 | |||
de3470190c | |||
1c3eabb000 | |||
8d1e86b47f | |||
ff10cc9d08 | |||
14454326e9 | |||
3a45c1a121 | |||
ad9c42b66a | |||
08215e7646 | |||
c18bb27723 | |||
dd0adcc96c | |||
e4505de346 | |||
a513ccabb7 | |||
d817818b5d | |||
6a273c9fbd | |||
c2d3ecad35 | |||
e57b3a0978 | |||
1cdb280a30 | |||
d01fafcb40 | |||
67df02c844 | |||
b85b6b3519 | |||
94c715d97e | |||
4dacbb8d39 | |||
aa685e711a | |||
edafeb85c4 | |||
68d07c4662 | |||
8fae99aafe | |||
c7ada124d0 | |||
f6617e6d25 | |||
2499534729 | |||
e0a2043089 | |||
e6e52abae7 | |||
7f72c84122 | |||
fc8ac71e76 | |||
a8968caa5b | |||
5285df8f22 | |||
4366aa2fb0 | |||
bf7d811cda | |||
248b29ecf6 | |||
bcb45d31eb | |||
3bc31488ce | |||
92b39e3d1f | |||
1c7fd314d1 | |||
fb8175567e | |||
63abf9cb3f | |||
ceb4932ca8 | |||
aa289c9694 | |||
579f4ce846 | |||
8d3cb5f57b | |||
de82a40d04 | |||
a86151f24f | |||
e6e8791848 | |||
83b958763f | |||
4dbb02c57b | |||
4fcdac40d1 | |||
04feb90d79 | |||
dc3ace55c3 | |||
e5453f0d46 | |||
b507fa43da | |||
336a49b428 | |||
fa174b093f | |||
94ca9a7ab9 | |||
0bb74e03aa | |||
8d3f48ab75 | |||
1c11626f0a | |||
05e2cd287e | |||
bac91b426e | |||
a75164c77e | |||
c59e75f873 | |||
4392a7b164 | |||
a659240dc2 | |||
68e9b91e9c | |||
7162b36f2d | |||
73be07672b | |||
e2c0c598a3 | |||
66f88cfa07 | |||
9b6a2577fa | |||
457fe049a8 | |||
6a2e1ae440 | |||
47c973b3aa | |||
56bc8ebb9b | |||
361398bc0d | |||
08131e7ff2 | |||
fd6e8d7ea8 | |||
0ba710affa | |||
72231abe6d | |||
7254040998 | |||
57bf378412 | |||
b58157f024 | |||
ab596db285 | |||
7f92edf3a1 | |||
837579a40c | |||
b241a121a3 | |||
5054a68bfb | |||
d16f050d74 | |||
5900d78cc9 | |||
776275361a | |||
651341d53b | |||
001f2d3f76 | |||
b0242d31f8 | |||
f5de7f9894 | |||
3193f7aae7 | |||
bbec051e51 | |||
f071b66013 | |||
57bc42517b | |||
bb2ac91115 | |||
e3888cbe02 | |||
310155832a | |||
ed8a744cd5 | |||
69aec6fb5b | |||
75880fbe2d | |||
65de924493 | |||
eaee6687e0 | |||
f5f3b0d49d | |||
f6cc85a796 | |||
cc600b67f6 | |||
d418bf3951 | |||
4b1d9667af | |||
a02990a39a | |||
403a7c5315 | |||
cc328a4c9c | |||
3d4621bbea | |||
d8f9173390 | |||
df334eacaa | |||
5cfdc2e0a7 | |||
5a9fbca54a | |||
4f8f773b9f | |||
69ed40e401 | |||
ec8692fb9e | |||
b1cd07a7b5 | |||
61e624bc9d | |||
31065389f1 | |||
829c6c3c71 | |||
e145102345 | |||
a1e8e1a30e | |||
efd39a67a2 | |||
d77e6cd6e9 | |||
fa12dc8a22 | |||
a10780ca0d | |||
9373a8e9f5 | |||
e2e99f269b | |||
7b36ca3b6c | |||
d97cc3d96e | |||
6ab69a7dc9 | |||
af12807451 | |||
9d599040b7 | |||
05e6725b7a | |||
ec535ea14e | |||
58bf92e4f7 | |||
a664b28248 | |||
edec3bb07a | |||
770c314dd1 | |||
94b5017e89 | |||
fe069d6de2 | |||
4d4896e553 | |||
d11ef05aaf | |||
6fa073879e | |||
5a3a1ec25c | |||
441e6e1d31 | |||
8bdcb47815 | |||
46e7fea72d | |||
4a7d011317 | |||
4fea054ee0 | |||
7f862ac21c | |||
0808939f81 | |||
19dde1363e | |||
5b43809b48 | |||
947574d3dc | |||
7bfc320bda | |||
7646b31907 | |||
bc0bff3f87 | |||
c28b8556f5 | |||
24d583d5c3 | |||
b2c6497d63 | |||
4e162bd0ea | |||
73dc35cbbd | |||
b1af3d1f7d | |||
92275bdfa8 | |||
56d57d472a | |||
3da86df48f | |||
5ec59eebe4 | |||
a32e3797d4 | |||
e5fbdd6bda | |||
3da189769c | |||
a6846e3b71 | |||
3b4abbfc97 | |||
7f6477ed83 | |||
56b810e91d | |||
6168abd9a0 | |||
888b9dcf30 | |||
e1ab92275c | |||
3c44785e82 | |||
8635365a30 | |||
ceaed32e94 | |||
fba61390d8 | |||
7bdb55f9a8 | |||
b35301659b | |||
60f1504978 | |||
aa75838a5f | |||
5f7070e94f | |||
8f40eec122 | |||
e4f493cd1d | |||
7419649eae | |||
6b6c903585 | |||
9dc93aafaa | |||
ba85da88a0 | |||
901b895c02 | |||
55be521ff8 | |||
c877c1a64f | |||
bc75ac2990 | |||
c3cbf07946 | |||
e15b6aa3e0 | |||
12034b19d7 | |||
369a078c54 | |||
deb64ab676 | |||
1ec5591574 | |||
291bb27f6a | |||
b5935a4aff | |||
9d680b24f0 | |||
2b9e2f71b7 | |||
510e0cd202 | |||
2beb4e7fd6 | |||
d5873b177b | |||
da97f78e25 | |||
48ec654d0c | |||
f291a19fd5 | |||
c12a4c8239 | |||
86ea6fe8c4 | |||
dce34f37db | |||
1b59e918f7 | |||
9cd9c0cd52 | |||
9857a545e2 | |||
54b61ebe7c | |||
0bf561dfe7 | |||
329e90c239 | |||
9f5ec5a69a | |||
df98b0417b | |||
a8eb2fc675 | |||
4a434fdc1b | |||
d18ccfeec7 | |||
f8a2f6e760 | |||
4e55897059 | |||
41facc02ef | |||
865bfb2acf | |||
62877b5d14 | |||
f4ea0270c8 | |||
8f623fb241 | |||
3bbf8d8f7a | |||
90b6d7cb5b | |||
7fa5495d64 | |||
5b8182cc0a | |||
af8b17bd75 | |||
42f046ec19 | |||
987c61e935 | |||
2b72bae3d9 | |||
2b6804aa50 | |||
d9911c8da5 | |||
d287c167bc | |||
79afd56565 | |||
5cc2750ebc | |||
58e7881e98 | |||
e072c789d1 | |||
fd7ac25108 | |||
bfea338d36 | |||
e3b87f40aa | |||
70f86cb6cb | |||
13304d5991 | |||
7b441d2142 | |||
cb5dd28985 | |||
5e565e8046 | |||
b9b6aeeed9 | |||
0607450f78 | |||
aea3a9efe4 | |||
414adfb146 | |||
b5fb6f2d0a | |||
670ab2f178 | |||
48d3243abe | |||
d7f3c4f9d3 | |||
a79306ccd9 | |||
6238f5f9f9 | |||
67274b9594 | |||
11b44c15b5 | |||
7001116be5 | |||
ced914a3b7 | |||
7623d20f69 | |||
33552764ad | |||
953f8c9631 | |||
b0c962911e | |||
1bfbd7bcc8 | |||
065c720c28 | |||
5ff2519e1e | |||
0c9c8d58ab | |||
3b4dd387b8 | |||
61574a1818 | |||
be2ed243f6 | |||
ba57202ed7 | |||
f21309f52f | |||
f581318dd5 | |||
4d2a4d02b0 | |||
9b2d35d1ac | |||
0f582e1708 | |||
d32d937d47 | |||
f46c909b5a | |||
a947e731c3 | |||
122f449960 | |||
6bcc6c31dd | |||
3e8c8d248d | |||
301c34373d | |||
58970e2a37 | |||
c1e0bab4f8 | |||
d5eebf7214 | |||
70319e27ee | |||
8a97065e30 | |||
adf44419c0 | |||
5219f75719 | |||
a485ecd7ba | |||
030de805e1 | |||
aa6aec90e1 | |||
3e158a2313 | |||
68973aed1e | |||
bcdfb703d5 | |||
27281e9130 | |||
098294beec | |||
b02f8ac07d | |||
e52ffc4447 | |||
c5cdd5ad73 | |||
c0630f8169 | |||
6db8d9d5a5 | |||
71438559ae | |||
c0f0edf044 | |||
fa7357b483 | |||
91cb82d3d2 | |||
7672b974ff | |||
8489364528 | |||
0d0accfa9f | |||
623bad2c8b | |||
f66cf8f0dd | |||
4e25f51581 | |||
a46b309975 | |||
f99a96e0a2 | |||
90834bff8e | |||
a910f6a247 | |||
caa5c0cc76 | |||
5e5cb86e83 | |||
2be7838fc6 | |||
f6eb341b15 | |||
b2fe43cf7a | |||
648a246be0 | |||
2e5ae28c39 | |||
5025d75160 | |||
c79ea6d1d3 | |||
e926efd62e | |||
17d1e738fa | |||
9e5d3bbe25 | |||
f3c7dc9d89 | |||
c5e0c3d7e2 | |||
ea47b6e0d6 | |||
9e1c535b1d | |||
cfd207cae5 | |||
f5d334d9bf | |||
f03823cde5 | |||
4dd659edad | |||
1ab24d26ee | |||
a7120116b0 | |||
83f34e7fa4 | |||
d0cfb98133 | |||
13f7321def | |||
46495abb49 | |||
f7cb00d6d3 | |||
7dae6c7a6b | |||
2b209aaaf0 | |||
977cbab8e7 | |||
09086fbe0a | |||
ec614c95dd | |||
21b18eb294 | |||
faa09884db | |||
b740076ab1 | |||
c9fea2ef67 | |||
818ef2e692 | |||
5f97b12576 | |||
1dea9111a2 | |||
174e6c3cab | |||
eefcfeed23 | |||
18129480ae | |||
1b1ff8995b | |||
867ae5148d | |||
72e91845e4 | |||
537b59d4d1 | |||
7ff12b8fcb | |||
574665b45b | |||
437890d386 | |||
1edf72c040 | |||
7a54aca468 | |||
81f7da3b23 | |||
5ea2a76d80 | |||
5044fce1ff | |||
038e11da4b | |||
225804c147 | |||
96cbd48df6 | |||
e45e31c218 | |||
371c783344 | |||
47b254ea51 | |||
912c0bb9c9 | |||
9897fa8819 | |||
5a6e7dd452 | |||
90eb1165d1 | |||
cf9b9e2afa | |||
e7869b9f14 | |||
4c7ad7e194 | |||
f8a40b3f24 | |||
82bb6627c9 | |||
d1dd9f5aec | |||
e7f0860d85 | |||
954051f240 | |||
39f93f09f9 | |||
2d0c3b70f8 | |||
4252ffa43e | |||
8cacc85913 | |||
588e6a4d4c | |||
fa6a17755a | |||
52cb5ecec7 | |||
1565a2815d | |||
14de71898e | |||
5fa8493675 | |||
b31a973fa7 | |||
9ac3592190 | |||
35011d2e4d | |||
60f01bdd86 | |||
15a2388d75 | |||
68b4c0388a | |||
eb4bfe1366 | |||
e7ca4b7b04 | |||
fc36239be5 | |||
f493219c7d | |||
aa199120c7 | |||
ae30f97af6 | |||
47c342a3e4 | |||
7eac2cfd8b | |||
a0c216bf4b | |||
06d9ba42de | |||
573c7c20c4 | |||
d5cbf79f2f | |||
ce5be709d5 | |||
9829bf69cd | |||
c8282e6e76 | |||
a73aa7eec1 | |||
1fe8f09caa | |||
47509cf927 | |||
5b96a11a1f | |||
7faa1dcab9 | |||
65af37f7b0 | |||
d6361136e1 | |||
e05ef23743 | |||
73a9d494fb | |||
94f10d3c50 | |||
c4bbc8e236 | |||
6dffb72ce0 | |||
eb73441032 | |||
9a6a147369 | |||
5c2cc50455 | |||
58eb1ea7c3 | |||
719ca71d4a | |||
c542e5d86a | |||
a9074c7444 | |||
b2f26d30ee | |||
75cd4ab7a5 | |||
33602889c1 | |||
98d28d7aa0 | |||
e4b837e0c4 | |||
2b9c5a62cb | |||
99e89743bd | |||
39a039fa42 | |||
fcf23101dd | |||
c8898df3dd | |||
b2961915a6 | |||
6f3fd7834a | |||
f78af2c9c8 | |||
eecaf1e93b | |||
950ada4cba | |||
0d4239ef56 | |||
e3a9356178 | |||
5118e21c6e | |||
40455b5c18 | |||
fe6755ff4c | |||
f57d217e91 | |||
e0a75ededa | |||
2496cd38ad | |||
965e53a164 | |||
dc85742034 | |||
196f177cfe | |||
b9afcdfd92 | |||
3bb874fcec | |||
feb82eed33 | |||
0ef15c111a | |||
d341d94976 | |||
0265c28e6e | |||
5bd8cb84de | |||
ebeeae19a6 | |||
5ede167835 | |||
49324ea412 | |||
21e51c8cf6 | |||
e7a2abb03b | |||
b862de1f5b | |||
8090531acd | |||
49253c5dbb | |||
c5ac17711d | |||
1d62e9d8cc | |||
e3195c246f | |||
2a3bc608dc | |||
90ab32f046 | |||
7434a682e5 | |||
0fbb5f90a1 | |||
5e6627c895 | |||
f872fa9b0b | |||
d5668f536d | |||
a5db176903 | |||
7fbbf83011 | |||
42e368e964 | |||
9c9a8cddce | |||
fd7ee2e083 | |||
179d32cfaf | |||
f2b7a31509 | |||
d8312a09a3 | |||
813c9f1545 | |||
ef9a154d09 | |||
0da72fad00 | |||
71fa7c6674 | |||
f3f24e03ae | |||
bc21514ecf | |||
04e05907c3 | |||
61e060694d | |||
602b9807eb | |||
f372a4c4ab | |||
d18dcc0c7f | |||
74c57eef0e | |||
fdbddc4b8c | |||
ca3b44bf60 | |||
dca46febc9 | |||
1a1bd1448d | |||
9620f79cdc | |||
583b560f71 | |||
db3dafd64d | |||
bb2c5303ae | |||
5038dcc251 | |||
69c00ebbe4 | |||
a0e7d5e0aa | |||
51ec2a25fe | |||
5d1e3be7d4 | |||
9d42e4a2e2 | |||
cc75dd3612 | |||
5f621c5adc | |||
29b9015f51 | |||
e6619f5514 | |||
6b4a17b3e0 | |||
30abe062f2 | |||
dff239267f | |||
8b64195cf2 | |||
9caa51b3a5 | |||
5513073a53 | |||
659d26c231 | |||
648800f07f | |||
23bf4f0c13 | |||
57ac7e39bf | |||
272ab6c8d8 | |||
0731b47655 | |||
3b7d8e8b5d | |||
a35c2a2067 | |||
55b2ead967 | |||
e1a0ec3724 | |||
66896dbde6 | |||
338bc1f8e7 | |||
ab80054e97 | |||
a6d2cd69f8 | |||
6996db1e3a | |||
e30c3ac01b | |||
a5477a4eaf | |||
7a40498cb4 | |||
65f1adbe65 | |||
c8454271e1 | |||
62f83a0dc2 | |||
611fa5c7f1 | |||
4defbc2174 | |||
33a39fae06 | |||
e4eab9ec0c | |||
6aaa87f143 | |||
08f97eb4ef | |||
8d18848fb0 | |||
805078e0a9 | |||
52dafafd10 | |||
dba7524b37 | |||
617bf0aa41 | |||
dce19b074b | |||
edbf162e4c | |||
b23c53df5f | |||
cea0c4a9d6 | |||
879e8dcd6d | |||
729c7bde0c | |||
8982315b4e | |||
0f556c16f5 | |||
7ec277e1e1 | |||
1a627872f0 | |||
592836c4dc | |||
68defc2c6a | |||
d225555830 | |||
251e689283 | |||
01f4be6b4d | |||
dc28449d81 | |||
3ad0d4b310 | |||
4dbf067b4b | |||
5567f0ab3b | |||
0c34706799 | |||
f9fc2fc9ee | |||
a2d7e8977a | |||
73cace360d | |||
c96636d192 | |||
d56ff5a351 | |||
3028bc8081 | |||
91295a0790 | |||
d600c60779 | |||
e3bc5b564d | |||
53eac36a3f | |||
ce5c76d79b | |||
b7ffdbbb92 | |||
8f1eacdab7 | |||
1366e833a1 | |||
6930feac43 | |||
54275ad181 | |||
21ca7eaf1b | |||
a86779198f | |||
ac05ef6f95 | |||
74251fca9f | |||
0a445c7656 | |||
a710f32a6a | |||
9689763e20 | |||
1a63a915f2 | |||
4071c4645d | |||
f56db26d8b | |||
0dd2b9a74c | |||
4cd2b95a23 | |||
7ab88416ac | |||
ba625063e7 | |||
b4b93ccb21 | |||
01643e06d3 | |||
4387454fe0 | |||
55fdbc6dbc | |||
e69c9ee38c | |||
894c3f787d | |||
a7797918b8 | |||
46975107a7 | |||
e0f3e8a492 | |||
158322e1e4 | |||
2438848487 | |||
f5432a76b6 | |||
800d278369 | |||
8507af6f4a | |||
7c98ad6f9b | |||
d774dbc1b7 | |||
1a93cc3b9d | |||
e26e63e9b0 | |||
0f69f12b94 | |||
a8ffc6fcfc | |||
7fbdb79a08 | |||
6fad8ff32a | |||
b9a60372f2 | |||
eb20e32914 | |||
f41a4c9acb | |||
767f23c3a9 | |||
f8678ad7e9 | |||
63c18b29e4 | |||
99df8b86ae | |||
66fd29cb58 | |||
3d40e2217d | |||
16dff7c2e6 | |||
88e2d42ba4 | |||
69701430c1 | |||
6dc24fa9f5 | |||
3e1665bbbd | |||
2b96ab8edc | |||
7503356e03 | |||
22ef0de7ef | |||
5d7fec2027 | |||
6423864160 | |||
fd064dac6c | |||
d9bd550414 | |||
94eca2ce44 | |||
e65fd664d1 | |||
7a3789f1a9 | |||
b31f36bf89 | |||
3d583ab19c | |||
616be1d0fc | |||
7c81229261 | |||
452e1c0180 | |||
31bc022d0d | |||
ce1078bc00 | |||
6d0589d14f | |||
ef121e690c | |||
c0340053d1 | |||
39f323b5ad | |||
22c76a3da4 | |||
a332352dbc | |||
ef70767475 | |||
1cecf9efc5 | |||
1f143393e5 | |||
6d1f77132c | |||
2e195d7cb2 | |||
947a897238 | |||
1edbc89749 | |||
0faa844a75 | |||
3db3ad7d1e | |||
4743e9b0b8 | |||
8499696021 | |||
e4e56511b9 | |||
5960ecfc10 | |||
c341ab2ecf | |||
c62ffedfca | |||
a6d46c17b1 | |||
fa96086a49 | |||
4c06b0919a | |||
8429157ab5 | |||
2605b8319e | |||
91f6880734 | |||
b3332184cf | |||
1cb6e1407c | |||
a5e0bbcb62 | |||
3b7829b011 | |||
bea89a0bf2 | |||
cda6f575f0 | |||
b5bcfa1168 | |||
96f1151ab2 | |||
f5be0d30f7 | |||
c8c233f900 | |||
8c59f41d02 | |||
74efea91d1 | |||
70077039b4 | |||
558087399f | |||
1c8481bff6 | |||
118f28344d | |||
523e024ba0 | |||
99e44eb8e1 | |||
4223e643dc | |||
26422257f5 | |||
d6eac28955 | |||
5cab2a7844 | |||
843aad4382 | |||
5e725df892 | |||
f82862ec9c | |||
10e1c1895c | |||
0ac054a74f | |||
6f36a88993 | |||
bf9956b634 | |||
3a5f7d6cae | |||
0353c921bd | |||
72310dd5a3 | |||
7f4ab26732 | |||
700847e295 | |||
52aa17fa68 |
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
|
12
.gitattributes
vendored
Normal file
12
.gitattributes
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
/.github export-ignore
|
||||
/doc export-ignore
|
||||
/test export-ignore
|
||||
/test_old export-ignore
|
||||
/tools export-ignore
|
||||
.editorconfig export-ignore
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
CHANGELOG.md export-ignore
|
||||
CONTRIBUTING.md export-ignore
|
||||
phpunit.xml.dist export-ignore
|
||||
UPGRADE-*.md export-ignore
|
119
.github/workflows/main.yml
vendored
Normal file
119
.github/workflows/main.yml
vendored
Normal file
@ -0,0 +1,119 @@
|
||||
# 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.4 Unit Tests (with coverage)"
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v3"
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
coverage: "xdebug"
|
||||
php-version: "7.4"
|
||||
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:
|
||||
- "8.0"
|
||||
- "8.1"
|
||||
- "8.2"
|
||||
- "8.3"
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v3"
|
||||
- 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.4 Code on PHP 8.2 Integration Tests"
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v3"
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
coverage: "none"
|
||||
php-version: "8.2"
|
||||
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.4.33"
|
||||
test_old_80_70:
|
||||
runs-on: "ubuntu-latest"
|
||||
name: "PHP 8.3 Code on PHP 7.4 Integration Tests"
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v3"
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
coverage: "none"
|
||||
php-version: "7.4"
|
||||
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.3.0RC2"
|
||||
phpstan:
|
||||
runs-on: "ubuntu-latest"
|
||||
name: "PHPStan"
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v3"
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
coverage: "none"
|
||||
php-version: "8.2"
|
||||
tools: composer:v2
|
||||
- name: "Install dependencies"
|
||||
run: |
|
||||
cd tools && composer install
|
||||
- name: "PHPStan"
|
||||
run: "php tools/vendor/bin/phpstan"
|
||||
php-cs-fixer:
|
||||
runs-on: "ubuntu-latest"
|
||||
name: "PHP-CS-Fixer"
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v3"
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
coverage: "none"
|
||||
php-version: "8.2"
|
||||
tools: composer:v2
|
||||
- name: "Install dependencies"
|
||||
run: |
|
||||
cd tools && composer install
|
||||
- name: "php-cs-fixer"
|
||||
run: "php tools/vendor/bin/php-cs-fixer fix"
|
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.idea/
|
||||
vendor/
|
||||
composer.lock
|
||||
grammar/kmyacc.exe
|
||||
grammar/y.output
|
||||
.phpunit.result.cache
|
||||
.php-cs-fixer.cache
|
36
.php-cs-fixer.dist.php
Normal file
36
.php-cs-fixer.dist.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?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'],
|
||||
'binary_operator_spaces' => [
|
||||
'default' => 'at_least_single_space',
|
||||
// Work around https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7303.
|
||||
'operators' => ['=' => null],
|
||||
],
|
||||
'phpdoc_align' => ['align' => 'left'],
|
||||
'phpdoc_trim' => true,
|
||||
'no_empty_phpdoc' => true,
|
||||
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
|
||||
'no_extra_blank_lines' => true,
|
||||
])
|
||||
->setFinder($finder)
|
||||
;
|
@ -1,7 +0,0 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.2
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
1210
CHANGELOG.md
1210
CHANGELOG.md
File diff suppressed because it is too large
Load Diff
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.
|
46
LICENSE
46
LICENSE
@ -1,31 +1,29 @@
|
||||
Copyright (c) 2011 by Nikita Popov.
|
||||
BSD 3-Clause License
|
||||
|
||||
Some rights reserved.
|
||||
Copyright (c) 2011, Nikita Popov
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* The names of the contributors may not be used to endorse or
|
||||
promote products derived from this software without specific
|
||||
prior written permission.
|
||||
3. Neither the name of the copyright holder nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
10
Makefile
Normal file
10
Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
.PHONY: phpstan php-cs-fixer
|
||||
|
||||
tools/vendor:
|
||||
composer install -d tools
|
||||
|
||||
phpstan: tools/vendor
|
||||
tools/vendor/bin/phpstan
|
||||
|
||||
php-cs-fixer: tools/vendor
|
||||
tools/vendor/bin/php-cs-fixer fix
|
252
README.md
252
README.md
@ -1,78 +1,236 @@
|
||||
PHP Parser
|
||||
==========
|
||||
|
||||
This is a PHP 5.5 (and older) parser written in PHP. It's purpose is to simplify static code analysis and
|
||||
[](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
|
||||
|
||||
This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and
|
||||
manipulation.
|
||||
|
||||
Documentation can be found in the [`doc/`][1] directory.
|
||||
[Documentation for version 5.x][doc_master] (in development; for running on PHP >= 7.4; for parsing PHP 7.0 to PHP 8.3, with limited support for parsing PHP 5.x).
|
||||
|
||||
***Note: This project is experimental, so the API is subject to change.***
|
||||
[**Documentation for version 4.x**][doc_4_x] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.3).
|
||||
|
||||
In a Nutshell
|
||||
-------------
|
||||
[Documentation for version 3.x][doc_3_x] (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
|
||||
|
||||
Basically, the parser does nothing more than turn some PHP code into an abstract syntax tree. ("nothing
|
||||
more" is kind of sarcastic here as PHP has a ... uhm, let's just say "not nice" ... grammar, which makes
|
||||
parsing PHP very hard.)
|
||||
Features
|
||||
--------
|
||||
|
||||
For example, if you stick this code in the parser:
|
||||
The main features provided by this library are:
|
||||
|
||||
* Parsing PHP 7, and PHP 8 code into an abstract syntax tree (AST).
|
||||
* Invalid code can be parsed into a partial AST.
|
||||
* The AST contains accurate location information.
|
||||
* Dumping the AST in human-readable form.
|
||||
* Converting an AST back to PHP code.
|
||||
* Formatting can be preserved for partially changed ASTs.
|
||||
* Infrastructure to traverse and modify ASTs.
|
||||
* Resolution of namespaced names.
|
||||
* Evaluation of constant expressions.
|
||||
* Builders to simplify AST construction for code generation.
|
||||
* Converting an AST into JSON and back.
|
||||
|
||||
Quick Start
|
||||
-----------
|
||||
|
||||
Install the library using [composer](https://getcomposer.org):
|
||||
|
||||
php composer.phar require nikic/php-parser
|
||||
|
||||
Parse some PHP code into an AST and dump the result in human-readable form:
|
||||
|
||||
```php
|
||||
<?php
|
||||
echo 'Hi', 'World';
|
||||
hello\world('foo', 'bar' . 'baz');
|
||||
use PhpParser\Error;
|
||||
use PhpParser\NodeDumper;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
|
||||
function test($foo)
|
||||
{
|
||||
var_dump($foo);
|
||||
}
|
||||
CODE;
|
||||
|
||||
$parser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||
try {
|
||||
$ast = $parser->parse($code);
|
||||
} catch (Error $error) {
|
||||
echo "Parse error: {$error->getMessage()}\n";
|
||||
return;
|
||||
}
|
||||
|
||||
$dumper = new NodeDumper;
|
||||
echo $dumper->dump($ast) . "\n";
|
||||
```
|
||||
|
||||
You'll get a syntax tree looking roughly like this:
|
||||
This dumps an AST looking something like this:
|
||||
|
||||
```
|
||||
array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Scalar_String(
|
||||
value: Hi
|
||||
)
|
||||
1: Scalar_String(
|
||||
value: World
|
||||
0: Stmt_Function(
|
||||
attrGroups: array(
|
||||
)
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: test
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
attrGroups: array(
|
||||
)
|
||||
flags: 0
|
||||
type: null
|
||||
byRef: false
|
||||
variadic: false
|
||||
var: Expr_Variable(
|
||||
name: foo
|
||||
)
|
||||
default: null
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: hello
|
||||
1: world
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Scalar_String(
|
||||
value: foo
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
1: Arg(
|
||||
value: Expr_Concat(
|
||||
left: Scalar_String(
|
||||
value: bar
|
||||
returnType: null
|
||||
stmts: array(
|
||||
0: Stmt_Expression(
|
||||
expr: Expr_FuncCall(
|
||||
name: Name(
|
||||
name: var_dump
|
||||
)
|
||||
right: Scalar_String(
|
||||
value: baz
|
||||
args: array(
|
||||
0: Arg(
|
||||
name: null
|
||||
value: Expr_Variable(
|
||||
name: foo
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
You can then work with this syntax tree, for example to statically analyze the code (e.g. to find
|
||||
programming errors or security issues).
|
||||
Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
|
||||
|
||||
Additionally, you can convert a syntax tree back to PHP code. This allows you to do code preprocessing
|
||||
(like automatedly porting code to older PHP versions).
|
||||
```php
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
So, that's it, in a nutshell. You can find everything else in the [docs][1].
|
||||
$traverser = new NodeTraverser();
|
||||
$traverser->addVisitor(new class extends NodeVisitorAbstract {
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Function_) {
|
||||
// Clean out the function body
|
||||
$node->stmts = [];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
[1]: https://github.com/nikic/PHP-Parser/tree/master/doc
|
||||
$ast = $traverser->traverse($ast);
|
||||
echo $dumper->dump($ast) . "\n";
|
||||
```
|
||||
|
||||
This gives us an AST where the `Function_::$stmts` are empty:
|
||||
|
||||
```
|
||||
array(
|
||||
0: Stmt_Function(
|
||||
attrGroups: array(
|
||||
)
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: test
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
attrGroups: array(
|
||||
)
|
||||
type: null
|
||||
byRef: false
|
||||
variadic: false
|
||||
var: Expr_Variable(
|
||||
name: foo
|
||||
)
|
||||
default: null
|
||||
)
|
||||
)
|
||||
returnType: null
|
||||
stmts: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
Finally, we can convert the new AST back to PHP code:
|
||||
|
||||
```php
|
||||
use PhpParser\PrettyPrinter;
|
||||
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
echo $prettyPrinter->prettyPrintFile($ast);
|
||||
```
|
||||
|
||||
This gives us our original code, minus the `var_dump()` call inside the function:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
function test($foo)
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
For a more comprehensive introduction, see the documentation.
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
|
||||
1. [Introduction](doc/0_Introduction.markdown)
|
||||
2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
|
||||
|
||||
Component documentation:
|
||||
|
||||
* [Walking the AST](doc/component/Walking_the_AST.markdown)
|
||||
* Node visitors
|
||||
* Modifying the AST from a visitor
|
||||
* Short-circuiting traversals
|
||||
* Interleaved visitors
|
||||
* Simple node finding API
|
||||
* Parent and sibling references
|
||||
* [Name resolution](doc/component/Name_resolution.markdown)
|
||||
* Name resolver options
|
||||
* Name resolution context
|
||||
* [Pretty printing](doc/component/Pretty_printing.markdown)
|
||||
* Converting AST back to PHP code
|
||||
* Customizing formatting
|
||||
* Formatting-preserving code transformations
|
||||
* [AST builders](doc/component/AST_builders.markdown)
|
||||
* Fluent builders for AST nodes
|
||||
* [Lexer](doc/component/Lexer.markdown)
|
||||
* Lexer options
|
||||
* Token and file positions for nodes
|
||||
* Custom attributes
|
||||
* [Error handling](doc/component/Error_handling.markdown)
|
||||
* Column information for errors
|
||||
* Error recovery (parsing of syntactically incorrect code)
|
||||
* [Constant expression evaluation](doc/component/Constant_expression_evaluation.markdown)
|
||||
* Evaluating constant/property/etc initializers
|
||||
* Handling errors and unsupported expressions
|
||||
* [JSON representation](doc/component/JSON_representation.markdown)
|
||||
* JSON encoding and decoding of ASTs
|
||||
* [Performance](doc/component/Performance.markdown)
|
||||
* Disabling Xdebug
|
||||
* Reusing objects
|
||||
* Garbage collection impact
|
||||
* [Frequently asked questions](doc/component/FAQ.markdown)
|
||||
* Parent and sibling references
|
||||
|
||||
[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
|
||||
|
121
UPGRADE-1.0.md
Normal file
121
UPGRADE-1.0.md
Normal file
@ -0,0 +1,121 @@
|
||||
Upgrading from PHP-Parser 0.9 to 1.0
|
||||
====================================
|
||||
|
||||
### PHP version requirements
|
||||
|
||||
PHP-Parser now requires PHP 5.3 or newer to run. It is however still possible to *parse* PHP 5.2 source code, while
|
||||
running on a newer version.
|
||||
|
||||
### Move to namespaced names
|
||||
|
||||
The library has been moved to use namespaces with the `PhpParser` vendor prefix. However, the old names using
|
||||
underscores are still available as aliases, as such most code should continue running on the new version without
|
||||
further changes.
|
||||
|
||||
Old (still works, but discouraged):
|
||||
|
||||
```php
|
||||
$parser = new \PHPParser_Parser(new \PHPParser_Lexer_Emulative);
|
||||
$prettyPrinter = new \PHPParser_PrettyPrinter_Default;
|
||||
```
|
||||
|
||||
New:
|
||||
|
||||
```php
|
||||
$parser = new \PhpParser\Parser(new PhpParser\Lexer\Emulative);
|
||||
$prettyPrinter = new \PhpParser\PrettyPrinter\Standard;
|
||||
```
|
||||
|
||||
Note that the `PHPParser` prefix was changed to `PhpParser`. While PHP class names are technically case-insensitive,
|
||||
the autoloader will not be able to load `PHPParser\Parser` or other case variants.
|
||||
|
||||
Due to conflicts with reserved keywords, some class names now end with an underscore, e.g. `PHPParser_Node_Stmt_Class`
|
||||
is now `PhpParser\Node\Stmt\Class_`. (But as usual, the old name is still available.)
|
||||
|
||||
### Changes to `Node::getType()`
|
||||
|
||||
The `Node::getType()` method continues to return names using underscores instead of namespace separators and also does
|
||||
not contain the trailing underscore that may be present in the class name. As such its output will not change in many
|
||||
cases.
|
||||
|
||||
However, some node classes have been moved to a different namespace or renamed, which will result in a different
|
||||
`Node::getType()` output:
|
||||
|
||||
```
|
||||
Expr_AssignBitwiseAnd => Expr_AssignOp_BitwiseAnd
|
||||
Expr_AssignBitwiseOr => Expr_AssignOp_BitwiseOr
|
||||
Expr_AssignBitwiseXor => Expr_AssignOp_BitwiseXor
|
||||
Expr_AssignConcat => Expr_AssignOp_Concat
|
||||
Expr_AssignDiv => Expr_AssignOp_Div
|
||||
Expr_AssignMinus => Expr_AssignOp_Minus
|
||||
Expr_AssignMod => Expr_AssignOp_Mod
|
||||
Expr_AssignMul => Expr_AssignOp_Mul
|
||||
Expr_AssignPlus => Expr_AssignOp_Plus
|
||||
Expr_AssignShiftLeft => Expr_AssignOp_ShiftLeft
|
||||
Expr_AssignShiftRight => Expr_AssignOp_ShiftRight
|
||||
|
||||
Expr_BitwiseAnd => Expr_BinaryOp_BitwiseAnd
|
||||
Expr_BitwiseOr => Expr_BinaryOp_BitwiseOr
|
||||
Expr_BitwiseXor => Expr_BinaryOp_BitwiseXor
|
||||
Expr_BooleanAnd => Expr_BinaryOp_BooleanAnd
|
||||
Expr_BooleanOr => Expr_BinaryOp_BooleanOr
|
||||
Expr_Concat => Expr_BinaryOp_Concat
|
||||
Expr_Div => Expr_BinaryOp_Div
|
||||
Expr_Equal => Expr_BinaryOp_Equal
|
||||
Expr_Greater => Expr_BinaryOp_Greater
|
||||
Expr_GreaterOrEqual => Expr_BinaryOp_GreaterOrEqual
|
||||
Expr_Identical => Expr_BinaryOp_Identical
|
||||
Expr_LogicalAnd => Expr_BinaryOp_LogicalAnd
|
||||
Expr_LogicalOr => Expr_BinaryOp_LogicalOr
|
||||
Expr_LogicalXor => Expr_BinaryOp_LogicalXor
|
||||
Expr_Minus => Expr_BinaryOp_Minus
|
||||
Expr_Mod => Expr_BinaryOp_Mod
|
||||
Expr_Mul => Expr_BinaryOp_Mul
|
||||
Expr_NotEqual => Expr_BinaryOp_NotEqual
|
||||
Expr_NotIdentical => Expr_BinaryOp_NotIdentical
|
||||
Expr_Plus => Expr_BinaryOp_Plus
|
||||
Expr_ShiftLeft => Expr_BinaryOp_ShiftLeft
|
||||
Expr_ShiftRight => Expr_BinaryOp_ShiftRight
|
||||
Expr_Smaller => Expr_BinaryOp_Smaller
|
||||
Expr_SmallerOrEqual => Expr_BinaryOp_SmallerOrEqual
|
||||
|
||||
Scalar_ClassConst => Scalar_MagicConst_Class
|
||||
Scalar_DirConst => Scalar_MagicConst_Dir
|
||||
Scalar_FileConst => Scalar_MagicConst_File
|
||||
Scalar_FuncConst => Scalar_MagicConst_Function
|
||||
Scalar_LineConst => Scalar_MagicConst_Line
|
||||
Scalar_MethodConst => Scalar_MagicConst_Method
|
||||
Scalar_NSConst => Scalar_MagicConst_Namespace
|
||||
Scalar_TraitConst => Scalar_MagicConst_Trait
|
||||
```
|
||||
|
||||
These changes may affect custom pretty printers and code comparing the return value of `Node::getType()` to specific
|
||||
strings.
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
* The classes `Template` and `TemplateLoader` have been removed. You should use some other [code generation][code_gen]
|
||||
project built on top of PHP-Parser instead.
|
||||
|
||||
* The `PrettyPrinterAbstract::pStmts()` method now emits a leading newline if the statement list is not empty.
|
||||
Custom pretty printers should remove the explicit newline before `pStmts()` calls.
|
||||
|
||||
Old:
|
||||
|
||||
```php
|
||||
public function pStmt_Trait(PHPParser_Node_Stmt_Trait $node) {
|
||||
return 'trait ' . $node->name
|
||||
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
```
|
||||
|
||||
New:
|
||||
|
||||
```php
|
||||
public function pStmt_Trait(Stmt\Trait_ $node) {
|
||||
return 'trait ' . $node->name
|
||||
. "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
```
|
||||
|
||||
[code_gen]: https://github.com/nikic/PHP-Parser/wiki/Projects-using-the-PHP-Parser#code-generation
|
74
UPGRADE-2.0.md
Normal file
74
UPGRADE-2.0.md
Normal file
@ -0,0 +1,74 @@
|
||||
Upgrading from PHP-Parser 1.x to 2.0
|
||||
====================================
|
||||
|
||||
### PHP version requirements
|
||||
|
||||
PHP-Parser now requires PHP 5.4 or newer to run. It is however still possible to *parse* PHP 5.2 and
|
||||
PHP 5.3 source code, while running on a newer version.
|
||||
|
||||
### Creating a parser instance
|
||||
|
||||
Parser instances should now be created through the `ParserFactory`. Old direct instantiation code
|
||||
will not work, because the parser class was renamed.
|
||||
|
||||
Old:
|
||||
|
||||
```php
|
||||
use PhpParser\Parser, PhpParser\Lexer;
|
||||
$parser = new Parser(new Lexer\Emulative);
|
||||
```
|
||||
|
||||
New:
|
||||
|
||||
```php
|
||||
use PhpParser\ParserFactory;
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
```
|
||||
|
||||
The first argument to `ParserFactory` determines how different PHP versions are handled. The
|
||||
possible values are:
|
||||
|
||||
* `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.
|
||||
|
||||
For most practical purposes the difference between `PREFER_PHP7` and `PREFER_PHP5` is mainly whether
|
||||
a scalar type hint like `string` will be stored as `'string'` (PHP 7) or as `new Name('string')`
|
||||
(PHP 5).
|
||||
|
||||
To use a custom lexer, pass it as the second argument to the `create()` method:
|
||||
|
||||
```php
|
||||
use PhpParser\ParserFactory;
|
||||
$lexer = new MyLexer;
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer);
|
||||
```
|
||||
|
||||
### Rename of the `PhpParser\Parser` class
|
||||
|
||||
`PhpParser\Parser` is now an interface, which is implemented by `Parser\Php5`, `Parser\Php7` and
|
||||
`Parser\Multiple`. Parser tokens are now defined in `Parser\Tokens`. If you use the `ParserFactory`
|
||||
described above to create your parser instance, these changes should have no further impact on you.
|
||||
|
||||
### Removal of legacy aliases
|
||||
|
||||
All legacy aliases for classes have been removed. This includes the old non-namespaced `PHPParser_`
|
||||
classes, as well as the classes that had to be renamed for PHP 7 support.
|
||||
|
||||
### Deprecations
|
||||
|
||||
The `set()`, `setFirst()`, `append()` and `prepend()` methods of the `Node\Name` class have been
|
||||
deprecated. Instead `Name::concat()` and `Name->slice()` should be used.
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
* The `NodeTraverser` no longer clones nodes by default. If you want to restore the old behavior,
|
||||
pass `true` to the constructor.
|
||||
* The legacy node format has been removed. If you use custom nodes, they are now expected to
|
||||
implement a `getSubNodeNames()` method.
|
||||
* The default value for `Scalar` node constructors was removed. This means that something like
|
||||
`new LNumber()` should be replaced by `new LNumber(0)`.
|
||||
* String parts of encapsed strings are now represented using `Scalar\EncapsStringPart` nodes, while
|
||||
previously raw strings were used. This affects the `parts` child of `Scalar\Encaps` and
|
||||
`Expr\ShellExec`.
|
160
UPGRADE-3.0.md
Normal file
160
UPGRADE-3.0.md
Normal file
@ -0,0 +1,160 @@
|
||||
Upgrading from PHP-Parser 2.x to 3.0
|
||||
====================================
|
||||
|
||||
The backwards-incompatible changes in this release may be summarized as follows:
|
||||
|
||||
* The specific details of the node representation have changed in some cases, primarily to
|
||||
accommodate new PHP 7.1 features.
|
||||
* There have been significant changes to the error recovery implementation. This may affect you,
|
||||
if you used the error recovery mode or have a custom lexer implementation.
|
||||
* A number of deprecated methods were removed.
|
||||
|
||||
### PHP version requirements
|
||||
|
||||
PHP-Parser now requires PHP 5.5 or newer to run. It is however still possible to *parse* PHP 5.2,
|
||||
5.3 and 5.4 source code, while running on a newer version.
|
||||
|
||||
### Changes to the node structure
|
||||
|
||||
The following changes are likely to require code changes if the respective nodes are used:
|
||||
|
||||
* The `List` subnode `vars` has been renamed to `items` and now contains `ArrayItem`s instead of
|
||||
plain variables.
|
||||
* The `Catch` subnode `type` has been renamed to `types` and is now an array of `Name`s.
|
||||
* The `TryCatch` subnode `finallyStmts` has been replaced with a `finally` subnode that holds an
|
||||
explicit `Finally` node.
|
||||
* The `type` subnode on `Class`, `ClassMethod` and `Property` has been renamed to `flags`. The
|
||||
`type` subnode has retained for backwards compatibility and is populated to the same value as
|
||||
`flags`. However, writes to `type` will not update `flags` and use of `type` is discouraged.
|
||||
|
||||
The following changes are unlikely to require code changes:
|
||||
|
||||
* The `ClassConst` constructor changed to accept an additional `flags` subnode.
|
||||
* The `Trait` constructor now has the same form as the `Class` and `Interface` constructors: It
|
||||
takes an array of subnodes. Unlike classes/interfaces, traits can only have a `stmts` subnode.
|
||||
* The `Array` subnode `items` may now contain `null` elements (due to destructuring).
|
||||
* `void` and `iterable` types are now stored as strings if the PHP 7 parser is used. Previously
|
||||
these would have been represented as `Name` instances.
|
||||
|
||||
### Changes to error recovery mode
|
||||
|
||||
Previously, error recovery mode was enabled by setting the `throwOnError` option to `false` when
|
||||
creating the parser, while collected errors were retrieved using the `getErrors()` method:
|
||||
|
||||
```php
|
||||
$lexer = ...;
|
||||
$parser = (new ParserFactory)->create(ParserFactor::ONLY_PHP7, $lexer, [
|
||||
'throwOnError' => true,
|
||||
]);
|
||||
|
||||
$stmts = $parser->parse($code);
|
||||
$errors = $parser->getErrors();
|
||||
if ($errors) {
|
||||
handleErrors($errors);
|
||||
}
|
||||
processAst($stmts);
|
||||
```
|
||||
|
||||
Both the `throwOnError` option and the `getErrors()` method have been removed in PHP-Parser 3.0.
|
||||
Instead an instance of `ErrorHandler\Collecting` should be passed to the `parse()` method:
|
||||
|
||||
```php
|
||||
$lexer = ...;
|
||||
$parser = (new ParserFactory)->create(ParserFactor::ONLY_PHP7, $lexer);
|
||||
|
||||
$errorHandler = new ErrorHandler\Collecting;
|
||||
$stmts = $parser->parse($code, $errorHandler);
|
||||
if ($errorHandler->hasErrors()) {
|
||||
handleErrors($errorHandler->getErrors());
|
||||
}
|
||||
processAst($stmts);
|
||||
```
|
||||
|
||||
#### Multiple parser fallback in error recovery mode
|
||||
|
||||
As a result of this change, if a `Multiple` parser is used (e.g. through the `ParserFactory` using
|
||||
`PREFER_PHP7` or `PREFER_PHP5`), it will now return the result of the first *non-throwing* parse. As
|
||||
parsing never throws in error recovery mode, the result from the first parser will always be
|
||||
returned.
|
||||
|
||||
The PHP 7 parser is a superset of the PHP 5 parser, with the exceptions that `=& new` and
|
||||
`global $$foo->bar` are not supported (other differences are in representation only). The PHP 7
|
||||
parser will be able to recover from the error in both cases. For this reason, this change will
|
||||
likely pass unnoticed if you do not specifically test for this syntax.
|
||||
|
||||
It is possible to restore the precise previous behavior with the following code:
|
||||
|
||||
```php
|
||||
$lexer = ...;
|
||||
$parser7 = new Parser\Php7($lexer);
|
||||
$parser5 = new Parser\Php5($lexer);
|
||||
|
||||
$errors7 = new ErrorHandler\Collecting();
|
||||
$stmts7 = $parser7->parse($code, $errors7);
|
||||
if ($errors7->hasErrors()) {
|
||||
$errors5 = new ErrorHandler\Collecting();
|
||||
$stmts5 = $parser5->parse($code, $errors5);
|
||||
if (!$errors5->hasErrors()) {
|
||||
// If PHP 7 parse has errors but PHP 5 parse has no errors, use PHP 5 result
|
||||
return [$stmts5, $errors5];
|
||||
}
|
||||
}
|
||||
// If PHP 7 succeeds or both fail use PHP 7 result
|
||||
return [$stmts7, $errors7];
|
||||
```
|
||||
|
||||
#### Error handling in the lexer
|
||||
|
||||
In order to support recovery from lexer errors, the signature of the `startLexing()` method changed
|
||||
to optionally accept an `ErrorHandler`:
|
||||
|
||||
```php
|
||||
// OLD
|
||||
public function startLexing($code);
|
||||
// NEW
|
||||
public function startLexing($code, ErrorHandler $errorHandler = null);
|
||||
```
|
||||
|
||||
If you use a custom lexer with overridden `startLexing()` method, it needs to be changed to accept
|
||||
the extra parameter. The value should be passed on to the parent method.
|
||||
|
||||
#### Error checks in node constructors
|
||||
|
||||
The constructors of certain nodes used to contain additional checks for semantic errors, such as
|
||||
creating a try block without either catch or finally. These checks have been moved from the node
|
||||
constructors into the parser. This allows recovery from such errors, as well as representing the
|
||||
resulting (invalid) AST.
|
||||
|
||||
This means that certain error conditions are no longer checked for manually constructed nodes.
|
||||
|
||||
### Removed methods, arguments, options
|
||||
|
||||
The following methods, arguments or options have been removed:
|
||||
|
||||
* `Comment::setLine()`, `Comment::setText()`: Create new `Comment` instances instead.
|
||||
* `Name::set()`, `Name::setFirst()`, `Name::setLast()`, `Name::append()`, `Name::prepend()`:
|
||||
Use `Name::concat()` in combination with `Name::slice()` instead.
|
||||
* `Error::getRawLine()`, `Error::setRawLine()`. Use `Error::getStartLine()` and
|
||||
`Error::setStartLine()` instead.
|
||||
* `Parser::getErrors()`. Use `ErrorHandler\Collecting` instead.
|
||||
* `$separator` argument of `Name::toString()`. Use `strtr()` instead, if you really need it.
|
||||
* `$cloneNodes` argument of `NodeTraverser::__construct()`. Explicitly clone nodes in the visitor
|
||||
instead.
|
||||
* `throwOnError` parser option. Use `ErrorHandler\Collecting` instead.
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
* The `NameResolver` will now resolve unqualified function and constant names in the global
|
||||
namespace into fully qualified names. For example `foo()` in the global namespace resolves to
|
||||
`\foo()`. For names where no static resolution is possible, a `namespacedName` attribute is
|
||||
added now, containing the namespaced variant of the name.
|
||||
* All methods on `PrettyPrinter\Standard` are now protected. Previously most of them were public.
|
||||
The pretty printer should only be invoked using the `prettyPrint()`, `prettyPrintFile()` and
|
||||
`prettyPrintExpr()` methods.
|
||||
* The node dumper now prints numeric values that act as enums/flags in a string representation.
|
||||
If node dumper results are used in tests, updates may be needed to account for this.
|
||||
* The constants on `NameTraverserInterface` have been moved into the `NameTraverser` class.
|
||||
* The emulative lexer now directly postprocesses tokens, instead of using `~__EMU__~` sequences.
|
||||
This changes the protected API of the emulative lexer.
|
||||
* The `Name::slice()` method now returns `null` for empty slices, previously `new Name([])` was
|
||||
used. `Name::concat()` now also supports concatenation with `null`.
|
77
UPGRADE-4.0.md
Normal file
77
UPGRADE-4.0.md
Normal file
@ -0,0 +1,77 @@
|
||||
Upgrading from PHP-Parser 3.x to 4.0
|
||||
====================================
|
||||
|
||||
### PHP version requirements
|
||||
|
||||
PHP-Parser now requires PHP 7.0 or newer to run. It is however still possible to *parse* PHP 5.2-5.6
|
||||
source code, while running on a newer version.
|
||||
|
||||
HHVM is no longer actively supported.
|
||||
|
||||
### Changes to the node structure
|
||||
|
||||
* Many subnodes that previously held simple strings now store `Identifier` nodes instead (or
|
||||
`VarLikeIdentifier` nodes if they have form `$ident`). The constructors of the affected nodes will
|
||||
automatically convert strings to `Identifier`s and `Identifier`s implement `__toString()`. As such
|
||||
some code continues to work without changes, but anything using `is_string()`, type-strict
|
||||
comparisons or strict-mode may require adjustment. The following is an exhaustive list of all
|
||||
affected subnodes:
|
||||
|
||||
* `Const_::$name`
|
||||
* `NullableType::$type` (for simple types)
|
||||
* `Param::$type` (for simple types)
|
||||
* `Expr\ClassConstFetch::$name`
|
||||
* `Expr\Closure::$returnType` (for simple types)
|
||||
* `Expr\MethodCall::$name`
|
||||
* `Expr\PropertyFetch::$name`
|
||||
* `Expr\StaticCall::$name`
|
||||
* `Expr\StaticPropertyFetch::$name` (uses `VarLikeIdentifier`)
|
||||
* `Stmt\Class_::$name`
|
||||
* `Stmt\ClassMethod::$name`
|
||||
* `Stmt\ClassMethod::$returnType` (for simple types)
|
||||
* `Stmt\Function_::$name`
|
||||
* `Stmt\Function_::$returnType` (for simple types)
|
||||
* `Stmt\Goto_::$name`
|
||||
* `Stmt\Interface_::$name`
|
||||
* `Stmt\Label::$name`
|
||||
* `Stmt\PropertyProperty::$name` (uses `VarLikeIdentifier`)
|
||||
* `Stmt\TraitUseAdaptation\Alias::$method`
|
||||
* `Stmt\TraitUseAdaptation\Alias::$newName`
|
||||
* `Stmt\TraitUseAdaptation\Precedence::$method`
|
||||
* `Stmt\Trait_::$name`
|
||||
* `Stmt\UseUse::$alias`
|
||||
|
||||
* Expression statements (`expr;`) are now represented using a `Stmt\Expression` node. Previously
|
||||
these statements were directly represented as their constituent expression.
|
||||
* The `name` subnode of `Param` has been renamed to `var` and now contains a `Variable` rather than
|
||||
a plain string.
|
||||
* The `name` subnode of `StaticVar` has been renamed to `var` and now contains a `Variable` rather
|
||||
than a plain string.
|
||||
* The `var` subnode of `ClosureUse` now contains a `Variable` rather than a plain string.
|
||||
* The `var` subnode of `Catch_` now contains a `Variable` rather than a plain string.
|
||||
* The `alias` subnode of `UseUse` is now `null` if no explicit alias is given. As such,
|
||||
`use Foo\Bar` and `use Foo\Bar as Bar` are now represented differently. The `getAlias()` method
|
||||
can be used to get the effective alias, even if it is not explicitly given.
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
* The indentation handling in the pretty printer has been changed (this is only relevant if you
|
||||
extend the pretty printer). Previously indentation was automatic, and parts were excluded using
|
||||
`pNoindent()`. Now no-indent is the default and newlines that require indentation should use
|
||||
`$this->nl`.
|
||||
|
||||
### Removed functionality
|
||||
|
||||
* Removed `type` subnode on `Class_`, `ClassMethod` and `Property` nodes. Use `flags` instead.
|
||||
* The `ClassConst::isStatic()` method has been removed. Constants cannot have a static modifier.
|
||||
* The `NodeTraverser` no longer accepts `false` as a return value from a `leaveNode()` method.
|
||||
`NodeTraverser::REMOVE_NODE` should be returned instead.
|
||||
* The `Node::setLine()` method has been removed. If you really need to, you can use `setAttribute()`
|
||||
instead.
|
||||
* The misspelled `Class_::VISIBILITY_MODIFER_MASK` constant has been dropped in favor of
|
||||
`Class_::VISIBILITY_MODIFIER_MASK`.
|
||||
* The XML serializer has been removed. As such, the classes `Serializer\XML`, and
|
||||
`Unserializer\XML`, as well as the interfaces `Serializer` and `Unserializer` no longer exist.
|
||||
* The `BuilderAbstract` class has been removed. It's functionality is moved into `BuilderHelpers`.
|
||||
However, this is an internal class and should not be used directly.
|
||||
* The `Autoloader` class has been removed in favor of relying on the Composer autoloader.
|
343
UPGRADE-5.0.md
Normal file
343
UPGRADE-5.0.md
Normal file
@ -0,0 +1,343 @@
|
||||
Upgrading from PHP-Parser 4.x to 5.0
|
||||
====================================
|
||||
|
||||
### PHP version requirements
|
||||
|
||||
PHP-Parser now requires PHP 7.4 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.
|
||||
|
||||
### Changes to the name representation
|
||||
|
||||
Previously, `Name` nodes had a `parts` subnode, which stores an array of name parts, split by
|
||||
namespace separators. Now, `Name` nodes instead have a `name` subnode, which stores a plain string.
|
||||
|
||||
For example, the name `Foo\Bar` was previously represented by `Name(parts: ['Foo', 'Bar'])` and is
|
||||
now represented by `Name(name: 'Foo\Bar')` instead.
|
||||
|
||||
It is possible to convert the name to the previous representation using `$name->getParts()`. The
|
||||
`Name` constructor continues to accept both the string and the array representation.
|
||||
|
||||
### 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 node constructors
|
||||
|
||||
Node constructor arguments accepting types now longer accept plain strings. Either an `Identifier` or `Name` (or `ComplexType`) should be passed instead. This affects the following constructor arguments:
|
||||
|
||||
* The `'returnType'` key of `$subNodes` argument of `Node\Expr\ArrowFunction`.
|
||||
* The `'returnType'` key of `$subNodes` argument of `Node\Expr\Closure`.
|
||||
* The `'returnType'` key of `$subNodes` argument of `Node\Stmt\ClassMethod`.
|
||||
* The `'returnType'` key of `$subNodes` argument of `Node\Stmt\Function_`.
|
||||
* The `$type` argument of `Node\NullableType`.
|
||||
* The `$type` argument of `Node\Param`.
|
||||
* The `$type` argument of `Node\Stmt\Property`.
|
||||
* The `$type` argument of `Node\ClassConst`.
|
||||
|
||||
To follow the previous behavior, an `Identifier` should be passed, which indicates a built-in type.
|
||||
|
||||
### 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';
|
||||
'\\\\';
|
||||
```
|
||||
|
||||
`else if` structures will now omit redundant parentheses:
|
||||
|
||||
```php
|
||||
# Before
|
||||
else {
|
||||
if ($x) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
# After
|
||||
else if ($x) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
The pretty printer now accepts a `phpVersion` option, which accepts a `PhpVersion` object and defaults to PHP 7.4. 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 (default), 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 (default), 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.
|
||||
* For PHP >= 7.3 (default), heredoc/nowdoc strings are now indented just like regular code. This was allowed 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 the node traverser
|
||||
|
||||
If there are multiple visitors, the node traverser will now call `leaveNode()` and `afterTraverse()` methods in the reverse order of the corresponding `enterNode()` and `beforeTraverse()` calls:
|
||||
|
||||
```php
|
||||
# Before
|
||||
$visitor1->enterNode($node);
|
||||
$visitor2->enterNode($node);
|
||||
$visitor1->leaveNode($node);
|
||||
$visitor2->leaveNode($node);
|
||||
|
||||
# After
|
||||
$visitor1->enterNode($node);
|
||||
$visitor2->enterNode($node);
|
||||
$visitor2->leaveNode($node);
|
||||
$visitor1->leaveNode($node);
|
||||
```
|
||||
|
||||
Additionally, the special `NodeVisitor` return values have been moved from `NodeTraverser` to `NodeVisitor`. The old names are deprecated, but still available.
|
||||
|
||||
```php
|
||||
PhpParser\NodeTraverser::REMOVE_NODE -> PhpParser\NodeVisitor::REMOVE_NODE
|
||||
PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN -> PhpParser\NodeVisitor::DONT_TRAVERSE_CHILDREN
|
||||
PhpParser\NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN -> PhpParser\NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
|
||||
PhpParser\NodeTraverser::STOP_TRAVERSAL -> PhpParser\NodeVisitor::STOP_TRAVERSAL
|
||||
```
|
||||
|
||||
Visitors can now also be passed directly to the `NodeTraverser` constructor:
|
||||
|
||||
```php
|
||||
# Before (and still supported)
|
||||
$traverser = new NodeTraverser();
|
||||
$traverser->addVisitor(new NameResolver());
|
||||
|
||||
# After
|
||||
$traverser = new NodeTraverser(new NameResolver());
|
||||
```
|
||||
|
||||
### 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 token array is now 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.
|
||||
|
||||
### Changes to the lexer
|
||||
|
||||
The lexer API is reduced to a single `Lexer::tokenize()` method, which returns an array of tokens. The `startLexing()` and `getNextToken()` methods have been removed.
|
||||
|
||||
Responsibility for determining start and end attributes for nodes has been moved from the lexer to the parser. The lexer no longer accepts an options array. The `usedAttributes` option has been removed without replacement, and the parser will now unconditionally add the `comments`, `startLine`, `endLine`, `startFilePos`, `startEndPos`, `startTokenPos` and `startEndPos` attributes.
|
||||
|
||||
There should no longer be a need to directly interact with the `Lexer` for end users, as the `ParserFactory` will create an appropriate instance, and no additional configuration of the lexer is necessary. To use formatting-preserving pretty printing, the setup boilerplate changes as follows:
|
||||
|
||||
```php
|
||||
# Before
|
||||
|
||||
$lexer = new Lexer\Emulative([
|
||||
'usedAttributes' => [
|
||||
'comments',
|
||||
'startLine', 'endLine',
|
||||
'startTokenPos', 'endTokenPos',
|
||||
],
|
||||
]);
|
||||
|
||||
$parser = new Parser\Php7($lexer);
|
||||
$oldStmts = $parser->parse($code);
|
||||
$oldTokens = $lexer->getTokens();
|
||||
|
||||
$traverser = new NodeTraverser();
|
||||
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
||||
$newStmts = $traverser->traverse($oldStmts);
|
||||
|
||||
# After
|
||||
|
||||
$parser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||
$oldStmts = $parser->parse($code);
|
||||
$oldTokens = $lexer->getTokens();
|
||||
|
||||
$traverser = new NodeTraverser(new NodeVisitor\CloningVisitor());
|
||||
$newStmts = $traverser->traverse($oldStmts);
|
||||
```
|
||||
|
||||
### Miscellaneous changes
|
||||
|
||||
* 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.
|
||||
* The deprecated `Comment::getLine()`, `Comment::getTokenPos()` and `Comment::getFilePos()` methods have been removed. Use `Comment::getStartLine()`, `Comment::getStartTokenPos()` and `Comment::getStartFilePos()` instead.
|
||||
* `Comment::getReformattedText()` now normalizes CRLF newlines to LF newlines.
|
||||
* The `Node::getLine()` method has been deprecated. Use `Node::getStartLine()` instead.
|
206
bin/php-parse
Executable file
206
bin/php-parse
Executable file
@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
|
||||
if (file_exists($file)) {
|
||||
require $file;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ini_set('xdebug.max_nesting_level', 3000);
|
||||
|
||||
// Disable Xdebug var_dump() output truncation
|
||||
ini_set('xdebug.var_display_max_children', -1);
|
||||
ini_set('xdebug.var_display_max_data', -1);
|
||||
ini_set('xdebug.var_display_max_depth', -1);
|
||||
|
||||
list($operations, $files, $attributes) = parseArgs($argv);
|
||||
|
||||
/* Dump nodes by default */
|
||||
if (empty($operations)) {
|
||||
$operations[] = 'dump';
|
||||
}
|
||||
|
||||
if (empty($files)) {
|
||||
showHelp("Must specify at least one file.");
|
||||
}
|
||||
|
||||
$parser = (new PhpParser\ParserFactory())->createForVersion($attributes['version']);
|
||||
$dumper = new PhpParser\NodeDumper([
|
||||
'dumpComments' => true,
|
||||
'dumpPositions' => $attributes['with-positions'],
|
||||
]);
|
||||
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
||||
|
||||
$traverser = new PhpParser\NodeTraverser();
|
||||
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file === '-') {
|
||||
$code = file_get_contents('php://stdin');
|
||||
fwrite(STDERR, "====> Stdin:\n");
|
||||
} else if (strpos($file, '<?php') === 0) {
|
||||
$code = $file;
|
||||
fwrite(STDERR, "====> Code $code\n");
|
||||
} else {
|
||||
if (!file_exists($file)) {
|
||||
fwrite(STDERR, "File $file does not exist.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$code = file_get_contents($file);
|
||||
fwrite(STDERR, "====> File $file:\n");
|
||||
}
|
||||
|
||||
if ($attributes['with-recovery']) {
|
||||
$errorHandler = new PhpParser\ErrorHandler\Collecting;
|
||||
$stmts = $parser->parse($code, $errorHandler);
|
||||
foreach ($errorHandler->getErrors() as $error) {
|
||||
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
|
||||
fwrite(STDERR, $message . "\n");
|
||||
}
|
||||
if (null === $stmts) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
} catch (PhpParser\Error $error) {
|
||||
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
|
||||
fwrite(STDERR, $message . "\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($operations as $operation) {
|
||||
if ('dump' === $operation) {
|
||||
fwrite(STDERR, "==> Node dump:\n");
|
||||
echo $dumper->dump($stmts, $code), "\n";
|
||||
} elseif ('pretty-print' === $operation) {
|
||||
fwrite(STDERR, "==> Pretty print:\n");
|
||||
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
|
||||
} elseif ('json-dump' === $operation) {
|
||||
fwrite(STDERR, "==> JSON dump:\n");
|
||||
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
||||
} elseif ('var-dump' === $operation) {
|
||||
fwrite(STDERR, "==> var_dump():\n");
|
||||
var_dump($stmts);
|
||||
} elseif ('resolve-names' === $operation) {
|
||||
fwrite(STDERR, "==> Resolved names.\n");
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
|
||||
if ($withColumnInfo && $e->hasColumnInfo()) {
|
||||
return $e->getMessageWithColumnInfo($code);
|
||||
} else {
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
function showHelp($error = '') {
|
||||
if ($error) {
|
||||
fwrite(STDERR, $error . "\n\n");
|
||||
}
|
||||
fwrite($error ? STDERR : STDOUT, <<<'OUTPUT'
|
||||
Usage: php-parse [operations] file1.php [file2.php ...]
|
||||
or: php-parse [operations] "<?php code"
|
||||
Turn PHP source code into an abstract syntax tree.
|
||||
|
||||
Operations is a list of the following options (--dump by default):
|
||||
|
||||
-d, --dump Dump nodes using NodeDumper
|
||||
-p, --pretty-print Pretty print file using PrettyPrinter\Standard
|
||||
-j, --json-dump Print json_encode() result
|
||||
--var-dump var_dump() nodes (for exact structure)
|
||||
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
|
||||
-c, --with-column-info Show column-numbers for errors (if available)
|
||||
-P, --with-positions Show positions in node dumps
|
||||
-r, --with-recovery Use parsing with error recovery
|
||||
--version=VERSION Target specific PHP version (default: newest)
|
||||
-h, --help Display this page
|
||||
|
||||
Example:
|
||||
php-parse -d -p -N -d file.php
|
||||
|
||||
Dumps nodes, pretty prints them, then resolves names and dumps them again.
|
||||
|
||||
|
||||
OUTPUT
|
||||
);
|
||||
exit($error ? 1 : 0);
|
||||
}
|
||||
|
||||
function parseArgs($args) {
|
||||
$operations = [];
|
||||
$files = [];
|
||||
$attributes = [
|
||||
'with-column-info' => false,
|
||||
'with-positions' => false,
|
||||
'with-recovery' => false,
|
||||
'version' => PhpParser\PhpVersion::getNewestSupported(),
|
||||
];
|
||||
|
||||
array_shift($args);
|
||||
$parseOptions = true;
|
||||
foreach ($args as $arg) {
|
||||
if (!$parseOptions) {
|
||||
$files[] = $arg;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($arg) {
|
||||
case '--dump':
|
||||
case '-d':
|
||||
$operations[] = 'dump';
|
||||
break;
|
||||
case '--pretty-print':
|
||||
case '-p':
|
||||
$operations[] = 'pretty-print';
|
||||
break;
|
||||
case '--json-dump':
|
||||
case '-j':
|
||||
$operations[] = 'json-dump';
|
||||
break;
|
||||
case '--var-dump':
|
||||
$operations[] = 'var-dump';
|
||||
break;
|
||||
case '--resolve-names':
|
||||
case '-N';
|
||||
$operations[] = 'resolve-names';
|
||||
break;
|
||||
case '--with-column-info':
|
||||
case '-c';
|
||||
$attributes['with-column-info'] = true;
|
||||
break;
|
||||
case '--with-positions':
|
||||
case '-P':
|
||||
$attributes['with-positions'] = true;
|
||||
break;
|
||||
case '--with-recovery':
|
||||
case '-r':
|
||||
$attributes['with-recovery'] = true;
|
||||
break;
|
||||
case '--help':
|
||||
case '-h';
|
||||
showHelp();
|
||||
break;
|
||||
case '--':
|
||||
$parseOptions = false;
|
||||
break;
|
||||
default:
|
||||
if (preg_match('/^--version=(.*)$/', $arg, $matches)) {
|
||||
$attributes['version'] = PhpParser\PhpVersion::fromString($matches[1]);
|
||||
} elseif ($arg[0] === '-' && \strlen($arg[0]) > 1) {
|
||||
showHelp("Invalid operation $arg.");
|
||||
} else {
|
||||
$files[] = $arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$operations, $files, $attributes];
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
{
|
||||
"name": "nikic/php-parser",
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": ["php", "parser"],
|
||||
"type": "library",
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": [
|
||||
"php",
|
||||
"parser"
|
||||
],
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
@ -10,14 +13,31 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.2"
|
||||
"php": ">=7.4",
|
||||
"ext-tokenizer": "*",
|
||||
"ext-json": "*",
|
||||
"ext-ctype": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "PHPParser": "lib/" }
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
|
||||
"ircmaxell/php-yacc": "^0.0.7"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "0.9-dev"
|
||||
"dev-master": "5.0-dev"
|
||||
}
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhpParser\\": "lib/PhpParser"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"PhpParser\\": "test/PhpParser/"
|
||||
}
|
||||
},
|
||||
"bin": [
|
||||
"bin/php-parse"
|
||||
]
|
||||
}
|
||||
|
@ -1,43 +1,59 @@
|
||||
Introduction
|
||||
============
|
||||
|
||||
This project is a PHP 5.5 (and older) parser **written in PHP itself**.
|
||||
This project is a PHP parser **written in PHP itself**.
|
||||
|
||||
What is this for?
|
||||
-----------------
|
||||
|
||||
A parser is useful for [static analysis][0] and manipulation of code and basically any other
|
||||
A parser is useful for [static analysis][0], manipulation of code and basically any other
|
||||
application dealing with code programmatically. A parser constructs an [Abstract Syntax Tree][1]
|
||||
(AST) of the code and thus allows dealing with it in an abstract and robust way.
|
||||
|
||||
There are other ways of dealing with source code. One that PHP supports natively is using the
|
||||
There are other ways of processing source code. One that PHP supports natively is using the
|
||||
token stream generated by [`token_get_all`][2]. The token stream is much more low level than
|
||||
the AST and thus has different applications: It allows to also analyze the exact formatting of
|
||||
a file. On the other hand the token stream is much harder to deal with for more complex analysis.
|
||||
For example an AST abstracts away the fact that in PHP variables can be written as `$foo`, but also
|
||||
a file. On the other hand, the token stream is much harder to deal with for more complex analysis.
|
||||
For example, an AST abstracts away the fact that, in PHP, variables can be written as `$foo`, but also
|
||||
as `$$bar`, `${'foobar'}` or even `${!${''}=barfoo()}`. You don't have to worry about recognizing
|
||||
all the different syntaxes from a stream of tokens.
|
||||
|
||||
Another questions is: Why would I want to have a PHP parser *written in PHP*? Well, PHP might not be
|
||||
Another question is: Why would I want to have a PHP parser *written in PHP*? Well, PHP might not be
|
||||
a language especially suited for fast parsing, but processing the AST is much easier in PHP than it
|
||||
would be in other, faster languages like C. Furthermore the people most probably wanting to do
|
||||
would be in other, faster languages like C. Furthermore the people most likely wanting to do
|
||||
programmatic PHP code analysis are incidentally PHP developers, not C developers.
|
||||
|
||||
What can it parse?
|
||||
------------------
|
||||
|
||||
The parser uses a PHP 5.5 compliant grammar, which is backwards compatible with at least PHP 5.4, PHP 5.3
|
||||
and PHP 5.2 (and maybe older).
|
||||
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
|
||||
version it runs on), additionally a wrapper for emulating new tokens from 5.3, 5.4 and 5.5 is provided. This
|
||||
allows to parse PHP 5.5 source code running on PHP 5.2, for example. This emulation is very hacky and not
|
||||
yet perfect, but it should work well on any sane code.
|
||||
version it runs on), additionally a wrapper for emulating tokens from newer versions is provided.
|
||||
This allows to parse PHP 8.3 source code running on PHP 7.4, for example. This emulation is not
|
||||
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?
|
||||
----------------------------
|
||||
|
||||
The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks like
|
||||
The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks
|
||||
can best be seen in an example. The program `<?php echo 'Hi', 'World';` will give you a node tree
|
||||
roughly looking like this:
|
||||
|
||||
@ -56,26 +72,26 @@ array(
|
||||
)
|
||||
```
|
||||
|
||||
This matches the semantics the program had: An echo statement, which takes two strings as expressions,
|
||||
with the values `Hi` and `World!`.
|
||||
This matches the structure of the code: An echo statement, which takes two strings as expressions,
|
||||
with the values `Hi` and `World`.
|
||||
|
||||
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?
|
||||
--------------------
|
||||
|
||||
Apart from the parser itself this package also bundles support for some other, related features:
|
||||
Apart from the parser itself, this package also bundles support for some other, related features:
|
||||
|
||||
* Support for pretty printing, which is the act of converting an AST into PHP code. Please note
|
||||
that "pretty printing" does not imply that the output is especially pretty. It's just how it's
|
||||
called ;)
|
||||
* Support for serializing and unserializing the node tree to XML
|
||||
* Support for dumping the node tree in a human readable form (see the section above for an
|
||||
example of how the output looks like)
|
||||
* Infrastructure for traversing and changing the AST (node traverser and node visitors)
|
||||
* A node visitor for resolving namespaced names
|
||||
* 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
|
||||
example of how the output looks like).
|
||||
* Infrastructure for traversing and changing the AST (node traverser and node visitors).
|
||||
* A node visitor for resolving namespaced names.
|
||||
|
||||
[0]: http://en.wikipedia.org/wiki/Static_program_analysis
|
||||
[1]: http://en.wikipedia.org/wiki/Abstract_syntax_tree
|
||||
[2]: http://php.net/token_get_all
|
||||
[2]: http://php.net/token_get_all
|
||||
|
@ -1,48 +0,0 @@
|
||||
Installation
|
||||
============
|
||||
|
||||
There are multiple ways to include the PHP parser into your project:
|
||||
|
||||
Installing from the Zip- or Tarball
|
||||
-----------------------------------
|
||||
|
||||
Download the latest version from [the download page][2], unpack it and move the files somewhere into your project.
|
||||
|
||||
Installing via Composer
|
||||
-----------------------
|
||||
|
||||
Create a `composer.json` file in your project root and use it to define your dependencies:
|
||||
|
||||
{
|
||||
"require": {
|
||||
"nikic/php-parser": "0.9.4"
|
||||
}
|
||||
}
|
||||
|
||||
Then install Composer in your project (or [download the composer.phar][1] directly):
|
||||
|
||||
curl -s http://getcomposer.org/installer | php
|
||||
|
||||
And finally ask Composer to install the dependencies:
|
||||
|
||||
php composer.phar install
|
||||
|
||||
Installing as a PEAR package
|
||||
----------------------------
|
||||
|
||||
Run the following two commands:
|
||||
|
||||
pear channel-discover nikic.github.com/pear
|
||||
pear install nikic/PHPParser-0.9.4
|
||||
|
||||
Installing as a Git Submodule
|
||||
-----------------------------
|
||||
|
||||
Run the following command to install the parser into the `vendor/PHP-Parser` folder:
|
||||
|
||||
git submodule add git://github.com/nikic/PHP-Parser.git vendor/PHP-Parser
|
||||
|
||||
|
||||
|
||||
[1]: http://getcomposer.org/composer.phar
|
||||
[2]: https://github.com/nikic/PHP-Parser/tags
|
@ -6,73 +6,144 @@ This document explains how to use the parser, the pretty printer and the node tr
|
||||
Bootstrapping
|
||||
-------------
|
||||
|
||||
The library needs to register a class autoloader; this is done by including the
|
||||
`bootstrap.php` file:
|
||||
To bootstrap the library, include the autoloader generated by composer:
|
||||
|
||||
```php
|
||||
<?php
|
||||
require 'path/to/PHP-Parser/lib/bootstrap.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', 2000);
|
||||
ini_set('xdebug.max_nesting_level', 3000);
|
||||
```
|
||||
|
||||
This ensures that there will be no errors when traversing highly nested node trees.
|
||||
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
|
||||
slower.
|
||||
|
||||
Parsing
|
||||
-------
|
||||
|
||||
In order to parse some source code you first have to create a `PHPParser_Parser` object (which
|
||||
needs to be passed a `PHPParser_Lexer` instance) and then pass the code (including `<?php` opening
|
||||
tags) to the `parse` method. If a syntax error is encountered `PHPParser_Error` is thrown, so this
|
||||
exception should be `catch`ed.
|
||||
In order to parse code, you first have to create a parser instance:
|
||||
|
||||
```php
|
||||
use PhpParser\ParserFactory;
|
||||
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'));
|
||||
```
|
||||
|
||||
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).
|
||||
|
||||
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).
|
||||
|
||||
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, a `PhpParser\Error` exception will
|
||||
be thrown by default:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$code = '<?php // some code';
|
||||
use PhpParser\Error;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer);
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
printLine('Hello World!!!');
|
||||
CODE;
|
||||
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
} catch (PHPParser_Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
// $stmts is an array of statement nodes
|
||||
} catch (Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage(), "\n";
|
||||
}
|
||||
```
|
||||
|
||||
The `parse` method will return an array of statement nodes (`$stmts`).
|
||||
A parser instance can be reused to parse multiple files.
|
||||
|
||||
### Emulative lexer
|
||||
Node dumping
|
||||
------------
|
||||
|
||||
Instead of `PHPParser_Lexer` one can also use `PHPParser_Lexer_Emulative`. This class will emulate tokens
|
||||
of newer PHP versions and as such allow parsing PHP 5.5 on PHP 5.2, for example. So if you want to parse
|
||||
PHP code of newer versions than the one you are running, you should use the emulative lexer.
|
||||
To dump the abstract syntax tree in human-readable form, a `NodeDumper` can be used:
|
||||
|
||||
Node tree
|
||||
---------
|
||||
```php
|
||||
<?php
|
||||
use PhpParser\NodeDumper;
|
||||
|
||||
If you use the above code with `$code = "<?php echo 'Hi ', hi\\getTarget();"` the parser will
|
||||
generate a node tree looking like this:
|
||||
$nodeDumper = new NodeDumper;
|
||||
echo $nodeDumper->dump($stmts), "\n";
|
||||
```
|
||||
|
||||
For the sample code from the previous section, this will produce the following output:
|
||||
|
||||
```
|
||||
array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Scalar_String(
|
||||
value: Hi
|
||||
0: Stmt_Function(
|
||||
attrGroups: array(
|
||||
)
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: printLine
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
attrGroups: array(
|
||||
)
|
||||
flags: 0
|
||||
type: null
|
||||
byRef: false
|
||||
variadic: false
|
||||
var: Expr_Variable(
|
||||
name: msg
|
||||
)
|
||||
default: null
|
||||
)
|
||||
1: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: hi
|
||||
1: getTarget
|
||||
)
|
||||
returnType: null
|
||||
stmts: array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Expr_Variable(
|
||||
name: msg
|
||||
)
|
||||
1: Scalar_String(
|
||||
value:
|
||||
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
)
|
||||
)
|
||||
)
|
||||
1: Stmt_Expression(
|
||||
expr: Expr_FuncCall(
|
||||
name: Name(
|
||||
name: printLine
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
name: null
|
||||
value: Scalar_String(
|
||||
value: Hello World!!!
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -80,55 +151,84 @@ array(
|
||||
)
|
||||
```
|
||||
|
||||
Thus `$stmts` will contain an array with only one node, with this node being an instance of
|
||||
`PHPParser_Node_Stmt_Echo`.
|
||||
You can also use the `php-parse` script to obtain such a node dump by calling it either with a file
|
||||
name or code string:
|
||||
|
||||
As PHP is a large language there are approximately 140 different nodes. In order to make work
|
||||
```sh
|
||||
vendor/bin/php-parse file.php
|
||||
vendor/bin/php-parse "<?php foo();"
|
||||
```
|
||||
|
||||
This can be very helpful if you want to quickly check how certain syntax is represented in the AST.
|
||||
|
||||
Node tree structure
|
||||
-------------------
|
||||
|
||||
Looking at the node dump above, you can see that `$stmts` for this example code is an array of two
|
||||
nodes, a `Stmt_Function` and a `Stmt_Expression`. The corresponding class names are:
|
||||
|
||||
* `Stmt_Function -> PhpParser\Node\Stmt\Function_`
|
||||
* `Stmt_Expression -> PhpParser\Node\Stmt\Expression`
|
||||
|
||||
The additional `_` at the end of the first class name is necessary, because `Function` is a
|
||||
reserved keyword. Many node class names in this library have a trailing `_` to avoid clashing with
|
||||
a keyword.
|
||||
|
||||
As PHP is a large language there are approximately 140 different nodes. In order to make working
|
||||
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.
|
||||
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
|
||||
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
|
||||
and thus can occur in other expressions. Examples of expressions are `$var`
|
||||
(`PHPParser_Node_Expr_Variable`) and `func()` (`PHPParser_Node_Expr_FuncCall`).
|
||||
* `PHPParser_Node_Scalar`s are nodes representing scalar values, like `'string'`
|
||||
(`PHPParser_Node_Scalar_String`), `0` (`PHPParser_Node_Scalar_LNumber`) or magic constants
|
||||
like `__FILE__` (`PHPParser_Node_Scalar_FileConst`). All `PHPParser_Node_Scalar`s extend
|
||||
`PHPParser_Node_Expr`, as scalars are expressions, too.
|
||||
* There are some nodes not in either of these groups, for example names (`PHPParser_Node_Name`)
|
||||
and call arguments (`PHPParser_Node_Arg`).
|
||||
(`PhpParser\Node\Expr\Variable`) and `func()` (`PhpParser\Node\Expr\FuncCall`).
|
||||
* `PhpParser\Node\Scalar`s are nodes representing scalar values, like `'string'`
|
||||
(`PhpParser\Node\Scalar\String_`), `0` (`PhpParser\Node\Scalar\LNumber`) or magic constants
|
||||
like `__FILE__` (`PhpParser\Node\Scalar\MagicConst\File`). All `PhpParser\Node\Scalar`s extend
|
||||
`PhpParser\Node\Expr`, as scalars are expressions, too.
|
||||
* There are some nodes not in either of these groups, for example names (`PhpParser\Node\Name`)
|
||||
and call arguments (`PhpParser\Node\Arg`).
|
||||
|
||||
The `Node\Stmt\Expression` node is somewhat confusing in that it contains both the terms "statement"
|
||||
and "expression". This node distinguishes `expr`, which is a `Node\Expr`, from `expr;`, which is
|
||||
an "expression statement" represented by `Node\Stmt\Expression` and containing `expr` as a sub-node.
|
||||
|
||||
Every node has a (possibly zero) number of subnodes. You can access subnodes by writing
|
||||
`$node->subNodeName`. The `Stmt_Echo` node has only one subnode `exprs`. So in order to access it
|
||||
in the above example you would write `$stmts[0]->exprs`. If you wanted to access name of the function
|
||||
`$node->subNodeName`. The `Stmt\Echo_` node has only one subnode `exprs`. So in order to access it
|
||||
in the above example you would write `$stmts[0]->exprs`. If you wanted to access the name of the function
|
||||
call, you would write `$stmts[0]->exprs[1]->name`.
|
||||
|
||||
All nodes also define a `getType()` method that returns the node type (the type is the class name
|
||||
without the `PHPParser_Node_` prefix).
|
||||
All nodes also define a `getType()` method that returns the node type. The type is the class name
|
||||
without the `PhpParser\Node\` prefix and `\` replaced with `_`. It also does not contain a trailing
|
||||
`_` for reserved-keyword class names.
|
||||
|
||||
It is possible to associate custom metadata with a node using the `setAttribute()` method. This data
|
||||
can then be retrieved using `hasAttribute()`, `getAttribute()` and `getAttributes()`.
|
||||
|
||||
By default the lexer adds the `startLine`, `endLine` and `comments` attributes. `comments` is an array
|
||||
of `PHPParser_Comment[_Doc]` instances.
|
||||
By default, the parser adds the `startLine`, `endLine`, `startTokenPos`, `endTokenPos`,
|
||||
`startFilePos`, `endFilePos` and `comments` attributes. `comments` is an array of
|
||||
`PhpParser\Comment[\Doc]` instances.
|
||||
|
||||
The start line can also be accessed using `getLine()`/`setLine()` (instead of `getAttribute('startLine')`).
|
||||
The last doc comment from the `comments` attribute can be obtained using `getDocComment()`.
|
||||
The pre-defined attributes can also be accessed using `getStartLine()` instead of
|
||||
`getAttribute('startLine')`, and so on. The last doc comment from the `comments` attribute can be
|
||||
obtained using `getDocComment()`.
|
||||
|
||||
Pretty printer
|
||||
--------------
|
||||
|
||||
The pretty printer component compiles the AST back to PHP code. As the parser does not retain formatting
|
||||
information the formatting is done using a specified scheme. Currently there is only one scheme available,
|
||||
namely `PHPParser_PrettyPrinter_Default`.
|
||||
The pretty printer component compiles the AST back to PHP code according to a specified scheme.
|
||||
Currently, there is only one scheme available, namely `PhpParser\PrettyPrinter\Standard`.
|
||||
|
||||
```php
|
||||
<?php
|
||||
use PhpParser\Error;
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
|
||||
$code = "<?php echo 'Hi ', hi\\getTarget();";
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer);
|
||||
$prettyPrinter = new PHPParser_PrettyPrinter_Default;
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
$prettyPrinter = new PrettyPrinter\Standard();
|
||||
|
||||
try {
|
||||
// parse
|
||||
@ -139,29 +239,37 @@ try {
|
||||
->exprs // sub expressions
|
||||
[0] // the first of them (the string node)
|
||||
->value // it's value, i.e. 'Hi '
|
||||
= 'Hallo '; // change to 'Hallo '
|
||||
= 'Hello '; // change to 'Hello '
|
||||
|
||||
// pretty print
|
||||
$code = '<?php ' . $prettyPrinter->prettyPrint($stmts);
|
||||
$code = $prettyPrinter->prettyPrint($stmts);
|
||||
|
||||
echo $code;
|
||||
} catch (PHPParser_Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
} catch (Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage(), "\n";
|
||||
}
|
||||
```
|
||||
|
||||
The above code will output:
|
||||
|
||||
<?php echo 'Hallo ', hi\getTarget();
|
||||
echo 'Hello ', hi\getTarget();
|
||||
|
||||
As you can see the source code was first parsed using `PHPParser_Parser->parse`, then changed and then
|
||||
again converted to code using `PHPParser_PrettyPrinter_Default->prettyPrint`.
|
||||
As you can see, the source code was first parsed using `PhpParser\Parser->parse()`, then changed and then
|
||||
again converted to code using `PhpParser\PrettyPrinter\Standard->prettyPrint()`.
|
||||
|
||||
The `prettyPrint` method pretty prints a statements array. It is also possible to pretty print only a
|
||||
single expression using `prettyPrintExpr`.
|
||||
The `prettyPrint()` method pretty prints a statements array. It is also possible to pretty print only a
|
||||
single expression using `prettyPrintExpr()`.
|
||||
|
||||
Node traversation
|
||||
-----------------
|
||||
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.
|
||||
|
||||
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)
|
||||
|
||||
Node traversal
|
||||
--------------
|
||||
|
||||
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.
|
||||
@ -169,20 +277,23 @@ Usually you want to change / analyze code in a generic way, where you don't know
|
||||
going to look like.
|
||||
|
||||
For this purpose the parser provides a component for traversing and visiting the node tree. The basic
|
||||
structure of a program using this `PHPParser_NodeTraverser` looks like this:
|
||||
structure of a program using this `PhpParser\NodeTraverser` looks like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$code = "<?php // some code";
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer);
|
||||
$traverser = new PHPParser_NodeTraverser;
|
||||
$prettyPrinter = new PHPParser_PrettyPrinter_Default;
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
$traverser = new NodeTraverser;
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
|
||||
// add your visitor
|
||||
$traverser->addVisitor(new MyNodeVisitor);
|
||||
|
||||
try {
|
||||
$code = file_get_contents($fileName);
|
||||
|
||||
// parse
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
@ -190,22 +301,23 @@ try {
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
|
||||
// pretty print
|
||||
$code = '<?php ' . $prettyPrinter->prettyPrint($stmts);
|
||||
$code = $prettyPrinter->prettyPrintFile($stmts);
|
||||
|
||||
echo $code;
|
||||
} catch (PHPParser_Error $e) {
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
A same node visitor for this code might look like this:
|
||||
The corresponding node visitor might look like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class MyNodeVisitor extends PHPParser_NodeVisitorAbstract
|
||||
{
|
||||
public function leaveNode(PHPParser_Node $node) {
|
||||
if ($node instanceof PHPParser_Node_Scalar_String) {
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
class MyNodeVisitor extends NodeVisitorAbstract {
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Scalar\String_) {
|
||||
$node->value = 'foo';
|
||||
}
|
||||
}
|
||||
@ -214,38 +326,51 @@ class MyNodeVisitor extends PHPParser_NodeVisitorAbstract
|
||||
|
||||
The above node visitor would change all string literals in the program to `'foo'`.
|
||||
|
||||
All visitors must implement the `PHPParser_NodeVisitor` interface, which defined the following four
|
||||
All visitors must implement the `PhpParser\NodeVisitor` interface, which defines the following four
|
||||
methods:
|
||||
|
||||
public function beforeTraverse(array $nodes);
|
||||
public function enterNode(PHPParser_Node $node);
|
||||
public function leaveNode(PHPParser_Node $node);
|
||||
public function afterTraverse(array $nodes);
|
||||
```php
|
||||
public function beforeTraverse(array $nodes);
|
||||
public function enterNode(\PhpParser\Node $node);
|
||||
public function leaveNode(\PhpParser\Node $node);
|
||||
public function afterTraverse(array $nodes);
|
||||
```
|
||||
|
||||
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
|
||||
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 traversal or
|
||||
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
|
||||
it is called once after the traversal.
|
||||
|
||||
The `enterNode` and `leaveNode` methods are called on every node, the former when it is entered,
|
||||
The `enterNode()` and `leaveNode()` methods are called on every node, the former when it is entered,
|
||||
i.e. before its subnodes are traversed, the latter when it is left.
|
||||
|
||||
All four methods can either return the changed node or not return at all (i.e. `null`) in which
|
||||
case the current node is not changed. The `leaveNode` method can furthermore return two special
|
||||
values: If `false` is returned the current node will be removed from the parent array. If an `array`
|
||||
is returned the current node 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 be
|
||||
`array(A, X, Y, Z, C)`.
|
||||
case the current node is not changed.
|
||||
|
||||
The `enterNode()` method can additionally return the value `NodeVisitor::DONT_TRAVERSE_CHILDREN`,
|
||||
which instructs the traverser to skip all children of the current node. To furthermore prevent subsequent
|
||||
visitors from visiting the current node, `NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN` can be used instead.
|
||||
|
||||
Both methods can additionally return the following values:
|
||||
|
||||
* `NodeVisitor::STOP_TRAVERSAL`, in which case no further nodes will be visited.
|
||||
* `NodeVisitor::REMOVE_NODE`, in which case the current node will be removed from the parent array.
|
||||
* `NodeVisitor::REPLACE_WITH_NULL`, in which case the current node will be replaced with `null`.
|
||||
* 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
|
||||
be `array(A, X, Y, Z, C)`.
|
||||
|
||||
Instead of manually implementing the `NodeVisitor` interface you can also extend the `NodeVisitorAbstract`
|
||||
class, which will define empty default implementations for all the above methods.
|
||||
|
||||
> Read more: [Walking the AST](component/Walking_the_AST.markdown)
|
||||
|
||||
The NameResolver node visitor
|
||||
-----------------------------
|
||||
|
||||
One visitor is already bundled with the package: `PHPParser_NodeVisitor_NameResolver`. This visitor
|
||||
One visitor that is already bundled with the package is `PhpParser\NodeVisitor\NameResolver`. This visitor
|
||||
helps you work with namespaced code by trying to resolve most names to fully qualified ones.
|
||||
|
||||
For example, consider the following code:
|
||||
@ -256,18 +381,21 @@ For example, consider the following code:
|
||||
In order to know that `B\C` really is `A\C` you would need to track aliases and namespaces yourself.
|
||||
The `NameResolver` takes care of that and resolves names as far as possible.
|
||||
|
||||
After running it most names will be fully qualified. The only names that will stay unqualified are
|
||||
After running it, most names will be fully qualified. The only names that will stay unqualified are
|
||||
unqualified function and constant names. These are resolved at runtime and thus the visitor can't
|
||||
know which function they are referring to. In most cases this is a non-issue as the global functions
|
||||
are meant.
|
||||
|
||||
Also the `NameResolver` adds a `namespacedName` subnode to class, function and constant declarations
|
||||
that contains the namespaced name instead of only the shortname that is available via `name`.
|
||||
Additionally, the `NameResolver` adds a `namespacedName` subnode to class, function and constant
|
||||
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)
|
||||
|
||||
Example: Converting namespaced code to pseudo namespaces
|
||||
--------------------------------------------------------
|
||||
|
||||
A small example to understand the concept: We want to convert namespaced code to pseudo namespaces
|
||||
A small example to understand the concept: We want to convert namespaced code to pseudo namespaces,
|
||||
so it works on 5.2, i.e. names like `A\\B` should be converted to `A_B`. Note that such conversions
|
||||
are fairly complicated if you take PHP's dynamic features into account, so our conversion will
|
||||
assume that no dynamic features are used.
|
||||
@ -275,26 +403,29 @@ assume that no dynamic features are used.
|
||||
We start off with the following base code:
|
||||
|
||||
```php
|
||||
<?php
|
||||
const IN_DIR = '/some/path';
|
||||
const OUT_DIR = '/some/other/path';
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor\NameResolver;
|
||||
|
||||
// use the emulative lexer here, as we are running PHP 5.2 but want to parse PHP 5.3
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative);
|
||||
$traverser = new PHPParser_NodeTraverser;
|
||||
$prettyPrinter = new PHPParser_PrettyPrinter_Default;
|
||||
$inDir = '/some/path';
|
||||
$outDir = '/some/other/path';
|
||||
|
||||
$traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver); // we will need resolved names
|
||||
$traverser->addVisitor(new NodeVisitor_NamespaceConverter); // our own node visitor
|
||||
$parser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||
$traverser = new NodeTraverser;
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
|
||||
$traverser->addVisitor(new NameResolver); // we will need resolved names
|
||||
$traverser->addVisitor(new NamespaceConverter); // our own node visitor
|
||||
|
||||
// iterate over all .php files in the directory
|
||||
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(IN_DIR));
|
||||
$files = new RegexIterator($files, '/\.php$/');
|
||||
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($inDir));
|
||||
$files = new \RegexIterator($files, '/\.php$/');
|
||||
|
||||
foreach ($files as $file) {
|
||||
try {
|
||||
// read the file that should be converted
|
||||
$code = file_get_contents($file);
|
||||
$code = file_get_contents($file->getPathName());
|
||||
|
||||
// parse
|
||||
$stmts = $parser->parse($code);
|
||||
@ -303,58 +434,61 @@ foreach ($files as $file) {
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
|
||||
// pretty print
|
||||
$code = '<?php ' . $prettyPrinter->prettyPrint($stmts);
|
||||
$code = $prettyPrinter->prettyPrintFile($stmts);
|
||||
|
||||
// write the converted file to the target directory
|
||||
file_put_contents(
|
||||
substr_replace($file->getPathname(), OUT_DIR, 0, strlen(IN_DIR)),
|
||||
substr_replace($file->getPathname(), $outDir, 0, strlen($inDir)),
|
||||
$code
|
||||
);
|
||||
} catch (PHPParser_Error $e) {
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now lets start with the main code, the `NodeVisitor_NamespaceConverter`. One thing it needs to do
|
||||
Now lets start with the main code, the `NamespaceConverter`. One thing it needs to do
|
||||
is convert `A\\B` style names to `A_B` style ones.
|
||||
|
||||
```php
|
||||
<?php
|
||||
class NodeVisitor_NamespaceConverter extends PHPParser_NodeVisitorAbstract
|
||||
use PhpParser\Node;
|
||||
|
||||
class NamespaceConverter extends \PhpParser\NodeVisitorAbstract
|
||||
{
|
||||
public function leaveNode(PHPParser_Node $node) {
|
||||
if ($node instanceof PHPParser_Node_Name) {
|
||||
return new PHPParser_Node_Name($node->toString('_'));
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Name) {
|
||||
return new Node\Name(str_replace('\\', '_', $node->toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The above code profits from the fact that the `NameResolver` already resolved all names as far as
|
||||
possible, so we don't need to do that. All the need to create a string with the name parts separated
|
||||
by underscores instead of backslashes. This is what `$node->toString('_')` does. (If you want to
|
||||
possible, so we don't need to do that. We only need to create a string with the name parts separated
|
||||
by underscores instead of backslashes. This is what `str_replace('\\', '_', $node->toString())` does. (If you want to
|
||||
create a name with backslashes either write `$node->toString()` or `(string) $node`.) Then we create
|
||||
a new name from the string and return it. Returning a new node replaces the old node.
|
||||
|
||||
Another thing we need to do is change the class/function/const declarations. Currently they contain
|
||||
only the shortname (i.e. the last part of the name), but they need to contain the complete class
|
||||
name:
|
||||
only the shortname (i.e. the last part of the name), but they need to contain the complete name including
|
||||
the namespace prefix:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class NodeVisitor_NamespaceConverter extends PHPParser_NodeVisitorAbstract
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class NodeVisitor_NamespaceConverter extends \PhpParser\NodeVisitorAbstract
|
||||
{
|
||||
public function leaveNode(PHPParser_Node $node) {
|
||||
if ($node instanceof PHPParser_Node_Name) {
|
||||
return new PHPParser_Node_Name($node->toString('_'));
|
||||
} elseif ($node instanceof PHPParser_Node_Stmt_Class
|
||||
|| $node instanceof PHPParser_Node_Stmt_Interface
|
||||
|| $node instanceof PHPParser_Node_Stmt_Function) {
|
||||
$node->name = $node->namespacedName->toString('_');
|
||||
} elseif ($node instanceof PHPParser_Node_Stmt_Const) {
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Name) {
|
||||
return new Node\Name(str_replace('\\', '_', $node->toString()));
|
||||
} elseif ($node instanceof Stmt\Class_
|
||||
|| $node instanceof Stmt\Interface_
|
||||
|| $node instanceof Stmt\Function_) {
|
||||
$node->name = str_replace('\\', '_', $node->namespacedName->toString());
|
||||
} elseif ($node instanceof Stmt\Const_) {
|
||||
foreach ($node->consts as $const) {
|
||||
$const->name = $const->namespacedName->toString('_');
|
||||
$const->name = str_replace('\\', '_', $const->namespacedName->toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -366,29 +500,32 @@ There is not much more to it than converting the namespaced name to string with
|
||||
The last thing we need to do is remove the `namespace` and `use` statements:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class NodeVisitor_NamespaceConverter extends PHPParser_NodeVisitorAbstract
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\NodeVisitor;
|
||||
|
||||
class NodeVisitor_NamespaceConverter extends \PhpParser\NodeVisitorAbstract
|
||||
{
|
||||
public function leaveNode(PHPParser_Node $node) {
|
||||
if ($node instanceof PHPParser_Node_Name) {
|
||||
return new PHPParser_Node_Name($node->toString('_'));
|
||||
} elseif ($node instanceof PHPParser_Node_Stmt_Class
|
||||
|| $node instanceof PHPParser_Node_Stmt_Interface
|
||||
|| $node instanceof PHPParser_Node_Stmt_Function) {
|
||||
$node->name = $node->namespacedName->toString('_');
|
||||
} elseif ($node instanceof PHPParser_Node_Stmt_Const) {
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Name) {
|
||||
return new Node\Name(str_replace('\\', '_', $node->toString()));
|
||||
} elseif ($node instanceof Stmt\Class_
|
||||
|| $node instanceof Stmt\Interface_
|
||||
|| $node instanceof Stmt\Function_) {
|
||||
$node->name = str_replace('\\', '_', $node->namespacedName->toString();
|
||||
} elseif ($node instanceof Stmt\Const_) {
|
||||
foreach ($node->consts as $const) {
|
||||
$const->name = $const->namespacedName->toString('_');
|
||||
$const->name = str_replace('\\', '_', $const->namespacedName->toString());
|
||||
}
|
||||
} elseif ($node instanceof PHPParser_Node_Stmt_Namespace) {
|
||||
} elseif ($node instanceof Stmt\Namespace_) {
|
||||
// returning an array merges is into the parent array
|
||||
return $node->stmts;
|
||||
} elseif ($node instanceof PHPParser_Node_Stmt_Use) {
|
||||
// returning false removed the node altogether
|
||||
return false;
|
||||
} elseif ($node instanceof Stmt\Use_) {
|
||||
// remove use nodes altogether
|
||||
return NodeVisitor::REMOVE_NODE;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
That's all.
|
||||
That's all.
|
||||
|
@ -1,201 +0,0 @@
|
||||
Other node tree representations
|
||||
===============================
|
||||
|
||||
It is possible to convert the AST in several textual representations, which serve different uses.
|
||||
|
||||
Simple serialization
|
||||
--------------------
|
||||
|
||||
It is possible to serialize the node tree using `serialize()` and also unserialize it using
|
||||
`unserialize()`. The output is not human readable and not easily processable from anything
|
||||
but PHP, but it is compact and generates fast. The main application thus is in caching.
|
||||
|
||||
Human readable dumping
|
||||
----------------------
|
||||
|
||||
Furthermore it is possible to dump nodes into a human readable form using the `dump` method of
|
||||
`PHPParser_NodeDumper`. This can be used for debugging.
|
||||
|
||||
```php
|
||||
<?php
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
|
||||
printLine('Hallo World!!!');
|
||||
CODE;
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer);
|
||||
$nodeDumper = new PHPParser_NodeDumper;
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
echo '<pre>' . htmlspecialchars($nodeDumper->dump($stmts)) . '</pre>';
|
||||
} catch (PHPParser_Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
The above output will have an output looking roughly like this:
|
||||
|
||||
```
|
||||
array(
|
||||
0: Stmt_Function(
|
||||
byRef: false
|
||||
params: array(
|
||||
0: Param(
|
||||
name: msg
|
||||
default: null
|
||||
type: null
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
stmts: array(
|
||||
0: Stmt_Echo(
|
||||
exprs: array(
|
||||
0: Expr_Variable(
|
||||
name: msg
|
||||
)
|
||||
1: Scalar_String(
|
||||
value:
|
||||
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
name: printLine
|
||||
)
|
||||
1: Expr_FuncCall(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: printLine
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Scalar_String(
|
||||
value: Hallo World!!!
|
||||
)
|
||||
byRef: false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
Serialization to XML
|
||||
--------------------
|
||||
|
||||
It is also possible to serialize the node tree to XML using `PHPParser_Serializer_XML->serialize()`
|
||||
and to unserialize it using `PHPParser_Unserializer_XML->unserialize()`. This is useful for
|
||||
interfacing with other languages and applications or for doing transformation using XSLT.
|
||||
|
||||
```php
|
||||
<?php
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
|
||||
printLine('Hallo World!!!');
|
||||
CODE;
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer);
|
||||
$serializer = new PHPParser_Serializer_XML;
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
echo '<pre>' . htmlspecialchars($serializer->serialize($stmts)) . '</pre>';
|
||||
} catch (PHPParser_Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
Produces:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
|
||||
<scalar:array>
|
||||
<node:Stmt_Function line="2">
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
<subNode:params>
|
||||
<scalar:array>
|
||||
<node:Param line="2">
|
||||
<subNode:name>
|
||||
<scalar:string>msg</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<scalar:null/>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
</scalar:array>
|
||||
</subNode:params>
|
||||
<subNode:stmts>
|
||||
<scalar:array>
|
||||
<node:Stmt_Echo line="3">
|
||||
<subNode:exprs>
|
||||
<scalar:array>
|
||||
<node:Expr_Variable line="3">
|
||||
<subNode:name>
|
||||
<scalar:string>msg</scalar:string>
|
||||
</subNode:name>
|
||||
</node:Expr_Variable>
|
||||
<node:Scalar_String line="3">
|
||||
<subNode:value>
|
||||
<scalar:string>
|
||||
</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
</scalar:array>
|
||||
</subNode:exprs>
|
||||
</node:Stmt_Echo>
|
||||
</scalar:array>
|
||||
</subNode:stmts>
|
||||
<subNode:name>
|
||||
<scalar:string>printLine</scalar:string>
|
||||
</subNode:name>
|
||||
</node:Stmt_Function>
|
||||
<node:Expr_FuncCall line="6">
|
||||
<subNode:name>
|
||||
<node:Name line="6">
|
||||
<subNode:parts>
|
||||
<scalar:array>
|
||||
<scalar:string>printLine</scalar:string>
|
||||
</scalar:array>
|
||||
</subNode:parts>
|
||||
</node:Name>
|
||||
</subNode:name>
|
||||
<subNode:args>
|
||||
<scalar:array>
|
||||
<node:Arg line="6">
|
||||
<subNode:value>
|
||||
<node:Scalar_String line="6">
|
||||
<subNode:value>
|
||||
<scalar:string>Hallo World!!!</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
</subNode:value>
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
</node:Arg>
|
||||
</scalar:array>
|
||||
</subNode:args>
|
||||
</node:Expr_FuncCall>
|
||||
</scalar:array>
|
||||
</AST>
|
||||
```
|
@ -1,265 +0,0 @@
|
||||
Code generation
|
||||
===============
|
||||
|
||||
It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the
|
||||
pretty printer to convert it to PHP code. To simplify code generation, the project comes with a set of builders for
|
||||
common structures as well as simple templating support. Both features are described in the following:
|
||||
|
||||
Builders
|
||||
--------
|
||||
|
||||
The project provides builders for classes, interfaces, methods, functions, parameters and properties, which
|
||||
allow creating node trees with a fluid interface, instead of instantiating all nodes manually.
|
||||
|
||||
Here is an example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$factory = new PHPParser_BuilderFactory;
|
||||
$node = $factory->class('SomeClass')
|
||||
->extend('SomeOtherClass')
|
||||
->implement('A\Few', 'Interfaces')
|
||||
->makeAbstract() // ->makeFinal()
|
||||
|
||||
->addStmt($factory->method('someMethod')
|
||||
->makeAbstract() // ->makeFinal()
|
||||
->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
||||
)
|
||||
|
||||
->addStmt($factory->method('anotherMethod')
|
||||
->makeProtected() // ->makePublic() [default], ->makePrivate()
|
||||
->addParam($factory->param('someParam')->setDefault('test'))
|
||||
// it is possible to add manually created nodes
|
||||
->addStmt(new PHPParser_Node_Expr_Print(new PHPParser_Node_Expr_Variable('someParam')))
|
||||
)
|
||||
|
||||
// properties will be correctly reordered above the methods
|
||||
->addStmt($factory->property('someProperty')->makeProtected())
|
||||
->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
|
||||
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$stmts = array($node);
|
||||
echo $prettyPrinter->prettyPrint($stmts);
|
||||
```
|
||||
|
||||
This will produce the following output with the default pretty printer:
|
||||
|
||||
```php
|
||||
<?php
|
||||
abstract class SomeClass extends SomeOtherClass implements A\Few, Interfaces
|
||||
{
|
||||
protected $someProperty;
|
||||
private $anotherProperty = array(1, 2, 3);
|
||||
abstract function someMethod(SomeClass $someParam);
|
||||
protected function anotherMethod($someParam = 'test')
|
||||
{
|
||||
print $someParam;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Templates
|
||||
---------
|
||||
|
||||
Additionally it is possible to generate code from reusable templates.
|
||||
|
||||
As an example consider the following template, which defines a general getter/setter skeleton in terms of a property
|
||||
`__name__` and its `__type__`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
class GetterSetterTemplate
|
||||
{
|
||||
/**
|
||||
* @var __type__ The __name__
|
||||
*/
|
||||
protected $__name__;
|
||||
|
||||
/**
|
||||
* Gets the __name__.
|
||||
*
|
||||
* @return __type__ The __name__
|
||||
*/
|
||||
public function get__Name__() {
|
||||
return $this->__name__;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the __name__.
|
||||
*
|
||||
* @param __type__ $__name__ The new __name__
|
||||
*/
|
||||
public function set__Name__($__name__) {
|
||||
$this->__name__ = $__name__;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Using this template we can easily create a class with multiple properties and their respective getters and setters:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
// $templateString contains the above template
|
||||
$template = new PHPParser_Template($parser, $templateString);
|
||||
|
||||
// We only have to specify the __name__ placeholder, as the
|
||||
// capitalized __Name__ placeholder is automatically created
|
||||
$properties = [
|
||||
['name' => 'title', 'type' => 'string'],
|
||||
['name' => 'body', 'type' => 'string'],
|
||||
['name' => 'author', 'type' => 'User'],
|
||||
['name' => 'timestamp', 'type' => 'DateTime'],
|
||||
];
|
||||
|
||||
$class = $factory->class('BlogPost')->implement('Post');
|
||||
|
||||
foreach ($properties as $propertyPlaceholders) {
|
||||
$stmts = $template->getStmts($propertyPlaceholders);
|
||||
|
||||
$class->addStmts(
|
||||
// $stmts contains all statements from the template. So [0] fetches the class statement
|
||||
// and ->stmts retrieves the methods.
|
||||
$stmts[0]->stmts
|
||||
);
|
||||
}
|
||||
|
||||
echo $prettyPrinter->prettyPrint(array($class->getNode()));
|
||||
```
|
||||
|
||||
The result would look roughly like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
class BlogPost implements Post
|
||||
{
|
||||
/**
|
||||
* @var string The title
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* @var string The body
|
||||
*/
|
||||
protected $body;
|
||||
|
||||
/**
|
||||
* @var User The author
|
||||
*/
|
||||
protected $author;
|
||||
|
||||
/**
|
||||
* @var DateTime The timestamp
|
||||
*/
|
||||
protected $timestamp;
|
||||
|
||||
/**
|
||||
* Gets the title.
|
||||
*
|
||||
* @return string The title
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the title.
|
||||
*
|
||||
* @param string $title The new title
|
||||
*/
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the body.
|
||||
*
|
||||
* @return string The body
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the body.
|
||||
*
|
||||
* @param string $body The new body
|
||||
*/
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the author.
|
||||
*
|
||||
* @return User The author
|
||||
*/
|
||||
public function getAuthor()
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the author.
|
||||
*
|
||||
* @param User $author The new author
|
||||
*/
|
||||
public function setAuthor($author)
|
||||
{
|
||||
$this->author = $author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the timestamp.
|
||||
*
|
||||
* @return DateTime The timestamp
|
||||
*/
|
||||
public function getTimestamp()
|
||||
{
|
||||
return $this->timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the timestamp.
|
||||
*
|
||||
* @param DateTime $timestamp The new timestamp
|
||||
*/
|
||||
public function setTimestamp($timestamp)
|
||||
{
|
||||
$this->timestamp = $timestamp;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
When using multiple templates it is easier to manage them on the filesystem. They can be loaded using the
|
||||
`TemplateLoader`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
// We'll store our templates in ./templates and give them a .php suffix
|
||||
$loader = new PHPParser_TemplateLoader($parser, './templates', '.php');
|
||||
|
||||
// loads ./templates/GetterSetter.php
|
||||
$getterSetterTemplate = $loader->load('GetterSetter');
|
||||
|
||||
// loads ./templates/Collection.php
|
||||
$collectionTemplate = $loader->load('Collection');
|
||||
|
||||
// The use of a suffix is optional. The following code for example is equivalent:
|
||||
$loader = new PHPParser_TemplateLoader($parser, './templates');
|
||||
|
||||
// loads ./templates/GetterSetter.php
|
||||
$getterSetterTemplate = $loader->load('GetterSetter.php');
|
||||
|
||||
// loads ./templates/Collection.php
|
||||
$collectionTemplate = $loader->load('Collection.php');
|
||||
```
|
46
doc/README.md
Normal file
46
doc/README.md
Normal file
@ -0,0 +1,46 @@
|
||||
Table of Contents
|
||||
=================
|
||||
|
||||
Guide
|
||||
-----
|
||||
|
||||
1. [Introduction](0_Introduction.markdown)
|
||||
2. [Usage of basic components](2_Usage_of_basic_components.markdown)
|
||||
|
||||
Component documentation
|
||||
-----------------------
|
||||
|
||||
* [Walking the AST](component/Walking_the_AST.markdown)
|
||||
* Node visitors
|
||||
* Modifying the AST from a visitor
|
||||
* Short-circuiting traversals
|
||||
* Interleaved visitors
|
||||
* Simple node finding API
|
||||
* Parent and sibling references
|
||||
* [Name resolution](component/Name_resolution.markdown)
|
||||
* Name resolver options
|
||||
* Name resolution context
|
||||
* [Pretty printing](component/Pretty_printing.markdown)
|
||||
* Converting AST back to PHP code
|
||||
* Customizing formatting
|
||||
* Formatting-preserving code transformations
|
||||
* [AST builders](component/AST_builders.markdown)
|
||||
* Fluent builders for AST nodes
|
||||
* [Lexer](component/Lexer.markdown)
|
||||
* Lexer options
|
||||
* Token and file positions for nodes
|
||||
* Custom attributes
|
||||
* [Error handling](component/Error_handling.markdown)
|
||||
* Column information for errors
|
||||
* Error recovery (parsing of syntactically incorrect code)
|
||||
* [Constant expression evaluation](component/Constant_expression_evaluation.markdown)
|
||||
* Evaluating constant/property/etc initializers
|
||||
* Handling errors and unsupported expressions
|
||||
* [JSON representation](component/JSON_representation.markdown)
|
||||
* JSON encoding and decoding of ASTs
|
||||
* [Performance](component/Performance.markdown)
|
||||
* Disabling Xdebug
|
||||
* Reusing objects
|
||||
* Garbage collection impact
|
||||
* [Frequently asked questions](component/FAQ.markdown)
|
||||
* Parent and sibling references
|
141
doc/component/AST_builders.markdown
Normal file
141
doc/component/AST_builders.markdown
Normal file
@ -0,0 +1,141 @@
|
||||
AST builders
|
||||
============
|
||||
|
||||
When PHP-Parser is used to generate (or modify) code by first creating an Abstract Syntax Tree and
|
||||
then using the [pretty printer](Pretty_printing.markdown) to convert it to PHP code, it can often
|
||||
be tedious to manually construct AST nodes. The project provides a number of utilities to simplify
|
||||
the construction of common AST nodes.
|
||||
|
||||
Fluent builders
|
||||
---------------
|
||||
|
||||
The library comes with a number of builders, which allow creating node trees using a fluent
|
||||
interface. Builders are created using the `BuilderFactory` and the final constructed node is
|
||||
accessed through `getNode()`. Fluent builders are available for
|
||||
the following syntactic elements:
|
||||
|
||||
* namespaces and use statements
|
||||
* classes, interfaces, traits and enums
|
||||
* methods, functions and parameters
|
||||
* properties, class constants and enum cases
|
||||
* trait uses and trait use adaptations
|
||||
|
||||
Here is an example:
|
||||
|
||||
```php
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use PhpParser\Node;
|
||||
|
||||
$factory = new BuilderFactory;
|
||||
$node = $factory->namespace('Name\Space')
|
||||
->addStmt($factory->use('Some\Other\Thingy')->as('SomeClass'))
|
||||
->addStmt($factory->useFunction('strlen'))
|
||||
->addStmt($factory->useConst('PHP_VERSION'))
|
||||
->addStmt($factory->class('SomeOtherClass')
|
||||
->extend('SomeClass')
|
||||
->implement('A\Few', '\Interfaces')
|
||||
->makeAbstract() // ->makeFinal()
|
||||
|
||||
->addStmt($factory->useTrait('FirstTrait'))
|
||||
|
||||
->addStmt($factory->useTrait('SecondTrait', 'ThirdTrait')
|
||||
->and('AnotherTrait')
|
||||
->with($factory->traitUseAdaptation('foo')->as('bar'))
|
||||
->with($factory->traitUseAdaptation('AnotherTrait', 'baz')->as('test'))
|
||||
->with($factory->traitUseAdaptation('AnotherTrait', 'func')->insteadof('SecondTrait')))
|
||||
|
||||
->addStmt($factory->method('someMethod')
|
||||
->makePublic()
|
||||
->makeAbstract() // ->makeFinal()
|
||||
->setReturnType('bool') // ->makeReturnByRef()
|
||||
->addParam($factory->param('someParam')->setType('SomeClass'))
|
||||
->setDocComment('/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/')
|
||||
)
|
||||
|
||||
->addStmt($factory->method('anotherMethod')
|
||||
->makeProtected() // ->makePublic() [default], ->makePrivate()
|
||||
->addParam($factory->param('someParam')->setDefault('test'))
|
||||
// it is possible to add manually created nodes
|
||||
->addStmt(new Node\Expr\Print_(new Node\Expr\Variable('someParam')))
|
||||
)
|
||||
|
||||
// properties will be correctly reordered above the methods
|
||||
->addStmt($factory->property('someProperty')->makeProtected())
|
||||
->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
|
||||
)
|
||||
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$stmts = array($node);
|
||||
$prettyPrinter = new PrettyPrinter\Standard();
|
||||
echo $prettyPrinter->prettyPrintFile($stmts);
|
||||
```
|
||||
|
||||
This will produce the following output with the standard pretty printer:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace Name\Space;
|
||||
|
||||
use Some\Other\Thingy as SomeClass;
|
||||
use function strlen;
|
||||
use const PHP_VERSION;
|
||||
abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
|
||||
{
|
||||
use FirstTrait;
|
||||
use SecondTrait, ThirdTrait, AnotherTrait {
|
||||
foo as bar;
|
||||
AnotherTrait::baz as test;
|
||||
AnotherTrait::func insteadof SecondTrait;
|
||||
}
|
||||
protected $someProperty;
|
||||
private $anotherProperty = [1, 2, 3];
|
||||
/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/
|
||||
abstract public function someMethod(SomeClass $someParam): bool;
|
||||
protected function anotherMethod($someParam = 'test')
|
||||
{
|
||||
print $someParam;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Additional helper methods
|
||||
-------------------------
|
||||
|
||||
The `BuilderFactory` also provides a number of additional helper methods, which directly return
|
||||
nodes. The following methods are currently available:
|
||||
|
||||
* `val($value)`: Creates an AST node for a literal value like `42` or `[1, 2, 3]`.
|
||||
* `var($name)`: Creates variable node.
|
||||
* `args(array $args)`: Creates an array of function/method arguments, including the required `Arg`
|
||||
wrappers. Also converts literals to AST nodes.
|
||||
* `funcCall($name, array $args = [])`: Create a function call node. Converts `$name` to a `Name`
|
||||
node and normalizes arguments.
|
||||
* `methodCall(Expr $var, $name, array $args = [])`: Create a method call node. Converts `$name` to
|
||||
an `Identifier` node and normalizes arguments.
|
||||
* `staticCall($class, $name, array $args = [])`: Create a static method call node. Converts
|
||||
`$class` to a `Name` node, `$name` to an `Identifier` node and normalizes arguments.
|
||||
* `new($class, array $args = [])`: Create a "new" (object creation) node. Converts `$class` to a
|
||||
`Name` node.
|
||||
* `constFetch($name)`: Create a constant fetch node. Converts `$name` to a `Name` node.
|
||||
* `classConstFetch($class, $name)`: Create a class constant fetch node. Converts `$class` to a
|
||||
`Name` node and `$name` to an `Identifier` node.
|
||||
* `propertyFetch($var, $name)`: Creates a property fetch node. Converts `$name` to an `Identifier`
|
||||
node.
|
||||
* `concat(...$exprs)`: Create a tree of `BinaryOp\Concat` nodes for the given expressions.
|
||||
* `attribute($name, $args)`: Create a `Attribute` node. Converts `$name` to a `Name` node and
|
||||
normalizes arguments.
|
||||
|
||||
These methods may be expanded on an as-needed basis. Please open an issue or PR if a common
|
||||
operation is missing.
|
117
doc/component/Constant_expression_evaluation.markdown
Normal file
117
doc/component/Constant_expression_evaluation.markdown
Normal file
@ -0,0 +1,117 @@
|
||||
Constant expression evaluation
|
||||
==============================
|
||||
|
||||
Initializers for constants, properties, parameters, etc. have limited support for expressions. For
|
||||
example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class Test {
|
||||
const SECONDS_IN_HOUR = 60 * 60;
|
||||
const SECONDS_IN_DAY = 24 * self::SECONDS_IN_HOUR;
|
||||
}
|
||||
```
|
||||
|
||||
PHP-Parser supports evaluation of such constant expressions through the `ConstExprEvaluator` class:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||
|
||||
$evaluator = new ConstExprEvaluator();
|
||||
try {
|
||||
$value = $evaluator->evaluateSilently($someExpr);
|
||||
} catch (ConstExprEvaluationException $e) {
|
||||
// Either the expression contains unsupported expression types,
|
||||
// or an error occurred during evaluation
|
||||
}
|
||||
```
|
||||
|
||||
Error handling
|
||||
--------------
|
||||
|
||||
The constant evaluator provides two methods, `evaluateDirectly()` and `evaluateSilently()`, which
|
||||
differ in error behavior. `evaluateDirectly()` will evaluate the expression as PHP would, including
|
||||
any generated warnings or Errors. `evaluateSilently()` will instead convert warnings and Errors into
|
||||
a `ConstExprEvaluationException`. For example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||
use PhpParser\Node\{Expr, Scalar};
|
||||
|
||||
$evaluator = new ConstExprEvaluator();
|
||||
|
||||
// 10 / 0
|
||||
$expr = new Expr\BinaryOp\Div(new Scalar\Int_(10), new Scalar\Int_(0));
|
||||
|
||||
var_dump($evaluator->evaluateDirectly($expr)); // float(INF)
|
||||
// Warning: Division by zero
|
||||
|
||||
try {
|
||||
$evaluator->evaluateSilently($expr);
|
||||
} catch (ConstExprEvaluationException $e) {
|
||||
var_dump($e->getPrevious()->getMessage()); // Division by zero
|
||||
}
|
||||
```
|
||||
|
||||
For the purposes of static analysis, you will likely want to use `evaluateSilently()` and leave
|
||||
erroring expressions unevaluated.
|
||||
|
||||
Unsupported expressions and evaluator fallback
|
||||
----------------------------------------------
|
||||
|
||||
The constant expression evaluator supports all expression types that are permitted in constant
|
||||
expressions, apart from the following:
|
||||
|
||||
* `Scalar\MagicConst\*`
|
||||
* `Expr\ConstFetch` (only null/false/true are handled)
|
||||
* `Expr\ClassConstFetch`
|
||||
* `Expr\New_` (since PHP 8.1)
|
||||
* `Expr\PropertyFetch` (since PHP 8.2)
|
||||
|
||||
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
|
||||
an unsupported expression type.
|
||||
|
||||
It is possible to override this behavior and support resolution for these expression types by
|
||||
specifying an evaluation fallback function:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
|
||||
use PhpParser\Node\Expr;
|
||||
|
||||
$evaluator = new ConstExprEvaluator(function(Expr $expr) {
|
||||
if ($expr instanceof Expr\ConstFetch) {
|
||||
return fetchConstantSomehow($expr);
|
||||
}
|
||||
if ($expr instanceof Expr\ClassConstFetch) {
|
||||
return fetchClassConstantSomehow($expr);
|
||||
}
|
||||
// etc.
|
||||
throw new ConstExprEvaluationException(
|
||||
"Expression of type {$expr->getType()} cannot be evaluated");
|
||||
});
|
||||
|
||||
try {
|
||||
$evaluator->evaluateSilently($someExpr);
|
||||
} catch (ConstExprEvaluationException $e) {
|
||||
// Handle exception
|
||||
}
|
||||
```
|
||||
|
||||
Implementers are advised to ensure that evaluation of indirect constant references cannot lead to
|
||||
infinite recursion. For example, the following code could lead to infinite recursion if constant
|
||||
lookup is implemented naively.
|
||||
|
||||
```php
|
||||
<?php
|
||||
class Test {
|
||||
const A = self::B;
|
||||
const B = self::A;
|
||||
}
|
||||
```
|
60
doc/component/Error_handling.markdown
Normal file
60
doc/component/Error_handling.markdown
Normal file
@ -0,0 +1,60 @@
|
||||
Error handling
|
||||
==============
|
||||
|
||||
Errors during parsing or analysis are represented using the `PhpParser\Error` exception class. In addition to an error
|
||||
message, an error can also store additional information about the location the error occurred at.
|
||||
|
||||
How much location information is available depends on the origin of the error. At a minimum the start line of the error
|
||||
is usually available.
|
||||
|
||||
Column information
|
||||
------------------
|
||||
|
||||
Before using column information, its availability needs to be checked with `$e->hasColumnInfo()`, as the precise
|
||||
location of an error cannot always be determined. The methods for retrieving column information also have to be passed
|
||||
the source code of the parsed file. An example for printing an error:
|
||||
|
||||
```php
|
||||
if ($e->hasColumnInfo()) {
|
||||
echo $e->getRawMessage() . ' from ' . $e->getStartLine() . ':' . $e->getStartColumn($code)
|
||||
. ' to ' . $e->getEndLine() . ':' . $e->getEndColumn($code);
|
||||
// or:
|
||||
echo $e->getMessageWithColumnInfo($code);
|
||||
} else {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
Both line numbers and column numbers are 1-based. EOF errors will be located at the position one past the end of the
|
||||
file.
|
||||
|
||||
Error recovery
|
||||
--------------
|
||||
|
||||
The error behavior of the parser (and other components) is controlled by an `ErrorHandler`. Whenever an error is
|
||||
encountered, `ErrorHandler::handleError()` is invoked. The default error handling strategy is `ErrorHandler\Throwing`,
|
||||
which will immediately throw when an error is encountered.
|
||||
|
||||
To instead collect all encountered errors into an array, while trying to continue parsing the rest of the source code,
|
||||
an instance of `ErrorHandler\Collecting` can be passed to the `Parser::parse()` method. A usage example:
|
||||
|
||||
```php
|
||||
$parser = (new PhpParser\ParserFactory())->createForHostVersion();
|
||||
$errorHandler = new PhpParser\ErrorHandler\Collecting;
|
||||
|
||||
$stmts = $parser->parse($code, $errorHandler);
|
||||
|
||||
if ($errorHandler->hasErrors()) {
|
||||
foreach ($errorHandler->getErrors() as $error) {
|
||||
// $error is an ordinary PhpParser\Error
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $stmts) {
|
||||
// $stmts is a best-effort partial AST
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
53
doc/component/FAQ.markdown
Normal file
53
doc/component/FAQ.markdown
Normal file
@ -0,0 +1,53 @@
|
||||
Frequently Asked Questions
|
||||
==========================
|
||||
|
||||
* [How can the parent of a node be obtained?](#how-can-the-parent-of-a-node-be-obtained)
|
||||
* [How can the next/previous sibling of a node be obtained?](#how-can-the-nextprevious-sibling-of-a-node-be-obtained)
|
||||
|
||||
How can the parent of a node be obtained?
|
||||
-----
|
||||
|
||||
The AST does not store parent nodes by default. However, the `ParentConnectingVisitor` can be used to achieve this:
|
||||
|
||||
```php
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor\ParentConnectingVisitor;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$code = '...';
|
||||
|
||||
$traverser = new NodeTraverser(new ParentConnectingVisitor);
|
||||
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
$ast = $parser->parse($code);
|
||||
$ast = $traverser->traverse($ast);
|
||||
```
|
||||
|
||||
After running this visitor, the parent node can be obtained through `$node->getAttribute('parent')`.
|
||||
|
||||
How can the next/previous sibling of a node be obtained?
|
||||
-----
|
||||
|
||||
Again, siblings are not stored by default, but the `NodeConnectingVisitor` can be used to store
|
||||
the previous / next node with a common parent as well:
|
||||
|
||||
```php
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor\NodeConnectingVisitor;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$code = '...';
|
||||
|
||||
$traverser = new NodeTraverser(new NodeConnectingVisitor);
|
||||
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
$ast = $parser->parse($code);
|
||||
$ast = $traverser->traverse($ast);
|
||||
```
|
||||
|
||||
After running this visitor, the parent node can be obtained through `$node->getAttribute('parent')`,
|
||||
the previous node can be obtained through `$node->getAttribute('previous')`, and the next node can be
|
||||
obtained through `$node->getAttribute('next')`.
|
||||
|
||||
`ParentConnectingVisitor` and `NodeConnectingVisitor` should not be used at the same time. The latter
|
||||
includes the functionality of the former.
|
139
doc/component/JSON_representation.markdown
Normal file
139
doc/component/JSON_representation.markdown
Normal file
@ -0,0 +1,139 @@
|
||||
JSON representation
|
||||
===================
|
||||
|
||||
Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON
|
||||
encode the AST directly using `json_encode()`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
|
||||
/** @param string $msg */
|
||||
function printLine($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
CODE;
|
||||
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
This will result in the following output (which includes attributes):
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"nodeType": "Stmt_Function",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"comments": [
|
||||
{
|
||||
"nodeType": "Comment_Doc",
|
||||
"text": "\/** @param string $msg *\/",
|
||||
"line": 3,
|
||||
"filePos": 7,
|
||||
"tokenPos": 2,
|
||||
"endLine": 3,
|
||||
"endFilePos": 31,
|
||||
"endTokenPos": 2
|
||||
}
|
||||
],
|
||||
"endLine": 6
|
||||
},
|
||||
"byRef": false,
|
||||
"name": {
|
||||
"nodeType": "Identifier",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
},
|
||||
"name": "printLine"
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"nodeType": "Param",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
},
|
||||
"type": null,
|
||||
"byRef": false,
|
||||
"variadic": false,
|
||||
"var": {
|
||||
"nodeType": "Expr_Variable",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
},
|
||||
"name": "msg"
|
||||
},
|
||||
"default": null,
|
||||
"flags": 0,
|
||||
"attrGroups": []
|
||||
}
|
||||
],
|
||||
"returnType": null,
|
||||
"stmts": [
|
||||
{
|
||||
"nodeType": "Stmt_Echo",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
},
|
||||
"exprs": [
|
||||
{
|
||||
"nodeType": "Expr_Variable",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
},
|
||||
"name": "msg"
|
||||
},
|
||||
{
|
||||
"nodeType": "Scalar_String",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
"kind": 2,
|
||||
"rawValue": "\"\\n\""
|
||||
},
|
||||
"value": "\n"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"attrGroups": [],
|
||||
"namespacedName": null
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
The JSON representation may be converted back into an AST using the `JsonDecoder`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
$jsonDecoder = new PhpParser\JsonDecoder();
|
||||
$ast = $jsonDecoder->decode($json);
|
||||
```
|
||||
|
||||
Note that not all ASTs can be represented using JSON. In particular:
|
||||
|
||||
* JSON only supports UTF-8 strings.
|
||||
* JSON does not support non-finite floating-point numbers. This can occur if the original source
|
||||
code contains non-representable floating-pointing literals such as `1e1000`.
|
||||
|
||||
If the node tree is not representable in JSON, the initial `json_encode()` call will fail.
|
||||
|
||||
From the command line, a JSON dump can be obtained using `vendor/bin/php-parse -j file.php`.
|
@ -1,114 +1,126 @@
|
||||
Lexer component documentation
|
||||
=============================
|
||||
|
||||
The lexer is responsible for providing tokens to the parser. The project comes with two lexers: `PHPParser_Lexer` and
|
||||
`PHPParser_Lexer_Emulative`. The latter is an extension of the former, which adds the ability to emulate tokens of
|
||||
newer PHP versions and thus allows parsing of new code on older versions.
|
||||
The lexer is responsible for providing tokens to the parser. Typical use of the library does not require direct
|
||||
interaction with the lexer, as an appropriate lexer is created by `PhpParser\ParserFactory`. The tokens produced
|
||||
by the lexer can then be retrieved using `PhpParser\Parser::getTokens()`.
|
||||
|
||||
A lexer has to define the following public interface:
|
||||
Emulation
|
||||
---------
|
||||
|
||||
startLexing($code);
|
||||
getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null);
|
||||
handleHaltCompiler();
|
||||
While this library implements a custom parser, it relies on PHP's `ext/tokenizer` extension to perform lexing. However,
|
||||
this extension only supports lexing code for the PHP version you are running on, while this library also wants to support
|
||||
parsing newer code. For that reason, the lexer performs additional "emulation" in three layers:
|
||||
|
||||
startLexing
|
||||
-----------
|
||||
First, PhpParser uses the `PhpToken` based representation introduced in PHP 8.0, rather than the array-based tokens from
|
||||
previous versions. The `PhpParser\Token` class either extends `PhpToken` (on PHP 8.0) or a polyfill implementation. The
|
||||
polyfill implementation will also perform two emulations that are required by the parser and cannot be disabled:
|
||||
|
||||
The `startLexing` method is invoked when the `parse()` method of the parser is called. It's argument will be whatever
|
||||
was passed to the `parse()` method.
|
||||
* Single-line comments use the PHP 8.0 representation that does not include a trailing newline. The newline will be
|
||||
part of a following `T_WHITESPACE` token.
|
||||
* Namespaced names use the PHP 8.0 representation using `T_NAME_FULLY_QUALIFIED`, `T_NAME_QUALIFIED` and
|
||||
`T_NAME_RELATIVE` tokens, rather than the previous representation using a sequence of `T_STRING` and `T_NS_SEPARATOR`.
|
||||
This means that certain code that is legal on older versions (namespaced names including whitespace, such as `A \ B`)
|
||||
will not be accepted by the parser.
|
||||
|
||||
Even though `startLexing` is meant to accept a source code string, you could for example overwrite it to accept a file:
|
||||
Second, the `PhpParser\Lexer` base class will convert `&` tokens into the PHP 8.1 representation of either
|
||||
`T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG` or `T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG`. This is required by the parser
|
||||
and cannot be disabled.
|
||||
|
||||
Finally, `PhpParser\Lexer\Emulative` performs other, optional emulations. This lexer is parameterized by `PhpVersion`
|
||||
and will try to emulate `ext/tokenizer` output for that version. This is done using separate `TokenEmulator`s for each
|
||||
emulated feature.
|
||||
|
||||
Emulation is usually used to support newer PHP versions, but there is also very limited support for reverse emulation to
|
||||
older PHP versions, which can make keywords from newer versions non-reserved.
|
||||
|
||||
Tokens, positions and attributes
|
||||
--------------------------------
|
||||
|
||||
The `Lexer::tokenize()` method returns an array of `PhpParser\Token`s. The most important parts of the interface can be
|
||||
summarized as follows:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class Token {
|
||||
/** @var int Token ID, either T_* or ord($char) for single-character tokens. */
|
||||
public int $id;
|
||||
/** @var string The textual content of the token. */
|
||||
public string $text;
|
||||
/** @var int The 1-based starting line of the token (or -1 if unknown). */
|
||||
public int $line;
|
||||
/** @var int The 0-based starting position of the token (or -1 if unknown). */
|
||||
public int $pos;
|
||||
|
||||
class FileLexer extends PHPParser_Lexer {
|
||||
public function startLexing($fileName) {
|
||||
if (!file_exists($fileName)) {
|
||||
throw new InvalidArgumentException(sprintf('File "%s" does not exist', $fileName));
|
||||
/** @param int|string|(int|string)[] $kind Token ID or text (or array of them) */
|
||||
public function is($kind): bool;
|
||||
}
|
||||
```
|
||||
|
||||
Unlike PHP's own `PhpToken::tokenize()` output, the token array is terminated by a sentinel token with ID 0.
|
||||
|
||||
The lexer is normally invoked implicitly by the parser. In that case, the tokens for the last parse can be retrieved
|
||||
using `Parser::getTokens()`.
|
||||
|
||||
Nodes in the AST produced by the parser always corresponds to some range of tokens. The parser adds a number of
|
||||
positioning attributes to allow mapping nodes back to lines, tokens or file offsets:
|
||||
|
||||
* `startLine`: Line in which the node starts. Used by `$node->getStartLine()`.
|
||||
* `endLine`: Line in which the node ends. Used by `$node->getEndLine()`.
|
||||
* `startTokenPos`: Offset into the token array of the first token in the node. Used by `$node->getStartTokenPos()`.
|
||||
* `endTokenPos`: Offset into the token array of the last token in the node. Used by `$node->getEndTokenPos()`.
|
||||
* `startFilePos`: Offset into the code string of the first character that is part of the node. Used by `$node->getStartFilePos()`.
|
||||
* `endFilePos`: Offset into the code string of the last character that is part of the node. Used by `$node->getEndFilePos()`.
|
||||
|
||||
Note that `start`/`end` here are closed rather than half-open ranges. This means that a node consisting of a single
|
||||
token will have `startTokenPos == endTokenPos` rather than `startTokenPos + 1 == endTokenPos`. This also means that a
|
||||
zero-length node will have `startTokenPos -1 == endTokenPos`.
|
||||
|
||||
### Using token positions
|
||||
|
||||
> **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
|
||||
> 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.
|
||||
|
||||
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
|
||||
information based on the token position:
|
||||
|
||||
```php
|
||||
/** @param PhpParser\Token[] $tokens */
|
||||
function isDeclaredUsingVar(array $tokens, PhpParser\Node\Stmt\Property $prop) {
|
||||
$i = $prop->getStartTokenPos();
|
||||
return $tokens[$i]->id === T_VAR;
|
||||
}
|
||||
```
|
||||
|
||||
In order to make use of this function, you will have to provide the tokens from the lexer to your node visitor using
|
||||
code similar to the following:
|
||||
|
||||
```php
|
||||
class MyNodeVisitor extends PhpParser\NodeVisitorAbstract {
|
||||
private $tokens;
|
||||
public function setTokens(array $tokens) {
|
||||
$this->tokens = $tokens;
|
||||
}
|
||||
|
||||
public function leaveNode(PhpParser\Node $node) {
|
||||
if ($node instanceof PhpParser\Node\Stmt\Property) {
|
||||
var_dump(isDeclaredUsingVar($this->tokens, $node));
|
||||
}
|
||||
|
||||
parent::startLexing(file_get_contents($fileName));
|
||||
}
|
||||
}
|
||||
|
||||
$parser = new PHPParser_Parser(new FileLexer);
|
||||
$parser = (new PhpParser\ParserFactory())->createForHostVersion($lexerOptions);
|
||||
|
||||
var_dump($parser->parse('someFile.php'));
|
||||
var_dump($parser->parse('someOtherFile.php'));
|
||||
```
|
||||
$visitor = new MyNodeVisitor();
|
||||
$traverser = new PhpParser\NodeTraverser($visitor);
|
||||
|
||||
getNextToken
|
||||
------------
|
||||
|
||||
`getNextToken` returns the ID of the next token and sets some additional information in the three variables which it
|
||||
accepts by-ref. If no more tokens are available it has to return `0`, which is the ID of the `EOF` token.
|
||||
|
||||
The first by-ref variable `$value` should contain the textual content of the token. It is what will be available as `$1`
|
||||
etc in the parser.
|
||||
|
||||
The other two by-ref variables `$startAttributes` and `$endAttributes` define which attributes will eventually be
|
||||
assigned to the generated nodes: The parser will take the `$startAttributes` from the first token which is part of the
|
||||
node and the `$endAttributes` from the last token that is part of the node.
|
||||
|
||||
E.g. if the tokens `T_FUNCTION T_STRING ... '{' ... '}'` constitute a node, then the `$startAttributes` from the
|
||||
`T_FUNCTION` token will be taken and the `$endAttributes` from the `'}'` token.
|
||||
|
||||
By default the lexer creates the attributes `startLine`, `comments` (both part of `$startAttributes`) and `endLine`
|
||||
(part of `$endAttributes`).
|
||||
|
||||
If you don't want all these attributes to be added (to reduce memory usage of the AST) you can simply remove them by
|
||||
overriding the method:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
class LessAttributesLexer extends PHPParser_Lexer {
|
||||
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
||||
$tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
|
||||
|
||||
// only keep startLine attribute
|
||||
unset($startAttributes['comments']);
|
||||
unset($endAttributes['endLine']);
|
||||
|
||||
return $tokenId;
|
||||
}
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
$visitor->setTokens($parser->getTokens());
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
You can obviously also add additional attributes. E.g. in conjunction with the above `FileLexer` you might want to add
|
||||
a `fileName` attribute to all nodes:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
class FileLexer extends PHPParser_Lexer {
|
||||
protected $fileName;
|
||||
|
||||
public function startLexing($fileName) {
|
||||
if (!file_exists($fileName)) {
|
||||
throw new InvalidArgumentException(sprintf('File "%s" does not exist', $fileName));
|
||||
}
|
||||
|
||||
$this->fileName = $fileName;
|
||||
parent::startLexing(file_get_contents($fileName));
|
||||
}
|
||||
|
||||
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
||||
$tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
|
||||
|
||||
// we could use either $startAttributes or $endAttributes here, because the fileName is always the same
|
||||
// (regardless of whether it is the start or end token). We choose $endAttributes, because it is slightly
|
||||
// more efficient (as the parser has to keep a stack for the $startAttributes).
|
||||
$endAttributes['fileName'] = $fileName;
|
||||
|
||||
return $tokenId;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
handleHaltCompiler
|
||||
------------------
|
||||
|
||||
The method is invoked whenever a `T_HALT_COMPILER` token is encountered. It has to return the remaining string after the
|
||||
construct (not including `();`).
|
87
doc/component/Name_resolution.markdown
Normal file
87
doc/component/Name_resolution.markdown
Normal file
@ -0,0 +1,87 @@
|
||||
Name resolution
|
||||
===============
|
||||
|
||||
Since the introduction of namespaces in PHP 5.3, literal names in PHP code are subject to a
|
||||
relatively complex name resolution process, which is based on the current namespace, the current
|
||||
import table state, as well the type of the referenced symbol. PHP-Parser implements name
|
||||
resolution and related functionality, both as reusable logic (NameContext), as well as a node
|
||||
visitor (NameResolver) based on it.
|
||||
|
||||
The NameResolver visitor
|
||||
------------------------
|
||||
|
||||
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.
|
||||
|
||||
```php
|
||||
$nameResolver = new PhpParser\NodeVisitor\NameResolver;
|
||||
$nodeTraverser = new PhpParser\NodeTraverser;
|
||||
$nodeTraverser->addVisitor($nameResolver);
|
||||
|
||||
// Resolve names
|
||||
$stmts = $nodeTraverser->traverse($stmts);
|
||||
```
|
||||
|
||||
In the default configuration, the name resolver will perform three actions:
|
||||
|
||||
* Declarations of functions, classes, interfaces, traits, enums and global constants will have a
|
||||
`namespacedName` property added, which contains the function/class/etc name including the
|
||||
namespace prefix. For historic reasons this is a **property** rather than an attribute.
|
||||
* Names will be replaced by fully qualified resolved names, which are instances of
|
||||
`Node\Name\FullyQualified`.
|
||||
* Unqualified function and constant names inside a namespace cannot be statically resolved. Inside
|
||||
a namespace `Foo`, a call to `strlen()` may either refer to the namespaced `\Foo\strlen()`, or
|
||||
the global `\strlen()`. Because PHP-Parser does not have the necessary context to decide this,
|
||||
such names are left unresolved. Additionally, a `namespacedName` **attribute** is added to the
|
||||
name node.
|
||||
|
||||
The name resolver accepts an option array as the second argument, with the following default values:
|
||||
|
||||
```php
|
||||
$nameResolver = new PhpParser\NodeVisitor\NameResolver(null, [
|
||||
'preserveOriginalNames' => false,
|
||||
'replaceNodes' => true,
|
||||
]);
|
||||
```
|
||||
|
||||
If the `preserveOriginalNames` option is enabled, then the resolved (fully qualified) name will have
|
||||
an `originalName` attribute, which contains the unresolved name.
|
||||
|
||||
If the `replaceNodes` option is disabled, then names will no longer be resolved in-place. Instead, a
|
||||
`resolvedName` attribute will be added to each name, which contains the resolved (fully qualified)
|
||||
name. Once again, if an unqualified function or constant name cannot be resolved, then the
|
||||
`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
|
||||
probably do not wish the resulting code to have fully resolved names as a side-effect.
|
||||
|
||||
The NameContext
|
||||
---------------
|
||||
|
||||
The actual name resolution logic is implemented in the `NameContext` class, which has the following
|
||||
public API:
|
||||
|
||||
```php
|
||||
class NameContext {
|
||||
public function __construct(ErrorHandler $errorHandler);
|
||||
public function startNamespace(Name $namespace = null);
|
||||
public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []);
|
||||
|
||||
public function getNamespace();
|
||||
public function getResolvedName(Name $name, int $type);
|
||||
public function getResolvedClassName(Name $name) : Name;
|
||||
public function getPossibleNames(string $name, int $type) : array;
|
||||
public function getShortName(string $name, int $type) : Name;
|
||||
}
|
||||
```
|
||||
|
||||
The `$type` parameters accept one of the `Stmt\Use_::TYPE_*` constants, which represent the three
|
||||
basic symbol types in PHP (functions, constants and everything else).
|
||||
|
||||
Next to name resolution, the `NameContext` also supports the reverse operation of finding a short
|
||||
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
|
||||
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
|
||||
independent context and explicitly feed `Namespace` and `Use` nodes to it.
|
44
doc/component/Performance.markdown
Normal file
44
doc/component/Performance.markdown
Normal file
@ -0,0 +1,44 @@
|
||||
Performance
|
||||
===========
|
||||
|
||||
Parsing is computationally expensive task, to which the PHP language is not very well suited.
|
||||
Nonetheless, there are a few things you can do to improve the performance of this library, which are
|
||||
described in the following.
|
||||
|
||||
Xdebug
|
||||
------
|
||||
|
||||
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
|
||||
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
|
||||
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.
|
||||
|
||||
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
|
||||
[composer/xdebug-handler](https://github.com/composer/xdebug-handler) package can be used to do
|
||||
this.
|
||||
|
||||
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
|
||||
the AST uses recursion and will generate an error if the value of this option is too low.
|
||||
|
||||
Assertions
|
||||
----------
|
||||
|
||||
Assertions should be disabled in a production context by setting `zend.assertions=-1` (or
|
||||
`zend.assertions=0` if set at runtime). The library currently doesn't make heavy use of assertions,
|
||||
but they are used in an increasing number of places.
|
||||
|
||||
Object reuse
|
||||
------------
|
||||
|
||||
Many objects in this project are designed for reuse. For example, one `Parser` object can be used to
|
||||
parse multiple files.
|
||||
|
||||
When possible, objects should be reused rather than being newly instantiated for every use. Some
|
||||
objects have expensive initialization procedures, which will be unnecessarily repeated if the object
|
||||
is not reused. (Currently two objects with particularly expensive setup are parsers and pretty
|
||||
printers, though the details might change between versions of this library.)
|
101
doc/component/Pretty_printing.markdown
Normal file
101
doc/component/Pretty_printing.markdown
Normal file
@ -0,0 +1,101 @@
|
||||
Pretty printing
|
||||
===============
|
||||
|
||||
Pretty printing is the process of converting a syntax tree back to PHP code. In its basic mode of
|
||||
operation the pretty printer provided by this library will print the AST using a certain predefined
|
||||
code style and will discard (nearly) all formatting of the original code. Because programmers tend
|
||||
to be rather picky about their code formatting, this mode of operation is not very suitable for
|
||||
refactoring code, but can be used for automatically generated code, which is usually only read for
|
||||
debugging purposes.
|
||||
|
||||
Basic usage
|
||||
-----------
|
||||
|
||||
```php
|
||||
$stmts = $parser->parse($code);
|
||||
|
||||
// MODIFY $stmts here
|
||||
|
||||
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
||||
$newCode = $prettyPrinter->prettyPrintFile($stmts);
|
||||
```
|
||||
|
||||
The pretty printer has three basic printing methods: `prettyPrint()`, `prettyPrintFile()` and
|
||||
`prettyPrintExpr()`. The one that is most commonly useful is `prettyPrintFile()`, which takes an
|
||||
array of statements and produces a full PHP file, including opening `<?php`.
|
||||
|
||||
`prettyPrint()` also takes a statement array, but produces code which is valid inside an already
|
||||
open `<?php` context. Lastly, `prettyPrintExpr()` takes an `Expr` node and prints only a single
|
||||
expression.
|
||||
|
||||
Customizing the formatting
|
||||
--------------------------
|
||||
|
||||
The pretty printer respects a number of `kind` attributes used by some nodes (e.g., whether an
|
||||
integer should be printed as decimal, hexadecimal, etc). Additionally, it supports three options:
|
||||
|
||||
* `phpVersion` (defaults to 7.4) allows opting into formatting that is not supported by older PHP
|
||||
versions.
|
||||
* `newline` (defaults to `"\n"`) can be set to `"\r\n"` in order to produce Windows newlines.
|
||||
* `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.
|
||||
|
||||
The behaviors controlled by `phpVersion` (defaults to PHP 7.4) are:
|
||||
|
||||
* For PHP >= 7.0, 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, parentheses around `yield` expressions will only be printed when necessary.
|
||||
* 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.
|
||||
* For PHP >= 7.3, heredoc/nowdoc strings are indented just like regular code. This was allowed with
|
||||
the introduction of flexible heredoc/nowdoc strings.
|
||||
|
||||
The default pretty printer does not provide 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
|
||||
and override the methods responsible for the node types you are interested in.
|
||||
|
||||
If you want to have more fine-grained formatting control, the recommended method is to combine the
|
||||
default pretty printer with an existing library for code reformatting, such as
|
||||
[PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).
|
||||
|
||||
Formatting-preserving pretty printing
|
||||
-------------------------------------
|
||||
|
||||
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
|
||||
this, because it will also reformat parts of the code which have not been modified.
|
||||
|
||||
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
|
||||
code which has been modified or newly inserted.
|
||||
|
||||
Use of the formatting-preservation functionality requires some additional preparatory steps:
|
||||
|
||||
```php
|
||||
use PhpParser\{NodeTraverser, NodeVisitor, ParserFactory, PrettyPrinter};
|
||||
|
||||
$parser = (new ParserFactory())->createForHostVersion();
|
||||
$oldStmts = $parser->parse($code);
|
||||
$oldTokens = $parser->getTokens();
|
||||
|
||||
// Run CloningVisitor before making changes to the AST.
|
||||
$traverser = new NodeTraverser(new NodeVisitor\CloningVisitor());
|
||||
$newStmts = $traverser->traverse($oldStmts);
|
||||
|
||||
// MODIFY $newStmts HERE
|
||||
|
||||
$printer = new PrettyPrinter\Standard();
|
||||
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
||||
```
|
||||
|
||||
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 directly modifying
|
||||
the AST and causing spurious changes to the pretty printed code. For more information, see the
|
||||
[name resolution documentation](Name_resolution.markdown).
|
||||
|
||||
The formatting-preservation works on a best-effort basis and may sometimes reformat more code tha
|
||||
necessary. If you encounter problems while using this functionality, please open an issue.
|
364
doc/component/Walking_the_AST.markdown
Normal file
364
doc/component/Walking_the_AST.markdown
Normal file
@ -0,0 +1,364 @@
|
||||
Walking the AST
|
||||
===============
|
||||
|
||||
The most common way to work with the AST is by using a node traverser and one or more node visitors.
|
||||
As a basic example, the following code changes all literal integers in the AST into strings (e.g.,
|
||||
`42` becomes `'42'`.)
|
||||
|
||||
```php
|
||||
use PhpParser\{Node, NodeTraverser, NodeVisitorAbstract};
|
||||
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor(new class extends NodeVisitorAbstract {
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Scalar\Int_) {
|
||||
return new Node\Scalar\String_((string) $node->value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$stmts = ...;
|
||||
$modifiedStmts = $traverser->traverse($stmts);
|
||||
```
|
||||
|
||||
Visitors can be either passed to the `NodeTraverser` constructor, or added using `addVisitor()`:
|
||||
|
||||
```php
|
||||
$traverser = new NodeTraverser($visitor1, $visitor2, $visitor3);
|
||||
|
||||
// Equivalent to:
|
||||
$traverser = new NodeTraverser();
|
||||
$traverser->addVisitor($visitor1);
|
||||
$traverser->addVisitor($visitor2);
|
||||
$traverser->addVisitor($visitor3);
|
||||
```
|
||||
|
||||
Node visitors
|
||||
-------------
|
||||
|
||||
Each node visitor implements an interface with following four methods:
|
||||
|
||||
```php
|
||||
interface NodeVisitor {
|
||||
public function beforeTraverse(array $nodes);
|
||||
public function enterNode(Node $node);
|
||||
public function leaveNode(Node $node);
|
||||
public function afterTraverse(array $nodes);
|
||||
}
|
||||
```
|
||||
|
||||
The `beforeTraverse()` and `afterTraverse()` methods are called before and after the traversal
|
||||
respectively, and are passed the entire AST. They can be used to perform any necessary state
|
||||
setup or cleanup.
|
||||
|
||||
The `enterNode()` method is called when a node is first encountered, before its children are
|
||||
processed ("preorder"). The `leaveNode()` method is called after all children have been visited
|
||||
("postorder").
|
||||
|
||||
For example, if we have the following excerpt of an AST
|
||||
|
||||
```
|
||||
Expr_FuncCall(
|
||||
name: Name(
|
||||
name: printLine
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
name: null
|
||||
value: Scalar_String(
|
||||
value: Hello World!!!
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
then the enter/leave methods will be called in the following order:
|
||||
|
||||
```
|
||||
enterNode(Expr_FuncCall)
|
||||
enterNode(Name)
|
||||
leaveNode(Name)
|
||||
enterNode(Arg)
|
||||
enterNode(Scalar_String)
|
||||
leaveNode(Scalar_String)
|
||||
leaveNode(Arg)
|
||||
leaveNode(Expr_FuncCall)
|
||||
```
|
||||
|
||||
A common pattern is that `enterNode` is used to collect some information and then `leaveNode`
|
||||
performs modifications based on that. At the time when `leaveNode` is called, all the code inside
|
||||
the node will have already been visited and necessary information collected.
|
||||
|
||||
As you usually do not want to implement all four methods, it is recommended that you extend
|
||||
`NodeVisitorAbstract` instead of implementing the interface directly. The abstract class provides
|
||||
empty default implementations.
|
||||
|
||||
Modifying the AST
|
||||
-----------------
|
||||
|
||||
There are a number of ways in which the AST can be modified from inside a node visitor. The first
|
||||
and simplest is to simply change AST properties inside the visitor:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Scalar\LNumber) {
|
||||
// increment all integer literals
|
||||
$node->value++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The second is to replace a node entirely by returning a new node:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Expr\BinaryOp\BooleanAnd) {
|
||||
// Convert all $a && $b expressions into !($a && $b)
|
||||
return new Node\Expr\BooleanNot($node);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Doing this is supported both inside enterNode and leaveNode. However, you have to be mindful about
|
||||
where you perform the replacement: If a node is replaced in enterNode, then the recursive traversal
|
||||
will also consider the children of the new node. If you aren't careful, this can lead to infinite
|
||||
recursion. For example, let's take the previous code sample and use enterNode instead:
|
||||
|
||||
```php
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Expr\BinaryOp\BooleanAnd) {
|
||||
// Convert all $a && $b expressions into !($a && $b)
|
||||
return new Node\Expr\BooleanNot($node);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now `$a && $b` will be replaced by `!($a && $b)`. Then the traverser will go into the first (and
|
||||
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.
|
||||
|
||||
Finally, there are three special replacement types. The first is removal of a node:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Return_) {
|
||||
// Remove all return statements
|
||||
return NodeVisitor::REMOVE_NODE;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Node removal only works if the parent structure is an array. This means that usually it only makes
|
||||
sense to remove nodes of type `Node\Stmt`, as they always occur inside statement lists (and a few
|
||||
more node types like `Arg` or `Expr\ArrayItem`, which are also always part of lists).
|
||||
|
||||
On the other hand, removing a `Node\Expr` does not make sense: If you have `$a * $b`, there is no
|
||||
meaningful way in which the `$a` part could be removed. If you want to remove an expression, you
|
||||
generally want to remove it together with a surrounding expression statement:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Expression
|
||||
&& $node->expr instanceof Node\Expr\FuncCall
|
||||
&& $node->expr->name instanceof Node\Name
|
||||
&& $node->expr->name->toString() === 'var_dump'
|
||||
) {
|
||||
return NodeVisitor::REMOVE_NODE;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This example will remove all calls to `var_dump()` which occur as expression statements. This means
|
||||
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).
|
||||
|
||||
Another way to remove nodes is to replace them with `null`. For example, all `else` statements could
|
||||
be removed as follows:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Else_) {
|
||||
return NodeVisitor::REPLACE_WITH_NULL;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This is only safe to do if the subnode the node is stored in is nullable. `Node\Stmt\Else_` only
|
||||
occurs inside `Node\Stmt\If_::$else`, which is nullable, so this particular replacement is safe.
|
||||
|
||||
Next to removing nodes, it is also possible to replace one node with multiple nodes. This
|
||||
only works if the parent structure is an array.
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Return_ && $node->expr !== null) {
|
||||
// Convert "return foo();" into "$retval = foo(); return $retval;"
|
||||
$var = new Node\Expr\Variable('retval');
|
||||
return [
|
||||
new Node\Stmt\Expression(new Node\Expr\Assign($var, $node->expr)),
|
||||
new Node\Stmt\Return_($var),
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Short-circuiting traversal
|
||||
--------------------------
|
||||
|
||||
An AST can easily contain thousands of nodes, and traversing over all of them may be slow,
|
||||
especially if you have more than one visitor. In some cases, it is possible to avoid a full
|
||||
traversal.
|
||||
|
||||
If you are looking for all class declarations in a file (and assuming you're not interested in
|
||||
anonymous classes), you know that once you've seen a class declaration, there is no point in also
|
||||
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:
|
||||
|
||||
```php
|
||||
private $classes = [];
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Class_) {
|
||||
$this->classes[] = $node;
|
||||
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Of course, this option is only available in enterNode, because it's already too late by the time
|
||||
leaveNode is reached.
|
||||
|
||||
If you are only looking for one specific node, it is also possible to abort the traversal entirely
|
||||
after finding it. For example, if you are looking for the node of a class with a certain name (and
|
||||
discounting exotic cases like conditionally defining a class two times), you can stop traversal
|
||||
once you found it:
|
||||
|
||||
```php
|
||||
private $class = null;
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Class_ &&
|
||||
$node->namespacedName->toString() === 'Foo\Bar\Baz'
|
||||
) {
|
||||
$this->class = $node;
|
||||
return NodeVisitor::STOP_TRAVERSAL;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This works both in enterNode and leaveNode. Note that this particular case can also be more easily
|
||||
handled using a NodeFinder, which will be introduced below.
|
||||
|
||||
Multiple visitors
|
||||
-----------------
|
||||
|
||||
A single traverser can be used with multiple visitors:
|
||||
|
||||
```php
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitorA);
|
||||
$traverser->addVisitor($visitorB);
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
```
|
||||
|
||||
It is important to understand that if a traverser is run with multiple visitors, the visitors will
|
||||
be interleaved. Given the following AST excerpt
|
||||
|
||||
```
|
||||
Stmt_Return(
|
||||
expr: Expr_Variable(
|
||||
name: foobar
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
the following method calls will be performed:
|
||||
|
||||
```php
|
||||
$visitorA->enterNode(Stmt_Return)
|
||||
$visitorB->enterNode(Stmt_Return)
|
||||
$visitorA->enterNode(Expr_Variable)
|
||||
$visitorB->enterNode(Expr_Variable)
|
||||
$visitorB->leaveNode(Expr_Variable)
|
||||
$visitorA->leaveNode(Expr_Variable)
|
||||
$visitorB->leaveNode(Stmt_Return)
|
||||
$visitorA->leaveNode(Stmt_Return)
|
||||
```
|
||||
|
||||
That is, when visiting a node, `enterNode()` and `leaveNode()` will always be called for all
|
||||
visitors, with the `leaveNode()` calls happening in the reverse order of the `enterNode()` calls.
|
||||
Running multiple visitors in parallel improves performance, as the AST only has to be traversed
|
||||
once. However, it is not always possible to write visitors in a way that allows interleaved
|
||||
execution. In this case, you can always fall back to performing multiple traversals:
|
||||
|
||||
```php
|
||||
$traverserA = new NodeTraverser;
|
||||
$traverserA->addVisitor($visitorA);
|
||||
$traverserB = new NodeTraverser;
|
||||
$traverserB->addVisitor($visitorB);
|
||||
$stmts = $traverserA->traverser($stmts);
|
||||
$stmts = $traverserB->traverser($stmts);
|
||||
```
|
||||
|
||||
When using multiple visitors, it is important to understand how they interact with the various
|
||||
special enterNode/leaveNode return values:
|
||||
|
||||
* If *any* visitor returns `DONT_TRAVERSE_CHILDREN`, the children will be skipped for *all*
|
||||
visitors.
|
||||
* If *any* visitor returns `DONT_TRAVERSE_CURRENT_AND_CHILDREN`, the children will be skipped for *all*
|
||||
visitors, and all *subsequent* visitors will not visit the current node.
|
||||
* If *any* visitor returns `STOP_TRAVERSAL`, traversal is stopped for *all* visitors.
|
||||
* If a visitor returns a replacement node, subsequent visitors will be passed the replacement node,
|
||||
not the original one.
|
||||
* If a visitor returns `REMOVE_NODE`, subsequent visitors will not see this node.
|
||||
* If a visitor returns `REPLACE_WITH_NULL`, subsequent visitors will not see this node.
|
||||
* If a visitor returns an array of replacement nodes, subsequent visitors will see neither the node
|
||||
that was replaced, nor the replacement nodes.
|
||||
|
||||
Simple node finding
|
||||
-------------------
|
||||
|
||||
While the node visitor mechanism is very flexible, creating a node visitor can be overly cumbersome
|
||||
for minor tasks. For this reason a `NodeFinder` is provided, which can find AST nodes that either
|
||||
satisfy a certain callback, or which are instances of a certain node type. A couple of examples are
|
||||
shown in the following:
|
||||
|
||||
```php
|
||||
use PhpParser\{Node, NodeFinder};
|
||||
|
||||
$nodeFinder = new NodeFinder;
|
||||
|
||||
// Find all class nodes.
|
||||
$classes = $nodeFinder->findInstanceOf($stmts, Node\Stmt\Class_::class);
|
||||
|
||||
// Find all classes that extend another class
|
||||
$extendingClasses = $nodeFinder->find($stmts, function(Node $node) {
|
||||
return $node instanceof Node\Stmt\Class_
|
||||
&& $node->extends !== null;
|
||||
});
|
||||
|
||||
// Find first class occurring in the AST. Returns null if no class exists.
|
||||
$class = $nodeFinder->findFirstInstanceOf($stmts, Node\Stmt\Class_::class);
|
||||
|
||||
// Find first class that has name $name
|
||||
$class = $nodeFinder->findFirst($stmts, function(Node $node) use ($name) {
|
||||
return $node instanceof Node\Stmt\Class_
|
||||
&& $node->resolvedName->toString() === $name;
|
||||
});
|
||||
```
|
||||
|
||||
Internally, the `NodeFinder` also uses a node traverser. It only simplifies the interface for a
|
||||
common use case.
|
||||
|
||||
Parent and sibling references
|
||||
-----------------------------
|
||||
|
||||
The node visitor mechanism is somewhat rigid, in that it prescribes an order in which nodes should
|
||||
be accessed: From parents to children. However, it can often be convenient to operate in the
|
||||
reverse direction: When working on a node, you might want to check if the parent node satisfies a
|
||||
certain property.
|
||||
|
||||
PHP-Parser does not add parent (or sibling) references to nodes by default, but you can enable them
|
||||
using the `ParentConnectingVisitor` or `NodeConnectingVisitor`. See the [FAQ](FAQ.markdown) for
|
||||
more information.
|
@ -1,29 +1,27 @@
|
||||
What do all those files mean?
|
||||
=============================
|
||||
|
||||
* `zend_language_parser.phpy`: PHP grammer written in a pseudo language
|
||||
* `analyze.php`: Analyzes the `.phpy`-grammer and outputs some info about it
|
||||
* `rebuildParser.php`: Preprocesses the `.phpy`-grammar and builds the parser using `kmyacc`
|
||||
* `kmyacc.php.parser`: A `kmyacc` parser prototype file for PHP
|
||||
* `php.y`: PHP 5-8 grammar written in a pseudo language
|
||||
* `parser.template`: A `kmyacc` parser prototype file for PHP
|
||||
* `rebuildParsers.php`: Preprocesses the grammar and builds the parser using `kmyacc`
|
||||
|
||||
.phpy pseudo language
|
||||
=====================
|
||||
|
||||
The `.phpy` file is a normal grammer in `kmyacc` (`yacc`) style, with some transformations
|
||||
The `.y` file is a normal grammar in `kmyacc` (`yacc`) style, with some transformations
|
||||
applied to it:
|
||||
|
||||
* Nodes are created using the syntax `Name[..., ...]`. This is transformed into
|
||||
`new PHPParser_Node_Name(..., ..., $attributes)`
|
||||
* `Name::abc` is transformed to `PHPParser_Node_Name::abc`
|
||||
* Some function-like constructs are resolved (see `rebuildParser.php` for a list)
|
||||
* Associative arrays are written as `[key: value, ...]`, which is transformed to
|
||||
`array('key' => value, ...)`
|
||||
`new Name(..., ..., attributes())`
|
||||
* Some function-like constructs are resolved (see `rebuildParsers.php` for a list)
|
||||
|
||||
Building the parser
|
||||
===================
|
||||
|
||||
In order to rebuild the parser, you need [moriyoshi's fork of kmyacc](https://github.com/moriyoshi/kmyacc-forked).
|
||||
After you compiled/installed it, run the `rebuildParser.php` script.
|
||||
Run `php grammar/rebuildParsers.php` to rebuild the parsers. Additional options:
|
||||
|
||||
By default only the `Parser.php` is built. If you want to additionally build `Parser/Debug.php` and `y.output` run the
|
||||
script with `--debug`. If you want to retain the preprocessed grammar pass `--keep-tmp-grammar`.
|
||||
* The `KMYACC` environment variable can be used to specify an alternative `kmyacc` binary.
|
||||
By default the `phpyacc` dev dependency will be used. To use the original `kmyacc`, you
|
||||
need to compile [moriyoshi's fork](https://github.com/moriyoshi/kmyacc-forked).
|
||||
* The `--debug` option enables emission of debug symbols and creates the `y.output` file.
|
||||
* The `--keep-tmp-grammar` option preserves the preprocessed grammar file.
|
||||
|
@ -1,96 +0,0 @@
|
||||
<?php
|
||||
|
||||
const GRAMMAR_FILE = './zend_language_parser.phpy';
|
||||
|
||||
const LIB = '(?(DEFINE)
|
||||
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
|
||||
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
|
||||
(?<string>(?&singleQuotedString)|(?&doubleQuotedString))
|
||||
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
|
||||
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
|
||||
)';
|
||||
|
||||
const RULE_BLOCK = '(?<name>[a-z_]++):(?<rules>[^\'"/{};]*+(?:(?:(?&string)|(?&comment)|(?&code)|/|})[^\'"/{};]*+)*+);';
|
||||
|
||||
$usedTerminals = array_flip(array(
|
||||
'T_VARIABLE', 'T_STRING', 'T_INLINE_HTML', 'T_ENCAPSED_AND_WHITESPACE',
|
||||
'T_LNUMBER', 'T_DNUMBER', 'T_CONSTANT_ENCAPSED_STRING', 'T_STRING_VARNAME', 'T_NUM_STRING'
|
||||
));
|
||||
$unusedNonterminals = array_flip(array(
|
||||
'case_separator', 'optional_comma'
|
||||
));
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return array_filter($pieces);
|
||||
}
|
||||
|
||||
echo '<pre>';
|
||||
|
||||
////////////////////
|
||||
////////////////////
|
||||
////////////////////
|
||||
|
||||
list($defs, $ruleBlocks) = magicSplit('%%', file_get_contents(GRAMMAR_FILE));
|
||||
|
||||
if ('' !== trim(preg_replace(regex(RULE_BLOCK), '', $ruleBlocks))) {
|
||||
die('Not all rule blocks were properly recognized!');
|
||||
}
|
||||
|
||||
preg_match_all(regex(RULE_BLOCK), $ruleBlocks, $ruleBlocksMatches, PREG_SET_ORDER);
|
||||
foreach ($ruleBlocksMatches as $match) {
|
||||
$ruleBlockName = $match['name'];
|
||||
$rules = magicSplit('\|', $match['rules']);
|
||||
|
||||
foreach ($rules as &$rule) {
|
||||
$parts = magicSplit('\s+', $rule);
|
||||
$usedParts = array();
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if ('{' === $part[0]) {
|
||||
preg_match_all('~\$([0-9]+)~', $part, $backReferencesMatches, PREG_SET_ORDER);
|
||||
foreach ($backReferencesMatches as $match) {
|
||||
$usedParts[$match[1]] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$i = 1;
|
||||
foreach ($parts as &$part) {
|
||||
if ('/' === $part[0]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($usedParts[$i])) {
|
||||
if ('\'' === $part[0] || '{' === $part[0]
|
||||
|| (ctype_upper($part[0]) && !isset($usedTerminals[$part]))
|
||||
|| (ctype_lower($part[0]) && isset($unusedNonterminals[$part]))
|
||||
) {
|
||||
$part = '<span style="background-color: red; color: white;">' . $part . '</span>';
|
||||
} else {
|
||||
$part = '<strong><em>' . $part . '</em></strong>';
|
||||
}
|
||||
} elseif ((ctype_upper($part[0]) && isset($usedTerminals[$part]))
|
||||
|| (ctype_lower($part[0]) && !isset($unusedNonterminals[$part]))
|
||||
|
||||
) {
|
||||
$part = '<span style="background-color: blue; color: white;">' . $part . '</span>';
|
||||
}
|
||||
|
||||
++$i;
|
||||
}
|
||||
|
||||
$rule = implode(' ', $parts);
|
||||
}
|
||||
|
||||
echo $ruleBlockName, ':', "\n", ' ', implode("\n" . ' | ', $rules), "\n", ';', "\n\n";
|
||||
}
|
@ -1,361 +0,0 @@
|
||||
<?php
|
||||
$meta #
|
||||
#semval($) $this->yyval
|
||||
#semval($,%t) $this->yyval
|
||||
#semval(%n) $this->yyastk[$this->stackPos-(%l-%n)]
|
||||
#semval(%n,%t) $this->yyastk[$this->stackPos-(%l-%n)]
|
||||
#include;
|
||||
|
||||
/* This is an automatically GENERATED file, which should not be manually edited.
|
||||
* Instead edit one of the following:
|
||||
* * the grammar file grammar/zend_language_parser.phpy
|
||||
* * the parser skeleton grammar/kymacc.php.parser
|
||||
* * the preprocessing script grammar/rebuildParser.php
|
||||
*
|
||||
* The skeleton for this parser was written by Moriyoshi Koizumi and is based on
|
||||
* the work by Masato Bito and is in the PUBLIC DOMAIN.
|
||||
*/
|
||||
#if -t
|
||||
class #(-p)_Debug extends #(-p)
|
||||
#endif
|
||||
#ifnot -t
|
||||
class #(-p)
|
||||
#endif
|
||||
{
|
||||
#ifnot -t
|
||||
const TOKEN_NONE = -1;
|
||||
const TOKEN_INVALID = #(YYBADCH);
|
||||
|
||||
const TOKEN_MAP_SIZE = #(YYMAXLEX);
|
||||
|
||||
const YYLAST = #(YYLAST);
|
||||
const YY2TBLSTATE = #(YY2TBLSTATE);
|
||||
const YYGLAST = #(YYGLAST);
|
||||
const YYNLSTATES = #(YYNLSTATES);
|
||||
const YYUNEXPECTED = #(YYUNEXPECTED);
|
||||
const YYDEFAULT = #(YYDEFAULT);
|
||||
|
||||
// {{{ Tokens
|
||||
#tokenval
|
||||
const %s = %n;
|
||||
#endtokenval
|
||||
// }}}
|
||||
|
||||
/* @var array Map of token ids to their respective names */
|
||||
protected static $terminals = array(
|
||||
#listvar terminals
|
||||
, "???"
|
||||
);
|
||||
|
||||
/* @var array Map which translates lexer tokens to internal tokens */
|
||||
protected static $translate = array(
|
||||
#listvar yytranslate
|
||||
);
|
||||
|
||||
protected static $yyaction = array(
|
||||
#listvar yyaction
|
||||
);
|
||||
|
||||
protected static $yycheck = array(
|
||||
#listvar yycheck
|
||||
);
|
||||
|
||||
protected static $yybase = array(
|
||||
#listvar yybase
|
||||
);
|
||||
|
||||
protected static $yydefault = array(
|
||||
#listvar yydefault
|
||||
);
|
||||
|
||||
protected static $yygoto = array(
|
||||
#listvar yygoto
|
||||
);
|
||||
|
||||
protected static $yygcheck = array(
|
||||
#listvar yygcheck
|
||||
);
|
||||
|
||||
protected static $yygbase = array(
|
||||
#listvar yygbase
|
||||
);
|
||||
|
||||
protected static $yygdefault = array(
|
||||
#listvar yygdefault
|
||||
);
|
||||
|
||||
protected static $yylhs = array(
|
||||
#listvar yylhs
|
||||
);
|
||||
|
||||
protected static $yylen = array(
|
||||
#listvar yylen
|
||||
);
|
||||
|
||||
protected $yyval;
|
||||
protected $yyastk;
|
||||
protected $stackPos;
|
||||
protected $lexer;
|
||||
|
||||
/**
|
||||
* Creates a parser instance.
|
||||
*
|
||||
* @param PHPParser_Lexer $lexer A lexer
|
||||
*/
|
||||
public function __construct(PHPParser_Lexer $lexer) {
|
||||
$this->lexer = $lexer;
|
||||
}
|
||||
#endif
|
||||
#if -t
|
||||
protected static $yyproduction = array(
|
||||
#production-strings;
|
||||
);
|
||||
|
||||
protected function yyprintln($msg) {
|
||||
echo $msg, "\n";
|
||||
}
|
||||
|
||||
protected function YYTRACE_NEWSTATE($state, $tokenId) {
|
||||
$this->yyprintln(
|
||||
'% State ' . $state
|
||||
. ', Lookahead ' . ($tokenId == self::TOKEN_NONE ? '--none--' : self::$terminals[$tokenId])
|
||||
);
|
||||
}
|
||||
|
||||
protected function YYTRACE_READ($tokenId) {
|
||||
$this->yyprintln('% Reading ' . self::$terminals[$tokenId]);
|
||||
}
|
||||
|
||||
protected function YYTRACE_SHIFT($tokenId) {
|
||||
$this->yyprintln('% Shift ' . self::$terminals[$tokenId]);
|
||||
}
|
||||
|
||||
protected function YYTRACE_ACCEPT() {
|
||||
$this->yyprintln('% Accepted.');
|
||||
}
|
||||
|
||||
protected function YYTRACE_REDUCE($n) {
|
||||
$this->yyprintln('% Reduce by (' . $n . ') ' . self::$yyproduction[$n]);
|
||||
}
|
||||
|
||||
protected function YYTRACE_POP($state) {
|
||||
$this->yyprintln('% Recovering, uncovers state ' . $state);
|
||||
}
|
||||
|
||||
protected function YYTRACE_DISCARD($tokenId) {
|
||||
$this->yyprintln('% Discard ' . self::$terminals[$tokenId]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
#ifnot -t
|
||||
* Parses PHP code into a node tree.
|
||||
#endif
|
||||
#if -t
|
||||
* Parses PHP code into a node tree and prints out debugging information.
|
||||
#endif
|
||||
*
|
||||
* @param string $code The source code to parse
|
||||
*
|
||||
* @return PHPParser_Node[] Array of statements
|
||||
*/
|
||||
public function parse($code) {
|
||||
$this->lexer->startLexing($code);
|
||||
|
||||
// We start off with no lookahead-token
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
// The attributes for a node are taken from the first and last token of the node.
|
||||
// From the first token only the startAttributes are taken and from the last only
|
||||
// the endAttributes. Both are merged using the array union operator (+).
|
||||
$startAttributes = array('startLine' => 1);
|
||||
$endAttributes = array();
|
||||
|
||||
// In order to figure out the attributes for the starting token, we have to keep
|
||||
// them in a stack
|
||||
$attributeStack = array($startAttributes);
|
||||
|
||||
// Start off in the initial state and keep a stack of previous states
|
||||
$state = 0;
|
||||
$stateStack = array($state);
|
||||
|
||||
// AST stack (?)
|
||||
$this->yyastk = array();
|
||||
|
||||
// Current position in the stack(s)
|
||||
$this->stackPos = 0;
|
||||
|
||||
for (;;) {
|
||||
#if -t
|
||||
$this->YYTRACE_NEWSTATE($state, $tokenId);
|
||||
|
||||
#endif
|
||||
if (self::$yybase[$state] == 0) {
|
||||
$yyn = self::$yydefault[$state];
|
||||
} else {
|
||||
if ($tokenId === self::TOKEN_NONE) {
|
||||
// Fetch the next token id from the lexer and fetch additional info by-ref.
|
||||
// The end attributes are fetched into a temporary variable and only set once the token is really
|
||||
// shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is
|
||||
// reduced after a token was read but not yet shifted.
|
||||
$origTokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $nextEndAttributes);
|
||||
|
||||
// map the lexer token id to the internally used token id's
|
||||
$tokenId = $origTokenId >= 0 && $origTokenId < self::TOKEN_MAP_SIZE
|
||||
? self::$translate[$origTokenId]
|
||||
: self::TOKEN_INVALID;
|
||||
|
||||
if ($tokenId === self::TOKEN_INVALID) {
|
||||
throw new RangeException(sprintf(
|
||||
'The lexer returned an invalid token (id=%d, value=%s)',
|
||||
$origTokenId, $tokenValue
|
||||
));
|
||||
}
|
||||
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
#if -t
|
||||
|
||||
$this->YYTRACE_READ($tokenId);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((($yyn = self::$yybase[$state] + $tokenId) >= 0
|
||||
&& $yyn < self::YYLAST && self::$yycheck[$yyn] == $tokenId
|
||||
|| ($state < self::YY2TBLSTATE
|
||||
&& ($yyn = self::$yybase[$state + self::YYNLSTATES] + $tokenId) >= 0
|
||||
&& $yyn < self::YYLAST
|
||||
&& self::$yycheck[$yyn] == $tokenId))
|
||||
&& ($yyn = self::$yyaction[$yyn]) != self::YYDEFAULT) {
|
||||
/*
|
||||
* >= YYNLSTATE: shift and reduce
|
||||
* > 0: shift
|
||||
* = 0: accept
|
||||
* < 0: reduce
|
||||
* = -YYUNEXPECTED: error
|
||||
*/
|
||||
if ($yyn > 0) {
|
||||
/* shift */
|
||||
#if -t
|
||||
$this->YYTRACE_SHIFT($tokenId);
|
||||
|
||||
#endif
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state = $yyn;
|
||||
$this->yyastk[$this->stackPos] = $tokenValue;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
$endAttributes = $nextEndAttributes;
|
||||
$tokenId = self::TOKEN_NONE;
|
||||
|
||||
if ($yyn < self::YYNLSTATES)
|
||||
continue;
|
||||
|
||||
/* $yyn >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn -= self::YYNLSTATES;
|
||||
} else {
|
||||
$yyn = -$yyn;
|
||||
}
|
||||
} else {
|
||||
$yyn = self::$yydefault[$state];
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* reduce/error */
|
||||
if ($yyn == 0) {
|
||||
/* accept */
|
||||
#if -t
|
||||
$this->YYTRACE_ACCEPT();
|
||||
#endif
|
||||
return $this->yyval;
|
||||
} elseif ($yyn != self::YYUNEXPECTED) {
|
||||
/* reduce */
|
||||
#if -t
|
||||
$this->YYTRACE_REDUCE($yyn);
|
||||
#endif
|
||||
try {
|
||||
$this->{'yyn' . $yyn}(
|
||||
$attributeStack[$this->stackPos - self::$yylen[$yyn]]
|
||||
+ $endAttributes
|
||||
);
|
||||
} catch (PHPParser_Error $e) {
|
||||
if (-1 === $e->getRawLine()) {
|
||||
$e->setRawLine($startAttributes['startLine']);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/* Goto - shift nonterminal */
|
||||
$this->stackPos -= self::$yylen[$yyn];
|
||||
$yyn = self::$yylhs[$yyn];
|
||||
if (($yyp = self::$yygbase[$yyn] + $stateStack[$this->stackPos]) >= 0
|
||||
&& $yyp < self::YYGLAST
|
||||
&& self::$yygcheck[$yyp] == $yyn) {
|
||||
$state = self::$yygoto[$yyp];
|
||||
} else {
|
||||
$state = self::$yygdefault[$yyn];
|
||||
}
|
||||
|
||||
++$this->stackPos;
|
||||
|
||||
$stateStack[$this->stackPos] = $state;
|
||||
$this->yyastk[$this->stackPos] = $this->yyval;
|
||||
$attributeStack[$this->stackPos] = $startAttributes;
|
||||
} else {
|
||||
/* error */
|
||||
$expected = array();
|
||||
|
||||
$base = self::$yybase[$state];
|
||||
for ($i = 0; $i < self::TOKEN_MAP_SIZE; ++$i) {
|
||||
$n = $base + $i;
|
||||
if ($n >= 0 && $n < self::YYLAST && self::$yycheck[$n] == $i
|
||||
|| $state < self::YY2TBLSTATE
|
||||
&& ($n = self::$yybase[$state + self::YYNLSTATES] + $i) >= 0
|
||||
&& $n < self::YYLAST && self::$yycheck[$n] == $i
|
||||
) {
|
||||
if (self::$yyaction[$n] != self::YYUNEXPECTED) {
|
||||
if (count($expected) == 4) {
|
||||
/* Too many expected tokens */
|
||||
$expected = array();
|
||||
break;
|
||||
}
|
||||
|
||||
$expected[] = self::$terminals[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$expectedString = '';
|
||||
if ($expected) {
|
||||
$expectedString = ', expecting ' . implode(' or ', $expected);
|
||||
}
|
||||
|
||||
throw new PHPParser_Error(
|
||||
'Syntax error, unexpected ' . self::$terminals[$tokenId] . $expectedString,
|
||||
$startAttributes['startLine']
|
||||
);
|
||||
}
|
||||
|
||||
if ($state < self::YYNLSTATES)
|
||||
break;
|
||||
/* >= YYNLSTATES means shift-and-reduce */
|
||||
$yyn = $state - self::YYNLSTATES;
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifnot -t
|
||||
#reduce
|
||||
|
||||
protected function yyn%n($attributes) {
|
||||
%b
|
||||
}
|
||||
#noact
|
||||
|
||||
protected function yyn%n() {
|
||||
$this->yyval = $this->yyastk[$this->stackPos];
|
||||
}
|
||||
#endreduce
|
||||
#endif
|
||||
}
|
||||
#tailcode;
|
109
grammar/parser.template
Normal file
109
grammar/parser.template
Normal file
@ -0,0 +1,109 @@
|
||||
<?php declare(strict_types=1);
|
||||
$meta #
|
||||
#semval($) $this->semValue
|
||||
#semval($,%t) $this->semValue
|
||||
#semval(%n) $stackPos-(%l-%n)
|
||||
#semval(%n,%t) $stackPos-(%l-%n)
|
||||
|
||||
namespace PhpParser\Parser;
|
||||
|
||||
use PhpParser\Error;
|
||||
use PhpParser\Modifiers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar;
|
||||
use PhpParser\Node\Stmt;
|
||||
#include;
|
||||
|
||||
/* This is an automatically GENERATED file, which should not be manually edited.
|
||||
* Instead edit one of the following:
|
||||
* * the grammar file grammar/php.y
|
||||
* * the skeleton file grammar/parser.template
|
||||
* * the preprocessing script grammar/rebuildParsers.php
|
||||
*/
|
||||
class #(-p) extends \PhpParser\ParserAbstract
|
||||
{
|
||||
#tokenval
|
||||
public const %s = %n;
|
||||
#endtokenval
|
||||
|
||||
protected $tokenToSymbolMapSize = #(YYMAXLEX);
|
||||
protected $actionTableSize = #(YYLAST);
|
||||
protected $gotoTableSize = #(YYGLAST);
|
||||
|
||||
protected $invalidSymbol = #(YYBADCH);
|
||||
protected $errorSymbol = #(YYINTERRTOK);
|
||||
protected $defaultAction = #(YYDEFAULT);
|
||||
protected $unexpectedTokenRule = #(YYUNEXPECTED);
|
||||
|
||||
protected $YY2TBLSTATE = #(YY2TBLSTATE);
|
||||
protected $numNonLeafStates = #(YYNLSTATES);
|
||||
|
||||
protected $symbolToName = array(
|
||||
#listvar terminals
|
||||
);
|
||||
|
||||
protected $tokenToSymbol = array(
|
||||
#listvar yytranslate
|
||||
);
|
||||
|
||||
protected $action = array(
|
||||
#listvar yyaction
|
||||
);
|
||||
|
||||
protected $actionCheck = array(
|
||||
#listvar yycheck
|
||||
);
|
||||
|
||||
protected $actionBase = array(
|
||||
#listvar yybase
|
||||
);
|
||||
|
||||
protected $actionDefault = array(
|
||||
#listvar yydefault
|
||||
);
|
||||
|
||||
protected $goto = array(
|
||||
#listvar yygoto
|
||||
);
|
||||
|
||||
protected $gotoCheck = array(
|
||||
#listvar yygcheck
|
||||
);
|
||||
|
||||
protected $gotoBase = array(
|
||||
#listvar yygbase
|
||||
);
|
||||
|
||||
protected $gotoDefault = array(
|
||||
#listvar yygdefault
|
||||
);
|
||||
|
||||
protected $ruleToNonTerminal = array(
|
||||
#listvar yylhs
|
||||
);
|
||||
|
||||
protected $ruleToLength = array(
|
||||
#listvar yylen
|
||||
);
|
||||
#if -t
|
||||
|
||||
protected $productions = array(
|
||||
#production-strings;
|
||||
);
|
||||
#endif
|
||||
|
||||
protected function initReduceCallbacks(): void {
|
||||
$this->reduceCallbacks = [
|
||||
#reduce
|
||||
%n => function ($stackPos) {
|
||||
%b
|
||||
},
|
||||
#noact
|
||||
%n => null,
|
||||
#endreduce
|
||||
];
|
||||
}
|
||||
}
|
||||
#tailcode;
|
1383
grammar/php.y
Normal file
1383
grammar/php.y
Normal file
File diff suppressed because it is too large
Load Diff
178
grammar/phpyLang.php
Normal file
178
grammar/phpyLang.php
Normal file
@ -0,0 +1,178 @@
|
||||
<?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->getAttributes($this->tokenStartStack[#1], $this->tokenEndStack[$stackPos])';
|
||||
}
|
||||
|
||||
if ('stackAttributes' === $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
return '$this->getAttributes($this->tokenStartStack[' . $args[0] . '], '
|
||||
. ' $this->tokenEndStack[' . $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(1, $args, $name);
|
||||
|
||||
return $args[0] . ' = $this->maybeCreateNop($this->tokenStartStack[#1], $this->tokenEndStack[$stackPos])';
|
||||
}
|
||||
|
||||
if ('makeZeroLengthNop' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return $args[0] . ' = $this->maybeCreateZeroLengthNop($this->tokenPos);';
|
||||
}
|
||||
|
||||
if ('prependLeadingComments' === $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return '$comments = $this->getCommentsBeforeToken($this->tokenStartStack[#1]); $stmts = ' . $args[0] . '; '
|
||||
. 'if (!empty($comments)) {'
|
||||
. '$stmts[0]->setAttribute(\'comments\', '
|
||||
. 'array_merge($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,225 +0,0 @@
|
||||
<?php
|
||||
|
||||
$grammarFile = __DIR__ . '/zend_language_parser.phpy';
|
||||
$skeletonFile = __DIR__ . '/kmyacc.php.parser';
|
||||
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
||||
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
||||
$parserResultFile = __DIR__ . '/../lib/PHPParser/Parser.php';
|
||||
$debugParserResultFile = __DIR__ . '/../lib/PHPParser/Parser/Debug.php';
|
||||
|
||||
// check for kmyacc.exe binary in this directory, otherwise fall back to global name
|
||||
$kmyacc = __DIR__ . '/kmyacc.exe';
|
||||
if (!file_exists($kmyacc)) {
|
||||
$kmyacc = 'kmyacc';
|
||||
}
|
||||
|
||||
$options = array_flip($argv);
|
||||
$optionDebug = isset($options['--debug']);
|
||||
$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 ///
|
||||
///////////////////
|
||||
|
||||
echo 'Building temporary preproprocessed grammar file.', "\n";
|
||||
|
||||
$grammarCode = file_get_contents($grammarFile);
|
||||
|
||||
$grammarCode = resolveConstants($grammarCode);
|
||||
$grammarCode = resolveNodes($grammarCode);
|
||||
$grammarCode = resolveMacros($grammarCode);
|
||||
$grammarCode = resolveArrays($grammarCode);
|
||||
|
||||
file_put_contents($tmpGrammarFile, $grammarCode);
|
||||
|
||||
echo "Building parser.\n";
|
||||
$output = trim(shell_exec("$kmyacc -l -m $skeletonFile -p PHPParser_Parser $tmpGrammarFile 2>&1"));
|
||||
echo "Output: \"$output\"\n";
|
||||
|
||||
moveFileWithDirCheck($tmpResultFile, $parserResultFile);
|
||||
|
||||
if ($optionDebug) {
|
||||
echo "Building debug parser.\n";
|
||||
$output = trim(shell_exec("$kmyacc -t -v -l -m $skeletonFile -p PHPParser_Parser $tmpGrammarFile 2>&1"));
|
||||
echo "Output: \"$output\"\n";
|
||||
|
||||
moveFileWithDirCheck($tmpResultFile, $debugParserResultFile);
|
||||
}
|
||||
|
||||
if (!$optionKeepTmpGrammar) {
|
||||
unlink($tmpGrammarFile);
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
/// Preprocessing functions ///
|
||||
///////////////////////////////
|
||||
|
||||
function resolveConstants($code) {
|
||||
return preg_replace('~[A-Z][a-zA-Z_]++::~', 'PHPParser_Node_$0', $code);
|
||||
}
|
||||
|
||||
function resolveNodes($code) {
|
||||
return preg_replace_callback(
|
||||
'~(?<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 PHPParser_Node_' . $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 ('error' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return 'throw new PHPParser_Error(' . $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(2, $args, $name);
|
||||
|
||||
return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = PHPParser_Node_Scalar_String::parseEscapeSequences($s, ' . $args[1] . '); } }';
|
||||
}
|
||||
|
||||
if ('parseEncapsedDoc' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = PHPParser_Node_Scalar_String::parseEscapeSequences($s, null); } } $s = preg_replace(\'~(\r\n|\n|\r)$~\', \'\', $s); if (\'\' === $s) array_pop(' . $args[0] . ');';
|
||||
}
|
||||
|
||||
throw new Exception(sprintf('Unknown macro "%s"', $name));
|
||||
},
|
||||
$code
|
||||
);
|
||||
}
|
||||
|
||||
function assertArgs($num, $args, $name) {
|
||||
if ($num != count($args)) {
|
||||
die('Wrong argument count for ' . $name . '().');
|
||||
}
|
||||
}
|
||||
|
||||
function resolveArrays($code) {
|
||||
return preg_replace_callback(
|
||||
'~' . PARAMS . '~',
|
||||
function ($matches) {
|
||||
$elements = magicSplit(
|
||||
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
||||
$matches['params']
|
||||
);
|
||||
|
||||
// don't convert [] to array, it might have different meaning
|
||||
if (empty($elements)) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
$elementCodes = array();
|
||||
foreach ($elements as $element) {
|
||||
// convert only arrays where all elements have keys
|
||||
if (false === strpos($element, ':')) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
list($key, $value) = explode(':', $element, 2);
|
||||
$elementCodes[] = "'" . $key . "' =>" . $value;
|
||||
}
|
||||
|
||||
return 'array(' . implode(', ', $elementCodes) . ')';
|
||||
},
|
||||
$code
|
||||
);
|
||||
}
|
||||
|
||||
function moveFileWithDirCheck($fromPath, $toPath) {
|
||||
$dir = dirname($toPath);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
rename($fromPath, $toPath);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
/// 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);
|
||||
}
|
||||
|
||||
return array_filter($pieces);
|
||||
}
|
80
grammar/rebuildParsers.php
Normal file
80
grammar/rebuildParsers.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/phpyLang.php';
|
||||
|
||||
$parserToDefines = [
|
||||
'Php7' => ['PHP7' => true],
|
||||
'Php8' => ['PHP8' => true],
|
||||
];
|
||||
|
||||
$grammarFile = __DIR__ . '/php.y';
|
||||
$skeletonFile = __DIR__ . '/parser.template';
|
||||
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
||||
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
||||
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
||||
|
||||
$kmyacc = getenv('KMYACC');
|
||||
if (!$kmyacc) {
|
||||
// Use phpyacc from dev dependencies by default.
|
||||
$kmyacc = __DIR__ . '/../vendor/bin/phpyacc';
|
||||
}
|
||||
|
||||
$options = array_flip($argv);
|
||||
$optionDebug = isset($options['--debug']);
|
||||
$optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
|
||||
|
||||
///////////////////
|
||||
/// Main script ///
|
||||
///////////////////
|
||||
|
||||
foreach ($parserToDefines as $name => $defines) {
|
||||
echo "Building temporary $name grammar file.\n";
|
||||
|
||||
$grammarCode = file_get_contents($grammarFile);
|
||||
$grammarCode = replaceIfBlocks($grammarCode, $defines);
|
||||
$grammarCode = preprocessGrammar($grammarCode);
|
||||
|
||||
file_put_contents($tmpGrammarFile, $grammarCode);
|
||||
|
||||
$additionalArgs = $optionDebug ? '-t -v' : '';
|
||||
|
||||
echo "Building $name parser.\n";
|
||||
$output = execCmd("$kmyacc $additionalArgs -m $skeletonFile -p $name $tmpGrammarFile");
|
||||
|
||||
$resultCode = file_get_contents($tmpResultFile);
|
||||
$resultCode = removeTrailingWhitespace($resultCode);
|
||||
|
||||
ensureDirExists($resultDir);
|
||||
file_put_contents("$resultDir/$name.php", $resultCode);
|
||||
unlink($tmpResultFile);
|
||||
|
||||
if (!$optionKeepTmpGrammar) {
|
||||
unlink($tmpGrammarFile);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
/// Utility helper functions ///
|
||||
////////////////////////////////
|
||||
|
||||
function ensureDirExists($dir) {
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
}
|
||||
|
||||
function execCmd($cmd) {
|
||||
$output = trim(shell_exec("$cmd 2>&1") ?? '');
|
||||
if ($output !== "") {
|
||||
echo "> " . $cmd . "\n";
|
||||
echo $output;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
function replaceIfBlocks(string $code, array $defines): string {
|
||||
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] : '';
|
||||
}, $code);
|
||||
}
|
@ -1,906 +0,0 @@
|
||||
%pure_parser
|
||||
%expect 2
|
||||
|
||||
%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
|
||||
%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
|
||||
%left '?' ':'
|
||||
%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
|
||||
%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 '['
|
||||
%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_CHARACTER
|
||||
%token T_BAD_CHARACTER
|
||||
%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_ENDSWITCH
|
||||
%token T_CASE
|
||||
%token T_DEFAULT
|
||||
%token T_BREAK
|
||||
%token T_CONTINUE
|
||||
%token T_GOTO
|
||||
%token T_FUNCTION
|
||||
%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_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_COMMENT
|
||||
%token T_DOC_COMMENT
|
||||
%token T_OPEN_TAG
|
||||
%token T_OPEN_TAG_WITH_ECHO
|
||||
%token T_CLOSE_TAG
|
||||
%token T_WHITESPACE
|
||||
%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
|
||||
|
||||
%%
|
||||
|
||||
start:
|
||||
top_statement_list { $$ = Stmt_Namespace::postprocess($1); }
|
||||
;
|
||||
|
||||
top_statement_list:
|
||||
top_statement_list top_statement { pushNormalizing($1, $2); }
|
||||
| /* empty */ { init(); }
|
||||
;
|
||||
|
||||
namespace_name:
|
||||
T_STRING { init($1); }
|
||||
| namespace_name T_NS_SEPARATOR T_STRING { push($1, $3); }
|
||||
;
|
||||
|
||||
top_statement:
|
||||
statement { $$ = $1; }
|
||||
| function_declaration_statement { $$ = $1; }
|
||||
| class_declaration_statement { $$ = $1; }
|
||||
| T_HALT_COMPILER
|
||||
{ $$ = Stmt_HaltCompiler[$this->lexer->handleHaltCompiler()]; }
|
||||
| T_NAMESPACE namespace_name ';' { $$ = Stmt_Namespace[Name[$2], null]; }
|
||||
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = Stmt_Namespace[Name[$2], $4]; }
|
||||
| T_NAMESPACE '{' top_statement_list '}' { $$ = Stmt_Namespace[null, $3]; }
|
||||
| T_USE use_declarations ';' { $$ = Stmt_Use[$2]; }
|
||||
| T_CONST constant_declaration_list ';' { $$ = Stmt_Const[$2]; }
|
||||
;
|
||||
|
||||
use_declarations:
|
||||
use_declarations ',' use_declaration { push($1, $3); }
|
||||
| use_declaration { init($1); }
|
||||
;
|
||||
|
||||
use_declaration:
|
||||
namespace_name { $$ = Stmt_UseUse[Name[$1], null]; }
|
||||
| namespace_name T_AS T_STRING { $$ = Stmt_UseUse[Name[$1], $3]; }
|
||||
| T_NS_SEPARATOR namespace_name { $$ = Stmt_UseUse[Name[$2], null]; }
|
||||
| T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = Stmt_UseUse[Name[$2], $4]; }
|
||||
;
|
||||
|
||||
constant_declaration_list:
|
||||
constant_declaration_list ',' constant_declaration { push($1, $3); }
|
||||
| constant_declaration { init($1); }
|
||||
;
|
||||
|
||||
constant_declaration:
|
||||
T_STRING '=' static_scalar { $$ = Const[$1, $3]; }
|
||||
;
|
||||
|
||||
inner_statement_list:
|
||||
inner_statement_list inner_statement { pushNormalizing($1, $2); }
|
||||
| /* empty */ { init(); }
|
||||
;
|
||||
|
||||
inner_statement:
|
||||
statement { $$ = $1; }
|
||||
| function_declaration_statement { $$ = $1; }
|
||||
| class_declaration_statement { $$ = $1; }
|
||||
| T_HALT_COMPILER { error('__halt_compiler() can only be used from the outermost scope'); }
|
||||
;
|
||||
|
||||
statement:
|
||||
'{' inner_statement_list '}' { $$ = $2; }
|
||||
| T_IF parentheses_expr statement elseif_list else_single
|
||||
{ $$ = Stmt_If[$2, [stmts: toArray($3), elseifs: $4, else: $5]]; }
|
||||
| T_IF parentheses_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
|
||||
{ $$ = Stmt_If[$2, [stmts: $4, elseifs: $5, else: $6]]; }
|
||||
| T_WHILE parentheses_expr while_statement { $$ = Stmt_While[$2, $3]; }
|
||||
| T_DO statement T_WHILE parentheses_expr ';' { $$ = Stmt_Do [$4, toArray($2)]; }
|
||||
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
|
||||
{ $$ = Stmt_For[[init: $3, cond: $5, loop: $7, stmts: $9]]; }
|
||||
| T_SWITCH parentheses_expr switch_case_list { $$ = Stmt_Switch[$2, $3]; }
|
||||
| T_BREAK ';' { $$ = Stmt_Break[null]; }
|
||||
| T_BREAK expr ';' { $$ = Stmt_Break[$2]; }
|
||||
| T_CONTINUE ';' { $$ = Stmt_Continue[null]; }
|
||||
| T_CONTINUE expr ';' { $$ = Stmt_Continue[$2]; }
|
||||
| T_RETURN ';' { $$ = Stmt_Return[null]; }
|
||||
| T_RETURN expr ';' { $$ = Stmt_Return[$2]; }
|
||||
| yield_expr ';' { $$ = $1; }
|
||||
| T_GLOBAL global_var_list ';' { $$ = Stmt_Global[$2]; }
|
||||
| T_STATIC static_var_list ';' { $$ = Stmt_Static[$2]; }
|
||||
| T_ECHO expr_list ';' { $$ = Stmt_Echo[$2]; }
|
||||
| T_INLINE_HTML { $$ = Stmt_InlineHTML[$1]; }
|
||||
| expr ';' { $$ = $1; }
|
||||
| T_UNSET '(' variables_list ')' ';' { $$ = Stmt_Unset[$3]; }
|
||||
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
|
||||
{ $$ = Stmt_Foreach[$3, $5[0], [keyVar: null, byRef: $5[1], stmts: $7]]; }
|
||||
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
|
||||
{ $$ = Stmt_Foreach[$3, $7[0], [keyVar: $5, byRef: $7[1], stmts: $9]]; }
|
||||
| T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt_Declare[$3, $5]; }
|
||||
| ';' { $$ = array(); /* means: no statement */ }
|
||||
| T_TRY '{' inner_statement_list '}' catches optional_finally
|
||||
{ $$ = Stmt_TryCatch[$3, $5, $6]; }
|
||||
| T_THROW expr ';' { $$ = Stmt_Throw[$2]; }
|
||||
| T_GOTO T_STRING ';' { $$ = Stmt_Goto[$2]; }
|
||||
| T_STRING ':' { $$ = Stmt_Label[$1]; }
|
||||
;
|
||||
|
||||
catches:
|
||||
/* empty */ { init(); }
|
||||
| catches catch { push($1, $2); }
|
||||
;
|
||||
|
||||
catch:
|
||||
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
|
||||
{ $$ = Stmt_Catch[$3, parseVar($4), $7]; }
|
||||
;
|
||||
|
||||
optional_finally:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
|
||||
;
|
||||
|
||||
variables_list:
|
||||
variable { init($1); }
|
||||
| variables_list ',' variable { push($1, $3); }
|
||||
;
|
||||
|
||||
optional_ref:
|
||||
/* empty */ { $$ = false; }
|
||||
| '&' { $$ = true; }
|
||||
;
|
||||
|
||||
function_declaration_statement:
|
||||
T_FUNCTION optional_ref T_STRING '(' parameter_list ')' '{' inner_statement_list '}'
|
||||
{ $$ = Stmt_Function[$3, [byRef: $2, params: $5, stmts: $8]]; }
|
||||
;
|
||||
|
||||
class_declaration_statement:
|
||||
class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'
|
||||
{ $$ = Stmt_Class[$2, [type: $1, extends: $3, implements: $4, stmts: $6]]; }
|
||||
| T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'
|
||||
{ $$ = Stmt_Interface[$2, [extends: $3, stmts: $5]]; }
|
||||
| T_TRAIT T_STRING '{' class_statement_list '}'
|
||||
{ $$ = Stmt_Trait[$2, $4]; }
|
||||
;
|
||||
|
||||
class_entry_type:
|
||||
T_CLASS { $$ = 0; }
|
||||
| T_ABSTRACT T_CLASS { $$ = Stmt_Class::MODIFIER_ABSTRACT; }
|
||||
| T_FINAL T_CLASS { $$ = Stmt_Class::MODIFIER_FINAL; }
|
||||
;
|
||||
|
||||
extends_from:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_EXTENDS name { $$ = $2; }
|
||||
;
|
||||
|
||||
interface_extends_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_EXTENDS name_list { $$ = $2; }
|
||||
;
|
||||
|
||||
implements_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_IMPLEMENTS name_list { $$ = $2; }
|
||||
;
|
||||
|
||||
name_list:
|
||||
name { init($1); }
|
||||
| name_list ',' name { push($1, $3); }
|
||||
;
|
||||
|
||||
for_statement:
|
||||
statement { $$ = toArray($1); }
|
||||
| ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
|
||||
;
|
||||
|
||||
foreach_statement:
|
||||
statement { $$ = toArray($1); }
|
||||
| ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
|
||||
;
|
||||
|
||||
declare_statement:
|
||||
statement { $$ = toArray($1); }
|
||||
| ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
|
||||
;
|
||||
|
||||
declare_list:
|
||||
declare_list_element { init($1); }
|
||||
| declare_list ',' declare_list_element { push($1, $3); }
|
||||
;
|
||||
|
||||
declare_list_element:
|
||||
T_STRING '=' static_scalar { $$ = Stmt_DeclareDeclare[$1, $3]; }
|
||||
;
|
||||
|
||||
switch_case_list:
|
||||
'{' case_list '}' { $$ = $2; }
|
||||
| '{' ';' case_list '}' { $$ = $3; }
|
||||
| ':' case_list T_ENDSWITCH ';' { $$ = $2; }
|
||||
| ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
|
||||
;
|
||||
|
||||
case_list:
|
||||
/* empty */ { init(); }
|
||||
| case_list case { push($1, $2); }
|
||||
;
|
||||
|
||||
case:
|
||||
T_CASE expr case_separator inner_statement_list { $$ = Stmt_Case[$2, $4]; }
|
||||
| T_DEFAULT case_separator inner_statement_list { $$ = Stmt_Case[null, $3]; }
|
||||
;
|
||||
|
||||
case_separator:
|
||||
':'
|
||||
| ';'
|
||||
;
|
||||
|
||||
while_statement:
|
||||
statement { $$ = toArray($1); }
|
||||
| ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
|
||||
;
|
||||
|
||||
elseif_list:
|
||||
/* empty */ { init(); }
|
||||
| elseif_list elseif { push($1, $2); }
|
||||
;
|
||||
|
||||
elseif:
|
||||
T_ELSEIF parentheses_expr statement { $$ = Stmt_ElseIf[$2, toArray($3)]; }
|
||||
;
|
||||
|
||||
new_elseif_list:
|
||||
/* empty */ { init(); }
|
||||
| new_elseif_list new_elseif { push($1, $2); }
|
||||
;
|
||||
|
||||
new_elseif:
|
||||
T_ELSEIF parentheses_expr ':' inner_statement_list { $$ = Stmt_ElseIf[$2, $4]; }
|
||||
;
|
||||
|
||||
else_single:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_ELSE statement { $$ = Stmt_Else[toArray($2)]; }
|
||||
;
|
||||
|
||||
new_else_single:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_ELSE ':' inner_statement_list { $$ = Stmt_Else[$3]; }
|
||||
;
|
||||
|
||||
foreach_variable:
|
||||
variable { $$ = array($1, false); }
|
||||
| '&' variable { $$ = array($2, true); }
|
||||
| list_expr { $$ = array($1, false); }
|
||||
;
|
||||
|
||||
parameter_list:
|
||||
non_empty_parameter_list { $$ = $1; }
|
||||
| /* empty */ { $$ = array(); }
|
||||
;
|
||||
|
||||
non_empty_parameter_list:
|
||||
parameter { init($1); }
|
||||
| non_empty_parameter_list ',' parameter { push($1, $3); }
|
||||
;
|
||||
|
||||
parameter:
|
||||
optional_class_type optional_ref T_VARIABLE
|
||||
{ $$ = Param[parseVar($3), null, $1, $2]; }
|
||||
| optional_class_type optional_ref T_VARIABLE '=' static_scalar
|
||||
{ $$ = Param[parseVar($3), $5, $1, $2]; }
|
||||
;
|
||||
|
||||
optional_class_type:
|
||||
/* empty */ { $$ = null; }
|
||||
| name { $$ = $1; }
|
||||
| T_ARRAY { $$ = 'array'; }
|
||||
| T_CALLABLE { $$ = 'callable'; }
|
||||
;
|
||||
|
||||
argument_list:
|
||||
'(' ')' { $$ = array(); }
|
||||
| '(' non_empty_argument_list ')' { $$ = $2; }
|
||||
| '(' yield_expr ')' { $$ = array(Arg[$2, false]); }
|
||||
;
|
||||
|
||||
non_empty_argument_list:
|
||||
argument { init($1); }
|
||||
| non_empty_argument_list ',' argument { push($1, $3); }
|
||||
;
|
||||
|
||||
argument:
|
||||
expr { $$ = Arg[$1, false]; }
|
||||
| '&' variable { $$ = Arg[$2, true]; }
|
||||
;
|
||||
|
||||
global_var_list:
|
||||
global_var_list ',' global_var { push($1, $3); }
|
||||
| global_var { init($1); }
|
||||
;
|
||||
|
||||
global_var:
|
||||
T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
|
||||
| '$' variable { $$ = Expr_Variable[$2]; }
|
||||
| '$' '{' expr '}' { $$ = Expr_Variable[$3]; }
|
||||
;
|
||||
|
||||
static_var_list:
|
||||
static_var_list ',' static_var { push($1, $3); }
|
||||
| static_var { init($1); }
|
||||
;
|
||||
|
||||
static_var:
|
||||
T_VARIABLE { $$ = Stmt_StaticVar[parseVar($1), null]; }
|
||||
| T_VARIABLE '=' static_scalar { $$ = Stmt_StaticVar[parseVar($1), $3]; }
|
||||
;
|
||||
|
||||
class_statement_list:
|
||||
class_statement_list class_statement { push($1, $2); }
|
||||
| /* empty */ { init(); }
|
||||
;
|
||||
|
||||
class_statement:
|
||||
variable_modifiers property_declaration_list ';' { $$ = Stmt_Property[$1, $2]; }
|
||||
| T_CONST constant_declaration_list ';' { $$ = Stmt_ClassConst[$2]; }
|
||||
| method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body
|
||||
{ $$ = Stmt_ClassMethod[$4, [type: $1, byRef: $3, params: $6, stmts: $8]]; }
|
||||
| T_USE name_list trait_adaptations { $$ = Stmt_TraitUse[$2, $3]; }
|
||||
;
|
||||
|
||||
trait_adaptations:
|
||||
';' { $$ = array(); }
|
||||
| '{' trait_adaptation_list '}' { $$ = $2; }
|
||||
;
|
||||
|
||||
trait_adaptation_list:
|
||||
/* empty */ { init(); }
|
||||
| trait_adaptation_list trait_adaptation { push($1, $2); }
|
||||
;
|
||||
|
||||
trait_adaptation:
|
||||
trait_method_reference_fully_qualified T_INSTEADOF name_list ';'
|
||||
{ $$ = Stmt_TraitUseAdaptation_Precedence[$1[0], $1[1], $3]; }
|
||||
| trait_method_reference T_AS member_modifier T_STRING ';'
|
||||
{ $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], $3, $4]; }
|
||||
| trait_method_reference T_AS member_modifier ';'
|
||||
{ $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], $3, null]; }
|
||||
| trait_method_reference T_AS T_STRING ';'
|
||||
{ $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], null, $3]; }
|
||||
;
|
||||
|
||||
trait_method_reference_fully_qualified:
|
||||
name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = array($1, $3); }
|
||||
;
|
||||
trait_method_reference:
|
||||
trait_method_reference_fully_qualified { $$ = $1; }
|
||||
| T_STRING { $$ = array(null, $1); }
|
||||
;
|
||||
|
||||
method_body:
|
||||
';' /* abstract method */ { $$ = null; }
|
||||
| '{' inner_statement_list '}' { $$ = $2; }
|
||||
;
|
||||
|
||||
variable_modifiers:
|
||||
non_empty_member_modifiers { $$ = $1; }
|
||||
| T_VAR { $$ = Stmt_Class::MODIFIER_PUBLIC; }
|
||||
;
|
||||
|
||||
method_modifiers:
|
||||
/* empty */ { $$ = Stmt_Class::MODIFIER_PUBLIC; }
|
||||
| non_empty_member_modifiers { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_member_modifiers:
|
||||
member_modifier { $$ = $1; }
|
||||
| non_empty_member_modifiers member_modifier { Stmt_Class::verifyModifier($1, $2); $$ = $1 | $2; }
|
||||
;
|
||||
|
||||
member_modifier:
|
||||
T_PUBLIC { $$ = Stmt_Class::MODIFIER_PUBLIC; }
|
||||
| T_PROTECTED { $$ = Stmt_Class::MODIFIER_PROTECTED; }
|
||||
| T_PRIVATE { $$ = Stmt_Class::MODIFIER_PRIVATE; }
|
||||
| T_STATIC { $$ = Stmt_Class::MODIFIER_STATIC; }
|
||||
| T_ABSTRACT { $$ = Stmt_Class::MODIFIER_ABSTRACT; }
|
||||
| T_FINAL { $$ = Stmt_Class::MODIFIER_FINAL; }
|
||||
;
|
||||
|
||||
property_declaration_list:
|
||||
property_declaration { init($1); }
|
||||
| property_declaration_list ',' property_declaration { push($1, $3); }
|
||||
;
|
||||
|
||||
property_declaration:
|
||||
T_VARIABLE { $$ = Stmt_PropertyProperty[parseVar($1), null]; }
|
||||
| T_VARIABLE '=' static_scalar { $$ = Stmt_PropertyProperty[parseVar($1), $3]; }
|
||||
;
|
||||
|
||||
expr_list:
|
||||
expr_list ',' expr { push($1, $3); }
|
||||
| expr { init($1); }
|
||||
;
|
||||
|
||||
for_expr:
|
||||
/* empty */ { $$ = array(); }
|
||||
| expr_list { $$ = $1; }
|
||||
;
|
||||
|
||||
expr:
|
||||
variable { $$ = $1; }
|
||||
| list_expr '=' expr { $$ = Expr_Assign[$1, $3]; }
|
||||
| variable '=' expr { $$ = Expr_Assign[$1, $3]; }
|
||||
| variable '=' '&' variable { $$ = Expr_AssignRef[$1, $4]; }
|
||||
| variable '=' '&' new_expr { $$ = Expr_AssignRef[$1, $4]; }
|
||||
| new_expr { $$ = $1; }
|
||||
| T_CLONE expr { $$ = Expr_Clone[$2]; }
|
||||
| variable T_PLUS_EQUAL expr { $$ = Expr_AssignPlus [$1, $3]; }
|
||||
| variable T_MINUS_EQUAL expr { $$ = Expr_AssignMinus [$1, $3]; }
|
||||
| variable T_MUL_EQUAL expr { $$ = Expr_AssignMul [$1, $3]; }
|
||||
| variable T_DIV_EQUAL expr { $$ = Expr_AssignDiv [$1, $3]; }
|
||||
| variable T_CONCAT_EQUAL expr { $$ = Expr_AssignConcat [$1, $3]; }
|
||||
| variable T_MOD_EQUAL expr { $$ = Expr_AssignMod [$1, $3]; }
|
||||
| variable T_AND_EQUAL expr { $$ = Expr_AssignBitwiseAnd[$1, $3]; }
|
||||
| variable T_OR_EQUAL expr { $$ = Expr_AssignBitwiseOr [$1, $3]; }
|
||||
| variable T_XOR_EQUAL expr { $$ = Expr_AssignBitwiseXor[$1, $3]; }
|
||||
| variable T_SL_EQUAL expr { $$ = Expr_AssignShiftLeft [$1, $3]; }
|
||||
| variable T_SR_EQUAL expr { $$ = Expr_AssignShiftRight[$1, $3]; }
|
||||
| variable T_INC { $$ = Expr_PostInc[$1]; }
|
||||
| T_INC variable { $$ = Expr_PreInc [$2]; }
|
||||
| variable T_DEC { $$ = Expr_PostDec[$1]; }
|
||||
| T_DEC variable { $$ = Expr_PreDec [$2]; }
|
||||
| expr T_BOOLEAN_OR expr { $$ = Expr_BooleanOr [$1, $3]; }
|
||||
| expr T_BOOLEAN_AND expr { $$ = Expr_BooleanAnd[$1, $3]; }
|
||||
| expr T_LOGICAL_OR expr { $$ = Expr_LogicalOr [$1, $3]; }
|
||||
| expr T_LOGICAL_AND expr { $$ = Expr_LogicalAnd[$1, $3]; }
|
||||
| expr T_LOGICAL_XOR expr { $$ = Expr_LogicalXor[$1, $3]; }
|
||||
| expr '|' expr { $$ = Expr_BitwiseOr [$1, $3]; }
|
||||
| expr '&' expr { $$ = Expr_BitwiseAnd[$1, $3]; }
|
||||
| expr '^' expr { $$ = Expr_BitwiseXor[$1, $3]; }
|
||||
| expr '.' expr { $$ = Expr_Concat [$1, $3]; }
|
||||
| expr '+' expr { $$ = Expr_Plus [$1, $3]; }
|
||||
| expr '-' expr { $$ = Expr_Minus [$1, $3]; }
|
||||
| expr '*' expr { $$ = Expr_Mul [$1, $3]; }
|
||||
| expr '/' expr { $$ = Expr_Div [$1, $3]; }
|
||||
| expr '%' expr { $$ = Expr_Mod [$1, $3]; }
|
||||
| expr T_SL expr { $$ = Expr_ShiftLeft [$1, $3]; }
|
||||
| expr T_SR expr { $$ = Expr_ShiftRight[$1, $3]; }
|
||||
| '+' expr %prec T_INC { $$ = Expr_UnaryPlus [$2]; }
|
||||
| '-' expr %prec T_INC { $$ = Expr_UnaryMinus[$2]; }
|
||||
| '!' expr { $$ = Expr_BooleanNot[$2]; }
|
||||
| '~' expr { $$ = Expr_BitwiseNot[$2]; }
|
||||
| expr T_IS_IDENTICAL expr { $$ = Expr_Identical [$1, $3]; }
|
||||
| expr T_IS_NOT_IDENTICAL expr { $$ = Expr_NotIdentical [$1, $3]; }
|
||||
| expr T_IS_EQUAL expr { $$ = Expr_Equal [$1, $3]; }
|
||||
| expr T_IS_NOT_EQUAL expr { $$ = Expr_NotEqual [$1, $3]; }
|
||||
| expr '<' expr { $$ = Expr_Smaller [$1, $3]; }
|
||||
| expr T_IS_SMALLER_OR_EQUAL expr { $$ = Expr_SmallerOrEqual[$1, $3]; }
|
||||
| expr '>' expr { $$ = Expr_Greater [$1, $3]; }
|
||||
| expr T_IS_GREATER_OR_EQUAL expr { $$ = Expr_GreaterOrEqual[$1, $3]; }
|
||||
| expr T_INSTANCEOF class_name_reference { $$ = Expr_Instanceof [$1, $3]; }
|
||||
| parentheses_expr { $$ = $1; }
|
||||
/* we need a separate '(' new_expr ')' rule to avoid problems caused by a s/r conflict */
|
||||
| '(' new_expr ')' { $$ = $2; }
|
||||
| expr '?' expr ':' expr { $$ = Expr_Ternary[$1, $3, $5]; }
|
||||
| expr '?' ':' expr { $$ = Expr_Ternary[$1, null, $4]; }
|
||||
| T_ISSET '(' variables_list ')' { $$ = Expr_Isset[$3]; }
|
||||
| T_EMPTY '(' expr ')' { $$ = Expr_Empty[$3]; }
|
||||
| T_INCLUDE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_INCLUDE]; }
|
||||
| T_INCLUDE_ONCE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_INCLUDE_ONCE]; }
|
||||
| T_EVAL parentheses_expr { $$ = Expr_Eval[$2]; }
|
||||
| T_REQUIRE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_REQUIRE]; }
|
||||
| T_REQUIRE_ONCE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_REQUIRE_ONCE]; }
|
||||
| T_INT_CAST expr { $$ = Expr_Cast_Int [$2]; }
|
||||
| T_DOUBLE_CAST expr { $$ = Expr_Cast_Double [$2]; }
|
||||
| T_STRING_CAST expr { $$ = Expr_Cast_String [$2]; }
|
||||
| T_ARRAY_CAST expr { $$ = Expr_Cast_Array [$2]; }
|
||||
| T_OBJECT_CAST expr { $$ = Expr_Cast_Object [$2]; }
|
||||
| T_BOOL_CAST expr { $$ = Expr_Cast_Bool [$2]; }
|
||||
| T_UNSET_CAST expr { $$ = Expr_Cast_Unset [$2]; }
|
||||
| T_EXIT exit_expr { $$ = Expr_Exit [$2]; }
|
||||
| '@' expr { $$ = Expr_ErrorSuppress[$2]; }
|
||||
| scalar { $$ = $1; }
|
||||
| array_expr { $$ = $1; }
|
||||
| scalar_dereference { $$ = $1; }
|
||||
| '`' backticks_expr '`' { $$ = Expr_ShellExec[$2]; }
|
||||
| T_PRINT expr { $$ = Expr_Print[$2]; }
|
||||
| T_YIELD { $$ = Expr_Yield[null, null]; }
|
||||
| T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
|
||||
{ $$ = Expr_Closure[[static: false, byRef: $2, params: $4, uses: $6, stmts: $8]]; }
|
||||
| T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
|
||||
{ $$ = Expr_Closure[[static: true, byRef: $3, params: $5, uses: $7, stmts: $9]]; }
|
||||
;
|
||||
|
||||
parentheses_expr:
|
||||
'(' expr ')' { $$ = $2; }
|
||||
| '(' yield_expr ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
yield_expr:
|
||||
T_YIELD expr { $$ = Expr_Yield[$2, null]; }
|
||||
| T_YIELD expr T_DOUBLE_ARROW expr { $$ = Expr_Yield[$4, $2]; }
|
||||
;
|
||||
|
||||
array_expr:
|
||||
T_ARRAY '(' array_pair_list ')' { $$ = Expr_Array[$3]; }
|
||||
| '[' array_pair_list ']' { $$ = Expr_Array[$2]; }
|
||||
;
|
||||
|
||||
scalar_dereference:
|
||||
array_expr '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
| T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']'
|
||||
{ $$ = Expr_ArrayDimFetch[Scalar_String[Scalar_String::parse($1)], $3]; }
|
||||
| scalar_dereference '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
/* alternative array syntax missing intentionally */
|
||||
;
|
||||
|
||||
new_expr:
|
||||
T_NEW class_name_reference ctor_arguments { $$ = Expr_New[$2, $3]; }
|
||||
;
|
||||
|
||||
lexical_vars:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_USE '(' lexical_var_list ')' { $$ = $3; }
|
||||
;
|
||||
|
||||
lexical_var_list:
|
||||
lexical_var { init($1); }
|
||||
| lexical_var_list ',' lexical_var { push($1, $3); }
|
||||
;
|
||||
|
||||
lexical_var:
|
||||
optional_ref T_VARIABLE { $$ = Expr_ClosureUse[parseVar($2), $1]; }
|
||||
;
|
||||
|
||||
function_call:
|
||||
name argument_list { $$ = Expr_FuncCall[$1, $2]; }
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_STRING argument_list
|
||||
{ $$ = Expr_StaticCall[$1, $3, $4]; }
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '{' expr '}' argument_list
|
||||
{ $$ = Expr_StaticCall[$1, $4, $6]; }
|
||||
| static_property argument_list {
|
||||
if ($1 instanceof PHPParser_Node_Expr_StaticPropertyFetch) {
|
||||
$$ = Expr_StaticCall[$1->class, Expr_Variable[$1->name], $2];
|
||||
} elseif ($1 instanceof PHPParser_Node_Expr_ArrayDimFetch) {
|
||||
$tmp = $1;
|
||||
while ($tmp->var instanceof PHPParser_Node_Expr_ArrayDimFetch) {
|
||||
$tmp = $tmp->var;
|
||||
}
|
||||
|
||||
$$ = Expr_StaticCall[$tmp->var->class, $1, $2];
|
||||
$tmp->var = Expr_Variable[$tmp->var->name];
|
||||
} else {
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
| variable_without_objects argument_list
|
||||
{ $$ = Expr_FuncCall[$1, $2]; }
|
||||
| function_call '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
/* alternative array syntax missing intentionally */
|
||||
;
|
||||
|
||||
class_name:
|
||||
T_STATIC { $$ = Name['static']; }
|
||||
| name { $$ = $1; }
|
||||
;
|
||||
|
||||
name:
|
||||
namespace_name { $$ = Name[$1]; }
|
||||
| T_NS_SEPARATOR namespace_name { $$ = Name_FullyQualified[$2]; }
|
||||
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $$ = Name_Relative[$3]; }
|
||||
;
|
||||
|
||||
class_name_reference:
|
||||
class_name { $$ = $1; }
|
||||
| dynamic_class_name_reference { $$ = $1; }
|
||||
;
|
||||
|
||||
dynamic_class_name_reference:
|
||||
object_access_for_dcnr { $$ = $1; }
|
||||
| base_variable { $$ = $1; }
|
||||
;
|
||||
|
||||
class_name_or_var:
|
||||
class_name { $$ = $1; }
|
||||
| reference_variable { $$ = $1; }
|
||||
;
|
||||
|
||||
object_access_for_dcnr:
|
||||
| base_variable T_OBJECT_OPERATOR object_property
|
||||
{ $$ = Expr_PropertyFetch[$1, $3]; }
|
||||
| object_access_for_dcnr T_OBJECT_OPERATOR object_property
|
||||
{ $$ = Expr_PropertyFetch[$1, $3]; }
|
||||
| object_access_for_dcnr '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
| object_access_for_dcnr '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
;
|
||||
|
||||
exit_expr:
|
||||
/* empty */ { $$ = null; }
|
||||
| '(' ')' { $$ = null; }
|
||||
| parentheses_expr { $$ = $1; }
|
||||
;
|
||||
|
||||
backticks_expr:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_ENCAPSED_AND_WHITESPACE { $$ = array(Scalar_String::parseEscapeSequences($1, '`')); }
|
||||
| encaps_list { parseEncapsed($1, '`'); $$ = $1; }
|
||||
;
|
||||
|
||||
ctor_arguments:
|
||||
/* empty */ { $$ = array(); }
|
||||
| argument_list { $$ = $1; }
|
||||
;
|
||||
|
||||
common_scalar:
|
||||
T_LNUMBER { $$ = Scalar_LNumber[Scalar_LNumber::parse($1)]; }
|
||||
| T_DNUMBER { $$ = Scalar_DNumber[Scalar_DNumber::parse($1)]; }
|
||||
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar_String[Scalar_String::parse($1)]; }
|
||||
| T_LINE { $$ = Scalar_LineConst[]; }
|
||||
| T_FILE { $$ = Scalar_FileConst[]; }
|
||||
| T_DIR { $$ = Scalar_DirConst[]; }
|
||||
| T_CLASS_C { $$ = Scalar_ClassConst[]; }
|
||||
| T_TRAIT_C { $$ = Scalar_TraitConst[]; }
|
||||
| T_METHOD_C { $$ = Scalar_MethodConst[]; }
|
||||
| T_FUNC_C { $$ = Scalar_FuncConst[]; }
|
||||
| T_NS_C { $$ = Scalar_NSConst[]; }
|
||||
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
|
||||
{ $$ = Scalar_String[Scalar_String::parseDocString($1, $2)]; }
|
||||
| T_START_HEREDOC T_END_HEREDOC
|
||||
{ $$ = Scalar_String['']; }
|
||||
| name { $$ = Expr_ConstFetch[$1]; }
|
||||
;
|
||||
|
||||
static_scalar: /* compile-time evaluated scalars */
|
||||
common_scalar { $$ = $1; }
|
||||
| class_name T_PAAMAYIM_NEKUDOTAYIM class_const_name { $$ = Expr_ClassConstFetch[$1, $3]; }
|
||||
| '+' static_scalar { $$ = Expr_UnaryPlus[$2]; }
|
||||
| '-' static_scalar { $$ = Expr_UnaryMinus[$2]; }
|
||||
| T_ARRAY '(' static_array_pair_list ')' { $$ = Expr_Array[$3]; }
|
||||
| '[' static_array_pair_list ']' { $$ = Expr_Array[$2]; }
|
||||
;
|
||||
|
||||
scalar:
|
||||
common_scalar { $$ = $1; }
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM class_const_name
|
||||
{ $$ = Expr_ClassConstFetch[$1, $3]; }
|
||||
| '"' encaps_list '"'
|
||||
{ parseEncapsed($2, '"'); $$ = Scalar_Encapsed[$2]; }
|
||||
| T_START_HEREDOC encaps_list T_END_HEREDOC
|
||||
{ parseEncapsedDoc($2); $$ = Scalar_Encapsed[$2]; }
|
||||
;
|
||||
|
||||
class_const_name:
|
||||
T_STRING { $$ = $1; }
|
||||
| T_CLASS { $$ = 'class'; }
|
||||
;
|
||||
|
||||
static_array_pair_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| non_empty_static_array_pair_list optional_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
optional_comma:
|
||||
/* empty */
|
||||
| ','
|
||||
;
|
||||
|
||||
non_empty_static_array_pair_list:
|
||||
non_empty_static_array_pair_list ',' static_array_pair { push($1, $3); }
|
||||
| static_array_pair { init($1); }
|
||||
;
|
||||
|
||||
static_array_pair:
|
||||
static_scalar T_DOUBLE_ARROW static_scalar { $$ = Expr_ArrayItem[$3, $1, false]; }
|
||||
| static_scalar { $$ = Expr_ArrayItem[$1, null, false]; }
|
||||
;
|
||||
|
||||
variable:
|
||||
object_access { $$ = $1; }
|
||||
| base_variable { $$ = $1; }
|
||||
| function_call { $$ = $1; }
|
||||
| new_expr_array_deref { $$ = $1; }
|
||||
;
|
||||
|
||||
new_expr_array_deref:
|
||||
'(' new_expr ')' '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$2, $5]; }
|
||||
| new_expr_array_deref '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
/* alternative array syntax missing intentionally */
|
||||
;
|
||||
|
||||
object_access:
|
||||
variable_or_new_expr T_OBJECT_OPERATOR object_property
|
||||
{ $$ = Expr_PropertyFetch[$1, $3]; }
|
||||
| variable_or_new_expr T_OBJECT_OPERATOR object_property argument_list
|
||||
{ $$ = Expr_MethodCall[$1, $3, $4]; }
|
||||
| object_access argument_list { $$ = Expr_FuncCall[$1, $2]; }
|
||||
| object_access '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
| object_access '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
;
|
||||
|
||||
variable_or_new_expr:
|
||||
variable { $$ = $1; }
|
||||
| '(' new_expr ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
variable_without_objects:
|
||||
reference_variable { $$ = $1; }
|
||||
| '$' variable_without_objects { $$ = Expr_Variable[$2]; }
|
||||
;
|
||||
|
||||
base_variable:
|
||||
variable_without_objects { $$ = $1; }
|
||||
| static_property { $$ = $1; }
|
||||
;
|
||||
|
||||
static_property:
|
||||
class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '$' reference_variable
|
||||
{ $$ = Expr_StaticPropertyFetch[$1, $4]; }
|
||||
| static_property_with_arrays { $$ = $1; }
|
||||
;
|
||||
|
||||
static_property_with_arrays:
|
||||
class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_VARIABLE
|
||||
{ $$ = Expr_StaticPropertyFetch[$1, parseVar($3)]; }
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '$' '{' expr '}'
|
||||
{ $$ = Expr_StaticPropertyFetch[$1, $5]; }
|
||||
| static_property_with_arrays '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
| static_property_with_arrays '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
;
|
||||
|
||||
reference_variable:
|
||||
reference_variable '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
| reference_variable '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
|
||||
| T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
|
||||
| '$' '{' expr '}' { $$ = Expr_Variable[$3]; }
|
||||
;
|
||||
|
||||
dim_offset:
|
||||
/* empty */ { $$ = null; }
|
||||
| expr { $$ = $1; }
|
||||
;
|
||||
|
||||
object_property:
|
||||
T_STRING { $$ = $1; }
|
||||
| '{' expr '}' { $$ = $2; }
|
||||
| variable_without_objects { $$ = $1; }
|
||||
;
|
||||
|
||||
list_expr:
|
||||
T_LIST '(' list_expr_elements ')' { $$ = Expr_List[$3]; }
|
||||
;
|
||||
|
||||
list_expr_elements:
|
||||
list_expr_elements ',' list_expr_element { push($1, $3); }
|
||||
| list_expr_element { init($1); }
|
||||
;
|
||||
|
||||
list_expr_element:
|
||||
variable { $$ = $1; }
|
||||
| list_expr { $$ = $1; }
|
||||
| /* empty */ { $$ = null; }
|
||||
;
|
||||
|
||||
array_pair_list:
|
||||
/* empty */ { $$ = array(); }
|
||||
| non_empty_array_pair_list optional_comma { $$ = $1; }
|
||||
;
|
||||
|
||||
non_empty_array_pair_list:
|
||||
non_empty_array_pair_list ',' array_pair { push($1, $3); }
|
||||
| array_pair { init($1); }
|
||||
;
|
||||
|
||||
array_pair:
|
||||
expr T_DOUBLE_ARROW expr { $$ = Expr_ArrayItem[$3, $1, false]; }
|
||||
| expr { $$ = Expr_ArrayItem[$1, null, false]; }
|
||||
| expr T_DOUBLE_ARROW '&' variable { $$ = Expr_ArrayItem[$4, $1, true]; }
|
||||
| '&' variable { $$ = Expr_ArrayItem[$2, null, true]; }
|
||||
;
|
||||
|
||||
encaps_list:
|
||||
encaps_list encaps_var { push($1, $2); }
|
||||
| encaps_list T_ENCAPSED_AND_WHITESPACE { push($1, $2); }
|
||||
| encaps_var { init($1); }
|
||||
| T_ENCAPSED_AND_WHITESPACE encaps_var { init($1, $2); }
|
||||
;
|
||||
|
||||
encaps_var:
|
||||
T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
|
||||
| T_VARIABLE '[' encaps_var_offset ']' { $$ = Expr_ArrayDimFetch[Expr_Variable[parseVar($1)], $3]; }
|
||||
| T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = Expr_PropertyFetch[Expr_Variable[parseVar($1)], $3]; }
|
||||
| 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 ']' '}'
|
||||
{ $$ = Expr_ArrayDimFetch[Expr_Variable[$2], $4]; }
|
||||
| T_CURLY_OPEN variable '}' { $$ = $2; }
|
||||
;
|
||||
|
||||
encaps_var_offset:
|
||||
T_STRING { $$ = Scalar_String[$1]; }
|
||||
| T_NUM_STRING { $$ = Scalar_String[$1]; }
|
||||
| T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
|
||||
;
|
||||
|
||||
%%
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class PHPParser_Autoloader
|
||||
{
|
||||
/**
|
||||
* Registers PHPParser_Autoloader as an SPL autoloader.
|
||||
*/
|
||||
static public function register()
|
||||
{
|
||||
ini_set('unserialize_callback_func', 'spl_autoload_call');
|
||||
spl_autoload_register(array(__CLASS__, 'autoload'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles autoloading of classes.
|
||||
*
|
||||
* @param string $class A class name.
|
||||
*/
|
||||
static public function autoload($class)
|
||||
{
|
||||
if (0 !== strpos($class, 'PHPParser')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file = dirname(dirname(__FILE__)) . '/' . strtr($class, '_', '/') . '.php';
|
||||
if (is_file($file)) {
|
||||
require $file;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
interface PHPParser_Builder
|
||||
{
|
||||
/**
|
||||
* Returns the built node.
|
||||
*
|
||||
* @return PHPParser_Node The built node
|
||||
*/
|
||||
public function getNode();
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Class extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $extends;
|
||||
protected $implements;
|
||||
protected $type;
|
||||
|
||||
protected $uses;
|
||||
protected $constants;
|
||||
protected $properties;
|
||||
protected $methods;
|
||||
|
||||
/**
|
||||
* Creates a class builder.
|
||||
*
|
||||
* @param string $name Name of the class
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
|
||||
$this->type = 0;
|
||||
$this->extends = null;
|
||||
$this->implements = array();
|
||||
|
||||
$this->uses = $this->constants = $this->properties = $this->methods = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extends a class.
|
||||
*
|
||||
* @param PHPParser_Node_Name|string $class Name of class to extend
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function extend($class) {
|
||||
$this->extends = $this->normalizeName($class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements one or more interfaces.
|
||||
*
|
||||
* @param PHPParser_Node_Name|string $interface Name of interface to implement
|
||||
* @param PHPParser_Node_Name|string $... More interfaces to implement
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function implement() {
|
||||
foreach (func_get_args() as $interface) {
|
||||
$this->implements[] = $this->normalizeName($interface);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the class abstract.
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeAbstract() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_ABSTRACT);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the class final.
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeFinal() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_FINAL);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement.
|
||||
*
|
||||
* @param PHPParser_Node_Stmt|PHPParser_Builder $stmt The statement to add
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$stmt = $this->normalizeNode($stmt);
|
||||
|
||||
$targets = array(
|
||||
'Stmt_TraitUse' => &$this->uses,
|
||||
'Stmt_ClassConst' => &$this->constants,
|
||||
'Stmt_Property' => &$this->properties,
|
||||
'Stmt_ClassMethod' => &$this->methods,
|
||||
);
|
||||
|
||||
$type = $stmt->getType();
|
||||
if (!isset($targets[$type])) {
|
||||
throw new LogicException(sprintf('Unexpected node of type "%s"', $type));
|
||||
}
|
||||
|
||||
$targets[$type][] = $stmt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple statements.
|
||||
*
|
||||
* @param array $stmts The statements to add
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmts(array $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
$this->addStmt($stmt);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built class node.
|
||||
*
|
||||
* @return PHPParser_Node_Stmt_Class The built class node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Stmt_Class($this->name, array(
|
||||
'type' => $this->type,
|
||||
'extends' => $this->extends,
|
||||
'implements' => $this->implements,
|
||||
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
|
||||
));
|
||||
}
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Function extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $returnByRef;
|
||||
protected $params;
|
||||
protected $stmts;
|
||||
|
||||
/**
|
||||
* Creates a function builder.
|
||||
*
|
||||
* @param string $name Name of the function
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
|
||||
$this->returnByRef = false;
|
||||
$this->params = array();
|
||||
$this->stmts = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the function return by reference.
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeReturnByRef() {
|
||||
$this->returnByRef = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a parameter.
|
||||
*
|
||||
* @param PHPParser_Node_Param|PHPParser_Builder_Param $param The parameter to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParam($param) {
|
||||
$param = $this->normalizeNode($param);
|
||||
|
||||
if (!$param instanceof PHPParser_Node_Param) {
|
||||
throw new LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
|
||||
}
|
||||
|
||||
$this->params[] = $param;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple parameters.
|
||||
*
|
||||
* @param array $params The parameters to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParams(array $params) {
|
||||
foreach ($params as $param) {
|
||||
$this->addParam($param);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement.
|
||||
*
|
||||
* @param PHPParser_Node|PHPParser_Builder $stmt The statement to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$this->stmts[] = $this->normalizeNode($stmt);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple statements.
|
||||
*
|
||||
* @param array $stmts The statements to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmts(array $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
$this->addStmt($stmt);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built function node.
|
||||
*
|
||||
* @return PHPParser_Node_Stmt_Function The built function node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Stmt_Function($this->name, array(
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'stmts' => $this->stmts,
|
||||
));
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Interface extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
protected $extends;
|
||||
protected $constants;
|
||||
protected $methods;
|
||||
|
||||
/**
|
||||
* Creates an interface builder.
|
||||
*
|
||||
* @param string $name Name of the interface
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
$this->extends = array();
|
||||
$this->constants = $this->methods = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extends one or more interfaces.
|
||||
*
|
||||
* @param PHPParser_Node_Name|string $interface Name of interface to extend
|
||||
* @param PHPParser_Node_Name|string $... More interfaces to extend
|
||||
*
|
||||
* @return PHPParser_Builder_Interface The builder instance (for fluid interface)
|
||||
*/
|
||||
public function extend() {
|
||||
foreach (func_get_args() as $interface) {
|
||||
$this->extends[] = $this->normalizeName($interface);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement.
|
||||
*
|
||||
* @param PHPParser_Node_Stmt|PHPParser_Builder $stmt The statement to add
|
||||
*
|
||||
* @return PHPParser_Builder_Interface The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$stmt = $this->normalizeNode($stmt);
|
||||
|
||||
$type = $stmt->getType();
|
||||
switch ($type) {
|
||||
case 'Stmt_ClassConst':
|
||||
$this->constants[] = $stmt;
|
||||
break;
|
||||
|
||||
case 'Stmt_ClassMethod':
|
||||
// we erase all statements in the body of an interface method
|
||||
$stmt->stmts = null;
|
||||
$this->methods[] = $stmt;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new LogicException(sprintf('Unexpected node of type "%s"', $type));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple statements.
|
||||
*
|
||||
* @param array $stmts The statements to add
|
||||
*
|
||||
* @return PHPParser_Builder_Class The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmts(array $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
$this->addStmt($stmt);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built class node.
|
||||
*
|
||||
* @return PHPParser_Node_Stmt_Interface The built interface node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Stmt_Interface($this->name, array(
|
||||
'extends' => $this->extends,
|
||||
'stmts' => array_merge($this->constants, $this->methods),
|
||||
));
|
||||
}
|
||||
}
|
@ -1,187 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Method extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $type;
|
||||
protected $returnByRef;
|
||||
protected $params;
|
||||
protected $stmts;
|
||||
|
||||
/**
|
||||
* Creates a method builder.
|
||||
*
|
||||
* @param string $name Name of the method
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
|
||||
$this->type = 0;
|
||||
$this->returnByRef = false;
|
||||
$this->params = array();
|
||||
$this->stmts = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the method public.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePublic() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the method protected.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeProtected() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the method private.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePrivate() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the method static.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeStatic() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_STATIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the method abstract.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeAbstract() {
|
||||
if (!empty($this->stmts)) {
|
||||
throw new LogicException('Cannot make method with statements abstract');
|
||||
}
|
||||
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_ABSTRACT);
|
||||
$this->stmts = null; // abstract methods don't have statements
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the method final.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeFinal() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_FINAL);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the method return by reference.
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeReturnByRef() {
|
||||
$this->returnByRef = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a parameter.
|
||||
*
|
||||
* @param PHPParser_Node_Param|PHPParser_Builder_Param $param The parameter to add
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParam($param) {
|
||||
$param = $this->normalizeNode($param);
|
||||
|
||||
if (!$param instanceof PHPParser_Node_Param) {
|
||||
throw new LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
|
||||
}
|
||||
|
||||
$this->params[] = $param;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple parameters.
|
||||
*
|
||||
* @param array $params The parameters to add
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParams(array $params) {
|
||||
foreach ($params as $param) {
|
||||
$this->addParam($param);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement.
|
||||
*
|
||||
* @param PHPParser_Node|PHPParser_Builder $stmt The statement to add
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
if (null === $this->stmts) {
|
||||
throw new LogicException('Cannot add statements to an abstract method');
|
||||
}
|
||||
|
||||
$this->stmts[] = $this->normalizeNode($stmt);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple statements.
|
||||
*
|
||||
* @param array $stmts The statements to add
|
||||
*
|
||||
* @return PHPParser_Builder_Method The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmts(array $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
$this->addStmt($stmt);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built method node.
|
||||
*
|
||||
* @return PHPParser_Node_Stmt_ClassMethod The built method node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Stmt_ClassMethod($this->name, array(
|
||||
'type' => $this->type !== 0 ? $this->type : PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'stmts' => $this->stmts,
|
||||
));
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Param extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $default;
|
||||
protected $type;
|
||||
protected $byRef;
|
||||
|
||||
/**
|
||||
* Creates a parameter builder.
|
||||
*
|
||||
* @param string $name Name of the parameter
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
|
||||
$this->default = null;
|
||||
$this->type = null;
|
||||
$this->byRef = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default value for the parameter.
|
||||
*
|
||||
* @param mixed $value Default value to use
|
||||
*
|
||||
* @return PHPParser_Builder_Param The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setDefault($value) {
|
||||
$this->default = $this->normalizeValue($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets type hint for the parameter.
|
||||
*
|
||||
* @param string|PHPParser_Node_Name $type Type hint to use
|
||||
*
|
||||
* @return PHPParser_Builder_Param The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setTypeHint($type) {
|
||||
if ($type === 'array' || $type === 'callable') {
|
||||
$this->type = $type;
|
||||
} else {
|
||||
$this->type = $this->normalizeName($type);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the parameter accept the value by reference.
|
||||
*
|
||||
* @return PHPParser_Builder_Param The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeByRef() {
|
||||
$this->byRef = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built parameter node.
|
||||
*
|
||||
* @return PHPParser_Node_Param The built parameter node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Param(
|
||||
$this->name, $this->default, $this->type, $this->byRef
|
||||
);
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Property extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $type;
|
||||
protected $default;
|
||||
|
||||
/**
|
||||
* Creates a property builder.
|
||||
*
|
||||
* @param string $name Name of the property
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
|
||||
$this->type = 0;
|
||||
$this->default = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the property public.
|
||||
*
|
||||
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePublic() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the property protected.
|
||||
*
|
||||
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeProtected() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the property private.
|
||||
*
|
||||
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePrivate() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the property static.
|
||||
*
|
||||
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeStatic() {
|
||||
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_STATIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default value for the property.
|
||||
*
|
||||
* @param mixed $value Default value to use
|
||||
*
|
||||
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setDefault($value) {
|
||||
$this->default = $this->normalizeValue($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built class node.
|
||||
*
|
||||
* @return PHPParser_Node_Stmt_Property The built property node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Stmt_Property(
|
||||
$this->type !== 0 ? $this->type : PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
|
||||
array(
|
||||
new PHPParser_Node_Stmt_PropertyProperty($this->name, $this->default)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
abstract class PHPParser_BuilderAbstract implements PHPParser_Builder {
|
||||
/**
|
||||
* Normalizes a node: Converts builder objects to nodes.
|
||||
*
|
||||
* @param PHPParser_Node|PHPParser_Builder $node The node to normalize
|
||||
*
|
||||
* @return PHPParser_Node The normalized node
|
||||
*/
|
||||
protected function normalizeNode($node) {
|
||||
if ($node instanceof PHPParser_Builder) {
|
||||
return $node->getNode();
|
||||
} elseif ($node instanceof PHPParser_Node) {
|
||||
return $node;
|
||||
}
|
||||
|
||||
throw new LogicException('Expected node or builder object');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a name: Converts plain string names to PHPParser_Node_Name.
|
||||
*
|
||||
* @param PHPParser_Node_Name|string $name The name to normalize
|
||||
*
|
||||
* @return PHPParser_Node_Name The normalized name
|
||||
*/
|
||||
protected function normalizeName($name) {
|
||||
if ($name instanceof PHPParser_Node_Name) {
|
||||
return $name;
|
||||
} else {
|
||||
return new PHPParser_Node_Name($name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a value: Converts nulls, booleans, integers,
|
||||
* floats, strings and arrays into their respective nodes
|
||||
*
|
||||
* @param mixed $value The value to normalize
|
||||
*
|
||||
* @return PHPParser_Node_Expr The normalized value
|
||||
*/
|
||||
protected function normalizeValue($value) {
|
||||
if ($value instanceof PHPParser_Node) {
|
||||
return $value;
|
||||
} elseif (is_null($value)) {
|
||||
return new PHPParser_Node_Expr_ConstFetch(
|
||||
new PHPParser_Node_Name('null')
|
||||
);
|
||||
} elseif (is_bool($value)) {
|
||||
return new PHPParser_Node_Expr_ConstFetch(
|
||||
new PHPParser_Node_Name($value ? 'true' : 'false')
|
||||
);
|
||||
} elseif (is_int($value)) {
|
||||
return new PHPParser_Node_Scalar_LNumber($value);
|
||||
} elseif (is_float($value)) {
|
||||
return new PHPParser_Node_Scalar_DNumber($value);
|
||||
} elseif (is_string($value)) {
|
||||
return new PHPParser_Node_Scalar_String($value);
|
||||
} elseif (is_array($value)) {
|
||||
$items = array();
|
||||
$lastKey = -1;
|
||||
foreach ($value as $itemKey => $itemValue) {
|
||||
// for consecutive, numeric keys don't generate keys
|
||||
if (null !== $lastKey && ++$lastKey === $itemKey) {
|
||||
$items[] = new PHPParser_Node_Expr_ArrayItem(
|
||||
$this->normalizeValue($itemValue)
|
||||
);
|
||||
} else {
|
||||
$lastKey = null;
|
||||
$items[] = new PHPParser_Node_Expr_ArrayItem(
|
||||
$this->normalizeValue($itemValue),
|
||||
$this->normalizeValue($itemKey)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new PHPParser_Node_Expr_Array($items);
|
||||
} else {
|
||||
throw new LogicException('Invalid value');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a modifier in the $this->type property.
|
||||
*
|
||||
* @param int $modifier Modifier to set
|
||||
*/
|
||||
protected function setModifier($modifier) {
|
||||
PHPParser_Node_Stmt_Class::verifyModifier($this->type, $modifier);
|
||||
$this->type |= $modifier;
|
||||
}
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* "class", "interface" and "function" are reserved keywords, so the methods are defined as _class(),
|
||||
* _interface() and _function() in the class and are made available as class(), interface() and function()
|
||||
* through __call() magic.
|
||||
*
|
||||
* @method PHPParser_Builder_Class class(string $name) Creates a class builder.
|
||||
* @method PHPParser_Builder_Function function(string $name) Creates a function builder
|
||||
* @method PHPParser_Builder_Interface interface(string $name) Creates an interface builder.
|
||||
*/
|
||||
class PHPParser_BuilderFactory
|
||||
{
|
||||
/**
|
||||
* Creates a class builder.
|
||||
*
|
||||
* @param string $name Name of the class
|
||||
*
|
||||
* @return PHPParser_Builder_Class The created class builder
|
||||
*/
|
||||
protected function _class($name) {
|
||||
return new PHPParser_Builder_Class($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a interface builder.
|
||||
*
|
||||
* @param string $name Name of the interface
|
||||
*
|
||||
* @return PHPParser_Builder_Class The created interface builder
|
||||
*/
|
||||
protected function _interface($name) {
|
||||
return new PHPParser_Builder_Interface($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a method builder.
|
||||
*
|
||||
* @param string $name Name of the method
|
||||
*
|
||||
* @return PHPParser_Builder_Method The created method builder
|
||||
*/
|
||||
public function method($name) {
|
||||
return new PHPParser_Builder_Method($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parameter builder.
|
||||
*
|
||||
* @param string $name Name of the parameter
|
||||
*
|
||||
* @return PHPParser_Builder_Param The created parameter builder
|
||||
*/
|
||||
public function param($name) {
|
||||
return new PHPParser_Builder_Param($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a property builder.
|
||||
*
|
||||
* @param string $name Name of the property
|
||||
*
|
||||
* @return PHPParser_Builder_Property The created property builder
|
||||
*/
|
||||
public function property($name) {
|
||||
return new PHPParser_Builder_Property($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function builder.
|
||||
*
|
||||
* @param string $name Name of the function
|
||||
*
|
||||
* @return PHPParser_Builder_Property The created function builder
|
||||
*/
|
||||
protected function _function($name) {
|
||||
return new PHPParser_Builder_Function($name);
|
||||
}
|
||||
|
||||
public function __call($name, array $args) {
|
||||
if (method_exists($this, '_' . $name)) {
|
||||
return call_user_func_array(array($this, '_' . $name), $args);
|
||||
}
|
||||
|
||||
throw new LogicException(sprintf('Method "%s" does not exist', $name));
|
||||
}
|
||||
}
|
@ -1,117 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Comment
|
||||
{
|
||||
protected $text;
|
||||
protected $line;
|
||||
|
||||
/**
|
||||
* Constructs a comment node.
|
||||
*
|
||||
* @param string $text Comment text (including comment delimiters like /*)
|
||||
* @param int $line Line number the comment started on
|
||||
*/
|
||||
public function __construct($text, $line = -1) {
|
||||
$this->text = $text;
|
||||
$this->line = $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the comment text.
|
||||
*
|
||||
* @return string The comment text (including comment delimiters like /*)
|
||||
*/
|
||||
public function getText() {
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the comment text.
|
||||
*
|
||||
* @param string $text The comment text (including comment delimiters like /*)
|
||||
*/
|
||||
public function setText($text) {
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the line number the comment started on.
|
||||
*
|
||||
* @return int Line number
|
||||
*/
|
||||
public function getLine() {
|
||||
return $this->line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the line number the comment started on.
|
||||
*
|
||||
* @param int $line Line number
|
||||
*/
|
||||
public function setLine($line) {
|
||||
$this->line = $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the comment text.
|
||||
*
|
||||
* @return string The comment text (including comment delimiters like /*)
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reformatted comment text.
|
||||
*
|
||||
* "Reformatted" here means that we try to clean up the whitespace at the
|
||||
* starts of the lines. This is necessary because we receive the comments
|
||||
* without trailing whitespace on the first line, but with trailing whitespace
|
||||
* on all subsequent lines.
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getReformattedText() {
|
||||
$text = trim($this->text);
|
||||
if (false === strpos($text, "\n")) {
|
||||
// Single line comments don't need further processing
|
||||
return $text;
|
||||
} elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
|
||||
// Multi line comment of the type
|
||||
//
|
||||
// /*
|
||||
// * Some text.
|
||||
// * Some more text.
|
||||
// */
|
||||
//
|
||||
// is handled by replacing the whitespace sequences before the * by a single space
|
||||
return preg_replace('(^\s+\*)m', ' *', $this->text);
|
||||
} elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
|
||||
// Multi line comment of the type
|
||||
//
|
||||
// /*
|
||||
// Some text.
|
||||
// Some more text.
|
||||
// */
|
||||
//
|
||||
// is handled by removing the whitespace sequence on the line before the closing
|
||||
// */ on all lines. So if the last line is " */", then " " is removed at the
|
||||
// start of all lines.
|
||||
return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
|
||||
} elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
|
||||
// Multi line comment of the type
|
||||
//
|
||||
// /* Some text.
|
||||
// Some more text.
|
||||
// Even more text. */
|
||||
//
|
||||
// is handled by taking the length of the "/* " segment and leaving only that
|
||||
// many space characters before the lines. Thus in the above example only three
|
||||
// space characters are left at the start of every line.
|
||||
return preg_replace('(^\s*(?= {' . strlen($matches[0]) . '}(?!\s)))m', '', $text);
|
||||
}
|
||||
|
||||
// No idea how to format this comment, so simply return as is
|
||||
return $text;
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Comment_Doc extends PHPParser_Comment
|
||||
{
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Error extends RuntimeException
|
||||
{
|
||||
protected $rawMessage;
|
||||
protected $rawLine;
|
||||
|
||||
/**
|
||||
* Creates an Exception signifying a parse error.
|
||||
*
|
||||
* @param string $message Error message
|
||||
* @param int $line Error line in PHP file
|
||||
*/
|
||||
public function __construct($message, $line = -1) {
|
||||
$this->rawMessage = (string) $message;
|
||||
$this->rawLine = (int) $line;
|
||||
$this->updateMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error message
|
||||
*
|
||||
* @return string Error message
|
||||
*/
|
||||
public function getRawMessage() {
|
||||
return $this->rawMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the line of the PHP file the error occurred in.
|
||||
*
|
||||
* @param string $message Error message
|
||||
*/
|
||||
public function setRawMessage($message) {
|
||||
$this->rawMessage = (string) $message;
|
||||
$this->updateMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the error line in the PHP file.
|
||||
*
|
||||
* @return int Error line in the PHP file
|
||||
*/
|
||||
public function getRawLine() {
|
||||
return $this->rawLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the line of the PHP file the error occurred in.
|
||||
*
|
||||
* @param int $line Error line in the PHP file
|
||||
*/
|
||||
public function setRawLine($line) {
|
||||
$this->rawLine = (int) $line;
|
||||
$this->updateMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the exception message after a change to rawMessage or rawLine.
|
||||
*/
|
||||
protected function updateMessage() {
|
||||
$this->message = $this->rawMessage;
|
||||
|
||||
if (-1 === $this->rawLine) {
|
||||
$this->message .= ' on unknown line';
|
||||
} else {
|
||||
$this->message .= ' on line ' . $this->rawLine;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,191 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Lexer
|
||||
{
|
||||
protected $code;
|
||||
protected $tokens;
|
||||
protected $pos;
|
||||
protected $line;
|
||||
|
||||
protected $tokenMap;
|
||||
protected $dropTokens;
|
||||
|
||||
/**
|
||||
* Creates a Lexer.
|
||||
*/
|
||||
public function __construct() {
|
||||
// map from internal tokens to PHPParser tokens
|
||||
$this->tokenMap = $this->createTokenMap();
|
||||
|
||||
// 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.)
|
||||
$this->dropTokens = array_fill_keys(array(T_WHITESPACE, T_OPEN_TAG), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the lexer for lexing the provided source code.
|
||||
*
|
||||
* @param string $code The source code to lex
|
||||
*
|
||||
* @throws PHPParser_Error on lexing errors (unterminated comment or unexpected character)
|
||||
*/
|
||||
public function startLexing($code) {
|
||||
$this->resetErrors();
|
||||
$this->tokens = @token_get_all($code);
|
||||
$this->handleErrors();
|
||||
|
||||
$this->code = $code; // keep the code around for __halt_compiler() handling
|
||||
$this->pos = -1;
|
||||
$this->line = 1;
|
||||
}
|
||||
|
||||
protected function resetErrors() {
|
||||
// clear error_get_last() by forcing an undefined variable error
|
||||
@$undefinedVariable;
|
||||
}
|
||||
|
||||
protected function handleErrors() {
|
||||
$error = error_get_last();
|
||||
|
||||
if (preg_match(
|
||||
'~^Unterminated comment starting line ([0-9]+)$~',
|
||||
$error['message'], $matches
|
||||
)) {
|
||||
throw new PHPParser_Error('Unterminated comment', $matches[1]);
|
||||
}
|
||||
|
||||
if (preg_match(
|
||||
'~^Unexpected character in input: \'(.)\' \(ASCII=([0-9]+)\)~s',
|
||||
$error['message'], $matches
|
||||
)) {
|
||||
throw new PHPParser_Error(sprintf(
|
||||
'Unexpected character "%s" (ASCII %d)',
|
||||
$matches[1], $matches[2]
|
||||
));
|
||||
}
|
||||
|
||||
// PHP cuts error message after null byte, so need special case
|
||||
if (preg_match('~^Unexpected character in input: \'$~', $error['message'])) {
|
||||
throw new PHPParser_Error('Unexpected null byte');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the next token.
|
||||
*
|
||||
* @param mixed $value Variable to store token content in
|
||||
* @param mixed $startAttributes Variable to store start attributes in
|
||||
* @param mixed $endAttributes Variable to store end attributes in
|
||||
*
|
||||
* @return int Token id
|
||||
*/
|
||||
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
||||
$startAttributes = array();
|
||||
$endAttributes = array();
|
||||
|
||||
while (isset($this->tokens[++$this->pos])) {
|
||||
$token = $this->tokens[$this->pos];
|
||||
|
||||
if (is_string($token)) {
|
||||
$startAttributes['startLine'] = $this->line;
|
||||
$endAttributes['endLine'] = $this->line;
|
||||
|
||||
// bug in token_get_all
|
||||
if ('b"' === $token) {
|
||||
$value = 'b"';
|
||||
return ord('"');
|
||||
} else {
|
||||
$value = $token;
|
||||
return ord($token);
|
||||
}
|
||||
} else {
|
||||
$this->line += substr_count($token[1], "\n");
|
||||
|
||||
if (T_COMMENT === $token[0]) {
|
||||
$startAttributes['comments'][] = new PHPParser_Comment($token[1], $token[2]);
|
||||
} elseif (T_DOC_COMMENT === $token[0]) {
|
||||
$startAttributes['comments'][] = new PHPParser_Comment_Doc($token[1], $token[2]);
|
||||
} elseif (!isset($this->dropTokens[$token[0]])) {
|
||||
$value = $token[1];
|
||||
$startAttributes['startLine'] = $token[2];
|
||||
$endAttributes['endLine'] = $this->line;
|
||||
|
||||
return $this->tokenMap[$token[0]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$startAttributes['startLine'] = $this->line;
|
||||
|
||||
// 0 is the EOF token
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles __halt_compiler() by returning the text after it.
|
||||
*
|
||||
* @return string Remaining text
|
||||
*/
|
||||
public function handleHaltCompiler() {
|
||||
// get the length of the text before the T_HALT_COMPILER token
|
||||
$textBefore = '';
|
||||
for ($i = 0; $i <= $this->pos; ++$i) {
|
||||
if (is_string($this->tokens[$i])) {
|
||||
$textBefore .= $this->tokens[$i];
|
||||
} else {
|
||||
$textBefore .= $this->tokens[$i][1];
|
||||
}
|
||||
}
|
||||
|
||||
// text after T_HALT_COMPILER, still including ();
|
||||
$textAfter = substr($this->code, strlen($textBefore));
|
||||
|
||||
// ensure that it is followed by ();
|
||||
// this simplifies the situation, by not allowing any comments
|
||||
// in between of the tokens.
|
||||
if (!preg_match('~\s*\(\s*\)\s*(?:;|\?>\r?\n?)~', $textAfter, $matches)) {
|
||||
throw new PHPParser_Error('__halt_compiler must be followed by "();"');
|
||||
}
|
||||
|
||||
// prevent the lexer from returning any further tokens
|
||||
$this->pos = count($this->tokens);
|
||||
|
||||
// return with (); removed
|
||||
return (string) substr($textAfter, strlen($matches[0])); // (string) converts false to ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
$tokenMap = array();
|
||||
|
||||
// 256 is the minimum possible token number, as everything below
|
||||
// it is an ASCII value
|
||||
for ($i = 256; $i < 1000; ++$i) {
|
||||
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
|
||||
if (T_DOUBLE_COLON === $i) {
|
||||
$tokenMap[$i] = PHPParser_Parser::T_PAAMAYIM_NEKUDOTAYIM;
|
||||
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
|
||||
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
|
||||
$tokenMap[$i] = PHPParser_Parser::T_ECHO;
|
||||
// T_CLOSE_TAG is equivalent to ';'
|
||||
} elseif(T_CLOSE_TAG === $i) {
|
||||
$tokenMap[$i] = ord(';');
|
||||
// and the others can be mapped directly
|
||||
} elseif ('UNKNOWN' !== ($name = token_name($i))
|
||||
&& defined($name = 'PHPParser_Parser::' . $name)
|
||||
) {
|
||||
$tokenMap[$i] = constant($name);
|
||||
}
|
||||
}
|
||||
|
||||
return $tokenMap;
|
||||
}
|
||||
}
|
@ -1,200 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ATTENTION: This code is WRITE-ONLY. Do not try to read it.
|
||||
*/
|
||||
class PHPParser_Lexer_Emulative extends PHPParser_Lexer
|
||||
{
|
||||
protected $newKeywords;
|
||||
protected $inObjectAccess;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$newKeywordsPerVersion = array(
|
||||
'5.5.0-dev' => array(
|
||||
'finally' => PHPParser_Parser::T_FINALLY,
|
||||
'yield' => PHPParser_Parser::T_YIELD,
|
||||
),
|
||||
'5.4.0-dev' => array(
|
||||
'callable' => PHPParser_Parser::T_CALLABLE,
|
||||
'insteadof' => PHPParser_Parser::T_INSTEADOF,
|
||||
'trait' => PHPParser_Parser::T_TRAIT,
|
||||
'__trait__' => PHPParser_Parser::T_TRAIT_C,
|
||||
),
|
||||
'5.3.0-dev' => array(
|
||||
'__dir__' => PHPParser_Parser::T_DIR,
|
||||
'goto' => PHPParser_Parser::T_GOTO,
|
||||
'namespace' => PHPParser_Parser::T_NAMESPACE,
|
||||
'__namespace__' => PHPParser_Parser::T_NS_C,
|
||||
),
|
||||
);
|
||||
|
||||
$this->newKeywords = array();
|
||||
foreach ($newKeywordsPerVersion as $version => $newKeywords) {
|
||||
if (version_compare(PHP_VERSION, $version, '>=')) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->newKeywords += $newKeywords;
|
||||
}
|
||||
}
|
||||
|
||||
public function startLexing($code) {
|
||||
$this->inObjectAccess = false;
|
||||
|
||||
// on PHP 5.4 don't do anything
|
||||
if (version_compare(PHP_VERSION, '5.4.0RC1', '>=')) {
|
||||
parent::startLexing($code);
|
||||
} else {
|
||||
$code = $this->preprocessCode($code);
|
||||
parent::startLexing($code);
|
||||
$this->postprocessTokens();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Replaces new features in the code by ~__EMU__{NAME}__{DATA}__~ sequences.
|
||||
* ~LABEL~ is never valid PHP code, that's why we can (to some degree) safely
|
||||
* use it here.
|
||||
* Later when preprocessing the tokens these sequences will either be replaced
|
||||
* by real tokens or replaced with their original content (e.g. if they occured
|
||||
* inside a string, i.e. a place where they don't have a special meaning).
|
||||
*/
|
||||
protected function preprocessCode($code) {
|
||||
// binary notation (0b010101101001...)
|
||||
$code = preg_replace('(\b0b[01]+\b)', '~__EMU__BINARY__$0__~', $code);
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
|
||||
// namespace separator (backslash not followed by some special characters,
|
||||
// which are not valid after a NS separator, but would cause problems with
|
||||
// escape sequence parsing if one would replace the backslash there)
|
||||
$code = preg_replace('(\\\\(?!["\'`${\\\\]))', '~__EMU__NS__~', $code);
|
||||
|
||||
// nowdoc (<<<'ABC'\ncontent\nABC;)
|
||||
$code = preg_replace_callback(
|
||||
'((*BSR_ANYCRLF) # set \R to (?>\r\n|\r|\n)
|
||||
(b?<<<[\t ]*\'([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\'\R) # opening token
|
||||
((?:(?!\2;?\R).*\R)*) # content
|
||||
(\2) # closing token
|
||||
(?=;?\R) # must be followed by newline (with optional semicolon)
|
||||
)x',
|
||||
array($this, 'encodeNowdocCallback'),
|
||||
$code
|
||||
);
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/*
|
||||
* As nowdocs can have arbitrary content but LABELs can only contain a certain
|
||||
* range of characters, the nowdoc content is encoded as hex and separated by
|
||||
* 'x' tokens. So the result of the encoding will look like this:
|
||||
* ~__EMU__NOWDOC__{HEX(START_TOKEN)}x{HEX(CONTENT)}x{HEX(END_TOKEN)}~
|
||||
*/
|
||||
public function encodeNowdocCallback(array $matches) {
|
||||
return '~__EMU__NOWDOC__'
|
||||
. bin2hex($matches[1]) . 'x' . bin2hex($matches[3]) . 'x' . bin2hex($matches[4])
|
||||
. '__~';
|
||||
}
|
||||
|
||||
/*
|
||||
* Replaces the ~__EMU__...~ sequences with real tokens or their original
|
||||
* value.
|
||||
*/
|
||||
protected function postprocessTokens() {
|
||||
// we need to manually iterate and manage a count because we'll change
|
||||
// the tokens array on the way
|
||||
for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
|
||||
// first check that the following tokens are form ~LABEL~,
|
||||
// then match the __EMU__... sequence.
|
||||
if ('~' === $this->tokens[$i]
|
||||
&& isset($this->tokens[$i + 2])
|
||||
&& '~' === $this->tokens[$i + 2]
|
||||
&& T_STRING === $this->tokens[$i + 1][0]
|
||||
&& preg_match('(^__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?$)', $this->tokens[$i + 1][1], $matches)
|
||||
) {
|
||||
if ('BINARY' === $matches[1]) {
|
||||
// the binary number can either be an integer or a double, so return a LNUMBER
|
||||
// or DNUMBER respectively
|
||||
$replace = array(
|
||||
array(is_int(bindec($matches[2])) ? T_LNUMBER : T_DNUMBER, $matches[2], $this->tokens[$i + 1][2])
|
||||
);
|
||||
} elseif ('NS' === $matches[1]) {
|
||||
// a \ single char token is returned here and replaced by a
|
||||
// PHPParser_Parser::T_NS_SEPARATOR token in ->getNextToken(). This hacks around
|
||||
// the limitations arising from T_NS_SEPARATOR not being defined on 5.3
|
||||
$replace = array('\\');
|
||||
} elseif ('NOWDOC' === $matches[1]) {
|
||||
// decode the encoded nowdoc payload; pack('H*' is bin2hex( for 5.3
|
||||
list($start, $content, $end) = explode('x', $matches[2]);
|
||||
list($start, $content, $end) = array(pack('H*', $start), pack('H*', $content), pack('H*', $end));
|
||||
|
||||
$replace = array();
|
||||
$replace[] = array(T_START_HEREDOC, $start, $this->tokens[$i + 1][2]);
|
||||
if ('' !== $content) {
|
||||
$replace[] = array(T_ENCAPSED_AND_WHITESPACE, $content, -1);
|
||||
}
|
||||
$replace[] = array(T_END_HEREDOC, $end, -1);
|
||||
} else {
|
||||
// just ignore all other __EMU__ sequences
|
||||
continue;
|
||||
}
|
||||
|
||||
array_splice($this->tokens, $i, 3, $replace);
|
||||
$c -= 3 - count($replace);
|
||||
// for multichar tokens (e.g. strings) replace any ~__EMU__...~ sequences
|
||||
// in their content with the original character sequence
|
||||
} elseif (is_array($this->tokens[$i])
|
||||
&& 0 !== strpos($this->tokens[$i][1], '__EMU__')
|
||||
) {
|
||||
$this->tokens[$i][1] = preg_replace_callback(
|
||||
'(~__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?~)',
|
||||
array($this, 'restoreContentCallback'),
|
||||
$this->tokens[$i][1]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method is a callback for restoring EMU sequences in
|
||||
* multichar tokens (like strings) to their original value.
|
||||
*/
|
||||
public function restoreContentCallback(array $matches) {
|
||||
if ('BINARY' === $matches[1]) {
|
||||
return $matches[2];
|
||||
} elseif ('NS' === $matches[1]) {
|
||||
return '\\';
|
||||
} elseif ('NOWDOC' === $matches[1]) {
|
||||
list($start, $content, $end) = explode('x', $matches[2]);
|
||||
return pack('H*', $start) . pack('H*', $content) . pack('H*', $end);
|
||||
} else {
|
||||
return $matches[0];
|
||||
}
|
||||
}
|
||||
|
||||
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
||||
$token = parent::getNextToken($value, $startAttributes, $endAttributes);
|
||||
|
||||
// replace new keywords by their respective tokens. This is not done
|
||||
// if we currently are in an object access (e.g. in $obj->namespace
|
||||
// "namespace" stays a T_STRING tokens and isn't converted to T_NAMESPACE)
|
||||
if (PHPParser_Parser::T_STRING === $token && !$this->inObjectAccess) {
|
||||
if (isset($this->newKeywords[strtolower($value)])) {
|
||||
return $this->newKeywords[strtolower($value)];
|
||||
}
|
||||
// backslashes are replaced by T_NS_SEPARATOR tokens
|
||||
} elseif (92 === $token) { // ord('\\')
|
||||
return PHPParser_Parser::T_NS_SEPARATOR;
|
||||
// keep track of whether we currently are in an object access (after ->)
|
||||
} elseif (PHPParser_Parser::T_OBJECT_OPERATOR === $token) {
|
||||
$this->inObjectAccess = true;
|
||||
} else {
|
||||
$this->inObjectAccess = false;
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
interface PHPParser_Node
|
||||
{
|
||||
/**
|
||||
* Gets the type of the node.
|
||||
*
|
||||
* @return string Type of the node
|
||||
*/
|
||||
public function getType();
|
||||
|
||||
/**
|
||||
* Gets the names of the sub nodes.
|
||||
*
|
||||
* @return array Names of sub nodes
|
||||
*/
|
||||
public function getSubNodeNames();
|
||||
|
||||
/**
|
||||
* Gets line the node started in.
|
||||
*
|
||||
* @return int Line
|
||||
*/
|
||||
public function getLine();
|
||||
|
||||
/**
|
||||
* Sets line the node started in.
|
||||
*
|
||||
* @param int $line Line
|
||||
*/
|
||||
public function setLine($line);
|
||||
|
||||
/**
|
||||
* Gets the doc comment of the node.
|
||||
*
|
||||
* The doc comment has to be the last comment associated with the node.
|
||||
*
|
||||
* @return null|PHPParser_Comment_Doc Doc comment object or null
|
||||
*/
|
||||
public function getDocComment();
|
||||
|
||||
/**
|
||||
* Sets an attribute on a node.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setAttribute($key, $value);
|
||||
|
||||
/**
|
||||
* Returns whether an attribute exists.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAttribute($key);
|
||||
|
||||
/**
|
||||
* Returns the value of an attribute.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function &getAttribute($key, $default = null);
|
||||
|
||||
/**
|
||||
* Returns all attributes for the given node.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributes();
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $value Value to pass
|
||||
* @property bool $byRef Whether to pass by ref
|
||||
*/
|
||||
class PHPParser_Node_Arg extends PHPParser_NodeAbstract
|
||||
{
|
||||
/**
|
||||
* Constructs a function call argument node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $value Value to pass
|
||||
* @param bool $byRef Whether to pass by ref
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $value, $byRef = false, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'value' => $value,
|
||||
'byRef' => $byRef
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property string $name Name
|
||||
* @property PHPParser_Node_Expr $value Value
|
||||
*/
|
||||
class PHPParser_Node_Const extends PHPParser_NodeAbstract
|
||||
{
|
||||
/**
|
||||
* Constructs a const node for use in class const and const statements.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param PHPParser_Node_Expr $value Value
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct($name, PHPParser_Node_Expr $value, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
abstract class PHPParser_Node_Expr extends PHPParser_NodeAbstract
|
||||
{
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr_ArrayItem[] $items Items
|
||||
*/
|
||||
class PHPParser_Node_Expr_Array extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an array node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr_ArrayItem[] $items Items of the array
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(array $items = array(), array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'items' => $items
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property null|PHPParser_Node_Expr $dim Array index / dim
|
||||
*/
|
||||
class PHPParser_Node_Expr_ArrayDimFetch extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an array index fetch node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param null|PHPParser_Node_Expr $dim Array index / dim
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $dim = null, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'dim' => $dim
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $value Value
|
||||
* @property null|PHPParser_Node_Expr $key Key
|
||||
* @property bool $byRef Whether to assign by reference
|
||||
*/
|
||||
class PHPParser_Node_Expr_ArrayItem extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an array item node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $value Value
|
||||
* @param null|PHPParser_Node_Expr $key Key
|
||||
* @param bool $byRef Whether to assign by reference
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $value, PHPParser_Node_Expr $key = null, $byRef = false, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
'byRef' => $byRef
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_Assign extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignBitwiseAnd extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with bitwise and node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignBitwiseOr extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with bitwise or node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignBitwiseXor extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with bitwise xor node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignConcat extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with concat node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignDiv extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with division node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignMinus extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with minus node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignMod extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with modulo node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignMul extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with multiplication node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignPlus extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with addition node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable reference is assigned to
|
||||
* @property PHPParser_Node_Expr $expr Variable which is referenced
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignRef extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignShiftLeft extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with left shift node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignShiftRight extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an assignment with right shift node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BitwiseAnd extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a bitwise and node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BitwiseNot extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a bitwise not node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BitwiseOr extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a bitwise or node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BitwiseXor extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a bitwise xor node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BooleanAnd extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a boolean and node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BooleanNot extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a boolean not node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_BooleanOr extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a boolean or node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
abstract class PHPParser_Node_Expr_Cast extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a cast node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_Array extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_Bool extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_Double extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_Int extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_Object extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_String extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Node_Expr_Cast_Unset extends PHPParser_Node_Expr_Cast
|
||||
{
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Name|PHPParser_Node_Expr $class Class name
|
||||
* @property string $name Constant name
|
||||
*/
|
||||
class PHPParser_Node_Expr_ClassConstFetch extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a class const fetch node.
|
||||
*
|
||||
* @param PHPParser_Node_Name|PHPParser_Node_Expr $class Class name
|
||||
* @param string $name Constant name
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct($class, $name, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'class' => $class,
|
||||
'name' => $name
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_Clone extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a clone node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'expr' => $expr
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node[] $stmts Statements
|
||||
* @property PHPParser_Node_Param[] $params Parameters
|
||||
* @property PHPParser_Node_Expr_ClosureUse[] $uses use()s
|
||||
* @property bool $byRef Whether to return by reference
|
||||
* @property bool $static Whether the closure is static
|
||||
*/
|
||||
class PHPParser_Node_Expr_Closure extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a lambda function node.
|
||||
*
|
||||
* @param array $subNodes Array of the following optional subnodes:
|
||||
* 'stmts' => array(): Statements
|
||||
* 'params' => array(): Parameters
|
||||
* 'uses' => array(): use()s
|
||||
* 'byRef' => false : Whether to return by reference
|
||||
* 'static' => false : Whether the closure is static
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(array $subNodes = array(), array $attributes = array()) {
|
||||
parent::__construct(
|
||||
$subNodes + array(
|
||||
'stmts' => array(),
|
||||
'params' => array(),
|
||||
'uses' => array(),
|
||||
'byRef' => false,
|
||||
'static' => false,
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property string $var Name of variable
|
||||
* @property bool $byRef Whether to use by reference
|
||||
*/
|
||||
class PHPParser_Node_Expr_ClosureUse extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a closure use node.
|
||||
*
|
||||
* @param string $var Name of variable
|
||||
* @param bool $byRef Whether to use by reference
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct($var, $byRef = false, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var,
|
||||
'byRef' => $byRef
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_Concat extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a concat node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Name $name Constant name
|
||||
*/
|
||||
class PHPParser_Node_Expr_ConstFetch extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a const fetch node.
|
||||
*
|
||||
* @param PHPParser_Node_Name $name Constant name
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Name $name, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'name' => $name
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $left The left hand side expression
|
||||
* @property PHPParser_Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_Div extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a division node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $left The left hand side expression
|
||||
* @param PHPParser_Node_Expr $right The right hand side expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $left, PHPParser_Node_Expr $right, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'left' => $left,
|
||||
'right' => $right
|
||||
),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user