| 1 |
#include "CxxVariableScanner.h" |
| 2 |
|
| 3 |
#include "CxxScannerTokens.h" |
| 4 |
#include "file_logger.h" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <unordered_set> |
| 8 |
|
| 9 |
CxxVariableScanner::CxxVariableScanner(const wxString& buffer, eCxxStandard standard, const wxStringTable_t& macros, |
| 10 |
bool isFuncSignature) |
| 11 |
: m_scanner(NULL) |
| 12 |
, m_buffer(buffer) |
| 13 |
, m_eof(false) |
| 14 |
, m_parenthesisDepth(0) |
| 15 |
, m_standard(standard) |
| 16 |
, m_macros(macros) |
| 17 |
, m_isFuncSignature(isFuncSignature) |
| 18 |
{ |
| 19 |
if(!m_buffer.IsEmpty() && m_buffer[0] == '(') { |
| 20 |
m_buffer.Remove(0, 1); |
| 21 |
} |
| 22 |
m_nativeTypes.insert(T_AUTO); |
| 23 |
m_nativeTypes.insert(T_BOOL); |
| 24 |
m_nativeTypes.insert(T_CHAR); |
| 25 |
m_nativeTypes.insert(T_CHAR16_T); |
| 26 |
m_nativeTypes.insert(T_CHAR32_T); |
| 27 |
m_nativeTypes.insert(T_DOUBLE); |
| 28 |
m_nativeTypes.insert(T_FLOAT); |
| 29 |
m_nativeTypes.insert(T_INT); |
| 30 |
m_nativeTypes.insert(T_LONG); |
| 31 |
m_nativeTypes.insert(T_SHORT); |
| 32 |
m_nativeTypes.insert(T_SIGNED); |
| 33 |
m_nativeTypes.insert(T_UNSIGNED); |
| 34 |
m_nativeTypes.insert(T_VOID); |
| 35 |
m_nativeTypes.insert(T_WCHAR_T); |
| 36 |
// optimize the buffer |
| 37 |
DoOptimizeBuffer(); |
| 38 |
} |
| 39 |
|
| 40 |
CxxVariableScanner::~CxxVariableScanner() {} |
| 41 |
|
| 42 |
CxxVariable::Vec_t CxxVariableScanner::GetVariables(bool sort) |
| 43 |
{ |
| 44 |
// this call does nothing if the buffer was already optimized |
| 45 |
CxxVariable::Vec_t vars = DoGetVariables(GetOptimizeBuffer(), sort); |
| 46 |
if(sort) { |
| 47 |
std::sort(vars.begin(), vars.end(), |
| 48 |
[&](CxxVariable::Ptr_t a, CxxVariable::Ptr_t b) { return a->GetName() < b->GetName(); }); |
| 49 |
} |
| 50 |
return vars; |
| 51 |
} |
| 52 |
|
| 53 |
bool CxxVariableScanner::ReadType(CxxVariable::LexerToken::Vec_t& vartype, bool& isAuto) |
| 54 |
{ |
| 55 |
isAuto = false; |
| 56 |
int depth = 0; |
| 57 |
CxxLexerToken token; |
| 58 |
while(GetNextToken(token)) { |
| 59 |
if(depth == 0) { |
| 60 |
if(vartype.empty()) { |
| 61 |
// a type can start the following tokens |
| 62 |
switch(token.GetType()) { |
| 63 |
case T_AUTO: |
| 64 |
isAuto = true; |
| 65 |
// fall |
| 66 |
case T_CLASS: |
| 67 |
case T_STRUCT: |
| 68 |
case T_IDENTIFIER: |
| 69 |
case T_DOUBLE_COLONS: |
| 70 |
case T_BOOL: |
| 71 |
case T_CHAR: |
| 72 |
case T_CHAR16_T: |
| 73 |
case T_CHAR32_T: |
| 74 |
case T_CONST: |
| 75 |
case T_CONSTEXPR: |
| 76 |
case T_DOUBLE: |
| 77 |
case T_FLOAT: |
| 78 |
case T_INT: |
| 79 |
case T_LONG: |
| 80 |
case T_MUTABLE: |
| 81 |
case T_REGISTER: |
| 82 |
case T_SHORT: |
| 83 |
case T_SIGNED: |
| 84 |
case T_STATIC: |
| 85 |
case T_UNSIGNED: |
| 86 |
case T_VOLATILE: |
| 87 |
case T_VOID: |
| 88 |
case T_USING: |
| 89 |
case T_WCHAR_T: { |
| 90 |
vartype.push_back(CxxVariable::LexerToken(token, depth)); |
| 91 |
break; |
| 92 |
} |
| 93 |
default: |
| 94 |
// Not a type definition |
| 95 |
return false; |
| 96 |
} |
| 97 |
} else { |
| 98 |
const CxxVariable::LexerToken& lastToken = vartype.back(); |
| 99 |
switch(token.GetType()) { |
| 100 |
case T_IDENTIFIER: { |
| 101 |
if(TypeHasIdentifier(vartype) && (vartype.back().type != T_DOUBLE_COLONS)) { |
| 102 |
// We already found the identifier for this type, its probably part of the name |
| 103 |
UngetToken(token); |
| 104 |
return true; |
| 105 |
} else if(HasNativeTypeInList(vartype) && (vartype.back().type != T_DOUBLE_COLONS)) { |
| 106 |
UngetToken(token); |
| 107 |
return true; |
| 108 |
} |
| 109 |
// Found an identifier |
| 110 |
// We consider this part of the type only if the previous token was "::" or |
| 111 |
// if it was T_CONST |
| 112 |
switch(lastToken.type) { |
| 113 |
case T_CLASS: |
| 114 |
case T_STRUCT: |
| 115 |
case T_DOUBLE_COLONS: |
| 116 |
case T_CONST: |
| 117 |
case T_CONSTEXPR: |
| 118 |
case T_REGISTER: |
| 119 |
case T_MUTABLE: |
| 120 |
case T_VOLATILE: |
| 121 |
case T_STATIC: |
| 122 |
vartype.push_back(CxxVariable::LexerToken(token, depth)); |
| 123 |
break; |
| 124 |
default: |
| 125 |
UngetToken(token); |
| 126 |
return true; |
| 127 |
} |
| 128 |
break; |
| 129 |
case T_AUTO: |
| 130 |
isAuto = true; |
| 131 |
// fall |
| 132 |
case T_DOUBLE_COLONS: |
| 133 |
case T_BOOL: |
| 134 |
case T_CHAR: |
| 135 |
case T_CHAR16_T: |
| 136 |
case T_CHAR32_T: |
| 137 |
case T_CONST: |
| 138 |
case T_CONSTEXPR: |
| 139 |
case T_DOUBLE: |
| 140 |
case T_FLOAT: |
| 141 |
case T_INT: |
| 142 |
case T_LONG: |
| 143 |
case T_SHORT: |
| 144 |
case T_SIGNED: |
| 145 |
case T_UNSIGNED: |
| 146 |
case T_VOID: |
| 147 |
case T_WCHAR_T: { |
| 148 |
vartype.push_back(CxxVariable::LexerToken(token, depth)); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
case '<': |
| 153 |
case '[': |
| 154 |
vartype.push_back(CxxVariable::LexerToken(token, depth)); |
| 155 |
depth++; |
| 156 |
break; |
| 157 |
case '*': |
| 158 |
case '&': |
| 159 |
case '@': // AngelScript |
| 160 |
case T_AND_AND: // C++11 reference rvalue |
| 161 |
// Part of the name |
| 162 |
UngetToken(token); |
| 163 |
return true; |
| 164 |
default: |
| 165 |
return false; |
| 166 |
} |
| 167 |
} |
| 168 |
} else { |
| 169 |
// Depth > 0 |
| 170 |
vartype.push_back(CxxVariable::LexerToken(token, depth)); |
| 171 |
if(token.GetType() == '>' || token.GetType() == ']') { |
| 172 |
--depth; |
| 173 |
} else if(token.GetType() == '<' || token.GetType() == '[') { |
| 174 |
++depth; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
thread_local std::unordered_set<int> s_validLocalTerminators; |
| 182 |
bool CxxVariableScanner::ReadName(wxString& varname, wxString& pointerOrRef, int& line_number, |
| 183 |
wxString& varInitialization) |
| 184 |
{ |
| 185 |
CxxLexerToken token; |
| 186 |
while(GetNextToken(token)) { |
| 187 |
if(token.GetType() == '@') { |
| 188 |
// AngelScript. @ is similar to * in C/C++ |
| 189 |
// @see https://github.com/eranif/codelite/issues/1839 |
| 190 |
if(!GetNextToken(token) || token.GetType() != T_IDENTIFIER) { |
| 191 |
varname.Clear(); |
| 192 |
return false; |
| 193 |
} |
| 194 |
varname = token.GetWXString(); |
| 195 |
line_number = token.GetLineNumber(); |
| 196 |
varInitialization.Clear(); |
| 197 |
pointerOrRef = "@"; |
| 198 |
return true; |
| 199 |
} else if(token.GetType() == T_IDENTIFIER) { |
| 200 |
varname = token.GetWXString(); |
| 201 |
|
| 202 |
// When parsing function signature, we don't have multiple arguments |
| 203 |
// tied to the same TYPE |
| 204 |
if(m_isFuncSignature) { |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
// Peek at the next token |
| 209 |
// We can expect "=", "," "(", ";" or ")" |
| 210 |
// Examples: |
| 211 |
// TYPE name = 1; |
| 212 |
// TYPE name, secondVariable; |
| 213 |
// TYPE name(10); |
| 214 |
// TYPE name; |
| 215 |
if(s_validLocalTerminators.empty()) { |
| 216 |
s_validLocalTerminators.insert((int)','); |
| 217 |
s_validLocalTerminators.insert((int)'='); |
| 218 |
s_validLocalTerminators.insert((int)';'); |
| 219 |
s_validLocalTerminators.insert((int)')'); |
| 220 |
s_validLocalTerminators.insert((int)'('); |
| 221 |
s_validLocalTerminators.insert((int)'{'); // C++11 initialization, e.g: vector<int> v {1,2,3}; |
| 222 |
s_validLocalTerminators.insert((int)'['); // Array |
| 223 |
} |
| 224 |
|
| 225 |
// Now that we got the name, check if have more variables to expect |
| 226 |
if(!GetNextToken(token)) { |
| 227 |
// We reached EOF, but we do got the variable name |
| 228 |
// se we return false ("don't continue") but we dont clear the name |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
// Always return the token |
| 233 |
UngetToken(token); |
| 234 |
|
| 235 |
if(s_validLocalTerminators.count(token.GetType()) == 0) { |
| 236 |
varname.Clear(); |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
ConsumeInitialization(varInitialization); |
| 241 |
|
| 242 |
// Now that we got the name, check if have more variables to expect |
| 243 |
if(!GetNextToken(token)) { |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
if((token.GetType() == '{') && !varInitialization.IsEmpty()) { |
| 248 |
// Don't collect functions and consider them as variables |
| 249 |
UngetToken(token); |
| 250 |
varname.clear(); |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
if(!varInitialization.empty()) { |
| 255 |
varInitialization.RemoveLast(); |
| 256 |
} |
| 257 |
|
| 258 |
// If we found comma, return true. Unless we are parsing a function signature |
| 259 |
if(!m_isFuncSignature && (token.GetType() == ',')) { |
| 260 |
return true; |
| 261 |
} else { |
| 262 |
UngetToken(token); |
| 263 |
return false; |
| 264 |
} |
| 265 |
} else if(token.GetType() == '*' || token.GetType() == '&' || token.GetType() == T_AND_AND) { |
| 266 |
pointerOrRef << token.GetWXString(); |
| 267 |
} else { |
| 268 |
return false; |
| 269 |
} |
| 270 |
} |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
void CxxVariableScanner::ConsumeInitialization(wxString& consumed) |
| 275 |
{ |
| 276 |
CxxLexerToken token; |
| 277 |
wxString dummy; |
| 278 |
if(!GetNextToken(token)) |
| 279 |
return; |
| 280 |
|
| 281 |
int type = wxNOT_FOUND; |
| 282 |
int tokType = token.GetType(); |
| 283 |
if(tokType == '=') { |
| 284 |
CxxLexerToken next_token; |
| 285 |
if(!GetNextToken(next_token)) |
| 286 |
return; |
| 287 |
|
| 288 |
if(next_token.GetType() == '[') { |
| 289 |
// ... = [ -> we expect lambda to come |
| 290 |
return; |
| 291 |
} else if(next_token.GetType() == ']') { |
| 292 |
// ... =] -> lambda body is expected |
| 293 |
return; |
| 294 |
} else { |
| 295 |
UngetToken(next_token); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if(tokType == '(') { |
| 300 |
// Read the initialization |
| 301 |
std::unordered_set<int> delims; |
| 302 |
delims.insert(')'); |
| 303 |
if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) { |
| 304 |
return; |
| 305 |
} |
| 306 |
consumed.Prepend("("); |
| 307 |
// Now read until the delimiter |
| 308 |
delims.clear(); |
| 309 |
delims.insert(';'); |
| 310 |
delims.insert(','); |
| 311 |
delims.insert('{'); |
| 312 |
type = ReadUntil(delims, token, dummy); |
| 313 |
|
| 314 |
} else if(tokType == '[') { |
| 315 |
// Array |
| 316 |
std::unordered_set<int> delims; |
| 317 |
delims.insert(']'); |
| 318 |
if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) { |
| 319 |
return; |
| 320 |
} |
| 321 |
consumed.Prepend("["); |
| 322 |
// Now read until the delimiter |
| 323 |
delims.clear(); |
| 324 |
delims.insert(';'); |
| 325 |
delims.insert(','); |
| 326 |
type = ReadUntil(delims, token, dummy); |
| 327 |
|
| 328 |
} else if(tokType == '{') { |
| 329 |
// Read the initialization |
| 330 |
std::unordered_set<int> delims; |
| 331 |
delims.insert('}'); |
| 332 |
if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) { |
| 333 |
return; |
| 334 |
} |
| 335 |
consumed.Prepend("{"); |
| 336 |
// Now read until the delimiter |
| 337 |
delims.clear(); |
| 338 |
delims.insert(';'); |
| 339 |
delims.insert(','); |
| 340 |
type = ReadUntil(delims, token, dummy); |
| 341 |
|
| 342 |
} else if(tokType == '=') { |
| 343 |
std::unordered_set<int> delims; |
| 344 |
delims.insert(';'); |
| 345 |
delims.insert(','); |
| 346 |
type = ReadUntil(delims, token, consumed); |
| 347 |
} else { |
| 348 |
UngetToken(token); |
| 349 |
consumed.clear(); |
| 350 |
std::unordered_set<int> delims; |
| 351 |
delims.insert(';'); |
| 352 |
delims.insert(','); |
| 353 |
delims.insert('{'); |
| 354 |
type = ReadUntil(delims, token, dummy); |
| 355 |
} |
| 356 |
|
| 357 |
if(type == ',' || type == (int)'{' || type == ';') { |
| 358 |
UngetToken(token); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
int CxxVariableScanner::ReadUntil(const std::unordered_set<int>& delims, CxxLexerToken& token, wxString& consumed) |
| 363 |
{ |
| 364 |
// loop until we find the open brace |
| 365 |
CxxVariable::LexerToken::Vec_t v; |
| 366 |
int depth = 0; |
| 367 |
while(GetNextToken(token)) { |
| 368 |
v.push_back(CxxVariable::LexerToken(token, depth)); |
| 369 |
if(depth == 0) { |
| 370 |
if(delims.count(token.GetType())) { |
| 371 |
consumed = CxxVariable::PackType(v, m_standard); |
| 372 |
return token.GetType(); |
| 373 |
} else { |
| 374 |
switch(token.GetType()) { |
| 375 |
case '<': |
| 376 |
case '{': |
| 377 |
case '[': |
| 378 |
case '(': |
| 379 |
depth++; |
| 380 |
break; |
| 381 |
default: |
| 382 |
// ignore it |
| 383 |
break; |
| 384 |
} |
| 385 |
} |
| 386 |
} else { |
| 387 |
switch(token.GetType()) { |
| 388 |
case '>': |
| 389 |
case '}': |
| 390 |
case ']': |
| 391 |
case ')': |
| 392 |
depth--; |
| 393 |
break; |
| 394 |
default: |
| 395 |
// ignore it |
| 396 |
break; |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
return wxNOT_FOUND; |
| 401 |
} |
| 402 |
|
| 403 |
bool CxxVariableScanner::GetNextToken(CxxLexerToken& token) |
| 404 |
{ |
| 405 |
bool res = false; |
| 406 |
|
| 407 |
while(true) { |
| 408 |
res = ::LexerNext(m_scanner, token); |
| 409 |
if(!res) |
| 410 |
break; |
| 411 |
|
| 412 |
// Ignore any T_IDENTIFIER which is declared as macro |
| 413 |
if((token.GetType() == T_IDENTIFIER) && m_macros.count(token.GetWXString())) { |
| 414 |
continue; |
| 415 |
} |
| 416 |
break; |
| 417 |
} |
| 418 |
|
| 419 |
m_eof = !res; |
| 420 |
switch(token.GetType()) { |
| 421 |
case '(': |
| 422 |
++m_parenthesisDepth; |
| 423 |
break; |
| 424 |
case ')': |
| 425 |
--m_parenthesisDepth; |
| 426 |
break; |
| 427 |
default: |
| 428 |
break; |
| 429 |
} |
| 430 |
return res; |
| 431 |
} |
| 432 |
|
| 433 |
void CxxVariableScanner::DoOptimizeBuffer() |
| 434 |
{ |
| 435 |
if(m_buffer_optimized) { |
| 436 |
return; |
| 437 |
} |
| 438 |
|
| 439 |
Scanner_t sc = ::LexerNew(m_buffer); |
| 440 |
if(!sc) { |
| 441 |
clWARNING() << "CxxVariableScanner::DoOptimizeBuffer(): failed to create Scanner_t" << clEndl; |
| 442 |
return; // Failed to allocate scanner |
| 443 |
} |
| 444 |
|
| 445 |
CppLexerUserData* userData = ::LexerGetUserData(sc); |
| 446 |
CxxLexerToken tok; |
| 447 |
CxxLexerToken lastToken; |
| 448 |
|
| 449 |
// Cleanup |
| 450 |
m_buffers.clear(); |
| 451 |
PushBuffer(); |
| 452 |
int parenthesisDepth = 0; |
| 453 |
while(::LexerNext(sc, tok)) { |
| 454 |
// Skip prep processing state |
| 455 |
if(userData && userData->IsInPreProcessorSection()) { |
| 456 |
continue; |
| 457 |
} |
| 458 |
|
| 459 |
// Return the working buffer, which depends on the current state |
| 460 |
wxString& buffer = Buffer(); |
| 461 |
|
| 462 |
// Outer switch: state based |
| 463 |
switch(tok.GetType()) { |
| 464 |
case T_PP_STATE_EXIT: |
| 465 |
break; |
| 466 |
case T_FOR: { |
| 467 |
wxString variable_definition; |
| 468 |
if(OnForLoop(sc, variable_definition)) { |
| 469 |
// move the variable to the next scope |
| 470 |
Buffer() << "for () {"; |
| 471 |
PushBuffer(); |
| 472 |
Buffer() << variable_definition; |
| 473 |
} else { |
| 474 |
// single line for() |
| 475 |
Buffer() << "for ()"; |
| 476 |
} |
| 477 |
} break; |
| 478 |
case T_CATCH: |
| 479 |
OnCatch(sc); |
| 480 |
break; |
| 481 |
case T_DECLTYPE: |
| 482 |
OnDeclType(sc); |
| 483 |
break; |
| 484 |
case T_WHILE: |
| 485 |
OnWhile(sc); |
| 486 |
break; |
| 487 |
case '(': |
| 488 |
buffer << tok.GetWXString(); |
| 489 |
if(skip_parenthesis_block(sc)) { |
| 490 |
buffer << ")"; |
| 491 |
} |
| 492 |
break; |
| 493 |
case '{': |
| 494 |
buffer << tok.GetWXString(); |
| 495 |
PushBuffer(); |
| 496 |
break; |
| 497 |
case '}': |
| 498 |
buffer = PopBuffer(); |
| 499 |
// The closing curly bracket is added *after* we switch buffers |
| 500 |
buffer << tok.GetWXString(); |
| 501 |
break; |
| 502 |
case ')': |
| 503 |
--parenthesisDepth; |
| 504 |
buffer = PopBuffer(); |
| 505 |
buffer << ")"; |
| 506 |
break; |
| 507 |
default: |
| 508 |
buffer << tok.GetWXString() << " "; |
| 509 |
break; |
| 510 |
} |
| 511 |
lastToken = tok; |
| 512 |
} |
| 513 |
::LexerDestroy(&sc); |
| 514 |
|
| 515 |
// Merge the buffers |
| 516 |
std::for_each(m_buffers.rbegin(), m_buffers.rend(), [&](const wxString& buffer) { |
| 517 |
// append the buffers in reverse order |
| 518 |
m_optimized_buffer << buffer; |
| 519 |
}); |
| 520 |
m_buffer_optimized = true; |
| 521 |
} |
| 522 |
|
| 523 |
CxxVariable::Vec_t CxxVariableScanner::DoGetVariables(const wxString& buffer, bool sort) |
| 524 |
{ |
| 525 |
// First, we strip all parenthesis content from the buffer |
| 526 |
m_scanner = ::LexerNew(buffer); |
| 527 |
m_eof = false; |
| 528 |
m_parenthesisDepth = 0; |
| 529 |
if(!m_scanner) |
| 530 |
return CxxVariable::Vec_t(); // Empty list |
| 531 |
|
| 532 |
CxxVariable::Vec_t vars; |
| 533 |
|
| 534 |
// Read the variable type |
| 535 |
while(!IsEof()) { |
| 536 |
bool isAuto; |
| 537 |
CxxVariable::LexerToken::Vec_t vartype; |
| 538 |
if(!ReadType(vartype, isAuto)) |
| 539 |
continue; |
| 540 |
|
| 541 |
// Get the variable(s) name |
| 542 |
wxString varname, pointerOrRef, varInitialization; |
| 543 |
bool cont = false; |
| 544 |
do { |
| 545 |
int line_number = wxNOT_FOUND; |
| 546 |
cont = ReadName(varname, pointerOrRef, line_number, varInitialization); |
| 547 |
CxxVariable::Ptr_t var(new CxxVariable(m_standard)); |
| 548 |
var->SetName(varname); |
| 549 |
var->SetType(vartype); |
| 550 |
var->SetDefaultValue(varInitialization); |
| 551 |
var->SetPointerOrReference(pointerOrRef); |
| 552 |
var->SetIsAuto(isAuto); |
| 553 |
var->SetLine(line_number); |
| 554 |
|
| 555 |
// the below condition fixes this type: |
| 556 |
// if(something && GetCtrl(). -> we can mistaken this as: VarType: something, VarName: GetCtrl, And |
| 557 |
// pointerOrRef: && |
| 558 |
bool not_ok = varInitialization.Contains("(") && pointerOrRef == "&&"; |
| 559 |
if(not_ok) { |
| 560 |
break; |
| 561 |
} else if(var->IsOk()) { |
| 562 |
vars.push_back(var); |
| 563 |
} else if(!varInitialization.IsEmpty()) { |
| 564 |
// This means that the above was a function call |
| 565 |
// Parse the siganture which is placed inside the varInitialization |
| 566 |
CxxVariableScanner scanner(varInitialization, m_standard, m_macros, true); |
| 567 |
CxxVariable::Vec_t args = scanner.GetVariables(sort); |
| 568 |
vars.insert(vars.end(), args.begin(), args.end()); |
| 569 |
break; |
| 570 |
} |
| 571 |
} while(cont && (m_parenthesisDepth == 0) /* not inside a function */); |
| 572 |
} |
| 573 |
|
| 574 |
::LexerDestroy(&m_scanner); |
| 575 |
return vars; |
| 576 |
} |
| 577 |
|
| 578 |
bool CxxVariableScanner::TypeHasIdentifier(const CxxVariable::LexerToken::Vec_t& type) |
| 579 |
{ |
| 580 |
// do we have an identifier in the type? |
| 581 |
CxxVariable::LexerToken::Vec_t::const_iterator iter = |
| 582 |
std::find_if(type.begin(), type.end(), |
| 583 |
[&](const CxxVariable::LexerToken& token) { return (token.GetType() == T_IDENTIFIER); }); |
| 584 |
return (iter != type.end()); |
| 585 |
} |
| 586 |
|
| 587 |
CxxVariable::Map_t CxxVariableScanner::GetVariablesMap() |
| 588 |
{ |
| 589 |
CxxVariable::Vec_t l = GetVariables(true); |
| 590 |
CxxVariable::Map_t m; |
| 591 |
std::for_each(l.begin(), l.end(), [&](CxxVariable::Ptr_t v) { |
| 592 |
if(m.count(v->GetName()) == 0) { |
| 593 |
m.insert(std::make_pair(v->GetName(), v)); |
| 594 |
} |
| 595 |
}); |
| 596 |
return m; |
| 597 |
} |
| 598 |
|
| 599 |
bool CxxVariableScanner::HasNativeTypeInList(const CxxVariable::LexerToken::Vec_t& type) const |
| 600 |
{ |
| 601 |
CxxVariable::LexerToken::Vec_t::const_iterator iter = |
| 602 |
std::find_if(type.begin(), type.end(), [&](const CxxVariable::LexerToken& token) { |
| 603 |
return ((token._depth == 0) && (m_nativeTypes.count(token.GetType()) != 0)); |
| 604 |
}); |
| 605 |
return (iter != type.end()); |
| 606 |
} |
| 607 |
|
| 608 |
CxxVariable::Vec_t CxxVariableScanner::DoParseFunctionArguments(const wxString& buffer) |
| 609 |
{ |
| 610 |
m_scanner = ::LexerNew(buffer); |
| 611 |
m_eof = false; |
| 612 |
m_parenthesisDepth = 0; |
| 613 |
if(!m_scanner) |
| 614 |
return CxxVariable::Vec_t(); // Empty list |
| 615 |
|
| 616 |
CxxVariable::Vec_t vars; |
| 617 |
|
| 618 |
// Read the variable type |
| 619 |
while(!IsEof()) { |
| 620 |
bool isAuto; |
| 621 |
CxxVariable::LexerToken::Vec_t vartype; |
| 622 |
if(!ReadType(vartype, isAuto)) |
| 623 |
continue; |
| 624 |
|
| 625 |
// Get the variable(s) name |
| 626 |
int line_number = wxNOT_FOUND; |
| 627 |
wxString varname, pointerOrRef, varInitialization; |
| 628 |
ReadName(varname, pointerOrRef, line_number, varInitialization); |
| 629 |
CxxVariable::Ptr_t var(new CxxVariable(m_standard)); |
| 630 |
var->SetName(varname); |
| 631 |
var->SetType(vartype); |
| 632 |
var->SetDefaultValue(varInitialization); |
| 633 |
var->SetPointerOrReference(pointerOrRef); |
| 634 |
var->SetIsAuto(isAuto); |
| 635 |
var->SetLine(line_number); |
| 636 |
vars.push_back(var); |
| 637 |
} |
| 638 |
::LexerDestroy(&m_scanner); |
| 639 |
return vars; |
| 640 |
} |
| 641 |
|
| 642 |
CxxVariable::Vec_t CxxVariableScanner::ParseFunctionArguments() { return DoParseFunctionArguments(m_buffer); } |
| 643 |
|
| 644 |
void CxxVariableScanner::UngetToken(const CxxLexerToken& token) |
| 645 |
{ |
| 646 |
::LexerUnget(m_scanner); |
| 647 |
|
| 648 |
// Fix the depth if needed |
| 649 |
if(token.GetType() == '(') { |
| 650 |
--m_parenthesisDepth; |
| 651 |
} else if(token.GetType() == ')') { |
| 652 |
++m_parenthesisDepth; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
wxString& CxxVariableScanner::Buffer() { return m_buffers[0]; } |
| 657 |
|
| 658 |
bool CxxVariableScanner::OnForLoop(Scanner_t scanner, wxString& variable_definition) |
| 659 |
{ |
| 660 |
CxxLexerToken tok; |
| 661 |
|
| 662 |
// The next token must be '(' |
| 663 |
if(!::LexerNext(scanner, tok)) |
| 664 |
return false; |
| 665 |
|
| 666 |
// Parser error |
| 667 |
if(tok.GetType() != '(') |
| 668 |
return false; |
| 669 |
|
| 670 |
constexpr int STATE_NORMAL = 0; |
| 671 |
constexpr int STATE_CXX11 = 1; |
| 672 |
int state = STATE_NORMAL; |
| 673 |
|
| 674 |
int depth = 0; |
| 675 |
bool cont = true; |
| 676 |
while(cont && ::LexerNext(scanner, tok)) { |
| 677 |
if(tok.is_keyword() || tok.is_builtin_type()) { |
| 678 |
variable_definition << " " << tok.GetWXString(); |
| 679 |
continue; |
| 680 |
} |
| 681 |
switch(tok.GetType()) { |
| 682 |
case '(': |
| 683 |
case '<': |
| 684 |
case '[': |
| 685 |
case '{': |
| 686 |
depth++; |
| 687 |
variable_definition << tok.GetWXString(); |
| 688 |
break; |
| 689 |
case '>': |
| 690 |
case ']': |
| 691 |
case '}': |
| 692 |
depth--; |
| 693 |
variable_definition << tok.GetWXString(); |
| 694 |
break; |
| 695 |
case ')': |
| 696 |
if(depth == 0) { |
| 697 |
// we are done |
| 698 |
cont = false; |
| 699 |
if(state == STATE_CXX11) { |
| 700 |
// append ".begin()" |
| 701 |
variable_definition << ".begin()"; |
| 702 |
} |
| 703 |
variable_definition << ";"; |
| 704 |
} else { |
| 705 |
variable_definition << ")"; |
| 706 |
depth--; |
| 707 |
} |
| 708 |
break; |
| 709 |
case ':': |
| 710 |
// c++11 ranged for loop |
| 711 |
state = STATE_CXX11; |
| 712 |
// we are going to create a variable of this type: |
| 713 |
// TYPENAME name = CONTAINER.begin(); |
| 714 |
variable_definition << "="; |
| 715 |
break; |
| 716 |
case T_IDENTIFIER: |
| 717 |
variable_definition << " " << tok.GetWXString(); |
| 718 |
break; |
| 719 |
case ';': |
| 720 |
// no need to check for depth, we cant have ';' in non depth 0 |
| 721 |
variable_definition << ";"; |
| 722 |
cont = false; |
| 723 |
break; |
| 724 |
default: |
| 725 |
variable_definition << tok.GetWXString(); |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
// read the remainder (for C++11 ranged loop we already consumed the closing parenthesis) |
| 731 |
if(state == STATE_NORMAL && !SkipToClosingParenthesis(scanner)) { |
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
// we are now expecting a '{' |
| 736 |
::LexerNext(scanner, tok); |
| 737 |
if(tok.GetType() != '{') |
| 738 |
return false; |
| 739 |
return true; |
| 740 |
} |
| 741 |
|
| 742 |
bool CxxVariableScanner::OnCatch(Scanner_t scanner) |
| 743 |
{ |
| 744 |
CxxLexerToken tok; |
| 745 |
|
| 746 |
// The next token must be '(' |
| 747 |
if(!::LexerNext(scanner, tok)) |
| 748 |
return false; |
| 749 |
|
| 750 |
// Parser error |
| 751 |
if(tok.GetType() != '(') |
| 752 |
return false; |
| 753 |
int depth(1); |
| 754 |
wxString& buffer = Buffer(); |
| 755 |
buffer << ";"; // Help the parser |
| 756 |
while(::LexerNext(scanner, tok)) { |
| 757 |
switch(tok.GetType()) { |
| 758 |
case '(': |
| 759 |
++depth; |
| 760 |
buffer << tok.GetWXString(); |
| 761 |
break; |
| 762 |
case ')': |
| 763 |
--depth; |
| 764 |
buffer << tok.GetWXString(); |
| 765 |
if(depth == 0) { |
| 766 |
return true; |
| 767 |
} |
| 768 |
break; |
| 769 |
default: |
| 770 |
buffer << tok.GetWXString() << " "; |
| 771 |
break; |
| 772 |
} |
| 773 |
} |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
bool CxxVariableScanner::OnWhile(Scanner_t scanner) |
| 778 |
{ |
| 779 |
|
| 780 |
CxxLexerToken tok; |
| 781 |
|
| 782 |
// The next token must be '(' |
| 783 |
if(!::LexerNext(scanner, tok)) |
| 784 |
return false; |
| 785 |
|
| 786 |
// Parser error |
| 787 |
if(tok.GetType() != '(') |
| 788 |
return false; |
| 789 |
int depth(1); |
| 790 |
while(::LexerNext(scanner, tok)) { |
| 791 |
switch(tok.GetType()) { |
| 792 |
case '(': |
| 793 |
++depth; |
| 794 |
break; |
| 795 |
case ')': |
| 796 |
--depth; |
| 797 |
if(depth == 0) |
| 798 |
return true; |
| 799 |
break; |
| 800 |
default: |
| 801 |
break; |
| 802 |
} |
| 803 |
} |
| 804 |
return false; |
| 805 |
} |
| 806 |
|
| 807 |
bool CxxVariableScanner::OnDeclType(Scanner_t scanner) |
| 808 |
{ |
| 809 |
CxxLexerToken tok; |
| 810 |
wxString& buffer = Buffer(); |
| 811 |
|
| 812 |
// The next token must be '(' |
| 813 |
if(!::LexerNext(scanner, tok)) |
| 814 |
return false; |
| 815 |
|
| 816 |
// Parser error |
| 817 |
if(tok.GetType() != '(') |
| 818 |
return false; |
| 819 |
int depth(1); |
| 820 |
buffer << "decltype("; |
| 821 |
while(::LexerNext(scanner, tok)) { |
| 822 |
switch(tok.GetType()) { |
| 823 |
case '(': |
| 824 |
++depth; |
| 825 |
buffer << tok.GetWXString(); |
| 826 |
break; |
| 827 |
case ')': |
| 828 |
--depth; |
| 829 |
buffer << ")"; |
| 830 |
if(depth == 0) { |
| 831 |
return true; |
| 832 |
} |
| 833 |
break; |
| 834 |
default: |
| 835 |
break; |
| 836 |
} |
| 837 |
} |
| 838 |
return false; |
| 839 |
} |
| 840 |
|
| 841 |
wxString& CxxVariableScanner::PushBuffer() |
| 842 |
{ |
| 843 |
wxString buffer; |
| 844 |
m_buffers.insert(m_buffers.begin(), buffer); |
| 845 |
return m_buffers[0]; |
| 846 |
} |
| 847 |
|
| 848 |
wxString& CxxVariableScanner::PopBuffer() |
| 849 |
{ |
| 850 |
if(m_buffers.size() > 1) { |
| 851 |
m_buffers.erase(m_buffers.begin()); |
| 852 |
} |
| 853 |
return m_buffers[0]; |
| 854 |
} |
| 855 |
|
| 856 |
bool CxxVariableScanner::SkipToClosingParenthesis(Scanner_t scanner) |
| 857 |
{ |
| 858 |
int depth = 0; |
| 859 |
CxxLexerToken tok; |
| 860 |
while(::LexerNext(scanner, tok)) { |
| 861 |
switch(tok.GetType()) { |
| 862 |
case '(': |
| 863 |
depth++; |
| 864 |
break; |
| 865 |
case ')': |
| 866 |
if(depth == 0) { |
| 867 |
return true; |
| 868 |
} |
| 869 |
depth--; |
| 870 |
break; |
| 871 |
default: |
| 872 |
break; |
| 873 |
} |
| 874 |
} |
| 875 |
return false; |
| 876 |
} |
| 877 |
|
| 878 |
wxString CxxVariableScanner::ToString(CxxVariable::LexerToken::Vec_t& vartype) |
| 879 |
{ |
| 880 |
wxString str; |
| 881 |
for(const auto& token : vartype) { |
| 882 |
str << token.text << " "; |
| 883 |
} |
| 884 |
str.Trim(); |
| 885 |
return str; |
| 886 |
} |
| 887 |
|
| 888 |
bool CxxVariableScanner::skip_parenthesis_block(Scanner_t scanner) |
| 889 |
{ |
| 890 |
int depth = 0; |
| 891 |
CxxLexerToken token; |
| 892 |
while(::LexerNext(scanner, token)) { |
| 893 |
// Skip prep processing state |
| 894 |
switch(token.GetType()) { |
| 895 |
case '(': |
| 896 |
depth++; |
| 897 |
break; |
| 898 |
case ')': |
| 899 |
if(depth == 0) { |
| 900 |
return true; |
| 901 |
} |
| 902 |
depth--; |
| 903 |
break; |
| 904 |
default: |
| 905 |
break; |
| 906 |
} |
| 907 |
} |
| 908 |
return false; |
| 909 |
} |
| 910 |
|
| 911 |
bool CxxVariableScanner::skip_curly_brackets_block(Scanner_t scanner) |
| 912 |
{ |
| 913 |
int depth = 0; |
| 914 |
CxxLexerToken token; |
| 915 |
while(::LexerNext(scanner, token)) { |
| 916 |
// Skip prep processing state |
| 917 |
switch(token.GetType()) { |
| 918 |
case '{': |
| 919 |
depth++; |
| 920 |
break; |
| 921 |
case '}': |
| 922 |
if(depth == 0) { |
| 923 |
return true; |
| 924 |
} |
| 925 |
depth--; |
| 926 |
break; |
| 927 |
default: |
| 928 |
break; |
| 929 |
} |
| 930 |
} |
| 931 |
return false; |
| 932 |
} |