| 349 | } |
| 350 | |
| 351 | Error GDScriptParser::parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body) { |
| 352 | clear(); |
| 353 | |
| 354 | String source = p_source_code; |
| 355 | int cursor_line = -1; |
| 356 | int cursor_column = -1; |
| 357 | for_completion = p_for_completion; |
| 358 | parse_body = p_parse_body; |
| 359 | |
| 360 | int tab_size = 4; |
| 361 | #ifdef TOOLS_ENABLED |
| 362 | if (EditorSettings::get_singleton()) { |
| 363 | tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size"); |
| 364 | } |
| 365 | #endif // TOOLS_ENABLED |
| 366 | |
| 367 | if (p_for_completion) { |
| 368 | // Remove cursor sentinel char. |
| 369 | const Vector<String> lines = p_source_code.split("\n"); |
| 370 | cursor_line = 1; |
| 371 | cursor_column = 1; |
| 372 | for (int i = 0; i < lines.size(); i++) { |
| 373 | bool found = false; |
| 374 | const String &line = lines[i]; |
| 375 | for (int j = 0; j < line.size(); j++) { |
| 376 | if (line[j] == char32_t(0xFFFF)) { |
| 377 | found = true; |
| 378 | break; |
| 379 | } else if (line[j] == '\t') { |
| 380 | cursor_column += tab_size - 1; |
| 381 | } |
| 382 | cursor_column++; |
| 383 | } |
| 384 | if (found) { |
| 385 | break; |
| 386 | } |
| 387 | cursor_line++; |
| 388 | cursor_column = 1; |
| 389 | } |
| 390 | |
| 391 | source = source.replace_first(String::chr(0xFFFF), String()); |
| 392 | } |
| 393 | |
| 394 | GDScriptTokenizerText *text_tokenizer = memnew(GDScriptTokenizerText); |
| 395 | text_tokenizer->set_source_code(source); |
| 396 | |
| 397 | tokenizer = text_tokenizer; |
| 398 | |
| 399 | tokenizer->set_cursor_position(cursor_line, cursor_column); |
| 400 | script_path = p_script_path.simplify_path(); |
| 401 | current = tokenizer->scan(); |
| 402 | // Avoid error or newline as the first token. |
| 403 | // The latter can mess with the parser when opening files filled exclusively with comments and newlines. |
| 404 | while (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::NEWLINE) { |
| 405 | if (current.type == GDScriptTokenizer::Token::ERROR) { |
| 406 | push_error(current.literal); |
| 407 | } |
| 408 | current = tokenizer->scan(); |
no test coverage detected