| 56 | namespace GDScriptTests { |
| 57 | |
| 58 | static void test_tokenizer(const String &p_code, const Vector<String> &p_lines) { |
| 59 | GDScriptTokenizerText tokenizer; |
| 60 | tokenizer.set_source_code(p_code); |
| 61 | |
| 62 | int tab_size = 4; |
| 63 | #ifdef TOOLS_ENABLED |
| 64 | if (EditorSettings::get_singleton()) { |
| 65 | tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size"); |
| 66 | } |
| 67 | #endif // TOOLS_ENABLED |
| 68 | String tab = String(" ").repeat(tab_size); |
| 69 | |
| 70 | GDScriptTokenizer::Token current = tokenizer.scan(); |
| 71 | while (current.type != GDScriptTokenizer::Token::TK_EOF) { |
| 72 | StringBuilder token; |
| 73 | token += " --> "; // Padding for line number. |
| 74 | |
| 75 | if (current.start_line != current.end_line) { |
| 76 | // Print "vvvvvv" to point at the token. |
| 77 | StringBuilder pointer; |
| 78 | pointer += " "; // Padding for line number. |
| 79 | |
| 80 | int line_width = 0; |
| 81 | if (current.start_line - 1 >= 0 && current.start_line - 1 < p_lines.size()) { |
| 82 | line_width = p_lines[current.start_line - 1].replace("\t", tab).length(); |
| 83 | } |
| 84 | |
| 85 | const int offset = MAX(0, current.start_column - 1); |
| 86 | const int width = MAX(0, line_width - current.start_column + 1); |
| 87 | pointer += String::chr(' ').repeat(offset) + String::chr('v').repeat(width); |
| 88 | |
| 89 | print_line(pointer.as_string()); |
| 90 | } |
| 91 | |
| 92 | for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) { |
| 93 | print_line(vformat("%04d %s", l, p_lines[l - 1]).replace("\t", tab)); |
| 94 | } |
| 95 | |
| 96 | { |
| 97 | // Print "^^^^^^" to point at the token. |
| 98 | StringBuilder pointer; |
| 99 | pointer += " "; // Padding for line number. |
| 100 | |
| 101 | if (current.start_line == current.end_line) { |
| 102 | const int offset = MAX(0, current.start_column - 1); |
| 103 | const int width = MAX(0, current.end_column - current.start_column); |
| 104 | pointer += String::chr(' ').repeat(offset) + String::chr('^').repeat(width); |
| 105 | } else { |
| 106 | const int width = MAX(0, current.end_column - 1); |
| 107 | pointer += String::chr('^').repeat(width); |
| 108 | } |
| 109 | |
| 110 | print_line(pointer.as_string()); |
| 111 | } |
| 112 | |
| 113 | token += current.get_name(); |
| 114 | |
| 115 | if (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::LITERAL || current.type == GDScriptTokenizer::Token::IDENTIFIER || current.type == GDScriptTokenizer::Token::ANNOTATION) { |
no test coverage detected