test_gdscript.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.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 test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
  116. GDScriptParser parser;
  117. Error err = parser.parse(p_code, p_script_path, false);
  118. if (err != OK) {
  119. print_line("Error in parser:");
  120. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  121. for (const GDScriptParser::ParserError &error : errors) {
  122. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  123. }
  124. return;
  125. }
  126. GDScriptAnalyzer analyzer(&parser);
  127. err = analyzer.analyze();
  128. if (err != OK) {
  129. print_line("Error in analyzer:");
  130. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  131. for (const GDScriptParser::ParserError &error : errors) {
  132. print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
  133. }
  134. return;
  135. }
  136. GDScriptCompiler compiler;
  137. Ref<GDScript> script;
  138. script.instantiate();
  139. script->set_path(p_script_path);
  140. err = compiler.compile(&parser, script.ptr(), false);
  141. if (err) {
  142. print_line("Error in compiler:");
  143. print_line(vformat("%02d:%02d: %s", compiler.get_error_line(), compiler.get_error_column(), compiler.get_error()));
  144. return;
  145. }
  146. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  147. const GDScriptFunction *func = E->value();
  148. String signature = "Disassembling " + func->get_name().operator String() + "(";
  149. for (int i = 0; i < func->get_argument_count(); i++) {
  150. if (i > 0) {
  151. signature += ", ";
  152. }
  153. signature += func->get_argument_name(i);
  154. }
  155. print_line(signature + ")");
  156. #ifdef TOOLS_ENABLED
  157. func->disassemble(p_lines);
  158. #endif
  159. print_line("");
  160. print_line("");
  161. }
  162. }
  163. void test(TestType p_type) {
  164. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  165. if (cmdlargs.is_empty()) {
  166. return;
  167. }
  168. String test = cmdlargs.back()->get();
  169. if (!test.ends_with(".gd")) {
  170. print_line("This test expects a path to a GDScript file as its last parameter. Got: " + test);
  171. return;
  172. }
  173. FileAccessRef fa = FileAccess::open(test, FileAccess::READ);
  174. ERR_FAIL_COND_MSG(!fa, "Could not open file: " + test);
  175. // Initialize the language for the test routine.
  176. init_language(fa->get_path_absolute().get_base_dir());
  177. Vector<uint8_t> buf;
  178. uint64_t flen = fa->get_length();
  179. buf.resize(flen + 1);
  180. fa->get_buffer(buf.ptrw(), flen);
  181. buf.write[flen] = 0;
  182. String code;
  183. code.parse_utf8((const char *)&buf[0]);
  184. Vector<String> lines;
  185. int last = 0;
  186. for (int i = 0; i <= code.length(); i++) {
  187. if (code[i] == '\n' || code[i] == 0) {
  188. lines.push_back(code.substr(last, i - last));
  189. last = i + 1;
  190. }
  191. }
  192. switch (p_type) {
  193. case TEST_TOKENIZER:
  194. test_tokenizer(code, lines);
  195. break;
  196. case TEST_PARSER:
  197. test_parser(code, test, lines);
  198. break;
  199. case TEST_COMPILER:
  200. test_compiler(code, test, lines);
  201. break;
  202. case TEST_BYTECODE:
  203. print_line("Not implemented.");
  204. }
  205. finish_language();
  206. }
  207. } // namespace GDScriptTests