Skip to content

Commit 2b6ad33

Browse files
committed
Add a tree printer for gdscript.
1 parent f4b0c7a commit 2b6ad33

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

modules/gdscript/doc_classes/GDScript.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@
2929
[/codeblock]
3030
</description>
3131
</method>
32+
<method name="print_tree">
33+
<return type="int" enum="Error" />
34+
<description>
35+
Parses the script and prints its syntax tree to the standard output. This method is mostly useful for debugging the GDScript parser and compiler. Returns [constant OK] on success or an error code if parsing fails.
36+
</description>
37+
</method>
3238
</methods>
3339
</class>

modules/gdscript/gdscript.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ void GDScript::_bind_methods() {
10161016
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &GDScript::_new, MethodInfo("new"));
10171017

10181018
ClassDB::bind_method(D_METHOD("get_as_byte_code"), &GDScript::get_as_byte_code);
1019+
ClassDB::bind_method(D_METHOD("print_tree"), &GDScript::print_tree);
10191020
}
10201021

10211022
Vector<uint8_t> GDScript::get_as_byte_code() const {
@@ -2381,3 +2382,20 @@ void ResourceFormatSaverGDScript::get_recognized_extensions(const RES &p_resourc
23812382
bool ResourceFormatSaverGDScript::recognize(const RES &p_resource) const {
23822383
return Object::cast_to<GDScript>(*p_resource) != nullptr;
23832384
}
2385+
2386+
Error GDScript::print_tree() {
2387+
GDScriptParser parser;
2388+
Error err = parser.parse(source, path, false);
2389+
if (err) {
2390+
if (EngineDebugger::is_active()) {
2391+
GDScriptLanguage::get_singleton()->debug_break_parse(_get_debug_path(), parser.get_errors().front()->get().line, "Parser Error: " + parser.get_errors().front()->get().message);
2392+
}
2393+
_err_print_error("GDScript::reload", path.is_empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_errors().front()->get().line, ("Parse Error: " + parser.get_errors().front()->get().message).utf8().get_data(), false, ERR_HANDLER_SCRIPT);
2394+
ERR_FAIL_V(ERR_PARSE_ERROR);
2395+
}
2396+
#ifdef TOOLS_ENABLED
2397+
GDScriptParser::TreePrinter printer;
2398+
printer.print_tree(parser);
2399+
#endif
2400+
return err;
2401+
}

modules/gdscript/gdscript.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class GDScript : public Script {
172172
static void _bind_methods();
173173

174174
public:
175+
virtual Error print_tree();
175176
virtual bool is_valid() const override { return valid; }
176177

177178
bool inherits_script(const Ref<Script> &p_script) const override;

0 commit comments

Comments
 (0)