test_gdscript.cpp 7.9 KB

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