mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-06-21 15:23:33 +02:00
Compare commits
1017 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
1e5e280ae8 | |||
3d467ca18e | |||
c8695a8f56 | |||
01123ae6af | |||
09c106d11f | |||
77c08a75c9 | |||
f9c3aa2a22 | |||
5ccf6196d6 | |||
12faad529e | |||
c0da1b88b2 | |||
bc9ab604f6 | |||
900a3f3b7c | |||
92df3e5add | |||
ba91348142 | |||
8e686ce7a7 | |||
08f0cde6f9 | |||
5fca55702b | |||
dc95f3b425 | |||
75ec7a3e78 | |||
a249c002dd | |||
81d20bf10e | |||
db18906dfc | |||
fbaa1e5fc3 | |||
222c9612ab | |||
eeb5e899a5 | |||
01c5b84db5 | |||
98ebfc8d54 | |||
9f0e12bfca | |||
cdbad02fb2 | |||
b0c8787406 | |||
2ae2410dbd | |||
bdb58ada7c | |||
efa872692e | |||
fc56da59ce | |||
df17d62b40 | |||
ac6f221c50 | |||
759c04db9b | |||
9e43acee2c | |||
9d8e13b4a9 | |||
af5d288fb3 | |||
f6c1ab6657 | |||
4259b44a84 | |||
417a8bb07e | |||
ae3774f0f2 | |||
f8f1e17e41 | |||
8d218110db | |||
a590937fdf | |||
84b23a3eb5 | |||
5a947e9843 |
9
.gitattributes
vendored
Normal file
9
.gitattributes
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
/doc export-ignore
|
||||
/test export-ignore
|
||||
/test_old export-ignore
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
.travis.yml export-ignore
|
||||
CHANGELOG.md export-ignore
|
||||
phpunit.xml.dist export-ignore
|
||||
UPGRADE-*.md export-ignore
|
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
vendor/
|
||||
composer.lock
|
||||
grammar/kmyacc.exe
|
||||
grammar/y.output
|
||||
.phpunit.result.cache
|
45
.travis.yml
45
.travis.yml
@ -1,6 +1,43 @@
|
||||
language: php
|
||||
dist: xenial
|
||||
|
||||
php:
|
||||
- 5.2
|
||||
- 5.3
|
||||
- 5.4
|
||||
before_install: composer self-update --2
|
||||
|
||||
install: composer update --no-progress --prefer-dist
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- name: PHP 7.0 Unit Tests
|
||||
php: 7.0
|
||||
install:
|
||||
- composer require php-coveralls/php-coveralls:^2.2 --dev --no-update
|
||||
- composer update --no-progress --prefer-dist
|
||||
script: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
|
||||
after_success: php vendor/bin/coveralls
|
||||
- name: PHP 7.1 Unit Tests
|
||||
php: 7.1
|
||||
- name: PHP 7.2 Unit Tests
|
||||
php: 7.2
|
||||
- name: PHP 7.3 Unit Tests
|
||||
php: 7.3
|
||||
- name: PHP 7.4 Unit Tests
|
||||
php: 7.4
|
||||
- name: PHP 8.0 Unit Tests
|
||||
php: nightly
|
||||
install:
|
||||
- composer update --ignore-platform-req=php --no-progress --prefer-dist
|
||||
- name: PHP 7.3 Code on PHP 8.0 Integration Tests
|
||||
php: nightly
|
||||
install:
|
||||
- composer update --ignore-platform-req=php --no-progress --prefer-dist
|
||||
script:
|
||||
- test_old/run-php-src.sh 7.3.21
|
||||
- name: PHP 8.0 Code on PHP 7.0 Integration Tests
|
||||
php: 7.0
|
||||
script:
|
||||
- test_old/run-php-src.sh 8.0.0beta1
|
||||
allow_failures:
|
||||
- name: PHP 8.0 Code on PHP 7.0 Integration Tests
|
||||
fast_finish: true
|
||||
|
||||
script: vendor/bin/phpunit
|
||||
|
859
CHANGELOG.md
859
CHANGELOG.md
@ -1,82 +1,803 @@
|
||||
Version 0.9.3-dev
|
||||
Version 4.9.2-dev
|
||||
-----------------
|
||||
|
||||
Nothing yet.
|
||||
|
||||
Version 0.9.2 (07.07.2012)
|
||||
--------------------------
|
||||
* Add `Class->getMethods()` function, which returns all methods contained in the `stmts` array of the class node. This
|
||||
does not take inherited methods into account.
|
||||
|
||||
* Add `isPublic()`, `isProtected()`, `isPrivate()`. `isAbstract()`, `isFinal()` and `isStatic()` accessors to the
|
||||
`ClassMethod`, `Property` and `Class` nodes. (`Property` and `Class` obviously only have the accessors relevant to
|
||||
them.)
|
||||
|
||||
* Fix parsing of new expressions in parentheses, e.g. `return(new Foo);`.
|
||||
|
||||
* [BC] Due to the below changes nodes now optionally accept an `$attributes` array as the
|
||||
last parameter, instead of the previously used `$line` and `$docComment` parameters.
|
||||
|
||||
* Add mechanism for adding attributes to nodes in the lexer.
|
||||
|
||||
The following attributes are now added by default:
|
||||
|
||||
* `startLine`: The line the node started in.
|
||||
* `endLine`: The line the node ended in.
|
||||
* `comments`: An array of comments. The comments are instances of `PHPParser_Comment`
|
||||
(or `PHPParser_Comment_Doc` for doc comments).
|
||||
|
||||
The methods `getLine()` and `setLine()` still exist and function as before, but internally
|
||||
operator on the `startLine` attribute.
|
||||
|
||||
`getDocComment()` also continues to exist. It returns the last comment in the `comments`
|
||||
attribute if it is a doc comment, otherwise `null`. As `getDocComment()` now returns a
|
||||
comment object (which can be modified using `->setText()`) the `setDocComment()` method was
|
||||
removed. Comment objects implement a `__toString()` method, so `getDocComment()` should
|
||||
continue to work properly with old code.
|
||||
|
||||
* [BC] Use inject-once approach for lexer:
|
||||
|
||||
Now the lexer is injected only once when creating the parser. Instead of
|
||||
|
||||
$parser = new PHPParser_Parser;
|
||||
$parser->parse(new PHPParser_Lexer($code));
|
||||
$parser->parse(new PHPParser_Lexer($code2));
|
||||
|
||||
you write:
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer);
|
||||
$parser->parse($code);
|
||||
$parser->parse($code2);
|
||||
|
||||
* Fix `NameResolver` visitor to also resolve class names in `catch` blocks.
|
||||
|
||||
Version 0.9.1 (24.04.2012)
|
||||
Version 4.9.1 (2020-08-30)
|
||||
--------------------------
|
||||
|
||||
* Add ability to add attributes to nodes:
|
||||
### Added
|
||||
|
||||
It is now possible to add attributes to a node using `$node->setAttribute('name', 'value')` and to retrieve them using
|
||||
`$node->getAttribute('name' [, 'default'])`. Additionally the existance of an attribute can be checked with
|
||||
`$node->hasAttribute('name')` and all attributes can be returned using `$node->getAttributes()`.
|
||||
* Added support for removing the first element of a list to the formatting-preserving pretty
|
||||
printer.
|
||||
|
||||
* Add code generation features: Builders and templates.
|
||||
### Fixed
|
||||
|
||||
For more infos, see the [code generation documentation][1].
|
||||
* Allow member modifiers as part of namespaced names. These were missed when support for other
|
||||
keywords was added.
|
||||
|
||||
* [BC] Don't traverse nodes merged by another visitor:
|
||||
|
||||
If a NodeVisitor returns an array of nodes to merge, these will no longer be traversed by all other visitors. This
|
||||
behavior only caused problems.
|
||||
|
||||
* Fix line numbers for some list structures.
|
||||
* Fix XML unserialization of empty nodes.
|
||||
* Fix parsing of integers that overflow into floats.
|
||||
* Fix emulation of NOWDOC and binary floats.
|
||||
|
||||
Version 0.9.0 (05.01.2012)
|
||||
Version 4.9.0 (2020-08-18)
|
||||
--------------------------
|
||||
|
||||
First version.
|
||||
### Added
|
||||
|
||||
[1]: https://github.com/nikic/PHP-Parser/blob/master/doc/3_Code_generation.markdown
|
||||
* [PHP 8.0] Added support for named arguments, represented using a new `name` subnode on `Arg`.
|
||||
* [PHP 8.0] Added support for static return type, represented like a normal class return type.
|
||||
* [PHP 8.0] Added support for throw expression, represented using a new `Expr\Throw_` node. For
|
||||
backwards compatibility reasons, throw expressions in statement context continue to be
|
||||
represented using `Stmt\Throw_`.
|
||||
* [PHP 8.0] Added support for keywords as parts of namespaced names.
|
||||
|
||||
### Fixed
|
||||
|
||||
* Emit parentheses for class constant fetch with complex left-hand-side.
|
||||
* Emit parentheses for new/instanceof on complex class expression.
|
||||
|
||||
Version 4.8.0 (2020-08-09)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 8.0] Added support for nullsafe operator, represented using the new
|
||||
`Expr\NullsafePropertyFetch` and `Expr\NullsafeMethodCall` nodes.
|
||||
* Added `phpVersion` option to the emulative lexer, which allows controlling the target version to
|
||||
emulate (defaults to the latest available, currently PHP 8.0). This is useful to parse code that
|
||||
uses reserved keywords from newer PHP versions as identifiers.
|
||||
|
||||
Version 4.7.0 (2020-07-25)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Add `ParentConnectingVisitor` and `NodeConnectingVisitor` classes.
|
||||
* [PHP 8.0] Added support for match expressions. These are represented using a new `Expr\Match_`
|
||||
containing `MatchArm`s.
|
||||
* [PHP 8.0] Added support for trailing comma in closure use lists.
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed missing error for unterminated comment with trailing newline (#688).
|
||||
* Compatibility with PHP 8.0 has been restored: Namespaced names are now always represented by
|
||||
`T_NAME_*` tokens, using emulationg on older PHP versions. Full support for reserved keywords
|
||||
in namespaced names is not yet present.
|
||||
|
||||
Version 4.6.0 (2020-07-02)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 8.0] Added support for trailing commas in parameter lists.
|
||||
* [PHP 8.0] Added support for constructor promotion. The parameter visibility is stored in
|
||||
`Node\Param::$flags`.
|
||||
|
||||
### Fixed
|
||||
|
||||
* Comment tokens now always follow the PHP 8 interpretation, and do not include trailing
|
||||
whitespace.
|
||||
* As a result of the previous change, some whitespace issues when inserting a statement into a
|
||||
method containing only a comment, and using the formatting-preserving pretty printer, have been
|
||||
resolved.
|
||||
|
||||
Version 4.5.0 (2020-06-03)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 8.0] Added support for the mixed type. This means `mixed` types are now parsed as an
|
||||
`Identifier` rather than a `Name`.
|
||||
* [PHP 8.0] Added support for catching without capturing the exception. This means that
|
||||
`Catch_::$var` may now be null.
|
||||
|
||||
Version 4.4.0 (2020-04-10)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for passing union types in builders.
|
||||
* Added end line, token position and file position information for comments.
|
||||
* Added `getProperty()` method to `ClassLike` nodes.
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed generation of invalid code when using the formatting preserving pretty printer, and
|
||||
inserting code next to certain nop statements. The formatting is still ugly though.
|
||||
* `getDocComment()` no longer requires that the very last comment before a node be a doc comment.
|
||||
There may not be non-doc comments between the doc comment and the declaration.
|
||||
* Allowed arbitrary expressions in `isset()` and `list()`, rather than just variables.
|
||||
In particular, this allows `isset(($x))`, which is legal PHP code.
|
||||
* [PHP 8.0] Add support for [variable syntax tweaks RFC](https://wiki.php.net/rfc/variable_syntax_tweaks).
|
||||
|
||||
Version 4.3.0 (2019-11-08)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 8.0] Added support for union types using a new `UnionType` node.
|
||||
|
||||
Version 4.2.5 (2019-10-25)
|
||||
--------------------------
|
||||
|
||||
### Changed
|
||||
|
||||
* Tests and documentation are no longer included in source archives. They can still be accessed
|
||||
by cloning the repository.
|
||||
* php-yacc is now used to generate the parser. This has no impact on users of the library.
|
||||
|
||||
Version 4.2.4 (2019-09-01)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Added getProperties(), getConstants() and getTraitUses() to ClassLike. (#629, #630)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed flexible heredoc emulation to check for digits after the end label. This synchronizes
|
||||
behavior with the upcoming PHP 7.3.10 release.
|
||||
|
||||
Version 4.2.3 (2019-08-12)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for numeric literal separators. (#615)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed resolution of return types for arrow functions. (#613)
|
||||
* Fixed compatibility with PHP 7.4.
|
||||
|
||||
Version 4.2.2 (2019-05-25)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for arrow functions using a new `Expr\ArrowFunction` node. (#602)
|
||||
* [PHP 7.4] Add support for array spreads, using a new `unpack` subnode on `ArrayItem`. (#609)
|
||||
* Added support for inserting into empty list nodes in the formatting preserving pretty printer.
|
||||
|
||||
### Changed
|
||||
|
||||
* `php-parse` will now print messages to stderr, so that stdout only contains the actual result of
|
||||
the operation (such as a JSON dump). (#605)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed attribute assignment for zero-length nop statements, and a related assertion failure in
|
||||
the formatting-preserving pretty printer. (#589)
|
||||
|
||||
Version 4.2.1 (2019-02-16)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for `??=` operator through a new `AssignOp\Coalesce` node. (#575)
|
||||
|
||||
Version 4.2.0 (2019-01-12)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.4] Add support for typed properties through a new `type` subnode of `Stmt\Property`.
|
||||
Additionally `Builder\Property` now has a `setType()` method. (#567)
|
||||
* Add `kind` attribute to `Cast\Double_`, which allows to distinguish between `(float)`,
|
||||
`(double)` and `(real)`. The form of the cast will be preserved by the pretty printer. (#565)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Remove assertion when pretty printing anonymous class with a name (#554).
|
||||
|
||||
Version 4.1.1 (2018-12-26)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fix "undefined offset" notice when parsing specific malformed code (#551).
|
||||
|
||||
### Added
|
||||
|
||||
* Support error recovery for missing return type (`function foo() : {}`) (#544).
|
||||
|
||||
Version 4.1.0 (2018-10-10)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for PHP 7.3 flexible heredoc/nowdoc strings, completing support for PHP 7.3. There
|
||||
are two caveats for this feature:
|
||||
* In some rare, pathological cases flexible heredoc/nowdoc strings change the interpretation of
|
||||
existing doc strings. PHP-Parser will now use the new interpretation.
|
||||
* Flexible heredoc/nowdoc strings require special support from the lexer. Because this is not
|
||||
available on PHP versions before 7.3, support has to be emulated. This emulation is not perfect
|
||||
and some cases which we do not expect to occur in practice (such as flexible doc strings being
|
||||
nested within each other through abuse of variable-variable interpolation syntax) may not be
|
||||
recognized correctly.
|
||||
* Added `DONT_TRAVERSE_CURRENT_AND_CHILDREN` to `NodeTraverser` to skip both traversal of child
|
||||
nodes, and prevent subsequent visitors from visiting the current node.
|
||||
|
||||
Version 4.0.4 (2018-09-18)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* The following methods have been added to `BuilderFactory`:
|
||||
* `useTrait()` (fluent builder)
|
||||
* `traitUseAdaptation()` (fluent builder)
|
||||
* `useFunction()` (fluent builder)
|
||||
* `useConst()` (fluent builder)
|
||||
* `var()`
|
||||
* `propertyFetch()`
|
||||
|
||||
### Deprecated
|
||||
|
||||
* `Builder\Param::setTypeHint()` has been deprecated in favor of the newly introduced
|
||||
`Builder\Param::setType()`.
|
||||
|
||||
Version 4.0.3 (2018-07-15)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed possible undefined offset notice in formatting-preserving printer. (#513)
|
||||
|
||||
### Added
|
||||
|
||||
* Improved error recovery inside arrays.
|
||||
* Preserve trailing comment inside classes. **Note:** This change is possibly BC breaking if your
|
||||
code validates that classes can only contain certain statement types. After this change, classes
|
||||
can also contain Nop statements, while this was not previously possible. (#509)
|
||||
|
||||
Version 4.0.2 (2018-06-03)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Improved error recovery inside classes.
|
||||
* Support error recovery for `foreach` without `as`.
|
||||
* Support error recovery for parameters without variable (`function (Type ) {}`).
|
||||
* Support error recovery for functions without body (`function ($foo)`).
|
||||
|
||||
Version 4.0.1 (2018-03-25)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.3] Added support for trailing commas in function calls.
|
||||
* [PHP 7.3] Added support for by-reference array destructuring.
|
||||
* Added checks to node traverser to prevent replacing a statement with an expression or vice versa.
|
||||
This should prevent common mistakes in the implementation of node visitors.
|
||||
* Added the following methods to `BuilderFactory`, to simplify creation of expressions:
|
||||
* `funcCall()`
|
||||
* `methodCall()`
|
||||
* `staticCall()`
|
||||
* `new()`
|
||||
* `constFetch()`
|
||||
* `classConstFetch()`
|
||||
|
||||
Version 4.0.0 (2018-02-28)
|
||||
--------------------------
|
||||
|
||||
* No significant code changes since the beta 1 release.
|
||||
|
||||
Version 4.0.0-beta1 (2018-01-27)
|
||||
--------------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* In formatting-preserving pretty printer: Fixed indentation when inserting into lists. (#466)
|
||||
|
||||
### Added
|
||||
|
||||
* In formatting-preserving pretty printer: Improved formatting of elements inserted into multi-line
|
||||
arrays.
|
||||
|
||||
### Removed
|
||||
|
||||
* The `Autoloader` class has been removed. It is now required to use the Composer autoloader.
|
||||
|
||||
Version 4.0.0-alpha3 (2017-12-26)
|
||||
---------------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* In the formatting-preserving pretty printer:
|
||||
* Fixed comment indentation.
|
||||
* Fixed handling of inline HTML in the fallback case.
|
||||
* Fixed insertion into list nodes that require creation of a code block.
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for inserting at the start of list nodes in formatting-preserving pretty printer.
|
||||
|
||||
Version 4.0.0-alpha2 (2017-11-10)
|
||||
---------------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* In the formatting-preserving pretty printer:
|
||||
* Added support for changing modifiers.
|
||||
* Added support for anonymous classes.
|
||||
* Added support for removing from list nodes.
|
||||
* Improved support for changing comments.
|
||||
* Added start token offsets to comments.
|
||||
|
||||
Version 4.0.0-alpha1 (2017-10-18)
|
||||
---------------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Added experimental support for format-preserving pretty-printing. In this mode formatting will be
|
||||
preserved for parts of the code which have not been modified.
|
||||
* Added `replaceNodes` option to `NameResolver`, defaulting to true. If this option is disabled,
|
||||
resolved names will be added as `resolvedName` attributes, instead of replacing the original
|
||||
names.
|
||||
* Added `NodeFinder` class, which can be used to find nodes based on a callback or class name. This
|
||||
is a utility to avoid custom node visitor implementations for simple search operations.
|
||||
* Added `ClassMethod::isMagic()` method.
|
||||
* Added `BuilderFactory` methods: `val()` method for creating an AST for a simple value, `concat()`
|
||||
for creating concatenation trees, `args()` for preparing function arguments.
|
||||
* Added `NameContext` class, which encapsulates the `NameResolver` logic independently of the actual
|
||||
AST traversal. This facilitates use in other context, such as class names in doc comments.
|
||||
Additionally it provides an API for getting the shortest representation of a name.
|
||||
* Added `Node::setAttributes()` method.
|
||||
* Added `JsonDecoder`. This allows conversion JSON back into an AST.
|
||||
* Added `Name` methods `toLowerString()` and `isSpecialClassName()`.
|
||||
* Added `Identifier` and `VarLikeIdentifier` nodes, which are used in place of simple strings in
|
||||
many places.
|
||||
* Added `getComments()`, `getStartLine()`, `getEndLine()`, `getStartTokenPos()`, `getEndTokenPos()`,
|
||||
`getStartFilePos()` and `getEndFilePos()` methods to `Node`. These provide a more obvious access
|
||||
point for the already existing attributes of the same name.
|
||||
* Added `ConstExprEvaluator` to evaluate constant expressions to PHP values.
|
||||
* Added `Expr\BinaryOp::getOperatorSigil()`, returning `+` for `Expr\BinaryOp\Plus`, etc.
|
||||
|
||||
### Changed
|
||||
|
||||
* Many subnodes that previously held simple strings now use `Identifier` (or `VarLikeIdentifier`)
|
||||
nodes. Please see the UPGRADE-4.0 file for an exhaustive list of affected nodes and some notes on
|
||||
possible impact.
|
||||
* 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.
|
||||
|
||||
### Removed
|
||||
|
||||
* Support for running on PHP 5 and HHVM has been removed. You can however still parse code of old
|
||||
PHP versions (such as PHP 5.2), while running on PHP 7.
|
||||
* 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.
|
||||
|
||||
Version 3.1.5 (2018-02-28)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed duplicate comment assignment in switch statements. (#469)
|
||||
* Improve compatibility with PHP-Scoper. (#477)
|
||||
|
||||
Version 3.1.4 (2018-01-25)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed pretty printing of `-(-$x)` and `+(+$x)`. (#459)
|
||||
|
||||
Version 3.1.3 (2017-12-26)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Improve compatibility with php-scoper, by supporting prefixed namespaces in
|
||||
`NodeAbstract::getType()`.
|
||||
|
||||
Version 3.1.2 (2017-11-04)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Comments on empty blocks are now preserved on a `Stmt\Nop` node. (#382)
|
||||
|
||||
### Added
|
||||
|
||||
* Added `kind` attribute for `Stmt\Namespace_` node, which is one of `KIND_SEMICOLON` or
|
||||
`KIND_BRACED`. (#417)
|
||||
* Added `setDocComment()` method to namespace builder. (#437)
|
||||
|
||||
Version 3.1.1 (2017-09-02)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed syntax error on comment after brace-style namespace declaration. (#412)
|
||||
* Added support for TraitUse statements in trait builder. (#413)
|
||||
|
||||
Version 3.1.0 (2017-07-28)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.2] Added support for trailing comma in group use statements.
|
||||
* [PHP 7.2] Added support for `object` type. This means `object` types will now be represented as a
|
||||
builtin type (a simple `"object"` string), rather than a class `Name`.
|
||||
|
||||
### Fixed
|
||||
|
||||
* Floating-point numbers are now printed correctly if the LC_NUMERIC locale uses a comma as decimal
|
||||
separator.
|
||||
|
||||
### Changed
|
||||
|
||||
* `Name::$parts` is no longer deprecated.
|
||||
|
||||
Version 3.0.6 (2017-06-28)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed the spelling of `Class_::VISIBILITY_MODIFIER_MASK`. The previous spelling of
|
||||
`Class_::VISIBILITY_MODIFER_MASK` is preserved for backwards compatibility.
|
||||
* The pretty printing will now preserve comments inside array literals and function calls by
|
||||
printing the array items / function arguments on separate lines. Array literals and functions that
|
||||
do not contain comments are not affected.
|
||||
|
||||
### Added
|
||||
|
||||
* Added `Builder\Param::makeVariadic()`.
|
||||
|
||||
### Deprecated
|
||||
|
||||
* The `Node::setLine()` method has been deprecated.
|
||||
|
||||
Version 3.0.5 (2017-03-05)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Name resolution of `NullableType`s is now performed earlier, so that a fully resolved signature is
|
||||
available when a function is entered. (#360)
|
||||
* `Error` nodes are now considered empty, while previously they extended until the token where the
|
||||
error occurred. This made some nodes larger than expected. (#359)
|
||||
* Fixed notices being thrown during error recovery in some situations. (#362)
|
||||
|
||||
Version 3.0.4 (2017-02-10)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed some extensibility issues in pretty printer (`pUseType()` is now public and `pPrec()` calls
|
||||
into `p()`, instead of directly dispatching to the type-specific printing method).
|
||||
* Fixed notice in `bin/php-parse` script.
|
||||
|
||||
### Added
|
||||
|
||||
* Error recovery from missing semicolons is now supported in more cases.
|
||||
* Error recovery from trailing commas in positions where PHP does not support them is now supported.
|
||||
|
||||
Version 3.0.3 (2017-02-03)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* In `"$foo[0]"` the `0` is now parsed as an `LNumber` rather than `String`. (#325)
|
||||
* Ensure integers and floats are always pretty printed preserving semantics, even if the particular
|
||||
value can only be manually constructed.
|
||||
* Throw a `LogicException` when trying to pretty-print an `Error` node. Previously this resulted in
|
||||
an undefined method exception or fatal error.
|
||||
|
||||
### Added
|
||||
|
||||
* [PHP 7.1] Added support for negative interpolated offsets: `"$foo[-1]"`
|
||||
* Added `preserveOriginalNames` option to `NameResolver`. If this option is enabled, an
|
||||
`originalName` attribute, containing the unresolved name, will be added to each resolved name.
|
||||
* Added `php-parse --with-positions` option, which dumps nodes with position information.
|
||||
|
||||
### Deprecated
|
||||
|
||||
* The XML serializer has been deprecated. In particular, the classes `Serializer\XML`,
|
||||
`Unserializer\XML`, as well as the interfaces `Serializer` and `Unserializer` are deprecated.
|
||||
|
||||
Version 3.0.2 (2016-12-06)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed name resolution of nullable types. (#324)
|
||||
* Fixed pretty-printing of nullable types.
|
||||
|
||||
Version 3.0.1 (2016-12-01)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed handling of nested `list()`s: If the nested list was unkeyed, it was directly included in
|
||||
the list items. If it was keyed, it was wrapped in `ArrayItem`. Now nested `List_` nodes are
|
||||
always wrapped in `ArrayItem`s. (#321)
|
||||
|
||||
Version 3.0.0 (2016-11-30)
|
||||
--------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for dumping node positions in the NodeDumper through the `dumpPositions` option.
|
||||
* Added error recovery support for `$`, `new`, `Foo::`.
|
||||
|
||||
Version 3.0.0-beta2 (2016-10-29)
|
||||
--------------------------------
|
||||
|
||||
This release primarily improves our support for error recovery.
|
||||
|
||||
### Added
|
||||
|
||||
* Added `Node::setDocComment()` method.
|
||||
* Added `Error::getMessageWithColumnInfo()` method.
|
||||
* Added support for recovery from lexer errors.
|
||||
* Added support for recovering from "special" errors (i.e. non-syntax parse errors).
|
||||
* Added precise location information for lexer errors.
|
||||
* Added `ErrorHandler` interface, and `ErrorHandler\Throwing` and `ErrorHandler\Collecting` as
|
||||
specific implementations. These provide a general mechanism for handling error recovery.
|
||||
* Added optional `ErrorHandler` argument to `Parser::parse()`, `Lexer::startLexing()` and
|
||||
`NameResolver::__construct()`.
|
||||
* The `NameResolver` now adds a `namespacedName` attribute on name nodes that cannot be statically
|
||||
resolved (unqualified unaliased function or constant names in namespaces).
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed attribute assignment for `GroupUse` prefix and variables in interpolated strings.
|
||||
|
||||
### Changed
|
||||
|
||||
* The constants on `NameTraverserInterface` have been moved into the `NameTraverser` class.
|
||||
* Due to the error handling changes, the `Parser` interface and `Lexer` API have changed.
|
||||
* The emulative lexer now directly postprocesses tokens, instead of using `~__EMU__~` sequences.
|
||||
This changes the protected API of the lexer.
|
||||
* The `Name::slice()` method now returns `null` for empty slices, previously `new Name([])` was
|
||||
used. `Name::concat()` now also supports concatenation with `null`.
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed `Name::append()` and `Name::prepend()`. These mutable methods have been superseded by
|
||||
the immutable `Name::concat()`.
|
||||
* Removed `Error::getRawLine()` and `Error::setRawLine()`. These methods have been superseded by
|
||||
`Error::getStartLine()` and `Error::setStartLine()`.
|
||||
* Removed support for node cloning in the `NodeTraverser`.
|
||||
* Removed `$separator` argument from `Name::toString()`.
|
||||
* Removed `throw_on_error` parser option and `Parser::getErrors()` method. Use the `ErrorHandler`
|
||||
mechanism instead.
|
||||
|
||||
Version 3.0.0-beta1 (2016-09-16)
|
||||
--------------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [7.1] Function/method and parameter builders now support PHP 7.1 type hints (void, iterable and
|
||||
nullable types).
|
||||
* Nodes and Comments now implement `JsonSerializable`. The node kind is stored in a `nodeType`
|
||||
property.
|
||||
* The `InlineHTML` node now has an `hasLeadingNewline` attribute, that specifies whether the
|
||||
preceding closing tag contained a newline. The pretty printer honors this attribute.
|
||||
* Partial parsing of `$obj->` (with missing property name) is now supported in error recovery mode.
|
||||
* The error recovery mode is now exposed in the `php-parse` script through the `--with-recovery`
|
||||
or `-r` flags.
|
||||
|
||||
The following changes are also part of PHP-Parser 2.1.1:
|
||||
|
||||
* The PHP 7 parser will now generate a parse error for `$var =& new Obj` assignments.
|
||||
* Comments on free-standing code blocks will now be retained as comments on the first statement in
|
||||
the code block.
|
||||
|
||||
Version 3.0.0-alpha1 (2016-07-25)
|
||||
---------------------------------
|
||||
|
||||
### Added
|
||||
|
||||
* [7.1] Added support for `void` and `iterable` types. These will now be represented as strings
|
||||
(instead of `Name` instances) similar to other builtin types.
|
||||
* [7.1] Added support for class constant visibility. The `ClassConst` node now has a `flags` subnode
|
||||
holding the visibility modifier, as well as `isPublic()`, `isProtected()` and `isPrivate()`
|
||||
methods. The constructor changed to accept the additional subnode.
|
||||
* [7.1] Added support for nullable types. These are represented using a new `NullableType` node
|
||||
with a single `type` subnode.
|
||||
* [7.1] Added support for short array destructuring syntax. This means that `Array` nodes may now
|
||||
appear as the left-hand-side of assignments and foreach value targets. Additionally the array
|
||||
items may now contain `null` values if elements are skipped.
|
||||
* [7.1] Added support for keys in list() destructuring. The `List` subnode `vars` has been renamed
|
||||
to `items` and now contains `ArrayItem`s instead of plain variables.
|
||||
* [7.1] Added support for multi-catch. The `Catch` subnode `type` has been renamed to `types` and
|
||||
is now an array of `Name`s.
|
||||
* `Name::slice()` now supports lengths and negative offsets. This brings it in line with
|
||||
`array_slice()` functionality.
|
||||
|
||||
### Changed
|
||||
|
||||
Due to PHP 7.1 support additions described above, the node structure changed as follows:
|
||||
|
||||
* `void` and `iterable` types are now stored as strings if the PHP 7 parser is used.
|
||||
* The `ClassConst` constructor changed to accept an additional `flags` subnode.
|
||||
* The `Array` subnode `items` may now contain `null` elements (destructuring).
|
||||
* 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.
|
||||
|
||||
Additionally the following changes were made:
|
||||
|
||||
* 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`.
|
||||
* The `TryCatch` subnode `finallyStmts` has been replaced with a `finally` subnode that holds an
|
||||
explicit `Finally` node. This allows for more accurate attribute assignment.
|
||||
* 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 `NodeDumper` now prints class/method/property/constant modifiers, as well as the include and
|
||||
use type in a textual representation, instead of only showing the number.
|
||||
* All methods on `PrettyPrinter\Standard` are now protected. Previously most of them were public.
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed support for running on PHP 5.4. It is however still possible to parse PHP 5.2-5.4 code
|
||||
while running on a newer version.
|
||||
* The deprecated `Comment::setLine()` and `Comment::setText()` methods have been removed.
|
||||
* The deprecated `Name::set()`, `Name::setFirst()` and `Name::setLast()` methods have been removed.
|
||||
|
||||
Version 2.1.1 (2016-09-16)
|
||||
--------------------------
|
||||
|
||||
### Changed
|
||||
|
||||
* The pretty printer will now escape all control characters in the range `\x00-\x1F` inside double
|
||||
quoted strings. If no special escape sequence is available, an octal escape will be used.
|
||||
* The quality of the error recovery has been improved. In particular unterminated expressions should
|
||||
be handled more gracefully.
|
||||
* The PHP 7 parser will now generate a parse error for `$var =& new Obj` assignments.
|
||||
* Comments on free-standing code blocks will no be retained as comments on the first statement in
|
||||
the code block.
|
||||
|
||||
Version 2.1.0 (2016-04-19)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Properly support `B""` strings (with uppercase `B`) in a number of places.
|
||||
* Fixed reformatting of indented parts in a certain non-standard comment style.
|
||||
|
||||
### Added
|
||||
|
||||
* Added `dumpComments` option to node dumper, to enable dumping of comments associated with nodes.
|
||||
* Added `Stmt\Nop` node, that is used to collect comments located at the end of a block or at the
|
||||
end of a file (without a following node with which they could otherwise be associated).
|
||||
* Added `kind` attribute to `Expr\Exit` to distinguish between `exit` and `die`.
|
||||
* Added `kind` attribute to `Scalar\LNumber` to distinguish between decimal, binary, octal and
|
||||
hexadecimal numbers.
|
||||
* Added `kind` attribute to `Expr\Array` to distinguish between `array()` and `[]`.
|
||||
* Added `kind` attribute to `Scalar\String` and `Scalar\Encapsed` to distinguish between
|
||||
single-quoted, double-quoted, heredoc and nowdoc string.
|
||||
* Added `docLabel` attribute to `Scalar\String` and `Scalar\Encapsed`, if it is a heredoc or
|
||||
nowdoc string.
|
||||
* Added start file offset information to `Comment` nodes.
|
||||
* Added `setReturnType()` method to function and method builders.
|
||||
* Added `-h` and `--help` options to `php-parse` script.
|
||||
|
||||
### Changed
|
||||
|
||||
* Invalid octal literals now throw a parse error in PHP 7 mode.
|
||||
* The pretty printer takes all the new attributes mentioned in the previous section into account.
|
||||
* The protected `AbstractPrettyPrinter::pComments()` method no longer returns a trailing newline.
|
||||
* The bundled autoloader supports library files being stored in a different directory than
|
||||
`PhpParser` for easier downstream distribution.
|
||||
|
||||
### Deprecated
|
||||
|
||||
* The `Comment::setLine()` and `Comment::setText()` methods have been deprecated. Construct new
|
||||
objects instead.
|
||||
|
||||
### Removed
|
||||
|
||||
* The internal (but public) method `Scalar\LNumber::parse()` has been removed. A non-internal
|
||||
`LNumber::fromString()` method has been added instead.
|
||||
|
||||
Version 2.0.1 (2016-02-28)
|
||||
--------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* `declare() {}` and `declare();` are not semantically equivalent and will now result in different
|
||||
ASTs. The format case will have an empty `stmts` array, while the latter will set `stmts` to
|
||||
`null`.
|
||||
* Magic constants are now supported as semi-reserved keywords.
|
||||
* A shebang line like `#!/usr/bin/env php` is now allowed at the start of a namespaced file.
|
||||
Previously this generated an exception.
|
||||
* The `prettyPrintFile()` method will not strip a trailing `?>` from the raw data that follows a
|
||||
`__halt_compiler()` statement.
|
||||
* The `prettyPrintFile()` method will not strip an opening `<?php` if the file starts with a
|
||||
comment followed by InlineHTML.
|
||||
|
||||
Version 2.0.0 (2015-12-04)
|
||||
--------------------------
|
||||
|
||||
### Changed
|
||||
|
||||
* String parts of encapsed strings are now represented using `Scalar\EncapsStringPart` nodes.
|
||||
Previously raw strings were used. This affects the `parts` child of `Scalar\Encaps` and
|
||||
`Expr\ShellExec`. The change has been done to allow assignment of attributes to encapsed string
|
||||
parts.
|
||||
|
||||
Version 2.0.0-beta1 (2015-10-21)
|
||||
--------------------------------
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed issue with too many newlines being stripped at the end of heredoc/nowdoc strings in some
|
||||
cases. (#227)
|
||||
|
||||
### Changed
|
||||
|
||||
* Update group use support to be in line with recent PHP 7.0 builds.
|
||||
* Renamed `php-parse.php` to `php-parse` and registered it as a composer bin.
|
||||
* Use composer PSR-4 autoloader instead of custom autoloader.
|
||||
* Specify phpunit as a dev dependency.
|
||||
|
||||
### Added
|
||||
|
||||
* Added `shortArraySyntax` option to pretty printer, to print all arrays using short syntax.
|
||||
|
||||
Version 2.0.0-alpha1 (2015-07-14)
|
||||
---------------------------------
|
||||
|
||||
A more detailed description of backwards incompatible changes can be found in the
|
||||
[upgrading guide](UPGRADE-2.0.md).
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed support for running on PHP 5.3. It is however still possible to parse PHP 5.2 and PHP 5.3
|
||||
code while running on a newer version.
|
||||
* Removed legacy class name aliases. This includes the old non-namespaced class names and the old
|
||||
names for classes that were renamed for PHP 7 compatibility.
|
||||
* Removed support for legacy node format. All nodes must have a `getSubNodeNames()` method now.
|
||||
|
||||
### Added
|
||||
|
||||
* Added support for remaining PHP 7 features that were not present in 1.x:
|
||||
* Group use declarations. These are represented using `Stmt\GroupUse` nodes. Furthermore a `type`
|
||||
attribute was added to `Stmt\UseUse` to handle mixed group use declarations.
|
||||
* Uniform variable syntax.
|
||||
* Generalized yield operator.
|
||||
* Scalar type declarations. These are presented using `'bool'`, `'int'`, `'float'` and `'string'`
|
||||
as the type. The PHP 5 parser also accepts these, however they'll be `Name` instances there.
|
||||
* Unicode escape sequences.
|
||||
* Added `PhpParser\ParserFactory` class, which should be used to create parser instances.
|
||||
* Added `Name::concat()` which concatenates two names.
|
||||
* Added `Name->slice()` which takes a subslice of a name.
|
||||
|
||||
### Changed
|
||||
|
||||
* `PhpParser\Parser` is now an interface, implemented by `Parser\Php5`, `Parser\Php7` and
|
||||
`Parser\Multiple`. The `Multiple` parser will try multiple parsers, until one succeeds.
|
||||
* Token constants are now defined on `PhpParser\Parser\Tokens` rather than `PhpParser\Parser`.
|
||||
* The `Name->set()`, `Name->append()`, `Name->prepend()` and `Name->setFirst()` methods are
|
||||
deprecated in favor of `Name::concat()` and `Name->slice()`.
|
||||
* The `NodeTraverser` no longer clones nodes by default. The old behavior can be restored by
|
||||
passing `true` to the constructor.
|
||||
* The constructor for `Scalar` nodes no longer has a default value. E.g. `new LNumber()` should now
|
||||
be written as `new LNumber(0)`.
|
||||
|
||||
---
|
||||
|
||||
**This changelog only includes changes from the 2.0 series. For older changes see the
|
||||
[1.x series changelog](https://github.com/nikic/PHP-Parser/blob/1.x/CHANGELOG.md) and the
|
||||
[0.9 series changelog](https://github.com/nikic/PHP-Parser/blob/0.9/CHANGELOG.md).**
|
||||
|
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.
|
||||
|
241
README.md
241
README.md
@ -1,78 +1,225 @@
|
||||
PHP Parser
|
||||
==========
|
||||
|
||||
This is a PHP 5.4 (and older) parser written in PHP. It's purpose is to simplify static code analysis and
|
||||
[](https://travis-ci.org/nikic/PHP-Parser) [](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
|
||||
|
||||
This is a PHP 5.2 to PHP 7.4 parser written in PHP. Its purpose is to simplify static code analysis and
|
||||
manipulation.
|
||||
|
||||
Documentation can be found in the [`doc/`][1] directory.
|
||||
[**Documentation for version 4.x**][doc_master] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.4).
|
||||
|
||||
***Note: This project is experimental, so the API is subject to change.***
|
||||
[Documentation for version 3.x][doc_3_x] (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
|
||||
|
||||
In a Nutshell
|
||||
-------------
|
||||
Features
|
||||
--------
|
||||
|
||||
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.)
|
||||
The main features provided by this library are:
|
||||
|
||||
For example, if you stick this code in the parser:
|
||||
* Parsing PHP 5 and PHP 7 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.
|
||||
* Experimental: 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)->create(ParserFactory::PREFER_PHP7);
|
||||
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(
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: test
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
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(
|
||||
parts: array(
|
||||
0: var_dump
|
||||
)
|
||||
)
|
||||
right: Scalar_String(
|
||||
value: baz
|
||||
args: array(
|
||||
0: Arg(
|
||||
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(
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: test
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
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_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.
|
205
bin/php-parse
Executable file
205
bin/php-parse
Executable file
@ -0,0 +1,205 @@
|
||||
#!/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.");
|
||||
}
|
||||
|
||||
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
|
||||
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
|
||||
]]);
|
||||
$parser = (new PhpParser\ParserFactory)->create(
|
||||
PhpParser\ParserFactory::PREFER_PHP7,
|
||||
$lexer
|
||||
);
|
||||
$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 (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
|
||||
-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,
|
||||
];
|
||||
|
||||
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 ($arg[0] === '-') {
|
||||
showHelp("Invalid operation $arg.");
|
||||
} else {
|
||||
$files[] = $arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$operations, $files, $attributes];
|
||||
}
|
35
composer.json
Executable file → Normal file
35
composer.json
Executable file → Normal file
@ -1,18 +1,41 @@
|
||||
{
|
||||
"name": "nikic/php-parser",
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": ["php", "parser"],
|
||||
"type": "library",
|
||||
"license": "BSD",
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": [
|
||||
"php",
|
||||
"parser"
|
||||
],
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nikita Popov"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.2"
|
||||
"php": ">=7.0",
|
||||
"ext-tokenizer": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0",
|
||||
"ircmaxell/php-yacc": "^0.0.7"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "4.9-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "PHPParser": "lib/" }
|
||||
}
|
||||
"psr-4": {
|
||||
"PhpParser\\": "lib/PhpParser"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"PhpParser\\": "test/PhpParser/"
|
||||
}
|
||||
},
|
||||
"bin": [
|
||||
"bin/php-parse"
|
||||
]
|
||||
}
|
||||
|
@ -1,43 +1,42 @@
|
||||
Introduction
|
||||
============
|
||||
|
||||
This project is a PHP 5.4 (and older) parser **written in PHP itself**.
|
||||
This project is a PHP 5.2 to PHP 7.4 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 analyize the exact formating of
|
||||
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
|
||||
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
|
||||
programmatic PHP code analysis are incidentially PHP developers, not C developers.
|
||||
programmatic PHP code analysis are incidentally PHP developers, not C developers.
|
||||
|
||||
What can it parse?
|
||||
------------------
|
||||
|
||||
The parser uses a PHP 5.4 compliant grammar, which is backwards compatible with at least PHP 5.3 and PHP
|
||||
5.2 (and maybe older).
|
||||
The parser supports parsing PHP 5.2-7.4.
|
||||
|
||||
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 and 5.4 is provided. This
|
||||
allows to parse PHP 5.4 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 7.4 source code running on PHP 7.0, for example. This emulation is somewhat
|
||||
hacky and not perfect, but it should work well on any sane code.
|
||||
|
||||
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,8 +55,8 @@ 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.
|
||||
@ -70,7 +69,7 @@ Apart from the parser itself this package also bundles support for some other, r
|
||||
* 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 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)
|
||||
@ -78,4 +77,4 @@ Apart from the parser itself this package also bundles support for some other, r
|
||||
|
||||
[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.2"
|
||||
}
|
||||
}
|
||||
|
||||
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 channel://nikic.github.com/pear/PHPParser-0.9.2
|
||||
|
||||
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,136 @@ 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:
|
||||
|
||||
```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;
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
```
|
||||
|
||||
The factory accepts a kind argument, that determines how different PHP versions are treated:
|
||||
|
||||
Kind | Behavior
|
||||
-----|---------
|
||||
`ParserFactory::PREFER_PHP7` | Try to parse code as PHP 7. If this fails, try to parse it as PHP 5.
|
||||
`ParserFactory::PREFER_PHP5` | Try to parse code as PHP 5. If this fails, try to parse it as PHP 7.
|
||||
`ParserFactory::ONLY_PHP7` | Parse code as PHP 7.
|
||||
`ParserFactory::ONLY_PHP5` | Parse code as PHP 5.
|
||||
|
||||
Unless you have a strong reason to use something else, `PREFER_PHP7` is a reasonable default.
|
||||
|
||||
The `create()` method optionally accepts a `Lexer` instance as the second argument. Some use cases
|
||||
that require customized lexers are discussed in the [lexer documentation](component/Lexer.markdown).
|
||||
|
||||
Subsequently you can pass PHP code (including the opening `<?php` tag) to the `parse` method in order to
|
||||
create a syntax tree. If a syntax error is encountered, an `PhpParser\Error` exception will be thrown:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$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)->create(ParserFactory::PREFER_PHP7);
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
} catch (PHPParser_Error $e) {
|
||||
// $stmts is an array of statement nodes
|
||||
} catch (Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
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.4 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(
|
||||
byRef: false
|
||||
name: Identifier(
|
||||
name: printLine
|
||||
)
|
||||
params: array(
|
||||
0: Param(
|
||||
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(
|
||||
parts: array(
|
||||
0: printLine
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
value: Scalar_String(
|
||||
value: Hello World!!!
|
||||
)
|
||||
byRef: false
|
||||
unpack: false
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -80,38 +143,63 @@ 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
|
||||
* `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.
|
||||
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()`.
|
||||
@ -121,15 +209,17 @@ 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_Zend` (the name "Zend" might be misleading. It does not strictly adhere
|
||||
to the Zend Coding Standard.)
|
||||
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_Zend;
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
|
||||
try {
|
||||
// parse
|
||||
@ -140,26 +230,31 @@ 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) {
|
||||
} catch (Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
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_Zend->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()`.
|
||||
|
||||
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.
|
||||
|
||||
> Read more: [Pretty printing documentation](component/Pretty_printing.markdown)
|
||||
|
||||
Node traversation
|
||||
-----------------
|
||||
@ -170,20 +265,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_Zend;
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
$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);
|
||||
|
||||
@ -191,22 +289,24 @@ 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
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
class MyNodeVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
public function leaveNode(PHPParser_Node $node) {
|
||||
if ($node instanceof PHPParser_Node_Scalar_String) {
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Scalar\String_) {
|
||||
$node->value = 'foo';
|
||||
}
|
||||
}
|
||||
@ -215,38 +315,48 @@ 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
|
||||
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
|
||||
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 `NodeTraverser::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, `NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN` can be used instead.
|
||||
|
||||
The `leaveNode()` method can additionally return the value `NodeTraverser::REMOVE_NODE`, in which
|
||||
case the current node will be removed from the parent array. Furthermore it is possible to return
|
||||
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:
|
||||
@ -257,7 +367,7 @@ 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.
|
||||
@ -265,6 +375,8 @@ 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`.
|
||||
|
||||
> Read more: [Name resolution documentation](component/Name_resolution.markdown)
|
||||
|
||||
Example: Converting namespaced code to pseudo namespaces
|
||||
--------------------------------------------------------
|
||||
|
||||
@ -276,31 +388,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_Zend;
|
||||
$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)->create(ParserFactory::PREFER_PHP7);
|
||||
$traverser = new NodeTraverser;
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
|
||||
// iterate over all files in the directory
|
||||
foreach (new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator(IN_DIR),
|
||||
RecursiveIteratorIterator::LEAVES_ONLY)
|
||||
as $file) {
|
||||
// only convert .php files
|
||||
if (!preg_match('~\.php$~', $file)) {
|
||||
continue;
|
||||
}
|
||||
$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($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);
|
||||
@ -309,58 +419,61 @@ foreach (new RecursiveIteratorIterator(
|
||||
$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 `NodeVisitor\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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -372,29 +485,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\NodeTraverser;
|
||||
|
||||
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 NodeTraverser::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, 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
|
138
doc/component/AST_builders.markdown
Normal file
138
doc/component/AST_builders.markdown
Normal file
@ -0,0 +1,138 @@
|
||||
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 and traits
|
||||
* methods, functions and parameters
|
||||
* properties
|
||||
|
||||
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 = array(1, 2, 3);
|
||||
/**
|
||||
* This method does something.
|
||||
*
|
||||
* @param SomeClass And takes a parameter
|
||||
*/
|
||||
public abstract 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.
|
||||
|
||||
These methods may be expanded on an as-needed basis. Please open an issue or PR if a common
|
||||
operation is missing.
|
115
doc/component/Constant_expression_evaluation.markdown
Normal file
115
doc/component/Constant_expression_evaluation.markdown
Normal file
@ -0,0 +1,115 @@
|
||||
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};
|
||||
|
||||
$evalutator = new ConstExprEvaluator();
|
||||
try {
|
||||
$value = $evalutator->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\LNumber(10), new Scalar\LNumber(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`
|
||||
|
||||
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;
|
||||
|
||||
$evalutator = 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 {
|
||||
$evalutator->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;
|
||||
}
|
||||
```
|
75
doc/component/Error_handling.markdown
Normal file
75
doc/component/Error_handling.markdown
Normal file
@ -0,0 +1,75 @@
|
||||
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 and how many lexer attributes have been
|
||||
enabled. At a minimum the start line of the error is usually available.
|
||||
|
||||
Column information
|
||||
------------------
|
||||
|
||||
In order to receive information about not only the line, but also the column span an error occurred at, the file
|
||||
position attributes in the lexer need to be enabled:
|
||||
|
||||
```php
|
||||
$lexer = new PhpParser\Lexer(array(
|
||||
'usedAttributes' => array('comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'),
|
||||
));
|
||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
// ...
|
||||
} catch (PhpParser\Error $e) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
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();
|
||||
} 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)->create(PhpParser\ParserFactory::ONLY_PHP7);
|
||||
$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 `NameResolver` visitor also accepts an `ErrorHandler` as a constructor argument.
|
55
doc/component/FAQ.markdown
Normal file
55
doc/component/FAQ.markdown
Normal file
@ -0,0 +1,55 @@
|
||||
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;
|
||||
$traverser->addVisitor(new ParentConnectingVisitor);
|
||||
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
$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;
|
||||
$traverser->addVisitor(new NodeConnectingVisitor);
|
||||
|
||||
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
$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.
|
131
doc/component/JSON_representation.markdown
Normal file
131
doc/component/JSON_representation.markdown
Normal file
@ -0,0 +1,131 @@
|
||||
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)->create(ParserFactory::PREFER_PHP7);
|
||||
|
||||
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",
|
||||
"byRef": false,
|
||||
"name": {
|
||||
"nodeType": "Identifier",
|
||||
"name": "printLine",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"nodeType": "Param",
|
||||
"type": null,
|
||||
"byRef": false,
|
||||
"variadic": false,
|
||||
"var": {
|
||||
"nodeType": "Expr_Variable",
|
||||
"name": "msg",
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
},
|
||||
"default": null,
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"endLine": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"returnType": null,
|
||||
"stmts": [
|
||||
{
|
||||
"nodeType": "Stmt_Echo",
|
||||
"exprs": [
|
||||
{
|
||||
"nodeType": "Expr_Variable",
|
||||
"name": "msg",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"nodeType": "Scalar_String",
|
||||
"value": "\n",
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5,
|
||||
"kind": 2
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
}
|
||||
}
|
||||
],
|
||||
"attributes": {
|
||||
"startLine": 4,
|
||||
"comments": [
|
||||
{
|
||||
"nodeType": "Comment_Doc",
|
||||
"text": "\/** @param string $msg *\/",
|
||||
"line": 3,
|
||||
"filePos": 9,
|
||||
"tokenPos": 2
|
||||
}
|
||||
],
|
||||
"endLine": 6
|
||||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
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,51 +1,127 @@
|
||||
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
|
||||
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.
|
||||
|
||||
A lexer has to define the following public interface:
|
||||
This documentation discusses options available for the default lexers and explains how lexers can be extended.
|
||||
|
||||
startLexing($code);
|
||||
getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null);
|
||||
handleHaltCompiler();
|
||||
Lexer options
|
||||
-------------
|
||||
|
||||
startLexing
|
||||
-----------
|
||||
|
||||
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.
|
||||
|
||||
Even though `startLexing` is meant to accept a source code string, you could for example overwrite it to accept a file:
|
||||
The two default lexers accept an `$options` array in the constructor. Currently only the `'usedAttributes'` option is
|
||||
supported, which allows you to specify which attributes will be added to the AST nodes. The attributes can then be
|
||||
accessed using `$node->getAttribute()`, `$node->setAttribute()`, `$node->hasAttribute()` and `$node->getAttributes()`
|
||||
methods. A sample options array:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$lexer = new PhpParser\Lexer(array(
|
||||
'usedAttributes' => array(
|
||||
'comments', 'startLine', 'endLine'
|
||||
)
|
||||
));
|
||||
```
|
||||
|
||||
class FileLexer extends PHPParser_Lexer {
|
||||
public function startLexing($fileName) {
|
||||
if (!file_exists($fileName)) {
|
||||
throw new InvalidArgumentException(sprintf('File "%s" does not exist', $fileName));
|
||||
The attributes used in this example match the default behavior of the lexer. The following attributes are supported:
|
||||
|
||||
* `comments`: Array of `PhpParser\Comment` or `PhpParser\Comment\Doc` instances, representing all comments that occurred
|
||||
between the previous non-discarded token and the current one. Use of this attribute is required for the
|
||||
`$node->getComments()` and `$node->getDocComment()` methods to work. The attribute is also needed if you wish the pretty
|
||||
printer to retain comments present in the original code.
|
||||
* `startLine`: Line in which the node starts. This attribute is required for the `$node->getLine()` to work. It is also
|
||||
required if syntax errors should contain line number information.
|
||||
* `endLine`: Line in which the node ends. Required for `$node->getEndLine()`.
|
||||
* `startTokenPos`: Offset into the token array of the first token in the node. Required for `$node->getStartTokenPos()`.
|
||||
* `endTokenPos`: Offset into the token array of the last token in the node. Required for `$node->getEndTokenPos()`.
|
||||
* `startFilePos`: Offset into the code string of the first character that is part of the node. Required for `$node->getStartFilePos()`.
|
||||
* `endFilePos`: Offset into the code string of the last character that is part of the node. Required for `$node->getEndFilePos()`.
|
||||
|
||||
### 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
|
||||
function isDeclaredUsingVar(array $tokens, PhpParser\Node\Stmt\Property $prop) {
|
||||
$i = $prop->getAttribute('startTokenPos');
|
||||
return $tokens[$i][0] === 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);
|
||||
$lexer = new PhpParser\Lexer(array(
|
||||
'usedAttributes' => array(
|
||||
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
|
||||
)
|
||||
));
|
||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::ONLY_PHP7, $lexer);
|
||||
|
||||
var_dump($parser->parse('someFile.php'));
|
||||
var_dump($parser->parse('someOtherFile.php'));
|
||||
$visitor = new MyNodeVisitor();
|
||||
$traverser = new PhpParser\NodeTraverser();
|
||||
$traverser->addVisitor($visitor);
|
||||
|
||||
try {
|
||||
$stmts = $parser->parse($code);
|
||||
$visitor->setTokens($lexer->getTokens());
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
} catch (PhpParser\Error $e) {
|
||||
echo 'Parse Error: ', $e->getMessage();
|
||||
}
|
||||
```
|
||||
|
||||
getNextToken
|
||||
------------
|
||||
The same approach can also be used to perform specific modifications in the code, without changing the formatting in
|
||||
other places (which is the case when using the pretty printer).
|
||||
|
||||
`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.
|
||||
Lexer extension
|
||||
---------------
|
||||
|
||||
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.
|
||||
A lexer has to define the following public interface:
|
||||
|
||||
```php
|
||||
function startLexing(string $code, ErrorHandler $errorHandler = null): void;
|
||||
function getTokens(): array;
|
||||
function handleHaltCompiler(): string;
|
||||
function getNextToken(string &$value = null, array &$startAttributes = null, array &$endAttributes = null): int;
|
||||
```
|
||||
|
||||
The `startLexing()` method is invoked whenever the `parse()` method of the parser is called and is passed the source
|
||||
code that is to be lexed (including the opening tag). It can be used to reset state or preprocess the source code or tokens. The
|
||||
passed `ErrorHandler` should be used to report lexing errors.
|
||||
|
||||
The `getTokens()` method returns the current token array, in the usual `token_get_all()` format. This method is not
|
||||
used by the parser (which uses `getNextToken()`), but is useful in combination with the token position attributes.
|
||||
|
||||
The `handleHaltCompiler()` method is called whenever a `T_HALT_COMPILER` token is encountered. It has to return the
|
||||
remaining string after the construct (not including `();`).
|
||||
|
||||
The `getNextToken()` method returns the ID of the next token (as defined by the `Parser::T_*` constants). If no more
|
||||
tokens are available it must return `0`, which is the ID of the `EOF` token. Furthermore the string content of the
|
||||
token should be written into the by-reference `$value` parameter (which will then be available as `$n` in the parser).
|
||||
|
||||
### Attribute handling
|
||||
|
||||
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
|
||||
@ -54,61 +130,30 @@ 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:
|
||||
An application of custom attributes is storing the exact original formatting of literals: While the parser does retain
|
||||
some information about the formatting of integers (like decimal vs. hexadecimal) or strings (like used quote type), it
|
||||
does not preserve the exact original formatting (e.g. leading zeros for integers or escape sequences in strings). This
|
||||
can be remedied by storing the original value in an attribute:
|
||||
|
||||
```php
|
||||
<?php
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\Parser\Tokens;
|
||||
|
||||
class LessAttributesLexer extends PHPParser_Lexer {
|
||||
class KeepOriginalValueLexer extends Lexer // or Lexer\Emulative
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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));
|
||||
if ($tokenId == Tokens::T_CONSTANT_ENCAPSED_STRING // non-interpolated string
|
||||
|| $tokenId == Tokens::T_ENCAPSED_AND_WHITESPACE // interpolated string
|
||||
|| $tokenId == Tokens::T_LNUMBER // integer
|
||||
|| $tokenId == Tokens::T_DNUMBER // floating point number
|
||||
) {
|
||||
// could also use $startAttributes, doesn't really matter here
|
||||
$endAttributes['originalValue'] = $value;
|
||||
}
|
||||
|
||||
$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, is) 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 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 resoluting 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 on 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.
|
65
doc/component/Performance.markdown
Normal file
65
doc/component/Performance.markdown
Normal file
@ -0,0 +1,65 @@
|
||||
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 lexers and pretty
|
||||
printers, though the details might change between versions of this library.)
|
||||
|
||||
Garbage collection
|
||||
------------------
|
||||
|
||||
A limitation in PHP's cyclic garbage collector may lead to major performance degradation when the
|
||||
active working set exceeds 10000 objects (or arrays). Especially when parsing very large files this
|
||||
limit is significantly exceeded and PHP will spend the majority of time performing unnecessary
|
||||
garbage collection attempts.
|
||||
|
||||
Without GC, parsing time is roughly linear in the input size. With GC, this degenerates to quadratic
|
||||
runtime for large files. While the specifics may differ, as a rough guideline you may expect a 2.5x
|
||||
GC overhead for 500KB files and a 5x overhead for 1MB files.
|
||||
|
||||
Because this a limitation in PHP's implementation, there is no easy way to work around this. If
|
||||
possible, you should avoid parsing very large files, as they will impact overall execution time
|
||||
disproportionally (and are usually generated anyway).
|
||||
|
||||
Of course, you can also try to (temporarily) disable GC. By design the AST generated by PHP-Parser
|
||||
is cycle-free, so the AST itself will never cause leaks with GC disabled. However, other code
|
||||
(including for example the parser object itself) may hold cycles, so disabling of GC should be
|
||||
approached with care.
|
96
doc/component/Pretty_printing.markdown
Normal file
96
doc/component/Pretty_printing.markdown
Normal file
@ -0,0 +1,96 @@
|
||||
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
|
||||
--------------------------
|
||||
|
||||
Apart from an `shortArraySyntax` option, the default pretty printer does not provide any
|
||||
functionality to customize the formatting of the generated code. The pretty printer does respect a
|
||||
number of `kind` attributes used by some notes (e.g., whether an integer should be printed as
|
||||
decimal, hexadecimal, etc), but there are no options to control brace placement or similar.
|
||||
|
||||
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
|
||||
-------------------------------------
|
||||
|
||||
> **Note:** This functionality is **experimental** and not yet complete.
|
||||
|
||||
For automated code refactoring, migration and similar, you will usually only want to modify a small
|
||||
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, an experimental 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\{Lexer, NodeTraverser, NodeVisitor, Parser, PrettyPrinter};
|
||||
|
||||
$lexer = new Lexer\Emulative([
|
||||
'usedAttributes' => [
|
||||
'comments',
|
||||
'startLine', 'endLine',
|
||||
'startTokenPos', 'endTokenPos',
|
||||
],
|
||||
]);
|
||||
$parser = new Parser\Php7($lexer);
|
||||
|
||||
$traverser = new NodeTraverser();
|
||||
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
||||
|
||||
$printer = new PrettyPrinter\Standard();
|
||||
|
||||
$oldStmts = $parser->parse($code);
|
||||
$oldTokens = $lexer->getTokens();
|
||||
|
||||
$newStmts = $traverser->traverse($oldStmts);
|
||||
|
||||
// MODIFY $newStmts HERE
|
||||
|
||||
$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 directlying modifying
|
||||
the AST and causing spurious changes to the pretty printed code. For more information, see the
|
||||
[name resolution documentation](Name_resolution.markdown).
|
||||
|
||||
This functionality is experimental and not yet fully implemented. It should not provide incorrect
|
||||
code, but it may sometimes reformat more code than necessary. Open issues are tracked in
|
||||
[issue #344](https://github.com/nikic/PHP-Parser/issues/344). If you encounter problems while using
|
||||
this functionality, please open an issue, so we know what to prioritize.
|
337
doc/component/Walking_the_AST.markdown
Normal file
337
doc/component/Walking_the_AST.markdown
Normal file
@ -0,0 +1,337 @@
|
||||
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\LNumber) {
|
||||
return new Node\Scalar\String_((string) $node->value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$stmts = ...;
|
||||
$modifiedStmts = $traverser->traverse($stmts);
|
||||
```
|
||||
|
||||
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(
|
||||
parts: array(
|
||||
0: printLine
|
||||
)
|
||||
)
|
||||
args: array(
|
||||
0: Arg(
|
||||
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, two special replacement types are supported only by leaveNode. The first is removal of a
|
||||
node:
|
||||
|
||||
```php
|
||||
public function leaveNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Return_) {
|
||||
// Remove all return statements
|
||||
return NodeTraverser::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 NodeTraverser::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).
|
||||
|
||||
Next to removing nodes, it is also possible to replace one node with multiple nodes. Again, this
|
||||
only works inside leaveNode and only 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:
|
||||
|
||||
```
|
||||
private $classes = [];
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Class_) {
|
||||
$this->classes[] = $node;
|
||||
return NodeTraverser::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:
|
||||
|
||||
```
|
||||
private $class = null;
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Stmt\Class_ &&
|
||||
$node->namespacedName->toString() === 'Foo\Bar\Baz'
|
||||
) {
|
||||
$this->class = $node;
|
||||
return NodeTraverser::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:
|
||||
|
||||
```
|
||||
$visitorA->enterNode(Stmt_Return)
|
||||
$visitorB->enterNode(Stmt_Return)
|
||||
$visitorA->enterNode(Expr_Variable)
|
||||
$visitorB->enterNode(Expr_Variable)
|
||||
$visitorA->leaveNode(Expr_Variable)
|
||||
$visitorB->leaveNode(Expr_Variable)
|
||||
$visitorA->leaveNode(Stmt_Return)
|
||||
$visitorB->leaveNode(Stmt_Return)
|
||||
```
|
||||
|
||||
That is, when visiting a node, enterNode and leaveNode will always be called for all visitors.
|
||||
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 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 itself, but you can easily
|
||||
emulate this with a visitor. See the [FAQ](FAQ.markdown) for more information.
|
@ -1,30 +1,30 @@
|
||||
What do all those files mean?
|
||||
=============================
|
||||
|
||||
* `zend_language_parser.y`: Original PHP grammer this parser is based on
|
||||
* `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
|
||||
* `php5.y`: PHP 5 grammar written in a pseudo language
|
||||
* `php7.y`: PHP 7 grammar written in a pseudo language
|
||||
* `tokens.y`: Tokens definition shared between PHP 5 and PHP 7 grammars
|
||||
* `parser.template`: A `kmyacc` parser prototype file for PHP
|
||||
* `tokens.template`: A `kmyacc` prototype file for the `Tokens` class
|
||||
* `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` file.
|
||||
Run `php grammar/rebuildParsers.php` to rebuild the parsers. Additional options:
|
||||
|
||||
By default only the Parser.php is built. If you want to build the Parser/Debug.php and the y.output
|
||||
file you need to call the file with the debug option: `rebuildParser.php?debug`.
|
||||
* 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,330 +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 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 array 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
|
||||
$origTokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes);
|
||||
|
||||
// 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;
|
||||
$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 */
|
||||
throw new PHPParser_Error(
|
||||
'Unexpected token ' . self::$terminals[$tokenId],
|
||||
$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;
|
106
grammar/parser.template
Normal file
106
grammar/parser.template
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
$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\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 files grammar/php5.y or grammar/php7.y
|
||||
* * the skeleton file grammar/parser.template
|
||||
* * the preprocessing script grammar/rebuildParsers.php
|
||||
*/
|
||||
class #(-p) extends \PhpParser\ParserAbstract
|
||||
{
|
||||
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() {
|
||||
$this->reduceCallbacks = [
|
||||
#reduce
|
||||
%n => function ($stackPos) {
|
||||
%b
|
||||
},
|
||||
#noact
|
||||
%n => function ($stackPos) {
|
||||
$this->semValue = $this->semStack[$stackPos];
|
||||
},
|
||||
#endreduce
|
||||
];
|
||||
}
|
||||
}
|
||||
#tailcode;
|
1026
grammar/php5.y
Normal file
1026
grammar/php5.y
Normal file
File diff suppressed because it is too large
Load Diff
1104
grammar/php7.y
Normal file
1104
grammar/php7.y
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,212 +0,0 @@
|
||||
<?php
|
||||
|
||||
const GRAMMAR_FILE = './zend_language_parser.phpy';
|
||||
const TMP_FILE = './tmp_parser.phpy';
|
||||
const RESULT_FILE = './tmp_parser.php';
|
||||
|
||||
///////////////////////////////
|
||||
/// 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 '<pre>';
|
||||
|
||||
echo 'Building temporary preproprocessed grammar file.', "\n";
|
||||
|
||||
$grammarCode = file_get_contents(GRAMMAR_FILE);
|
||||
|
||||
$grammarCode = resolveConstants($grammarCode);
|
||||
$grammarCode = resolveNodes($grammarCode);
|
||||
$grammarCode = resolveMacros($grammarCode);
|
||||
$grammarCode = resolveArrays($grammarCode);
|
||||
|
||||
file_put_contents(TMP_FILE, $grammarCode);
|
||||
|
||||
echo 'Building parser. Output: "',
|
||||
trim(shell_exec('kmyacc -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
|
||||
'"', "\n";
|
||||
|
||||
rename(RESULT_FILE, '../lib/PHPParser/Parser.php');
|
||||
|
||||
if (isset($_GET['debug'])) {
|
||||
echo 'Building debug parser. Output: "',
|
||||
trim(shell_exec('kmyacc -t -v -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
|
||||
'"', "\n";
|
||||
|
||||
if (!is_dir('../lib/PHPParser/Parser')) {
|
||||
mkdir('../lib/PHPParser/Parser');
|
||||
}
|
||||
rename(RESULT_FILE, '../lib/PHPParser/Parser/Debug.php');
|
||||
}
|
||||
|
||||
|
||||
unlink(TMP_FILE);
|
||||
|
||||
echo 'The following temporary preproprocessed grammar file was used:', "\n", $grammarCode;
|
||||
|
||||
echo '</pre>';
|
||||
|
||||
///////////////////////////////
|
||||
/// 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
|
||||
);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
/// 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);
|
||||
}
|
261
grammar/rebuildParsers.php
Normal file
261
grammar/rebuildParsers.php
Normal file
@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
$grammarFileToName = [
|
||||
__DIR__ . '/php5.y' => 'Php5',
|
||||
__DIR__ . '/php7.y' => 'Php7',
|
||||
];
|
||||
|
||||
$tokensFile = __DIR__ . '/tokens.y';
|
||||
$tokensTemplate = __DIR__ . '/tokens.template';
|
||||
$skeletonFile = __DIR__ . '/parser.template';
|
||||
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
||||
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
||||
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
||||
$tokensResultsFile = $resultDir . '/Tokens.php';
|
||||
|
||||
$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']);
|
||||
|
||||
///////////////////////////////
|
||||
/// 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 ///
|
||||
///////////////////
|
||||
|
||||
$tokens = file_get_contents($tokensFile);
|
||||
|
||||
foreach ($grammarFileToName as $grammarFile => $name) {
|
||||
echo "Building temporary $name grammar file.\n";
|
||||
|
||||
$grammarCode = file_get_contents($grammarFile);
|
||||
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);
|
||||
|
||||
$grammarCode = resolveNodes($grammarCode);
|
||||
$grammarCode = resolveMacros($grammarCode);
|
||||
$grammarCode = resolveStackAccess($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);
|
||||
|
||||
echo "Building token definition.\n";
|
||||
$output = execCmd("$kmyacc -m $tokensTemplate $tmpGrammarFile");
|
||||
rename($tmpResultFile, $tokensResultsFile);
|
||||
|
||||
if (!$optionKeepTmpGrammar) {
|
||||
unlink($tmpGrammarFile);
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
/// Preprocessing functions ///
|
||||
///////////////////////////////
|
||||
|
||||
function resolveNodes($code) {
|
||||
return preg_replace_callback(
|
||||
'~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
|
||||
function($matches) {
|
||||
// recurse
|
||||
$matches['params'] = resolveNodes($matches['params']);
|
||||
|
||||
$params = magicSplit(
|
||||
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
||||
$matches['params']
|
||||
);
|
||||
|
||||
$paramCode = '';
|
||||
foreach ($params as $param) {
|
||||
$paramCode .= $param . ', ';
|
||||
}
|
||||
|
||||
return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
|
||||
},
|
||||
$code
|
||||
);
|
||||
}
|
||||
|
||||
function resolveMacros($code) {
|
||||
return preg_replace_callback(
|
||||
'~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
|
||||
function($matches) {
|
||||
// recurse
|
||||
$matches['args'] = resolveMacros($matches['args']);
|
||||
|
||||
$name = $matches['name'];
|
||||
$args = magicSplit(
|
||||
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
||||
$matches['args']
|
||||
);
|
||||
|
||||
if ('attributes' == $name) {
|
||||
assertArgs(0, $args, $name);
|
||||
return '$this->startAttributeStack[#1] + $this->endAttributes';
|
||||
}
|
||||
|
||||
if ('stackAttributes' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
return '$this->startAttributeStack[' . $args[0] . ']'
|
||||
. ' + $this->endAttributeStack[' . $args[0] . ']';
|
||||
}
|
||||
|
||||
if ('init' == $name) {
|
||||
return '$$ = array(' . implode(', ', $args) . ')';
|
||||
}
|
||||
|
||||
if ('push' == $name) {
|
||||
assertArgs(2, $args, $name);
|
||||
|
||||
return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
|
||||
}
|
||||
|
||||
if ('pushNormalizing' == $name) {
|
||||
assertArgs(2, $args, $name);
|
||||
|
||||
return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
|
||||
. ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
|
||||
}
|
||||
|
||||
if ('toArray' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
|
||||
}
|
||||
|
||||
if ('parseVar' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return 'substr(' . $args[0] . ', 1)';
|
||||
}
|
||||
|
||||
if ('parseEncapsed' == $name) {
|
||||
assertArgs(3, $args, $name);
|
||||
|
||||
return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
|
||||
. ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
|
||||
}
|
||||
|
||||
if ('makeNop' == $name) {
|
||||
assertArgs(3, $args, $name);
|
||||
|
||||
return '$startAttributes = ' . $args[1] . ';'
|
||||
. ' if (isset($startAttributes[\'comments\']))'
|
||||
. ' { ' . $args[0] . ' = new Stmt\Nop($startAttributes + ' . $args[2] . '); }'
|
||||
. ' else { ' . $args[0] . ' = null; }';
|
||||
}
|
||||
|
||||
if ('makeZeroLengthNop' == $name) {
|
||||
assertArgs(2, $args, $name);
|
||||
|
||||
return '$startAttributes = ' . $args[1] . ';'
|
||||
. ' if (isset($startAttributes[\'comments\']))'
|
||||
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createCommentNopAttributes($startAttributes[\'comments\'])); }'
|
||||
. ' else { ' . $args[0] . ' = null; }';
|
||||
}
|
||||
|
||||
if ('strKind' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return '(' . $args[0] . '[0] === "\'" || (' . $args[0] . '[1] === "\'" && '
|
||||
. '(' . $args[0] . '[0] === \'b\' || ' . $args[0] . '[0] === \'B\')) '
|
||||
. '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
|
||||
}
|
||||
|
||||
if ('prependLeadingComments' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
||||
. 'if (!empty($attrs[\'comments\'])) {'
|
||||
. '$stmts[0]->setAttribute(\'comments\', '
|
||||
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
},
|
||||
$code
|
||||
);
|
||||
}
|
||||
|
||||
function assertArgs($num, $args, $name) {
|
||||
if ($num != count($args)) {
|
||||
die('Wrong argument count for ' . $name . '().');
|
||||
}
|
||||
}
|
||||
|
||||
function resolveStackAccess($code) {
|
||||
$code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
|
||||
$code = preg_replace('/#(\d+)/', '$$1', $code);
|
||||
return $code;
|
||||
}
|
||||
|
||||
function removeTrailingWhitespace($code) {
|
||||
$lines = explode("\n", $code);
|
||||
$lines = array_map('rtrim', $lines);
|
||||
return implode("\n", $lines);
|
||||
}
|
||||
|
||||
function ensureDirExists($dir) {
|
||||
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;
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
/// 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;
|
||||
}
|
17
grammar/tokens.template
Normal file
17
grammar/tokens.template
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
$meta #
|
||||
#semval($) $this->semValue
|
||||
#semval($,%t) $this->semValue
|
||||
#semval(%n) $this->stackPos-(%l-%n)
|
||||
#semval(%n,%t) $this->stackPos-(%l-%n)
|
||||
|
||||
namespace PhpParser\Parser;
|
||||
#include;
|
||||
|
||||
/* GENERATED file based on grammar/tokens.y */
|
||||
final class Tokens
|
||||
{
|
||||
#tokenval
|
||||
const %s = %n;
|
||||
#endtokenval
|
||||
}
|
112
grammar/tokens.y
Normal file
112
grammar/tokens.y
Normal file
@ -0,0 +1,112 @@
|
||||
/* We currently rely on the token ID mapping to be the same between PHP 5 and PHP 7 - so the same lexer can be used for
|
||||
* both. This is enforced by sharing this token file. */
|
||||
|
||||
%right T_THROW
|
||||
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
|
||||
%left ','
|
||||
%left T_LOGICAL_OR
|
||||
%left T_LOGICAL_XOR
|
||||
%left T_LOGICAL_AND
|
||||
%right T_PRINT
|
||||
%right T_YIELD
|
||||
%right T_DOUBLE_ARROW
|
||||
%right T_YIELD_FROM
|
||||
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL T_COALESCE_EQUAL
|
||||
%left '?' ':'
|
||||
%right T_COALESCE
|
||||
%left T_BOOLEAN_OR
|
||||
%left T_BOOLEAN_AND
|
||||
%left '|'
|
||||
%left '^'
|
||||
%left '&'
|
||||
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
|
||||
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
|
||||
%left T_SL T_SR
|
||||
%left '+' '-' '.'
|
||||
%left '*' '/' '%'
|
||||
%right '!'
|
||||
%nonassoc T_INSTANCEOF
|
||||
%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
|
||||
%right T_POW
|
||||
%right '['
|
||||
%nonassoc T_NEW T_CLONE
|
||||
%token T_EXIT
|
||||
%token T_IF
|
||||
%left T_ELSEIF
|
||||
%left T_ELSE
|
||||
%left T_ENDIF
|
||||
%token T_LNUMBER
|
||||
%token T_DNUMBER
|
||||
%token T_STRING
|
||||
%token T_STRING_VARNAME
|
||||
%token T_VARIABLE
|
||||
%token T_NUM_STRING
|
||||
%token T_INLINE_HTML
|
||||
%token T_ENCAPSED_AND_WHITESPACE
|
||||
%token T_CONSTANT_ENCAPSED_STRING
|
||||
%token T_ECHO
|
||||
%token T_DO
|
||||
%token T_WHILE
|
||||
%token T_ENDWHILE
|
||||
%token T_FOR
|
||||
%token T_ENDFOR
|
||||
%token T_FOREACH
|
||||
%token T_ENDFOREACH
|
||||
%token T_DECLARE
|
||||
%token T_ENDDECLARE
|
||||
%token T_AS
|
||||
%token T_SWITCH
|
||||
%token T_MATCH
|
||||
%token T_ENDSWITCH
|
||||
%token T_CASE
|
||||
%token T_DEFAULT
|
||||
%token T_BREAK
|
||||
%token T_CONTINUE
|
||||
%token T_GOTO
|
||||
%token T_FUNCTION
|
||||
%token T_FN
|
||||
%token T_CONST
|
||||
%token T_RETURN
|
||||
%token T_TRY
|
||||
%token T_CATCH
|
||||
%token T_FINALLY
|
||||
%token T_THROW
|
||||
%token T_USE
|
||||
%token T_INSTEADOF
|
||||
%token T_GLOBAL
|
||||
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
|
||||
%token T_VAR
|
||||
%token T_UNSET
|
||||
%token T_ISSET
|
||||
%token T_EMPTY
|
||||
%token T_HALT_COMPILER
|
||||
%token T_CLASS
|
||||
%token T_TRAIT
|
||||
%token T_INTERFACE
|
||||
%token T_EXTENDS
|
||||
%token T_IMPLEMENTS
|
||||
%token T_OBJECT_OPERATOR
|
||||
%token T_NULLSAFE_OBJECT_OPERATOR
|
||||
%token T_DOUBLE_ARROW
|
||||
%token T_LIST
|
||||
%token T_ARRAY
|
||||
%token T_CALLABLE
|
||||
%token T_CLASS_C
|
||||
%token T_TRAIT_C
|
||||
%token T_METHOD_C
|
||||
%token T_FUNC_C
|
||||
%token T_LINE
|
||||
%token T_FILE
|
||||
%token T_START_HEREDOC
|
||||
%token T_END_HEREDOC
|
||||
%token T_DOLLAR_OPEN_CURLY_BRACES
|
||||
%token T_CURLY_OPEN
|
||||
%token T_PAAMAYIM_NEKUDOTAYIM
|
||||
%token T_NAMESPACE
|
||||
%token T_NS_C
|
||||
%token T_DIR
|
||||
%token T_NS_SEPARATOR
|
||||
%token T_ELLIPSIS
|
||||
%token T_NAME_FULLY_QUALIFIED
|
||||
%token T_NAME_QUALIFIED
|
||||
%token T_NAME_RELATIVE
|
@ -1,857 +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
|
||||
%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_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 '(' expr ')' statement elseif_list else_single { $$ = Stmt_If[$3, [stmts: toArray($5), elseifs: $6, else: $7]]; }
|
||||
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
|
||||
{ $$ = Stmt_If[$3, [stmts: $6, elseifs: $7, else: $8]]; }
|
||||
| T_WHILE '(' expr ')' while_statement { $$ = Stmt_While[$3, $5]; }
|
||||
| T_DO statement T_WHILE '(' expr ')' ';' { $$ = Stmt_Do [$5, toArray($2)]; }
|
||||
| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
|
||||
{ $$ = Stmt_For[[init: $3, cond: $5, loop: $7, stmts: $9]]; }
|
||||
| T_SWITCH '(' expr ')' switch_case_list { $$ = Stmt_Switch[$3, $5]; }
|
||||
| T_BREAK ';' { $$ = 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]; }
|
||||
| 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 variable ')' foreach_statement
|
||||
{ $$ = Stmt_Foreach[$3, $5, [keyVar: null, byRef: false, stmts: $7]]; }
|
||||
| T_FOREACH '(' expr T_AS '&' variable ')' foreach_statement
|
||||
{ $$ = Stmt_Foreach[$3, $6, [keyVar: null, byRef: true, stmts: $8]]; }
|
||||
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW optional_ref variable ')' foreach_statement
|
||||
{ $$ = Stmt_Foreach[$3, $8, [keyVar: $5, byRef: $7, stmts: $10]]; }
|
||||
| T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt_Declare[$3, $5]; }
|
||||
| ';' { $$ = array(); /* means: no statement */ }
|
||||
| T_TRY '{' inner_statement_list '}' catches { $$ = Stmt_TryCatch[$3, $5]; }
|
||||
| T_THROW expr ';' { $$ = Stmt_Throw[$2]; }
|
||||
| T_GOTO T_STRING ';' { $$ = Stmt_Goto[$2]; }
|
||||
| T_STRING ':' { $$ = Stmt_Label[$1]; }
|
||||
;
|
||||
|
||||
catches:
|
||||
catch { init($1); }
|
||||
| catches catch { push($1, $2); }
|
||||
;
|
||||
|
||||
catch:
|
||||
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
|
||||
{ $$ = Stmt_Catch[$3, parseVar($4), $7]; }
|
||||
;
|
||||
|
||||
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 '(' expr ')' statement { $$ = Stmt_ElseIf[$3, toArray($5)]; }
|
||||
;
|
||||
|
||||
new_elseif_list:
|
||||
/* empty */ { init(); }
|
||||
| new_elseif_list new_elseif { push($1, $2); }
|
||||
;
|
||||
|
||||
new_elseif:
|
||||
T_ELSEIF '(' expr ')' ':' inner_statement_list { $$ = Stmt_ElseIf[$3, $6]; }
|
||||
;
|
||||
|
||||
else_single:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_ELSE statement { $$ = Stmt_Else[toArray($2)]; }
|
||||
;
|
||||
|
||||
new_else_single:
|
||||
/* empty */ { $$ = null; }
|
||||
| T_ELSE ':' inner_statement_list { $$ = Stmt_Else[$3]; }
|
||||
;
|
||||
|
||||
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:
|
||||
non_empty_argument_list { $$ = $1; }
|
||||
| /* empty */ { $$ = array(); }
|
||||
;
|
||||
|
||||
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; }
|
||||
| T_LIST '(' assignment_list ')' '=' expr { $$ = Expr_AssignList[$3, $6]; }
|
||||
| variable '=' expr { $$ = Expr_Assign[$1, $3]; }
|
||||
| variable '=' '&' variable { $$ = Expr_AssignRef[$1, $4]; }
|
||||
| variable '=' '&' new_expr { $$ = Expr_Assign[$1, $4]; } /* reference dropped intentially */
|
||||
| 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]; }
|
||||
| '(' expr ')' { $$ = $2; }
|
||||
/* 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 '(' variable ')' { $$ = 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 '(' expr ')' { $$ = Expr_Eval[$3]; }
|
||||
| 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; }
|
||||
| T_ARRAY '(' array_pair_list ')' { $$ = Expr_Array[$3]; }
|
||||
| '[' array_pair_list ']' { $$ = Expr_Array[$2]; }
|
||||
| '`' backticks_expr '`' { $$ = Expr_ShellExec[$2]; }
|
||||
| T_PRINT expr { $$ = Expr_Print[$2]; }
|
||||
| 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]]; }
|
||||
;
|
||||
|
||||
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, $3]; }
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' argument_list ')'
|
||||
{ $$ = Expr_StaticCall[$1, $3, $5]; }
|
||||
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '{' expr '}' '(' argument_list ')'
|
||||
{ $$ = Expr_StaticCall[$1, $4, $7]; }
|
||||
| static_property '(' argument_list ')' {
|
||||
if ($1 instanceof PHPParser_Node_Expr_StaticPropertyFetch) {
|
||||
$$ = Expr_StaticCall[$1->class, Expr_Variable[$1->name], $3];
|
||||
} 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, $3];
|
||||
$tmp->var = Expr_Variable[$tmp->var->name];
|
||||
} else {
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
| variable_without_objects '(' argument_list ')'
|
||||
{ $$ = Expr_FuncCall[$1, $3]; }
|
||||
| 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; }
|
||||
| '(' expr ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
backticks_expr:
|
||||
/* empty */ { $$ = array(); }
|
||||
| T_ENCAPSED_AND_WHITESPACE { $$ = array(Scalar_String::parseEscapeSequences($1, '`')); }
|
||||
| encaps_list { parseEncapsed($1, '`'); $$ = $1; }
|
||||
;
|
||||
|
||||
ctor_arguments:
|
||||
/* empty */ { $$ = array(); }
|
||||
| '(' argument_list ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
common_scalar:
|
||||
T_LNUMBER { $$ = Scalar_LNumber[Scalar_LNumber::parse($1)]; }
|
||||
| T_DNUMBER { $$ = Scalar_DNumber[Scalar_DNumber::parse($1)]; }
|
||||
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar_String::create($1, $attributes); }
|
||||
| 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 T_STRING { $$ = 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 T_STRING { $$ = Expr_ClassConstFetch[$1, $3]; }
|
||||
| '"' encaps_list '"'
|
||||
{ parseEncapsed($2, '"'); $$ = Scalar_Encapsed[$2]; }
|
||||
| T_START_HEREDOC encaps_list T_END_HEREDOC
|
||||
{ parseEncapsedDoc($2); $$ = Scalar_Encapsed[$2]; }
|
||||
;
|
||||
|
||||
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, $5]; }
|
||||
| object_access '(' argument_list ')' { $$ = Expr_FuncCall[$1, $3]; }
|
||||
| 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; }
|
||||
;
|
||||
|
||||
assignment_list:
|
||||
assignment_list ',' assignment_list_element { push($1, $3); }
|
||||
| assignment_list_element { init($1); }
|
||||
;
|
||||
|
||||
assignment_list_element:
|
||||
variable { $$ = $1; }
|
||||
| T_LIST '(' assignment_list ')' { $$ = $3; }
|
||||
| /* 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,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 and strings 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,77 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* "class" and "function" are reserved keywords, so the methods are defined as _class()
|
||||
* and _function() in the class and are made available as class() 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
|
||||
*/
|
||||
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 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 ('class' === $name) {
|
||||
return call_user_func_array(array($this, '_class'), $args);
|
||||
} elseif ('function' === $name) {
|
||||
return call_user_func_array(array($this, '_function'), $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 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,198 +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();
|
||||
|
||||
$this->newKeywords = array();
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.4.0RC1', '>=')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// new PHP 5.4 keywords
|
||||
$this->newKeywords += array(
|
||||
'callable' => PHPParser_Parser::T_CALLABLE,
|
||||
'insteadof' => PHPParser_Parser::T_INSTEADOF,
|
||||
'trait' => PHPParser_Parser::T_TRAIT,
|
||||
'__trait__' => PHPParser_Parser::T_TRAIT_C,
|
||||
);
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// new PHP 5.3 keywords
|
||||
$this->newKeywords += array(
|
||||
'__dir__' => PHPParser_Parser::T_DIR,
|
||||
'goto' => PHPParser_Parser::T_GOTO,
|
||||
'namespace' => PHPParser_Parser::T_NAMESPACE,
|
||||
'__namespace__' => PHPParser_Parser::T_NS_C,
|
||||
);
|
||||
}
|
||||
|
||||
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 array $vars List of variables to assign to
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_AssignList extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a list() assignment node.
|
||||
*
|
||||
* @param array $vars List of variables to assign to
|
||||
* @param PHPParser_Node_Expr $expr Expression
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(array $vars, PHPParser_Node_Expr $expr, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'vars' => $vars,
|
||||
'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_Stmt_FuncParam[] $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
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $var Variable
|
||||
*/
|
||||
class PHPParser_Node_Expr_Empty extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an empty() node.
|
||||
*
|
||||
* @param PHPParser_Node_Expr $var Variable
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(PHPParser_Node_Expr $var, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'var' => $var
|
||||
),
|
||||
$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_Equal extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs a equality comparison 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_ErrorSuppress extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an error suppress 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,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Expr $expr Expression
|
||||
*/
|
||||
class PHPParser_Node_Expr_Eval extends PHPParser_Node_Expr
|
||||
{
|
||||
/**
|
||||
* Constructs an eval() 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
|
||||
);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user