test_gdscript.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**************************************************************************/
  2. /* test_gdscript.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "test_gdscript.h"
  31. #include "../gdscript_analyzer.h"
  32. #include "../gdscript_compiler.h"
  33. #include "../gdscript_parser.h"
  34. #include "../gdscript_tokenizer.h"
  35. #include "../gdscript_tokenizer_buffer.h"
  36. #include "core/config/project_settings.h"
  37. #include "core/io/file_access.h"
  38. #include "core/io/file_access_pack.h"
  39. #include "core/os/main_loop.h"
  40. #include "core/os/os.h"
  41. #include "core/string/string_builder.h"
  42. #include "scene/resources/packed_scene.h"
  43. #ifdef TOOLS_ENABLED
  44. #include "editor/editor_settings.h"
  45. #endif
  46. namespace GDScriptTests {
  47. static void test_tokenizer(const String &p_code, const Vector<String> &p_lines) {
  48. GDScriptTokenizerText tokenizer;
  49. tokenizer.set_source_code(p_code);
  50. int tab_size = 4;
  51. #ifdef TOOLS_ENABLED
  52. if (EditorSettings::get_singleton()) {
  53. tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size");
  54. }
  55. #endif // TOOLS_ENABLED
  56. String tab = String(" ").repeat(tab_size);
  57. GDScriptTokenizer::Token current = tokenizer.scan();
  58. while (current.type != GDScriptTokenizer::Token::TK_EOF) {
  59. StringBuilder token;
  60. token += " --> "; // Padding for line number.
  61. for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) {
  62. print_line(vformat("%04d %s", l, p_lines[l - 1]).replace("\t", tab));
  63. }
  64. {
  65. // Print carets to point at the token.
  66. StringBuilder pointer;
  67. pointer += " "; // Padding for line number.
  68. int rightmost_column = current.rightmost_column;
  69. if (current.end_line > current.start_line) {
  70. rightmost_column--; // Don't point to the newline as a column.
  71. }
  72. for (int col = 1; col < rightmost_column; col++) {
  73. if (col < current.leftmost_column) {
  74. pointer += " ";
  75. } else {
  76. pointer += "^";
  77. }
  78. }
  79. print_line(pointer.as_string());
  80. }
  81. token += current.get_name();
  82. if (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::LITERAL || current.type == GDScriptTokenizer::Token::IDENTIFIER || current.type == GDScriptTokenizer::Token::ANNOTATION) {
  83. token += "(";
  84. token += Variant::get_type_name(current.literal.get_type());
  85. token += ") ";
  86. token += current.literal;
  87. }
  88. print_line(token.as_string());
  89. print_line("-------------------------------------------------------");
  90. current = tokenizer.scan();
  91. }
  92. print_line(current.get_name()); // Should be EOF
  93. }
  94. static void test_tokenizer_buffer(const Vector<uint8_t> &p_buffer, const Vector<String> &p_lines);
  95. static void test_tokenizer_buffer(const String &p_code, const Vector<String> &p_lines) {
  96. Vector<uint8_t> binary = GDScriptTokenizerBuffer::parse_code_string(p_code);
  97. test_tokenizer_buffer(binary, p_lines);
  98. }
  99. static void test_tokenizer_buffer(const Vector<uint8_t> &p_buffer, const Vector<String> &p_lines) {
  100. GDScriptTokenizerBuffer tokenizer;
  101. tokenizer.set_code_buffer(p_buffer);
  102. int tab_size = 4;
  103. #ifdef TOOLS_ENABLED
  104. if (EditorSettings::get_singleton()) {
  105. tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size");
  106. }
  107. #endif // TOOLS_ENABLED
  108. String tab = String(" ").repeat(tab_size);
  109. GDScriptTokenizer::Token current = tokenizer.scan();
  110. while (current.type != GDScriptTokenizer::Token::TK_EOF) {
  111. StringBuilder token;
  112. token += " --> "; // Padding for line number.
  113. for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) {
  114. print_line(vformat("%04d %s", l, p_lines[l - 1]).replace("\t", tab));
  115. }
  116. token += current.get_name();
  117. if (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::LITERAL || current.type == GDScriptTokenizer::Token::IDENTIFIER || current.type == GDScriptTokenizer::Token::ANNOTATION) {
  118. token += "(";
  119. token += Variant::get_type_name(current.literal.get_type());
  120. token += ") ";
  121. token += current.literal;
  122. }
  123. print_line(token.as_string());
  124. print_line("-------------------------------------------------------");
  125. current = tokenizer.scan();
  126. }
  127. print_line(current.get_name()); // Should be EOF
  128. }
  129. static void test_parser(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
  130. GDScriptParser parser;
  131. Error err = parser.parse(p_code, p_script_path, false);
  132. if (err != OK) {
  133. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  134. for (const GDScriptParser::ParserError &error : errors) {
  135. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  136. }
  137. }
  138. GDScriptAnalyzer analyzer(&parser);
  139. err = analyzer.analyze();
  140. if (err != OK) {
  141. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  142. for (const GDScriptParser::ParserError &error : errors) {
  143. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  144. }
  145. }
  146. #ifdef TOOLS_ENABLED
  147. GDScriptParser::TreePrinter printer;
  148. printer.print_tree(parser);
  149. #endif
  150. }
  151. static void recursively_disassemble_functions(const Ref<GDScript> script, const Vector<String> &p_lines) {
  152. for (const KeyValue<StringName, GDScriptFunction *> &E : script->get_member_functions()) {
  153. const GDScriptFunction *func = E.value;
  154. const MethodInfo &mi = func->get_method_info();
  155. String signature = "Disassembling " + mi.name + "(";
  156. for (int i = 0; i < mi.arguments.size(); i++) {
  157. if (i > 0) {
  158. signature += ", ";
  159. }
  160. signature += mi.arguments[i].name;
  161. }
  162. print_line(signature + ")");
  163. #ifdef TOOLS_ENABLED
  164. func->disassemble(p_lines);
  165. #endif
  166. print_line("");
  167. print_line("");
  168. }
  169. for (const KeyValue<StringName, Ref<GDScript>> &F : script->get_subclasses()) {
  170. const Ref<GDScript> inner_script = F.value;
  171. print_line("");
  172. print_line(vformat("Inner Class: %s", inner_script->get_local_name()));
  173. print_line("");
  174. recursively_disassemble_functions(inner_script, p_lines);
  175. }
  176. }
  177. static void test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
  178. GDScriptParser parser;
  179. Error err = parser.parse(p_code, p_script_path, false);
  180. if (err != OK) {
  181. print_line("Error in parser:");
  182. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  183. for (const GDScriptParser::ParserError &error : errors) {
  184. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  185. }
  186. return;
  187. }
  188. GDScriptAnalyzer analyzer(&parser);
  189. err = analyzer.analyze();
  190. if (err != OK) {
  191. print_line("Error in analyzer:");
  192. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  193. for (const GDScriptParser::ParserError &error : errors) {
  194. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  195. }
  196. return;
  197. }
  198. GDScriptCompiler compiler;
  199. Ref<GDScript> script;
  200. script.instantiate();
  201. script->set_path(p_script_path);
  202. err = compiler.compile(&parser, script.ptr(), false);
  203. if (err) {
  204. print_line("Error in compiler:");
  205. print_line(vformat("%02d:%02d: %s", compiler.get_error_line(), compiler.get_error_column(), compiler.get_error()));
  206. return;
  207. }
  208. recursively_disassemble_functions(script, p_lines);
  209. }
  210. void test(TestType p_type) {
  211. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  212. if (cmdlargs.is_empty()) {
  213. return;
  214. }
  215. String test = cmdlargs.back()->get();
  216. if (!test.ends_with(".gd") && !test.ends_with(".gdc")) {
  217. print_line("This test expects a path to a GDScript file as its last parameter. Got: " + test);
  218. return;
  219. }
  220. Ref<FileAccess> fa = FileAccess::open(test, FileAccess::READ);
  221. ERR_FAIL_COND_MSG(fa.is_null(), "Could not open file: " + test);
  222. // Initialize the language for the test routine.
  223. init_language(fa->get_path_absolute().get_base_dir());
  224. // Load global classes.
  225. TypedArray<Dictionary> script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  226. for (int i = 0; i < script_classes.size(); i++) {
  227. Dictionary c = script_classes[i];
  228. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
  229. continue;
  230. }
  231. ScriptServer::add_global_class(c["class"], c["base"], c["language"], c["path"]);
  232. }
  233. Vector<uint8_t> buf;
  234. uint64_t flen = fa->get_length();
  235. buf.resize(flen + 1);
  236. fa->get_buffer(buf.ptrw(), flen);
  237. buf.write[flen] = 0;
  238. String code;
  239. code.parse_utf8((const char *)&buf[0]);
  240. Vector<String> lines;
  241. int last = 0;
  242. for (int i = 0; i <= code.length(); i++) {
  243. if (code[i] == '\n' || code[i] == 0) {
  244. lines.push_back(code.substr(last, i - last));
  245. last = i + 1;
  246. }
  247. }
  248. switch (p_type) {
  249. case TEST_TOKENIZER:
  250. test_tokenizer(code, lines);
  251. break;
  252. case TEST_TOKENIZER_BUFFER:
  253. if (test.ends_with(".gdc")) {
  254. test_tokenizer_buffer(buf, lines);
  255. } else {
  256. test_tokenizer_buffer(code, lines);
  257. }
  258. break;
  259. case TEST_PARSER:
  260. test_parser(code, test, lines);
  261. break;
  262. case TEST_COMPILER:
  263. test_compiler(code, test, lines);
  264. break;
  265. case TEST_BYTECODE:
  266. print_line("Not implemented.");
  267. }
  268. finish_language();
  269. }
  270. } // namespace GDScriptTests