test_gdscript.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*************************************************************************/
  2. /* test_gdscript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_pack.h"
  33. #include "core/os/file_access.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/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 List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
  99. const GDScriptParser::ParserError &error = E->get();
  100. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  101. }
  102. }
  103. GDScriptAnalyzer analyzer(&parser);
  104. analyzer.analyze();
  105. if (err != OK) {
  106. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  107. for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
  108. const GDScriptParser::ParserError &error = E->get();
  109. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  110. }
  111. }
  112. #ifdef TOOLS_ENABLED
  113. GDScriptParser::TreePrinter printer;
  114. printer.print_tree(parser);
  115. #endif
  116. }
  117. static void test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
  118. GDScriptParser parser;
  119. Error err = parser.parse(p_code, p_script_path, false);
  120. if (err != OK) {
  121. print_line("Error in parser:");
  122. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  123. for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
  124. const GDScriptParser::ParserError &error = E->get();
  125. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  126. }
  127. return;
  128. }
  129. GDScriptAnalyzer analyzer(&parser);
  130. err = analyzer.analyze();
  131. if (err != OK) {
  132. print_line("Error in analyzer:");
  133. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  134. for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
  135. const GDScriptParser::ParserError &error = E->get();
  136. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  137. }
  138. return;
  139. }
  140. GDScriptCompiler compiler;
  141. Ref<GDScript> script;
  142. script.instance();
  143. script->set_path(p_script_path);
  144. err = compiler.compile(&parser, script.ptr(), false);
  145. if (err) {
  146. print_line("Error in compiler:");
  147. print_line(vformat("%02d:%02d: %s", compiler.get_error_line(), compiler.get_error_column(), compiler.get_error()));
  148. return;
  149. }
  150. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  151. const GDScriptFunction *func = E->value();
  152. String signature = "Disassembling " + func->get_name().operator String() + "(";
  153. for (int i = 0; i < func->get_argument_count(); i++) {
  154. if (i > 0) {
  155. signature += ", ";
  156. }
  157. signature += func->get_argument_name(i);
  158. }
  159. print_line(signature + ")");
  160. #ifdef TOOLS_ENABLED
  161. func->disassemble(p_lines);
  162. #endif
  163. print_line("");
  164. print_line("");
  165. }
  166. }
  167. void test(TestType p_type) {
  168. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  169. if (cmdlargs.is_empty()) {
  170. return;
  171. }
  172. String test = cmdlargs.back()->get();
  173. if (!test.ends_with(".gd")) {
  174. print_line("This test expects a path to a GDScript file as its last parameter. Got: " + test);
  175. return;
  176. }
  177. FileAccessRef fa = FileAccess::open(test, FileAccess::READ);
  178. ERR_FAIL_COND_MSG(!fa, "Could not open file: " + test);
  179. // Initialize the language for the test routine.
  180. init_language(fa->get_path_absolute().get_base_dir());
  181. Vector<uint8_t> buf;
  182. uint64_t flen = fa->get_len();
  183. buf.resize(fa->get_len() + 1);
  184. fa->get_buffer(buf.ptrw(), flen);
  185. buf.write[flen] = 0;
  186. String code;
  187. code.parse_utf8((const char *)&buf[0]);
  188. Vector<String> lines;
  189. int last = 0;
  190. for (int i = 0; i <= code.length(); i++) {
  191. if (code[i] == '\n' || code[i] == 0) {
  192. lines.push_back(code.substr(last, i - last));
  193. last = i + 1;
  194. }
  195. }
  196. switch (p_type) {
  197. case TEST_TOKENIZER:
  198. test_tokenizer(code, lines);
  199. break;
  200. case TEST_PARSER:
  201. test_parser(code, test, lines);
  202. break;
  203. case TEST_COMPILER:
  204. test_compiler(code, test, lines);
  205. break;
  206. case TEST_BYTECODE:
  207. print_line("Not implemented.");
  208. }
  209. finish_language();
  210. }
  211. } // namespace GDScriptTests