MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / test_tokenizer

Function test_tokenizer

modules/gdscript/tests/test_gdscript.cpp:58–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56namespace GDScriptTests {
57
58static 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) {

Callers 1

testFunction · 0.70

Calls 14

MAXFunction · 0.85
print_lineFunction · 0.85
vformatFunction · 0.85
repeatMethod · 0.80
sizeMethod · 0.65
StringClass · 0.50
set_source_codeMethod · 0.45
get_settingMethod · 0.45
scanMethod · 0.45
lengthMethod · 0.45
replaceMethod · 0.45
as_stringMethod · 0.45

Tested by

no test coverage detected