Path: blob/master/modules/gdscript/gdscript_parser.cpp
10277 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_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();35963597pop_multiline();3598consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after preload path.)*");3599complete_extents(preload);36003601return preload;3602}36033604GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign) {3605LambdaNode *lambda = alloc_node<LambdaNode>();3606lambda->parent_function = current_function;3607lambda->parent_lambda = current_lambda;36083609FunctionNode *function = alloc_node<FunctionNode>();3610function->source_lambda = lambda;36113612function->is_static = current_function != nullptr ? current_function->is_static : false;36133614if (match(GDScriptTokenizer::Token::IDENTIFIER)) {3615function->identifier = parse_identifier();3616}36173618bool multiline_context = multiline_stack.back()->get();36193620push_completion_call(nullptr);36213622// Reset the multiline stack since we don't want the multiline mode one in the lambda body.3623push_multiline(false);3624if (multiline_context) {3625tokenizer->push_expression_indented_block();3626}36273628push_multiline(true); // For the parameters.3629if (function->identifier) {3630consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after lambda name.)");3631} else {3632consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after "func".)");3633}36343635FunctionNode *previous_function = current_function;3636current_function = function;36373638LambdaNode *previous_lambda = current_lambda;3639current_lambda = lambda;36403641SuiteNode *body = alloc_node<SuiteNode>();3642body->parent_function = current_function;3643body->parent_block = current_suite;36443645SuiteNode *previous_suite = current_suite;3646current_suite = body;36473648parse_function_signature(function, body, "lambda", -1);36493650current_suite = previous_suite;36513652bool previous_in_lambda = in_lambda;3653in_lambda = true;36543655// Save break/continue state.3656bool could_break = can_break;3657bool could_continue = can_continue;36583659// Disallow break/continue.3660can_break = false;3661can_continue = false;36623663function->body = parse_suite("lambda declaration", body, true);3664complete_extents(function);3665complete_extents(lambda);36663667pop_multiline();36683669pop_completion_call();36703671if (multiline_context) {3672// If we're in multiline mode, we want to skip the spurious DEDENT and NEWLINE tokens.3673while (check(GDScriptTokenizer::Token::DEDENT) || check(GDScriptTokenizer::Token::INDENT) || check(GDScriptTokenizer::Token::NEWLINE)) {3674current = tokenizer->scan(); // Not advance() since we don't want to change the previous token.3675}3676tokenizer->pop_expression_indented_block();3677}36783679current_function = previous_function;3680current_lambda = previous_lambda;3681in_lambda = previous_in_lambda;3682lambda->function = function;36833684// Reset break/continue state.3685can_break = could_break;3686can_continue = could_continue;36873688return lambda;3689}36903691GDScriptParser::ExpressionNode *GDScriptParser::parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign) {3692// x is not int3693// ^ ^^^ ExpressionNode, TypeNode3694// ^^^^^^^^^^^^ TypeTestNode3695// ^^^^^^^^^^^^ UnaryOpNode3696UnaryOpNode *not_node = nullptr;3697if (match(GDScriptTokenizer::Token::NOT)) {3698not_node = alloc_node<UnaryOpNode>();3699not_node->operation = UnaryOpNode::OP_LOGIC_NOT;3700not_node->variant_op = Variant::OP_NOT;3701reset_extents(not_node, p_previous_operand);3702update_extents(not_node);3703}37043705TypeTestNode *type_test = alloc_node<TypeTestNode>();3706reset_extents(type_test, p_previous_operand);3707update_extents(type_test);37083709type_test->operand = p_previous_operand;3710type_test->test_type = parse_type();3711complete_extents(type_test);37123713if (not_node != nullptr) {3714not_node->operand = type_test;3715complete_extents(not_node);3716}37173718if (type_test->test_type == nullptr) {3719if (not_node == nullptr) {3720push_error(R"(Expected type specifier after "is".)");3721} else {3722push_error(R"(Expected type specifier after "is not".)");3723}3724}37253726if (not_node != nullptr) {3727return not_node;3728}37293730return type_test;3731}37323733GDScriptParser::ExpressionNode *GDScriptParser::parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign) {3734push_error(R"("yield" was removed in Godot 4. Use "await" instead.)");3735return nullptr;3736}37373738GDScriptParser::ExpressionNode *GDScriptParser::parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign) {3739// Just for better error messages.3740GDScriptTokenizer::Token::Type invalid = previous.type;37413742switch (invalid) {3743case GDScriptTokenizer::Token::QUESTION_MARK:3744push_error(R"(Unexpected "?" in source. If you want a ternary operator, use "truthy_value if true_condition else falsy_value".)");3745break;3746default:3747return nullptr; // Unreachable.3748}37493750// Return the previous expression.3751return p_previous_operand;3752}37533754GDScriptParser::TypeNode *GDScriptParser::parse_type(bool p_allow_void) {3755TypeNode *type = alloc_node<TypeNode>();3756make_completion_context(p_allow_void ? COMPLETION_TYPE_NAME_OR_VOID : COMPLETION_TYPE_NAME, type);3757if (!match(GDScriptTokenizer::Token::IDENTIFIER)) {3758if (match(GDScriptTokenizer::Token::TK_VOID)) {3759if (p_allow_void) {3760complete_extents(type);3761TypeNode *void_type = type;3762return void_type;3763} else {3764push_error(R"("void" is only allowed for a function return type.)");3765}3766}3767// Leave error message to the caller who knows the context.3768complete_extents(type);3769return nullptr;3770}37713772IdentifierNode *type_element = parse_identifier();37733774type->type_chain.push_back(type_element);37753776if (match(GDScriptTokenizer::Token::BRACKET_OPEN)) {3777// Typed collection (like Array[int], Dictionary[String, int]).3778bool first_pass = true;3779do {3780TypeNode *container_type = parse_type(false); // Don't allow void for element type.3781if (container_type == nullptr) {3782push_error(vformat(R"(Expected type for collection after "%s".)", first_pass ? "[" : ","));3783complete_extents(type);3784type = nullptr;3785break;3786} else if (container_type->container_types.size() > 0) {3787push_error("Nested typed collections are not supported.");3788} else {3789type->container_types.append(container_type);3790}3791first_pass = false;3792} while (match(GDScriptTokenizer::Token::COMMA));3793consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected closing "]" after collection type.)");3794if (type != nullptr) {3795complete_extents(type);3796}3797return type;3798}37993800int chain_index = 1;3801while (match(GDScriptTokenizer::Token::PERIOD)) {3802make_completion_context(COMPLETION_TYPE_ATTRIBUTE, type, chain_index++);3803if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected inner type name after ".".)")) {3804type_element = parse_identifier();3805type->type_chain.push_back(type_element);3806}3807}38083809complete_extents(type);3810return type;3811}38123813#ifdef TOOLS_ENABLED3814enum DocLineState {3815DOC_LINE_NORMAL,3816DOC_LINE_IN_CODE,3817DOC_LINE_IN_CODEBLOCK,3818DOC_LINE_IN_KBD,3819};38203821static String _process_doc_line(const String &p_line, const String &p_text, const String &p_space_prefix, DocLineState &r_state) {3822String line = p_line;3823if (r_state == DOC_LINE_NORMAL) {3824line = line.strip_edges(true, false);3825} else {3826line = line.trim_prefix(p_space_prefix);3827}38283829String line_join;3830if (!p_text.is_empty()) {3831if (r_state == DOC_LINE_NORMAL) {3832if (p_text.ends_with("[/codeblock]")) {3833line_join = "\n";3834} else if (!p_text.ends_with("[br]")) {3835line_join = " ";3836}3837} else {3838line_join = "\n";3839}3840}38413842String result;3843int from = 0;3844int buffer_start = 0;3845const int len = line.length();3846bool process = true;3847while (process) {3848switch (r_state) {3849case DOC_LINE_NORMAL: {3850int lb_pos = line.find_char('[', from);3851if (lb_pos < 0) {3852process = false;3853break;3854}3855int rb_pos = line.find_char(']', lb_pos + 1);3856if (rb_pos < 0) {3857process = false;3858break;3859}38603861from = rb_pos + 1;38623863String tag = line.substr(lb_pos + 1, rb_pos - lb_pos - 1);3864if (tag == "code" || tag.begins_with("code ")) {3865r_state = DOC_LINE_IN_CODE;3866} else if (tag == "codeblock" || tag.begins_with("codeblock ")) {3867if (lb_pos == 0) {3868line_join = "\n";3869} else {3870result += line.substr(buffer_start, lb_pos - buffer_start) + '\n';3871}3872result += "[" + tag + "]";3873if (from < len) {3874result += '\n';3875}38763877r_state = DOC_LINE_IN_CODEBLOCK;3878buffer_start = from;3879} else if (tag == "kbd") {3880r_state = DOC_LINE_IN_KBD;3881}3882} break;3883case DOC_LINE_IN_CODE: {3884int pos = line.find("[/code]", from);3885if (pos < 0) {3886process = false;3887break;3888}38893890from = pos + 7; // `len("[/code]")`.38913892r_state = DOC_LINE_NORMAL;3893} break;3894case DOC_LINE_IN_CODEBLOCK: {3895int pos = line.find("[/codeblock]", from);3896if (pos < 0) {3897process = false;3898break;3899}39003901from = pos + 12; // `len("[/codeblock]")`.39023903if (pos == 0) {3904line_join = "\n";3905} else {3906result += line.substr(buffer_start, pos - buffer_start) + '\n';3907}3908result += "[/codeblock]";3909if (from < len) {3910result += '\n';3911}39123913r_state = DOC_LINE_NORMAL;3914buffer_start = from;3915} break;3916case DOC_LINE_IN_KBD: {3917int pos = line.find("[/kbd]", from);3918if (pos < 0) {3919process = false;3920break;3921}39223923from = pos + 6; // `len("[/kbd]")`.39243925r_state = DOC_LINE_NORMAL;3926} break;3927}3928}39293930result += line.substr(buffer_start);3931if (r_state == DOC_LINE_NORMAL) {3932result = result.strip_edges(false, true);3933}39343935return line_join + result;3936}39373938bool GDScriptParser::has_comment(int p_line, bool p_must_be_doc) {3939bool has_comment = tokenizer->get_comments().has(p_line);3940// If there are no comments or if we don't care whether the comment3941// is a docstring, we have our result.3942if (!p_must_be_doc || !has_comment) {3943return has_comment;3944}39453946return tokenizer->get_comments()[p_line].comment.begins_with("##");3947}39483949GDScriptParser::MemberDocData GDScriptParser::parse_doc_comment(int p_line, bool p_single_line) {3950ERR_FAIL_COND_V(!has_comment(p_line, true), MemberDocData());39513952const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments();3953int line = p_line;39543955if (!p_single_line) {3956while (comments.has(line - 1) && comments[line - 1].new_line && comments[line - 1].comment.begins_with("##")) {3957line--;3958}3959}39603961max_script_doc_line = MIN(max_script_doc_line, line - 1);39623963String space_prefix;3964{3965int i = 2;3966for (; i < comments[line].comment.length(); i++) {3967if (comments[line].comment[i] != ' ') {3968break;3969}3970}3971space_prefix = String(" ").repeat(i - 2);3972}39733974DocLineState state = DOC_LINE_NORMAL;3975MemberDocData result;39763977while (line <= p_line) {3978String doc_line = comments[line].comment.trim_prefix("##");3979line++;39803981if (state == DOC_LINE_NORMAL) {3982String stripped_line = doc_line.strip_edges();3983if (stripped_line == "@deprecated" || stripped_line.begins_with("@deprecated:")) {3984result.is_deprecated = true;3985if (stripped_line.begins_with("@deprecated:")) {3986result.deprecated_message = stripped_line.trim_prefix("@deprecated:").strip_edges();3987}3988continue;3989} else if (stripped_line == "@experimental" || stripped_line.begins_with("@experimental:")) {3990result.is_experimental = true;3991if (stripped_line.begins_with("@experimental:")) {3992result.experimental_message = stripped_line.trim_prefix("@experimental:").strip_edges();3993}3994continue;3995}3996}39973998result.description += _process_doc_line(doc_line, result.description, space_prefix, state);3999}40004001return result;4002}40034004GDScriptParser::ClassDocData GDScriptParser::parse_class_doc_comment(int p_line, bool p_single_line) {4005ERR_FAIL_COND_V(!has_comment(p_line, true), ClassDocData());40064007const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments();4008int line = p_line;40094010if (!p_single_line) {4011while (comments.has(line - 1) && comments[line - 1].new_line && comments[line - 1].comment.begins_with("##")) {4012line--;4013}4014}40154016max_script_doc_line = MIN(max_script_doc_line, line - 1);40174018String space_prefix;4019{4020int i = 2;4021for (; i < comments[line].comment.length(); i++) {4022if (comments[line].comment[i] != ' ') {4023break;4024}4025}4026space_prefix = String(" ").repeat(i - 2);4027}40284029DocLineState state = DOC_LINE_NORMAL;4030bool is_in_brief = true;4031ClassDocData result;40324033while (line <= p_line) {4034String doc_line = comments[line].comment.trim_prefix("##");4035line++;40364037if (state == DOC_LINE_NORMAL) {4038String stripped_line = doc_line.strip_edges();40394040// A blank line separates the description from the brief.4041if (is_in_brief && !result.brief.is_empty() && stripped_line.is_empty()) {4042is_in_brief = false;4043continue;4044}40454046if (stripped_line.begins_with("@tutorial")) {4047String title, link;40484049int begin_scan = String("@tutorial").length();4050if (begin_scan >= stripped_line.length()) {4051continue; // Invalid syntax.4052}40534054if (stripped_line[begin_scan] == ':') { // No title.4055// Syntax: ## @tutorial: https://godotengine.org/ // The title argument is optional.4056title = "";4057link = stripped_line.trim_prefix("@tutorial:").strip_edges();4058} else {4059/* Syntax:4060* @tutorial ( The Title Here ) : https://the.url/4061* ^ open ^ close ^ colon ^ url4062*/4063int open_bracket_pos = begin_scan, close_bracket_pos = 0;4064while (open_bracket_pos < stripped_line.length() && (stripped_line[open_bracket_pos] == ' ' || stripped_line[open_bracket_pos] == '\t')) {4065open_bracket_pos++;4066}4067if (open_bracket_pos == stripped_line.length() || stripped_line[open_bracket_pos++] != '(') {4068continue; // Invalid syntax.4069}4070close_bracket_pos = open_bracket_pos;4071while (close_bracket_pos < stripped_line.length() && stripped_line[close_bracket_pos] != ')') {4072close_bracket_pos++;4073}4074if (close_bracket_pos == stripped_line.length()) {4075continue; // Invalid syntax.4076}40774078int colon_pos = close_bracket_pos + 1;4079while (colon_pos < stripped_line.length() && (stripped_line[colon_pos] == ' ' || stripped_line[colon_pos] == '\t')) {4080colon_pos++;4081}4082if (colon_pos == stripped_line.length() || stripped_line[colon_pos++] != ':') {4083continue; // Invalid syntax.4084}40854086title = stripped_line.substr(open_bracket_pos, close_bracket_pos - open_bracket_pos).strip_edges();4087link = stripped_line.substr(colon_pos).strip_edges();4088}40894090result.tutorials.append(Pair<String, String>(title, link));4091continue;4092} else if (stripped_line == "@deprecated" || stripped_line.begins_with("@deprecated:")) {4093result.is_deprecated = true;4094if (stripped_line.begins_with("@deprecated:")) {4095result.deprecated_message = stripped_line.trim_prefix("@deprecated:").strip_edges();4096}4097continue;4098} else if (stripped_line == "@experimental" || stripped_line.begins_with("@experimental:")) {4099result.is_experimental = true;4100if (stripped_line.begins_with("@experimental:")) {4101result.experimental_message = stripped_line.trim_prefix("@experimental:").strip_edges();4102}4103continue;4104}4105}41064107if (is_in_brief) {4108result.brief += _process_doc_line(doc_line, result.brief, space_prefix, state);4109} else {4110result.description += _process_doc_line(doc_line, result.description, space_prefix, state);4111}4112}41134114return result;4115}4116#endif // TOOLS_ENABLED41174118GDScriptParser::ParseRule *GDScriptParser::get_rule(GDScriptTokenizer::Token::Type p_token_type) {4119// Function table for expression parsing.4120// clang-format destroys the alignment here, so turn off for the table.4121/* clang-format off */4122static ParseRule rules[] = {4123// PREFIX INFIX PRECEDENCE (for infix)4124{ nullptr, nullptr, PREC_NONE }, // EMPTY,4125// Basic4126{ nullptr, nullptr, PREC_NONE }, // ANNOTATION,4127{ &GDScriptParser::parse_identifier, nullptr, PREC_NONE }, // IDENTIFIER,4128{ &GDScriptParser::parse_literal, nullptr, PREC_NONE }, // LITERAL,4129// Comparison4130{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // LESS,4131{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // LESS_EQUAL,4132{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // GREATER,4133{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // GREATER_EQUAL,4134{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // EQUAL_EQUAL,4135{ nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // BANG_EQUAL,4136// Logical4137{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_AND }, // AND,4138{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_OR }, // OR,4139{ &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_not_in_operator, PREC_CONTENT_TEST }, // NOT,4140{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_AND }, // AMPERSAND_AMPERSAND,4141{ nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_OR }, // PIPE_PIPE,4142{ &GDScriptParser::parse_unary_operator, nullptr, PREC_NONE }, // BANG,4143// Bitwise4144{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_AND }, // AMPERSAND,4145{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_OR }, // PIPE,4146{ &GDScriptParser::parse_unary_operator, nullptr, PREC_NONE }, // TILDE,4147{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_XOR }, // CARET,4148{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_SHIFT }, // LESS_LESS,4149{ nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_SHIFT }, // GREATER_GREATER,4150// Math4151{ &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_operator, PREC_ADDITION_SUBTRACTION }, // PLUS,4152{ &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_operator, PREC_ADDITION_SUBTRACTION }, // MINUS,4153{ nullptr, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // STAR,4154{ nullptr, &GDScriptParser::parse_binary_operator, PREC_POWER }, // STAR_STAR,4155{ nullptr, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // SLASH,4156{ &GDScriptParser::parse_get_node, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // PERCENT,4157// Assignment4158{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // EQUAL,4159{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PLUS_EQUAL,4160{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // MINUS_EQUAL,4161{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // STAR_EQUAL,4162{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // STAR_STAR_EQUAL,4163{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // SLASH_EQUAL,4164{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PERCENT_EQUAL,4165{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // LESS_LESS_EQUAL,4166{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // GREATER_GREATER_EQUAL,4167{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // AMPERSAND_EQUAL,4168{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PIPE_EQUAL,4169{ nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // CARET_EQUAL,4170// Control flow4171{ nullptr, &GDScriptParser::parse_ternary_operator, PREC_TERNARY }, // IF,4172{ nullptr, nullptr, PREC_NONE }, // ELIF,4173{ nullptr, nullptr, PREC_NONE }, // ELSE,4174{ nullptr, nullptr, PREC_NONE }, // FOR,4175{ nullptr, nullptr, PREC_NONE }, // WHILE,4176{ nullptr, nullptr, PREC_NONE }, // BREAK,4177{ nullptr, nullptr, PREC_NONE }, // CONTINUE,4178{ nullptr, nullptr, PREC_NONE }, // PASS,4179{ nullptr, nullptr, PREC_NONE }, // RETURN,4180{ nullptr, nullptr, PREC_NONE }, // MATCH,4181{ nullptr, nullptr, PREC_NONE }, // WHEN,4182// Keywords4183{ nullptr, &GDScriptParser::parse_cast, PREC_CAST }, // AS,4184{ nullptr, nullptr, PREC_NONE }, // ASSERT,4185{ &GDScriptParser::parse_await, nullptr, PREC_NONE }, // AWAIT,4186{ nullptr, nullptr, PREC_NONE }, // BREAKPOINT,4187{ nullptr, nullptr, PREC_NONE }, // CLASS,4188{ nullptr, nullptr, PREC_NONE }, // CLASS_NAME,4189{ nullptr, nullptr, PREC_NONE }, // TK_CONST,4190{ nullptr, nullptr, PREC_NONE }, // ENUM,4191{ nullptr, nullptr, PREC_NONE }, // EXTENDS,4192{ &GDScriptParser::parse_lambda, nullptr, PREC_NONE }, // FUNC,4193{ nullptr, &GDScriptParser::parse_binary_operator, PREC_CONTENT_TEST }, // TK_IN,4194{ nullptr, &GDScriptParser::parse_type_test, PREC_TYPE_TEST }, // IS,4195{ nullptr, nullptr, PREC_NONE }, // NAMESPACE,4196{ &GDScriptParser::parse_preload, nullptr, PREC_NONE }, // PRELOAD,4197{ &GDScriptParser::parse_self, nullptr, PREC_NONE }, // SELF,4198{ nullptr, nullptr, PREC_NONE }, // SIGNAL,4199{ nullptr, nullptr, PREC_NONE }, // STATIC,4200{ &GDScriptParser::parse_call, nullptr, PREC_NONE }, // SUPER,4201{ nullptr, nullptr, PREC_NONE }, // TRAIT,4202{ nullptr, nullptr, PREC_NONE }, // VAR,4203{ nullptr, nullptr, PREC_NONE }, // TK_VOID,4204{ &GDScriptParser::parse_yield, nullptr, PREC_NONE }, // YIELD,4205// Punctuation4206{ &GDScriptParser::parse_array, &GDScriptParser::parse_subscript, PREC_SUBSCRIPT }, // BRACKET_OPEN,4207{ nullptr, nullptr, PREC_NONE }, // BRACKET_CLOSE,4208{ &GDScriptParser::parse_dictionary, nullptr, PREC_NONE }, // BRACE_OPEN,4209{ nullptr, nullptr, PREC_NONE }, // BRACE_CLOSE,4210{ &GDScriptParser::parse_grouping, &GDScriptParser::parse_call, PREC_CALL }, // PARENTHESIS_OPEN,4211{ nullptr, nullptr, PREC_NONE }, // PARENTHESIS_CLOSE,4212{ nullptr, nullptr, PREC_NONE }, // COMMA,4213{ nullptr, nullptr, PREC_NONE }, // SEMICOLON,4214{ nullptr, &GDScriptParser::parse_attribute, PREC_ATTRIBUTE }, // PERIOD,4215{ nullptr, nullptr, PREC_NONE }, // PERIOD_PERIOD,4216{ nullptr, nullptr, PREC_NONE }, // PERIOD_PERIOD_PERIOD,4217{ nullptr, nullptr, PREC_NONE }, // COLON,4218{ &GDScriptParser::parse_get_node, nullptr, PREC_NONE }, // DOLLAR,4219{ nullptr, nullptr, PREC_NONE }, // FORWARD_ARROW,4220{ nullptr, nullptr, PREC_NONE }, // UNDERSCORE,4221// Whitespace4222{ nullptr, nullptr, PREC_NONE }, // NEWLINE,4223{ nullptr, nullptr, PREC_NONE }, // INDENT,4224{ nullptr, nullptr, PREC_NONE }, // DEDENT,4225// Constants4226{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_PI,4227{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_TAU,4228{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_INF,4229{ &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_NAN,4230// Error message improvement4231{ nullptr, nullptr, PREC_NONE }, // VCS_CONFLICT_MARKER,4232{ nullptr, nullptr, PREC_NONE }, // BACKTICK,4233{ nullptr, &GDScriptParser::parse_invalid_token, PREC_CAST }, // QUESTION_MARK,4234// Special4235{ nullptr, nullptr, PREC_NONE }, // ERROR,4236{ nullptr, nullptr, PREC_NONE }, // TK_EOF,4237};4238/* clang-format on */4239// Avoid desync.4240static_assert(std::size(rules) == GDScriptTokenizer::Token::TK_MAX, "Amount of parse rules don't match the amount of token types.");42414242// Let's assume this is never invalid, since nothing generates a TK_MAX.4243return &rules[p_token_type];4244}42454246bool GDScriptParser::SuiteNode::has_local(const StringName &p_name) const {4247if (locals_indices.has(p_name)) {4248return true;4249}4250if (parent_block != nullptr) {4251return parent_block->has_local(p_name);4252}4253return false;4254}42554256const GDScriptParser::SuiteNode::Local &GDScriptParser::SuiteNode::get_local(const StringName &p_name) const {4257if (locals_indices.has(p_name)) {4258return locals[locals_indices[p_name]];4259}4260if (parent_block != nullptr) {4261return parent_block->get_local(p_name);4262}4263return empty;4264}42654266bool GDScriptParser::AnnotationNode::apply(GDScriptParser *p_this, Node *p_target, ClassNode *p_class) {4267if (is_applied) {4268return true;4269}4270is_applied = true;4271return (p_this->*(p_this->valid_annotations[name].apply))(this, p_target, p_class);4272}42734274bool GDScriptParser::AnnotationNode::applies_to(uint32_t p_target_kinds) const {4275return (info->target_kind & p_target_kinds) > 0;4276}42774278bool GDScriptParser::validate_annotation_arguments(AnnotationNode *p_annotation) {4279ERR_FAIL_COND_V_MSG(!valid_annotations.has(p_annotation->name), false, vformat(R"(Annotation "%s" not found to validate.)", p_annotation->name));42804281const MethodInfo &info = valid_annotations[p_annotation->name].info;42824283if (((info.flags & METHOD_FLAG_VARARG) == 0) && p_annotation->arguments.size() > info.arguments.size()) {4284push_error(vformat(R"(Annotation "%s" requires at most %d arguments, but %d were given.)", p_annotation->name, info.arguments.size(), p_annotation->arguments.size()));4285return false;4286}42874288if (p_annotation->arguments.size() < info.arguments.size() - info.default_arguments.size()) {4289push_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()));4290return false;4291}42924293// Some annotations need to be resolved and applied in the parser.4294if (p_annotation->name == SNAME("@icon") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) {4295for (int i = 0; i < p_annotation->arguments.size(); i++) {4296ExpressionNode *argument = p_annotation->arguments[i];42974298if (argument->type != Node::LITERAL) {4299push_error(vformat(R"(Argument %d of annotation "%s" must be a string literal.)", i + 1, p_annotation->name), argument);4300return false;4301}43024303Variant value = static_cast<LiteralNode *>(argument)->value;43044305if (value.get_type() != Variant::STRING) {4306push_error(vformat(R"(Argument %d of annotation "%s" must be a string literal.)", i + 1, p_annotation->name), argument);4307return false;4308}43094310p_annotation->resolved_arguments.push_back(value);4311}4312}43134314// For other annotations, see `GDScriptAnalyzer::resolve_annotation()`.43154316return true;4317}43184319bool GDScriptParser::tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4320#ifdef DEBUG_ENABLED4321if (_is_tool) {4322push_error(R"("@tool" annotation can only be used once.)", p_annotation);4323return false;4324}4325#endif // DEBUG_ENABLED4326_is_tool = true;4327return true;4328}43294330bool GDScriptParser::icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4331ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, R"("@icon" annotation can only be applied to classes.)");4332ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);43334334ClassNode *class_node = static_cast<ClassNode *>(p_target);4335String path = p_annotation->resolved_arguments[0];43364337#ifdef DEBUG_ENABLED4338if (!class_node->icon_path.is_empty()) {4339push_error(R"("@icon" annotation can only be used once.)", p_annotation);4340return false;4341}4342if (path.is_empty()) {4343push_error(R"("@icon" annotation argument must contain the path to the icon.)", p_annotation->arguments[0]);4344return false;4345}4346#endif // DEBUG_ENABLED43474348class_node->icon_path = path;43494350if (path.is_empty() || path.is_absolute_path()) {4351class_node->simplified_icon_path = path.simplify_path();4352} else if (path.is_relative_path()) {4353class_node->simplified_icon_path = script_path.get_base_dir().path_join(path).simplify_path();4354} else {4355class_node->simplified_icon_path = path;4356}43574358return true;4359}43604361bool GDScriptParser::static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4362ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, vformat(R"("%s" annotation can only be applied to classes.)", p_annotation->name));4363ClassNode *class_node = static_cast<ClassNode *>(p_target);4364if (class_node->annotated_static_unload) {4365push_error(vformat(R"("%s" annotation can only be used once per script.)", p_annotation->name), p_annotation);4366return false;4367}4368class_node->annotated_static_unload = true;4369return true;4370}43714372bool GDScriptParser::abstract_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4373// NOTE: Use `p_target`, **not** `p_class`, because when `p_target` is a class then `p_class` refers to the outer class.4374if (p_target->type == Node::CLASS) {4375ClassNode *class_node = static_cast<ClassNode *>(p_target);4376if (class_node->is_abstract) {4377push_error(R"("@abstract" annotation can only be used once per class.)", p_annotation);4378return false;4379}4380class_node->is_abstract = true;4381return true;4382}4383if (p_target->type == Node::FUNCTION) {4384FunctionNode *function_node = static_cast<FunctionNode *>(p_target);4385if (function_node->is_static) {4386push_error(R"("@abstract" annotation cannot be applied to static functions.)", p_annotation);4387return false;4388}4389if (function_node->is_abstract) {4390push_error(R"("@abstract" annotation can only be used once per function.)", p_annotation);4391return false;4392}4393function_node->is_abstract = true;4394return true;4395}4396ERR_FAIL_V_MSG(false, R"("@abstract" annotation can only be applied to classes and functions.)");4397}43984399bool GDScriptParser::onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4400ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, R"("@onready" annotation can only be applied to class variables.)");44014402if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) {4403push_error(R"("@onready" can only be used in classes that inherit "Node".)", p_annotation);4404return false;4405}44064407VariableNode *variable = static_cast<VariableNode *>(p_target);4408if (variable->is_static) {4409push_error(R"("@onready" annotation cannot be applied to a static variable.)", p_annotation);4410return false;4411}4412if (variable->onready) {4413push_error(R"("@onready" annotation can only be used once per variable.)", p_annotation);4414return false;4415}4416variable->onready = true;4417current_class->onready_used = true;4418return true;4419}44204421static String _get_annotation_error_string(const StringName &p_annotation_name, const Vector<Variant::Type> &p_expected_types, const GDScriptParser::DataType &p_provided_type) {4422Vector<String> types;4423for (int i = 0; i < p_expected_types.size(); i++) {4424const Variant::Type &type = p_expected_types[i];4425types.push_back(Variant::get_type_name(type));4426types.push_back("Array[" + Variant::get_type_name(type) + "]");4427switch (type) {4428case Variant::INT:4429types.push_back("PackedByteArray");4430types.push_back("PackedInt32Array");4431types.push_back("PackedInt64Array");4432break;4433case Variant::FLOAT:4434types.push_back("PackedFloat32Array");4435types.push_back("PackedFloat64Array");4436break;4437case Variant::STRING:4438types.push_back("PackedStringArray");4439break;4440case Variant::VECTOR2:4441types.push_back("PackedVector2Array");4442break;4443case Variant::VECTOR3:4444types.push_back("PackedVector3Array");4445break;4446case Variant::COLOR:4447types.push_back("PackedColorArray");4448break;4449case Variant::VECTOR4:4450types.push_back("PackedVector4Array");4451break;4452default:4453break;4454}4455}44564457String string;4458if (types.size() == 1) {4459string = types[0].quote();4460} else if (types.size() == 2) {4461string = types[0].quote() + " or " + types[1].quote();4462} else if (types.size() >= 3) {4463string = types[0].quote();4464for (int i = 1; i < types.size() - 1; i++) {4465string += ", " + types[i].quote();4466}4467string += ", or " + types[types.size() - 1].quote();4468}44694470return 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());4471}44724473static StringName _find_narrowest_native_or_global_class(const GDScriptParser::DataType &p_type) {4474switch (p_type.kind) {4475case GDScriptParser::DataType::NATIVE: {4476if (p_type.is_meta_type) {4477return Object::get_class_static(); // `GDScriptNativeClass` is not an exposed class.4478}4479return p_type.native_type;4480} break;4481case GDScriptParser::DataType::SCRIPT: {4482Ref<Script> script;4483if (p_type.script_type.is_valid()) {4484script = p_type.script_type;4485} else {4486script = ResourceLoader::load(p_type.script_path, SNAME("Script"));4487}44884489if (p_type.is_meta_type) {4490return script.is_valid() ? script->get_class_name() : Script::get_class_static();4491}4492if (script.is_null()) {4493return p_type.native_type;4494}4495if (script->get_global_name() != StringName()) {4496return script->get_global_name();4497}44984499Ref<Script> base_script = script->get_base_script();4500if (base_script.is_null()) {4501return script->get_instance_base_type();4502}45034504GDScriptParser::DataType base_type;4505base_type.kind = GDScriptParser::DataType::SCRIPT;4506base_type.builtin_type = Variant::OBJECT;4507base_type.native_type = base_script->get_instance_base_type();4508base_type.script_type = base_script;4509base_type.script_path = base_script->get_path();45104511return _find_narrowest_native_or_global_class(base_type);4512} break;4513case GDScriptParser::DataType::CLASS: {4514if (p_type.is_meta_type) {4515return GDScript::get_class_static();4516}4517if (p_type.class_type == nullptr) {4518return p_type.native_type;4519}4520if (p_type.class_type->get_global_name() != StringName()) {4521return p_type.class_type->get_global_name();4522}4523return _find_narrowest_native_or_global_class(p_type.class_type->base_type);4524} break;4525default: {4526ERR_FAIL_V(StringName());4527} break;4528}4529}45304531template <PropertyHint t_hint, Variant::Type t_type>4532bool GDScriptParser::export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4533ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));4534ERR_FAIL_NULL_V(p_class, false);45354536VariableNode *variable = static_cast<VariableNode *>(p_target);4537if (variable->is_static) {4538push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4539return false;4540}4541if (variable->exported) {4542push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4543return false;4544}45454546variable->exported = true;45474548variable->export_info.type = t_type;4549variable->export_info.hint = t_hint;45504551String hint_string;4552for (int i = 0; i < p_annotation->resolved_arguments.size(); i++) {4553String arg_string = String(p_annotation->resolved_arguments[i]);45544555if (p_annotation->name != SNAME("@export_placeholder")) {4556if (arg_string.is_empty()) {4557push_error(vformat(R"(Argument %d of annotation "%s" is empty.)", i + 1, p_annotation->name), p_annotation->arguments[i]);4558return false;4559}4560if (arg_string.contains_char(',')) {4561push_error(vformat(R"(Argument %d of annotation "%s" contains a comma. Use separate arguments instead.)", i + 1, p_annotation->name), p_annotation->arguments[i]);4562return false;4563}4564}45654566// WARNING: Do not merge with the previous `if` because there `!=`, not `==`!4567if (p_annotation->name == SNAME("@export_flags")) {4568const int64_t max_flags = 32;4569Vector<String> t = arg_string.split(":", true, 1);4570if (t[0].is_empty()) {4571push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Expected flag name.)", i + 1), p_annotation->arguments[i]);4572return false;4573}4574if (t.size() == 2) {4575if (t[1].is_empty()) {4576push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Expected flag value.)", i + 1), p_annotation->arguments[i]);4577return false;4578}4579if (!t[1].is_valid_int()) {4580push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": The flag value must be a valid integer.)", i + 1), p_annotation->arguments[i]);4581return false;4582}4583int64_t value = t[1].to_int();4584if (value < 1 || value >= (1LL << max_flags)) {4585push_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]);4586return false;4587}4588} else if (i >= max_flags) {4589push_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]);4590return false;4591}4592} else if (p_annotation->name == SNAME("@export_node_path")) {4593String native_class = arg_string;4594if (ScriptServer::is_global_class(arg_string)) {4595native_class = ScriptServer::get_global_class_native_base(arg_string);4596}4597if (!ClassDB::class_exists(native_class)) {4598push_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]);4599return false;4600} else if (!ClassDB::is_parent_class(native_class, SNAME("Node"))) {4601push_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]);4602return false;4603}4604}46054606if (i > 0) {4607hint_string += ",";4608}4609hint_string += arg_string;4610}4611variable->export_info.hint_string = hint_string;46124613// This is called after the analyzer is done finding the type, so this should be set here.4614DataType export_type = variable->get_datatype();46154616// Use initializer type if specified type is `Variant`.4617if (export_type.is_variant() && variable->initializer != nullptr && variable->initializer->datatype.is_set()) {4618export_type = variable->initializer->get_datatype();4619export_type.type_source = DataType::INFERRED;4620}46214622const Variant::Type original_export_type_builtin = export_type.builtin_type;46234624// Process array and packed array annotations on the element type.4625bool is_array = false;4626if (export_type.builtin_type == Variant::ARRAY && export_type.has_container_element_type(0)) {4627is_array = true;4628export_type = export_type.get_container_element_type(0);4629} else if (export_type.is_typed_container_type()) {4630is_array = true;4631export_type = export_type.get_typed_container_type();4632export_type.type_source = variable->datatype.type_source;4633}46344635bool is_dict = false;4636if (export_type.builtin_type == Variant::DICTIONARY && export_type.has_container_element_types()) {4637is_dict = true;4638DataType inner_type = export_type.get_container_element_type_or_variant(1);4639export_type = export_type.get_container_element_type_or_variant(0);4640export_type.set_container_element_type(0, inner_type); // Store earlier extracted value within key to separately parse after.4641}46424643bool use_default_variable_type_check = true;46444645if (p_annotation->name == SNAME("@export_range")) {4646if (export_type.builtin_type == Variant::INT) {4647variable->export_info.type = Variant::INT;4648}4649} else if (p_annotation->name == SNAME("@export_multiline")) {4650use_default_variable_type_check = false;46514652if (export_type.builtin_type != Variant::STRING && export_type.builtin_type != Variant::DICTIONARY) {4653Vector<Variant::Type> expected_types = { Variant::STRING, Variant::DICTIONARY };4654push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation);4655return false;4656}46574658if (export_type.builtin_type == Variant::DICTIONARY) {4659variable->export_info.type = Variant::DICTIONARY;4660}4661} else if (p_annotation->name == SNAME("@export")) {4662use_default_variable_type_check = false;46634664if (variable->datatype_specifier == nullptr && variable->initializer == nullptr) {4665push_error(R"(Cannot use simple "@export" annotation with variable without type or initializer, since type can't be inferred.)", p_annotation);4666return false;4667}46684669if (export_type.has_no_type()) {4670push_error(R"(Cannot use simple "@export" annotation because the type of the initialized value can't be inferred.)", p_annotation);4671return false;4672}46734674switch (export_type.kind) {4675case GDScriptParser::DataType::BUILTIN:4676variable->export_info.type = export_type.builtin_type;4677variable->export_info.hint = PROPERTY_HINT_NONE;4678variable->export_info.hint_string = String();4679break;4680case GDScriptParser::DataType::NATIVE:4681case GDScriptParser::DataType::SCRIPT:4682case GDScriptParser::DataType::CLASS: {4683const StringName class_name = _find_narrowest_native_or_global_class(export_type);4684if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {4685variable->export_info.type = Variant::OBJECT;4686variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;4687variable->export_info.hint_string = class_name;4688} else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {4689variable->export_info.type = Variant::OBJECT;4690variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;4691variable->export_info.hint_string = class_name;4692} else {4693push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4694return false;4695}4696} break;4697case GDScriptParser::DataType::ENUM: {4698if (export_type.is_meta_type) {4699variable->export_info.type = Variant::DICTIONARY;4700} else {4701variable->export_info.type = Variant::INT;4702variable->export_info.hint = PROPERTY_HINT_ENUM;47034704String enum_hint_string;4705bool first = true;4706for (const KeyValue<StringName, int64_t> &E : export_type.enum_values) {4707if (!first) {4708enum_hint_string += ",";4709} else {4710first = false;4711}4712enum_hint_string += E.key.operator String().capitalize().xml_escape();4713enum_hint_string += ":";4714enum_hint_string += String::num_int64(E.value).xml_escape();4715}47164717variable->export_info.hint_string = enum_hint_string;4718variable->export_info.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;4719variable->export_info.class_name = String(export_type.native_type).replace("::", ".");4720}4721} break;4722case GDScriptParser::DataType::VARIANT: {4723if (export_type.is_variant()) {4724variable->export_info.type = Variant::NIL;4725variable->export_info.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;4726}4727} break;4728default:4729push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4730return false;4731}47324733if (variable->export_info.hint == PROPERTY_HINT_NODE_TYPE && !ClassDB::is_parent_class(p_class->base_type.native_type, SNAME("Node"))) {4734push_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);4735return false;4736}47374738if (is_dict) {4739String key_prefix = itos(variable->export_info.type);4740if (variable->export_info.hint) {4741key_prefix += "/" + itos(variable->export_info.hint);4742}4743key_prefix += ":" + variable->export_info.hint_string;47444745// Now parse value.4746export_type = export_type.get_container_element_type(0);47474748if (export_type.is_variant() || export_type.has_no_type()) {4749export_type.kind = GDScriptParser::DataType::BUILTIN;4750}4751switch (export_type.kind) {4752case GDScriptParser::DataType::BUILTIN:4753variable->export_info.type = export_type.builtin_type;4754variable->export_info.hint = PROPERTY_HINT_NONE;4755variable->export_info.hint_string = String();4756break;4757case GDScriptParser::DataType::NATIVE:4758case GDScriptParser::DataType::SCRIPT:4759case GDScriptParser::DataType::CLASS: {4760const StringName class_name = _find_narrowest_native_or_global_class(export_type);4761if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {4762variable->export_info.type = Variant::OBJECT;4763variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;4764variable->export_info.hint_string = class_name;4765} else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {4766variable->export_info.type = Variant::OBJECT;4767variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;4768variable->export_info.hint_string = class_name;4769} else {4770push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4771return false;4772}4773} break;4774case GDScriptParser::DataType::ENUM: {4775if (export_type.is_meta_type) {4776variable->export_info.type = Variant::DICTIONARY;4777} else {4778variable->export_info.type = Variant::INT;4779variable->export_info.hint = PROPERTY_HINT_ENUM;47804781String enum_hint_string;4782bool first = true;4783for (const KeyValue<StringName, int64_t> &E : export_type.enum_values) {4784if (!first) {4785enum_hint_string += ",";4786} else {4787first = false;4788}4789enum_hint_string += E.key.operator String().capitalize().xml_escape();4790enum_hint_string += ":";4791enum_hint_string += String::num_int64(E.value).xml_escape();4792}47934794variable->export_info.hint_string = enum_hint_string;4795variable->export_info.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;4796variable->export_info.class_name = String(export_type.native_type).replace("::", ".");4797}4798} break;4799default:4800push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);4801return false;4802}48034804if (variable->export_info.hint == PROPERTY_HINT_NODE_TYPE && !ClassDB::is_parent_class(p_class->base_type.native_type, SNAME("Node"))) {4805push_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);4806return false;4807}48084809String value_prefix = itos(variable->export_info.type);4810if (variable->export_info.hint) {4811value_prefix += "/" + itos(variable->export_info.hint);4812}4813value_prefix += ":" + variable->export_info.hint_string;48144815variable->export_info.type = Variant::DICTIONARY;4816variable->export_info.hint = PROPERTY_HINT_TYPE_STRING;4817variable->export_info.hint_string = key_prefix + ";" + value_prefix;4818variable->export_info.usage = PROPERTY_USAGE_DEFAULT;4819variable->export_info.class_name = StringName();4820}4821} else if (p_annotation->name == SNAME("@export_enum")) {4822use_default_variable_type_check = false;48234824Variant::Type enum_type = Variant::INT;48254826if (export_type.kind == DataType::BUILTIN && export_type.builtin_type == Variant::STRING) {4827enum_type = Variant::STRING;4828}48294830variable->export_info.type = enum_type;48314832if (!export_type.is_variant() && (export_type.kind != DataType::BUILTIN || export_type.builtin_type != enum_type)) {4833Vector<Variant::Type> expected_types = { Variant::INT, Variant::STRING };4834push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation);4835return false;4836}4837}48384839if (use_default_variable_type_check) {4840// Validate variable type with export.4841if (!export_type.is_variant() && (export_type.kind != DataType::BUILTIN || export_type.builtin_type != t_type)) {4842// Allow float/int conversion.4843if ((t_type != Variant::FLOAT || export_type.builtin_type != Variant::INT) && (t_type != Variant::INT || export_type.builtin_type != Variant::FLOAT)) {4844Vector<Variant::Type> expected_types = { t_type };4845push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation);4846return false;4847}4848}4849}48504851if (is_array) {4852String hint_prefix = itos(variable->export_info.type);4853if (variable->export_info.hint) {4854hint_prefix += "/" + itos(variable->export_info.hint);4855}4856variable->export_info.type = original_export_type_builtin;4857variable->export_info.hint = PROPERTY_HINT_TYPE_STRING;4858variable->export_info.hint_string = hint_prefix + ":" + variable->export_info.hint_string;4859variable->export_info.usage = PROPERTY_USAGE_DEFAULT;4860variable->export_info.class_name = StringName();4861}48624863return true;4864}48654866// For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values,4867// or handle array exports in a special way, so they are implemented as separate methods.48684869bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4870ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));48714872VariableNode *variable = static_cast<VariableNode *>(p_target);4873if (variable->is_static) {4874push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4875return false;4876}4877if (variable->exported) {4878push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4879return false;4880}48814882variable->exported = true;48834884// Save the info because the compiler uses export info for overwriting member info.4885variable->export_info = variable->get_datatype().to_property_info(variable->identifier->name);4886variable->export_info.usage |= PROPERTY_USAGE_STORAGE;48874888return true;4889}48904891bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4892ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));4893ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)");48944895VariableNode *variable = static_cast<VariableNode *>(p_target);4896if (variable->is_static) {4897push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4898return false;4899}4900if (variable->exported) {4901push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4902return false;4903}49044905variable->exported = true;49064907DataType export_type = variable->get_datatype();49084909variable->export_info.type = export_type.builtin_type;4910variable->export_info.hint = static_cast<PropertyHint>(p_annotation->resolved_arguments[0].operator int64_t());4911variable->export_info.hint_string = p_annotation->resolved_arguments[1];49124913if (p_annotation->resolved_arguments.size() >= 3) {4914variable->export_info.usage = p_annotation->resolved_arguments[2].operator int64_t();4915}4916return true;4917}49184919bool GDScriptParser::export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4920#ifdef TOOLS_ENABLED4921ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name));4922ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);49234924if (!is_tool()) {4925push_error(R"(Tool buttons can only be used in tool scripts (add "@tool" to the top of the script).)", p_annotation);4926return false;4927}49284929VariableNode *variable = static_cast<VariableNode *>(p_target);49304931if (variable->is_static) {4932push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation);4933return false;4934}4935if (variable->exported) {4936push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation);4937return false;4938}49394940const DataType variable_type = variable->get_datatype();4941if (!variable_type.is_variant() && variable_type.is_hard_type()) {4942if (variable_type.kind != DataType::BUILTIN || variable_type.builtin_type != Variant::CALLABLE) {4943push_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);4944return false;4945}4946}49474948variable->exported = true;49494950// Build the hint string (format: `<text>[,<icon>]`).4951String hint_string = p_annotation->resolved_arguments[0].operator String(); // Button text.4952if (p_annotation->resolved_arguments.size() > 1) {4953hint_string += "," + p_annotation->resolved_arguments[1].operator String(); // Button icon.4954}49554956variable->export_info.type = Variant::CALLABLE;4957variable->export_info.hint = PROPERTY_HINT_TOOL_BUTTON;4958variable->export_info.hint_string = hint_string;4959variable->export_info.usage = PROPERTY_USAGE_EDITOR;4960#endif // TOOLS_ENABLED49614962return true; // Only available in editor.4963}49644965template <PropertyUsageFlags t_usage>4966bool GDScriptParser::export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4967ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);49684969p_annotation->export_info.name = p_annotation->resolved_arguments[0];49704971switch (t_usage) {4972case PROPERTY_USAGE_CATEGORY: {4973p_annotation->export_info.usage = t_usage;4974} break;49754976case PROPERTY_USAGE_GROUP: {4977p_annotation->export_info.usage = t_usage;4978if (p_annotation->resolved_arguments.size() == 2) {4979p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1];4980}4981} break;49824983case PROPERTY_USAGE_SUBGROUP: {4984p_annotation->export_info.usage = t_usage;4985if (p_annotation->resolved_arguments.size() == 2) {4986p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1];4987}4988} break;4989}49904991return true;4992}49934994bool GDScriptParser::warning_ignore_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {4995#ifdef DEBUG_ENABLED4996if (is_ignoring_warnings) {4997return true; // We already ignore all warnings, let's optimize it.4998}49995000bool has_error = false;5001for (const Variant &warning_name : p_annotation->resolved_arguments) {5002GDScriptWarning::Code warning_code = GDScriptWarning::get_code_from_name(String(warning_name).to_upper());5003if (warning_code == GDScriptWarning::WARNING_MAX) {5004push_error(vformat(R"(Invalid warning name: "%s".)", warning_name), p_annotation);5005has_error = true;5006} else {5007int start_line = p_annotation->start_line;5008int end_line = p_target->end_line;50095010switch (p_target->type) {5011#define SIMPLE_CASE(m_type, m_class, m_property) \5012case m_type: { \5013m_class *node = static_cast<m_class *>(p_target); \5014if (node->m_property == nullptr) { \5015end_line = node->start_line; \5016} else { \5017end_line = node->m_property->end_line; \5018} \5019} break;50205021// Can contain properties (set/get).5022SIMPLE_CASE(Node::VARIABLE, VariableNode, initializer)50235024// Contain bodies.5025SIMPLE_CASE(Node::FOR, ForNode, list)5026SIMPLE_CASE(Node::IF, IfNode, condition)5027SIMPLE_CASE(Node::MATCH, MatchNode, test)5028SIMPLE_CASE(Node::WHILE, WhileNode, condition)5029#undef SIMPLE_CASE50305031case Node::CLASS: {5032end_line = p_target->start_line;5033for (const AnnotationNode *annotation : p_target->annotations) {5034start_line = MIN(start_line, annotation->start_line);5035end_line = MAX(end_line, annotation->end_line);5036}5037} break;50385039case Node::FUNCTION: {5040FunctionNode *function = static_cast<FunctionNode *>(p_target);5041end_line = function->start_line;5042for (int i = 0; i < function->parameters.size(); i++) {5043end_line = MAX(end_line, function->parameters[i]->end_line);5044if (function->parameters[i]->initializer != nullptr) {5045end_line = MAX(end_line, function->parameters[i]->initializer->end_line);5046}5047}5048} break;50495050case Node::MATCH_BRANCH: {5051MatchBranchNode *branch = static_cast<MatchBranchNode *>(p_target);5052end_line = branch->start_line;5053for (int i = 0; i < branch->patterns.size(); i++) {5054end_line = MAX(end_line, branch->patterns[i]->end_line);5055}5056} break;50575058default: {5059} break;5060}50615062end_line = MAX(start_line, end_line); // Prevent infinite loop.5063for (int line = start_line; line <= end_line; line++) {5064warning_ignored_lines[warning_code].insert(line);5065}5066}5067}5068return !has_error;5069#else // !DEBUG_ENABLED5070// Only available in debug builds.5071return true;5072#endif // DEBUG_ENABLED5073}50745075bool GDScriptParser::warning_ignore_region_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {5076#ifdef DEBUG_ENABLED5077bool has_error = false;5078const bool is_start = p_annotation->name == SNAME("@warning_ignore_start");5079for (const Variant &warning_name : p_annotation->resolved_arguments) {5080GDScriptWarning::Code warning_code = GDScriptWarning::get_code_from_name(String(warning_name).to_upper());5081if (warning_code == GDScriptWarning::WARNING_MAX) {5082push_error(vformat(R"(Invalid warning name: "%s".)", warning_name), p_annotation);5083has_error = true;5084continue;5085}5086if (is_start) {5087if (warning_ignore_start_lines[warning_code] != INT_MAX) {5088push_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);5089has_error = true;5090continue;5091}5092warning_ignore_start_lines[warning_code] = p_annotation->start_line;5093} else {5094if (warning_ignore_start_lines[warning_code] == INT_MAX) {5095push_error(vformat(R"(Warning "%s" is not being ignored by "@warning_ignore_start".)", String(warning_name).to_upper()), p_annotation);5096has_error = true;5097continue;5098}5099const int start_line = warning_ignore_start_lines[warning_code];5100const int end_line = MAX(start_line, p_annotation->start_line); // Prevent infinite loop.5101for (int i = start_line; i <= end_line; i++) {5102warning_ignored_lines[warning_code].insert(i);5103}5104warning_ignore_start_lines[warning_code] = INT_MAX;5105}5106}5107return !has_error;5108#else // !DEBUG_ENABLED5109// Only available in debug builds.5110return true;5111#endif // DEBUG_ENABLED5112}51135114bool GDScriptParser::rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) {5115ERR_FAIL_COND_V_MSG(p_target->type != Node::FUNCTION, false, vformat(R"("%s" annotation can only be applied to functions.)", p_annotation->name));51165117FunctionNode *function = static_cast<FunctionNode *>(p_target);5118if (function->rpc_config.get_type() != Variant::NIL) {5119push_error(R"(RPC annotations can only be used once per function.)", p_annotation);5120return false;5121}51225123Dictionary rpc_config;5124rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;5125if (!p_annotation->resolved_arguments.is_empty()) {5126unsigned char locality_args = 0;5127unsigned char permission_args = 0;5128unsigned char transfer_mode_args = 0;51295130for (int i = 0; i < p_annotation->resolved_arguments.size(); i++) {5131if (i == 3) {5132rpc_config["channel"] = p_annotation->resolved_arguments[i].operator int();5133continue;5134}51355136String arg = p_annotation->resolved_arguments[i].operator String();5137if (arg == "call_local") {5138locality_args++;5139rpc_config["call_local"] = true;5140} else if (arg == "call_remote") {5141locality_args++;5142rpc_config["call_local"] = false;5143} else if (arg == "any_peer") {5144permission_args++;5145rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;5146} else if (arg == "authority") {5147permission_args++;5148rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;5149} else if (arg == "reliable") {5150transfer_mode_args++;5151rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;5152} else if (arg == "unreliable") {5153transfer_mode_args++;5154rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_UNRELIABLE;5155} else if (arg == "unreliable_ordered") {5156transfer_mode_args++;5157rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_UNRELIABLE_ORDERED;5158} else {5159push_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);5160}5161}51625163if (locality_args > 1) {5164push_error(R"(Invalid RPC config. The locality ("call_local"/"call_remote") must be specified no more than once.)", p_annotation);5165} else if (permission_args > 1) {5166push_error(R"(Invalid RPC config. The permission ("any_peer"/"authority") must be specified no more than once.)", p_annotation);5167} else if (transfer_mode_args > 1) {5168push_error(R"(Invalid RPC config. The transfer mode ("reliable"/"unreliable"/"unreliable_ordered") must be specified no more than once.)", p_annotation);5169}5170}5171function->rpc_config = rpc_config;5172return true;5173}51745175GDScriptParser::DataType GDScriptParser::SuiteNode::Local::get_datatype() const {5176switch (type) {5177case CONSTANT:5178return constant->get_datatype();5179case VARIABLE:5180return variable->get_datatype();5181case PARAMETER:5182return parameter->get_datatype();5183case FOR_VARIABLE:5184case PATTERN_BIND:5185return bind->get_datatype();5186case UNDEFINED:5187return DataType();5188}5189return DataType();5190}51915192String GDScriptParser::SuiteNode::Local::get_name() const {5193switch (type) {5194case SuiteNode::Local::PARAMETER:5195return "parameter";5196case SuiteNode::Local::CONSTANT:5197return "constant";5198case SuiteNode::Local::VARIABLE:5199return "variable";5200case SuiteNode::Local::FOR_VARIABLE:5201return "for loop iterator";5202case SuiteNode::Local::PATTERN_BIND:5203return "pattern bind";5204case SuiteNode::Local::UNDEFINED:5205return "<undefined>";5206default:5207return String();5208}5209}52105211String GDScriptParser::DataType::to_string() const {5212switch (kind) {5213case VARIANT:5214return "Variant";5215case BUILTIN:5216if (builtin_type == Variant::NIL) {5217return "null";5218}5219if (builtin_type == Variant::ARRAY && has_container_element_type(0)) {5220return vformat("Array[%s]", get_container_element_type(0).to_string());5221}5222if (builtin_type == Variant::DICTIONARY && has_container_element_types()) {5223return vformat("Dictionary[%s, %s]", get_container_element_type_or_variant(0).to_string(), get_container_element_type_or_variant(1).to_string());5224}5225return Variant::get_type_name(builtin_type);5226case NATIVE:5227if (is_meta_type) {5228return GDScriptNativeClass::get_class_static();5229}5230return native_type.operator String();5231case CLASS:5232if (class_type->identifier != nullptr) {5233return class_type->identifier->name.operator String();5234}5235return class_type->fqcn;5236case SCRIPT: {5237if (is_meta_type) {5238return script_type.is_valid() ? script_type->get_class_name().operator String() : "";5239}5240String name = script_type.is_valid() ? script_type->get_name() : "";5241if (!name.is_empty()) {5242return name;5243}5244name = script_path;5245if (!name.is_empty()) {5246return name;5247}5248return native_type.operator String();5249}5250case ENUM: {5251// native_type contains either the native class defining the enum5252// or the fully qualified class name of the script defining the enum5253return String(native_type).get_file(); // Remove path, keep filename5254}5255case RESOLVING:5256case UNRESOLVED:5257return "<unresolved type>";5258}52595260ERR_FAIL_V_MSG("<unresolved type>", "Kind set outside the enum range.");5261}52625263PropertyInfo GDScriptParser::DataType::to_property_info(const String &p_name) const {5264PropertyInfo result;5265result.name = p_name;5266result.usage = PROPERTY_USAGE_NONE;52675268if (!is_hard_type()) {5269result.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;5270return result;5271}52725273switch (kind) {5274case BUILTIN:5275result.type = builtin_type;5276if (builtin_type == Variant::ARRAY && has_container_element_type(0)) {5277const DataType elem_type = get_container_element_type(0);5278switch (elem_type.kind) {5279case BUILTIN:5280result.hint = PROPERTY_HINT_ARRAY_TYPE;5281result.hint_string = Variant::get_type_name(elem_type.builtin_type);5282break;5283case NATIVE:5284result.hint = PROPERTY_HINT_ARRAY_TYPE;5285result.hint_string = elem_type.native_type;5286break;5287case SCRIPT:5288result.hint = PROPERTY_HINT_ARRAY_TYPE;5289if (elem_type.script_type.is_valid() && elem_type.script_type->get_global_name() != StringName()) {5290result.hint_string = elem_type.script_type->get_global_name();5291} else {5292result.hint_string = elem_type.native_type;5293}5294break;5295case CLASS:5296result.hint = PROPERTY_HINT_ARRAY_TYPE;5297if (elem_type.class_type != nullptr && elem_type.class_type->get_global_name() != StringName()) {5298result.hint_string = elem_type.class_type->get_global_name();5299} else {5300result.hint_string = elem_type.native_type;5301}5302break;5303case ENUM:5304result.hint = PROPERTY_HINT_ARRAY_TYPE;5305result.hint_string = String(elem_type.native_type).replace("::", ".");5306break;5307case VARIANT:5308case RESOLVING:5309case UNRESOLVED:5310break;5311}5312} else if (builtin_type == Variant::DICTIONARY && has_container_element_types()) {5313const DataType key_type = get_container_element_type_or_variant(0);5314const DataType value_type = get_container_element_type_or_variant(1);5315if ((key_type.kind == VARIANT && value_type.kind == VARIANT) || key_type.kind == RESOLVING ||5316key_type.kind == UNRESOLVED || value_type.kind == RESOLVING || value_type.kind == UNRESOLVED) {5317break;5318}5319String key_hint, value_hint;5320switch (key_type.kind) {5321case BUILTIN:5322key_hint = Variant::get_type_name(key_type.builtin_type);5323break;5324case NATIVE:5325key_hint = key_type.native_type;5326break;5327case SCRIPT:5328if (key_type.script_type.is_valid() && key_type.script_type->get_global_name() != StringName()) {5329key_hint = key_type.script_type->get_global_name();5330} else {5331key_hint = key_type.native_type;5332}5333break;5334case CLASS:5335if (key_type.class_type != nullptr && key_type.class_type->get_global_name() != StringName()) {5336key_hint = key_type.class_type->get_global_name();5337} else {5338key_hint = key_type.native_type;5339}5340break;5341case ENUM:5342key_hint = String(key_type.native_type).replace("::", ".");5343break;5344default:5345key_hint = "Variant";5346break;5347}5348switch (value_type.kind) {5349case BUILTIN:5350value_hint = Variant::get_type_name(value_type.builtin_type);5351break;5352case NATIVE:5353value_hint = value_type.native_type;5354break;5355case SCRIPT:5356if (value_type.script_type.is_valid() && value_type.script_type->get_global_name() != StringName()) {5357value_hint = value_type.script_type->get_global_name();5358} else {5359value_hint = value_type.native_type;5360}5361break;5362case CLASS:5363if (value_type.class_type != nullptr && value_type.class_type->get_global_name() != StringName()) {5364value_hint = value_type.class_type->get_global_name();5365} else {5366value_hint = value_type.native_type;5367}5368break;5369case ENUM:5370value_hint = String(value_type.native_type).replace("::", ".");5371break;5372default:5373value_hint = "Variant";5374break;5375}5376result.hint = PROPERTY_HINT_DICTIONARY_TYPE;5377result.hint_string = key_hint + ";" + value_hint;5378}5379break;5380case NATIVE:5381result.type = Variant::OBJECT;5382if (is_meta_type) {5383result.class_name = GDScriptNativeClass::get_class_static();5384} else {5385result.class_name = native_type;5386}5387break;5388case SCRIPT:5389result.type = Variant::OBJECT;5390if (is_meta_type) {5391result.class_name = script_type.is_valid() ? script_type->get_class_name() : Script::get_class_static();5392} else if (script_type.is_valid() && script_type->get_global_name() != StringName()) {5393result.class_name = script_type->get_global_name();5394} else {5395result.class_name = native_type;5396}5397break;5398case CLASS:5399result.type = Variant::OBJECT;5400if (is_meta_type) {5401result.class_name = GDScript::get_class_static();5402} else if (class_type != nullptr && class_type->get_global_name() != StringName()) {5403result.class_name = class_type->get_global_name();5404} else {5405result.class_name = native_type;5406}5407break;5408case ENUM:5409if (is_meta_type) {5410result.type = Variant::DICTIONARY;5411} else {5412result.type = Variant::INT;5413result.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;5414result.class_name = String(native_type).replace("::", ".");5415}5416break;5417case VARIANT:5418case RESOLVING:5419case UNRESOLVED:5420result.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;5421break;5422}54235424return result;5425}54265427static Variant::Type _variant_type_to_typed_array_element_type(Variant::Type p_type) {5428switch (p_type) {5429case Variant::PACKED_BYTE_ARRAY:5430case Variant::PACKED_INT32_ARRAY:5431case Variant::PACKED_INT64_ARRAY:5432return Variant::INT;5433case Variant::PACKED_FLOAT32_ARRAY:5434case Variant::PACKED_FLOAT64_ARRAY:5435return Variant::FLOAT;5436case Variant::PACKED_STRING_ARRAY:5437return Variant::STRING;5438case Variant::PACKED_VECTOR2_ARRAY:5439return Variant::VECTOR2;5440case Variant::PACKED_VECTOR3_ARRAY:5441return Variant::VECTOR3;5442case Variant::PACKED_COLOR_ARRAY:5443return Variant::COLOR;5444case Variant::PACKED_VECTOR4_ARRAY:5445return Variant::VECTOR4;5446default:5447return Variant::NIL;5448}5449}54505451bool GDScriptParser::DataType::is_typed_container_type() const {5452return kind == GDScriptParser::DataType::BUILTIN && _variant_type_to_typed_array_element_type(builtin_type) != Variant::NIL;5453}54545455GDScriptParser::DataType GDScriptParser::DataType::get_typed_container_type() const {5456GDScriptParser::DataType type;5457type.kind = GDScriptParser::DataType::BUILTIN;5458type.builtin_type = _variant_type_to_typed_array_element_type(builtin_type);5459return type;5460}54615462bool GDScriptParser::DataType::can_reference(const GDScriptParser::DataType &p_other) const {5463if (p_other.is_meta_type) {5464return false;5465} else if (builtin_type != p_other.builtin_type) {5466return false;5467} else if (builtin_type != Variant::OBJECT) {5468return true;5469}54705471if (native_type == StringName()) {5472return true;5473} else if (p_other.native_type == StringName()) {5474return false;5475} else if (native_type != p_other.native_type && !ClassDB::is_parent_class(p_other.native_type, native_type)) {5476return false;5477}54785479Ref<Script> script = script_type;5480if (kind == GDScriptParser::DataType::CLASS && script.is_null()) {5481Error err = OK;5482Ref<GDScript> scr = GDScriptCache::get_shallow_script(script_path, err);5483ERR_FAIL_COND_V_MSG(err, false, vformat(R"(Error while getting cache for script "%s".)", script_path));5484script.reference_ptr(scr->find_class(class_type->fqcn));5485}54865487Ref<Script> script_other = p_other.script_type;5488if (p_other.kind == GDScriptParser::DataType::CLASS && script_other.is_null()) {5489Error err = OK;5490Ref<GDScript> scr = GDScriptCache::get_shallow_script(p_other.script_path, err);5491ERR_FAIL_COND_V_MSG(err, false, vformat(R"(Error while getting cache for script "%s".)", p_other.script_path));5492script_other.reference_ptr(scr->find_class(p_other.class_type->fqcn));5493}54945495if (script.is_null()) {5496return true;5497} else if (script_other.is_null()) {5498return false;5499} else if (script != script_other && !script_other->inherits_script(script)) {5500return false;5501}55025503return true;5504}55055506void GDScriptParser::complete_extents(Node *p_node) {5507while (!nodes_in_progress.is_empty() && nodes_in_progress.back()->get() != p_node) {5508ERR_PRINT("Parser bug: Mismatch in extents tracking stack.");5509nodes_in_progress.pop_back();5510}5511if (nodes_in_progress.is_empty()) {5512ERR_PRINT("Parser bug: Extents tracking stack is empty.");5513} else {5514nodes_in_progress.pop_back();5515}5516}55175518void GDScriptParser::update_extents(Node *p_node) {5519p_node->end_line = previous.end_line;5520p_node->end_column = previous.end_column;5521}55225523void GDScriptParser::reset_extents(Node *p_node, GDScriptTokenizer::Token p_token) {5524p_node->start_line = p_token.start_line;5525p_node->end_line = p_token.end_line;5526p_node->start_column = p_token.start_column;5527p_node->end_column = p_token.end_column;5528}55295530void GDScriptParser::reset_extents(Node *p_node, Node *p_from) {5531if (p_from == nullptr) {5532return;5533}5534p_node->start_line = p_from->start_line;5535p_node->end_line = p_from->end_line;5536p_node->start_column = p_from->start_column;5537p_node->end_column = p_from->end_column;5538}55395540/*---------- PRETTY PRINT FOR DEBUG ----------*/55415542#ifdef DEBUG_ENABLED55435544void GDScriptParser::TreePrinter::increase_indent() {5545indent_level++;5546indent = "";5547for (int i = 0; i < indent_level * 4; i++) {5548if (i % 4 == 0) {5549indent += "|";5550} else {5551indent += " ";5552}5553}5554}55555556void GDScriptParser::TreePrinter::decrease_indent() {5557indent_level--;5558indent = "";5559for (int i = 0; i < indent_level * 4; i++) {5560if (i % 4 == 0) {5561indent += "|";5562} else {5563indent += " ";5564}5565}5566}55675568void GDScriptParser::TreePrinter::push_line(const String &p_line) {5569if (!p_line.is_empty()) {5570push_text(p_line);5571}5572printed += "\n";5573pending_indent = true;5574}55755576void GDScriptParser::TreePrinter::push_text(const String &p_text) {5577if (pending_indent) {5578printed += indent;5579pending_indent = false;5580}5581printed += p_text;5582}55835584void GDScriptParser::TreePrinter::print_annotation(const AnnotationNode *p_annotation) {5585push_text(p_annotation->name);5586push_text(" (");5587for (int i = 0; i < p_annotation->arguments.size(); i++) {5588if (i > 0) {5589push_text(" , ");5590}5591print_expression(p_annotation->arguments[i]);5592}5593push_line(")");5594}55955596void GDScriptParser::TreePrinter::print_array(ArrayNode *p_array) {5597push_text("[ ");5598for (int i = 0; i < p_array->elements.size(); i++) {5599if (i > 0) {5600push_text(" , ");5601}5602print_expression(p_array->elements[i]);5603}5604push_text(" ]");5605}56065607void GDScriptParser::TreePrinter::print_assert(AssertNode *p_assert) {5608push_text("Assert ( ");5609print_expression(p_assert->condition);5610push_line(" )");5611}56125613void GDScriptParser::TreePrinter::print_assignment(AssignmentNode *p_assignment) {5614switch (p_assignment->assignee->type) {5615case Node::IDENTIFIER:5616print_identifier(static_cast<IdentifierNode *>(p_assignment->assignee));5617break;5618case Node::SUBSCRIPT:5619print_subscript(static_cast<SubscriptNode *>(p_assignment->assignee));5620break;5621default:5622break; // Unreachable.5623}56245625push_text(" ");5626switch (p_assignment->operation) {5627case AssignmentNode::OP_ADDITION:5628push_text("+");5629break;5630case AssignmentNode::OP_SUBTRACTION:5631push_text("-");5632break;5633case AssignmentNode::OP_MULTIPLICATION:5634push_text("*");5635break;5636case AssignmentNode::OP_DIVISION:5637push_text("/");5638break;5639case AssignmentNode::OP_MODULO:5640push_text("%");5641break;5642case AssignmentNode::OP_POWER:5643push_text("**");5644break;5645case AssignmentNode::OP_BIT_SHIFT_LEFT:5646push_text("<<");5647break;5648case AssignmentNode::OP_BIT_SHIFT_RIGHT:5649push_text(">>");5650break;5651case AssignmentNode::OP_BIT_AND:5652push_text("&");5653break;5654case AssignmentNode::OP_BIT_OR:5655push_text("|");5656break;5657case AssignmentNode::OP_BIT_XOR:5658push_text("^");5659break;5660case AssignmentNode::OP_NONE:5661break;5662}5663push_text("= ");5664print_expression(p_assignment->assigned_value);5665push_line();5666}56675668void GDScriptParser::TreePrinter::print_await(AwaitNode *p_await) {5669push_text("Await ");5670print_expression(p_await->to_await);5671}56725673void GDScriptParser::TreePrinter::print_binary_op(BinaryOpNode *p_binary_op) {5674// Surround in parenthesis for disambiguation.5675push_text("(");5676print_expression(p_binary_op->left_operand);5677switch (p_binary_op->operation) {5678case BinaryOpNode::OP_ADDITION:5679push_text(" + ");5680break;5681case BinaryOpNode::OP_SUBTRACTION:5682push_text(" - ");5683break;5684case BinaryOpNode::OP_MULTIPLICATION:5685push_text(" * ");5686break;5687case BinaryOpNode::OP_DIVISION:5688push_text(" / ");5689break;5690case BinaryOpNode::OP_MODULO:5691push_text(" % ");5692break;5693case BinaryOpNode::OP_POWER:5694push_text(" ** ");5695break;5696case BinaryOpNode::OP_BIT_LEFT_SHIFT:5697push_text(" << ");5698break;5699case BinaryOpNode::OP_BIT_RIGHT_SHIFT:5700push_text(" >> ");5701break;5702case BinaryOpNode::OP_BIT_AND:5703push_text(" & ");5704break;5705case BinaryOpNode::OP_BIT_OR:5706push_text(" | ");5707break;5708case BinaryOpNode::OP_BIT_XOR:5709push_text(" ^ ");5710break;5711case BinaryOpNode::OP_LOGIC_AND:5712push_text(" AND ");5713break;5714case BinaryOpNode::OP_LOGIC_OR:5715push_text(" OR ");5716break;5717case BinaryOpNode::OP_CONTENT_TEST:5718push_text(" IN ");5719break;5720case BinaryOpNode::OP_COMP_EQUAL:5721push_text(" == ");5722break;5723case BinaryOpNode::OP_COMP_NOT_EQUAL:5724push_text(" != ");5725break;5726case BinaryOpNode::OP_COMP_LESS:5727push_text(" < ");5728break;5729case BinaryOpNode::OP_COMP_LESS_EQUAL:5730push_text(" <= ");5731break;5732case BinaryOpNode::OP_COMP_GREATER:5733push_text(" > ");5734break;5735case BinaryOpNode::OP_COMP_GREATER_EQUAL:5736push_text(" >= ");5737break;5738}5739print_expression(p_binary_op->right_operand);5740// Surround in parenthesis for disambiguation.5741push_text(")");5742}57435744void GDScriptParser::TreePrinter::print_call(CallNode *p_call) {5745if (p_call->is_super) {5746push_text("super");5747if (p_call->callee != nullptr) {5748push_text(".");5749print_expression(p_call->callee);5750}5751} else {5752print_expression(p_call->callee);5753}5754push_text("( ");5755for (int i = 0; i < p_call->arguments.size(); i++) {5756if (i > 0) {5757push_text(" , ");5758}5759print_expression(p_call->arguments[i]);5760}5761push_text(" )");5762}57635764void GDScriptParser::TreePrinter::print_cast(CastNode *p_cast) {5765print_expression(p_cast->operand);5766push_text(" AS ");5767print_type(p_cast->cast_type);5768}57695770void GDScriptParser::TreePrinter::print_class(ClassNode *p_class) {5771for (const AnnotationNode *E : p_class->annotations) {5772print_annotation(E);5773}5774push_text("Class ");5775if (p_class->identifier == nullptr) {5776push_text("<unnamed>");5777} else {5778print_identifier(p_class->identifier);5779}57805781if (p_class->extends_used) {5782bool first = true;5783push_text(" Extends ");5784if (!p_class->extends_path.is_empty()) {5785push_text(vformat(R"("%s")", p_class->extends_path));5786first = false;5787}5788for (int i = 0; i < p_class->extends.size(); i++) {5789if (!first) {5790push_text(".");5791} else {5792first = false;5793}5794push_text(p_class->extends[i]->name);5795}5796}57975798push_line(" :");57995800increase_indent();58015802for (int i = 0; i < p_class->members.size(); i++) {5803const ClassNode::Member &m = p_class->members[i];58045805switch (m.type) {5806case ClassNode::Member::CLASS:5807print_class(m.m_class);5808break;5809case ClassNode::Member::VARIABLE:5810print_variable(m.variable);5811break;5812case ClassNode::Member::CONSTANT:5813print_constant(m.constant);5814break;5815case ClassNode::Member::SIGNAL:5816print_signal(m.signal);5817break;5818case ClassNode::Member::FUNCTION:5819print_function(m.function);5820break;5821case ClassNode::Member::ENUM:5822print_enum(m.m_enum);5823break;5824case ClassNode::Member::ENUM_VALUE:5825break; // Nothing. Will be printed by enum.5826case ClassNode::Member::GROUP:5827break; // Nothing. Groups are only used by inspector.5828case ClassNode::Member::UNDEFINED:5829push_line("<unknown member>");5830break;5831}5832}58335834decrease_indent();5835}58365837void GDScriptParser::TreePrinter::print_constant(ConstantNode *p_constant) {5838push_text("Constant ");5839print_identifier(p_constant->identifier);58405841increase_indent();58425843push_line();5844push_text("= ");5845if (p_constant->initializer == nullptr) {5846push_text("<missing value>");5847} else {5848print_expression(p_constant->initializer);5849}5850decrease_indent();5851push_line();5852}58535854void GDScriptParser::TreePrinter::print_dictionary(DictionaryNode *p_dictionary) {5855push_line("{");5856increase_indent();5857for (int i = 0; i < p_dictionary->elements.size(); i++) {5858print_expression(p_dictionary->elements[i].key);5859if (p_dictionary->style == DictionaryNode::PYTHON_DICT) {5860push_text(" : ");5861} else {5862push_text(" = ");5863}5864print_expression(p_dictionary->elements[i].value);5865push_line(" ,");5866}5867decrease_indent();5868push_text("}");5869}58705871void GDScriptParser::TreePrinter::print_expression(ExpressionNode *p_expression) {5872if (p_expression == nullptr) {5873push_text("<invalid expression>");5874return;5875}5876switch (p_expression->type) {5877case Node::ARRAY:5878print_array(static_cast<ArrayNode *>(p_expression));5879break;5880case Node::ASSIGNMENT:5881print_assignment(static_cast<AssignmentNode *>(p_expression));5882break;5883case Node::AWAIT:5884print_await(static_cast<AwaitNode *>(p_expression));5885break;5886case Node::BINARY_OPERATOR:5887print_binary_op(static_cast<BinaryOpNode *>(p_expression));5888break;5889case Node::CALL:5890print_call(static_cast<CallNode *>(p_expression));5891break;5892case Node::CAST:5893print_cast(static_cast<CastNode *>(p_expression));5894break;5895case Node::DICTIONARY:5896print_dictionary(static_cast<DictionaryNode *>(p_expression));5897break;5898case Node::GET_NODE:5899print_get_node(static_cast<GetNodeNode *>(p_expression));5900break;5901case Node::IDENTIFIER:5902print_identifier(static_cast<IdentifierNode *>(p_expression));5903break;5904case Node::LAMBDA:5905print_lambda(static_cast<LambdaNode *>(p_expression));5906break;5907case Node::LITERAL:5908print_literal(static_cast<LiteralNode *>(p_expression));5909break;5910case Node::PRELOAD:5911print_preload(static_cast<PreloadNode *>(p_expression));5912break;5913case Node::SELF:5914print_self(static_cast<SelfNode *>(p_expression));5915break;5916case Node::SUBSCRIPT:5917print_subscript(static_cast<SubscriptNode *>(p_expression));5918break;5919case Node::TERNARY_OPERATOR:5920print_ternary_op(static_cast<TernaryOpNode *>(p_expression));5921break;5922case Node::TYPE_TEST:5923print_type_test(static_cast<TypeTestNode *>(p_expression));5924break;5925case Node::UNARY_OPERATOR:5926print_unary_op(static_cast<UnaryOpNode *>(p_expression));5927break;5928default:5929push_text(vformat("<unknown expression %d>", p_expression->type));5930break;5931}5932}59335934void GDScriptParser::TreePrinter::print_enum(EnumNode *p_enum) {5935push_text("Enum ");5936if (p_enum->identifier != nullptr) {5937print_identifier(p_enum->identifier);5938} else {5939push_text("<unnamed>");5940}59415942push_line(" {");5943increase_indent();5944for (int i = 0; i < p_enum->values.size(); i++) {5945const EnumNode::Value &item = p_enum->values[i];5946print_identifier(item.identifier);5947push_text(" = ");5948push_text(itos(item.value));5949push_line(" ,");5950}5951decrease_indent();5952push_line("}");5953}59545955void GDScriptParser::TreePrinter::print_for(ForNode *p_for) {5956push_text("For ");5957print_identifier(p_for->variable);5958push_text(" IN ");5959print_expression(p_for->list);5960push_line(" :");59615962increase_indent();59635964print_suite(p_for->loop);59655966decrease_indent();5967}59685969void GDScriptParser::TreePrinter::print_function(FunctionNode *p_function, const String &p_context) {5970for (const AnnotationNode *E : p_function->annotations) {5971print_annotation(E);5972}5973if (p_function->is_static) {5974push_text("Static ");5975}5976push_text(p_context);5977push_text(" ");5978if (p_function->identifier) {5979print_identifier(p_function->identifier);5980} else {5981push_text("<anonymous>");5982}5983push_text("( ");5984for (int i = 0; i < p_function->parameters.size(); i++) {5985if (i > 0) {5986push_text(" , ");5987}5988print_parameter(p_function->parameters[i]);5989}5990push_line(" ) :");5991increase_indent();5992print_suite(p_function->body);5993decrease_indent();5994}59955996void GDScriptParser::TreePrinter::print_get_node(GetNodeNode *p_get_node) {5997if (p_get_node->use_dollar) {5998push_text("$");5999}6000push_text(p_get_node->full_path);6001}60026003void GDScriptParser::TreePrinter::print_identifier(IdentifierNode *p_identifier) {6004if (p_identifier != nullptr) {6005push_text(p_identifier->name);6006} else {6007push_text("<invalid identifier>");6008}6009}60106011void GDScriptParser::TreePrinter::print_if(IfNode *p_if, bool p_is_elif) {6012if (p_is_elif) {6013push_text("Elif ");6014} else {6015push_text("If ");6016}6017print_expression(p_if->condition);6018push_line(" :");60196020increase_indent();6021print_suite(p_if->true_block);6022decrease_indent();60236024// FIXME: Properly detect "elif" blocks.6025if (p_if->false_block != nullptr) {6026push_line("Else :");6027increase_indent();6028print_suite(p_if->false_block);6029decrease_indent();6030}6031}60326033void GDScriptParser::TreePrinter::print_lambda(LambdaNode *p_lambda) {6034print_function(p_lambda->function, "Lambda");6035push_text("| captures [ ");6036for (int i = 0; i < p_lambda->captures.size(); i++) {6037if (i > 0) {6038push_text(" , ");6039}6040push_text(p_lambda->captures[i]->name.operator String());6041}6042push_line(" ]");6043}60446045void GDScriptParser::TreePrinter::print_literal(LiteralNode *p_literal) {6046// Prefix for string types.6047switch (p_literal->value.get_type()) {6048case Variant::NODE_PATH:6049push_text("^\"");6050break;6051case Variant::STRING:6052push_text("\"");6053break;6054case Variant::STRING_NAME:6055push_text("&\"");6056break;6057default:6058break;6059}6060push_text(p_literal->value);6061// Suffix for string types.6062switch (p_literal->value.get_type()) {6063case Variant::NODE_PATH:6064case Variant::STRING:6065case Variant::STRING_NAME:6066push_text("\"");6067break;6068default:6069break;6070}6071}60726073void GDScriptParser::TreePrinter::print_match(MatchNode *p_match) {6074push_text("Match ");6075print_expression(p_match->test);6076push_line(" :");60776078increase_indent();6079for (int i = 0; i < p_match->branches.size(); i++) {6080print_match_branch(p_match->branches[i]);6081}6082decrease_indent();6083}60846085void GDScriptParser::TreePrinter::print_match_branch(MatchBranchNode *p_match_branch) {6086for (int i = 0; i < p_match_branch->patterns.size(); i++) {6087if (i > 0) {6088push_text(" , ");6089}6090print_match_pattern(p_match_branch->patterns[i]);6091}60926093push_line(" :");60946095increase_indent();6096print_suite(p_match_branch->block);6097decrease_indent();6098}60996100void GDScriptParser::TreePrinter::print_match_pattern(PatternNode *p_match_pattern) {6101switch (p_match_pattern->pattern_type) {6102case PatternNode::PT_LITERAL:6103print_literal(p_match_pattern->literal);6104break;6105case PatternNode::PT_WILDCARD:6106push_text("_");6107break;6108case PatternNode::PT_REST:6109push_text("..");6110break;6111case PatternNode::PT_BIND:6112push_text("Var ");6113print_identifier(p_match_pattern->bind);6114break;6115case PatternNode::PT_EXPRESSION:6116print_expression(p_match_pattern->expression);6117break;6118case PatternNode::PT_ARRAY:6119push_text("[ ");6120for (int i = 0; i < p_match_pattern->array.size(); i++) {6121if (i > 0) {6122push_text(" , ");6123}6124print_match_pattern(p_match_pattern->array[i]);6125}6126push_text(" ]");6127break;6128case PatternNode::PT_DICTIONARY:6129push_text("{ ");6130for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {6131if (i > 0) {6132push_text(" , ");6133}6134if (p_match_pattern->dictionary[i].key != nullptr) {6135// Key can be null for rest pattern.6136print_expression(p_match_pattern->dictionary[i].key);6137push_text(" : ");6138}6139print_match_pattern(p_match_pattern->dictionary[i].value_pattern);6140}6141push_text(" }");6142break;6143}6144}61456146void GDScriptParser::TreePrinter::print_parameter(ParameterNode *p_parameter) {6147print_identifier(p_parameter->identifier);6148if (p_parameter->datatype_specifier != nullptr) {6149push_text(" : ");6150print_type(p_parameter->datatype_specifier);6151}6152if (p_parameter->initializer != nullptr) {6153push_text(" = ");6154print_expression(p_parameter->initializer);6155}6156}61576158void GDScriptParser::TreePrinter::print_preload(PreloadNode *p_preload) {6159push_text(R"(Preload ( ")");6160push_text(p_preload->resolved_path);6161push_text(R"(" )");6162}61636164void GDScriptParser::TreePrinter::print_return(ReturnNode *p_return) {6165push_text("Return");6166if (p_return->return_value != nullptr) {6167push_text(" ");6168print_expression(p_return->return_value);6169}6170push_line();6171}61726173void GDScriptParser::TreePrinter::print_self(SelfNode *p_self) {6174push_text("Self(");6175if (p_self->current_class->identifier != nullptr) {6176print_identifier(p_self->current_class->identifier);6177} else {6178push_text("<main class>");6179}6180push_text(")");6181}61826183void GDScriptParser::TreePrinter::print_signal(SignalNode *p_signal) {6184push_text("Signal ");6185print_identifier(p_signal->identifier);6186push_text("( ");6187for (int i = 0; i < p_signal->parameters.size(); i++) {6188print_parameter(p_signal->parameters[i]);6189}6190push_line(" )");6191}61926193void GDScriptParser::TreePrinter::print_subscript(SubscriptNode *p_subscript) {6194print_expression(p_subscript->base);6195if (p_subscript->is_attribute) {6196push_text(".");6197print_identifier(p_subscript->attribute);6198} else {6199push_text("[ ");6200print_expression(p_subscript->index);6201push_text(" ]");6202}6203}62046205void GDScriptParser::TreePrinter::print_statement(Node *p_statement) {6206switch (p_statement->type) {6207case Node::ASSERT:6208print_assert(static_cast<AssertNode *>(p_statement));6209break;6210case Node::VARIABLE:6211print_variable(static_cast<VariableNode *>(p_statement));6212break;6213case Node::CONSTANT:6214print_constant(static_cast<ConstantNode *>(p_statement));6215break;6216case Node::IF:6217print_if(static_cast<IfNode *>(p_statement));6218break;6219case Node::FOR:6220print_for(static_cast<ForNode *>(p_statement));6221break;6222case Node::WHILE:6223print_while(static_cast<WhileNode *>(p_statement));6224break;6225case Node::MATCH:6226print_match(static_cast<MatchNode *>(p_statement));6227break;6228case Node::RETURN:6229print_return(static_cast<ReturnNode *>(p_statement));6230break;6231case Node::BREAK:6232push_line("Break");6233break;6234case Node::CONTINUE:6235push_line("Continue");6236break;6237case Node::PASS:6238push_line("Pass");6239break;6240case Node::BREAKPOINT:6241push_line("Breakpoint");6242break;6243case Node::ASSIGNMENT:6244print_assignment(static_cast<AssignmentNode *>(p_statement));6245break;6246default:6247if (p_statement->is_expression()) {6248print_expression(static_cast<ExpressionNode *>(p_statement));6249push_line();6250} else {6251push_line(vformat("<unknown statement %d>", p_statement->type));6252}6253break;6254}6255}62566257void GDScriptParser::TreePrinter::print_suite(SuiteNode *p_suite) {6258for (int i = 0; i < p_suite->statements.size(); i++) {6259print_statement(p_suite->statements[i]);6260}6261}62626263void GDScriptParser::TreePrinter::print_ternary_op(TernaryOpNode *p_ternary_op) {6264// Surround in parenthesis for disambiguation.6265push_text("(");6266print_expression(p_ternary_op->true_expr);6267push_text(") IF (");6268print_expression(p_ternary_op->condition);6269push_text(") ELSE (");6270print_expression(p_ternary_op->false_expr);6271push_text(")");6272}62736274void GDScriptParser::TreePrinter::print_type(TypeNode *p_type) {6275if (p_type->type_chain.is_empty()) {6276push_text("Void");6277} else {6278for (int i = 0; i < p_type->type_chain.size(); i++) {6279if (i > 0) {6280push_text(".");6281}6282print_identifier(p_type->type_chain[i]);6283}6284}6285}62866287void GDScriptParser::TreePrinter::print_type_test(TypeTestNode *p_test) {6288print_expression(p_test->operand);6289push_text(" IS ");6290print_type(p_test->test_type);6291}62926293void GDScriptParser::TreePrinter::print_unary_op(UnaryOpNode *p_unary_op) {6294// Surround in parenthesis for disambiguation.6295push_text("(");6296switch (p_unary_op->operation) {6297case UnaryOpNode::OP_POSITIVE:6298push_text("+");6299break;6300case UnaryOpNode::OP_NEGATIVE:6301push_text("-");6302break;6303case UnaryOpNode::OP_LOGIC_NOT:6304push_text("NOT");6305break;6306case UnaryOpNode::OP_COMPLEMENT:6307push_text("~");6308break;6309}6310print_expression(p_unary_op->operand);6311// Surround in parenthesis for disambiguation.6312push_text(")");6313}63146315void GDScriptParser::TreePrinter::print_variable(VariableNode *p_variable) {6316for (const AnnotationNode *E : p_variable->annotations) {6317print_annotation(E);6318}63196320if (p_variable->is_static) {6321push_text("Static ");6322}6323push_text("Variable ");6324print_identifier(p_variable->identifier);63256326push_text(" : ");6327if (p_variable->datatype_specifier != nullptr) {6328print_type(p_variable->datatype_specifier);6329} else if (p_variable->infer_datatype) {6330push_text("<inferred type>");6331} else {6332push_text("Variant");6333}63346335increase_indent();63366337push_line();6338push_text("= ");6339if (p_variable->initializer == nullptr) {6340push_text("<default value>");6341} else {6342print_expression(p_variable->initializer);6343}6344push_line();63456346if (p_variable->property != VariableNode::PROP_NONE) {6347if (p_variable->getter != nullptr) {6348push_text("Get");6349if (p_variable->property == VariableNode::PROP_INLINE) {6350push_line(":");6351increase_indent();6352print_suite(p_variable->getter->body);6353decrease_indent();6354} else {6355push_line(" =");6356increase_indent();6357print_identifier(p_variable->getter_pointer);6358push_line();6359decrease_indent();6360}6361}6362if (p_variable->setter != nullptr) {6363push_text("Set (");6364if (p_variable->property == VariableNode::PROP_INLINE) {6365if (p_variable->setter_parameter != nullptr) {6366print_identifier(p_variable->setter_parameter);6367} else {6368push_text("<missing>");6369}6370push_line("):");6371increase_indent();6372print_suite(p_variable->setter->body);6373decrease_indent();6374} else {6375push_line(" =");6376increase_indent();6377print_identifier(p_variable->setter_pointer);6378push_line();6379decrease_indent();6380}6381}6382}63836384decrease_indent();6385push_line();6386}63876388void GDScriptParser::TreePrinter::print_while(WhileNode *p_while) {6389push_text("While ");6390print_expression(p_while->condition);6391push_line(" :");63926393increase_indent();6394print_suite(p_while->loop);6395decrease_indent();6396}63976398void GDScriptParser::TreePrinter::print_tree(const GDScriptParser &p_parser) {6399ClassNode *class_tree = p_parser.get_tree();6400ERR_FAIL_NULL_MSG(class_tree, "Parse the code before printing the parse tree.");64016402if (p_parser.is_tool()) {6403push_line("@tool");6404}6405if (!class_tree->icon_path.is_empty()) {6406push_text(R"(@icon (")");6407push_text(class_tree->icon_path);6408push_line("\")");6409}6410print_class(class_tree);64116412print_line(String(printed));6413}64146415#endif // DEBUG_ENABLED641664176418