1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
|
#include "PHPDocVisitor.h"
#include "PHPEntityClass.h"
#include "PHPEntityFunction.h"
#include "PHPEntityFunctionAlias.h"
#include "PHPEntityNamespace.h"
#include "PHPEntityVariable.h"
#include "PHPLookupTable.h"
#include "PHPScannerTokens.h"
#include "PHPSourceFile.h"
#include "fileutils.h"
#include <unordered_set>
#include <wx/arrstr.h>
#include <wx/ffile.h>
#define NEXT_TOKEN_BREAK_IF_NOT(t, action) \
{ \
if(!NextToken(token)) \
break; \
if(token.type != t) { \
action; \
break; \
} \
}
PHPSourceFile::PHPSourceFile(const wxString& content, PHPLookupTable* lookup)
: m_text(content)
, m_parseFunctionBody(false)
, m_depth(0)
, m_reachedEOF(false)
, m_converter(NULL)
, m_lookup(lookup)
{
m_scanner = ::phpLexerNew(content, kPhpLexerOpt_ReturnComments);
}
PHPSourceFile::PHPSourceFile(const wxFileName& filename, PHPLookupTable* lookup)
: m_filename(filename)
, m_parseFunctionBody(false)
, m_depth(0)
, m_reachedEOF(false)
, m_converter(NULL)
, m_lookup(lookup)
{
// Filename is kept in absolute path
m_filename.MakeAbsolute();
wxString content;
if(FileUtils::ReadFileContent(filename, content, wxConvISO8859_1)) {
m_text.swap(content);
}
m_scanner = ::phpLexerNew(m_text, kPhpLexerOpt_ReturnComments);
}
PHPSourceFile::~PHPSourceFile()
{
if(m_scanner) {
::phpLexerDestroy(&m_scanner);
}
}
bool PHPSourceFile::IsInPHPSection(const wxString& buffer)
{
PHPScanner_t scanner = ::phpLexerNew(buffer);
if(!scanner)
return false;
phpLexerToken tok;
bool inPhp = false;
while(::phpLexerNext(scanner, tok)) {
if(::phpLexerIsPHPCode(scanner)) {
inPhp = true;
} else {
inPhp = false;
}
}
::phpLexerDestroy(&scanner);
return inPhp;
}
void PHPSourceFile::Parse(int exitDepth)
{
int retDepth = exitDepth;
phpLexerToken token;
while(NextToken(token)) {
switch(token.type) {
case '=':
m_lookBackTokens.clear();
break;
case '{':
m_lookBackTokens.clear();
break;
case '}':
m_lookBackTokens.clear();
if(m_depth == retDepth) {
return;
}
break;
case ';':
m_lookBackTokens.clear();
break;
case kPHP_T_VARIABLE:
if(!CurrentScope()->Is(kEntityTypeClass)) {
// A global variable
OnVariable(token);
}
break;
case kPHP_T_CATCH:
// found 'catch (...)'
OnCatch();
break;
case kPHP_T_PUBLIC:
case kPHP_T_PRIVATE:
case kPHP_T_PROTECTED: {
int visibility = token.type;
PHPEntityClass* cls = CurrentScope()->Cast<PHPEntityClass>();
if(cls) {
/// keep the current token
m_lookBackTokens.push_back(token);
// Now we have a small problem here:
// public can be a start for a member or a function
// we let the lexer run forward until it finds kPHP_T_VARIABLE (for variable)
// or kPHP_T_IDENTIFIER
int what = ReadUntilFoundOneOf(kPHP_T_VARIABLE, kPHP_T_FUNCTION, token);
if(what == kPHP_T_VARIABLE) {
// A variable
PHPEntityBase::Ptr_t member(new PHPEntityVariable());
member->SetFilename(m_filename.GetFullPath());
member->Cast<PHPEntityVariable>()->SetVisibility(visibility);
member->Cast<PHPEntityVariable>()->SetFullName(token.Text());
size_t flags = LookBackForVariablesFlags();
member->Cast<PHPEntityVariable>()->SetFlag(kVar_Member);
member->Cast<PHPEntityVariable>()->SetFlag(kVar_Const, flags & kVar_Const);
member->Cast<PHPEntityVariable>()->SetFlag(kVar_Static, flags & kVar_Static);
member->Cast<PHPEntityVariable>()->SetLine(token.lineNumber);
CurrentScope()->AddChild(member);
// Handle member assignment
// public $memberVar = new Something();
// for such cases, assign $memberVar type of Something()
phpLexerToken t;
if(!NextToken(t)) {
// EOF
return;
}
if(t.type == '=') {
// assignment
wxString expr;
if(!ReadExpression(expr)) {
return;
}
// Optimize 'new ClassName(..)' expression
if(expr.StartsWith("new")) {
expr = expr.Mid(3);
expr.Trim().Trim(false);
expr = expr.BeforeFirst('(');
expr.Trim().Trim(false);
member->Cast<PHPEntityVariable>()->SetTypeHint(MakeIdentifierAbsolute(expr));
} else {
// keep the expression
member->Cast<PHPEntityVariable>()->SetExpressionHint(expr);
}
} else {
// restore the token
UngetToken(t);
if(!ConsumeUntil(';'))
return;
}
} else if(what == kPHP_T_FUNCTION) {
// A function...
OnFunction();
m_lookBackTokens.clear();
}
}
break;
}
case kPHP_T_DEFINE:
// Define statement
OnDefine(token);
break;
case kPHP_T_CONST:
OnConstant(token);
break;
case kPHP_T_REQUIRE:
case kPHP_T_REQUIRE_ONCE:
case kPHP_T_INCLUDE:
case kPHP_T_INCLUDE_ONCE:
// Handle include files
m_lookBackTokens.clear();
break;
case kPHP_T_FOREACH:
// found "foreach" statement
OnForEach();
m_lookBackTokens.clear();
break;
case kPHP_T_USE:
// Found outer 'use' statement - construct the alias table
if(Class()) {
// inside a class, this means that this is a 'use <trait>;'
OnUseTrait();
} else {
// alias table
OnUse();
}
m_lookBackTokens.clear();
break;
case kPHP_T_CLASS:
case kPHP_T_INTERFACE:
case kPHP_T_TRAIT:
// Found class
OnClass(token);
m_lookBackTokens.clear();
break;
case kPHP_T_NAMESPACE:
// Found a namespace
OnNamespace();
m_lookBackTokens.clear();
break;
case kPHP_T_FUNCTION:
// Found function
OnFunction();
m_lookBackTokens.clear();
break;
default:
// Keep the token
break;
}
}
PhaseTwo();
}
void PHPSourceFile::OnUse()
{
wxString fullname, alias, temp;
phpLexerToken token;
bool cont = true;
while(cont && NextToken(token)) {
switch(token.type) {
case ',':
case ';': {
if(fullname.IsEmpty()) {
// no full name yet
fullname.swap(temp);
} else if(alias.IsEmpty()) {
alias.swap(temp);
}
if(alias.IsEmpty()) {
// no alias provided, use the last part of the fullname
alias = fullname.AfterLast('\\');
}
if(!fullname.IsEmpty() && !alias.IsEmpty()) {
// Use namespace is alway refered as fullpath namespace
// So writing:
// use Zend\Mvc\Controll\Action;
// is equal for writing:
// use \Zend\Mvc\Controll\Action;
// For simplicitiy, we change it to fully qualified path
// so parsing is easier
if(!fullname.StartsWith("\\")) {
fullname.Prepend("\\");
}
m_aliases.insert(std::make_pair(alias, MakeIdentifierAbsolute(fullname)));
}
temp.clear();
fullname.clear();
alias.clear();
if(token.type == ';') {
cont = false;
}
} break;
case kPHP_T_AS: {
fullname.swap(temp);
temp.clear();
} break;
default:
temp << token.Text();
break;
}
}
}
void PHPSourceFile::OnNamespace()
{
// Read until we find the line delimiter ';' or EOF found
wxString path;
phpLexerToken token;
while(NextToken(token)) {
if(token.type == ';') {
break;
}
// Make sure that the namespace path is alway set in absolute path
// i.e. starts with kPHP_T_NS_SEPARATOR
if(path.IsEmpty() && token.type != kPHP_T_NS_SEPARATOR) {
path << "\\";
}
path << token.Text();
}
if(m_scopes.empty()) {
// no scope is set, push the global scope
m_scopes.push_back(PHPEntityBase::Ptr_t(new PHPEntityNamespace()));
PHPEntityNamespace* ns = CurrentScope()->Cast<PHPEntityNamespace>();
if(ns) {
ns->SetFullName(path); // Global namespace
}
} else {
// PHP parsing error... (namespace must be the first thing on the file)
}
}
void PHPSourceFile::OnFunction()
{
// read the next token
phpLexerToken token;
if(!NextToken(token)) {
return;
}
bool funcReturnRef = false;
if(token.type == '&') {
funcReturnRef = true;
if(!NextToken(token)) {
return;
}
}
PHPEntityFunction* func(NULL);
int funcDepth(0);
if(token.type == kPHP_T_IDENTIFIER) {
// the function name
func = new PHPEntityFunction();
func->SetFullName(token.Text());
func->SetLine(token.lineNumber);
} else if(token.type == '(') {
funcDepth = 1; // Since we already consumed the open brace
// anonymous function
func = new PHPEntityFunction();
func->SetLine(token.lineNumber);
}
if(!func)
return;
PHPEntityBase::Ptr_t funcPtr(func);
if(funcReturnRef) {
funcPtr->SetFlag(kFunc_ReturnReference);
}
// add the function to the current scope
CurrentScope()->AddChild(funcPtr);
// Set the function as the current scope
m_scopes.push_back(funcPtr);
// update function attributes
ParseFunctionSignature(funcDepth);
func->SetFlags(LookBackForFunctionFlags());
if(LookBackTokensContains(kPHP_T_ABSTRACT) || // The 'abstract modifier was found for this this function
(funcPtr->Parent() && funcPtr->Parent()->Is(kEntityTypeClass) &&
funcPtr->Parent()->Cast<PHPEntityClass>()->IsInterface())) // We are inside an interface
{
// Mark this function as an abstract function
func->SetFlags(func->GetFlags() | kFunc_Abstract);
}
if(func->HasFlag(kFunc_Abstract)) {
// an abstract function - it has no body
if(!ConsumeUntil(';')) {
// could not locate the function delimiter, remove it from the stack
// we probably reached EOF here
m_scopes.pop_back();
}
} else {
if(!NextToken(token))
return;
if(token.type == ':') {
// PHP 7 signature type
// function foobar(...) : RETURN_TYPE
if(!NextToken(token))
return;
if(token.type == '?') {
// PHP 7.1 nullable return
funcPtr->SetFlag(kFunc_ReturnNullable);
} else {
UngetToken(token);
}
wxString returnValuetype = ReadFunctionReturnValueFromSignature();
if(returnValuetype.IsEmpty())
return; // parse error
func->SetReturnValue(returnValuetype);
} else {
// untake the token and place it back on the "unget" list
UngetToken(token);
}
if(ReadUntilFound('{', token)) {
// found the function body starting point
if(IsParseFunctionBody()) {
ParseFunctionBody();
} else {
// Consume the function body
ConsumeFunctionBody();
}
} else {
// could not locate the open brace!
// remove this function from the stack
m_scopes.pop_back();
}
}
// Remove the current function from the scope list
if(!m_reachedEOF) {
m_scopes.pop_back();
}
m_lookBackTokens.clear();
}
PHPEntityBase::Ptr_t PHPSourceFile::CurrentScope()
{
if(m_scopes.empty()) {
// no scope is set, push the global scope
m_scopes.push_back(PHPEntityBase::Ptr_t(new PHPEntityNamespace()));
CurrentScope()->SetFullName("\\"); // Global namespace
}
return m_scopes.back();
}
size_t PHPSourceFile::LookBackForFunctionFlags()
{
size_t flags(0);
for(size_t i = 0; i < m_lookBackTokens.size(); ++i) {
const phpLexerToken& tok = m_lookBackTokens.at(i);
if(tok.type == kPHP_T_ABSTRACT) {
flags |= kFunc_Abstract;
} else if(tok.type == kPHP_T_FINAL) {
flags |= kFunc_Final;
} else if(tok.type == kPHP_T_STATIC) {
flags |= kFunc_Static;
} else if(tok.type == kPHP_T_PUBLIC) {
flags |= kFunc_Public;
flags &= ~kFunc_Private;
flags &= ~kFunc_Protected;
} else if(tok.type == kPHP_T_PRIVATE) {
flags |= kFunc_Private;
flags &= ~kFunc_Public;
flags &= ~kFunc_Protected;
} else if(tok.type == kPHP_T_PROTECTED) {
flags |= kFunc_Protected;
flags &= ~kFunc_Public;
flags &= ~kFunc_Private;
}
}
return flags;
}
void PHPSourceFile::ParseFunctionSignature(int startingDepth)
{
phpLexerToken token;
if(startingDepth == 0) {
// loop until we find the open brace
while(NextToken(token)) {
if(token.type == '(') {
++startingDepth;
break;
}
}
if(startingDepth == 0)
return;
}
// at this point the 'depth' is 1, as we already read the open brace
int depth = 1;
wxString typeHint;
wxString defaultValue;
wxString name;
PHPEntityVariable* var(NULL);
bool collectingDefaultValue = false;
while(NextToken(token)) {
switch(token.type) {
case kPHP_T_VARIABLE:
if(!var) {
// var can be non null if we are parsing PHP-7 function arguments
// with type-hinting
var = new PHPEntityVariable();
}
name = token.Text();
var->SetLine(token.lineNumber);
var->SetFilename(m_filename);
// Mark this variable as function argument
var->SetFlag(kVar_FunctionArg);
if(name.StartsWith("&")) {
var->SetIsReference(true);
name.Remove(0, 1);
} else if(typeHint.EndsWith("&")) {
var->SetIsReference(true);
typeHint.RemoveLast();
}
var->SetFullName(name);
var->SetTypeHint(MakeIdentifierAbsolute(typeHint));
break;
case '(':
depth++;
if(collectingDefaultValue) {
defaultValue << "(";
}
break;
case ')':
depth--;
// if the depth goes under 1 - we are done
if(depth < 1) {
if(var) {
var->SetDefaultValue(defaultValue);
CurrentScope()->AddChild(PHPEntityBase::Ptr_t(var));
}
return;
} else if(depth) {
defaultValue << token.Text();
}
break;
case '=':
// default value
collectingDefaultValue = true;
break;
case ',':
if(var) {
var->SetDefaultValue(defaultValue);
CurrentScope()->AddChild(PHPEntityBase::Ptr_t(var));
}
var = NULL;
typeHint.Clear();
defaultValue.Clear();
collectingDefaultValue = false;
break;
case '?':
if(!var) {
var = new PHPEntityVariable();
}
var->SetIsNullable(true);
break;
case kPHP_T_NS_SEPARATOR:
case kPHP_T_IDENTIFIER:
if(!var) {
// PHP-7 type hinting function arguments
var = new PHPEntityVariable();
UngetToken(token);
typeHint = ReadType();
if(!typeHint.IsEmpty()) {
break;
}
}
// all "else" cases simply fall into the default case
default:
if(collectingDefaultValue) {
defaultValue << token.Text();
} else {
typeHint << token.Text();
}
break;
}
}
}
void PHPSourceFile::PrintStdout()
{
// print the alias table
wxPrintf("Alias table:\n");
wxPrintf("===========\n");
std::map<wxString, wxString>::iterator iter = m_aliases.begin();
for(; iter != m_aliases.end(); ++iter) {
wxPrintf("%s => %s\n", iter->first, iter->second);
}
wxPrintf("===========\n");
if(m_scopes.empty())
return;
m_scopes.front()->PrintStdout(0);
}
bool PHPSourceFile::ReadUntilFound(int delim, phpLexerToken& token)
{
// loop until we find the open brace
while(NextToken(token)) {
if(token.type == delim) {
return true;
}
}
return false;
}
void PHPSourceFile::ConsumeFunctionBody()
{
int depth = m_depth;
phpLexerToken token;
while(NextToken(token)) {
switch(token.type) {
case '}':
if(m_depth < depth) {
return;
}
break;
default:
break;
}
}
}
void PHPSourceFile::ParseFunctionBody()
{
m_lookBackTokens.clear();
// when we reach the current depth-1 -> leave
int exitDepth = m_depth - 1;
phpLexerToken token;
PHPEntityBase::Ptr_t var(NULL);
while(NextToken(token)) {
switch(token.type) {
case '{':
m_lookBackTokens.clear();
break;
case '}':
m_lookBackTokens.clear();
if(m_depth == exitDepth) {
return;
}
break;
case ';':
m_lookBackTokens.clear();
break;
case kPHP_T_CATCH:
OnCatch();
break;
case kPHP_T_VARIABLE: {
OnVariable(token);
break;
} break;
default:
break;
}
}
}
wxString PHPSourceFile::ReadType()
{
bool cont = true;
wxString type;
phpLexerToken token;
while(cont && NextToken(token)) {
switch(token.type) {
case kPHP_T_IDENTIFIER:
type << token.Text();
break;
case kPHP_T_NS_SEPARATOR:
type << token.Text();
break;
default:
// restore the token so next call to NextToken
// will pick it up again
UngetToken(token);
cont = false;
break;
}
}
type = MakeTypehintAbsolute(type);
return type;
}
PHPEntityBase::Ptr_t PHPSourceFile::Namespace()
{
if(m_scopes.empty()) {
return CurrentScope();
}
return *m_scopes.begin();
}
wxString PHPSourceFile::LookBackForTypeHint()
{
if(m_lookBackTokens.empty())
return wxEmptyString;
wxArrayString tokens;
for(int i = (int)m_lookBackTokens.size() - 1; i >= 0; --i) {
if(m_lookBackTokens.at(i).type == kPHP_T_IDENTIFIER || m_lookBackTokens.at(i).type == kPHP_T_NS_SEPARATOR) {
tokens.Insert(m_lookBackTokens.at(i).Text(), 0);
} else {
break;
}
}
wxString type;
for(size_t i = 0; i < tokens.GetCount(); ++i) {
type << tokens.Item(i);
}
return type;
}
void PHPSourceFile::PhaseTwo()
{
// Visit each entity found during the parsing stage
// and try to match it with its phpdoc comment block (we do this by line number)
// the visitor also makes sure that each entity is properly assigned with the current file name
PHPDocVisitor visitor(*this, m_comments);
visitor.Visit(Namespace());
}
bool PHPSourceFile::NextToken(phpLexerToken& token)
{
bool res = ::phpLexerNext(m_scanner, token);
if(res && (token.type == kPHP_T_C_COMMENT)) {
m_comments.push_back(token);
// We keep comments made in the class body
if(!m_scopes.empty() && CurrentScope()->Is(kEntityTypeClass)) {
PHPDocVar::Ptr_t var(new PHPDocVar(*this, token.Text()));
if(var->IsOk()) {
var->SetLineNumber(token.lineNumber);
// this comment is a @var comment
CurrentScope()->Cast<PHPEntityClass>()->AddVarPhpDoc(var);
}
}
}
if(token.type == '{') {
m_depth++;
} else if(token.type == '}') {
m_depth--;
} else if(token.type == ';') {
m_lookBackTokens.clear();
}
if(!res)
m_reachedEOF = true;
if(res)
m_lookBackTokens.push_back(token);
return res;
}
wxString PHPSourceFile::MakeIdentifierAbsolute(const wxString& type) { return DoMakeIdentifierAbsolute(type, false); }
void PHPSourceFile::OnClass(const phpLexerToken& tok)
{
wxString classDoc;
const phpLexerToken& prevToken = GetPreviousToken();
if(!prevToken.IsNull() && prevToken.IsDocComment()) {
classDoc = prevToken.Text();
}
// A "complex" example: class A extends BaseClass implements C, D {}
bool isAbstractClass = LookBackTokensContains(kPHP_T_ABSTRACT);
// Read until we get the class name
phpLexerToken token;
while(NextToken(token)) {
if(token.IsAnyComment())
continue;
if(token.type != kPHP_T_IDENTIFIER) {
// expecting the class name
return;
}
break;
}
// create new class entity
PHPEntityBase::Ptr_t klass(new PHPEntityClass());
klass->SetFilename(m_filename.GetFullPath());
klass->SetDocComment(classDoc);
PHPEntityClass* pClass = klass->Cast<PHPEntityClass>();
// Is the class an interface?
pClass->SetIsInterface(tok.type == kPHP_T_INTERFACE);
pClass->SetIsAbstractClass(isAbstractClass);
pClass->SetIsTrait(tok.type == kPHP_T_TRAIT);
pClass->SetFullName(PrependCurrentScope(token.Text()));
pClass->SetLine(token.lineNumber);
while(NextToken(token)) {
if(token.IsAnyComment())
continue;
switch(token.type) {
case kPHP_T_EXTENDS: {
// inheritance
wxString extends = ReadExtends();
if(extends.IsEmpty())
return;
// No need to call 'MakeIdentifierAbsolute' it was called internally by
pClass->SetExtends(extends);
} break;
case kPHP_T_IMPLEMENTS: {
wxArrayString implements;
ReadImplements(implements);
pClass->SetImplements(implements);
} break;
case '{': {
// entering the class body
// add the current class to the current scope
CurrentScope()->AddChild(klass);
m_scopes.push_back(klass);
Parse(m_depth - 1);
if(!m_reachedEOF) {
m_scopes.pop_back();
}
return;
}
default:
break;
}
}
}
bool PHPSourceFile::ReadCommaSeparatedIdentifiers(int delim, wxArrayString& list)
{
phpLexerToken token;
wxString temp;
while(NextToken(token)) {
if(token.IsAnyComment())
continue;
if(token.type == delim) {
if(!temp.IsEmpty() && list.Index(temp) == wxNOT_FOUND) {
list.Add(MakeIdentifierAbsolute(temp));
}
UngetToken(token);
return true;
}
switch(token.type) {
case ',':
if(list.Index(temp) == wxNOT_FOUND) {
list.Add(MakeIdentifierAbsolute(temp));
}
temp.clear();
break;
default:
temp << token.Text();
break;
}
}
return false;
}
bool PHPSourceFile::ConsumeUntil(int delim)
{
phpLexerToken token;
while(NextToken(token)) {
if(token.type == delim) {
return true;
}
}
return false;
}
bool PHPSourceFile::ReadExpression(wxString& expression)
{
expression.clear();
phpLexerToken token;
int depth(0);
while(NextToken(token)) {
if(token.type == ';') {
return true;
} else if(token.type == '{') {
UngetToken(token);
return true;
}
switch(token.type) {
case kPHP_T_FUNCTION:
OnFunction();
break;
case kPHP_T_REQUIRE:
case kPHP_T_REQUIRE_ONCE:
expression.clear();
return false;
case kPHP_T_STRING_CAST:
case kPHP_T_CONSTANT_ENCAPSED_STRING:
case kPHP_T_C_COMMENT:
case kPHP_T_CXX_COMMENT:
// skip comments and strings
break;
case '(':
depth++;
expression << "(";
break;
case ')':
depth--;
if(depth == 0) {
expression << ")";
}
break;
case kPHP_T_NEW:
if(depth == 0) {
expression << token.Text() << " ";
}
break;
default:
if(depth == 0) {
expression << token.Text();
}
break;
}
}
// reached EOF
return false;
}
void PHPSourceFile::UngetToken(const phpLexerToken& token)
{
::phpLexerUnget(m_scanner);
// undo any depth / comments
if(token.type == '{') {
m_depth--;
} else if(token.type == '}') {
m_depth++;
} else if(token.type == kPHP_T_C_COMMENT && !m_comments.empty()) {
m_comments.erase(m_comments.begin() + m_comments.size() - 1);
}
}
const PHPEntityBase* PHPSourceFile::Class()
{
PHPEntityBase::Ptr_t curScope = CurrentScope();
PHPEntityBase* pScope = curScope.Get();
while(pScope) {
PHPEntityClass* cls = pScope->Cast<PHPEntityClass>();
if(cls) {
// this scope is a class
return pScope;
}
pScope = pScope->Parent();
}
return NULL;
}
int PHPSourceFile::ReadUntilFoundOneOf(int delim1, int delim2, phpLexerToken& token)
{
// loop until we find the open brace
while(NextToken(token)) {
if(token.type == delim1) {
return delim1;
} else if(token.type == delim2) {
return delim2;
}
}
return wxNOT_FOUND;
}
bool PHPSourceFile::LookBackTokensContains(int type) const
{
for(size_t i = 0; i < m_lookBackTokens.size(); ++i) {
if(m_lookBackTokens.at(i).type == type)
return true;
}
return false;
}
size_t PHPSourceFile::LookBackForVariablesFlags()
{
size_t flags(kVar_Public);
for(size_t i = 0; i < m_lookBackTokens.size(); ++i) {
const phpLexerToken& tok = m_lookBackTokens.at(i);
if(tok.type == kPHP_T_STATIC) {
flags |= kVar_Static;
} else if(tok.type == kPHP_T_CONST) {
flags |= kVar_Const;
} else if(tok.type == kPHP_T_PUBLIC) {
flags |= kVar_Public;
flags &= ~kVar_Private;
flags &= ~kVar_Protected;
} else if(tok.type == kPHP_T_PRIVATE) {
flags |= kVar_Private;
flags &= ~kVar_Public;
flags &= ~kVar_Protected;
} else if(tok.type == kPHP_T_PROTECTED) {
flags |= kVar_Protected;
flags &= ~kVar_Private;
flags &= ~kVar_Public;
}
}
return flags;
}
void PHPSourceFile::OnVariable(const phpLexerToken& tok)
{
PHPEntityBase::Ptr_t var(new PHPEntityVariable());
var->SetFullName(tok.Text());
var->SetFilename(m_filename.GetFullPath());
var->SetLine(tok.lineNumber);
if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
CurrentScope()->AddChild(var);
}
if(!ReadVariableInitialization(var)) {
m_lookBackTokens.clear();
return;
}
}
bool PHPSourceFile::ReadVariableInitialization(PHPEntityBase::Ptr_t var)
{
phpLexerToken token;
if(!NextToken(token)) {
return false;
}
if(token.type != '=') {
// restore the token
UngetToken(token);
return false;
}
wxString expr;
if(!ReadExpression(expr)) {
return false; // EOF
}
// Optimize 'new ClassName(..)' expression
if(expr.StartsWith("new")) {
expr = expr.Mid(3);
expr.Trim().Trim(false);
expr = expr.BeforeFirst('(');
expr.Trim().Trim(false);
var->Cast<PHPEntityVariable>()->SetTypeHint(MakeIdentifierAbsolute(expr));
} else {
// keep the expression
var->Cast<PHPEntityVariable>()->SetExpressionHint(expr);
}
return true;
}
PHPEntityBase::List_t PHPSourceFile::GetAliases() const
{
PHPEntityBase::List_t aliases;
std::map<wxString, wxString>::const_iterator iter = m_aliases.begin();
for(; iter != m_aliases.end(); ++iter) {
// wrap each alias with class entity
PHPEntityBase::Ptr_t klass(new PHPEntityClass());
klass->SetFullName(iter->second);
klass->SetShortName(iter->first);
klass->SetFilename(GetFilename());
aliases.push_back(klass);
}
return aliases;
}
void PHPSourceFile::OnDefine(const phpLexerToken& tok)
{
phpLexerToken token;
if(!NextToken(token))
return; // EOF
if(token.type != '(') {
ConsumeUntil(';');
return;
}
if(!NextToken(token))
return; // EOF
if(token.type != kPHP_T_CONSTANT_ENCAPSED_STRING) {
ConsumeUntil(';');
return;
}
// Remove the quotes
wxString varName = token.Text();
if((varName.StartsWith("\"") && varName.EndsWith("\"")) || (varName.StartsWith("'") && varName.EndsWith("'"))) {
varName.Remove(0, 1);
varName.RemoveLast();
// define() defines constants exactly as it was instructed
// i.e. it does not take the current namespace into consideration
PHPEntityBase::Ptr_t var(new PHPEntityVariable());
// Convert the variable into fullpath + relative name
if(!varName.StartsWith("\\")) {
varName.Prepend("\\");
}
wxString shortName = varName.AfterLast('\\');
var->SetFullName(varName);
var->SetShortName(shortName);
var->SetFlag(kVar_Define);
var->SetFilename(GetFilename());
var->SetLine(tok.lineNumber);
// We keep the defines in a special list
// this is because 'define' does not obay to the current scope
m_defines.push_back(var);
}
// Always consume the 'define' statement
ConsumeUntil(';');
}
void PHPSourceFile::OnUseTrait()
{
PHPEntityBase::Ptr_t clas = CurrentScope();
if(!clas)
return;
// Collect the identifiers followed the 'use' statement
wxArrayString identifiers;
wxString tempname;
phpLexerToken token;
while(NextToken(token)) {
switch(token.type) {
case ',': {
if(!tempname.IsEmpty()) {
identifiers.Add(MakeIdentifierAbsolute(tempname));
}
tempname.clear();
} break;
case '{': {
// we are looking at a case like:
// use A, B { ... }
if(!tempname.IsEmpty()) {
identifiers.Add(MakeIdentifierAbsolute(tempname));
ParseUseTraitsBody();
}
tempname.clear();
// add the traits as list of 'extends'
clas->Cast<PHPEntityClass>()->SetTraits(identifiers);
return;
} break;
case ';': {
if(!tempname.IsEmpty()) {
identifiers.Add(MakeIdentifierAbsolute(tempname));
}
tempname.clear();
// add the traits as list of 'extends'
clas->Cast<PHPEntityClass>()->SetTraits(identifiers);
return;
} break;
default:
tempname << token.Text();
break;
}
}
}
void PHPSourceFile::OnCatch()
{
// Read until we find the kPHP_T_VARIABLE
bool cont(true);
phpLexerToken token;
wxString typehint;
wxString varname;
while(cont && NextToken(token)) {
switch(token.type) {
case kPHP_T_VARIABLE:
cont = false;
varname = token.Text();
break;
case kPHP_T_IDENTIFIER:
case kPHP_T_NS_SEPARATOR:
typehint << token.Text();
break;
default:
break;
}
}
if(!varname.IsEmpty()) {
// we found the variable
PHPEntityBase::Ptr_t var(new PHPEntityVariable());
var->SetFullName(varname);
var->SetFilename(m_filename.GetFullPath());
var->SetLine(token.lineNumber);
var->Cast<PHPEntityVariable>()->SetTypeHint(MakeIdentifierAbsolute(typehint));
// add the variable to the current scope
if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
CurrentScope()->AddChild(var);
}
}
}
wxString PHPSourceFile::ReadExtends()
{
wxString type;
phpLexerToken token;
while(NextToken(token)) {
if(token.type == kPHP_T_IDENTIFIER || token.type == kPHP_T_NS_SEPARATOR) {
type << token.Text();
} else {
UngetToken(token);
break;
}
}
type = MakeIdentifierAbsolute(type);
return type;
}
void PHPSourceFile::ReadImplements(wxArrayString& impls)
{
wxString type;
phpLexerToken token;
while(NextToken(token)) {
switch(token.type) {
case kPHP_T_IDENTIFIER:
case kPHP_T_NS_SEPARATOR:
type << token.Text();
break;
case ',':
// More to come
if(!type.IsEmpty()) {
wxString fullyQualifiedType = MakeIdentifierAbsolute(type);
if(impls.Index(fullyQualifiedType) == wxNOT_FOUND) {
impls.Add(fullyQualifiedType);
}
type.clear();
}
break;
default:
// unexpected token
if(!type.IsEmpty()) {
wxString fullyQualifiedType = MakeIdentifierAbsolute(type);
if(impls.Index(fullyQualifiedType) == wxNOT_FOUND) {
impls.Add(fullyQualifiedType);
}
type.clear();
}
UngetToken(token);
return;
}
}
}
/*foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement*/
void PHPSourceFile::OnForEach()
{
// read until the "as" keyword
phpLexerToken token;
if(!ReadUntilFound(kPHP_T_AS, token))
return;
// Found the "as" key word and consumed it
if(!NextToken(token))
return;
phpLexerToken peekToken;
if(!NextToken(peekToken))
return;
// Ensure we got a variable
if(token.type != kPHP_T_VARIABLE)
return;
// Add the key
{
PHPEntityBase::Ptr_t var(new PHPEntityVariable());
var->SetFullName(token.Text());
var->SetFilename(m_filename.GetFullPath());
var->SetLine(token.lineNumber);
if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
CurrentScope()->AddChild(var);
}
}
// Check to see if we are using the syntax of:
// foreach (array_expression as $key => $value)
if(peekToken.type == kPHP_T_DOUBLE_ARROW) {
if(!NextToken(token) || token.type != kPHP_T_VARIABLE) {
return;
}
// Add the value as well
// Create a new variable
PHPEntityBase::Ptr_t var(new PHPEntityVariable());
var->SetFullName(token.Text());
var->SetFilename(m_filename.GetFullPath());
var->SetLine(token.lineNumber);
if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
CurrentScope()->AddChild(var);
}
} else {
UngetToken(peekToken);
}
}
void PHPSourceFile::ParseUseTraitsBody()
{
wxString fullname, alias, temp;
phpLexerToken token;
bool cont = true;
while(cont && NextToken(token)) {
switch(token.type) {
case '}': {
cont = false;
} break;
case ',':
case ';': {
if(fullname.IsEmpty()) {
// no full name yet
fullname.swap(temp);
} else if(alias.IsEmpty()) {
alias.swap(temp);
}
if(alias.IsEmpty()) {
// no alias provided, use the last part of the fullname
alias = fullname.AfterLast('\\');
}
if(!fullname.IsEmpty() && !alias.IsEmpty()) {
// Use namespace is alway refered as fullpath namespace
// So writing:
// use Zend\Mvc\Controll\Action;
// is equal for writing:
// use \Zend\Mvc\Controll\Action;
// For simplicitiy, we change it to fully qualified path
// so parsing is easier
if(!fullname.StartsWith("\\")) {
fullname.Prepend("\\");
}
PHPEntityBase::Ptr_t funcAlias(new PHPEntityFunctionAlias());
funcAlias->Cast<PHPEntityFunctionAlias>()->SetRealname(MakeIdentifierAbsolute(fullname));
funcAlias->Cast<PHPEntityFunctionAlias>()->SetScope(CurrentScope()->GetFullName());
funcAlias->SetShortName(alias);
funcAlias->SetFullName(CurrentScope()->GetFullName() + "\\" + alias);
funcAlias->SetFilename(GetFilename());
funcAlias->SetLine(token.lineNumber);
CurrentScope()->AddChild(funcAlias);
}
temp.clear();
fullname.clear();
alias.clear();
} break;
case kPHP_T_PAAMAYIM_NEKUDOTAYIM: {
// Convert "::" into "\\"
temp << "\\";
} break;
case kPHP_T_AS: {
fullname.swap(temp);
temp.clear();
} break;
case kPHP_T_INSTEADOF: {
// For now, we are not interested in
// A insteadof b; statements, so just clear the collected data so far
fullname.clear();
temp.clear();
alias.clear();
if(!ConsumeUntil(';'))
return;
} break;
default:
temp << token.Text();
break;
}
}
}
void PHPSourceFile::OnConstant(const phpLexerToken& tok)
{
// Parse constant line (possibly multiple constants)
phpLexerToken token;
PHPEntityBase::Ptr_t member;
while(NextToken(token)) {
if(token.type == '=') {
// The next value should contain the constant value
wxString constantValue;
while(NextToken(token)) {
if(token.type == ';') {
UngetToken(token);
break;
} else if(token.type == ',') {
break;
}
constantValue << token.Text();
}
if(member && !constantValue.IsEmpty()) {
// Keep the constant value, we will be using it later for tooltip
member->Cast<PHPEntityVariable>()->SetDefaultValue(constantValue);
}
}
if(token.type == ';') {
if(member) {
CurrentScope()->AddChild(member);
break;
}
} else if(token.type == ',') {
if(member) {
CurrentScope()->AddChild(member);
member.Reset(NULL);
}
} else if(token.type == kPHP_T_IDENTIFIER) {
// found the constant name
member.Reset(new PHPEntityVariable());
member->Cast<PHPEntityVariable>()->SetFlag(kVar_Const);
member->Cast<PHPEntityVariable>()->SetFlag(kVar_Member);
member->SetFullName(token.Text());
member->SetLine(token.lineNumber);
member->SetFilename(m_filename.GetFullPath());
} else {
// do nothing
}
}
}
phpLexerToken& PHPSourceFile::GetPreviousToken()
{
static phpLexerToken NullToken;
if(m_lookBackTokens.size() >= 2) {
// The last token in the list is the current one. We want the previous one
return m_lookBackTokens.at(m_lookBackTokens.size() - 2);
}
return NullToken;
}
wxString PHPSourceFile::ReadFunctionReturnValueFromSignature()
{
bool cont = true;
wxString type;
phpLexerToken token;
while(cont && NextToken(token)) {
switch(token.type) {
case kPHP_T_SELF:
case kPHP_T_ARRAY:
return token.Text();
case kPHP_T_IDENTIFIER:
type << token.Text();
break;
case kPHP_T_NS_SEPARATOR:
type << token.Text();
break;
default:
// restore the token so next call to NextToken
// will pick it up again
UngetToken(token);
cont = false;
break;
}
}
type = MakeIdentifierAbsolute(type);
return type;
}
wxString PHPSourceFile::PrependCurrentScope(const wxString& className)
{
wxString currentScope = Namespace()->GetFullName();
if(!currentScope.EndsWith("\\")) {
currentScope << "\\";
}
return currentScope + className;
}
wxString PHPSourceFile::MakeTypehintAbsolute(const wxString& type) { return DoMakeIdentifierAbsolute(type, true); }
wxString PHPSourceFile::DoMakeIdentifierAbsolute(const wxString& type, bool exactMatch)
{
if(m_converter) {
return m_converter->MakeIdentifierAbsolute(type);
}
static std::unordered_set<std::string> phpKeywords;
if(phpKeywords.empty()) {
// List taken from https://www.php.net/manual/en/language.types.intro.php
// Native types
phpKeywords.insert("bool");
phpKeywords.insert("int");
phpKeywords.insert("float");
phpKeywords.insert("string");
phpKeywords.insert("array");
phpKeywords.insert("object");
phpKeywords.insert("iterable");
phpKeywords.insert("callable");
phpKeywords.insert("null");
phpKeywords.insert("mixed");
phpKeywords.insert("void");
// Types that are common in documentation
phpKeywords.insert("boolean");
phpKeywords.insert("integer");
phpKeywords.insert("double");
phpKeywords.insert("real");
phpKeywords.insert("binery");
phpKeywords.insert("resource");
phpKeywords.insert("number");
phpKeywords.insert("callback");
}
wxString typeWithNS(type);
typeWithNS.Trim().Trim(false);
if(phpKeywords.count(type.ToStdString())) {
// primitives, don't bother...
return typeWithNS;
}
if(typeWithNS.IsEmpty())
return "";
// A fully qualified type? don't touch it
if(typeWithNS.StartsWith("\\")) {
return typeWithNS;
}
// Handle 'use' cases:
// use Zend\Form; // create an alias entry: Form => Zend\Form
// class A extends Form\Form {}
// The extends should be expanded to Zend\Form\Form
if(typeWithNS.Contains("\\")) {
wxString scopePart = typeWithNS.BeforeLast('\\');
wxString className = typeWithNS.AfterLast('\\');
if(m_aliases.find(scopePart) != m_aliases.end()) {
typeWithNS.clear();
typeWithNS << m_aliases.find(scopePart)->second << "\\" << className;
// Remove duplicate NS separators
typeWithNS.Replace("\\\\", "\\");
if(!typeWithNS.StartsWith("\\")) {
typeWithNS << "\\";
}
return typeWithNS;
}
}
// Use the alias table first
if(m_aliases.find(type) != m_aliases.end()) {
return m_aliases.find(type)->second;
}
wxString ns = Namespace()->GetFullName();
if(!ns.EndsWith("\\")) {
ns << "\\";
}
if(exactMatch && m_lookup && !typeWithNS.Contains("\\") && !m_lookup->ClassExists(ns + typeWithNS)) {
// Only when "exactMatch" apply this logic, otherwise, we might be getting a partialy typed string
// which we will not find by calling FindChild()
typeWithNS.Prepend("\\"); // Use the global NS
} else {
typeWithNS.Prepend(ns);
}
return typeWithNS;
}
const PHPEntityBase::List_t& PHPSourceFile::GetAllMatchesInOrder()
{
if(m_allMatchesInorder.empty()) {
auto ns = Namespace();
if(!ns) {
return m_allMatchesInorder;
}
if(ns->GetChildren().empty()) {
return m_allMatchesInorder;
}
PHPEntityBase::List_t Q;
Q.insert(Q.end(), ns->GetChildren().begin(), ns->GetChildren().end());
while(!Q.empty()) {
auto cur = Q.front();
Q.erase(Q.begin());
m_allMatchesInorder.push_back(cur);
if(!cur->GetChildren().empty()) {
// we want to process these children on the next iteration
// so we prepend them to the queue
Q.reserve(Q.size() + cur->GetChildren().size());
Q.insert(Q.begin(), cur->GetChildren().begin(), cur->GetChildren().end());
}
}
}
return m_allMatchesInorder;
}
|