test_gdscript.cpp 8.2 KB

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