Path: blob/master/modules/gdscript/gdscript_parser.cpp
11351 views
/**************************************************************************/1/* gdscript_parser.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gdscript_parser.h"3132#include "gdscript.h"33#include "gdscript_tokenizer_buffer.h"3435#include "core/config/project_settings.h"36#include "core/io/resource_loader.h"37#include "core/math/math_defs.h"38#include "scene/main/multiplayer_api.h"3940#ifdef DEBUG_ENABLED41#include "core/string/string_builder.h"42#include "servers/text/text_server.h"43#endif4445#ifdef TOOLS_ENABLED46#include "editor/settings/editor_settings.h"47#endif4849// This function is used to determine that a type is "built-in" as opposed to native50// and custom classes. So `Variant::NIL` and `Variant::OBJECT` are excluded:51// `Variant::NIL` - `null` is literal, not a type.52// `Variant::OBJECT` - `Object` should be treated as a class, not as a built-in type.53static HashMap<StringName, Variant::Type> builtin_types;54Variant::Type GDScriptParser::get_builtin_type(const StringName &p_type) {55if (unlikely(builtin_types.is_empty())) {56for (int i = 0; i < Variant::VARIANT_MAX; i++) {57Variant::Type type = (Variant::Type)i;58if (type != Variant::NIL && type != Variant::OBJECT) {59builtin_types[Variant::get_type_name(type)] = type;60}61}62}6364if (builtin_types.has(p_type)) {65return builtin_types[p_type];66}67return Variant::VARIANT_MAX;68}6970#ifdef TOOLS_ENABLED71HashMap<String, String> GDScriptParser::theme_color_names;72#endif7374HashMap<StringName, GDScriptParser::AnnotationInfo> GDScriptParser::valid_annotations;7576void GDScriptParser::cleanup() {77builtin_types.clear();78valid_annotations.clear();79}8081void GDScriptParser::get_annotation_list(List<MethodInfo> *r_annotations) const {82for (const KeyValue<StringName, AnnotationInfo> &E : valid_annotations) {83r_annotations->push_back(E.value.info);84}85}8687bool GDScriptParser::annotation_exists(const String &p_annotation_name) const {88return valid_annotations.has(p_annotation_name);89}9091GDScriptParser::GDScriptParser() {92// Register valid annotations.93if (unlikely(valid_annotations.is_empty())) {94// Script annotations.95register_annotation(MethodInfo("@tool"), AnnotationInfo::SCRIPT, &GDScriptParser::tool_annotation);96register_annotation(MethodInfo("@icon", PropertyInfo(Variant::STRING, "icon_path")), AnnotationInfo::SCRIPT, &GDScriptParser::icon_annotation);97register_annotation(MethodInfo("@static_unload"), AnnotationInfo::SCRIPT, &GDScriptParser::static_unload_annotation);98register_annotation(MethodInfo("@abstract"), AnnotationInfo::SCRIPT | AnnotationInfo::CLASS | AnnotationInfo::FUNCTION, &GDScriptParser::abstract_annotation);99// Onready annotation.100register_annotation(MethodInfo("@onready"), AnnotationInfo::VARIABLE, &GDScriptParser::onready_annotation);101// Export annotations.102register_annotation(MethodInfo("@export"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_NONE, Variant::NIL>);103register_annotation(MethodInfo("@export_enum", PropertyInfo(Variant::STRING, "names")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_ENUM, Variant::NIL>, varray(), true);104register_annotation(MethodInfo("@export_file", PropertyInfo(Variant::STRING, "filter")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_FILE, Variant::STRING>, varray(""), true);105register_annotation(MethodInfo("@export_file_path", PropertyInfo(Variant::STRING, "filter")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_FILE_PATH, Variant::STRING>, varray(""), true);106register_annotation(MethodInfo("@export_dir"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_DIR, Variant::STRING>);107register_annotation(MethodInfo("@export_global_file", PropertyInfo(Variant::STRING, "filter")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_GLOBAL_FILE, Variant::STRING>, varray(""), true);108register_annotation(MethodInfo("@export_global_dir"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_GLOBAL_DIR, Variant::STRING>);109register_annotation(MethodInfo("@export_multiline"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_MULTILINE_TEXT, Variant::STRING>);110register_annotation(MethodInfo("@export_placeholder", PropertyInfo(Variant::STRING, "placeholder")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_PLACEHOLDER_TEXT, Variant::STRING>);111register_annotation(MethodInfo("@export_range", PropertyInfo(Variant::FLOAT, "min"), PropertyInfo(Variant::FLOAT, "max"), PropertyInfo(Variant::FLOAT, "step"), PropertyInfo(Variant::STRING, "extra_hints")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_RANGE, Variant::FLOAT>, varray(1.0, ""), true);112register_annotation(MethodInfo("@export_exp_easing", PropertyInfo(Variant::STRING, "hints")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_EXP_EASING, Variant::FLOAT>, varray(""), true);113register_annotation(MethodInfo("@export_color_no_alpha"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_COLOR_NO_ALPHA, Variant::COLOR>);114register_annotation(MethodInfo("@export_node_path", PropertyInfo(Variant::STRING, "type")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_NODE_PATH_VALID_TYPES, Variant::NODE_PATH>, varray(""), true);115register_annotation(MethodInfo("@export_flags", PropertyInfo(Variant::STRING, "names")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_FLAGS, Variant::INT>, varray(), true);116register_annotation(MethodInfo("@export_flags_2d_render"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_2D_RENDER, Variant::INT>);117register_annotation(MethodInfo("@export_flags_2d_physics"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_2D_PHYSICS, Variant::INT>);118register_annotation(MethodInfo("@export_flags_2d_navigation"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_2D_NAVIGATION, Variant::INT>);119register_annotation(MethodInfo("@export_flags_3d_render"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_3D_RENDER, Variant::INT>);120register_annotation(MethodInfo("@export_flags_3d_physics"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_3D_PHYSICS, Variant::INT>);121register_annotation(MethodInfo("@export_flags_3d_navigation"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_3D_NAVIGATION, Variant::INT>);122register_annotation(MethodInfo("@export_flags_avoidance"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_AVOIDANCE, Variant::INT>);123register_annotation(MethodInfo("@export_storage"), AnnotationInfo::VARIABLE, &GDScriptParser::export_storage_annotation);124register_annotation(MethodInfo("@export_custom", PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "PropertyHint"), PropertyInfo(Variant::STRING, "hint_string"), PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_BITFIELD, "PropertyUsageFlags")), AnnotationInfo::VARIABLE, &GDScriptParser::export_custom_annotation, varray(PROPERTY_USAGE_DEFAULT));125register_annotation(MethodInfo("@export_tool_button", PropertyInfo(Variant::STRING, "text"), PropertyInfo(Variant::STRING, "icon")), AnnotationInfo::VARIABLE, &GDScriptParser::export_tool_button_annotation, varray(""));126// Export grouping annotations.127register_annotation(MethodInfo("@export_category", PropertyInfo(Variant::STRING, "name")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_CATEGORY>);128register_annotation(MethodInfo("@export_group", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "prefix")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_GROUP>, varray(""));129register_annotation(MethodInfo("@export_subgroup", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "prefix")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_SUBGROUP>, varray(""));130// Warning annotations.131register_annotation(MethodInfo("@warning_ignore", PropertyInfo(Variant::STRING, "warning")), AnnotationInfo::CLASS_LEVEL | AnnotationInfo::STATEMENT, &GDScriptParser::warning_ignore_annotation, varray(), true);132register_annotation(MethodInfo("@warning_ignore_start", PropertyInfo(Variant::STRING, "warning")), AnnotationInfo::STANDALONE, &GDScriptParser::warning_ignore_region_annotations, varray(), true);133register_annotation(MethodInfo("@warning_ignore_restore", PropertyInfo(Variant::STRING, "warning")), AnnotationInfo::STANDALONE, &GDScriptParser::warning_ignore_region_annotations, varray(), true);134// Networking.135register_annotation(MethodInfo("@rpc", PropertyInfo(Variant::STRING, "mode"), PropertyInfo(Variant::STRING, "sync"), PropertyInfo(Variant::STRING, "transfer_mode"), PropertyInfo(Variant::INT, "transfer_channel")), AnnotationInfo::FUNCTION, &GDScriptParser::rpc_annotation, varray("authority", "call_remote", "unreliable", 0));136}137138#ifdef DEBUG_ENABLED139is_ignoring_warnings = !(bool)GLOBAL_GET("debug/gdscript/warnings/enable");140for (int i = 0; i < GDScriptWarning::WARNING_MAX; i++) {141warning_ignore_start_lines[i] = INT_MAX;142}143#endif144145#ifdef TOOLS_ENABLED146if (unlikely(theme_color_names.is_empty())) {147// Vectors.148theme_color_names.insert("x", "axis_x_color");149theme_color_names.insert("y", "axis_y_color");150theme_color_names.insert("z", "axis_z_color");151theme_color_names.insert("w", "axis_w_color");152153// Color.154theme_color_names.insert("r", "axis_x_color");155theme_color_names.insert("r8", "axis_x_color");156theme_color_names.insert("g", "axis_y_color");157theme_color_names.insert("g8", "axis_y_color");158theme_color_names.insert("b", "axis_z_color");159theme_color_names.insert("b8", "axis_z_color");160theme_color_names.insert("a", "axis_w_color");161theme_color_names.insert("a8", "axis_w_color");162}163#endif164}165166GDScriptParser::~GDScriptParser() {167while (list != nullptr) {168Node *element = list;169list = list->next;170memdelete(element);171}172}173174void GDScriptParser::clear() {175GDScriptParser tmp;176tmp = *this;177*this = GDScriptParser();178}179180void GDScriptParser::push_error(const String &p_message, const Node *p_origin) {181// TODO: Improve error reporting by pointing at source code.182// TODO: Errors might point at more than one place at once (e.g. show previous declaration).183panic_mode = true;184// TODO: Improve positional information.185if (p_origin == nullptr) {186errors.push_back({ p_message, previous.start_line, previous.start_column });187} else {188errors.push_back({ p_message, p_origin->start_line, p_origin->start_column });189}190}191192#ifdef DEBUG_ENABLED193void GDScriptParser::push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols) {194ERR_FAIL_NULL(p_source);195ERR_FAIL_INDEX(p_code, GDScriptWarning::WARNING_MAX);196197if (is_ignoring_warnings) {198return;199}200if (GLOBAL_GET_CACHED(bool, "debug/gdscript/warnings/exclude_addons") && script_path.begins_with("res://addons/")) {201return;202}203GDScriptWarning::WarnLevel warn_level = (GDScriptWarning::WarnLevel)(int)GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(p_code));204if (warn_level == GDScriptWarning::IGNORE) {205return;206}207208PendingWarning pw;209pw.source = p_source;210pw.code = p_code;211pw.treated_as_error = warn_level == GDScriptWarning::ERROR;212pw.symbols = p_symbols;213214pending_warnings.push_back(pw);215}216217void GDScriptParser::apply_pending_warnings() {218for (const PendingWarning &pw : pending_warnings) {219if (warning_ignored_lines[pw.code].has(pw.source->start_line)) {220continue;221}222if (warning_ignore_start_lines[pw.code] <= pw.source->start_line) {223continue;224}225226GDScriptWarning warning;227warning.code = pw.code;228warning.symbols = pw.symbols;229warning.start_line = pw.source->start_line;230warning.end_line = pw.source->end_line;231232if (pw.treated_as_error) {233push_error(warning.get_message() + String(" (Warning treated as error.)"), pw.source);234continue;235}236237List<GDScriptWarning>::Element *before = nullptr;238for (List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {239if (E->get().start_line > warning.start_line) {240break;241}242before = E;243}244if (before) {245warnings.insert_after(before, warning);246} else {247warnings.push_front(warning);248}249}250251pending_warnings.clear();252}253#endif // DEBUG_ENABLED254255void GDScriptParser::override_completion_context(const Node *p_for_node, CompletionType p_type, Node *p_node, int p_argument) {256if (!for_completion) {257return;258}259if (p_for_node == nullptr || completion_context.node != p_for_node) {260return;261}262CompletionContext context;263context.type = p_type;264context.current_class = current_class;265context.current_function = current_function;266context.current_suite = current_suite;267context.current_line = tokenizer->get_cursor_line();268context.current_argument = p_argument;269context.node = p_node;270context.parser = this;271if (!completion_call_stack.is_empty()) {272context.call = completion_call_stack.back()->get();273}274completion_context = context;275}276277void GDScriptParser::make_completion_context(CompletionType p_type, Node *p_node, int p_argument, bool p_force) {278if (!for_completion || (!p_force && completion_context.type != COMPLETION_NONE)) {279return;280}281if (previous.cursor_place != GDScriptTokenizerText::CURSOR_MIDDLE && previous.cursor_place != GDScriptTokenizerText::CURSOR_END && current.cursor_place == GDScriptTokenizerText::CURSOR_NONE) {282return;283}284CompletionContext context;285context.type = p_type;286context.current_class = current_class;287context.current_function = current_function;288context.current_suite = current_suite;289context.current_line = tokenizer->get_cursor_line();290context.current_argument = p_argument;291context.node = p_node;292context.parser = this;293if (!completion_call_stack.is_empty()) {294context.call = completion_call_stack.back()->get();295}296completion_context = context;297}298299void GDScriptParser::make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force) {300if (!for_completion || (!p_force && completion_context.type != COMPLETION_NONE)) {301return;302}303if (previous.cursor_place != GDScriptTokenizerText::CURSOR_MIDDLE && previous.cursor_place != GDScriptTokenizerText::CURSOR_END && current.cursor_place == GDScriptTokenizerText::CURSOR_NONE) {304return;305}306CompletionContext context;307context.type = p_type;308context.current_class = current_class;309context.current_function = current_function;310context.current_suite = current_suite;311context.current_line = tokenizer->get_cursor_line();312context.builtin_type = p_builtin_type;313context.parser = this;314if (!completion_call_stack.is_empty()) {315context.call = completion_call_stack.back()->get();316}317completion_context = context;318}319320void GDScriptParser::push_completion_call(Node *p_call) {321if (!for_completion) {322return;323}324CompletionCall call;325call.call = p_call;326call.argument = 0;327completion_call_stack.push_back(call);328}329330void GDScriptParser::pop_completion_call() {331if (!for_completion) {332return;333}334ERR_FAIL_COND_MSG(completion_call_stack.is_empty(), "Trying to pop empty completion call stack");335completion_call_stack.pop_back();336}337338void GDScriptParser::set_last_completion_call_arg(int p_argument) {339if (!for_completion) {340return;341}342ERR_FAIL_COND_MSG(completion_call_stack.is_empty(), "Trying to set argument on empty completion call stack");343completion_call_stack.back()->get().argument = p_argument;344}345346Error GDScriptParser::parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body) {347clear();348349String source = p_source_code;350int cursor_line = -1;351int cursor_column = -1;352for_completion = p_for_completion;353parse_body = p_parse_body;354355int tab_size = 4;356#ifdef TOOLS_ENABLED357if (EditorSettings::get_singleton()) {358tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size");359}360#endif // TOOLS_ENABLED361362if (p_for_completion) {363// Remove cursor sentinel char.364const Vector<String> lines = p_source_code.split("\n");365cursor_line = 1;366cursor_column = 1;367for (int i = 0; i < lines.size(); i++) {368bool found = false;369const String &line = lines[i];370for (int j = 0; j < line.size(); j++) {371if (line[j] == char32_t(0xFFFF)) {372found = true;373break;374} else if (line[j] == '\t') {375cursor_column += tab_size - 1;376}377cursor_column++;378}379if (found) {380break;381}382cursor_line++;383cursor_column = 1;384}385386source = source.replace_first(String::chr(0xFFFF), String());387}388389GDScriptTokenizerText *text_tokenizer = memnew(GDScriptTokenizerText);390text_tokenizer->set_source_code(source);391392tokenizer = text_tokenizer;393394tokenizer->set_cursor_position(cursor_line, cursor_column);395script_path = p_script_path.simplify_path();396current = tokenizer->scan();397// Avoid error or newline as the first token.398// The latter can mess with the parser when opening files filled exclusively with comments and newlines.399while (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::NEWLINE) {400if (current.type == GDScriptTokenizer::Token::ERROR) {401push_error(current.literal);402}403current = tokenizer->scan();404}405406#ifdef DEBUG_ENABLED407// Warn about parsing an empty script file:408if (current.type == GDScriptTokenizer::Token::TK_EOF) {409// Create a dummy Node for the warning, pointing to the very beginning of the file410Node *nd = alloc_node<PassNode>();411nd->start_line = 1;412nd->start_column = 0;413nd->end_line = 1;414push_warning(nd, GDScriptWarning::EMPTY_FILE);415}416#endif417418push_multiline(false); // Keep one for the whole parsing.419parse_program();420pop_multiline();421422#ifdef TOOLS_ENABLED423comment_data = tokenizer->get_comments();424#endif425426memdelete(text_tokenizer);427tokenizer = nullptr;428429#ifdef DEBUG_ENABLED430if (multiline_stack.size() > 0) {431ERR_PRINT("Parser bug: Imbalanced multiline stack.");432}433#endif434435if (errors.is_empty()) {436return OK;437} else {438return ERR_PARSE_ERROR;439}440}441442Error GDScriptParser::parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path) {443GDScriptTokenizerBuffer *buffer_tokenizer = memnew(GDScriptTokenizerBuffer);444Error err = buffer_tokenizer->set_code_buffer(p_binary);445446if (err) {447memdelete(buffer_tokenizer);448return err;449}450451tokenizer = buffer_tokenizer;452script_path = p_script_path.simplify_path();453current = tokenizer->scan();454// Avoid error or newline as the first token.455// The latter can mess with the parser when opening files filled exclusively with comments and newlines.456while (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::NEWLINE) {457if (current.type == GDScriptTokenizer::Token::ERROR) {458push_error(current.literal);459}460current = tokenizer->scan();461}462463push_multiline(false); // Keep one for the whole parsing.464parse_program();465pop_multiline();466467memdelete(buffer_tokenizer);468tokenizer = nullptr;469470if (errors.is_empty()) {471return OK;472} else {473return ERR_PARSE_ERROR;474}475}476477GDScriptTokenizer::Token GDScriptParser::advance() {478lambda_ended = false; // Empty marker since we're past the end in any case.479480if (current.type == GDScriptTokenizer::Token::TK_EOF) {481ERR_FAIL_COND_V_MSG(current.type == GDScriptTokenizer::Token::TK_EOF, current, "GDScript parser bug: Trying to advance past the end of stream.");482}483previous = current;484current = tokenizer->scan();485while (current.type == GDScriptTokenizer::Token::ERROR) {486push_error(current.literal);487current = tokenizer->scan();488}489if (previous.type != GDScriptTokenizer::Token::DEDENT) { // `DEDENT` belongs to the next non-empty line.490for (Node *n : nodes_in_progress) {491update_extents(n);492}493}494return previous;495}496497bool GDScriptParser::match(GDScriptTokenizer::Token::Type p_token_type) {498if (!check(p_token_type)) {499return false;500}501advance();502return true;503}504505bool GDScriptParser::check(GDScriptTokenizer::Token::Type p_token_type) const {506if (p_token_type == GDScriptTokenizer::Token::IDENTIFIER) {507return current.is_identifier();508}509return current.type == p_token_type;510}511512bool GDScriptParser::consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message) {513if (match(p_token_type)) {514return true;515}516push_error(p_error_message);517return false;518}519520bool GDScriptParser::is_at_end() const {521return check(GDScriptTokenizer::Token::TK_EOF);522}523524void GDScriptParser::synchronize() {525panic_mode = false;526while (!is_at_end()) {527if (previous.type == GDScriptTokenizer::Token::NEWLINE || previous.type == GDScriptTokenizer::Token::SEMICOLON) {528return;529}530531switch (current.type) {532case GDScriptTokenizer::Token::CLASS:533case GDScriptTokenizer::Token::FUNC:534case GDScriptTokenizer::Token::STATIC:535case GDScriptTokenizer::Token::VAR:536case GDScriptTokenizer::Token::TK_CONST:537case GDScriptTokenizer::Token::SIGNAL:538//case GDScriptTokenizer::Token::IF: // Can also be inside expressions.539case GDScriptTokenizer::Token::FOR:540case GDScriptTokenizer::Token::WHILE:541case GDScriptTokenizer::Token::MATCH:542case GDScriptTokenizer::Token::RETURN:543case GDScriptTokenizer::Token::ANNOTATION:544return;545default:546// Do nothing.547break;548}549550advance();551}552}553554void GDScriptParser::push_multiline(bool p_state) {555multiline_stack.push_back(p_state);556tokenizer->set_multiline_mode(p_state);557if (p_state) {558// Consume potential whitespace tokens already waiting in line.559while (current.type == GDScriptTokenizer::Token::NEWLINE || current.type == GDScriptTokenizer::Token::INDENT || current.type == GDScriptTokenizer::Token::DEDENT) {560current = tokenizer->scan(); // Don't call advance() here, as we don't want to change the previous token.561}562}563}564565void GDScriptParser::pop_multiline() {566ERR_FAIL_COND_MSG(multiline_stack.is_empty(), "Parser bug: trying to pop from multiline stack without available value.");567multiline_stack.pop_back();568tokenizer->set_multiline_mode(multiline_stack.size() > 0 ? multiline_stack.back()->get() : false);569}570571bool GDScriptParser::is_statement_end_token() const {572return check(GDScriptTokenizer::Token::NEWLINE) || check(GDScriptTokenizer::Token::SEMICOLON) || check(GDScriptTokenizer::Token::TK_EOF);573}574575bool GDScriptParser::is_statement_end() const {576return lambda_ended || in_lambda || is_statement_end_token();577}578579void GDScriptParser::end_statement(const String &p_context) {580bool found = false;581while (is_statement_end() && !is_at_end()) {582// Remove sequential newlines/semicolons.583if (is_statement_end_token()) {584// Only consume if this is an actual token.585advance();586} else if (lambda_ended) {587lambda_ended = false; // Consume this "token".588found = true;589break;590} else {591if (!found) {592lambda_ended = true; // Mark the lambda as done since we found something else to end the statement.593found = true;594}595break;596}597598found = true;599}600if (!found && !is_at_end()) {601push_error(vformat(R"(Expected end of statement after %s, found "%s" instead.)", p_context, current.get_name()));602}603}604605void GDScriptParser::parse_program() {606head = alloc_node<ClassNode>();607head->start_line = 1;608head->end_line = 1;609head->fqcn = GDScript::canonicalize_path(script_path);610current_class = head;611bool can_have_class_or_extends = true;612613#define PUSH_PENDING_ANNOTATIONS_TO_HEAD \614if (!annotation_stack.is_empty()) { \615for (AnnotationNode *annot : annotation_stack) { \616head->annotations.push_back(annot); \617} \618annotation_stack.clear(); \619}620621while (!check(GDScriptTokenizer::Token::TK_EOF)) {622if (match(GDScriptTokenizer::Token::ANNOTATION)) {623AnnotationNode *annotation = parse_annotation(AnnotationInfo::SCRIPT | AnnotationInfo::CLASS_LEVEL | AnnotationInfo::STANDALONE);624if (annotation != nullptr) {625if (annotation->applies_to(AnnotationInfo::CLASS)) {626// We do not know in advance what the annotation will be applied to: the `head` class or the subsequent inner class.627// If we encounter `class_name`, `extends` or pure `SCRIPT` annotation, then it's `head`, otherwise it's an inner class.628annotation_stack.push_back(annotation);629} else if (annotation->applies_to(AnnotationInfo::SCRIPT)) {630PUSH_PENDING_ANNOTATIONS_TO_HEAD;631if (annotation->name == SNAME("@tool") || annotation->name == SNAME("@icon") || annotation->name == SNAME("@static_unload")) {632// Some annotations need to be resolved and applied in the parser.633// The root class is not in any class, so `head->outer == nullptr`.634annotation->apply(this, head, nullptr);635} else {636head->annotations.push_back(annotation);637}638} else if (annotation->applies_to(AnnotationInfo::STANDALONE)) {639if (previous.type != GDScriptTokenizer::Token::NEWLINE) {640push_error(R"(Expected newline after a standalone annotation.)");641}642if (annotation->name == SNAME("@export_category") || annotation->name == SNAME("@export_group") || annotation->name == SNAME("@export_subgroup")) {643head->add_member_group(annotation);644// This annotation must appear after script-level annotations and `class_name`/`extends`,645// so we stop looking for script-level stuff.646can_have_class_or_extends = false;647break;648} else if (annotation->name == SNAME("@warning_ignore_start") || annotation->name == SNAME("@warning_ignore_restore")) {649// Some annotations need to be resolved and applied in the parser.650annotation->apply(this, nullptr, nullptr);651} else {652push_error(R"(Unexpected standalone annotation.)");653}654} else {655annotation_stack.push_back(annotation);656// This annotation must appear after script-level annotations and `class_name`/`extends`,657// so we stop looking for script-level stuff.658can_have_class_or_extends = false;659break;660}661}662} else if (check(GDScriptTokenizer::Token::LITERAL) && current.literal.get_type() == Variant::STRING) {663// Allow strings in class body as multiline comments.664advance();665if (!match(GDScriptTokenizer::Token::NEWLINE)) {666push_error("Expected newline after comment string.");667}668} else {669break;670}671}672673if (current.type == GDScriptTokenizer::Token::CLASS_NAME || current.type == GDScriptTokenizer::Token::EXTENDS) {674// Set range of the class to only start at extends or class_name if present.675reset_extents(head, current);676}677678while (can_have_class_or_extends) {679// Order here doesn't matter, but there should be only one of each at most.680switch (current.type) {681case GDScriptTokenizer::Token::CLASS_NAME:682PUSH_PENDING_ANNOTATIONS_TO_HEAD;683advance();684if (head->identifier != nullptr) {685push_error(R"("class_name" can only be used once.)");686} else {687parse_class_name();688}689break;690case GDScriptTokenizer::Token::EXTENDS:691PUSH_PENDING_ANNOTATIONS_TO_HEAD;692advance();693if (head->extends_used) {694push_error(R"("extends" can only be used once.)");695} else {696parse_extends();697end_statement("superclass");698}699break;700case GDScriptTokenizer::Token::TK_EOF:701PUSH_PENDING_ANNOTATIONS_TO_HEAD;702can_have_class_or_extends = false;703break;704case GDScriptTokenizer::Token::LITERAL:705if (current.literal.get_type() == Variant::STRING) {706// Allow strings in class body as multiline comments.707advance();708if (!match(GDScriptTokenizer::Token::NEWLINE)) {709push_error("Expected newline after comment string.");710}711break;712}713[[fallthrough]];714default:715// No tokens are allowed between script annotations and class/extends.716can_have_class_or_extends = false;717break;718}719720if (panic_mode) {721synchronize();722}723}724725#undef PUSH_PENDING_ANNOTATIONS_TO_HEAD726727for (AnnotationNode *&annotation : head->annotations) {728if (annotation->name == SNAME("@abstract")) {729// Some annotations need to be resolved and applied in the parser.730// The root class is not in any class, so `head->outer == nullptr`.731annotation->apply(this, head, nullptr);732}733}734735// When the only thing needed is the class name, icon, and abstractness; we don't need to parse the whole file.736// It really speed up the call to `GDScriptLanguage::get_global_class_name()` especially for large script.737if (!parse_body) {738return;739}740741parse_class_body(true);742743head->end_line = current.end_line;744head->end_column = current.end_column;745746complete_extents(head);747748#ifdef TOOLS_ENABLED749const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments();750751int max_line = head->end_line;752if (!head->members.is_empty()) {753max_line = MIN(max_script_doc_line, head->members[0].get_line() - 1);754}755756int line = 0;757while (line <= max_line) {758// Find the start.759if (comments.has(line) && comments[line].new_line && comments[line].comment.begins_with("##")) {760// Find the end.761while (line + 1 <= max_line && comments.has(line + 1) && comments[line + 1].new_line && comments[line + 1].comment.begins_with("##")) {762line++;763}764head->doc_data = parse_class_doc_comment(line);765break;766}767line++;768}769#endif // TOOLS_ENABLED770771if (!check(GDScriptTokenizer::Token::TK_EOF)) {772push_error("Expected end of file.");773}774775clear_unused_annotations();776}777778Ref<GDScriptParserRef> GDScriptParser::get_depended_parser_for(const String &p_path) {779Ref<GDScriptParserRef> ref;780if (depended_parsers.has(p_path)) {781ref = depended_parsers[p_path];782} else {783Error err = OK;784ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, script_path);785if (ref.is_valid()) {786depended_parsers[p_path] = ref;787}788}789790return ref;791}792793const HashMap<String, Ref<GDScriptParserRef>> &GDScriptParser::get_depended_parsers() {794return depended_parsers;795}796797GDScriptParser::ClassNode *GDScriptParser::find_class(const String &p_qualified_name) const {798String first = p_qualified_name.get_slice("::", 0);799800Vector<String> class_names;801GDScriptParser::ClassNode *result = nullptr;802// Empty initial name means start at the head.803if (first.is_empty() || (head->identifier && first == head->identifier->name)) {804class_names = p_qualified_name.split("::");805result = head;806} else if (p_qualified_name.begins_with(script_path)) {807// Script path could have a class path separator("::") in it.808class_names = p_qualified_name.trim_prefix(script_path).split("::");809result = head;810} else if (head->has_member(first)) {811class_names = p_qualified_name.split("::");812GDScriptParser::ClassNode::Member member = head->get_member(first);813if (member.type == GDScriptParser::ClassNode::Member::CLASS) {814result = member.m_class;815}816}817818// Starts at index 1 because index 0 was handled above.819for (int i = 1; result != nullptr && i < class_names.size(); i++) {820const String ¤t_name = class_names[i];821GDScriptParser::ClassNode *next = nullptr;822if (result->has_member(current_name)) {823GDScriptParser::ClassNode::Member member = result->get_member(current_name);824if (member.type == GDScriptParser::ClassNode::Member::CLASS) {825next = member.m_class;826}827}828result = next;829}830831return result;832}833834bool GDScriptParser::has_class(const GDScriptParser::ClassNode *p_class) const {835if (head->fqcn.is_empty() && p_class->fqcn.get_slice("::", 0).is_empty()) {836return p_class == head;837} else if (p_class->fqcn.begins_with(head->fqcn)) {838return find_class(p_class->fqcn.trim_prefix(head->fqcn)) == p_class;839}840841return false;842}843844GDScriptParser::ClassNode *GDScriptParser::parse_class(bool p_is_static) {845ClassNode *n_class = alloc_node<ClassNode>();846847ClassNode *previous_class = current_class;848current_class = n_class;849n_class->outer = previous_class;850851if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier for the class name after "class".)")) {852n_class->identifier = parse_identifier();853if (n_class->outer) {854String fqcn = n_class->outer->fqcn;855if (fqcn.is_empty()) {856fqcn = GDScript::canonicalize_path(script_path);857}858n_class->fqcn = fqcn + "::" + n_class->identifier->name;859} else {860n_class->fqcn = n_class->identifier->name;861}862}863864if (match(GDScriptTokenizer::Token::EXTENDS)) {865parse_extends();866}867868consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after class declaration.)");869870bool multiline = match(GDScriptTokenizer::Token::NEWLINE);871872if (multiline && !consume(GDScriptTokenizer::Token::INDENT, R"(Expected indented block after class declaration.)")) {873current_class = previous_class;874complete_extents(n_class);875return n_class;876}877878if (match(GDScriptTokenizer::Token::EXTENDS)) {879if (n_class->extends_used) {880push_error(R"(Cannot use "extends" more than once in the same class.)");881}882parse_extends();883end_statement("superclass");884}885886parse_class_body(multiline);887complete_extents(n_class);888889if (multiline) {890consume(GDScriptTokenizer::Token::DEDENT, R"(Missing unindent at the end of the class body.)");891}892893current_class = previous_class;894return n_class;895}896897void GDScriptParser::parse_class_name() {898if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier for the global class name after "class_name".)")) {899current_class->identifier = parse_identifier();900current_class->fqcn = String(current_class->identifier->name);901}902903if (match(GDScriptTokenizer::Token::EXTENDS)) {904// Allow extends on the same line.905parse_extends();906end_statement("superclass");907} else {908end_statement("class_name statement");909}910}911912void GDScriptParser::parse_extends() {913current_class->extends_used = true;914915int chain_index = 0;916917if (match(GDScriptTokenizer::Token::LITERAL)) {918if (previous.literal.get_type() != Variant::STRING) {919push_error(vformat(R"(Only strings or identifiers can be used after "extends", found "%s" instead.)", Variant::get_type_name(previous.literal.get_type())));920}921current_class->extends_path = previous.literal;922923if (!match(GDScriptTokenizer::Token::PERIOD)) {924return;925}926}927928make_completion_context(COMPLETION_INHERIT_TYPE, current_class, chain_index++);929930if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected superclass name after "extends".)")) {931return;932}933current_class->extends.push_back(parse_identifier());934935while (match(GDScriptTokenizer::Token::PERIOD)) {936make_completion_context(COMPLETION_INHERIT_TYPE, current_class, chain_index++);937if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected superclass name after ".".)")) {938return;939}940current_class->extends.push_back(parse_identifier());941}942}943944template <typename T>945void GDScriptParser::parse_class_member(T *(GDScriptParser::*p_parse_function)(bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_static) {946advance();947948// Consume annotations.949List<AnnotationNode *> annotations;950while (!annotation_stack.is_empty()) {951AnnotationNode *last_annotation = annotation_stack.back()->get();952if (last_annotation->applies_to(p_target)) {953annotations.push_front(last_annotation);954annotation_stack.pop_back();955} else {956push_error(vformat(R"(Annotation "%s" cannot be applied to a %s.)", last_annotation->name, p_member_kind));957clear_unused_annotations();958}959}960961T *member = (this->*p_parse_function)(p_is_static);962if (member == nullptr) {963return;964}965966#ifdef TOOLS_ENABLED967int doc_comment_line = member->start_line - 1;968#endif // TOOLS_ENABLED969970for (AnnotationNode *&annotation : annotations) {971member->annotations.push_back(annotation);972#ifdef TOOLS_ENABLED973if (annotation->start_line <= doc_comment_line) {974doc_comment_line = annotation->start_line - 1;975}976#endif // TOOLS_ENABLED977}978979#ifdef TOOLS_ENABLED980if constexpr (std::is_same_v<T, ClassNode>) {981if (has_comment(member->start_line, true)) {982// Inline doc comment.983member->doc_data = parse_class_doc_comment(member->start_line, true);984} else if (has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) {985// Normal doc comment. Don't check `min_member_doc_line` because a class ends parsing after its members.986// This may not work correctly for cases like `var a; class B`, but it doesn't matter in practice.987member->doc_data = parse_class_doc_comment(doc_comment_line);988}989} else {990if (has_comment(member->start_line, true)) {991// Inline doc comment.992member->doc_data = parse_doc_comment(member->start_line, true);993} else if (doc_comment_line >= min_member_doc_line && has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) {994// Normal doc comment.995member->doc_data = parse_doc_comment(doc_comment_line);996}997}998999min_member_doc_line = member->end_line + 1; // Prevent multiple members from using the same doc comment.1000#endif // TOOLS_ENABLED10011002if (member->identifier != nullptr) {1003if (!((String)member->identifier->name).is_empty()) { // Enums may be unnamed.1004if (current_class->members_indices.has(member->identifier->name)) {1005push_error(vformat(R"(%s "%s" has the same name as a previously declared %s.)", p_member_kind.capitalize(), member->identifier->name, current_class->get_member(member->identifier->name).get_type_name()), member->identifier);1006} else {1007current_class->add_member(member);1008}1009} else {1010current_class->add_member(member);1011}1012}1013}10141015void GDScriptParser::parse_class_body(bool p_is_multiline) {1016bool class_end = false;1017bool next_is_static = false;1018while (!class_end && !is_at_end()) {1019GDScriptTokenizer::Token token = current;1020switch (token.type) {1021case GDScriptTokenizer::Token::VAR:1022parse_class_member(&GDScriptParser::parse_variable, AnnotationInfo::VARIABLE, "variable", next_is_static);1023if (next_is_static) {1024current_class->has_static_data = true;1025}1026break;1027case GDScriptTokenizer::Token::TK_CONST:1028parse_class_member(&GDScriptParser::parse_constant, AnnotationInfo::CONSTANT, "constant");1029break;1030case GDScriptTokenizer::Token::SIGNAL:1031parse_class_member(&GDScriptParser::parse_signal, AnnotationInfo::SIGNAL, "signal");1032break;1033case GDScriptTokenizer::Token::FUNC:1034parse_class_member(&GDScriptParser::parse_function, AnnotationInfo::FUNCTION, "function", next_is_static);1035break;1036case GDScriptTokenizer::Token::CLASS:1037parse_class_member(&GDScriptParser::parse_class, AnnotationInfo::CLASS, "class");1038break;1039case GDScriptTokenizer::Token::ENUM:1040parse_class_member(&GDScriptParser::parse_enum, AnnotationInfo::NONE, "enum");1041break;1042case GDScriptTokenizer::Token::STATIC: {1043advance();1044next_is_static = true;1045if (!check(GDScriptTokenizer::Token::FUNC) && !check(GDScriptTokenizer::Token::VAR)) {1046push_error(R"(Expected "func" or "var" after "static".)");1047}1048} break;1049case GDScriptTokenizer::Token::ANNOTATION: {1050advance();10511052// Check for class-level and standalone annotations.1053AnnotationNode *annotation = parse_annotation(AnnotationInfo::CLASS_LEVEL | AnnotationInfo::STANDALONE);1054if (annotation != nullptr) {1055if (annotation->applies_to(AnnotationInfo::STANDALONE)) {1056if (previous.type != GDScriptTokenizer::Token::NEWLINE) {1057push_error(R"(Expected newline after a standalone annotation.)");1058}1059if (annotation->name == SNAME("@export_category") || annotation->name == SNAME("@export_group") || annotation->name == SNAME("@export_subgroup")) {1060current_class->add_member_group(annotation);1061} else if (annotation->name == SNAME("@warning_ignore_start") || annotation->name == SNAME("@warning_ignore_restore")) {1062// Some annotations need to be resolved and applied in the parser.1063annotation->apply(this, nullptr, nullptr);1064} else {1065push_error(R"(Unexpected standalone annotation.)");1066}1067} else { // `AnnotationInfo::CLASS_LEVEL`.1068annotation_stack.push_back(annotation);1069}1070}1071break;1072}1073case GDScriptTokenizer::Token::PASS:1074advance();1075end_statement(R"("pass")");1076break;1077case GDScriptTokenizer::Token::DEDENT:1078class_end = true;1079break;1080case GDScriptTokenizer::Token::LITERAL:1081if (current.literal.get_type() == Variant::STRING) {1082// Allow strings in class body as multiline comments.1083advance();1084if (!match(GDScriptTokenizer::Token::NEWLINE)) {1085push_error("Expected newline after comment string.");1086}1087break;1088}1089[[fallthrough]];1090default:1091// Display a completion with identifiers.1092make_completion_context(COMPLETION_IDENTIFIER, nullptr);1093advance();1094if (previous.get_identifier() == "export") {1095push_error(R"(The "export" keyword was removed in Godot 4. Use an export annotation ("@export", "@export_range", etc.) instead.)");1096} else if (previous.get_identifier() == "tool") {1097push_error(R"(The "tool" keyword was removed in Godot 4. Use the "@tool" annotation instead.)");1098} else if (previous.get_identifier() == "onready") {1099push_error(R"(The "onready" keyword was removed in Godot 4. Use the "@onready" annotation instead.)");1100} else if (previous.get_identifier() == "remote") {1101push_error(R"(The "remote" keyword was removed in Godot 4. Use the "@rpc" annotation with "any_peer" instead.)");1102} else if (previous.get_identifier() == "remotesync") {1103push_error(R"(The "remotesync" keyword was removed in Godot 4. Use the "@rpc" annotation with "any_peer" and "call_local" instead.)");1104} else if (previous.get_identifier() == "puppet") {1105push_error(R"(The "puppet" keyword was removed in Godot 4. Use the "@rpc" annotation with "authority" instead.)");1106} else if (previous.get_identifier() == "puppetsync") {1107push_error(R"(The "puppetsync" keyword was removed in Godot 4. Use the "@rpc" annotation with "authority" and "call_local" instead.)");1108} else if (previous.get_identifier() == "master") {1109push_error(R"(The "master" keyword was removed in Godot 4. Use the "@rpc" annotation with "any_peer" and perform a check inside the function instead.)");1110} else if (previous.get_identifier() == "mastersync") {1111push_error(R"(The "mastersync" keyword was removed in Godot 4. Use the "@rpc" annotation with "any_peer" and "call_local", and perform a check inside the function instead.)");1112} else {1113push_error(vformat(R"(Unexpected %s in class body.)", previous.get_debug_name()));1114}1115break;1116}1117if (token.type != GDScriptTokenizer::Token::STATIC) {1118next_is_static = false;1119}1120if (panic_mode) {1121synchronize();1122}1123if (!p_is_multiline) {1124class_end = true;1125}1126}1127}11281129GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static) {1130return parse_variable(p_is_static, true);1131}11321133GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static, bool p_allow_property) {1134VariableNode *variable = alloc_node<VariableNode>();11351136if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected variable name after "var".)")) {1137complete_extents(variable);1138return nullptr;1139}11401141variable->identifier = parse_identifier();1142variable->export_info.name = variable->identifier->name;1143variable->is_static = p_is_static;11441145if (match(GDScriptTokenizer::Token::COLON)) {1146if (check(GDScriptTokenizer::Token::NEWLINE)) {1147if (p_allow_property) {1148advance();1149return parse_property(variable, true);1150} else {1151push_error(R"(Expected type after ":")");1152complete_extents(variable);1153return nullptr;1154}1155} else if (check((GDScriptTokenizer::Token::EQUAL))) {1156// Infer type.1157variable->infer_datatype = true;1158} else {1159if (p_allow_property) {1160make_completion_context(COMPLETION_PROPERTY_DECLARATION_OR_TYPE, variable);1161if (check(GDScriptTokenizer::Token::IDENTIFIER)) {1162// Check if get or set.1163if (current.get_identifier() == "get" || current.get_identifier() == "set") {1164return parse_property(variable, false);1165}1166}1167}11681169// Parse type.1170variable->datatype_specifier = parse_type();1171}1172}11731174if (match(GDScriptTokenizer::Token::EQUAL)) {1175// Initializer.1176variable->initializer = parse_expression(false);1177if (variable->initializer == nullptr) {1178push_error(R"(Expected expression for variable initial value after "=".)");1179}1180variable->assignments++;1181}11821183if (p_allow_property && match(GDScriptTokenizer::Token::COLON)) {1184if (match(GDScriptTokenizer::Token::NEWLINE)) {1185return parse_property(variable, true);1186} else {1187return parse_property(variable, false);1188}1189}11901191complete_extents(variable);1192end_statement("variable declaration");11931194return variable;1195}11961197GDScriptParser::VariableNode *GDScriptParser::parse_property(VariableNode *p_variable, bool p_need_indent) {1198if (p_need_indent) {1199if (!consume(GDScriptTokenizer::Token::INDENT, R"(Expected indented block for property after ":".)")) {1200complete_extents(p_variable);1201return nullptr;1202}1203}12041205VariableNode *property = p_variable;12061207make_completion_context(COMPLETION_PROPERTY_DECLARATION, property);12081209if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected "get" or "set" for property declaration.)")) {1210complete_extents(p_variable);1211return nullptr;1212}12131214IdentifierNode *function = parse_identifier();12151216if (check(GDScriptTokenizer::Token::EQUAL)) {1217p_variable->property = VariableNode::PROP_SETGET;1218} else {1219p_variable->property = VariableNode::PROP_INLINE;1220if (!p_need_indent) {1221push_error("Property with inline code must go to an indented block.");1222}1223}12241225bool getter_used = false;1226bool setter_used = false;12271228// Run with a loop because order doesn't matter.1229for (int i = 0; i < 2; i++) {1230if (function->name == SNAME("set")) {1231if (setter_used) {1232push_error(R"(Properties can only have one setter.)");1233} else {1234parse_property_setter(property);1235setter_used = true;1236}1237} else if (function->name == SNAME("get")) {1238if (getter_used) {1239push_error(R"(Properties can only have one getter.)");1240} else {1241parse_property_getter(property);1242getter_used = true;1243}1244} else {1245// TODO: Update message to only have the missing one if it's the case.1246push_error(R"(Expected "get" or "set" for property declaration.)");1247}12481249if (i == 0 && p_variable->property == VariableNode::PROP_SETGET) {1250if (match(GDScriptTokenizer::Token::COMMA)) {1251// Consume potential newline.1252if (match(GDScriptTokenizer::Token::NEWLINE)) {1253if (!p_need_indent) {1254push_error(R"(Inline setter/getter setting cannot span across multiple lines (use "\\"" if needed).)");1255}1256}1257} else {1258break;1259}1260}12611262if (!match(GDScriptTokenizer::Token::IDENTIFIER)) {1263break;1264}1265function = parse_identifier();1266}1267complete_extents(p_variable);12681269if (p_variable->property == VariableNode::PROP_SETGET) {1270end_statement("property declaration");1271}12721273if (p_need_indent) {1274consume(GDScriptTokenizer::Token::DEDENT, R"(Expected end of indented block for property.)");1275}1276return property;1277}12781279void GDScriptParser::parse_property_setter(VariableNode *p_variable) {1280switch (p_variable->property) {1281case VariableNode::PROP_INLINE: {1282FunctionNode *function = alloc_node<FunctionNode>();1283IdentifierNode *identifier = alloc_node<IdentifierNode>();1284complete_extents(identifier);1285identifier->name = "@" + p_variable->identifier->name + "_setter";1286function->identifier = identifier;1287function->is_static = p_variable->is_static;12881289consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after "set".)");12901291ParameterNode *parameter = alloc_node<ParameterNode>();1292if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected parameter name after "(".)")) {1293reset_extents(parameter, previous);1294p_variable->setter_parameter = parse_identifier();1295parameter->identifier = p_variable->setter_parameter;1296function->parameters_indices[parameter->identifier->name] = 0;1297function->parameters.push_back(parameter);1298}1299complete_extents(parameter);13001301consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after parameter name.)*");1302consume(GDScriptTokenizer::Token::COLON, R"*(Expected ":" after ")".)*");13031304FunctionNode *previous_function = current_function;1305current_function = function;1306if (p_variable->setter_parameter != nullptr) {1307SuiteNode *body = alloc_node<SuiteNode>();1308body->add_local(parameter, function);1309function->body = parse_suite("setter declaration", body);1310p_variable->setter = function;1311}1312current_function = previous_function;1313complete_extents(function);1314break;1315}1316case VariableNode::PROP_SETGET:1317consume(GDScriptTokenizer::Token::EQUAL, R"(Expected "=" after "set")");1318make_completion_context(COMPLETION_PROPERTY_METHOD, p_variable);1319if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected setter function name after "=".)")) {1320p_variable->setter_pointer = parse_identifier();1321}1322break;1323case VariableNode::PROP_NONE:1324break; // Unreachable.1325}1326}13271328void GDScriptParser::parse_property_getter(VariableNode *p_variable) {1329switch (p_variable->property) {1330case VariableNode::PROP_INLINE: {1331FunctionNode *function = alloc_node<FunctionNode>();13321333if (match(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) {1334consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after "get(".)*");1335consume(GDScriptTokenizer::Token::COLON, R"*(Expected ":" after "get()".)*");1336} else {1337consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" or "(" after "get".)");1338}13391340IdentifierNode *identifier = alloc_node<IdentifierNode>();1341complete_extents(identifier);1342identifier->name = "@" + p_variable->identifier->name + "_getter";1343function->identifier = identifier;1344function->is_static = p_variable->is_static;13451346FunctionNode *previous_function = current_function;1347current_function = function;13481349SuiteNode *body = alloc_node<SuiteNode>();1350function->body = parse_suite("getter declaration", body);1351p_variable->getter = function;13521353current_function = previous_function;1354complete_extents(function);1355break;1356}1357case VariableNode::PROP_SETGET:1358consume(GDScriptTokenizer::Token::EQUAL, R"(Expected "=" after "get")");1359make_completion_context(COMPLETION_PROPERTY_METHOD, p_variable);1360if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected getter function name after "=".)")) {1361p_variable->getter_pointer = parse_identifier();1362}1363break;1364case VariableNode::PROP_NONE:1365break; // Unreachable.1366}1367}13681369GDScriptParser::ConstantNode *GDScriptParser::parse_constant(bool p_is_static) {1370ConstantNode *constant = alloc_node<ConstantNode>();13711372if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected constant name after "const".)")) {1373complete_extents(constant);1374return nullptr;1375}13761377constant->identifier = parse_identifier();13781379if (match(GDScriptTokenizer::Token::COLON)) {1380if (check((GDScriptTokenizer::Token::EQUAL))) {1381// Infer type.1382constant->infer_datatype = true;1383} else {1384// Parse type.1385constant->datatype_specifier = parse_type();1386}1387}13881389if (consume(GDScriptTokenizer::Token::EQUAL, R"(Expected initializer after constant name.)")) {1390// Initializer.1391constant->initializer = parse_expression(false);13921393if (constant->initializer == nullptr) {1394push_error(R"(Expected initializer expression for constant.)");1395complete_extents(constant);1396return nullptr;1397}1398} else {1399complete_extents(constant);1400return nullptr;1401}14021403complete_extents(constant);1404end_statement("constant declaration");14051406return constant;1407}14081409GDScriptParser::ParameterNode *GDScriptParser::parse_parameter() {1410if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected parameter name.)")) {1411return nullptr;1412}14131414ParameterNode *parameter = alloc_node<ParameterNode>();1415parameter->identifier = parse_identifier();14161417if (match(GDScriptTokenizer::Token::COLON)) {1418if (check((GDScriptTokenizer::Token::EQUAL))) {1419// Infer type.1420parameter->infer_datatype = true;1421} else {1422// Parse type.1423make_completion_context(COMPLETION_TYPE_NAME, parameter);1424parameter->datatype_specifier = parse_type();1425}1426}14271428if (match(GDScriptTokenizer::Token::EQUAL)) {1429// Default value.1430parameter->initializer = parse_expression(false);1431}14321433complete_extents(parameter);1434return parameter;1435}14361437GDScriptParser::SignalNode *GDScriptParser::parse_signal(bool p_is_static) {1438SignalNode *signal = alloc_node<SignalNode>();14391440if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected signal name after "signal".)")) {1441complete_extents(signal);1442return nullptr;1443}14441445signal->identifier = parse_identifier();14461447if (check(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) {1448push_multiline(true);1449advance();1450do {1451if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) {1452// Allow for trailing comma.1453break;1454}14551456ParameterNode *parameter = parse_parameter();1457if (parameter == nullptr) {1458push_error("Expected signal parameter name.");1459break;1460}1461if (parameter->initializer != nullptr) {1462push_error(R"(Signal parameters cannot have a default value.)");1463}1464if (signal->parameters_indices.has(parameter->identifier->name)) {1465push_error(vformat(R"(Parameter with name "%s" was already declared for this signal.)", parameter->identifier->name));1466} else {1467signal->parameters_indices[parameter->identifier->name] = signal->parameters.size();1468signal->parameters.push_back(parameter);1469}1470} while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end());14711472pop_multiline();1473consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after signal parameters.)*");1474}14751476complete_extents(signal);1477end_statement("signal declaration");14781479return signal;1480}14811482GDScriptParser::EnumNode *GDScriptParser::parse_enum(bool p_is_static) {1483EnumNode *enum_node = alloc_node<EnumNode>();1484bool named = false;14851486if (match(GDScriptTokenizer::Token::IDENTIFIER)) {1487enum_node->identifier = parse_identifier();1488named = true;1489}14901491push_multiline(true);1492consume(GDScriptTokenizer::Token::BRACE_OPEN, vformat(R"(Expected "{" after %s.)", named ? "enum name" : R"("enum")"));1493#ifdef TOOLS_ENABLED1494int min_enum_value_doc_line = previous.end_line + 1;1495#endif14961497HashMap<StringName, int> elements;14981499#ifdef DEBUG_ENABLED1500List<MethodInfo> gdscript_funcs;1501GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs);1502#endif15031504do {1505if (check(GDScriptTokenizer::Token::BRACE_CLOSE)) {1506break; // Allow trailing comma.1507}1508if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier for enum key.)")) {1509GDScriptParser::IdentifierNode *identifier = parse_identifier();15101511EnumNode::Value item;1512item.identifier = identifier;1513item.parent_enum = enum_node;1514item.line = previous.start_line;1515item.start_column = previous.start_column;1516item.end_column = previous.end_column;15171518if (elements.has(item.identifier->name)) {1519push_error(vformat(R"(Name "%s" was already in this enum (at line %d).)", item.identifier->name, elements[item.identifier->name]), item.identifier);1520} else if (!named) {1521if (current_class->members_indices.has(item.identifier->name)) {1522push_error(vformat(R"(Name "%s" is already used as a class %s.)", item.identifier->name, current_class->get_member(item.identifier->name).get_type_name()));1523}1524}15251526elements[item.identifier->name] = item.line;15271528if (match(GDScriptTokenizer::Token::EQUAL)) {1529ExpressionNode *value = parse_expression(false);1530if (value == nullptr) {1531push_error(R"(Expected expression value after "=".)");1532}1533item.custom_value = value;1534}15351536item.index = enum_node->values.size();1537enum_node->values.push_back(item);1538if (!named) {1539// Add as member of current class.1540current_class->add_member(item);1541}1542}1543} while (match(GDScriptTokenizer::Token::COMMA));15441545#ifdef TOOLS_ENABLED1546// Enum values documentation.1547for (int i = 0; i < enum_node->values.size(); i++) {1548int enum_value_line = enum_node->values[i].line;1549int doc_comment_line = enum_value_line - 1;15501551MemberDocData doc_data;1552if (has_comment(enum_value_line, true)) {1553// Inline doc comment.1554if (i == enum_node->values.size() - 1 || enum_node->values[i + 1].line > enum_value_line) {1555doc_data = parse_doc_comment(enum_value_line, true);1556}1557} else if (doc_comment_line >= min_enum_value_doc_line && has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) {1558// Normal doc comment.1559doc_data = parse_doc_comment(doc_comment_line);1560}15611562if (named) {1563enum_node->values.write[i].doc_data = doc_data;1564} else {1565current_class->set_enum_value_doc_data(enum_node->values[i].identifier->name, doc_data);1566}15671568min_enum_value_doc_line = enum_value_line + 1; // Prevent multiple enum values from using the same doc comment.1569}1570#endif // TOOLS_ENABLED15711572pop_multiline();1573consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected closing "}" for enum.)");1574complete_extents(enum_node);1575end_statement("enum");15761577return enum_node;1578}15791580bool GDScriptParser::parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type, int p_signature_start) {1581if (!check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE) && !is_at_end()) {1582bool default_used = false;1583do {1584if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) {1585// Allow for trailing comma.1586break;1587}15881589bool is_rest = false;1590if (match(GDScriptTokenizer::Token::PERIOD_PERIOD_PERIOD)) {1591is_rest = true;1592}15931594ParameterNode *parameter = parse_parameter();1595if (parameter == nullptr) {1596break;1597}15981599if (p_function->is_vararg()) {1600push_error("Cannot have parameters after the rest parameter.");1601continue;1602}16031604if (parameter->initializer != nullptr) {1605if (is_rest) {1606push_error("The rest parameter cannot have a default value.");1607continue;1608}1609default_used = true;1610} else {1611if (default_used && !is_rest) {1612push_error("Cannot have mandatory parameters after optional parameters.");1613continue;1614}1615}16161617if (p_function->parameters_indices.has(parameter->identifier->name)) {1618push_error(vformat(R"(Parameter with name "%s" was already declared for this %s.)", parameter->identifier->name, p_type));1619} else if (is_rest) {1620p_function->rest_parameter = parameter;1621p_body->add_local(parameter, current_function);1622} else {1623p_function->parameters_indices[parameter->identifier->name] = p_function->parameters.size();1624p_function->parameters.push_back(parameter);1625p_body->add_local(parameter, current_function);1626}1627} while (match(GDScriptTokenizer::Token::COMMA));1628}16291630pop_multiline();1631consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, vformat(R"*(Expected closing ")" after %s parameters.)*", p_type));16321633if (match(GDScriptTokenizer::Token::FORWARD_ARROW)) {1634make_completion_context(COMPLETION_TYPE_NAME_OR_VOID, p_function);1635p_function->return_type = parse_type(true);1636if (p_function->return_type == nullptr) {1637push_error(R"(Expected return type or "void" after "->".)");1638}1639}16401641if (!p_function->source_lambda && p_function->identifier && p_function->identifier->name == GDScriptLanguage::get_singleton()->strings._static_init) {1642if (!p_function->is_static) {1643push_error(R"(Static constructor must be declared static.)");1644}1645if (!p_function->parameters.is_empty() || p_function->is_vararg()) {1646push_error(R"(Static constructor cannot have parameters.)");1647}1648current_class->has_static_data = true;1649}16501651#ifdef TOOLS_ENABLED1652if (p_type == "function" && p_signature_start != -1) {1653const int signature_end_pos = tokenizer->get_current_position() - 1;1654const String source_code = tokenizer->get_source_code();1655p_function->signature = source_code.substr(p_signature_start, signature_end_pos - p_signature_start).strip_edges(false, true);1656}1657#endif // TOOLS_ENABLED16581659// TODO: Improve token consumption so it synchronizes to a statement boundary. This way we can get into the function body with unrecognized tokens.1660if (p_type == "lambda") {1661return consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after lambda declaration.)");1662}1663// The colon may not be present in the case of abstract functions.1664return match(GDScriptTokenizer::Token::COLON);1665}16661667GDScriptParser::FunctionNode *GDScriptParser::parse_function(bool p_is_static) {1668FunctionNode *function = alloc_node<FunctionNode>();1669function->is_static = p_is_static;16701671make_completion_context(COMPLETION_OVERRIDE_METHOD, function);16721673#ifdef TOOLS_ENABLED1674// The signature is something like `(a: int, b: int = 0) -> void`.1675// We start one token earlier, since the parser looks one token ahead.1676const int signature_start_pos = tokenizer->get_current_position();1677#endif // TOOLS_ENABLED16781679if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected function name after "func".)")) {1680complete_extents(function);1681return nullptr;1682}16831684FunctionNode *previous_function = current_function;1685current_function = function;16861687function->identifier = parse_identifier();16881689SuiteNode *body = alloc_node<SuiteNode>();16901691SuiteNode *previous_suite = current_suite;1692current_suite = body;16931694push_multiline(true);1695consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after function name.)");16961697#ifdef TOOLS_ENABLED1698const bool has_body = parse_function_signature(function, body, "function", signature_start_pos);1699#else // !TOOLS_ENABLED1700const bool has_body = parse_function_signature(function, body, "function", -1);1701#endif // TOOLS_ENABLED17021703current_suite = previous_suite;17041705#ifdef TOOLS_ENABLED1706function->min_local_doc_line = previous.end_line + 1;1707#endif // TOOLS_ENABLED17081709if (!has_body) {1710// Abstract functions do not have a body.1711end_statement("bodyless function declaration");1712reset_extents(body, current);1713complete_extents(body);1714function->body = body;1715} else {1716function->body = parse_suite("function declaration", body);1717}17181719current_function = previous_function;1720complete_extents(function);1721return function;1722}17231724GDScriptParser::AnnotationNode *GDScriptParser::parse_annotation(uint32_t p_valid_targets) {1725AnnotationNode *annotation = alloc_node<AnnotationNode>();17261727annotation->name = previous.literal;17281729make_completion_context(COMPLETION_ANNOTATION, annotation);17301731bool valid = true;17321733if (!valid_annotations.has(annotation->name)) {1734if (annotation->name == "@deprecated") {1735push_error(R"("@deprecated" annotation does not exist. Use "## @deprecated: Reason here." instead.)");1736} else if (annotation->name == "@experimental") {1737push_error(R"("@experimental" annotation does not exist. Use "## @experimental: Reason here." instead.)");1738} else if (annotation->name == "@tutorial") {1739push_error(R"("@tutorial" annotation does not exist. Use "## @tutorial(Title): https://example.com" instead.)");1740} else {1741push_error(vformat(R"(Unrecognized annotation: "%s".)", annotation->name));1742}1743valid = false;1744}17451746if (valid) {1747annotation->info = &valid_annotations[annotation->name];17481749if (!annotation->applies_to(p_valid_targets)) {1750if (annotation->applies_to(AnnotationInfo::SCRIPT)) {1751push_error(vformat(R"(Annotation "%s" must be at the top of the script, before "extends" and "class_name".)", annotation->name));1752} else {1753push_error(vformat(R"(Annotation "%s" is not allowed in this level.)", annotation->name));1754}1755valid = false;1756}1757}17581759if (check(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) {1760push_multiline(true);1761advance();1762// Arguments.1763push_completion_call(annotation);1764int argument_index = 0;1765do {1766make_completion_context(COMPLETION_ANNOTATION_ARGUMENTS, annotation, argument_index);1767set_last_completion_call_arg(argument_index);1768if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) {1769// Allow for trailing comma.1770break;1771}17721773ExpressionNode *argument = parse_expression(false);17741775if (argument == nullptr) {1776push_error("Expected expression as the annotation argument.");1777valid = false;1778} else {1779annotation->arguments.push_back(argument);17801781if (argument->type == Node::LITERAL) {1782override_completion_context(argument, COMPLETION_ANNOTATION_ARGUMENTS, annotation, argument_index);1783}1784}17851786argument_index++;1787} while (match(GDScriptTokenizer::Token::COMMA));17881789pop_multiline();1790consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after annotation arguments.)*");1791pop_completion_call();1792}1793complete_extents(annotation);17941795match(GDScriptTokenizer::Token::NEWLINE); // Newline after annotation is optional.17961797if (valid) {1798valid = validate_annotation_arguments(annotation);1799}18001801return valid ? annotation : nullptr;1802}18031804void GDScriptParser::clear_unused_annotations() {1805for (const AnnotationNode *annotation : annotation_stack) {1806push_error(vformat(R"(Annotation "%s" does not precede a valid target, so it will have no effect.)", annotation->name), annotation);1807}18081809annotation_stack.clear();1810}18111812bool GDScriptParser::register_annotation(const MethodInfo &p_info, uint32_t p_target_kinds, AnnotationAction p_apply, const Vector<Variant> &p_default_arguments, bool p_is_vararg) {1813ERR_FAIL_COND_V_MSG(valid_annotations.has(p_info.name), false, vformat(R"(Annotation "%s" already registered.)", p_info.name));18141815AnnotationInfo new_annotation;1816new_annotation.info = p_info;1817new_annotation.info.default_arguments = p_default_arguments;1818if (p_is_vararg) {1819new_annotation.info.flags |= METHOD_FLAG_VARARG;1820}1821new_annotation.apply = p_apply;1822new_annotation.target_kind = p_target_kinds;18231824valid_annotations[p_info.name] = new_annotation;1825return true;1826}18271828GDScriptParser::SuiteNode *GDScriptParser::parse_suite(const String &p_context, SuiteNode *p_suite, bool p_for_lambda) {1829SuiteNode *suite = p_suite != nullptr ? p_suite : alloc_node<SuiteNode>();1830suite->parent_block = current_suite;1831suite->parent_function = current_function;1832current_suite = suite;18331834if (!p_for_lambda && suite->parent_block != nullptr && suite->parent_block->is_in_loop) {1835// Do not reset to false if true is set before calling parse_suite().1836suite->is_in_loop = true;1837}18381839bool multiline = false;18401841if (match(GDScriptTokenizer::Token::NEWLINE)) {1842multiline = true;1843}18441845if (multiline) {1846if (!consume(GDScriptTokenizer::Token::INDENT, vformat(R"(Expected indented block after %s.)", p_context))) {1847current_suite = suite->parent_block;1848complete_extents(suite);1849return suite;1850}1851}1852reset_extents(suite, current);18531854int error_count = 0;18551856do {1857if (is_at_end() || (!multiline && previous.type == GDScriptTokenizer::Token::SEMICOLON && check(GDScriptTokenizer::Token::NEWLINE))) {1858break;1859}1860Node *statement = parse_statement();1861if (statement == nullptr) {1862if (error_count++ > 100) {1863push_error("Too many statement errors.", suite);1864break;1865}1866continue;1867}1868suite->statements.push_back(statement);18691870// Register locals.1871switch (statement->type) {1872case Node::VARIABLE: {1873VariableNode *variable = static_cast<VariableNode *>(statement);1874const SuiteNode::Local &local = current_suite->get_local(variable->identifier->name);1875if (local.type != SuiteNode::Local::UNDEFINED) {1876push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", local.get_name(), variable->identifier->name), variable->identifier);1877}1878current_suite->add_local(variable, current_function);1879break;1880}1881case Node::CONSTANT: {1882ConstantNode *constant = static_cast<ConstantNode *>(statement);1883const SuiteNode::Local &local = current_suite->get_local(constant->identifier->name);1884if (local.type != SuiteNode::Local::UNDEFINED) {1885String name;1886if (local.type == SuiteNode::Local::CONSTANT) {1887name = "constant";1888} else {1889name = "variable";1890}1891push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", name, constant->identifier->name), constant->identifier);1892}1893current_suite->add_local(constant, current_function);1894break;1895}1896default:1897break;1898}18991900} while ((multiline || previous.type == GDScriptTokenizer::Token::SEMICOLON) && !check(GDScriptTokenizer::Token::DEDENT) && !lambda_ended && !is_at_end());19011902complete_extents(suite);19031904if (multiline) {1905if (!lambda_ended) {1906consume(GDScriptTokenizer::Token::DEDENT, vformat(R"(Missing unindent at the end of %s.)", p_context));19071908} else {1909match(GDScriptTokenizer::Token::DEDENT);1910}1911} else if (previous.type == GDScriptTokenizer::Token::SEMICOLON) {1912consume(GDScriptTokenizer::Token::NEWLINE, vformat(R"(Expected newline after ";" at the end of %s.)", p_context));1913}19141915if (p_for_lambda) {1916lambda_ended = true;1917}1918current_suite = suite->parent_block;1919return suite;1920}19211922GDScriptParser::Node *GDScriptParser::parse_statement() {1923Node *result = nullptr;1924#ifdef DEBUG_ENABLED1925bool unreachable = current_suite->has_return && !current_suite->has_unreachable_code;1926#endif19271928List<AnnotationNode *> annotations;1929if (current.type != GDScriptTokenizer::Token::ANNOTATION) {1930while (!annotation_stack.is_empty()) {1931AnnotationNode *last_annotation = annotation_stack.back()->get();1932if (last_annotation->applies_to(AnnotationInfo::STATEMENT)) {1933annotations.push_front(last_annotation);1934annotation_stack.pop_back();1935} else {1936push_error(vformat(R"(Annotation "%s" cannot be applied to a statement.)", last_annotation->name));1937clear_unused_annotations();1938}1939}1940}19411942switch (current.type) {1943case GDScriptTokenizer::Token::PASS:1944advance();1945result = alloc_node<PassNode>();1946complete_extents(result);1947end_statement(R"("pass")");1948break;1949case GDScriptTokenizer::Token::VAR:1950advance();1951result = parse_variable(false, false);1952break;1953case GDScriptTokenizer::Token::TK_CONST:1954advance();1955result = parse_constant(false);1956break;1957case GDScriptTokenizer::Token::IF:1958advance();1959result = parse_if();1960break;1961case GDScriptTokenizer::Token::FOR:1962advance();1963result = parse_for();1964break;1965case GDScriptTokenizer::Token::WHILE:1966advance();1967result = parse_while();1968break;1969case GDScriptTokenizer::Token::MATCH:1970advance();1971result = parse_match();1972break;1973case GDScriptTokenizer::Token::BREAK:1974advance();1975result = parse_break();1976break;1977case GDScriptTokenizer::Token::CONTINUE:1978advance();1979result = parse_continue();1980break;1981case GDScriptTokenizer::Token::RETURN: {1982advance();1983ReturnNode *n_return = alloc_node<ReturnNode>();1984if (!is_statement_end()) {1985if (current_function && (current_function->identifier->name == GDScriptLanguage::get_singleton()->strings._init || current_function->identifier->name == GDScriptLanguage::get_singleton()->strings._static_init)) {1986push_error(R"(Constructor cannot return a value.)");1987}1988n_return->return_value = parse_expression(false);1989} else if (in_lambda && !is_statement_end_token()) {1990// Try to parse it anyway as this might not be the statement end in a lambda.1991// If this fails the expression will be nullptr, but that's the same as no return, so it's fine.1992n_return->return_value = parse_expression(false);1993}1994complete_extents(n_return);1995result = n_return;19961997current_suite->has_return = true;19981999end_statement("return statement");2000break;2001}2002case GDScriptTokenizer::Token::BREAKPOINT:2003advance();2004result = alloc_node<BreakpointNode>();2005complete_extents(result);2006end_statement(R"("breakpoint")");2007break;2008case GDScriptTokenizer::Token::ASSERT:2009advance();2010result = parse_assert();2011break;2012case GDScriptTokenizer::Token::ANNOTATION: {2013advance();2014AnnotationNode *annotation = parse_annotation(AnnotationInfo::STATEMENT | AnnotationInfo::STANDALONE);2015if (annotation != nullptr) {2016if (annotation->applies_to(AnnotationInfo::STANDALONE)) {2017if (previous.type != GDScriptTokenizer::Token::NEWLINE) {2018push_error(R"(Expected newline after a standalone annotation.)");2019}2020if (annotation->name == SNAME("@warning_ignore_start") || annotation->name == SNAME("@warning_ignore_restore")) {2021// Some annotations need to be resolved and applied in the parser.2022annotation->apply(this, nullptr, nullptr);2023} else {2024push_error(R"(Unexpected standalone annotation.)");2025}2026} else {2027annotation_stack.push_back(annotation);2028}2029}2030break;2031}2032default: {2033// Expression statement.2034ExpressionNode *expression = parse_expression(true); // Allow assignment here.2035bool has_ended_lambda = false;2036if (expression == nullptr) {2037if (in_lambda) {2038// If it's not a valid expression beginning, it might be the continuation of the outer expression where this lambda is.2039lambda_ended = true;2040has_ended_lambda = true;2041} else {2042advance();2043push_error(vformat(R"(Expected statement, found "%s" instead.)", previous.get_name()));2044}2045} else {2046end_statement("expression");2047}2048lambda_ended = lambda_ended || has_ended_lambda;2049result = expression;20502051#ifdef DEBUG_ENABLED2052if (expression != nullptr) {2053switch (expression->type) {2054case Node::ASSIGNMENT:2055case Node::AWAIT:2056case Node::CALL:2057// Fine.2058break;2059case Node::PRELOAD:2060// `preload` is a function-like keyword.2061push_warning(expression, GDScriptWarning::RETURN_VALUE_DISCARDED, "preload");2062break;2063case Node::LAMBDA:2064// Standalone lambdas can't be used, so make this an error.2065push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression);2066break;2067case Node::LITERAL:2068// Allow strings as multiline comments.2069if (static_cast<GDScriptParser::LiteralNode *>(expression)->value.get_type() != Variant::STRING) {2070push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);2071}2072break;2073case Node::TERNARY_OPERATOR:2074push_warning(expression, GDScriptWarning::STANDALONE_TERNARY);2075break;2076default:2077push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);2078}2079}2080#endif2081break;2082}2083}20842085#ifdef TOOLS_ENABLED2086int doc_comment_line = 0;2087if (result != nullptr) {2088doc_comment_line = result->start_line - 1;2089}2090#endif // TOOLS_ENABLED20912092if (result != nullptr && !annotations.is_empty()) {2093for (AnnotationNode *&annotation : annotations) {2094result->annotations.push_back(annotation);2095#ifdef TOOLS_ENABLED2096if (annotation->start_line <= doc_comment_line) {2097doc_comment_line = annotation->start_line - 1;2098}2099#endif // TOOLS_ENABLED2100}2101}21022103#ifdef TOOLS_ENABLED2104if (result != nullptr) {2105MemberDocData doc_data;2106if (has_comment(result->start_line, true)) {2107// Inline doc comment.2108doc_data = parse_doc_comment(result->start_line, true);2109} else if (doc_comment_line >= current_function->min_local_doc_line && has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) {2110// Normal doc comment.2111doc_data = parse_doc_comment(doc_comment_line);2112}21132114if (result->type == Node::CONSTANT) {2115static_cast<ConstantNode *>(result)->doc_data = doc_data;2116} else if (result->type == Node::VARIABLE) {2117static_cast<VariableNode *>(result)->doc_data = doc_data;2118}21192120current_function->min_local_doc_line = result->end_line + 1; // Prevent multiple locals from using the same doc comment.2121}2122#endif // TOOLS_ENABLED21232124#ifdef DEBUG_ENABLED2125if (unreachable && result != nullptr) {2126current_suite->has_unreachable_code = true;2127if (current_function) {2128push_warning(result, GDScriptWarning::UNREACHABLE_CODE, current_function->identifier ? current_function->identifier->name : "<anonymous lambda>");2129} else {2130// TODO: Properties setters and getters with unreachable code are not being warned2131}2132}2133#endif21342135if (panic_mode) {2136synchronize();2137}21382139return result;2140}21412142GDScriptParser::AssertNode *GDScriptParser::parse_assert() {2143// TODO: Add assert message.2144AssertNode *assert = alloc_node<AssertNode>();21452146push_multiline(true);2147consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after "assert".)");21482149assert->condition = parse_expression(false);2150if (assert->condition == nullptr) {2151push_error("Expected expression to assert.");2152pop_multiline();2153complete_extents(assert);2154return nullptr;2155}21562157if (match(GDScriptTokenizer::Token::COMMA) && !check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) {2158assert->message = parse_expression(false);2159if (assert->message == nullptr) {2160push_error(R"(Expected error message for assert after ",".)");2161pop_multiline();2162complete_extents(assert);2163return nullptr;2164}2165match(GDScriptTokenizer::Token::COMMA);2166}21672168pop_multiline();2169consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after assert expression.)*");21702171complete_extents(assert);2172end_statement(R"("assert")");21732174return assert;2175}21762177GDScriptParser::BreakNode *GDScriptParser::parse_break() {2178if (!can_break) {2179push_error(R"(Cannot use "break" outside of a loop.)");2180}2181BreakNode *break_node = alloc_node<BreakNode>();2182complete_extents(break_node);2183end_statement(R"("break")");2184return break_node;2185}21862187GDScriptParser::ContinueNode *GDScriptParser::parse_continue() {2188if (!can_continue) {2189push_error(R"(Cannot use "continue" outside of a loop.)");2190}2191current_suite->has_continue = true;2192ContinueNode *cont = alloc_node<ContinueNode>();2193complete_extents(cont);2194end_statement(R"("continue")");2195return cont;2196}21972198GDScriptParser::ForNode *GDScriptParser::parse_for() {2199ForNode *n_for = alloc_node<ForNode>();22002201if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected loop variable name after "for".)")) {2202n_for->variable = parse_identifier();2203}22042205if (match(GDScriptTokenizer::Token::COLON)) {2206n_for->datatype_specifier = parse_type();2207if (n_for->datatype_specifier == nullptr) {2208push_error(R"(Expected type specifier after ":".)");2209}2210}22112212if (n_for->datatype_specifier == nullptr) {2213consume(GDScriptTokenizer::Token::TK_IN, R"(Expected "in" or ":" after "for" variable name.)");2214} else {2215consume(GDScriptTokenizer::Token::TK_IN, R"(Expected "in" after "for" variable type specifier.)");2216}22172218n_for->list = parse_expression(false);22192220if (!n_for->list) {2221push_error(R"(Expected iterable after "in".)");2222}22232224consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "for" condition.)");22252226// Save break/continue state.2227bool could_break = can_break;2228bool could_continue = can_continue;22292230// Allow break/continue.2231can_break = true;2232can_continue = true;22332234SuiteNode *suite = alloc_node<SuiteNode>();2235if (n_for->variable) {2236const SuiteNode::Local &local = current_suite->get_local(n_for->variable->name);2237if (local.type != SuiteNode::Local::UNDEFINED) {2238push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", local.get_name(), n_for->variable->name), n_for->variable);2239}2240suite->add_local(SuiteNode::Local(n_for->variable, current_function));2241}2242suite->is_in_loop = true;2243n_for->loop = parse_suite(R"("for" block)", suite);2244complete_extents(n_for);22452246// Reset break/continue state.2247can_break = could_break;2248can_continue = could_continue;22492250return n_for;2251}22522253GDScriptParser::IfNode *GDScriptParser::parse_if(const String &p_token) {2254IfNode *n_if = alloc_node<IfNode>();22552256n_if->condition = parse_expression(false);2257if (n_if->condition == nullptr) {2258push_error(vformat(R"(Expected conditional expression after "%s".)", p_token));2259}22602261consume(GDScriptTokenizer::Token::COLON, vformat(R"(Expected ":" after "%s" condition.)", p_token));22622263n_if->true_block = parse_suite(vformat(R"("%s" block)", p_token));2264n_if->true_block->parent_if = n_if;22652266if (n_if->true_block->has_continue) {2267current_suite->has_continue = true;2268}22692270if (match(GDScriptTokenizer::Token::ELIF)) {2271SuiteNode *else_block = alloc_node<SuiteNode>();2272else_block->parent_function = current_function;2273else_block->parent_block = current_suite;22742275SuiteNode *previous_suite = current_suite;2276current_suite = else_block;22772278IfNode *elif = parse_if("elif");2279else_block->statements.push_back(elif);2280complete_extents(else_block);2281n_if->false_block = else_block;22822283current_suite = previous_suite;2284} else if (match(GDScriptTokenizer::Token::ELSE)) {2285consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "else".)");2286n_if->false_block = parse_suite(R"("else" block)");2287}2288complete_extents(n_if);22892290if (n_if->false_block != nullptr && n_if->false_block->has_return && n_if->true_block->has_return) {2291current_suite->has_return = true;2292}2293if (n_if->false_block != nullptr && n_if->false_block->has_continue) {2294current_suite->has_continue = true;2295}22962297return n_if;2298}22992300GDScriptParser::MatchNode *GDScriptParser::parse_match() {2301MatchNode *match_node = alloc_node<MatchNode>();23022303match_node->test = parse_expression(false);2304if (match_node->test == nullptr) {2305push_error(R"(Expected expression to test after "match".)");2306}23072308consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "match" expression.)");2309consume(GDScriptTokenizer::Token::NEWLINE, R"(Expected a newline after "match" statement.)");23102311if (!consume(GDScriptTokenizer::Token::INDENT, R"(Expected an indented block after "match" statement.)")) {2312complete_extents(match_node);2313return match_node;2314}23152316bool all_have_return = true;2317bool have_wildcard = false;23182319List<AnnotationNode *> match_branch_annotation_stack;23202321while (!check(GDScriptTokenizer::Token::DEDENT) && !is_at_end()) {2322if (match(GDScriptTokenizer::Token::PASS)) {2323consume(GDScriptTokenizer::Token::NEWLINE, R"(Expected newline after "pass".)");2324continue;2325}23262327if (match(GDScriptTokenizer::Token::ANNOTATION)) {2328AnnotationNode *annotation = parse_annotation(AnnotationInfo::STATEMENT);2329if (annotation == nullptr) {2330continue;2331}2332if (annotation->name != SNAME("@warning_ignore")) {2333push_error(vformat(R"(Annotation "%s" is not allowed in this level.)", annotation->name), annotation);2334continue;2335}2336match_branch_annotation_stack.push_back(annotation);2337continue;2338}23392340MatchBranchNode *branch = parse_match_branch();2341if (branch == nullptr) {2342advance();2343continue;2344}23452346for (AnnotationNode *annotation : match_branch_annotation_stack) {2347branch->annotations.push_back(annotation);2348}2349match_branch_annotation_stack.clear();23502351#ifdef DEBUG_ENABLED2352if (have_wildcard && !branch->patterns.is_empty()) {2353push_warning(branch->patterns[0], GDScriptWarning::UNREACHABLE_PATTERN);2354}2355#endif23562357have_wildcard = have_wildcard || branch->has_wildcard;2358all_have_return = all_have_return && branch->block->has_return;2359match_node->branches.push_back(branch);2360}2361complete_extents(match_node);23622363consume(GDScriptTokenizer::Token::DEDENT, R"(Expected an indented block after "match" statement.)");23642365if (all_have_return && have_wildcard) {2366current_suite->has_return = true;2367}23682369for (const AnnotationNode *annotation : match_branch_annotation_stack) {2370push_error(vformat(R"(Annotation "%s" does not precede a valid target, so it will have no effect.)", annotation->name), annotation);2371}2372match_branch_annotation_stack.clear();23732374return match_node;2375}23762377GDScriptParser::MatchBranchNode *GDScriptParser::parse_match_branch() {2378MatchBranchNode *branch = alloc_node<MatchBranchNode>();2379reset_extents(branch, current);23802381bool has_bind = false;23822383do {2384PatternNode *pattern = parse_match_pattern();2385if (pattern == nullptr) {2386continue;2387}2388if (pattern->binds.size() > 0) {2389has_bind = true;2390}2391if (branch->patterns.size() > 0 && has_bind) {2392push_error(R"(Cannot use a variable bind with multiple patterns.)");2393}2394if (pattern->pattern_type == PatternNode::PT_REST) {2395push_error(R"(Rest pattern can only be used inside array and dictionary patterns.)");2396} else if (pattern->pattern_type == PatternNode::PT_BIND || pattern->pattern_type == PatternNode::PT_WILDCARD) {2397branch->has_wildcard = true;2398}2399branch->patterns.push_back(pattern);2400} while (match(GDScriptTokenizer::Token::COMMA));24012402if (branch->patterns.is_empty()) {2403push_error(R"(No pattern found for "match" branch.)");2404}24052406bool has_guard = false;2407if (match(GDScriptTokenizer::Token::WHEN)) {2408// Pattern guard.2409// Create block for guard because it also needs to access the bound variables from patterns, and we don't want to add them to the outer scope.2410branch->guard_body = alloc_node<SuiteNode>();2411if (branch->patterns.size() > 0) {2412for (const KeyValue<StringName, IdentifierNode *> &E : branch->patterns[0]->binds) {2413SuiteNode::Local local(E.value, current_function);2414local.type = SuiteNode::Local::PATTERN_BIND;2415branch->guard_body->add_local(local);2416}2417}24182419SuiteNode *parent_block = current_suite;2420branch->guard_body->parent_block = parent_block;2421current_suite = branch->guard_body;24222423ExpressionNode *guard = parse_expression(false);2424if (guard == nullptr) {2425push_error(R"(Expected expression for pattern guard after "when".)");2426} else {2427branch->guard_body->statements.append(guard);2428}2429current_suite = parent_block;2430complete_extents(branch->guard_body);24312432has_guard = true;2433branch->has_wildcard = false; // If it has a guard, the wildcard might still not match.2434}24352436if (!consume(GDScriptTokenizer::Token::COLON, vformat(R"(Expected ":"%s after "match" %s.)", has_guard ? "" : R"( or "when")", has_guard ? "pattern guard" : "patterns"))) {2437branch->block = alloc_recovery_suite();2438complete_extents(branch);2439// Consume the whole line and treat the next one as new match branch.2440while (current.type != GDScriptTokenizer::Token::NEWLINE && !is_at_end()) {2441advance();2442}2443if (!is_at_end()) {2444advance();2445}2446return branch;2447}24482449SuiteNode *suite = alloc_node<SuiteNode>();2450if (branch->patterns.size() > 0) {2451for (const KeyValue<StringName, IdentifierNode *> &E : branch->patterns[0]->binds) {2452SuiteNode::Local local(E.value, current_function);2453local.type = SuiteNode::Local::PATTERN_BIND;2454suite->add_local(local);2455}2456}24572458branch->block = parse_suite("match pattern block", suite);2459complete_extents(branch);24602461return branch;2462}24632464GDScriptParser::PatternNode *GDScriptParser::parse_match_pattern(PatternNode *p_root_pattern) {2465PatternNode *pattern = alloc_node<PatternNode>();2466reset_extents(pattern, current);24672468switch (current.type) {2469case GDScriptTokenizer::Token::VAR: {2470// Bind.2471advance();2472if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected bind name after "var".)")) {2473complete_extents(pattern);2474return nullptr;2475}2476pattern->pattern_type = PatternNode::PT_BIND;2477pattern->bind = parse_identifier();24782479PatternNode *root_pattern = p_root_pattern == nullptr ? pattern : p_root_pattern;24802481if (p_root_pattern != nullptr) {2482if (p_root_pattern->has_bind(pattern->bind->name)) {2483push_error(vformat(R"(Bind variable name "%s" was already used in this pattern.)", pattern->bind->name));2484complete_extents(pattern);2485return nullptr;2486}2487}24882489if (current_suite->has_local(pattern->bind->name)) {2490push_error(vformat(R"(There's already a %s named "%s" in this scope.)", current_suite->get_local(pattern->bind->name).get_name(), pattern->bind->name));2491complete_extents(pattern);2492return nullptr;2493}24942495root_pattern->binds[pattern->bind->name] = pattern->bind;24962497} break;2498case GDScriptTokenizer::Token::UNDERSCORE:2499// Wildcard.2500advance();2501pattern->pattern_type = PatternNode::PT_WILDCARD;2502break;2503case GDScriptTokenizer::Token::PERIOD_PERIOD:2504// Rest.2505advance();2506pattern->pattern_type = PatternNode::PT_REST;2507break;2508case GDScriptTokenizer::Token::BRACKET_OPEN: {2509// Array.2510push_multiline(true);2511advance();2512pattern->pattern_type = PatternNode::PT_ARRAY;2513do {2514if (is_at_end() || check(GDScriptTokenizer::Token::BRACKET_CLOSE)) {2515break;2516}2517PatternNode *sub_pattern = parse_match_pattern(p_root_pattern != nullptr ? p_root_pattern : pattern);2518if (sub_pattern == nullptr) {2519continue;2520}2521if (pattern->rest_used) {2522push_error(R"(The ".." pattern must be the last element in the pattern array.)");2523} else if (sub_pattern->pattern_type == PatternNode::PT_REST) {2524pattern->rest_used = true;2525}2526pattern->array.push_back(sub_pattern);2527} while (match(GDScriptTokenizer::Token::COMMA));2528consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected "]" to close the array pattern.)");2529pop_multiline();2530break;2531}2532case GDScriptTokenizer::Token::BRACE_OPEN: {2533// Dictionary.2534push_multiline(true);2535advance();2536pattern->pattern_type = PatternNode::PT_DICTIONARY;2537do {2538if (check(GDScriptTokenizer::Token::BRACE_CLOSE) || is_at_end()) {2539break;2540}2541if (match(GDScriptTokenizer::Token::PERIOD_PERIOD)) {2542// Rest.2543if (pattern->rest_used) {2544push_error(R"(The ".." pattern must be the last element in the pattern dictionary.)");2545} else {2546PatternNode *sub_pattern = alloc_node<PatternNode>();2547complete_extents(sub_pattern);2548sub_pattern->pattern_type = PatternNode::PT_REST;2549pattern->dictionary.push_back({ nullptr, sub_pattern });2550pattern->rest_used = true;2551}2552} else {2553ExpressionNode *key = parse_expression(false);2554if (key == nullptr) {2555push_error(R"(Expected expression as key for dictionary pattern.)");2556}2557if (match(GDScriptTokenizer::Token::COLON)) {2558// Value pattern.2559PatternNode *sub_pattern = parse_match_pattern(p_root_pattern != nullptr ? p_root_pattern : pattern);2560if (sub_pattern == nullptr) {2561continue;2562}2563if (pattern->rest_used) {2564push_error(R"(The ".." pattern must be the last element in the pattern dictionary.)");2565} else if (sub_pattern->pattern_type == PatternNode::PT_REST) {2566push_error(R"(The ".." pattern cannot be used as a value.)");2567} else {2568pattern->dictionary.push_back({ key, sub_pattern });2569}2570} else {2571// Key match only.2572pattern->dictionary.push_back({ key, nullptr });2573}2574}2575} while (match(GDScriptTokenizer::Token::COMMA));2576consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected "}" to close the dictionary pattern.)");2577pop_multiline();2578break;2579}2580default: {2581// Expression.2582ExpressionNode *expression = parse_expression(false);2583if (expression == nullptr) {2584push_error(R"(Expected expression for match pattern.)");2585complete_extents(pattern);2586return nullptr;2587} else {2588if (expression->type == GDScriptParser::Node::LITERAL) {2589pattern->pattern_type = PatternNode::PT_LITERAL;2590} else {2591pattern->pattern_type = PatternNode::PT_EXPRESSION;2592}2593pattern->expression = expression;2594}2595break;2596}2597}2598complete_extents(pattern);25992600return pattern;2601}26022603bool GDScriptParser::PatternNode::has_bind(const StringName &p_name) {2604return binds.has(p_name);2605}26062607GDScriptParser::IdentifierNode *GDScriptParser::PatternNode::get_bind(const StringName &p_name) {2608return binds[p_name];2609}26102611GDScriptParser::WhileNode *GDScriptParser::parse_while() {2612WhileNode *n_while = alloc_node<WhileNode>();26132614n_while->condition = parse_expression(false);2615if (n_while->condition == nullptr) {2616push_error(R"(Expected conditional expression after "while".)");2617}26182619consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "while" condition.)");26202621// Save break/continue state.2622bool could_break = can_break;2623bool could_continue = can_continue;26242625// Allow break/continue.2626can_break = true;2627can_continue = true;26282629SuiteNode *suite = alloc_node<SuiteNode>();2630suite->is_in_loop = true;2631n_while->loop = parse_suite(R"("while" block)", suite);2632complete_extents(n_while);26332634// Reset break/continue state.2635can_break = could_break;2636can_continue = could_continue;26372638return n_while;2639}26402641GDScriptParser::ExpressionNode *GDScriptParser::parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign) {2642// Switch multiline mode on for grouping tokens.2643// Do this early to avoid the tokenizer generating whitespace tokens.2644switch (current.type) {2645case GDScriptTokenizer::Token::PARENTHESIS_OPEN:2646case GDScriptTokenizer::Token::BRACE_OPEN:2647case GDScriptTokenizer::Token::BRACKET_OPEN:2648push_multiline(true);2649break;2650default:2651break; // Nothing to do.2652}26532654// Completion can appear whenever an expression is expected.2655make_completion_context(COMPLETION_IDENTIFIER, nullptr, -1, false);26562657GDScriptTokenizer::Token token = current;2658GDScriptTokenizer::Token::Type token_type = token.type;2659if (token.is_identifier()) {2660// Allow keywords that can be treated as identifiers.2661token_type = GDScriptTokenizer::Token::IDENTIFIER;2662}2663ParseFunction prefix_rule = get_rule(token_type)->prefix;26642665if (prefix_rule == nullptr) {2666// Expected expression. Let the caller give the proper error message.2667return nullptr;2668}26692670advance(); // Only consume the token if there's a valid rule.26712672// After a token was consumed, update the completion context regardless of a previously set context.26732674ExpressionNode *previous_operand = (this->*prefix_rule)(nullptr, p_can_assign);26752676#ifdef TOOLS_ENABLED2677// HACK: We can't create a context in parse_identifier since it is used in places were we don't want completion.2678if (previous_operand != nullptr && previous_operand->type == GDScriptParser::Node::IDENTIFIER && prefix_rule == static_cast<ParseFunction>(&GDScriptParser::parse_identifier)) {2679make_completion_context(COMPLETION_IDENTIFIER, previous_operand);2680}2681#endif26822683while (p_precedence <= get_rule(current.type)->precedence) {2684if (previous_operand == nullptr || (p_stop_on_assign && current.type == GDScriptTokenizer::Token::EQUAL) || lambda_ended) {2685return previous_operand;2686}2687// Also switch multiline mode on here for infix operators.2688switch (current.type) {2689// case GDScriptTokenizer::Token::BRACE_OPEN: // Not an infix operator.2690case GDScriptTokenizer::Token::PARENTHESIS_OPEN:2691case GDScriptTokenizer::Token::BRACKET_OPEN:2692push_multiline(true);2693break;2694default:2695break; // Nothing to do.2696}2697token = advance();2698ParseFunction infix_rule = get_rule(token.type)->infix;2699previous_operand = (this->*infix_rule)(previous_operand, p_can_assign);2700}27012702return previous_operand;2703}27042705GDScriptParser::ExpressionNode *GDScriptParser::parse_expression(bool p_can_assign, bool p_stop_on_assign) {2706return parse_precedence(PREC_ASSIGNMENT, p_can_assign, p_stop_on_assign);2707}27082709GDScriptParser::IdentifierNode *GDScriptParser::parse_identifier() {2710IdentifierNode *identifier = static_cast<IdentifierNode *>(parse_identifier(nullptr, false));2711#ifdef DEBUG_ENABLED2712// Check for spoofing here (if available in TextServer) since this isn't called inside expressions. This is only relevant for declarations.2713if (identifier && TS->has_feature(TextServer::FEATURE_UNICODE_SECURITY) && TS->spoof_check(identifier->name)) {2714push_warning(identifier, GDScriptWarning::CONFUSABLE_IDENTIFIER, identifier->name.operator String());2715}2716#endif2717return identifier;2718}27192720GDScriptParser::ExpressionNode *GDScriptParser::parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign) {2721if (!previous.is_identifier()) {2722ERR_FAIL_V_MSG(nullptr, "Parser bug: parsing identifier node without identifier token.");2723}2724IdentifierNode *identifier = alloc_node<IdentifierNode>();2725complete_extents(identifier);2726identifier->name = previous.get_identifier();2727if (identifier->name.operator String().is_empty()) {2728print_line("Empty identifier found.");2729}2730identifier->suite = current_suite;27312732if (current_suite != nullptr && current_suite->has_local(identifier->name)) {2733const SuiteNode::Local &declaration = current_suite->get_local(identifier->name);27342735identifier->source_function = declaration.source_function;2736switch (declaration.type) {2737case SuiteNode::Local::CONSTANT:2738identifier->source = IdentifierNode::LOCAL_CONSTANT;2739identifier->constant_source = declaration.constant;2740declaration.constant->usages++;2741break;2742case SuiteNode::Local::VARIABLE:2743identifier->source = IdentifierNode::LOCAL_VARIABLE;2744identifier->variable_source = declaration.variable;2745declaration.variable->usages++;2746break;2747case SuiteNode::Local::PARAMETER:2748identifier->source = IdentifierNode::FUNCTION_PARAMETER;2749identifier->parameter_source = declaration.parameter;2750declaration.parameter->usages++;2751break;2752case SuiteNode::Local::FOR_VARIABLE:2753identifier->source = IdentifierNode::LOCAL_ITERATOR;2754identifier->bind_source = declaration.bind;2755declaration.bind->usages++;2756break;2757case SuiteNode::Local::PATTERN_BIND:2758identifier->source = IdentifierNode::LOCAL_BIND;2759identifier->bind_source = declaration.bind;2760declaration.bind->usages++;2761break;2762case SuiteNode::Local::UNDEFINED:2763ERR_FAIL_V_MSG(nullptr, "Undefined local found.");2764}2765}27662767return identifier;2768}27692770GDScriptParser::LiteralNode *GDScriptParser::parse_literal() {2771return static_cast<LiteralNode *>(parse_literal(nullptr, false));2772}27732774GDScriptParser::ExpressionNode *GDScriptParser::parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign) {2775if (previous.type != GDScriptTokenizer::Token::LITERAL) {2776push_error("Parser bug: parsing literal node without literal token.");2777ERR_FAIL_V_MSG(nullptr, "Parser bug: parsing literal node without literal token.");2778}27792780LiteralNode *literal = alloc_node<LiteralNode>();2781literal->value = previous.literal;2782reset_extents(literal, p_previous_operand);2783update_extents(literal);2784make_completion_context(COMPLETION_NONE, literal, -1);2785complete_extents(literal);2786return literal;2787}27882789GDScriptParser::ExpressionNode *GDScriptParser::parse_self(ExpressionNode *p_previous_operand, bool p_can_assign) {2790if (current_function && current_function->is_static) {2791push_error(R"(Cannot use "self" inside a static function.)");2792}2793SelfNode *self = alloc_node<SelfNode>();2794complete_extents(self);2795self->current_class = current_class;2796return self;2797}27982799GDScriptParser::ExpressionNode *GDScriptParser::parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign) {2800GDScriptTokenizer::Token::Type op_type = previous.type;2801LiteralNode *constant = alloc_node<LiteralNode>();2802complete_extents(constant);28032804switch (op_type) {2805case GDScriptTokenizer::Token::CONST_PI:2806constant->value = Math::PI;2807break;2808case GDScriptTokenizer::Token::CONST_TAU:2809constant->value = Math::TAU;2810break;2811case GDScriptTokenizer::Token::CONST_INF:2812constant->value = Math::INF;2813break;2814case GDScriptTokenizer::Token::CONST_NAN:2815constant->value = Math::NaN;2816break;2817default:2818return nullptr; // Unreachable.2819}28202821return constant;2822}28232824GDScriptParser::ExpressionNode *GDScriptParser::parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign) {2825GDScriptTokenizer::Token::Type op_type = previous.type;2826UnaryOpNode *operation = alloc_node<UnaryOpNode>();28272828switch (op_type) {2829case GDScriptTokenizer::Token::MINUS:2830operation->operation = UnaryOpNode::OP_NEGATIVE;2831operation->variant_op = Variant::OP_NEGATE;2832operation->operand = parse_precedence(PREC_SIGN, false);2833if (operation->operand == nullptr) {2834push_error(R"(Expected expression after "-" operator.)");2835}2836break;2837case GDScriptTokenizer::Token::PLUS:2838operation->operation = UnaryOpNode::OP_POSITIVE;2839operation->variant_op = Variant::OP_POSITIVE;2840operation->operand = parse_precedence(PREC_SIGN, false);2841if (operation->operand == nullptr) {2842push_error(R"(Expected expression after "+" operator.)");2843}2844break;2845case GDScriptTokenizer::Token::TILDE:2846operation->operation = UnaryOpNode::OP_COMPLEMENT;2847operation->variant_op = Variant::OP_BIT_NEGATE;2848operation->operand = parse_precedence(PREC_BIT_NOT, false);2849if (operation->operand == nullptr) {2850push_error(R"(Expected expression after "~" operator.)");2851}2852break;2853case GDScriptTokenizer::Token::NOT:2854case GDScriptTokenizer::Token::BANG:2855operation->operation = UnaryOpNode::OP_LOGIC_NOT;2856operation->variant_op = Variant::OP_NOT;2857operation->operand = parse_precedence(PREC_LOGIC_NOT, false);2858if (operation->operand == nullptr) {2859push_error(vformat(R"(Expected expression after "%s" operator.)", op_type == GDScriptTokenizer::Token::NOT ? "not" : "!"));2860}2861break;2862default:2863complete_extents(operation);2864return nullptr; // Unreachable.2865}2866complete_extents(operation);28672868return operation;2869}28702871GDScriptParser::ExpressionNode *GDScriptParser::parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign) {2872// check that NOT is followed by IN by consuming it before calling parse_binary_operator which will only receive a plain IN2873UnaryOpNode *operation = alloc_node<UnaryOpNode>();2874reset_extents(operation, p_previous_operand);2875update_extents(operation);2876consume(GDScriptTokenizer::Token::TK_IN, R"(Expected "in" after "not" in content-test operator.)");2877ExpressionNode *in_operation = parse_binary_operator(p_previous_operand, p_can_assign);2878operation->operation = UnaryOpNode::OP_LOGIC_NOT;2879operation->variant_op = Variant::OP_NOT;2880operation->operand = in_operation;2881complete_extents(operation);2882return operation;2883}28842885GDScriptParser::ExpressionNode *GDScriptParser::parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign) {2886GDScriptTokenizer::Token op = previous;2887BinaryOpNode *operation = alloc_node<BinaryOpNode>();2888reset_extents(operation, p_previous_operand);2889update_extents(operation);28902891Precedence precedence = (Precedence)(get_rule(op.type)->precedence + 1);2892operation->left_operand = p_previous_operand;2893operation->right_operand = parse_precedence(precedence, false);2894complete_extents(operation);28952896if (operation->right_operand == nullptr) {2897push_error(vformat(R"(Expected expression after "%s" operator.)", op.get_name()));2898}28992900// TODO: Also for unary, ternary, and assignment.2901switch (op.type) {2902case GDScriptTokenizer::Token::PLUS:2903operation->operation = BinaryOpNode::OP_ADDITION;2904operation->variant_op = Variant::OP_ADD;2905break;2906case GDScriptTokenizer::Token::MINUS:2907operation->operation = BinaryOpNode::OP_SUBTRACTION;2908operation->variant_op = Variant::OP_SUBTRACT;2909break;2910case GDScriptTokenizer::Token::STAR:2911operation->operation = BinaryOpNode::OP_MULTIPLICATION;2912operation->variant_op = Variant::OP_MULTIPLY;2913break;2914case GDScriptTokenizer::Token::SLASH:2915operation->operation = BinaryOpNode::OP_DIVISION;2916operation->variant_op = Variant::OP_DIVIDE;2917break;2918case GDScriptTokenizer::Token::PERCENT:2919operation->operation = BinaryOpNode::OP_MODULO;2920operation->variant_op = Variant::OP_MODULE;2921break;2922case GDScriptTokenizer::Token::STAR_STAR:2923operation->operation = BinaryOpNode::OP_POWER;2924operation->variant_op = Variant::OP_POWER;2925break;2926case GDScriptTokenizer::Token::LESS_LESS:2927operation->operation = BinaryOpNode::OP_BIT_LEFT_SHIFT;2928operation->variant_op = Variant::OP_SHIFT_LEFT;2929break;2930case GDScriptTokenizer::Token::GREATER_GREATER:2931operation->operation = BinaryOpNode::OP_BIT_RIGHT_SHIFT;2932operation->variant_op = Variant::OP_SHIFT_RIGHT;2933break;2934case GDScriptTokenizer::Token::AMPERSAND:2935operation->operation = BinaryOpNode::OP_BIT_AND;2936operation->variant_op = Variant::OP_BIT_AND;2937break;2938case GDScriptTokenizer::Token::PIPE:2939operation->operation = BinaryOpNode::OP_BIT_OR;2940operation->variant_op = Variant::OP_BIT_OR;2941break;2942case GDScriptTokenizer::Token::CARET:2943operation->operation = BinaryOpNode::OP_BIT_XOR;2944operation->variant_op = Variant::OP_BIT_XOR;2945break;2946case GDScriptTokenizer::Token::AND:2947case GDScriptTokenizer::Token::AMPERSAND_AMPERSAND:2948operation->operation = BinaryOpNode::OP_LOGIC_AND;2949operation->variant_op = Variant::OP_AND;2950break;2951case GDScriptTokenizer::Token::OR:2952case GDScriptTokenizer::Token::PIPE_PIPE:2953operation->operation = BinaryOpNode::OP_LOGIC_OR;2954operation->variant_op = Variant::OP_OR;2955break;2956case GDScriptTokenizer::Token::TK_IN:2957operation->operation = BinaryOpNode::OP_CONTENT_TEST;2958operation->variant_op = Variant::OP_IN;2959break;2960case GDScriptTokenizer::Token::EQUAL_EQUAL:2961operation->operation = BinaryOpNode::OP_COMP_EQUAL;2962operation->variant_op = Variant::OP_EQUAL;2963break;2964case GDScriptTokenizer::Token::BANG_EQUAL:2965operation->operation = BinaryOpNode::OP_COMP_NOT_EQUAL;2966operation->variant_op = Variant::OP_NOT_EQUAL;2967break;2968case GDScriptTokenizer::Token::LESS:2969operation->operation = BinaryOpNode::OP_COMP_LESS;2970operation->variant_op = Variant::OP_LESS;2971break;2972case GDScriptTokenizer::Token::LESS_EQUAL:2973operation->operation = BinaryOpNode::OP_COMP_LESS_EQUAL;2974operation->variant_op = Variant::OP_LESS_EQUAL;2975break;2976case GDScriptTokenizer::Token::GREATER:2977operation->operation = BinaryOpNode::OP_COMP_GREATER;2978operation->variant_op = Variant::OP_GREATER;2979break;2980case GDScriptTokenizer::Token::GREATER_EQUAL:2981operation->operation = BinaryOpNode::OP_COMP_GREATER_EQUAL;2982operation->variant_op = Variant::OP_GREATER_EQUAL;2983break;2984default:2985return nullptr; // Unreachable.2986}29872988return operation;2989}29902991GDScriptParser::ExpressionNode *GDScriptParser::parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign) {2992// Only one ternary operation exists, so no abstraction here.2993TernaryOpNode *operation = alloc_node<TernaryOpNode>();2994reset_extents(operation, p_previous_operand);2995update_extents(operation);29962997operation->true_expr = p_previous_operand;2998operation->condition = parse_precedence(PREC_TERNARY, false);29993000if (operation->condition == nullptr) {3001push_error(R"(Expected expression as ternary condition after "if".)");3002}30033004consume(GDScriptTokenizer::Token::ELSE, R"(Expected "else" after ternary operator condition.)");30053006operation->false_expr = parse_precedence(PREC_TERNARY, false);30073008if (operation->false_expr == nullptr) {3009push_error(R"(Expected expression after "else".)");3010}30113012complete_extents(operation);3013return operation;3014}30153016GDScriptParser::ExpressionNode *GDScriptParser::parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign) {3017if (!p_can_assign) {3018push_error("Assignment is not allowed inside an expression.");3019return parse_expression(false); // Return the following expression.3020}3021if (p_previous_operand == nullptr) {3022return parse_expression(false); // Return the following expression.3023}30243025switch (p_previous_operand->type) {3026case Node::IDENTIFIER: {3027#ifdef DEBUG_ENABLED3028// Get source to store assignment count.3029// Also remove one usage since assignment isn't usage.3030IdentifierNode *id = static_cast<IdentifierNode *>(p_previous_operand);3031switch (id->source) {3032case IdentifierNode::LOCAL_VARIABLE:3033id->variable_source->usages--;3034break;3035case IdentifierNode::LOCAL_CONSTANT:3036id->constant_source->usages--;3037break;3038case IdentifierNode::FUNCTION_PARAMETER:3039id->parameter_source->usages--;3040break;3041case IdentifierNode::LOCAL_ITERATOR:3042case IdentifierNode::LOCAL_BIND:3043id->bind_source->usages--;3044break;3045default:3046break;3047}3048#endif3049} break;3050case Node::SUBSCRIPT:3051// Okay.3052break;3053default:3054push_error(R"(Only identifier, attribute access, and subscription access can be used as assignment target.)");3055return parse_expression(false); // Return the following expression.3056}30573058AssignmentNode *assignment = alloc_node<AssignmentNode>();3059reset_extents(assignment, p_previous_operand);3060update_extents(assignment);30613062make_completion_context(COMPLETION_ASSIGN, assignment);3063switch (previous.type) {3064case GDScriptTokenizer::Token::EQUAL:3065assignment->operation = AssignmentNode::OP_NONE;3066assignment->variant_op = Variant::OP_MAX;3067break;3068case GDScriptTokenizer::Token::PLUS_EQUAL:3069assignment->operation = AssignmentNode::OP_ADDITION;3070assignment->variant_op = Variant::OP_ADD;3071break;3072case GDScriptTokenizer::Token::MINUS_EQUAL:3073assignment->operation = AssignmentNode::OP_SUBTRACTION;3074assignment->variant_op = Variant::OP_SUBTRACT;3075break;3076case GDScriptTokenizer::Token::STAR_EQUAL:3077assignment->operation = AssignmentNode::OP_MULTIPLICATION;3078assignment->variant_op = Variant::OP_MULTIPLY;3079break;3080case GDScriptTokenizer::Token::STAR_STAR_EQUAL:3081assignment->operation = AssignmentNode::OP_POWER;3082assignment->variant_op = Variant::OP_POWER;3083break;3084case GDScriptTokenizer::Token::SLASH_EQUAL:3085assignment->operation = AssignmentNode::OP_DIVISION;3086assignment->variant_op = Variant::OP_DIVIDE;3087break;3088case GDScriptTokenizer::Token::PERCENT_EQUAL:3089assignment->operation = AssignmentNode::OP_MODULO;3090assignment->variant_op = Variant::OP_MODULE;3091break;3092case GDScriptTokenizer::Token::LESS_LESS_EQUAL:3093assignment->operation = AssignmentNode::OP_BIT_SHIFT_LEFT;3094assignment->variant_op = Variant::OP_SHIFT_LEFT;3095break;3096case GDScriptTokenizer::Token::GREATER_GREATER_EQUAL:3097assignment->operation = AssignmentNode::OP_BIT_SHIFT_RIGHT;3098assignment->variant_op = Variant::OP_SHIFT_RIGHT;3099break;3100case GDScriptTokenizer::Token::AMPERSAND_EQUAL:3101assignment->operation = AssignmentNode::OP_BIT_AND;3102assignment->variant_op = Variant::OP_BIT_AND;3103break;3104case GDScriptTokenizer::Token::PIPE_EQUAL:3105assignment->operation = AssignmentNode::OP_BIT_OR;3106assignment->variant_op = Variant::OP_BIT_OR;3107break;3108case GDScriptTokenizer::Token::CARET_EQUAL:3109assignment->operation = AssignmentNode::OP_BIT_XOR;3110assignment->variant_op = Variant::OP_BIT_XOR;3111break;3112default:3113break; // Unreachable.3114}3115assignment->assignee = p_previous_operand;3116assignment->assigned_value = parse_expression(false);3117#ifdef TOOLS_ENABLED3118if (assignment->assigned_value != nullptr && assignment->assigned_value->type == GDScriptParser::Node::IDENTIFIER) {3119override_completion_context(assignment->assigned_value, COMPLETION_ASSIGN, assignment);3120}3121#endif3122if (assignment->assigned_value == nullptr) {3123push_error(R"(Expected an expression after "=".)");3124}3125complete_extents(assignment);31263127return assignment;3128}31293130GDScriptParser::ExpressionNode *GDScriptParser::parse_await(ExpressionNode *p_previous_operand, bool p_can_assign) {3131AwaitNode *await = alloc_node<AwaitNode>();3132ExpressionNode *element = parse_precedence(PREC_AWAIT, false);3133if (element == nullptr) {3134push_error(R"(Expected signal or coroutine after "await".)");3135}3136await->to_await = element;3137complete_extents(await);31383139if (current_function) { // Might be null in a getter or setter.3140current_function->is_coroutine = true;3141}31423143return await;3144}31453146GDScriptParser::ExpressionNode *GDScriptParser::parse_array(ExpressionNode *p_previous_operand, bool p_can_assign) {3147ArrayNode *array = alloc_node<ArrayNode>();31483149if (!check(GDScriptTokenizer::Token::BRACKET_CLOSE)) {3150do {3151if (check(GDScriptTokenizer::Token::BRACKET_CLOSE)) {3152// Allow for trailing comma.3153break;3154}31553156ExpressionNode *element = parse_expression(false);3157if (element == nullptr) {3158push_error(R"(Expected expression as array element.)");3159} else {3160array->elements.push_back(element);3161}3162} while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end());3163}3164pop_multiline();3165consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected closing "]" after array elements.)");3166complete_extents(array);31673168return array;3169}31703171GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign) {3172DictionaryNode *dictionary = alloc_node<DictionaryNode>();31733174bool decided_style = false;3175if (!check(GDScriptTokenizer::Token::BRACE_CLOSE)) {3176do {3177if (check(GDScriptTokenizer::Token::BRACE_CLOSE)) {3178// Allow for trailing comma.3179break;3180}31813182// Key.3183ExpressionNode *key = parse_expression(false, true); // Stop on "=" so we can check for Lua table style.31843185if (key == nullptr) {3186push_error(R"(Expected expression as dictionary key.)");3187}31883189if (!decided_style) {3190switch (current.type) {3191case GDScriptTokenizer::Token::COLON:3192dictionary->style = DictionaryNode::PYTHON_DICT;3193break;3194case GDScriptTokenizer::Token::EQUAL:3195dictionary->style = DictionaryNode::LUA_TABLE;3196break;3197default:3198push_error(R"(Expected ":" or "=" after dictionary key.)");3199break;3200}3201decided_style = true;3202}32033204switch (dictionary->style) {3205case DictionaryNode::LUA_TABLE:3206if (key != nullptr && key->type != Node::IDENTIFIER && key->type != Node::LITERAL) {3207push_error(R"(Expected identifier or string as Lua-style dictionary key (e.g "{ key = value }").)");3208}3209if (key != nullptr && key->type == Node::LITERAL && static_cast<LiteralNode *>(key)->value.get_type() != Variant::STRING) {3210push_error(R"(Expected identifier or string as Lua-style dictionary key (e.g "{ key = value }").)");3211}3212if (!match(GDScriptTokenizer::Token::EQUAL)) {3213if (match(GDScriptTokenizer::Token::COLON)) {3214push_error(R"(Expected "=" after dictionary key. Mixing dictionary styles is not allowed.)");3215advance(); // Consume wrong separator anyway.3216} else {3217push_error(R"(Expected "=" after dictionary key.)");3218}3219}3220if (key != nullptr) {3221key->is_constant = true;3222if (key->type == Node::IDENTIFIER) {3223key->reduced_value = static_cast<IdentifierNode *>(key)->name;3224} else if (key->type == Node::LITERAL) {3225key->reduced_value = StringName(static_cast<LiteralNode *>(key)->value.operator String());3226}3227}3228break;3229case DictionaryNode::PYTHON_DICT:3230if (!match(GDScriptTokenizer::Token::COLON)) {3231if (match(GDScriptTokenizer::Token::EQUAL)) {3232push_error(R"(Expected ":" after dictionary key. Mixing dictionary styles is not allowed.)");3233advance(); // Consume wrong separator anyway.3234} else {3235push_error(R"(Expected ":" after dictionary key.)");3236}3237}3238break;3239}32403241// Value.3242ExpressionNode *value = parse_expression(false);3243if (value == nullptr) {3244push_error(R"(Expected expression as dictionary value.)");3245}32463247if (key != nullptr && value != nullptr) {3248dictionary->elements.push_back({ key, value });3249}32503251// Do phrase level recovery by inserting an imaginary expression for missing keys or values.3252// This ensures the successfully parsed expression is part of the AST and can be analyzed.3253if (key != nullptr && value == nullptr) {3254LiteralNode *dummy = alloc_recovery_node<LiteralNode>();3255dummy->value = Variant();32563257dictionary->elements.push_back({ key, dummy });3258} else if (key == nullptr && value != nullptr) {3259LiteralNode *dummy = alloc_recovery_node<LiteralNode>();3260dummy->value = Variant();32613262dictionary->elements.push_back({ dummy, value });3263}32643265} while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end());3266}3267pop_multiline();3268consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected closing "}" after dictionary elements.)");3269complete_extents(dictionary);32703271return dictionary;3272}32733274GDScriptParser::ExpressionNode *GDScriptParser::parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign) {3275ExpressionNode *grouped = parse_expression(false);3276pop_multiline();3277if (grouped == nullptr) {3278push_error(R"(Expected grouping expression.)");3279} else {3280consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after grouping expression.)*");3281}3282return grouped;3283}32843285GDScriptParser::ExpressionNode *GDScriptParser::parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign) {3286SubscriptNode *attribute = alloc_node<SubscriptNode>();3287reset_extents(attribute, p_previous_operand);3288update_extents(attribute);32893290if (for_completion) {3291bool is_builtin = false;3292if (p_previous_operand && p_previous_operand->type == Node::IDENTIFIER) {3293const IdentifierNode *id = static_cast<const IdentifierNode *>(p_previous_operand);3294Variant::Type builtin_type = get_builtin_type(id->name);3295if (builtin_type < Variant::VARIANT_MAX) {3296make_completion_context(COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD, builtin_type);3297is_builtin = true;3298}3299}3300if (!is_builtin) {3301make_completion_context(COMPLETION_ATTRIBUTE, attribute, -1);3302}3303}33043305attribute->base = p_previous_operand;33063307if (current.is_node_name()) {3308current.type = GDScriptTokenizer::Token::IDENTIFIER;3309}3310if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier after "." for attribute access.)")) {3311complete_extents(attribute);3312return attribute;3313}33143315attribute->is_attribute = true;3316attribute->attribute = parse_identifier();33173318complete_extents(attribute);3319return attribute;3320}33213322GDScriptParser::ExpressionNode *GDScriptParser::parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign) {3323SubscriptNode *subscript = alloc_node<SubscriptNode>();3324reset_extents(subscript, p_previous_operand);3325update_extents(subscript);33263327make_completion_context(COMPLETION_SUBSCRIPT, subscript);33283329subscript->base = p_previous_operand;3330subscript->index = parse_expression(false);33313332#ifdef TOOLS_ENABLED3333if (subscript->index != nullptr && subscript->index->type == Node::LITERAL) {3334override_completion_context(subscript->index, COMPLETION_SUBSCRIPT, subscript);3335}3336#endif33373338if (subscript->index == nullptr) {3339push_error(R"(Expected expression after "[".)");3340}33413342pop_multiline();3343consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected "]" after subscription index.)");3344complete_extents(subscript);33453346return subscript;3347}33483349GDScriptParser::ExpressionNode *GDScriptParser::parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign) {3350CastNode *cast = alloc_node<CastNode>();3351reset_extents(cast, p_previous_operand);3352update_extents(cast);33533354cast->operand = p_previous_operand;3355cast->cast_type = parse_type();3356complete_extents(cast);33573358if (cast->cast_type == nullptr) {3359push_error(R"(Expected type specifier after "as".)");3360return p_previous_operand;3361}33623363return cast;3364}33653366GDScriptParser::ExpressionNode *GDScriptParser::parse_call(ExpressionNode *p_previous_operand, bool p_can_assign) {3367CallNode *call = alloc_node<CallNode>();3368reset_extents(call, p_previous_operand);33693370if (previous.type == GDScriptTokenizer::Token::SUPER) {3371// Super call.3372call->is_super = true;3373if (!check(GDScriptTokenizer::Token::PERIOD)) {3374make_completion_context(COMPLETION_SUPER, call);3375}3376push_multiline(true);3377if (match(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) {3378// Implicit call to the parent method of the same name.3379if (current_function == nullptr) {3380push_error(R"(Cannot use implicit "super" call outside of a function.)");3381pop_multiline();3382complete_extents(call);3383return nullptr;3384}3385if (current_function->identifier) {3386call->function_name = current_function->identifier->name;3387} else {3388call->function_name = SNAME("<anonymous>");3389}3390} else {3391consume(GDScriptTokenizer::Token::PERIOD, R"(Expected "." or "(" after "super".)");3392make_completion_context(COMPLETION_SUPER_METHOD, call);3393if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected function name after ".".)")) {3394pop_multiline();3395complete_extents(call);3396return nullptr;3397}3398IdentifierNode *identifier = parse_identifier();3399call->callee = identifier;3400call->function_name = identifier->name;3401if (!consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after function name.)")) {3402pop_multiline();3403complete_extents(call);3404return nullptr;3405}3406}3407} else {3408call->callee = p_previous_operand;34093410if (call->callee == nullptr) {3411push_error(R"*(Cannot call on an expression. Use ".call()" if it's a Callable.)*");3412} else if (call->callee->type == Node::IDENTIFIER) {3413call->function_name = static_cast<IdentifierNode *>(call->callee)->name;3414make_completion_context(COMPLETION_METHOD, call->callee);3415} else if (call->callee->type == Node::SUBSCRIPT) {3416SubscriptNode *attribute = static_cast<SubscriptNode *>(call->callee);3417if (attribute->is_attribute) {3418if (attribute->attribute) {3419call->function_name = attribute->attribute->name;3420}3421make_completion_context(COMPLETION_ATTRIBUTE_METHOD, call->callee);3422} else {3423// TODO: The analyzer can see if this is actually a Callable and give better error message.3424push_error(R"*(Cannot call on an expression. Use ".call()" if it's a Callable.)*");3425}3426} else {3427push_error(R"*(Cannot call on an expression. Use ".call()" if it's a Callable.)*");3428}3429}34303431// Arguments.3432CompletionType ct = COMPLETION_CALL_ARGUMENTS;3433if (call->function_name == SNAME("load")) {3434ct = COMPLETION_RESOURCE_PATH;3435}3436push_completion_call(call);3437int argument_index = 0;3438do {3439make_completion_context(ct, call, argument_index);3440set_last_completion_call_arg(argument_index);3441if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) {3442// Allow for trailing comma.3443break;3444}3445ExpressionNode *argument = parse_expression(false);3446if (argument == nullptr) {3447push_error(R"(Expected expression as the function argument.)");3448} else {3449call->arguments.push_back(argument);34503451if (argument->type == Node::LITERAL) {3452override_completion_context(argument, ct, call, argument_index);3453}3454}34553456ct = COMPLETION_CALL_ARGUMENTS;3457argument_index++;3458} while (match(GDScriptTokenizer::Token::COMMA));3459pop_completion_call();34603461pop_multiline();3462consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after call arguments.)*");3463complete_extents(call);34643465return call;3466}34673468GDScriptParser::ExpressionNode *GDScriptParser::parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign) {3469// We want code completion after a DOLLAR even if the current code is invalid.3470make_completion_context(COMPLETION_GET_NODE, nullptr, -1);34713472if (!current.is_node_name() && !check(GDScriptTokenizer::Token::LITERAL) && !check(GDScriptTokenizer::Token::SLASH) && !check(GDScriptTokenizer::Token::PERCENT)) {3473push_error(vformat(R"(Expected node path as string or identifier after "%s".)", previous.get_name()));3474return nullptr;3475}34763477if (check(GDScriptTokenizer::Token::LITERAL)) {3478if (current.literal.get_type() != Variant::STRING) {3479push_error(vformat(R"(Expected node path as string or identifier after "%s".)", previous.get_name()));3480return nullptr;3481}3482}34833484GetNodeNode *get_node = alloc_node<GetNodeNode>();34853486// Store the last item in the path so the parser knows what to expect.3487// Allow allows more specific error messages.3488enum PathState {3489PATH_STATE_START,3490PATH_STATE_SLASH,3491PATH_STATE_PERCENT,3492PATH_STATE_NODE_NAME,3493} path_state = PATH_STATE_START;34943495if (previous.type == GDScriptTokenizer::Token::DOLLAR) {3496// Detect initial slash, which will be handled in the loop if it matches.3497match(GDScriptTokenizer::Token::SLASH);3498} else {3499get_node->use_dollar = false;3500}35013502int context_argument = 0;35033504do {3505if (previous.type == GDScriptTokenizer::Token::PERCENT) {3506if (path_state != PATH_STATE_START && path_state != PATH_STATE_SLASH) {3507push_error(R"("%" is only valid in the beginning of a node name (either after "$" or after "/"))");3508complete_extents(get_node);3509return nullptr;3510}35113512get_node->full_path += "%";35133514path_state = PATH_STATE_PERCENT;3515} else if (previous.type == GDScriptTokenizer::Token::SLASH) {3516if (path_state != PATH_STATE_START && path_state != PATH_STATE_NODE_NAME) {3517push_error(R"("/" is only valid at the beginning of the path or after a node name.)");3518complete_extents(get_node);3519return nullptr;3520}35213522get_node->full_path += "/";35233524path_state = PATH_STATE_SLASH;3525}35263527make_completion_context(COMPLETION_GET_NODE, get_node, context_argument++);35283529if (match(GDScriptTokenizer::Token::LITERAL)) {3530if (previous.literal.get_type() != Variant::STRING) {3531String previous_token;3532switch (path_state) {3533case PATH_STATE_START:3534previous_token = "$";3535break;3536case PATH_STATE_PERCENT:3537previous_token = "%";3538break;3539case PATH_STATE_SLASH:3540previous_token = "/";3541break;3542default:3543break;3544}3545push_error(vformat(R"(Expected node path as string or identifier after "%s".)", previous_token));3546complete_extents(get_node);3547return nullptr;3548}35493550get_node->full_path += previous.literal.operator String();35513552path_state = PATH_STATE_NODE_NAME;3553} else if (current.is_node_name()) {3554advance();35553556String identifier = previous.get_identifier();3557#ifdef DEBUG_ENABLED3558// Check spoofing.3559if (TS->has_feature(TextServer::FEATURE_UNICODE_SECURITY) && TS->spoof_check(identifier)) {3560push_warning(get_node, GDScriptWarning::CONFUSABLE_IDENTIFIER, identifier);3561}3562#endif3563get_node->full_path += identifier;35643565path_state = PATH_STATE_NODE_NAME;3566} else if (!check(GDScriptTokenizer::Token::SLASH) && !check(GDScriptTokenizer::Token::PERCENT)) {3567push_error(vformat(R"(Unexpected "%s" in node path.)", current.get_name()));3568complete_extents(get_node);3569return nullptr;3570}3571} while (match(GDScriptTokenizer::Token::SLASH) || match(GDScriptTokenizer::Token::PERCENT));35723573complete_extents(get_node);3574return get_node;3575}35763577GDScriptParser::ExpressionNode *GDScriptParser::parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign) {3578PreloadNode *preload = alloc_node<PreloadNode>();3579preload->resolved_path = "<missing path>";35803581push_multiline(true);3582consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after "preload".)");35833584make_completion_context(COMPLETION_RESOURCE_PATH, preload);3585push_completion_call(preload);35863587preload->path = parse_expression(false);35883589if (preload->path == nullptr) {3590push_error(R"(Expected resource path after "(".)");3591} else if (preload->path->type == Node::LITERAL) {3592override_completion_context(preload->path, COMPLETION_RESOURCE_PATH, preload);3593}35943595pop_completion_call();35963597// Allow trailing comma.3598match(GDScriptTokenizer::Token::COMMA);35993600pop_multiline();3601consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after preload path.)*");3602complete_extents(preload);36033604return preload;3605}36063607GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign) {3608LambdaNode *lambda = alloc_node<LambdaNode>();3609lambda->parent_function = current_function;3610lambda->parent_lambda = current_lambda;36113612FunctionNode *function = alloc_node<FunctionNode>();3613function->source_lambda = lambda;36143615function->is_static = current_function != nullptr ? current_function->is_static : false;36163617if (match(GDScriptTokenizer::Token::IDENTIFIER)) {3618function->identifier = parse_identifier();3619}36203621bool multiline_context = multiline_stack.back()->get();36223623push_completion_call(nullptr);36243625// Reset the multiline stack since we don't want the multiline mode one in the lambda body.3626push_multiline(false);3627if (multiline_context) {3628tokenizer->push_expression_indented_block();3629}36303631push_multiline(true); // For the parameters.3632if (function->identifier) {3633consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after lambda name.)");3634} else {3635consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after "func".)");3636}36373638FunctionNode *previous_function = current_function;3639current_function = function;36403641LambdaNode *previous_lambda = current_lambda;3642current_lambda = lambda;36433644SuiteNode *body = alloc_node<SuiteNode>();3645body->parent_function = current_function;3646body->parent_block = current_suite;36473648SuiteNode *previous_suite = current_suite;3649current_suite = body;36503651parse_function_signature(function, body, "lambda", -1);36523653current_suite = previous_suite;36543655bool previous_in_lambda = in_lambda;3656in_lambda = true;36573658// Save break/continue state.3659bool could_break = can_break;3660bool could_continue = can_continue;36613662// Disallow break/continue.3663can_break = false;3664can_continue = false;36653666function->body = parse_suite("lambda declaration", body, true);3667complete_extents(function);3668complete_extents(lambda);36693670pop_multiline();36713672pop_completion_call();36733674if (multiline_context) {3675// If we're in multiline mode, we want to skip the spurious DEDENT and NEWLINE tokens.3676while (check(GDScriptTokenizer::Token::DEDENT) || check(GDScriptTokenizer::Token::INDENT) || check(GDScriptTokenizer::Token::NEWLINE)) {3677current = tokenizer->scan(); // Not advance() since we don't want to change the previous token.3678}3679tokenizer->pop_expression_indented_block();3680}36813682current_function = previous_function;3683current_lambda = previous_lambda;3684in_lambda = previous_in_lambda;3685lambda->function = function;36863687// Reset break/continue state.3688can_break = could_break;3689can_continue = could_continue;36903691return lambda;3692}36933694GDScriptParser::ExpressionNode *GDScriptParser::parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign) {3695// x is not int3696// ^ ^^^ ExpressionNode, TypeNode3697// ^^^^^^^^^^^^ TypeTestNode3698// ^^^^^^^^^^^^ UnaryOpNode3699UnaryOpNode *not_node = nullptr;3700if (match(GDScriptTokenizer::Token::NOT)) {3701not_node = alloc_node<UnaryOpNode>();3702not_node->operation = UnaryOpNode::OP_LOGIC_NOT;3703not_node->variant_op = Variant::OP_NOT;3704reset_extents(not_node, p_previous_operand);3705update_extents(not_node);3706}37073708TypeTestNode *type_test = alloc_node<TypeTestNode>();3709reset_extents(type_test, p_previous_operand);3710update_extents(type_test);37113712type_test->operand = p_previous_operand;3713type_test->test_type = parse_type();3714complete_extents(type_test);37153716if (not_node != nullptr) {3717not_node->operand = type_test;3718complete_extents(not_node);3719}37203721if (type_test->test_type == nullptr) {3722if (not_node == nullptr) {3723push_error(R"(Expected type specifier after "is".)");3724} else {3725push_error(R"(Expected type specifier after "is not".)");3726}3727}37283729if (not_node != nullptr) {3730return not_node;3731}37323733return type_test;3734}37353736GDScriptParser::ExpressionNode *GDScriptParser::parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign) {3737push_error(R"("yield" was removed in Godot 4. Use "await" instead.)");3738return nullptr;3739}37403741GDScriptParser::ExpressionNode *GDScriptParser::parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign) {3742// Just for better error messages.3743GDScriptTokenizer::Token::Type invalid = previous.type;37443745switch (invalid) {3746case GDScriptTokenizer::Token::QUESTION_MARK:3747push_error(R"(Unexpected "?" in source. If you want a ternary operator, use "truthy_value if true_condition else falsy_value".)");3748break;3749default:3750return nullptr; // Unreachable.3751}37523753// Return the previous expression.3754return p_previous_operand;3755}37563757GDScriptParser::TypeNode *GDScriptParser::parse_type(bool p_allow_void) {3758TypeNode *type = alloc_node<TypeNode>();3759make_completion_context(p_allow_void ? COMPLETION_TYPE_NAME_OR_VOID : COMPLETION_TYPE_NAME, type);3760if (!match(GDScriptTokenizer::Token::IDENTIFIER)) {3761if (match(GDScriptTokenizer::Token::TK_VOID)) {3762if (p_allow_void) {3763complete_extents(type);3764TypeNode *void_type = type;3765return void_type;3766} else {3767push_error(R"("void" is only allowed for a function return type.)");3768}3769}3770// Leave error message to the caller who knows the context.3771complete_extents(type);3772return nullptr;3773}37743775IdentifierNode *type_element = parse_identifier();37763777type->type_chain.push_back(type_element);37783779if (match(GDScriptTokenizer::Token::BRACKET_OPEN)) {3780// Typed collection (like Array[int], Dictionary[String, int]).3781bool first_pass = true;3782do {3783TypeNode *container_type = parse_type(false); // Don't allow void for element type.3784if (container_type == nullptr) {3785push_error(vformat(R"(Expected type for collection after "%s".)", first_pass ? "[" : ","));3786complete_extents(type);3787type = nullptr;3788break;3789} else if (container_type->container_types.size() > 0) {3790push_error("Nested typed collections are not supported.");3791} else {3792type->container_types.append(container_type);3793}3794first_pass = false;3795} while (match(GDScriptTokenizer::Token::COMMA));3796consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected closing "]" after collection type.)");3797if (type != nullptr) {3798complete_extents(type);3799}3800return type;3801}38023803int chain_index = 1;3804while (match(GDScriptTokenizer::Token::PERIOD)) {3805make_completion_context(COMPLETION_TYPE_ATTRIBUTE, type, chain_index++);3806if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected inner type name after ".".)")) {3807type_element = parse_identifier();3808type->type_chain.push_back(type_element);3809}3810}38113812complete_extents(type);3813return type;3814}38153816#ifdef TOOLS_ENABLED3817enum DocLineState {3818DOC_LINE_NORMAL,3819DOC_LINE_IN_CODE,3820DOC_LINE_IN_CODEBLOCK,3821DOC_LINE_IN_KBD,3822};38233824static String _process_doc_line(const String &p_line, const String &p_text, const String &p_space_prefix, DocLineState &r_state) {3825String line = p_line;3826if (r_state == DOC_LINE_NORMAL) {3827line = line.strip_edges(true, false);3828} else {3829line = line.trim_prefix(p_space_prefix);3830}38313832String line_join;3833if (!p_text.is_empty()) {3834if (r_state == DOC_LINE_NORMAL) {3835if (p_text.ends_with("[/codeblock]")) {3836line_join = "\n";3837} else if (!p_text.ends_with("[br]")) {3838line_join = " ";3839}3840} else {3841line_join = "\n";3842}3843}38443845String result;3846int from = 0;3847int buffer_start = 0;3848const int len = line.length();3849bool process = true;3850while (process) {3851switch (r_state) {3852case DOC_LINE_NORMAL: {3853int lb_pos = line.find_char('[', from);3854if (lb_pos < 0) {3855process = false;3856break;3857}3858int rb_pos = line.find_char(']', lb_pos + 1);3859if (rb_pos < 0) {3860process = false;3861break;3862}38633864from = rb_pos + 1;38653866String tag = line.substr(lb_pos + 1, rb_pos - lb_pos - 1);3867if (tag == "code" || tag.begins_with("code ")) {3868r_state = DOC_LINE_IN_CODE;3869} else if (tag == "codeblock" || tag.begins_with("codeblock ")) {3870if (lb_pos == 0) {3871line_join = "\n";3872} else {3873result += line.substr(buffer_start, lb_pos - buffer_start) + '\n';3874}3875result += "[" + tag + "]";3876if (from < len) {3877result += '\n';3878}38793880r_state = DOC_LINE_IN_CODEBLOCK;3881buffer_start = from;3882} else if (tag == "kbd") {3883r_state = DOC_LINE_IN_KBD;3884}3885} break;3886case DOC_LINE_IN_CODE: {3887int pos = line.find("[/code]", from);3888if (pos < 0) {3889process = false;3890break;3891}38923893from = pos + 7; // `len("[/code]")`.38943895r_state = DOC_LINE_NORMAL;3896} break;3897case DOC_LINE_IN_CODEBLOCK: {3898int pos = line.find("[/codeblock]", from);3899if (pos < 0) {3900process = false;3901break;3902}39033904from = pos + 12; // `len("[/codeblock]")`.39053906if (pos == 0) {3907line_join = "\n";3908} else {3909result += line.substr(buffer_start, pos - buffer_start) + '\n';3910}3911result += "[/codeblock]";3912if (from < len) {3913result += '\n';3914}39153916r_state = DOC_LINE_NORMAL;3917buffer_start = from;3918} break;3919case DOC_LINE_IN_KBD: {3920int pos = line.find("[/kbd]", from);3921if (pos < 0) {3922process = false;3923break;3924}39253926from = pos + 6; // `len("[/kbd]")`.39273928r_state = DOC_LINE_NORMAL;3929} break;3930}3931}39323933result += line.substr(buffer_start);3934if (r_state == DOC_LINE_NORMAL) {3935result = result.strip_edges(false, true);3936}39373938return line_join + result;3939}39403941bool GDScriptParser::has_comment(int p_line, bool p_must_be_doc) {3942bool has_comment = tokenizer->get_comments().has(p_line);3943// If there are no comments or if we don't care whether the comment3944// is a docstring, we have our result.3945if (!p_must_be_doc || !has_comment) {3946return has_comment;3947}39483949return tokenizer->get_comments()[p_line].comment.begins_with("##");3950}39513952GDScriptParser::MemberDocData GDScriptParser::parse_doc_comment(int p_line, bool p_single_line) {3953ERR_FAIL_COND_V(!has_comment(p_line, true), MemberDocData());39543955const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments();3956int line = p_line;39573958if (!p_single_line) {3959while (comments.has(line - 1) && comments[line - 1].new_line && comments[line - 1].comment.begins_with("##")) {3960line--;3961}3962}39633964max_script_doc_line = MIN(max_script_doc_line, line - 1);39653966String space_prefix;3967{3968int i = 2;3969for (; i < comments[line].comment.length(); i++) {3970if (comments[line].comment[i] != ' ') {3971break;3972}3973}3974space_prefix = String(" ").repeat(i - 2);3975}39763977DocLineState state = DOC_LINE_NORMAL;3978MemberDocData result;39793980while (line <= p_line) {3981String doc_line = comments[line].comment.trim_prefix("##");3982line++;39833984if (state == DOC_LINE_NORMAL) {3985String stripped_line = doc_line.strip_edges();3986if (stripped_line == "@deprecated" || stripped_line.begins_with("@deprecated:")) {3987result.is_deprecated = true;3988if (stripped_line.begins_with("@deprecated:")) {3989result.deprecated_message = stripped_line.trim_prefix("@deprecated:").strip_edges();3990}3991continue;3992} else if (stripped_line == "@experimental" || stripped_line.begins_with("@experimental:")) {3993result.is_experimental = true;3994if (stripped_line.begins_with("@experimental:")) {3995result.experimental_message = stripped_line.trim_prefix("@experimental:").strip_edges();3996}3997continue;3998}3999}40004001result.description += _process_doc_line(doc_line, result.description, space_prefix, state);4002}40034004return result;4005}40064007GDScriptParser::ClassDocData GDScriptParser::parse_class_doc_comment(int p_line, bool p_single_line) {4008ERR_FAIL_COND_V(!has_comment(p_line, true), ClassDocData());40094010const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments();4011int line = p_line;40124013if (!p_single_line) {4014while (comments.has(line - 1) && comments[line - 1].new_line && comments[line - 1].comment.begins_with("##")) {4015line--;4016}4017}40184019max_script_doc_line = MIN(max_script_doc_line, line - 1);40204021String space_prefix;4022{4023int i = 2;4024for (; i < comments[line].comment.length(); i++) {4025if (comments[line].comment[i] != ' ') {4026break;4027}4028}4029space_prefix = String(" ").repeat(i - 2);4030}40314032DocLineState state = DOC_LINE_NORMAL;4033bool is_in_brief = true;4034ClassDocData result;40354036while (line <= p_line) {4037String doc_line = comments[line].comment.trim_prefix("##");4038line++;40394040if (state == DOC_LINE_NORMAL) {4041String stripped_line = doc_line.strip_edges();40424043// A blank line separates the description from the brief.4044if (is_in_brief && !result.brief.is_empty() && stripped_line.is_empty()) {4045is_in_brief = false;4046continue;4047}40484049if (stripped_line.begins_with("@tutorial")) {4050String title, link;40514052int begin_scan = String("@tutorial").length();4053if (begin_scan >= stripped_line.length()) {4054continue; // Invalid syntax.4055}40564057if (stripped_line[begin_scan] == ':') { // No title.4058// Syntax: ## @tutorial: https://godotengine.org/ // The title argument is optional.4059title = "";4060link = stripped_line.trim_prefix("@tutorial:").strip_edges();4061} else {4062/* Syntax:4063* @tutorial ( The Title Here ) : https://the.url/4064* ^ open ^ close ^ colon ^ url4065*/4066int open_bracket_pos = begin_scan, close_bracket_pos = 0;4067while (open_bracket_pos < stripped_line.length() && (stripped_line[open_bracket_pos] == ' ' || stripped_line[open_bracket_pos] == '\t')) {4068open_bracket_pos++;4069}4070if (open_bracket_pos == stripped_line.length() || stripped_line[open_bracket_pos++] != '(') {4071continue; // Invalid syntax.4072}4073close_bracket_pos = open_bracket_pos;4074while (close_bracket_pos < stripped_line.length() && stripped_line[close_bracket_pos] != ')') {4075close_bracket_pos++;4076}4077if (close_bracket_pos == stripped_line.length()) {4078continue; // Invalid syntax.4079}40804081int colon_pos = close_bracket_pos + 1;4082while (colon_pos < stripped_line.length() && (stripped_line[colon_pos] == ' ' || stripped_line[colon_pos] == '\t')) {4083colon_pos++;4084}4085if (colon_pos == stripped_line.length() || stripped_line[colon_pos++] != ':') {4086continue; // Invalid syntax.4087}40884089title = stripped_line.substr(open_bracket_pos, close_bracket_pos - open_bracket_pos).strip_edges();4090link = stripped_line.substr(colon_pos).strip_edges();4091}40924093result.tutorials.append(Pair<String, String>(title, link));4094continue;4095} else if (stripped_line == "@deprecated" || stripped_line.begins_with("@deprecated:")) {4096result.is_deprecated = true;4097if (stripped_line.begins_with("@deprecated:")) {4098result.deprecated_message = stripped_line.trim_prefix("@deprecated:").strip_edges();4099}4100continue;4101} else if (stripped_line == "@experimental" || stripped_line.begins_with("@experimental:")) {4102result.is_experimental = true;4103if (stripped_line.begins_with("@experimental:")) {4104result.experimental_message = stripped_line.trim_prefix("@experimental:").strip_edges();4105}4106continue;4107}4108}41094110if (is_in_brief) {4111result.brief += _process_doc_line(doc_line, result.brief, space_prefix, state);4112} else {4113result.description += _process_doc_line(doc_line, result.description, space_prefix, state);4114}4115}41164117return result;4118}4119#endif // TOOLS_ENABLED41204121GDScriptParser::ParseRule *GDScriptParser::get_rule(GDScriptTokenizer::Token::Type p_token_type) {4122// Function table for expression parsing.4123// clang-format destroys the alignment here, so turn off for the table.4124/* clang-format off */4125static ParseRule rules[] = {4126// PREFIX INFIX PRECEDENCE (for infix)4127{ nullptr, nullptr, PREC_NONE }, // EMPTY,4128// Basic4129{ nullptr, nullptr, PREC_NONE }, // ANNOTATION,4130{ &GDScriptParser::parse_identifier, nullptr, PREC_NONE }, // IDENTIFIER,4131{ &GDScriptParser::parse_literal, nullptr, PREC_NONE }, // LITERAL,4132// Comparison4133{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // LESS,4134{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // LESS_EQUAL,4135{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // GREATER,4136{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // GREATER_EQUAL,4137{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // EQUAL_EQUAL,4138{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // BANG_EQUAL,4139// Logical4140{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_AND }, // AND,4141{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_OR }, // OR,4142{ &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_not_in_operator, PREC_CONTENT_TEST }, // NOT,4143{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_AND }, // AMPERSAND_AMPERSAND,4144{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_OR }, // PIPE_PIPE,4145{ &GDScriptParser::parse_unary_operator, nullptr, PREC_NONE }, // BANG,4146// Bitwise4147{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_AND }, // AMPERSAND,4148{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_OR }, // PIPE,4149{ &GDScriptParser::parse_unary_operator, nullptr, PREC_NONE }, // TILDE,4150{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_XOR }, // CARET,4151{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_SHIFT }, // LESS_LESS,4152{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_SHIFT }, // GREATER_GREATER,4153// Math4154{ &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_operator, PREC_ADDITION_SUBTRACTION }, // PLUS,4155{ &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_operator, PREC_ADDITION_SUBTRACTION }, // MINUS,4156{ nullptr, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // STAR,4157{ nullptr, &GDScriptParser::parse_binary_operator, PREC_POWER }, // STAR_STAR,4158{ nullptr, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // SLASH,4159{ &GDScriptParser::parse_get_node, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // PERCENT,4160// Assignment4161{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // EQUAL,4162{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PLUS_EQUAL,4163{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // MINUS_EQUAL,4164{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // STAR_EQUAL,4165{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // STAR_STAR_EQUAL,4166{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // SLASH_EQUAL,4167{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PERCENT_EQUAL,4168{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // LESS_LESS_EQUAL,4169{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // GREATER_GREATER_EQUAL,4170{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // AMPERSAND_EQUAL,4171{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PIPE_EQUAL,4172{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // CARET_EQUAL,4173// Control flow4174{ nullptr, &GDScriptParser::parse_ternary_operator, PREC_TERNARY }, // IF,4175{ nullptr, nullptr, PREC_NONE }, // ELIF,4176{ nullptr, nullptr, PREC_NONE }, // ELSE,4177{ nullptr, nullptr, PREC_NONE }, // FOR,4178{ nullptr, nullptr, PREC_NONE }, // WHILE,4179{ nullptr, nullptr, PREC_NONE }, // BREAK,4180{ nullptr, nullptr, PREC_NONE }, // CONTINUE,4181{ nullptr, nullptr, PREC_NONE }, // PASS,4182{ nullptr, nullptr, PREC_NONE }, // RETURN,4183{ nullptr, nullptr, PREC_NONE }, // MATCH,4184{ nullptr, nullptr, PREC_NONE }, // WHEN,4185// Keywords4186{ nullptr, &GDScriptParser::parse_cast, PREC_CAST }, // AS,4187{ nullptr, nullptr, PREC_NONE }, // ASSERT,4188{ &GDScriptParser::parse_await, nullptr, PREC_NONE }, // AWAIT,4189{ nullptr, nullptr, PREC_NONE }, // BREAKPOINT,4190{ nullptr, nullptr, PREC_NONE }, // CLASS,4191{ nullptr, nullptr, PREC_NONE }, // CLASS_NAME,4192{ nullptr, nullptr, PREC_NONE }, // TK_CONST,4193{ nullptr, nullptr, PREC_NONE }, // ENUM,4194{ nullptr, nullptr, PREC_NONE }, // EXTENDS,4195{ &GDScriptParser::parse_lambda, nullptr, PREC_NONE }, // FUNC,4196{ nullptr, &GDScriptParser::parse_binary_operator, PREC_CONTENT_TEST }, // TK_IN,4197{ nullptr, &GDScriptParser::parse_type_test, PREC_TYPE_TEST }, // IS,4198{ nullptr, nullptr, PREC_NONE }, // NAMESPACE,4199{ &GDScriptParser::parse_preload, nullptr, PREC_NONE }, // PRELOAD,4200{ &GDScriptParser::parse_self, nullptr, PREC_NONE }, // SELF,4201{ nullptr, nullptr, PREC_NONE }, // SIGNAL,4202{ nullptr, nullptr, PREC_NONE }, // STATIC,4203{ &GDScriptParser::parse_call, nullptr, PREC_NONE }, // SUPER,4204{ nullptr, nullptr, PREC_NONE }, // TRAIT,4205{ nullptr, nullptr, PREC_NONE }, // VAR,4206{ nullptr, nullptr, PREC_NONE }, // TK_VOID,4207{ &GDScriptParser::parse_yield, nullptr, PREC_NONE }, // YIELD,4208// Punctuation4209{ &GDScriptParser::parse_array, &GDScriptParser::parse_subscript, PREC_SUBSCRIPT }, // BRACKET_OPEN,4210{ nullptr, nullptr, PREC_NONE }, // BRACKET_CLOSE,4211{ &GDScriptParser::parse_dictionary, nullptr, PREC_NONE }, // BRACE_OPEN,4212{ nullptr, nullptr, PREC_NONE }, // BRACE_CLOSE,4213{ &GDScriptParser::parse_grouping, &GDScriptParser::parse_call, PREC_CALL }, // PARENTHESIS_OPEN,4214{ nullptr, nullptr, PREC_NONE }, // PARENTHESIS_CLOSE,4215{ nullptr, nullptr, PREC_NONE }, // COMMA,4216{ nullptr, nullptr, PREC_NONE }, // SEMICOLON,4217{ nullptr, &GDScriptParser::parse_attribute, PREC_ATTRIBUTE }, // PERIOD,4218{ nullptr, nullptr, PREC_NONE }, // PERIOD_PERIOD,4219{ nullptr, nullptr, PREC_NONE }, // PERIOD_PERIOD_PERIOD,4220{ nullptr, nullptr, PREC_NONE }, // COLON,4221{ &GDScriptParser::parse_get_node, nullptr, PREC_NONE }, // DOLLAR,4222{ nullptr, nullptr, PREC_NONE }, // FORWARD_ARROW,4223{ nullptr, nullptr, PREC_NONE }, // UNDERSCORE,4224// Whitespace4225{ nullptr, nullptr, PREC_NONE }, // NEWLINE,4226{ nullptr, nullptr, PREC_NONE }, // INDENT,4227{ nullptr, nullptr, PREC_NONE }, // DEDENT,4228// Constants4229{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_PI,4230{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_TAU,4231{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_INF,4232{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_NAN,4233// Error message improvement4234{ nullptr, nullptr, PREC_NONE }, // VCS_CONFLICT_MARKER,4235{ nullptr, nullptr, PREC_NONE }, // BACKTICK,4236{ nullptr, &GDScriptParser::parse_invalid_token, PREC_CAST }, // QUESTION_MARK,4237// Special4238{ nullptr, nullptr, PREC_NONE }, // ERROR,4239{ nullptr, nullptr, PREC_NONE }, // TK_EOF,4240};4241/* clang-format on */4242// Avoid desync.4243static_assert(std_size(rules) == GDScriptTokenizer::Token::TK_MAX, "Amount of parse rules don't match the amount of token types.");42444245// Let's assume this is never invalid, since nothing generates a TK_MAX.4246return &rules[p_token_type];4247}42484249bool GDScriptParser::SuiteNode::has_local(const StringName &p_name) const {4250if (locals_indices.has(p_name)) {4251return true;4252}4253if (parent_block != nullptr) {4254return parent_block->has_local(p_name);4255}4256return false;4257}42584259const GDScriptParser::SuiteNode::Local &GDScriptParser::SuiteNode::get_local(const StringName &p_name) const {4260if (locals_indices.has(p_name)) {4261return locals[locals_indices[p_name]];4262}4263if (parent_block != nullptr) {4264return parent_block->get_local(p_name);4265}4266return empty;4267}42684269bool GDScriptParser::AnnotationNode::apply(GDScriptParser *p_this, Node *p_target, ClassNode *p_class) {4270if (is_applied) {4271return true;4272}4273is_applied = true;4274return (p_this->*(p_this->valid_annotations[name].apply))(this, p_target, p_class);4275}42764277bool GDScriptParser::AnnotationNode::applies_to(uint32_t p_target_kinds) const {4278return (info->target_kind & p_target_kinds) > 0;4279}42804281bool GDScriptParser::validate_annotation_arguments(AnnotationNode *p_annotation) {4282ERR_FAIL_COND_V_MSG(!valid_annotations.has(p_annotation->name), false, vformat(R"(Annotation "%s" not found to validate.)", p_annotation->name));42834284const MethodInfo &info = valid_annotations[p_annotation->name].info;42854286if (((info.flags & METHOD_FLAG_VARARG) == 0) && p_annotation->arguments.size() > info.arguments.size()) {4287push_error(vformat(R"(Annotation "%s" requires at most %d arguments, but %d were given.)", p_annotation->name, info.arguments.size(), p_annotation->arguments.size()));4288return false;4289}42904291if (p_annotation->arguments.size() < info.arguments.size() - info.default_arguments.size()) {4292push_error(vformat(R"(Annotation "%s" requires at least %d arguments, but %d were given.)", p_annotation->name, info.arguments.size() - info.default_arguments.size(), p_annotation->arguments.size()));4293return false;4294}42954296// Some annotations need to be resolved and applied in the parser.4297if (p_annotation->name == SNAME("@icon") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) {4298for (int i = 0; i < p_annotation->arguments.size(); i++) {4299ExpressionNode *argument = p_annotation->arguments[i];43004301if (argument->type != Node::LITERAL) {4302push_error(vformat(R"(Argument %d of annotation "%s" must be a string literal.)", i + 1, p_annotation->name), argument);4303return false;4304}43054306Variant value = static_cast<LiteralNode *>(argument)->value;43074308if (value.get_type() != Variant::STRING) {4309push_error(vformat(R"(Argument %d of annotation "%s" must be a string literal.)", i + 1, p_annotation->name), argument);4310return false;4311}43124313p_annotation->resolved_arguments.push_back(value);4314}4315}43164317// For other annotations, see `GDScriptAnalyzer::resolve_annotation()`.43184319return true;4320}43214322bool GDScriptParser::tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4323#ifdef DEBUG_ENABLED4324if (_is_tool) {4325push_error(R"("@tool" annotation can only be used once.)", p_annotation);4326return false;4327}4328#endif // DEBUG_ENABLED4329_is_tool = true;4330return true;4331}43324333bool GDScriptParser::icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4334ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, R"("@icon" annotation can only be applied to classes.)");4335ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);43364337ClassNode *class_node = static_cast<ClassNode *>(p_target);4338String path = p_annotation->resolved_arguments[0];43394340#ifdef DEBUG_ENABLED4341if (!class_node->icon_path.is_empty()) {4342push_error(R"("@icon" annotation can only be used once.)", p_annotation);4343return false;4344}4345if (path.is_empty()) {4346push_error(R"("@icon" annotation argument must contain the path to the icon.)", p_annotation->arguments[0]);4347return false;4348}4349#endif // DEBUG_ENABLED43504351class_node->icon_path = path;43524353if (path.is_empty() || path.is_absolute_path()) {4354class_node->simplified_icon_path = path.simplify_path();4355} else if (path.is_relative_path()) {4356class_node->simplified_icon_path = script_path.get_base_dir().path_join(path).simplify_path();4357} else {4358class_node->simplified_icon_path = path;4359}43604361return true;4362}43634364bool GDScriptParser::static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4365ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, vformat(R"("%s" annotation can only be applied to classes.)", p_annotation->name));4366ClassNode *class_node = static_cast<ClassNode *>(p_target);4367if (class_node->annotated_static_unload) {4368push_error(vformat(R"("%s" annotation can only be used once per script.)", p_annotation->name), p_annotation);4369return false;4370}4371class_node->annotated_static_unload = true;4372return true;4373}43744375bool GDScriptParser::abstract_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4376// NOTE: Use `p_target`, **not** `p_class`, because when `p_target` is a class then `p_class` refers to the outer class.4377if (p_target->type == Node::CLASS) {4378ClassNode *class_node = static_cast<ClassNode *>(p_target);4379if (class_node->is_abstract) {4380push_error(R"("@abstract" annotation can only be used once per class.)", p_annotation);4381return false;4382}4383class_node->is_abstract = true;4384return true;4385}4386if (p_target->type == Node::FUNCTION) {4387FunctionNode *function_node = static_cast<FunctionNode *>(p_target);4388if (function_node->is_static) {4389push_error(R"("@abstract" annotation cannot be applied to static functions.)", p_annotation);4390return false;4391}4392if (function_node->is_abstract) {4393push_error(R"("@abstract" annotation can only be used once per function.)", p_annotation);4394return false;4395}4396function_node->is_abstract = true;4397return true;4398}4399ERR_FAIL_V_MSG(false, R"("@abstract" annotation can only be applied to classes and functions.)");4400}44014402bool GDScriptParser::onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4403ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, R"("@onready" annotation can only be applied to class variables.)");44044405if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) {4406push_error(R"("@onready" can only be used in classes that inherit "Node".)", p_annotation);4407return false;4408}44094410VariableNode *variable = static_cast<VariableNode *>(p_target);4411if (variable->is_static) {4412push_error(R"("@onready" annotation cannot be applied to a static variable.)", p_annotation);4413return false;4414}4415if (variable->onready) {4416push_error(R"("@onready" annotation can only be used once per variable.)", p_annotation);4417return false;4418}4419variable->onready = true;4420current_class->onready_used = true;4421return true;4422}44234424static String _get_annotation_error_string(const StringName &p_annotation_name, const Vector<Variant::Type> &p_expected_types, const GDScriptParser::DataType &p_provided_type) {4425Vector<String> types;4426for (int i = 0; i < p_expected_types.size(); i++) {4427const Variant::Type &type = p_expected_types[i];4428types.push_back(Variant::get_type_name(type));4429types.push_back("Array[" + Variant::get_type_name(type) + "]");4430switch (type) {4431case Variant::INT:4432types.push_back("PackedByteArray");4433types.push_back("PackedInt32Array");4434types.push_back("PackedInt64Array");4435break;4436case Variant::FLOAT:4437types.push_back("PackedFloat32Array");4438types.push_back("PackedFloat64Array");4439break;4440case Variant::STRING:4441types.push_back("PackedStringArray");4442break;4443case Variant::VECTOR2:4444types.push_back("PackedVector2Array");4445break;4446case Variant::VECTOR3:4447types.push_back("PackedVector3Array");4448break;4449case Variant::COLOR:4450types.push_back("PackedColorArray");4451break;4452case Variant::VECTOR4:4453types.push_back("PackedVector4Array");4454break;4455default:4456break;4457}4458}44594460String string;4461if (types.size() == 1) {4462string = types[0].quote();4463} else if (types.size() == 2) {4464string = types[0].quote() + " or " + types[1].quote();4465} else if (types.size() >= 3) {4466string = types[0].quote();4467for (int i = 1; i < types.size() - 1; i++) {4468string += ", " + types[i].quote();4469}4470string += ", or " + types[types.size() - 1].quote();4471}44724473return vformat(R"("%s" annotation requires a variable of type %s, but type "%s" was given instead.)", p_annotation_name, string, p_provided_type.to_string());4474}44754476static StringName _find_narrowest_native_or_global_class(const GDScriptParser::DataType &p_type) {4477switch (p_type.kind) {4478case GDScriptParser::DataType::NATIVE: {4479if (p_type.is_meta_type) {4480return Object::get_class_static(); // `GDScriptNativeClass` is not an exposed class.4481}4482return p_type.native_type;4483} break;4484case GDScriptParser::DataType::SCRIPT: {4485Ref<Script> script;4486if (p_type.script_type.is_valid()) {4487script = p_type.script_type;4488} else {4489script = ResourceLoader::load(p_type.script_path, SNAME("Script"));4490}44914492if (p_type.is_meta_type) {4493return script.is_valid() ? script->get_class_name() : Script::get_class_static();4494}4495if (script.is_null()) {4496return p_type.native_type;4497}4498if (script->get_global_name() != StringName()) {4499return script->get_global_name();4500}45014502Ref<Script> base_script = script->get_base_script();4503if (base_script.is_null()) {4504return script->get_instance_base_type();4505}45064507GDScriptParser::DataType base_type;4508base_type.kind = GDScriptParser::DataType::SCRIPT;4509base_type.builtin_type = Variant::OBJECT;4510base_type.native_type = base_script->get_instance_base_type();4511base_type.script_type = base_script;4512base_type.script_path = base_script->get_path();45134514return _find_narrowest_native_or_global_class(base_type);4515} break;4516case GDScriptParser::DataType::CLASS: {4517if (p_type.is_meta_type) {4518return GDScript::get_class_static();4519}4520if (p_type.class_type == nullptr) {4521return p_type.native_type;4522}4523if (p_type.class_type->get_global_name() != StringName()) {4524return p_type.class_type->get_global_name();4525}4526return _find_narrowest_native_or_global_class(p_type.class_type->base_type);4527} break;4528default: {4529ERR_FAIL_V(StringName());4530} break;4531}4532}45334534template <PropertyHint t_hint, Variant::Type t_type>4535bool GDScriptParser::export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4536ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));4537ERR_FAIL_NULL_V(p_class, false);45384539VariableNode *variable = static_cast<VariableNode *>(p_target);4540if (variable->is_static) {4541push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4542return false;4543}4544if (variable->exported) {4545push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4546return false;4547}45484549variable->exported = true;45504551variable->export_info.type = t_type;4552variable->export_info.hint = t_hint;45534554String hint_string;4555for (int i = 0; i < p_annotation->resolved_arguments.size(); i++) {4556String arg_string = String(p_annotation->resolved_arguments[i]);45574558if (p_annotation->name != SNAME("@export_placeholder")) {4559if (arg_string.is_empty()) {4560push_error(vformat(R"(Argument %d of annotation "%s" is empty.)", i + 1, p_annotation->name), p_annotation->arguments[i]);4561return false;4562}4563if (arg_string.contains_char(',')) {4564push_error(vformat(R"(Argument %d of annotation "%s" contains a comma. Use separate arguments instead.)", i + 1, p_annotation->name), p_annotation->arguments[i]);4565return false;4566}4567}45684569// WARNING: Do not merge with the previous `if` because there `!=`, not `==`!4570if (p_annotation->name == SNAME("@export_flags")) {4571const int64_t max_flags = 32;4572Vector<String> t = arg_string.split(":", true, 1);4573if (t[0].is_empty()) {4574push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Expected flag name.)", i + 1), p_annotation->arguments[i]);4575return false;4576}4577if (t.size() == 2) {4578if (t[1].is_empty()) {4579push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Expected flag value.)", i + 1), p_annotation->arguments[i]);4580return false;4581}4582if (!t[1].is_valid_int()) {4583push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": The flag value must be a valid integer.)", i + 1), p_annotation->arguments[i]);4584return false;4585}4586int64_t value = t[1].to_int();4587if (value < 1 || value >= (1LL << max_flags)) {4588push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": The flag value must be at least 1 and at most 2 ** %d - 1.)", i + 1, max_flags), p_annotation->arguments[i]);4589return false;4590}4591} else if (i >= max_flags) {4592push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Starting from argument %d, the flag value must be specified explicitly.)", i + 1, max_flags + 1), p_annotation->arguments[i]);4593return false;4594}4595} else if (p_annotation->name == SNAME("@export_node_path")) {4596String native_class = arg_string;4597if (ScriptServer::is_global_class(arg_string)) {4598native_class = ScriptServer::get_global_class_native_base(arg_string);4599}4600if (!ClassDB::class_exists(native_class)) {4601push_error(vformat(R"(Invalid argument %d of annotation "@export_node_path": The class "%s" was not found in the global scope.)", i + 1, arg_string), p_annotation->arguments[i]);4602return false;4603} else if (!ClassDB::is_parent_class(native_class, SNAME("Node"))) {4604push_error(vformat(R"(Invalid argument %d of annotation "@export_node_path": The class "%s" does not inherit "Node".)", i + 1, arg_string), p_annotation->arguments[i]);4605return false;4606}4607}46084609if (i > 0) {4610hint_string += ",";4611}4612hint_string += arg_string;4613}4614variable->export_info.hint_string = hint_string;46154616// This is called after the analyzer is done finding the type, so this should be set here.4617DataType export_type = variable->get_datatype();46184619// Use initializer type if specified type is `Variant`.4620if (export_type.is_variant() && variable->initializer != nullptr && variable->initializer->datatype.is_set()) {4621export_type = variable->initializer->get_datatype();4622export_type.type_source = DataType::INFERRED;4623}46244625const Variant::Type original_export_type_builtin = export_type.builtin_type;46264627// Process array and packed array annotations on the element type.4628bool is_array = false;4629if (export_type.builtin_type == Variant::ARRAY && export_type.has_container_element_type(0)) {4630is_array = true;4631export_type = export_type.get_container_element_type(0);4632} else if (export_type.is_typed_container_type()) {4633is_array = true;4634export_type = export_type.get_typed_container_type();4635export_type.type_source = variable->datatype.type_source;4636}46374638bool is_dict = false;4639if (export_type.builtin_type == Variant::DICTIONARY && export_type.has_container_element_types()) {4640is_dict = true;4641DataType inner_type = export_type.get_container_element_type_or_variant(1);4642export_type = export_type.get_container_element_type_or_variant(0);4643export_type.set_container_element_type(0, inner_type); // Store earlier extracted value within key to separately parse after.4644}46454646bool use_default_variable_type_check = true;46474648if (p_annotation->name == SNAME("@export_range")) {4649if (export_type.builtin_type == Variant::INT) {4650variable->export_info.type = Variant::INT;4651}4652} else if (p_annotation->name == SNAME("@export_multiline")) {4653use_default_variable_type_check = false;46544655if (export_type.builtin_type != Variant::STRING && export_type.builtin_type != Variant::DICTIONARY) {4656Vector<Variant::Type> expected_types = { Variant::STRING, Variant::DICTIONARY };4657push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation);4658return false;4659}46604661if (export_type.builtin_type == Variant::DICTIONARY) {4662variable->export_info.type = Variant::DICTIONARY;4663}4664} else if (p_annotation->name == SNAME("@export")) {4665use_default_variable_type_check = false;46664667if (variable->datatype_specifier == nullptr && variable->initializer == nullptr) {4668push_error(R"(Cannot use simple "@export" annotation with variable without type or initializer, since type can't be inferred.)", p_annotation);4669return false;4670}46714672if (export_type.has_no_type()) {4673push_error(R"(Cannot use simple "@export" annotation because the type of the initialized value can't be inferred.)", p_annotation);4674return false;4675}46764677switch (export_type.kind) {4678case GDScriptParser::DataType::BUILTIN:4679variable->export_info.type = export_type.builtin_type;4680variable->export_info.hint = PROPERTY_HINT_NONE;4681variable->export_info.hint_string = String();4682break;4683case GDScriptParser::DataType::NATIVE:4684case GDScriptParser::DataType::SCRIPT:4685case GDScriptParser::DataType::CLASS: {4686const StringName class_name = _find_narrowest_native_or_global_class(export_type);4687if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {4688variable->export_info.type = Variant::OBJECT;4689variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;4690variable->export_info.hint_string = class_name;4691} else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {4692variable->export_info.type = Variant::OBJECT;4693variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;4694variable->export_info.hint_string = class_name;4695} else {4696push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4697return false;4698}4699} break;4700case GDScriptParser::DataType::ENUM: {4701if (export_type.is_meta_type) {4702variable->export_info.type = Variant::DICTIONARY;4703} else {4704variable->export_info.type = Variant::INT;4705variable->export_info.hint = PROPERTY_HINT_ENUM;47064707String enum_hint_string;4708bool first = true;4709for (const KeyValue<StringName, int64_t> &E : export_type.enum_values) {4710if (!first) {4711enum_hint_string += ",";4712} else {4713first = false;4714}4715enum_hint_string += E.key.operator String().capitalize().xml_escape();4716enum_hint_string += ":";4717enum_hint_string += String::num_int64(E.value).xml_escape();4718}47194720variable->export_info.hint_string = enum_hint_string;4721variable->export_info.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;4722variable->export_info.class_name = String(export_type.native_type).replace("::", ".");4723}4724} break;4725case GDScriptParser::DataType::VARIANT: {4726if (export_type.is_variant()) {4727variable->export_info.type = Variant::NIL;4728variable->export_info.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;4729}4730} break;4731default:4732push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4733return false;4734}47354736if (variable->export_info.hint == PROPERTY_HINT_NODE_TYPE && !ClassDB::is_parent_class(p_class->base_type.native_type, SNAME("Node"))) {4737push_error(vformat(R"(Node export is only supported in Node-derived classes, but the current class inherits "%s".)", p_class->base_type.to_string()), p_annotation);4738return false;4739}47404741if (is_dict) {4742String key_prefix = itos(variable->export_info.type);4743if (variable->export_info.hint) {4744key_prefix += "/" + itos(variable->export_info.hint);4745}4746key_prefix += ":" + variable->export_info.hint_string;47474748// Now parse value.4749export_type = export_type.get_container_element_type(0);47504751if (export_type.is_variant() || export_type.has_no_type()) {4752export_type.kind = GDScriptParser::DataType::BUILTIN;4753}4754switch (export_type.kind) {4755case GDScriptParser::DataType::BUILTIN:4756variable->export_info.type = export_type.builtin_type;4757variable->export_info.hint = PROPERTY_HINT_NONE;4758variable->export_info.hint_string = String();4759break;4760case GDScriptParser::DataType::NATIVE:4761case GDScriptParser::DataType::SCRIPT:4762case GDScriptParser::DataType::CLASS: {4763const StringName class_name = _find_narrowest_native_or_global_class(export_type);4764if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {4765variable->export_info.type = Variant::OBJECT;4766variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;4767variable->export_info.hint_string = class_name;4768} else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {4769variable->export_info.type = Variant::OBJECT;4770variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;4771variable->export_info.hint_string = class_name;4772} else {4773push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4774return false;4775}4776} break;4777case GDScriptParser::DataType::ENUM: {4778if (export_type.is_meta_type) {4779variable->export_info.type = Variant::DICTIONARY;4780} else {4781variable->export_info.type = Variant::INT;4782variable->export_info.hint = PROPERTY_HINT_ENUM;47834784String enum_hint_string;4785bool first = true;4786for (const KeyValue<StringName, int64_t> &E : export_type.enum_values) {4787if (!first) {4788enum_hint_string += ",";4789} else {4790first = false;4791}4792enum_hint_string += E.key.operator String().capitalize().xml_escape();4793enum_hint_string += ":";4794enum_hint_string += String::num_int64(E.value).xml_escape();4795}47964797variable->export_info.hint_string = enum_hint_string;4798variable->export_info.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;4799variable->export_info.class_name = String(export_type.native_type).replace("::", ".");4800}4801} break;4802default:4803push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4804return false;4805}48064807if (variable->export_info.hint == PROPERTY_HINT_NODE_TYPE && !ClassDB::is_parent_class(p_class->base_type.native_type, SNAME("Node"))) {4808push_error(vformat(R"(Node export is only supported in Node-derived classes, but the current class inherits "%s".)", p_class->base_type.to_string()), p_annotation);4809return false;4810}48114812String value_prefix = itos(variable->export_info.type);4813if (variable->export_info.hint) {4814value_prefix += "/" + itos(variable->export_info.hint);4815}4816value_prefix += ":" + variable->export_info.hint_string;48174818variable->export_info.type = Variant::DICTIONARY;4819variable->export_info.hint = PROPERTY_HINT_TYPE_STRING;4820variable->export_info.hint_string = key_prefix + ";" + value_prefix;4821variable->export_info.usage = PROPERTY_USAGE_DEFAULT;4822variable->export_info.class_name = StringName();4823}4824} else if (p_annotation->name == SNAME("@export_enum")) {4825use_default_variable_type_check = false;48264827Variant::Type enum_type = Variant::INT;48284829if (export_type.kind == DataType::BUILTIN && export_type.builtin_type == Variant::STRING) {4830enum_type = Variant::STRING;4831}48324833variable->export_info.type = enum_type;48344835if (!export_type.is_variant() && (export_type.kind != DataType::BUILTIN || export_type.builtin_type != enum_type)) {4836Vector<Variant::Type> expected_types = { Variant::INT, Variant::STRING };4837push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation);4838return false;4839}4840}48414842if (use_default_variable_type_check) {4843// Validate variable type with export.4844if (!export_type.is_variant() && (export_type.kind != DataType::BUILTIN || export_type.builtin_type != t_type)) {4845// Allow float/int conversion.4846if ((t_type != Variant::FLOAT || export_type.builtin_type != Variant::INT) && (t_type != Variant::INT || export_type.builtin_type != Variant::FLOAT)) {4847Vector<Variant::Type> expected_types = { t_type };4848push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation);4849return false;4850}4851}4852}48534854if (is_array) {4855String hint_prefix = itos(variable->export_info.type);4856if (variable->export_info.hint) {4857hint_prefix += "/" + itos(variable->export_info.hint);4858}4859variable->export_info.type = original_export_type_builtin;4860variable->export_info.hint = PROPERTY_HINT_TYPE_STRING;4861variable->export_info.hint_string = hint_prefix + ":" + variable->export_info.hint_string;4862variable->export_info.usage = PROPERTY_USAGE_DEFAULT;4863variable->export_info.class_name = StringName();4864}48654866return true;4867}48684869// For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values,4870// or handle array exports in a special way, so they are implemented as separate methods.48714872bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4873ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));48744875VariableNode *variable = static_cast<VariableNode *>(p_target);4876if (variable->is_static) {4877push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4878return false;4879}4880if (variable->exported) {4881push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4882return false;4883}48844885variable->exported = true;48864887// Save the info because the compiler uses export info for overwriting member info.4888variable->export_info = variable->get_datatype().to_property_info(variable->identifier->name);4889variable->export_info.usage |= PROPERTY_USAGE_STORAGE;48904891return true;4892}48934894bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4895ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));4896ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)");48974898VariableNode *variable = static_cast<VariableNode *>(p_target);4899if (variable->is_static) {4900push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4901return false;4902}4903if (variable->exported) {4904push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4905return false;4906}49074908variable->exported = true;49094910DataType export_type = variable->get_datatype();49114912variable->export_info.type = export_type.builtin_type;4913variable->export_info.hint = static_cast<PropertyHint>(p_annotation->resolved_arguments[0].operator int64_t());4914variable->export_info.hint_string = p_annotation->resolved_arguments[1];49154916if (p_annotation->resolved_arguments.size() >= 3) {4917variable->export_info.usage = p_annotation->resolved_arguments[2].operator int64_t();4918}4919return true;4920}49214922bool GDScriptParser::export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4923#ifdef TOOLS_ENABLED4924ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));4925ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);49264927if (!is_tool()) {4928push_error(R"(Tool buttons can only be used in tool scripts (add "@tool" to the top of the script).)", p_annotation);4929return false;4930}49314932VariableNode *variable = static_cast<VariableNode *>(p_target);49334934if (variable->is_static) {4935push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4936return false;4937}4938if (variable->exported) {4939push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4940return false;4941}49424943const DataType variable_type = variable->get_datatype();4944if (!variable_type.is_variant() && variable_type.is_hard_type()) {4945if (variable_type.kind != DataType::BUILTIN || variable_type.builtin_type != Variant::CALLABLE) {4946push_error(vformat(R"("@export_tool_button" annotation requires a variable of type "Callable", but type "%s" was given instead.)", variable_type.to_string()), p_annotation);4947return false;4948}4949}49504951variable->exported = true;49524953// Build the hint string (format: `<text>[,<icon>]`).4954String hint_string = p_annotation->resolved_arguments[0].operator String(); // Button text.4955if (p_annotation->resolved_arguments.size() > 1) {4956hint_string += "," + p_annotation->resolved_arguments[1].operator String(); // Button icon.4957}49584959variable->export_info.type = Variant::CALLABLE;4960variable->export_info.hint = PROPERTY_HINT_TOOL_BUTTON;4961variable->export_info.hint_string = hint_string;4962variable->export_info.usage = PROPERTY_USAGE_EDITOR;4963#endif // TOOLS_ENABLED49644965return true; // Only available in editor.4966}49674968template <PropertyUsageFlags t_usage>4969bool GDScriptParser::export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4970ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);49714972p_annotation->export_info.name = p_annotation->resolved_arguments[0];49734974switch (t_usage) {4975case PROPERTY_USAGE_CATEGORY: {4976p_annotation->export_info.usage = t_usage;4977} break;49784979case PROPERTY_USAGE_GROUP: {4980p_annotation->export_info.usage = t_usage;4981if (p_annotation->resolved_arguments.size() == 2) {4982p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1];4983}4984} break;49854986case PROPERTY_USAGE_SUBGROUP: {4987p_annotation->export_info.usage = t_usage;4988if (p_annotation->resolved_arguments.size() == 2) {4989p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1];4990}4991} break;4992}49934994return true;4995}49964997bool GDScriptParser::warning_ignore_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4998#ifdef DEBUG_ENABLED4999if (is_ignoring_warnings) {5000return true; // We already ignore all warnings, let's optimize it.5001}50025003bool has_error = false;5004for (const Variant &warning_name : p_annotation->resolved_arguments) {5005GDScriptWarning::Code warning_code = GDScriptWarning::get_code_from_name(String(warning_name).to_upper());5006if (warning_code == GDScriptWarning::WARNING_MAX) {5007push_error(vformat(R"(Invalid warning name: "%s".)", warning_name), p_annotation);5008has_error = true;5009} else {5010int start_line = p_annotation->start_line;5011int end_line = p_target->end_line;50125013switch (p_target->type) {5014#define SIMPLE_CASE(m_type, m_class, m_property) \5015case m_type: { \5016m_class *node = static_cast<m_class *>(p_target); \5017if (node->m_property == nullptr) { \5018end_line = node->start_line; \5019} else { \5020end_line = node->m_property->end_line; \5021} \5022} break;50235024// Can contain properties (set/get).5025SIMPLE_CASE(Node::VARIABLE, VariableNode, initializer)50265027// Contain bodies.5028SIMPLE_CASE(Node::FOR, ForNode, list)5029SIMPLE_CASE(Node::IF, IfNode, condition)5030SIMPLE_CASE(Node::MATCH, MatchNode, test)5031SIMPLE_CASE(Node::WHILE, WhileNode, condition)5032#undef SIMPLE_CASE50335034case Node::CLASS: {5035end_line = p_target->start_line;5036for (const AnnotationNode *annotation : p_target->annotations) {5037start_line = MIN(start_line, annotation->start_line);5038end_line = MAX(end_line, annotation->end_line);5039}5040} break;50415042case Node::FUNCTION: {5043FunctionNode *function = static_cast<FunctionNode *>(p_target);5044end_line = function->start_line;5045for (int i = 0; i < function->parameters.size(); i++) {5046end_line = MAX(end_line, function->parameters[i]->end_line);5047if (function->parameters[i]->initializer != nullptr) {5048end_line = MAX(end_line, function->parameters[i]->initializer->end_line);5049}5050}5051} break;50525053case Node::MATCH_BRANCH: {5054MatchBranchNode *branch = static_cast<MatchBranchNode *>(p_target);5055end_line = branch->start_line;5056for (int i = 0; i < branch->patterns.size(); i++) {5057end_line = MAX(end_line, branch->patterns[i]->end_line);5058}5059} break;50605061default: {5062} break;5063}50645065end_line = MAX(start_line, end_line); // Prevent infinite loop.5066for (int line = start_line; line <= end_line; line++) {5067warning_ignored_lines[warning_code].insert(line);5068}5069}5070}5071return !has_error;5072#else // !DEBUG_ENABLED5073// Only available in debug builds.5074return true;5075#endif // DEBUG_ENABLED5076}50775078bool GDScriptParser::warning_ignore_region_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {5079#ifdef DEBUG_ENABLED5080bool has_error = false;5081const bool is_start = p_annotation->name == SNAME("@warning_ignore_start");5082for (const Variant &warning_name : p_annotation->resolved_arguments) {5083GDScriptWarning::Code warning_code = GDScriptWarning::get_code_from_name(String(warning_name).to_upper());5084if (warning_code == GDScriptWarning::WARNING_MAX) {5085push_error(vformat(R"(Invalid warning name: "%s".)", warning_name), p_annotation);5086has_error = true;5087continue;5088}5089if (is_start) {5090if (warning_ignore_start_lines[warning_code] != INT_MAX) {5091push_error(vformat(R"(Warning "%s" is already being ignored by "@warning_ignore_start" at line %d.)", String(warning_name).to_upper(), warning_ignore_start_lines[warning_code]), p_annotation);5092has_error = true;5093continue;5094}5095warning_ignore_start_lines[warning_code] = p_annotation->start_line;5096} else {5097if (warning_ignore_start_lines[warning_code] == INT_MAX) {5098push_error(vformat(R"(Warning "%s" is not being ignored by "@warning_ignore_start".)", String(warning_name).to_upper()), p_annotation);5099has_error = true;5100continue;5101}5102const int start_line = warning_ignore_start_lines[warning_code];5103const int end_line = MAX(start_line, p_annotation->start_line); // Prevent infinite loop.5104for (int i = start_line; i <= end_line; i++) {5105warning_ignored_lines[warning_code].insert(i);5106}5107warning_ignore_start_lines[warning_code] = INT_MAX;5108}5109}5110return !has_error;5111#else // !DEBUG_ENABLED5112// Only available in debug builds.5113return true;5114#endif // DEBUG_ENABLED5115}51165117bool GDScriptParser::rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {5118ERR_FAIL_COND_V_MSG(p_target->type != Node::FUNCTION, false, vformat(R"("%s" annotation can only be applied to functions.)", p_annotation->name));51195120FunctionNode *function = static_cast<FunctionNode *>(p_target);5121if (function->rpc_config.get_type() != Variant::NIL) {5122push_error(R"(RPC annotations can only be used once per function.)", p_annotation);5123return false;5124}51255126Dictionary rpc_config;5127rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;5128if (!p_annotation->resolved_arguments.is_empty()) {5129unsigned char locality_args = 0;5130unsigned char permission_args = 0;5131unsigned char transfer_mode_args = 0;51325133for (int i = 0; i < p_annotation->resolved_arguments.size(); i++) {5134if (i == 3) {5135rpc_config["channel"] = p_annotation->resolved_arguments[i].operator int();5136continue;5137}51385139String arg = p_annotation->resolved_arguments[i].operator String();5140if (arg == "call_local") {5141locality_args++;5142rpc_config["call_local"] = true;5143} else if (arg == "call_remote") {5144locality_args++;5145rpc_config["call_local"] = false;5146} else if (arg == "any_peer") {5147permission_args++;5148rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;5149} else if (arg == "authority") {5150permission_args++;5151rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;5152} else if (arg == "reliable") {5153transfer_mode_args++;5154rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;5155} else if (arg == "unreliable") {5156transfer_mode_args++;5157rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_UNRELIABLE;5158} else if (arg == "unreliable_ordered") {5159transfer_mode_args++;5160rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_UNRELIABLE_ORDERED;5161} else {5162push_error(R"(Invalid RPC argument. Must be one of: "call_local"/"call_remote" (local calls), "any_peer"/"authority" (permission), "reliable"/"unreliable"/"unreliable_ordered" (transfer mode).)", p_annotation);5163}5164}51655166if (locality_args > 1) {5167push_error(R"(Invalid RPC config. The locality ("call_local"/"call_remote") must be specified no more than once.)", p_annotation);5168} else if (permission_args > 1) {5169push_error(R"(Invalid RPC config. The permission ("any_peer"/"authority") must be specified no more than once.)", p_annotation);5170} else if (transfer_mode_args > 1) {5171push_error(R"(Invalid RPC config. The transfer mode ("reliable"/"unreliable"/"unreliable_ordered") must be specified no more than once.)", p_annotation);5172}5173}5174function->rpc_config = rpc_config;5175return true;5176}51775178GDScriptParser::DataType GDScriptParser::SuiteNode::Local::get_datatype() const {5179switch (type) {5180case CONSTANT:5181return constant->get_datatype();5182case VARIABLE:5183return variable->get_datatype();5184case PARAMETER:5185return parameter->get_datatype();5186case FOR_VARIABLE:5187case PATTERN_BIND:5188return bind->get_datatype();5189case UNDEFINED:5190return DataType();5191}5192return DataType();5193}51945195String GDScriptParser::SuiteNode::Local::get_name() const {5196switch (type) {5197case SuiteNode::Local::PARAMETER:5198return "parameter";5199case SuiteNode::Local::CONSTANT:5200return "constant";5201case SuiteNode::Local::VARIABLE:5202return "variable";5203case SuiteNode::Local::FOR_VARIABLE:5204return "for loop iterator";5205case SuiteNode::Local::PATTERN_BIND:5206return "pattern bind";5207case SuiteNode::Local::UNDEFINED:5208return "<undefined>";5209default:5210return String();5211}5212}52135214String GDScriptParser::DataType::to_string() const {5215switch (kind) {5216case VARIANT:5217return "Variant";5218case BUILTIN:5219if (builtin_type == Variant::NIL) {5220return "null";5221}5222if (builtin_type == Variant::ARRAY && has_container_element_type(0)) {5223return vformat("Array[%s]", get_container_element_type(0).to_string());5224}5225if (builtin_type == Variant::DICTIONARY && has_container_element_types()) {5226return vformat("Dictionary[%s, %s]", get_container_element_type_or_variant(0).to_string(), get_container_element_type_or_variant(1).to_string());5227}5228return Variant::get_type_name(builtin_type);5229case NATIVE:5230if (is_meta_type) {5231return GDScriptNativeClass::get_class_static();5232}5233return native_type.operator String();5234case CLASS:5235if (class_type->identifier != nullptr) {5236return class_type->identifier->name.operator String();5237}5238return class_type->fqcn;5239case SCRIPT: {5240if (is_meta_type) {5241return script_type.is_valid() ? script_type->get_class_name().operator String() : "";5242}5243String name = script_type.is_valid() ? script_type->get_name() : "";5244if (!name.is_empty()) {5245return name;5246}5247name = script_path;5248if (!name.is_empty()) {5249return name;5250}5251return native_type.operator String();5252}5253case ENUM: {5254// native_type contains either the native class defining the enum5255// or the fully qualified class name of the script defining the enum5256return String(native_type).get_file(); // Remove path, keep filename5257}5258case RESOLVING:5259case UNRESOLVED:5260return "<unresolved type>";5261}52625263ERR_FAIL_V_MSG("<unresolved type>", "Kind set outside the enum range.");5264}52655266PropertyInfo GDScriptParser::DataType::to_property_info(const String &p_name) const {5267PropertyInfo result;5268result.name = p_name;5269result.usage = PROPERTY_USAGE_NONE;52705271if (!is_hard_type()) {5272result.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;5273return result;5274}52755276switch (kind) {5277case BUILTIN:5278result.type = builtin_type;5279if (builtin_type == Variant::ARRAY && has_container_element_type(0)) {5280const DataType elem_type = get_container_element_type(0);5281switch (elem_type.kind) {5282case BUILTIN:5283result.hint = PROPERTY_HINT_ARRAY_TYPE;5284result.hint_string = Variant::get_type_name(elem_type.builtin_type);5285break;5286case NATIVE:5287result.hint = PROPERTY_HINT_ARRAY_TYPE;5288result.hint_string = elem_type.native_type;5289break;5290case SCRIPT:5291result.hint = PROPERTY_HINT_ARRAY_TYPE;5292if (elem_type.script_type.is_valid() && elem_type.script_type->get_global_name() != StringName()) {5293result.hint_string = elem_type.script_type->get_global_name();5294} else {5295result.hint_string = elem_type.native_type;5296}5297break;5298case CLASS:5299result.hint = PROPERTY_HINT_ARRAY_TYPE;5300if (elem_type.class_type != nullptr && elem_type.class_type->get_global_name() != StringName()) {5301result.hint_string = elem_type.class_type->get_global_name();5302} else {5303result.hint_string = elem_type.native_type;5304}5305break;5306case ENUM:5307result.hint = PROPERTY_HINT_ARRAY_TYPE;5308result.hint_string = String(elem_type.native_type).replace("::", ".");5309break;5310case VARIANT:5311case RESOLVING:5312case UNRESOLVED:5313break;5314}5315} else if (builtin_type == Variant::DICTIONARY && has_container_element_types()) {5316const DataType key_type = get_container_element_type_or_variant(0);5317const DataType value_type = get_container_element_type_or_variant(1);5318if ((key_type.kind == VARIANT && value_type.kind == VARIANT) || key_type.kind == RESOLVING ||5319key_type.kind == UNRESOLVED || value_type.kind == RESOLVING || value_type.kind == UNRESOLVED) {5320break;5321}5322String key_hint, value_hint;5323switch (key_type.kind) {5324case BUILTIN:5325key_hint = Variant::get_type_name(key_type.builtin_type);5326break;5327case NATIVE:5328key_hint = key_type.native_type;5329break;5330case SCRIPT:5331if (key_type.script_type.is_valid() && key_type.script_type->get_global_name() != StringName()) {5332key_hint = key_type.script_type->get_global_name();5333} else {5334key_hint = key_type.native_type;5335}5336break;5337case CLASS:5338if (key_type.class_type != nullptr && key_type.class_type->get_global_name() != StringName()) {5339key_hint = key_type.class_type->get_global_name();5340} else {5341key_hint = key_type.native_type;5342}5343break;5344case ENUM:5345key_hint = String(key_type.native_type).replace("::", ".");5346break;5347default:5348key_hint = "Variant";5349break;5350}5351switch (value_type.kind) {5352case BUILTIN:5353value_hint = Variant::get_type_name(value_type.builtin_type);5354break;5355case NATIVE:5356value_hint = value_type.native_type;5357break;5358case SCRIPT:5359if (value_type.script_type.is_valid() && value_type.script_type->get_global_name() != StringName()) {5360value_hint = value_type.script_type->get_global_name();5361} else {5362value_hint = value_type.native_type;5363}5364break;5365case CLASS:5366if (value_type.class_type != nullptr && value_type.class_type->get_global_name() != StringName()) {5367value_hint = value_type.class_type->get_global_name();5368} else {5369value_hint = value_type.native_type;5370}5371break;5372case ENUM:5373value_hint = String(value_type.native_type).replace("::", ".");5374break;5375default:5376value_hint = "Variant";5377break;5378}5379result.hint = PROPERTY_HINT_DICTIONARY_TYPE;5380result.hint_string = key_hint + ";" + value_hint;5381}5382break;5383case NATIVE:5384result.type = Variant::OBJECT;5385if (is_meta_type) {5386result.class_name = GDScriptNativeClass::get_class_static();5387} else {5388result.class_name = native_type;5389}5390break;5391case SCRIPT:5392result.type = Variant::OBJECT;5393if (is_meta_type) {5394result.class_name = script_type.is_valid() ? script_type->get_class_name() : Script::get_class_static();5395} else if (script_type.is_valid() && script_type->get_global_name() != StringName()) {5396result.class_name = script_type->get_global_name();5397} else {5398result.class_name = native_type;5399}5400break;5401case CLASS:5402result.type = Variant::OBJECT;5403if (is_meta_type) {5404result.class_name = GDScript::get_class_static();5405} else if (class_type != nullptr && class_type->get_global_name() != StringName()) {5406result.class_name = class_type->get_global_name();5407} else {5408result.class_name = native_type;5409}5410break;5411case ENUM:5412if (is_meta_type) {5413result.type = Variant::DICTIONARY;5414} else {5415result.type = Variant::INT;5416result.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;5417result.class_name = String(native_type).replace("::", ".");5418}5419break;5420case VARIANT:5421case RESOLVING:5422case UNRESOLVED:5423result.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;5424break;5425}54265427return result;5428}54295430static Variant::Type _variant_type_to_typed_array_element_type(Variant::Type p_type) {5431switch (p_type) {5432case Variant::PACKED_BYTE_ARRAY:5433case Variant::PACKED_INT32_ARRAY:5434case Variant::PACKED_INT64_ARRAY:5435return Variant::INT;5436case Variant::PACKED_FLOAT32_ARRAY:5437case Variant::PACKED_FLOAT64_ARRAY:5438return Variant::FLOAT;5439case Variant::PACKED_STRING_ARRAY:5440return Variant::STRING;5441case Variant::PACKED_VECTOR2_ARRAY:5442return Variant::VECTOR2;5443case Variant::PACKED_VECTOR3_ARRAY:5444return Variant::VECTOR3;5445case Variant::PACKED_COLOR_ARRAY:5446return Variant::COLOR;5447case Variant::PACKED_VECTOR4_ARRAY:5448return Variant::VECTOR4;5449default:5450return Variant::NIL;5451}5452}54535454bool GDScriptParser::DataType::is_typed_container_type() const {5455return kind == GDScriptParser::DataType::BUILTIN && _variant_type_to_typed_array_element_type(builtin_type) != Variant::NIL;5456}54575458GDScriptParser::DataType GDScriptParser::DataType::get_typed_container_type() const {5459GDScriptParser::DataType type;5460type.kind = GDScriptParser::DataType::BUILTIN;5461type.builtin_type = _variant_type_to_typed_array_element_type(builtin_type);5462return type;5463}54645465bool GDScriptParser::DataType::can_reference(const GDScriptParser::DataType &p_other) const {5466if (p_other.is_meta_type) {5467return false;5468} else if (builtin_type != p_other.builtin_type) {5469return false;5470} else if (builtin_type != Variant::OBJECT) {5471return true;5472}54735474if (native_type == StringName()) {5475return true;5476} else if (p_other.native_type == StringName()) {5477return false;5478} else if (native_type != p_other.native_type && !ClassDB::is_parent_class(p_other.native_type, native_type)) {5479return false;5480}54815482Ref<Script> script = script_type;5483if (kind == GDScriptParser::DataType::CLASS && script.is_null()) {5484Error err = OK;5485Ref<GDScript> scr = GDScriptCache::get_shallow_script(script_path, err);5486ERR_FAIL_COND_V_MSG(err, false, vformat(R"(Error while getting cache for script "%s".)", script_path));5487script.reference_ptr(scr->find_class(class_type->fqcn));5488}54895490Ref<Script> script_other = p_other.script_type;5491if (p_other.kind == GDScriptParser::DataType::CLASS && script_other.is_null()) {5492Error err = OK;5493Ref<GDScript> scr = GDScriptCache::get_shallow_script(p_other.script_path, err);5494ERR_FAIL_COND_V_MSG(err, false, vformat(R"(Error while getting cache for script "%s".)", p_other.script_path));5495script_other.reference_ptr(scr->find_class(p_other.class_type->fqcn));5496}54975498if (script.is_null()) {5499return true;5500} else if (script_other.is_null()) {5501return false;5502} else if (script != script_other && !script_other->inherits_script(script)) {5503return false;5504}55055506return true;5507}55085509void GDScriptParser::complete_extents(Node *p_node) {5510while (!nodes_in_progress.is_empty() && nodes_in_progress.back()->get() != p_node) {5511ERR_PRINT("Parser bug: Mismatch in extents tracking stack.");5512nodes_in_progress.pop_back();5513}5514if (nodes_in_progress.is_empty()) {5515ERR_PRINT("Parser bug: Extents tracking stack is empty.");5516} else {5517nodes_in_progress.pop_back();5518}5519}55205521void GDScriptParser::update_extents(Node *p_node) {5522p_node->end_line = previous.end_line;5523p_node->end_column = previous.end_column;5524}55255526void GDScriptParser::reset_extents(Node *p_node, GDScriptTokenizer::Token p_token) {5527p_node->start_line = p_token.start_line;5528p_node->end_line = p_token.end_line;5529p_node->start_column = p_token.start_column;5530p_node->end_column = p_token.end_column;5531}55325533void GDScriptParser::reset_extents(Node *p_node, Node *p_from) {5534if (p_from == nullptr) {5535return;5536}5537p_node->start_line = p_from->start_line;5538p_node->end_line = p_from->end_line;5539p_node->start_column = p_from->start_column;5540p_node->end_column = p_from->end_column;5541}55425543/*---------- PRETTY PRINT FOR DEBUG ----------*/55445545#ifdef DEBUG_ENABLED55465547void GDScriptParser::TreePrinter::increase_indent() {5548indent_level++;5549indent = "";5550for (int i = 0; i < indent_level * 4; i++) {5551if (i % 4 == 0) {5552indent += "|";5553} else {5554indent += " ";5555}5556}5557}55585559void GDScriptParser::TreePrinter::decrease_indent() {5560indent_level--;5561indent = "";5562for (int i = 0; i < indent_level * 4; i++) {5563if (i % 4 == 0) {5564indent += "|";5565} else {5566indent += " ";5567}5568}5569}55705571void GDScriptParser::TreePrinter::push_line(const String &p_line) {5572if (!p_line.is_empty()) {5573push_text(p_line);5574}5575printed += "\n";5576pending_indent = true;5577}55785579void GDScriptParser::TreePrinter::push_text(const String &p_text) {5580if (pending_indent) {5581printed += indent;5582pending_indent = false;5583}5584printed += p_text;5585}55865587void GDScriptParser::TreePrinter::print_annotation(const AnnotationNode *p_annotation) {5588push_text(p_annotation->name);5589push_text(" (");5590for (int i = 0; i < p_annotation->arguments.size(); i++) {5591if (i > 0) {5592push_text(" , ");5593}5594print_expression(p_annotation->arguments[i]);5595}5596push_line(")");5597}55985599void GDScriptParser::TreePrinter::print_array(ArrayNode *p_array) {5600push_text("[ ");5601for (int i = 0; i < p_array->elements.size(); i++) {5602if (i > 0) {5603push_text(" , ");5604}5605print_expression(p_array->elements[i]);5606}5607push_text(" ]");5608}56095610void GDScriptParser::TreePrinter::print_assert(AssertNode *p_assert) {5611push_text("Assert ( ");5612print_expression(p_assert->condition);5613push_line(" )");5614}56155616void GDScriptParser::TreePrinter::print_assignment(AssignmentNode *p_assignment) {5617switch (p_assignment->assignee->type) {5618case Node::IDENTIFIER:5619print_identifier(static_cast<IdentifierNode *>(p_assignment->assignee));5620break;5621case Node::SUBSCRIPT:5622print_subscript(static_cast<SubscriptNode *>(p_assignment->assignee));5623break;5624default:5625break; // Unreachable.5626}56275628push_text(" ");5629switch (p_assignment->operation) {5630case AssignmentNode::OP_ADDITION:5631push_text("+");5632break;5633case AssignmentNode::OP_SUBTRACTION:5634push_text("-");5635break;5636case AssignmentNode::OP_MULTIPLICATION:5637push_text("*");5638break;5639case AssignmentNode::OP_DIVISION:5640push_text("/");5641break;5642case AssignmentNode::OP_MODULO:5643push_text("%");5644break;5645case AssignmentNode::OP_POWER:5646push_text("**");5647break;5648case AssignmentNode::OP_BIT_SHIFT_LEFT:5649push_text("<<");5650break;5651case AssignmentNode::OP_BIT_SHIFT_RIGHT:5652push_text(">>");5653break;5654case AssignmentNode::OP_BIT_AND:5655push_text("&");5656break;5657case AssignmentNode::OP_BIT_OR:5658push_text("|");5659break;5660case AssignmentNode::OP_BIT_XOR:5661push_text("^");5662break;5663case AssignmentNode::OP_NONE:5664break;5665}5666push_text("= ");5667print_expression(p_assignment->assigned_value);5668push_line();5669}56705671void GDScriptParser::TreePrinter::print_await(AwaitNode *p_await) {5672push_text("Await ");5673print_expression(p_await->to_await);5674}56755676void GDScriptParser::TreePrinter::print_binary_op(BinaryOpNode *p_binary_op) {5677// Surround in parenthesis for disambiguation.5678push_text("(");5679print_expression(p_binary_op->left_operand);5680switch (p_binary_op->operation) {5681case BinaryOpNode::OP_ADDITION:5682push_text(" + ");5683break;5684case BinaryOpNode::OP_SUBTRACTION:5685push_text(" - ");5686break;5687case BinaryOpNode::OP_MULTIPLICATION:5688push_text(" * ");5689break;5690case BinaryOpNode::OP_DIVISION:5691push_text(" / ");5692break;5693case BinaryOpNode::OP_MODULO:5694push_text(" % ");5695break;5696case BinaryOpNode::OP_POWER:5697push_text(" ** ");5698break;5699case BinaryOpNode::OP_BIT_LEFT_SHIFT:5700push_text(" << ");5701break;5702case BinaryOpNode::OP_BIT_RIGHT_SHIFT:5703push_text(" >> ");5704break;5705case BinaryOpNode::OP_BIT_AND:5706push_text(" & ");5707break;5708case BinaryOpNode::OP_BIT_OR:5709push_text(" | ");5710break;5711case BinaryOpNode::OP_BIT_XOR:5712push_text(" ^ ");5713break;5714case BinaryOpNode::OP_LOGIC_AND:5715push_text(" AND ");5716break;5717case BinaryOpNode::OP_LOGIC_OR:5718push_text(" OR ");5719break;5720case BinaryOpNode::OP_CONTENT_TEST:5721push_text(" IN ");5722break;5723case BinaryOpNode::OP_COMP_EQUAL:5724push_text(" == ");5725break;5726case BinaryOpNode::OP_COMP_NOT_EQUAL:5727push_text(" != ");5728break;5729case BinaryOpNode::OP_COMP_LESS:5730push_text(" < ");5731break;5732case BinaryOpNode::OP_COMP_LESS_EQUAL:5733push_text(" <= ");5734break;5735case BinaryOpNode::OP_COMP_GREATER:5736push_text(" > ");5737break;5738case BinaryOpNode::OP_COMP_GREATER_EQUAL:5739push_text(" >= ");5740break;5741}5742print_expression(p_binary_op->right_operand);5743// Surround in parenthesis for disambiguation.5744push_text(")");5745}57465747void GDScriptParser::TreePrinter::print_call(CallNode *p_call) {5748if (p_call->is_super) {5749push_text("super");5750if (p_call->callee != nullptr) {5751push_text(".");5752print_expression(p_call->callee);5753}5754} else {5755print_expression(p_call->callee);5756}5757push_text("( ");5758for (int i = 0; i < p_call->arguments.size(); i++) {5759if (i > 0) {5760push_text(" , ");5761}5762print_expression(p_call->arguments[i]);5763}5764push_text(" )");5765}57665767void GDScriptParser::TreePrinter::print_cast(CastNode *p_cast) {5768print_expression(p_cast->operand);5769push_text(" AS ");5770print_type(p_cast->cast_type);5771}57725773void GDScriptParser::TreePrinter::print_class(ClassNode *p_class) {5774for (const AnnotationNode *E : p_class->annotations) {5775print_annotation(E);5776}5777push_text("Class ");5778if (p_class->identifier == nullptr) {5779push_text("<unnamed>");5780} else {5781print_identifier(p_class->identifier);5782}57835784if (p_class->extends_used) {5785bool first = true;5786push_text(" Extends ");5787if (!p_class->extends_path.is_empty()) {5788push_text(vformat(R"("%s")", p_class->extends_path));5789first = false;5790}5791for (int i = 0; i < p_class->extends.size(); i++) {5792if (!first) {5793push_text(".");5794} else {5795first = false;5796}5797push_text(p_class->extends[i]->name);5798}5799}58005801push_line(" :");58025803increase_indent();58045805for (int i = 0; i < p_class->members.size(); i++) {5806const ClassNode::Member &m = p_class->members[i];58075808switch (m.type) {5809case ClassNode::Member::CLASS:5810print_class(m.m_class);5811break;5812case ClassNode::Member::VARIABLE:5813print_variable(m.variable);5814break;5815case ClassNode::Member::CONSTANT:5816print_constant(m.constant);5817break;5818case ClassNode::Member::SIGNAL:5819print_signal(m.signal);5820break;5821case ClassNode::Member::FUNCTION:5822print_function(m.function);5823break;5824case ClassNode::Member::ENUM:5825print_enum(m.m_enum);5826break;5827case ClassNode::Member::ENUM_VALUE:5828break; // Nothing. Will be printed by enum.5829case ClassNode::Member::GROUP:5830break; // Nothing. Groups are only used by inspector.5831case ClassNode::Member::UNDEFINED:5832push_line("<unknown member>");5833break;5834}5835}58365837decrease_indent();5838}58395840void GDScriptParser::TreePrinter::print_constant(ConstantNode *p_constant) {5841push_text("Constant ");5842print_identifier(p_constant->identifier);58435844increase_indent();58455846push_line();5847push_text("= ");5848if (p_constant->initializer == nullptr) {5849push_text("<missing value>");5850} else {5851print_expression(p_constant->initializer);5852}5853decrease_indent();5854push_line();5855}58565857void GDScriptParser::TreePrinter::print_dictionary(DictionaryNode *p_dictionary) {5858push_line("{");5859increase_indent();5860for (int i = 0; i < p_dictionary->elements.size(); i++) {5861print_expression(p_dictionary->elements[i].key);5862if (p_dictionary->style == DictionaryNode::PYTHON_DICT) {5863push_text(" : ");5864} else {5865push_text(" = ");5866}5867print_expression(p_dictionary->elements[i].value);5868push_line(" ,");5869}5870decrease_indent();5871push_text("}");5872}58735874void GDScriptParser::TreePrinter::print_expression(ExpressionNode *p_expression) {5875if (p_expression == nullptr) {5876push_text("<invalid expression>");5877return;5878}5879switch (p_expression->type) {5880case Node::ARRAY:5881print_array(static_cast<ArrayNode *>(p_expression));5882break;5883case Node::ASSIGNMENT:5884print_assignment(static_cast<AssignmentNode *>(p_expression));5885break;5886case Node::AWAIT:5887print_await(static_cast<AwaitNode *>(p_expression));5888break;5889case Node::BINARY_OPERATOR:5890print_binary_op(static_cast<BinaryOpNode *>(p_expression));5891break;5892case Node::CALL:5893print_call(static_cast<CallNode *>(p_expression));5894break;5895case Node::CAST:5896print_cast(static_cast<CastNode *>(p_expression));5897break;5898case Node::DICTIONARY:5899print_dictionary(static_cast<DictionaryNode *>(p_expression));5900break;5901case Node::GET_NODE:5902print_get_node(static_cast<GetNodeNode *>(p_expression));5903break;5904case Node::IDENTIFIER:5905print_identifier(static_cast<IdentifierNode *>(p_expression));5906break;5907case Node::LAMBDA:5908print_lambda(static_cast<LambdaNode *>(p_expression));5909break;5910case Node::LITERAL:5911print_literal(static_cast<LiteralNode *>(p_expression));5912break;5913case Node::PRELOAD:5914print_preload(static_cast<PreloadNode *>(p_expression));5915break;5916case Node::SELF:5917print_self(static_cast<SelfNode *>(p_expression));5918break;5919case Node::SUBSCRIPT:5920print_subscript(static_cast<SubscriptNode *>(p_expression));5921break;5922case Node::TERNARY_OPERATOR:5923print_ternary_op(static_cast<TernaryOpNode *>(p_expression));5924break;5925case Node::TYPE_TEST:5926print_type_test(static_cast<TypeTestNode *>(p_expression));5927break;5928case Node::UNARY_OPERATOR:5929print_unary_op(static_cast<UnaryOpNode *>(p_expression));5930break;5931default:5932push_text(vformat("<unknown expression %d>", p_expression->type));5933break;5934}5935}59365937void GDScriptParser::TreePrinter::print_enum(EnumNode *p_enum) {5938push_text("Enum ");5939if (p_enum->identifier != nullptr) {5940print_identifier(p_enum->identifier);5941} else {5942push_text("<unnamed>");5943}59445945push_line(" {");5946increase_indent();5947for (int i = 0; i < p_enum->values.size(); i++) {5948const EnumNode::Value &item = p_enum->values[i];5949print_identifier(item.identifier);5950push_text(" = ");5951push_text(itos(item.value));5952push_line(" ,");5953}5954decrease_indent();5955push_line("}");5956}59575958void GDScriptParser::TreePrinter::print_for(ForNode *p_for) {5959push_text("For ");5960print_identifier(p_for->variable);5961push_text(" IN ");5962print_expression(p_for->list);5963push_line(" :");59645965increase_indent();59665967print_suite(p_for->loop);59685969decrease_indent();5970}59715972void GDScriptParser::TreePrinter::print_function(FunctionNode *p_function, const String &p_context) {5973for (const AnnotationNode *E : p_function->annotations) {5974print_annotation(E);5975}5976if (p_function->is_static) {5977push_text("Static ");5978}5979push_text(p_context);5980push_text(" ");5981if (p_function->identifier) {5982print_identifier(p_function->identifier);5983} else {5984push_text("<anonymous>");5985}5986push_text("( ");5987for (int i = 0; i < p_function->parameters.size(); i++) {5988if (i > 0) {5989push_text(" , ");5990}5991print_parameter(p_function->parameters[i]);5992}5993push_line(" ) :");5994increase_indent();5995print_suite(p_function->body);5996decrease_indent();5997}59985999void GDScriptParser::TreePrinter::print_get_node(GetNodeNode *p_get_node) {6000if (p_get_node->use_dollar) {6001push_text("$");6002}6003push_text(p_get_node->full_path);6004}60056006void GDScriptParser::TreePrinter::print_identifier(IdentifierNode *p_identifier) {6007if (p_identifier != nullptr) {6008push_text(p_identifier->name);6009} else {6010push_text("<invalid identifier>");6011}6012}60136014void GDScriptParser::TreePrinter::print_if(IfNode *p_if, bool p_is_elif) {6015if (p_is_elif) {6016push_text("Elif ");6017} else {6018push_text("If ");6019}6020print_expression(p_if->condition);6021push_line(" :");60226023increase_indent();6024print_suite(p_if->true_block);6025decrease_indent();60266027// FIXME: Properly detect "elif" blocks.6028if (p_if->false_block != nullptr) {6029push_line("Else :");6030increase_indent();6031print_suite(p_if->false_block);6032decrease_indent();6033}6034}60356036void GDScriptParser::TreePrinter::print_lambda(LambdaNode *p_lambda) {6037print_function(p_lambda->function, "Lambda");6038push_text("| captures [ ");6039for (int i = 0; i < p_lambda->captures.size(); i++) {6040if (i > 0) {6041push_text(" , ");6042}6043push_text(p_lambda->captures[i]->name.operator String());6044}6045push_line(" ]");6046}60476048void GDScriptParser::TreePrinter::print_literal(LiteralNode *p_literal) {6049// Prefix for string types.6050switch (p_literal->value.get_type()) {6051case Variant::NODE_PATH:6052push_text("^\"");6053break;6054case Variant::STRING:6055push_text("\"");6056break;6057case Variant::STRING_NAME:6058push_text("&\"");6059break;6060default:6061break;6062}6063push_text(p_literal->value);6064// Suffix for string types.6065switch (p_literal->value.get_type()) {6066case Variant::NODE_PATH:6067case Variant::STRING:6068case Variant::STRING_NAME:6069push_text("\"");6070break;6071default:6072break;6073}6074}60756076void GDScriptParser::TreePrinter::print_match(MatchNode *p_match) {6077push_text("Match ");6078print_expression(p_match->test);6079push_line(" :");60806081increase_indent();6082for (int i = 0; i < p_match->branches.size(); i++) {6083print_match_branch(p_match->branches[i]);6084}6085decrease_indent();6086}60876088void GDScriptParser::TreePrinter::print_match_branch(MatchBranchNode *p_match_branch) {6089for (int i = 0; i < p_match_branch->patterns.size(); i++) {6090if (i > 0) {6091push_text(" , ");6092}6093print_match_pattern(p_match_branch->patterns[i]);6094}60956096push_line(" :");60976098increase_indent();6099print_suite(p_match_branch->block);6100decrease_indent();6101}61026103void GDScriptParser::TreePrinter::print_match_pattern(PatternNode *p_match_pattern) {6104switch (p_match_pattern->pattern_type) {6105case PatternNode::PT_LITERAL:6106print_literal(p_match_pattern->literal);6107break;6108case PatternNode::PT_WILDCARD:6109push_text("_");6110break;6111case PatternNode::PT_REST:6112push_text("..");6113break;6114case PatternNode::PT_BIND:6115push_text("Var ");6116print_identifier(p_match_pattern->bind);6117break;6118case PatternNode::PT_EXPRESSION:6119print_expression(p_match_pattern->expression);6120break;6121case PatternNode::PT_ARRAY:6122push_text("[ ");6123for (int i = 0; i < p_match_pattern->array.size(); i++) {6124if (i > 0) {6125push_text(" , ");6126}6127print_match_pattern(p_match_pattern->array[i]);6128}6129push_text(" ]");6130break;6131case PatternNode::PT_DICTIONARY:6132push_text("{ ");6133for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {6134if (i > 0) {6135push_text(" , ");6136}6137if (p_match_pattern->dictionary[i].key != nullptr) {6138// Key can be null for rest pattern.6139print_expression(p_match_pattern->dictionary[i].key);6140push_text(" : ");6141}6142print_match_pattern(p_match_pattern->dictionary[i].value_pattern);6143}6144push_text(" }");6145break;6146}6147}61486149void GDScriptParser::TreePrinter::print_parameter(ParameterNode *p_parameter) {6150print_identifier(p_parameter->identifier);6151if (p_parameter->datatype_specifier != nullptr) {6152push_text(" : ");6153print_type(p_parameter->datatype_specifier);6154}6155if (p_parameter->initializer != nullptr) {6156push_text(" = ");6157print_expression(p_parameter->initializer);6158}6159}61606161void GDScriptParser::TreePrinter::print_preload(PreloadNode *p_preload) {6162push_text(R"(Preload ( ")");6163push_text(p_preload->resolved_path);6164push_text(R"(" )");6165}61666167void GDScriptParser::TreePrinter::print_return(ReturnNode *p_return) {6168push_text("Return");6169if (p_return->return_value != nullptr) {6170push_text(" ");6171print_expression(p_return->return_value);6172}6173push_line();6174}61756176void GDScriptParser::TreePrinter::print_self(SelfNode *p_self) {6177push_text("Self(");6178if (p_self->current_class->identifier != nullptr) {6179print_identifier(p_self->current_class->identifier);6180} else {6181push_text("<main class>");6182}6183push_text(")");6184}61856186void GDScriptParser::TreePrinter::print_signal(SignalNode *p_signal) {6187push_text("Signal ");6188print_identifier(p_signal->identifier);6189push_text("( ");6190for (int i = 0; i < p_signal->parameters.size(); i++) {6191print_parameter(p_signal->parameters[i]);6192}6193push_line(" )");6194}61956196void GDScriptParser::TreePrinter::print_subscript(SubscriptNode *p_subscript) {6197print_expression(p_subscript->base);6198if (p_subscript->is_attribute) {6199push_text(".");6200print_identifier(p_subscript->attribute);6201} else {6202push_text("[ ");6203print_expression(p_subscript->index);6204push_text(" ]");6205}6206}62076208void GDScriptParser::TreePrinter::print_statement(Node *p_statement) {6209switch (p_statement->type) {6210case Node::ASSERT:6211print_assert(static_cast<AssertNode *>(p_statement));6212break;6213case Node::VARIABLE:6214print_variable(static_cast<VariableNode *>(p_statement));6215break;6216case Node::CONSTANT:6217print_constant(static_cast<ConstantNode *>(p_statement));6218break;6219case Node::IF:6220print_if(static_cast<IfNode *>(p_statement));6221break;6222case Node::FOR:6223print_for(static_cast<ForNode *>(p_statement));6224break;6225case Node::WHILE:6226print_while(static_cast<WhileNode *>(p_statement));6227break;6228case Node::MATCH:6229print_match(static_cast<MatchNode *>(p_statement));6230break;6231case Node::RETURN:6232print_return(static_cast<ReturnNode *>(p_statement));6233break;6234case Node::BREAK:6235push_line("Break");6236break;6237case Node::CONTINUE:6238push_line("Continue");6239break;6240case Node::PASS:6241push_line("Pass");6242break;6243case Node::BREAKPOINT:6244push_line("Breakpoint");6245break;6246case Node::ASSIGNMENT:6247print_assignment(static_cast<AssignmentNode *>(p_statement));6248break;6249default:6250if (p_statement->is_expression()) {6251print_expression(static_cast<ExpressionNode *>(p_statement));6252push_line();6253} else {6254push_line(vformat("<unknown statement %d>", p_statement->type));6255}6256break;6257}6258}62596260void GDScriptParser::TreePrinter::print_suite(SuiteNode *p_suite) {6261for (int i = 0; i < p_suite->statements.size(); i++) {6262print_statement(p_suite->statements[i]);6263}6264}62656266void GDScriptParser::TreePrinter::print_ternary_op(TernaryOpNode *p_ternary_op) {6267// Surround in parenthesis for disambiguation.6268push_text("(");6269print_expression(p_ternary_op->true_expr);6270push_text(") IF (");6271print_expression(p_ternary_op->condition);6272push_text(") ELSE (");6273print_expression(p_ternary_op->false_expr);6274push_text(")");6275}62766277void GDScriptParser::TreePrinter::print_type(TypeNode *p_type) {6278if (p_type->type_chain.is_empty()) {6279push_text("Void");6280} else {6281for (int i = 0; i < p_type->type_chain.size(); i++) {6282if (i > 0) {6283push_text(".");6284}6285print_identifier(p_type->type_chain[i]);6286}6287}6288}62896290void GDScriptParser::TreePrinter::print_type_test(TypeTestNode *p_test) {6291print_expression(p_test->operand);6292push_text(" IS ");6293print_type(p_test->test_type);6294}62956296void GDScriptParser::TreePrinter::print_unary_op(UnaryOpNode *p_unary_op) {6297// Surround in parenthesis for disambiguation.6298push_text("(");6299switch (p_unary_op->operation) {6300case UnaryOpNode::OP_POSITIVE:6301push_text("+");6302break;6303case UnaryOpNode::OP_NEGATIVE:6304push_text("-");6305break;6306case UnaryOpNode::OP_LOGIC_NOT:6307push_text("NOT");6308break;6309case UnaryOpNode::OP_COMPLEMENT:6310push_text("~");6311break;6312}6313print_expression(p_unary_op->operand);6314// Surround in parenthesis for disambiguation.6315push_text(")");6316}63176318void GDScriptParser::TreePrinter::print_variable(VariableNode *p_variable) {6319for (const AnnotationNode *E : p_variable->annotations) {6320print_annotation(E);6321}63226323if (p_variable->is_static) {6324push_text("Static ");6325}6326push_text("Variable ");6327print_identifier(p_variable->identifier);63286329push_text(" : ");6330if (p_variable->datatype_specifier != nullptr) {6331print_type(p_variable->datatype_specifier);6332} else if (p_variable->infer_datatype) {6333push_text("<inferred type>");6334} else {6335push_text("Variant");6336}63376338increase_indent();63396340push_line();6341push_text("= ");6342if (p_variable->initializer == nullptr) {6343push_text("<default value>");6344} else {6345print_expression(p_variable->initializer);6346}6347push_line();63486349if (p_variable->property != VariableNode::PROP_NONE) {6350if (p_variable->getter != nullptr) {6351push_text("Get");6352if (p_variable->property == VariableNode::PROP_INLINE) {6353push_line(":");6354increase_indent();6355print_suite(p_variable->getter->body);6356decrease_indent();6357} else {6358push_line(" =");6359increase_indent();6360print_identifier(p_variable->getter_pointer);6361push_line();6362decrease_indent();6363}6364}6365if (p_variable->setter != nullptr) {6366push_text("Set (");6367if (p_variable->property == VariableNode::PROP_INLINE) {6368if (p_variable->setter_parameter != nullptr) {6369print_identifier(p_variable->setter_parameter);6370} else {6371push_text("<missing>");6372}6373push_line("):");6374increase_indent();6375print_suite(p_variable->setter->body);6376decrease_indent();6377} else {6378push_line(" =");6379increase_indent();6380print_identifier(p_variable->setter_pointer);6381push_line();6382decrease_indent();6383}6384}6385}63866387decrease_indent();6388push_line();6389}63906391void GDScriptParser::TreePrinter::print_while(WhileNode *p_while) {6392push_text("While ");6393print_expression(p_while->condition);6394push_line(" :");63956396increase_indent();6397print_suite(p_while->loop);6398decrease_indent();6399}64006401void GDScriptParser::TreePrinter::print_tree(const GDScriptParser &p_parser) {6402ClassNode *class_tree = p_parser.get_tree();6403ERR_FAIL_NULL_MSG(class_tree, "Parse the code before printing the parse tree.");64046405if (p_parser.is_tool()) {6406push_line("@tool");6407}6408if (!class_tree->icon_path.is_empty()) {6409push_text(R"(@icon (")");6410push_text(class_tree->icon_path);6411push_line("\")");6412}6413print_class(class_tree);64146415print_line(String(printed));6416}64176418#endif // DEBUG_ENABLED641964206421