test_gdscript.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "core/config/project_settings.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/file_access_pack.h"
  34. #include "core/os/main_loop.h"
  35. #include "core/os/os.h"
  36. #include "core/string/string_builder.h"
  37. #include "scene/resources/packed_scene.h"
  38. #include "modules/gdscript/gdscript_analyzer.h"
  39. #include "modules/gdscript/gdscript_compiler.h"
  40. #include "modules/gdscript/gdscript_parser.h"
  41. #include "modules/gdscript/gdscript_tokenizer.h"
  42. #ifdef TOOLS_ENABLED
  43. #include "editor/editor_settings.h"
  44. #endif
  45. namespace GDScriptTests {
  46. static void test_tokenizer(const String &p_code, const Vector<String> &p_lines) {
  47. GDScriptTokenizer tokenizer;
  48. tokenizer.set_source_code(p_code);
  49. int tab_size = 4;
  50. #ifdef TOOLS_ENABLED
  51. if (EditorSettings::get_singleton()) {
  52. tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size");
  53. }
  54. #endif // TOOLS_ENABLED
  55. String tab = String(" ").repeat(tab_size);
  56. GDScriptTokenizer::Token current = tokenizer.scan();
  57. while (current.type != GDScriptTokenizer::Token::TK_EOF) {
  58. StringBuilder token;
  59. token += " --> "; // Padding for line number.
  60. for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) {
  61. print_line(vformat("%04d %s", l, p_lines[l - 1]).replace("\t", tab));
  62. }
  63. {
  64. // Print carets to point at the token.
  65. StringBuilder pointer;
  66. pointer += " "; // Padding for line number.
  67. int rightmost_column = current.rightmost_column;
  68. if (current.end_line > current.start_line) {
  69. rightmost_column--; // Don't point to the newline as a column.
  70. }
  71. for (int col = 1; col < rightmost_column; col++) {
  72. if (col < current.leftmost_column) {
  73. pointer += " ";
  74. } else {
  75. pointer += "^";
  76. }
  77. }
  78. print_line(pointer.as_string());
  79. }
  80. token += current.get_name();
  81. if (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::LITERAL || current.type == GDScriptTokenizer::Token::IDENTIFIER || current.type == GDScriptTokenizer::Token::ANNOTATION) {
  82. token += "(";
  83. token += Variant::get_type_name(current.literal.get_type());
  84. token += ") ";
  85. token += current.literal;
  86. }
  87. print_line(token.as_string());
  88. print_line("-------------------------------------------------------");
  89. current = tokenizer.scan();
  90. }
  91. print_line(current.get_name()); // Should be EOF
  92. }
  93. static void test_parser(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
  94. GDScriptParser parser;
  95. Error err = parser.parse(p_code, p_script_path, false);
  96. if (err != OK) {
  97. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  98. for (const GDScriptParser::ParserError &error : errors) {
  99. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  100. }
  101. }
  102. GDScriptAnalyzer analyzer(&parser);
  103. analyzer.analyze();
  104. if (err != OK) {
  105. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  106. for (const GDScriptParser::ParserError &error : errors) {
  107. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  108. }
  109. }
  110. #ifdef TOOLS_ENABLED
  111. GDScriptParser::TreePrinter printer;
  112. printer.print_tree(parser);
  113. #endif
  114. }
  115. static void recursively_disassemble_functions(const Ref<GDScript> script, const Vector<String> &p_lines) {
  116. for (const KeyValue<StringName, GDScriptFunction *> &E : script->get_member_functions()) {
  117. const GDScriptFunction *func = E.value;
  118. String signature = "Disassembling " + func->get_name().operator String() + "(";
  119. for (int i = 0; i < func->get_argument_count(); i++) {
  120. if (i > 0) {
  121. signature += ", ";
  122. }
  123. signature += func->get_argument_name(i);
  124. }
  125. print_line(signature + ")");
  126. #ifdef TOOLS_ENABLED
  127. func->disassemble(p_lines);
  128. #endif
  129. print_line("");
  130. print_line("");
  131. }
  132. for (const KeyValue<StringName, Ref<GDScript>> &F : script->get_subclasses()) {
  133. const Ref<GDScript> inner_script = F.value;
  134. print_line("");
  135. print_line(vformat("Inner Class: %s", inner_script->get_script_class_name()));
  136. print_line("");
  137. recursively_disassemble_functions(inner_script, p_lines);
  138. }
  139. }
  140. static void test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
  141. GDScriptParser parser;
  142. Error err = parser.parse(p_code, p_script_path, false);
  143. if (err != OK) {
  144. print_line("Error in parser:");
  145. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  146. for (const GDScriptParser::ParserError &error : errors) {
  147. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  148. }
  149. return;
  150. }
  151. GDScriptAnalyzer analyzer(&parser);
  152. err = analyzer.analyze();
  153. if (err != OK) {
  154. print_line("Error in analyzer:");
  155. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  156. for (const GDScriptParser::ParserError &error : errors) {
  157. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  158. }
  159. return;
  160. }
  161. GDScriptCompiler compiler;
  162. Ref<GDScript> script;
  163. script.instantiate();
  164. script->set_path(p_script_path);
  165. err = compiler.compile(&parser, script.ptr(), false);
  166. if (err) {
  167. print_line("Error in compiler:");
  168. print_line(vformat("%02d:%02d: %s", compiler.get_error_line(), compiler.get_error_column(), compiler.get_error()));
  169. return;
  170. }
  171. recursively_disassemble_functions(script, p_lines);
  172. }
  173. void test(TestType p_type) {
  174. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  175. if (cmdlargs.is_empty()) {
  176. return;
  177. }
  178. String test = cmdlargs.back()->get();
  179. if (!test.ends_with(".gd")) {
  180. print_line("This test expects a path to a GDScript file as its last parameter. Got: " + test);
  181. return;
  182. }
  183. Ref<FileAccess> fa = FileAccess::open(test, FileAccess::READ);
  184. ERR_FAIL_COND_MSG(fa.is_null(), "Could not open file: " + test);
  185. // Initialize the language for the test routine.
  186. init_language(fa->get_path_absolute().get_base_dir());
  187. Vector<uint8_t> buf;
  188. uint64_t flen = fa->get_length();
  189. buf.resize(flen + 1);
  190. fa->get_buffer(buf.ptrw(), flen);
  191. buf.write[flen] = 0;
  192. String code;
  193. code.parse_utf8((const char *)&buf[0]);
  194. Vector<String> lines;
  195. int last = 0;
  196. for (int i = 0; i <= code.length(); i++) {
  197. if (code[i] == '\n' || code[i] == 0) {
  198. lines.push_back(code.substr(last, i - last));
  199. last = i + 1;
  200. }
  201. }
  202. switch (p_type) {
  203. case TEST_TOKENIZER:
  204. test_tokenizer(code, lines);
  205. break;
  206. case TEST_PARSER:
  207. test_parser(code, test, lines);
  208. break;
  209. case TEST_COMPILER:
  210. test_compiler(code, test, lines);
  211. break;
  212. case TEST_BYTECODE:
  213. print_line("Not implemented.");
  214. }
  215. finish_language();
  216. }
  217. } // namespace GDScriptTests