| 1 |
#include "PHPExpression.h" |
| 2 |
#include "PHPSourceFile.h" |
| 3 |
#include "PHPEntityClass.h" |
| 4 |
#include "PHPEntityVariable.h" |
| 5 |
#include "PHPEntityFunction.h" |
| 6 |
#include <stack> |
| 7 |
#include <algorithm> |
| 8 |
#include <set> |
| 9 |
#include "PHPEntityNamespace.h" |
| 10 |
#include "PHPEntityFunctionAlias.h" |
| 11 |
|
| 12 |
PHPExpression::PHPExpression(const wxString& fulltext, const wxString& exprText, bool functionCalltipExpr) |
| 13 |
: m_type(kNone) |
| 14 |
, m_text(fulltext) |
| 15 |
, m_functionCalltipExpr(functionCalltipExpr) |
| 16 |
{ |
| 17 |
if(exprText.IsEmpty()) { |
| 18 |
// use the full text to extract the expression |
| 19 |
m_expression = CreateExpression(fulltext); |
| 20 |
|
| 21 |
} else { |
| 22 |
wxString phpExprText = exprText; |
| 23 |
if(!exprText.StartsWith("<?php")) { |
| 24 |
// without this, the parser will refuse to work :) |
| 25 |
phpExprText.Prepend("<?php "); |
| 26 |
} |
| 27 |
m_expression = CreateExpression(phpExprText); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
PHPExpression::~PHPExpression() {} |
| 32 |
|
| 33 |
phpLexerToken::Vet_t PHPExpression::CreateExpression(const wxString& text) |
| 34 |
{ |
| 35 |
m_exprStartsWithOpenTag = false; |
| 36 |
// Extract the expression at the end of the input text |
| 37 |
std::stack<phpLexerToken::Vet_t> stack; |
| 38 |
phpLexerToken::Vet_t tokens; |
| 39 |
stack.push(tokens); |
| 40 |
|
| 41 |
phpLexerToken::Vet_t* current = &stack.top(); |
| 42 |
|
| 43 |
// strip the text from any comments |
| 44 |
PHPScanner_t scanner = ::phpLexerNew(text); |
| 45 |
phpLexerToken token, lastToken; |
| 46 |
while(::phpLexerNext(scanner, token)) { |
| 47 |
lastToken = token; |
| 48 |
switch(token.type) { |
| 49 |
case kPHP_T_OPEN_TAG: |
| 50 |
if(current) current->push_back(token); |
| 51 |
break; |
| 52 |
// the following are tokens that once seen |
| 53 |
// we should start a new expression: |
| 54 |
case kPHP_T_NEW: |
| 55 |
case kPHP_T_ECHO: |
| 56 |
case kPHP_T_CASE: |
| 57 |
case kPHP_T_RETURN: |
| 58 |
case kPHP_T_THROW: |
| 59 |
case kPHP_T_CLASS: |
| 60 |
case kPHP_T_TRAIT: |
| 61 |
case kPHP_T_INTERFACE: |
| 62 |
case kPHP_T_EXTENDS: |
| 63 |
case kPHP_T_IMPLEMENTS: |
| 64 |
case kPHP_T_DOUBLE_ARROW: |
| 65 |
case kPHP_T_NS_C: |
| 66 |
case kPHP_T_FUNC_C: |
| 67 |
case kPHP_T_METHOD_C: |
| 68 |
case kPHP_T_CLASS_C: |
| 69 |
case kPHP_T_TRAIT_C: |
| 70 |
case kPHP_T_LINE: |
| 71 |
case kPHP_T_FILE: |
| 72 |
case kPHP_T_DIR: |
| 73 |
case kPHP_T_YIELD: |
| 74 |
case kPHP_T_LOGICAL_OR: |
| 75 |
case kPHP_T_LOGICAL_XOR: |
| 76 |
case kPHP_T_LOGICAL_AND: |
| 77 |
case kPHP_T_PRINT: |
| 78 |
case kPHP_T_PLUS_EQUAL: |
| 79 |
case kPHP_T_MINUS_EQUAL: |
| 80 |
case kPHP_T_MUL_EQUAL: |
| 81 |
case kPHP_T_DIV_EQUAL: |
| 82 |
case kPHP_T_CONCAT_EQUAL: |
| 83 |
case kPHP_T_MOD_EQUAL: |
| 84 |
case kPHP_T_AND_EQUAL: |
| 85 |
case kPHP_T_OR_EQUAL: |
| 86 |
case kPHP_T_XOR_EQUAL: |
| 87 |
case kPHP_T_SL_EQUAL: |
| 88 |
case kPHP_T_SR_EQUAL: |
| 89 |
case kPHP_T_BOOLEAN_OR: |
| 90 |
case kPHP_T_BOOLEAN_AND: |
| 91 |
case kPHP_T_IS_EQUAL: |
| 92 |
case kPHP_T_IS_NOT_EQUAL: |
| 93 |
case kPHP_T_IS_IDENTICAL: |
| 94 |
case kPHP_T_IS_NOT_IDENTICAL: |
| 95 |
case kPHP_T_IS_SMALLER_OR_EQUAL: |
| 96 |
case kPHP_T_IS_GREATER_OR_EQUAL: |
| 97 |
case kPHP_T_SL: |
| 98 |
case kPHP_T_SR: |
| 99 |
case kPHP_T_FUNCTION: |
| 100 |
case kPHP_T_ABSTRACT: |
| 101 |
case kPHP_T_PUBLIC: |
| 102 |
case kPHP_T_PROTECTED: |
| 103 |
case kPHP_T_PRIVATE: |
| 104 |
case kPHP_T_FINAL: |
| 105 |
case kPHP_T_CONST: |
| 106 |
case kPHP_T_REQUIRE: |
| 107 |
case kPHP_T_REQUIRE_ONCE: |
| 108 |
case kPHP_T_USE: |
| 109 |
case kPHP_T_INT_CAST: |
| 110 |
case kPHP_T_DOUBLE_CAST: |
| 111 |
case kPHP_T_STRING_CAST: |
| 112 |
case kPHP_T_OBJECT_CAST: |
| 113 |
case kPHP_T_ARRAY_CAST: |
| 114 |
case kPHP_T_BOOL_CAST: |
| 115 |
case kPHP_T_UNSET_CAST: |
| 116 |
case kPHP_T_INCLUDE: |
| 117 |
case kPHP_T_INCLUDE_ONCE: |
| 118 |
case '.': |
| 119 |
case ';': |
| 120 |
case '{': |
| 121 |
case '}': |
| 122 |
case '=': |
| 123 |
case ':': |
| 124 |
case ',': |
| 125 |
case '!': |
| 126 |
case '?': |
| 127 |
case '|': |
| 128 |
case '@': |
| 129 |
case '<': |
| 130 |
case '*': |
| 131 |
if(current) current->clear(); |
| 132 |
break; |
| 133 |
case '(': |
| 134 |
case '[': |
| 135 |
if(current) current->push_back(token); |
| 136 |
stack.push(phpLexerToken::Vet_t()); |
| 137 |
current = &stack.top(); |
| 138 |
break; |
| 139 |
case ')': |
| 140 |
case ']': |
| 141 |
if(stack.size() < 2) { |
| 142 |
// parse error... |
| 143 |
return phpLexerToken::Vet_t(); |
| 144 |
} |
| 145 |
// switch back to the previous set of tokens |
| 146 |
stack.pop(); |
| 147 |
current = &stack.top(); |
| 148 |
if(current) current->push_back(token); |
| 149 |
break; |
| 150 |
default: |
| 151 |
if(current) current->push_back(token); |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
::phpLexerDestroy(&scanner); |
| 157 |
phpLexerToken::Vet_t result; |
| 158 |
|
| 159 |
if(m_functionCalltipExpr) { |
| 160 |
// the expression parser was constructed for the purpose of |
| 161 |
// function-calltip so replace the current expression with the previous one from the stack |
| 162 |
// i.e. the one before the first open brace |
| 163 |
if(stack.size() > 1) { |
| 164 |
stack.pop(); // remove the last token sequence |
| 165 |
current = &stack.top(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if(current && !current->empty()) { |
| 170 |
if(current->at(0).type == kPHP_T_OPEN_TAG) { |
| 171 |
if(current->at(0).Text() == "<?") { |
| 172 |
m_exprStartsWithOpenTag = true; |
| 173 |
} |
| 174 |
current->erase(current->begin()); |
| 175 |
} |
| 176 |
result.swap(*current); |
| 177 |
} |
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
PHPEntityBase::Ptr_t PHPExpression::Resolve(PHPLookupTable& lookpTable, const wxString& sourceFileName) |
| 182 |
{ |
| 183 |
if(m_expression.empty()) return PHPEntityBase::Ptr_t(NULL); |
| 184 |
|
| 185 |
m_sourceFile.reset(new PHPSourceFile(m_text, &lookpTable)); |
| 186 |
m_sourceFile->SetParseFunctionBody(true); |
| 187 |
m_sourceFile->SetFilename(sourceFileName); |
| 188 |
m_sourceFile->Parse(); |
| 189 |
|
| 190 |
if(m_expression.size() == 1 && m_expression.at(0).type == kPHP_T_NS_SEPARATOR) { |
| 191 |
// user typed '\' |
| 192 |
return lookpTable.FindScope("\\"); |
| 193 |
} |
| 194 |
|
| 195 |
wxString asString = DoSimplifyExpression(0, m_sourceFile); |
| 196 |
wxUnusedVar(asString); |
| 197 |
|
| 198 |
if(m_parts.empty() && !m_filter.IsEmpty()) { |
| 199 |
|
| 200 |
// We have no expression, but the user did type something... |
| 201 |
// Return the parent scope of what the user typed so far |
| 202 |
if(m_filter.Contains("\\")) { |
| 203 |
// A namespace separator was found in the filter, break |
| 204 |
// the filter into 2: |
| 205 |
// scope + filter |
| 206 |
wxString scopePart = m_filter.BeforeLast('\\'); |
| 207 |
if(!scopePart.StartsWith("\\")) { |
| 208 |
scopePart.Prepend("\\"); |
| 209 |
} |
| 210 |
|
| 211 |
wxString filterPart = m_filter.AfterLast('\\'); |
| 212 |
|
| 213 |
// Adjust the filter |
| 214 |
m_filter.swap(filterPart); |
| 215 |
|
| 216 |
// Fix the scope part |
| 217 |
scopePart = m_sourceFile->MakeIdentifierAbsolute(scopePart); |
| 218 |
return lookpTable.FindScope(scopePart); |
| 219 |
|
| 220 |
} else { |
| 221 |
// No namespace separator was typed |
| 222 |
// try to guess: |
| 223 |
// if the m_filter contains "(" -> user wants a global functions |
| 224 |
// else we use the current file scope |
| 225 |
return lookpTable.FindScope(m_sourceFile->Namespace()->GetFullName()); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// Now, use the lookup table |
| 230 |
std::list<PHPExpression::Part>::iterator iter = m_parts.begin(); |
| 231 |
PHPEntityBase::Ptr_t currentToken(NULL); |
| 232 |
PHPEntityBase::Ptr_t parentToken(NULL); |
| 233 |
for(; iter != m_parts.end(); ++iter) { |
| 234 |
Part& part = *iter; |
| 235 |
if(!currentToken) { |
| 236 |
// first token |
| 237 |
// Check locks first |
| 238 |
if(part.m_text.StartsWith("$") && m_sourceFile->CurrentScope()) { |
| 239 |
// a variable |
| 240 |
currentToken = m_sourceFile->CurrentScope()->FindChild(part.m_text); |
| 241 |
} |
| 242 |
if(!currentToken) { |
| 243 |
currentToken = lookpTable.FindScope(part.m_text); |
| 244 |
if(!currentToken) { |
| 245 |
// If we are inside a namespace, try prepending the namespace |
| 246 |
// to the first token |
| 247 |
if(m_sourceFile->Namespace() && m_sourceFile->Namespace()->GetFullName() != "\\") { |
| 248 |
// Not the global namespace |
| 249 |
wxString fullns = |
| 250 |
PHPEntityNamespace::BuildNamespace(m_sourceFile->Namespace()->GetFullName(), part.m_text); |
| 251 |
// Check if it exists |
| 252 |
PHPEntityBase::Ptr_t pGuess = lookpTable.FindScope(fullns); |
| 253 |
if(pGuess) { |
| 254 |
currentToken = pGuess; |
| 255 |
part.m_text = fullns; |
| 256 |
} else { |
| 257 |
// Maybe its a global function.. |
| 258 |
currentToken = lookpTable.FindFunction(part.m_text); |
| 259 |
} |
| 260 |
} else { |
| 261 |
// Maybe its a global function.. |
| 262 |
currentToken = lookpTable.FindFunction(part.m_text); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
} else { |
| 268 |
// load the children of the current token (optionally, filter by the text) |
| 269 |
currentToken = lookpTable.FindMemberOf(currentToken->GetDbId(), part.m_text); |
| 270 |
if(currentToken && currentToken->Is(kEntityTypeFunctionAlias)) { |
| 271 |
// If the member is a function-alias, use the actual function instead |
| 272 |
currentToken = currentToken->Cast<PHPEntityFunctionAlias>()->GetFunc(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// If the current "part" of the expression ends with a scope resolving operator ("::") or |
| 277 |
// an object operator ("->") we need to resolve the operator to the actual type ( |
| 278 |
// incase of a functin it will be the return value, and in case of a variable it will be |
| 279 |
// the type hint) |
| 280 |
if(currentToken) { |
| 281 |
if(part.m_operator == kPHP_T_OBJECT_OPERATOR || part.m_operator == kPHP_T_PAAMAYIM_NEKUDOTAYIM) { |
| 282 |
wxString actualType; |
| 283 |
if(currentToken->Is(kEntityTypeFunction)) { |
| 284 |
// return the function return value |
| 285 |
actualType = currentToken->Cast<PHPEntityFunction>()->GetReturnValue(); |
| 286 |
|
| 287 |
if((actualType == "self" || actualType == "\\self") && parentToken) { |
| 288 |
// Resolve self to the actual class name |
| 289 |
actualType = parentToken->GetFullName(); |
| 290 |
} |
| 291 |
|
| 292 |
} else if(currentToken->Is(kEntityTypeVariable)) { |
| 293 |
// return the type hint |
| 294 |
actualType = currentToken->Cast<PHPEntityVariable>()->GetTypeHint(); |
| 295 |
} |
| 296 |
|
| 297 |
wxString fixedpath; |
| 298 |
if(!actualType.IsEmpty() && FixReturnValueNamespace(lookpTable, parentToken, actualType, fixedpath)) { |
| 299 |
actualType.swap(fixedpath); |
| 300 |
} |
| 301 |
|
| 302 |
if(!actualType.IsEmpty()) { |
| 303 |
currentToken = lookpTable.FindScope(actualType); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if(!currentToken) { |
| 309 |
// return NULL |
| 310 |
return currentToken; |
| 311 |
} |
| 312 |
parentToken = currentToken; |
| 313 |
} |
| 314 |
return currentToken; |
| 315 |
} |
| 316 |
|
| 317 |
wxString PHPExpression::DoSimplifyExpression(int depth, PHPSourceFile::Ptr_t sourceFile) |
| 318 |
{ |
| 319 |
if(depth > 5 || sourceFile == NULL) { |
| 320 |
// avoid infinite recursion, by limiting the nest level to 5 |
| 321 |
return ""; |
| 322 |
} |
| 323 |
|
| 324 |
// Use the provided sourceFile if 'm_sourceFile' is NULL |
| 325 |
if(!m_sourceFile) { |
| 326 |
m_sourceFile = sourceFile; |
| 327 |
} |
| 328 |
|
| 329 |
// Parse the input source file |
| 330 |
PHPEntityBase::Ptr_t scope = sourceFile->CurrentScope(); |
| 331 |
const PHPEntityBase* innerClass = sourceFile->Class(); |
| 332 |
|
| 333 |
// Check the first token |
| 334 |
|
| 335 |
// Phase 1: |
| 336 |
// Loop over the expression and resolve as much as we can here for the first token |
| 337 |
// (mainly, replace $this, self, static, $variable) |
| 338 |
|
| 339 |
phpLexerToken token; |
| 340 |
wxString newExpr; |
| 341 |
wxString firstToken; |
| 342 |
int firstTokenType = wxNOT_FOUND; |
| 343 |
for(size_t i = 0; i < m_expression.size(); ++i) { |
| 344 |
token = m_expression.at(i); |
| 345 |
if(i == 0) { |
| 346 |
// Perform basic replacements that we can conduct here without the need of the global |
| 347 |
// lookup table |
| 348 |
if(token.type == kPHP_T_PARENT) { |
| 349 |
if(!innerClass) return ""; |
| 350 |
firstToken = innerClass->GetFullName(); |
| 351 |
firstTokenType = kPHP_T_PARENT; |
| 352 |
|
| 353 |
} else if(token.type == kPHP_T_THIS) { |
| 354 |
// the first token is $this |
| 355 |
// replace it with the current class absolute path |
| 356 |
if(!innerClass) return ""; |
| 357 |
firstToken = innerClass->GetFullName(); // Is always in absolute path |
| 358 |
|
| 359 |
} else if(token.type == kPHP_T_SELF) { |
| 360 |
// Same as $this: replace it with the current class absolute path |
| 361 |
if(!innerClass) return ""; |
| 362 |
firstToken = innerClass->GetFullName(); // Is always in absolute path |
| 363 |
firstTokenType = kPHP_T_SELF; |
| 364 |
|
| 365 |
} else if(token.type == kPHP_T_STATIC) { |
| 366 |
// Same as $this: replace it with the current class absolute path |
| 367 |
if(!innerClass) return ""; |
| 368 |
firstToken = innerClass->GetFullName(); // Is always in absolute path |
| 369 |
firstTokenType = kPHP_T_STATIC; |
| 370 |
|
| 371 |
} else if(token.type == kPHP_T_VARIABLE) { |
| 372 |
// the expression being evaluated starts with a variable (e.g. $a->something()->) |
| 373 |
// in this case, use the current scope ('scope') and replace it with the real type |
| 374 |
// Note that the type can be another expression |
| 375 |
// e.g.: |
| 376 |
// $a = $this->getQuery(); |
| 377 |
// $b = $a->fetchAll()-> |
| 378 |
// so if the expression being evaluated is "$a->fetchAll()->" |
| 379 |
// we first replace $a with '$this->getQuery()' so it becomes: |
| 380 |
// $this->getQuery()->fetchAll()-> |
| 381 |
// However, $this also need a replacement so eventually, it becomes like this: |
| 382 |
// \MyClass->getQuery->fetchAll-> and this is something that we can evaluate easily using |
| 383 |
// our lookup tables (note that the parenthessis are missing on purpose) |
| 384 |
PHPEntityBase::Ptr_t local = scope->FindChild(token.Text()); |
| 385 |
if(local && local->Cast<PHPEntityVariable>()) { |
| 386 |
if(!local->Cast<PHPEntityVariable>()->GetTypeHint().IsEmpty()) { |
| 387 |
// we have type hint! - use it |
| 388 |
firstToken = local->Cast<PHPEntityVariable>()->GetTypeHint(); |
| 389 |
|
| 390 |
} else if(!local->Cast<PHPEntityVariable>()->GetExpressionHint().IsEmpty()) { |
| 391 |
// we have an expression hint - use it |
| 392 |
// append the "->" to the expression to make sure that the parser will understand it |
| 393 |
// as an expression |
| 394 |
PHPExpression e(m_text, local->Cast<PHPEntityVariable>()->GetExpressionHint() + "->"); |
| 395 |
firstToken = e.DoSimplifyExpression(depth + 1, m_sourceFile); |
| 396 |
if(firstToken.EndsWith("->")) { |
| 397 |
// remove the last 2 characters |
| 398 |
firstToken.RemoveLast(2); |
| 399 |
} |
| 400 |
} |
| 401 |
} else { |
| 402 |
// this local variable does not exist in the current scope |
| 403 |
// This is probably a word-completion for local variable |
| 404 |
m_filter = token.Text(); |
| 405 |
return ""; |
| 406 |
} |
| 407 |
|
| 408 |
} else if(token.type == kPHP_T_IDENTIFIER) { |
| 409 |
// an identifier, convert it to the fullpath |
| 410 |
firstToken = sourceFile->MakeIdentifierAbsolute(token.Text()); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
if(!firstToken.IsEmpty()) { |
| 415 |
newExpr = firstToken; |
| 416 |
firstToken.Clear(); |
| 417 |
} else { |
| 418 |
newExpr << " " << token.Text(); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
// Phase 2: |
| 423 |
// Now break the tokens from the lexers into something that we can work with |
| 424 |
// when using the lookup table. |
| 425 |
|
| 426 |
// The split in this phase is done by searching for kPHP_T_OBJECT_OPERATOR and kPHP_T_PAAMAYIM_NEKUDOTAYIM |
| 427 |
// separators |
| 428 |
m_expression = CreateExpression("<?php " + newExpr); |
| 429 |
Part part; |
| 430 |
wxString currentText; |
| 431 |
for(size_t i = 0; i < m_expression.size(); ++i) { |
| 432 |
token = m_expression.at(i); |
| 433 |
// Remove any braces and split by object kPHP_T_OBJECT_OPERATOR and kPHP_T_PAAMAYIM_NEKUDOTAYIM |
| 434 |
switch(token.type) { |
| 435 |
case kPHP_T_OPEN_TAG: |
| 436 |
break; |
| 437 |
case '(': |
| 438 |
if(!currentText.IsEmpty()) { |
| 439 |
part.m_text = currentText; |
| 440 |
} |
| 441 |
break; |
| 442 |
case ')': |
| 443 |
// skip it |
| 444 |
break; |
| 445 |
case kPHP_T_PAAMAYIM_NEKUDOTAYIM: |
| 446 |
case kPHP_T_OBJECT_OPERATOR: |
| 447 |
if(!currentText.IsEmpty() && part.m_text.IsEmpty()) { |
| 448 |
if(m_parts.empty() && token.type == kPHP_T_PAAMAYIM_NEKUDOTAYIM) { |
| 449 |
// The first token in the "parts" list has a scope resolving operator ("::") |
| 450 |
// we need to make sure that the indetifier is provided in fullpath |
| 451 |
part.m_text = sourceFile->MakeIdentifierAbsolute(currentText); |
| 452 |
} else { |
| 453 |
part.m_text = currentText; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if(m_parts.empty()) { |
| 458 |
// If the first token before the simplication was 'parent' |
| 459 |
// keyword, we need to carry this over |
| 460 |
part.m_textType = firstTokenType; |
| 461 |
} |
| 462 |
|
| 463 |
part.m_operator = token.type; |
| 464 |
part.m_operatorText = token.Text(); |
| 465 |
m_parts.push_back(part); |
| 466 |
|
| 467 |
// cleanup |
| 468 |
currentText.clear(); |
| 469 |
part.m_text.clear(); |
| 470 |
part.m_operatorText.clear(); |
| 471 |
part.m_operator = wxNOT_FOUND; |
| 472 |
break; |
| 473 |
case kPHP_T_THIS: |
| 474 |
case kPHP_T_SELF: |
| 475 |
case kPHP_T_STATIC: |
| 476 |
part.m_textType = token.type; |
| 477 |
currentText << token.Text(); |
| 478 |
break; |
| 479 |
default: |
| 480 |
currentText << token.Text(); |
| 481 |
break; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
if(!currentText.IsEmpty()) { |
| 486 |
m_filter = currentText; |
| 487 |
} |
| 488 |
|
| 489 |
wxString simplified; |
| 490 |
List_t::iterator iter = m_parts.begin(); |
| 491 |
for(; iter != m_parts.end(); ++iter) { |
| 492 |
simplified << iter->m_text << iter->m_operatorText; |
| 493 |
} |
| 494 |
return simplified.Trim().Trim(false); |
| 495 |
} |
| 496 |
|
| 497 |
wxString PHPExpression::GetExpressionAsString() const |
| 498 |
{ |
| 499 |
wxString expr; |
| 500 |
for(size_t i = 0; i < m_expression.size(); ++i) { |
| 501 |
expr << m_expression.at(i).Text(); |
| 502 |
} |
| 503 |
return expr; |
| 504 |
} |
| 505 |
|
| 506 |
size_t PHPExpression::GetLookupFlags() const |
| 507 |
{ |
| 508 |
size_t flags(0); |
| 509 |
if(m_parts.empty()) return flags; |
| 510 |
|
| 511 |
if(m_parts.size() == 1 && m_parts.back().m_textType == kPHP_T_PARENT) { |
| 512 |
Part firstPart = m_parts.back(); |
| 513 |
if(firstPart.m_textType == kPHP_T_PARENT) { |
| 514 |
flags |= PHPLookupTable::kLookupFlags_Parent; |
| 515 |
} |
| 516 |
} else { |
| 517 |
Part lastExpressionPart = m_parts.back(); |
| 518 |
if(lastExpressionPart.m_operator == kPHP_T_PAAMAYIM_NEKUDOTAYIM) { |
| 519 |
if(lastExpressionPart.m_textType == kPHP_T_SELF) |
| 520 |
flags |= PHPLookupTable::kLookupFlags_Self; |
| 521 |
else |
| 522 |
flags |= PHPLookupTable::kLookupFlags_Static; |
| 523 |
} |
| 524 |
} |
| 525 |
return flags; |
| 526 |
} |
| 527 |
|
| 528 |
void PHPExpression::Suggest(PHPEntityBase::Ptr_t resolved, PHPLookupTable& lookup, PHPEntityBase::List_t& matches) |
| 529 |
{ |
| 530 |
// sanity |
| 531 |
if(!resolved) return; |
| 532 |
PHPEntityBase::Ptr_t currentScope = GetSourceFile()->CurrentScope(); |
| 533 |
|
| 534 |
// GetCount() == 0 && !GetFilter().IsEmpty() i.e. a word completion is required. |
| 535 |
// We enhance the list with the following: |
| 536 |
// - PHP keywords |
| 537 |
// - Global functions |
| 538 |
// - Global constants |
| 539 |
// - Function arguments |
| 540 |
// - Local variables (of the current scope) |
| 541 |
// - And aliases e.g. 'use foo\bar as Bar;' |
| 542 |
if(GetCount() == 0 && !GetFilter().IsEmpty()) { |
| 543 |
|
| 544 |
// For functions and constants, PHP will fall back to global functions or constants if a |
| 545 |
// namespaced function or constant does not exist. |
| 546 |
PHPEntityBase::List_t globals = |
| 547 |
lookup.FindGlobalFunctionAndConsts(PHPLookupTable::kLookupFlags_Contains, GetFilter()); |
| 548 |
matches.insert(matches.end(), globals.begin(), globals.end()); |
| 549 |
|
| 550 |
if(currentScope && (currentScope->Is(kEntityTypeFunction) || currentScope->Is(kEntityTypeNamespace))) { |
| 551 |
// If the current scope is a function |
| 552 |
// add the local variables + function arguments to the current list of matches |
| 553 |
const PHPEntityBase::List_t& children = currentScope->GetChildren(); |
| 554 |
PHPEntityBase::List_t::const_iterator iter = children.begin(); |
| 555 |
for(; iter != children.end(); ++iter) { |
| 556 |
PHPEntityBase::Ptr_t child = *iter; |
| 557 |
if(child->Is(kEntityTypeVariable) && child->GetShortName().Contains(GetFilter()) && |
| 558 |
child->GetShortName() != GetFilter()) { |
| 559 |
matches.push_back(child); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
{ |
| 565 |
// Add aliases |
| 566 |
PHPEntityBase::List_t aliases = GetSourceFile()->GetAliases(); |
| 567 |
PHPEntityBase::List_t::iterator iter = aliases.begin(); |
| 568 |
for(; iter != aliases.end(); ++iter) { |
| 569 |
if((*iter)->GetShortName().Contains(GetFilter())) { |
| 570 |
matches.push_back(*iter); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
{ |
| 576 |
// Add $this incase we are inside a class (but only if '$this' contains the filter string) |
| 577 |
wxString lcFilter = GetFilter().Lower(); |
| 578 |
if(GetSourceFile()->Class() && wxString("$this").Contains(lcFilter)) { |
| 579 |
PHPEntityBase::Ptr_t thiz(new PHPEntityVariable()); |
| 580 |
thiz->SetFullName("$this"); |
| 581 |
thiz->SetShortName("$this"); |
| 582 |
thiz->SetFilename(currentScope->GetFilename()); |
| 583 |
matches.push_back(thiz); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
// Add the scoped matches |
| 589 |
// for the code completion |
| 590 |
size_t flags = PHPLookupTable::kLookupFlags_Contains | GetLookupFlags(); |
| 591 |
if(resolved->Is(kEntityTypeClass)) { |
| 592 |
if(resolved->Cast<PHPEntityClass>()->IsInterface() || resolved->Cast<PHPEntityClass>()->IsAbstractClass()) { |
| 593 |
flags |= PHPLookupTable::kLookupFlags_IncludeAbstractMethods; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
PHPEntityBase::List_t scopeChildren = lookup.FindChildren(resolved->GetDbId(), flags, GetFilter()); |
| 598 |
matches.insert(matches.end(), scopeChildren.begin(), scopeChildren.end()); |
| 599 |
|
| 600 |
// Incase the resolved is a namespace, suggest all children namespaces |
| 601 |
if(resolved->Is(kEntityTypeNamespace)) { |
| 602 |
PHPEntityBase::List_t namespaces = lookup.FindNamespaces(resolved->GetFullName(), GetFilter()); |
| 603 |
matches.insert(matches.end(), namespaces.begin(), namespaces.end()); |
| 604 |
} |
| 605 |
|
| 606 |
// and make the list unique |
| 607 |
DoMakeUnique(matches); |
| 608 |
} |
| 609 |
|
| 610 |
void PHPExpression::DoMakeUnique(PHPEntityBase::List_t& matches) |
| 611 |
{ |
| 612 |
std::set<wxString> uniqueNames; |
| 613 |
PHPEntityBase::List_t uniqueList; |
| 614 |
for(PHPEntityBase::List_t::iterator iter = matches.begin(); iter != matches.end(); ++iter) { |
| 615 |
if(uniqueNames.count((*iter)->GetFullName()) == 0) { |
| 616 |
uniqueNames.insert((*iter)->GetFullName()); |
| 617 |
uniqueList.push_back(*iter); |
| 618 |
} |
| 619 |
} |
| 620 |
matches.swap(uniqueList); |
| 621 |
} |
| 622 |
|
| 623 |
bool PHPExpression::FixReturnValueNamespace(PHPLookupTable& lookup, |
| 624 |
PHPEntityBase::Ptr_t parent, |
| 625 |
const wxString& classFullpath, |
| 626 |
wxString& fixedpath) |
| 627 |
{ |
| 628 |
if(!parent) return false; |
| 629 |
PHPEntityBase::Ptr_t pClass = lookup.FindClass(classFullpath); |
| 630 |
if(!pClass) { |
| 631 |
// classFullpath does not exist |
| 632 |
// prepend the parent namespace to its path and check again |
| 633 |
wxString parentNamespace = parent->GetFullName().BeforeLast('\\'); |
| 634 |
wxString returnValueNamespace = classFullpath.BeforeLast('\\'); |
| 635 |
wxString returnValueName = classFullpath.AfterLast('\\'); |
| 636 |
wxString newType = PHPEntityNamespace::BuildNamespace(parentNamespace, returnValueNamespace); |
| 637 |
newType << "\\" << returnValueName; |
| 638 |
pClass = lookup.FindClass(newType); |
| 639 |
if(pClass) { |
| 640 |
fixedpath = newType; |
| 641 |
return true; |
| 642 |
} |
| 643 |
} |
| 644 |
return false; |
| 645 |
} |