| 1 | /**************************************************************************/ |
| 2 | /* gdscript_parser.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "gdscript_parser.h" |
| 32 | |
| 33 | #include "gdscript.h" |
| 34 | #include "gdscript_tokenizer_buffer.h" |
| 35 | |
| 36 | #include "core/config/project_settings.h" |
| 37 | #include "core/io/resource_loader.h" |
| 38 | #include "core/math/math_defs.h" |
| 39 | #include "scene/main/multiplayer_api.h" |
| 40 | |
| 41 | #ifdef DEBUG_ENABLED |
| 42 | #include "core/string/string_builder.h" |
| 43 | #include "servers/text/text_server.h" |
| 44 | #endif |
| 45 | |
| 46 | #ifdef TOOLS_ENABLED |
| 47 | #include "editor/settings/editor_settings.h" |
| 48 | #endif |
| 49 | |
| 50 | // This function is used to determine that a type is "built-in" as opposed to native |
| 51 | // and custom classes. So `Variant::NIL` and `Variant::OBJECT` are excluded: |
| 52 | // `Variant::NIL` - `null` is literal, not a type. |
| 53 | // `Variant::OBJECT` - `Object` should be treated as a class, not as a built-in type. |
| 54 | static HashMap<StringName, Variant::Type> builtin_types; |
| 55 | Variant::Type GDScriptParser::get_builtin_type(const StringName &p_type) { |
| 56 | if (unlikely(builtin_types.is_empty())) { |
| 57 | for (int i = 0; i < Variant::VARIANT_MAX; i++) { |
| 58 | Variant::Type type = (Variant::Type)i; |
| 59 | if (type != Variant::NIL && type != Variant::OBJECT) { |
| 60 | builtin_types[Variant::get_type_name(type)] = type; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (builtin_types.has(p_type)) { |
| 66 | return builtin_types[p_type]; |
| 67 | } |
| 68 | return Variant::VARIANT_MAX; |
| 69 | } |
| 70 | |
| 71 | #ifdef DEBUG_ENABLED |
| 72 | bool GDScriptParser::is_project_ignoring_warnings = false; |
| 73 | GDScriptWarning::WarnLevel GDScriptParser::warning_levels[GDScriptWarning::WARNING_MAX]; |
| 74 | LocalVector<GDScriptParser::WarningDirectoryRule> GDScriptParser::warning_directory_rules; |
| 75 | #endif // DEBUG_ENABLED |
| 76 | |
| 77 | #ifdef TOOLS_ENABLED |
| 78 | HashMap<String, String> GDScriptParser::theme_color_names; |
| 79 | #endif // TOOLS_ENABLED |
| 80 | |
| 81 | HashMap<StringName, GDScriptParser::AnnotationInfo> GDScriptParser::valid_annotations; |
| 82 | |
| 83 | void GDScriptParser::cleanup() { |
| 84 | builtin_types.clear(); |
| 85 | valid_annotations.clear(); |
| 86 | } |
| 87 | |
| 88 | void GDScriptParser::get_annotation_list(List<MethodInfo> *r_annotations) const { |
| 89 | for (const KeyValue<StringName, AnnotationInfo> &E : valid_annotations) { |
| 90 | r_annotations->push_back(E.value.info); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | bool GDScriptParser::annotation_exists(const String &p_annotation_name) const { |
| 95 | return valid_annotations.has(p_annotation_name); |
| 96 | } |
| 97 | |
| 98 | #ifdef DEBUG_ENABLED |
| 99 | void GDScriptParser::update_project_settings() { |
| 100 | is_project_ignoring_warnings = !GLOBAL_GET("debug/gdscript/warnings/enable").booleanize(); |
| 101 | |
| 102 | for (int i = 0; i < GDScriptWarning::WARNING_MAX; i++) { |
| 103 | const String setting_path = GDScriptWarning::get_setting_path_from_code((GDScriptWarning::Code)i); |
| 104 | warning_levels[i] = (GDScriptWarning::WarnLevel)(int)GLOBAL_GET(setting_path); |
| 105 | } |
| 106 | |
| 107 | #ifndef DISABLE_DEPRECATED |
| 108 | // We do not use `GLOBAL_GET`, since we check without taking overrides into account. We leave the migration of non-trivial configurations to the user. |
| 109 | if (unlikely(ProjectSettings::get_singleton()->has_setting("debug/gdscript/warnings/exclude_addons"))) { |
| 110 | const bool is_excluding_addons = ProjectSettings::get_singleton()->get_setting("debug/gdscript/warnings/exclude_addons", true).booleanize(); |
| 111 | ProjectSettings::get_singleton()->clear("debug/gdscript/warnings/exclude_addons"); |
| 112 | |
| 113 | Dictionary rules = ProjectSettings::get_singleton()->get_setting("debug/gdscript/warnings/directory_rules"); |
| 114 | rules["res://addons"] = is_excluding_addons ? WarningDirectoryRule::DECISION_EXCLUDE : WarningDirectoryRule::DECISION_INCLUDE; |
| 115 | ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/directory_rules", rules); |
| 116 | } |
| 117 | #endif // DISABLE_DEPRECATED |
| 118 | |
| 119 | warning_directory_rules.clear(); |
| 120 | |
| 121 | const Dictionary rules = GLOBAL_GET("debug/gdscript/warnings/directory_rules"); |
| 122 | for (const KeyValue<Variant, Variant> &kv : rules) { |
| 123 | String dir = kv.key.operator String().simplify_path(); |
| 124 | ERR_CONTINUE_MSG(!dir.begins_with("res://"), R"(Paths in the project setting "debug/gdscript/warnings/directory_rules" keys must start with the "res://" prefix.)"); |
| 125 | if (!dir.ends_with("/")) { |
| 126 | dir += '/'; |
| 127 | } |
| 128 | |
| 129 | const int decision = kv.value; |
| 130 | ERR_CONTINUE(decision < 0 || decision >= WarningDirectoryRule::DECISION_MAX); |
| 131 | |
| 132 | warning_directory_rules.push_back({ dir, (WarningDirectoryRule::Decision)decision }); |
| 133 | } |
| 134 | |
| 135 | struct RuleSort { |
| 136 | bool operator()(const WarningDirectoryRule &p_a, const WarningDirectoryRule &p_b) const { |
| 137 | return p_a.directory_path.count("/") > p_b.directory_path.count("/"); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | warning_directory_rules.sort_custom<RuleSort>(); |
| 142 | } |
| 143 | #endif // DEBUG_ENABLED |
| 144 | |
| 145 | GDScriptParser::GDScriptParser() { |
| 146 | // Register valid annotations. |
| 147 | if (unlikely(valid_annotations.is_empty())) { |
| 148 | // Script annotations. |
| 149 | register_annotation(MethodInfo("@tool"), AnnotationInfo::SCRIPT, &GDScriptParser::tool_annotation); |
| 150 | register_annotation(MethodInfo("@icon", PropertyInfo(Variant::STRING, "icon_path")), AnnotationInfo::SCRIPT, &GDScriptParser::icon_annotation); |
| 151 | register_annotation(MethodInfo("@static_unload"), AnnotationInfo::SCRIPT, &GDScriptParser::static_unload_annotation); |
| 152 | register_annotation(MethodInfo("@abstract"), AnnotationInfo::SCRIPT | AnnotationInfo::CLASS | AnnotationInfo::FUNCTION, &GDScriptParser::abstract_annotation); |
| 153 | // Onready annotation. |
| 154 | register_annotation(MethodInfo("@onready"), AnnotationInfo::VARIABLE, &GDScriptParser::onready_annotation); |
| 155 | // Export annotations. |
| 156 | register_annotation(MethodInfo("@export"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_NONE, Variant::NIL>); |
| 157 | register_annotation(MethodInfo("@export_enum", PropertyInfo(Variant::STRING, "names")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_ENUM, Variant::NIL>, varray(), true); |
| 158 | register_annotation(MethodInfo("@export_file", PropertyInfo(Variant::STRING, "filter")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_FILE, Variant::STRING>, varray(""), true); |
| 159 | register_annotation(MethodInfo("@export_file_path", PropertyInfo(Variant::STRING, "filter")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_FILE_PATH, Variant::STRING>, varray(""), true); |
| 160 | register_annotation(MethodInfo("@export_dir"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_DIR, Variant::STRING>); |
| 161 | register_annotation(MethodInfo("@export_global_file", PropertyInfo(Variant::STRING, "filter")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_GLOBAL_FILE, Variant::STRING>, varray(""), true); |
| 162 | register_annotation(MethodInfo("@export_global_dir"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_GLOBAL_DIR, Variant::STRING>); |
| 163 | register_annotation(MethodInfo("@export_multiline", PropertyInfo(Variant::STRING, "hint")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_MULTILINE_TEXT, Variant::STRING>, varray(""), true); |
| 164 | register_annotation(MethodInfo("@export_placeholder", PropertyInfo(Variant::STRING, "placeholder")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_PLACEHOLDER_TEXT, Variant::STRING>); |
| 165 | register_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); |
| 166 | register_annotation(MethodInfo("@export_exp_easing", PropertyInfo(Variant::STRING, "hints")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_EXP_EASING, Variant::FLOAT>, varray(""), true); |
| 167 | register_annotation(MethodInfo("@export_color_no_alpha"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_COLOR_NO_ALPHA, Variant::COLOR>); |
| 168 | register_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); |
| 169 | register_annotation(MethodInfo("@export_flags", PropertyInfo(Variant::STRING, "names")), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_FLAGS, Variant::INT>, varray(), true); |
| 170 | register_annotation(MethodInfo("@export_flags_2d_render"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_2D_RENDER, Variant::INT>); |
| 171 | register_annotation(MethodInfo("@export_flags_2d_physics"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_2D_PHYSICS, Variant::INT>); |
| 172 | register_annotation(MethodInfo("@export_flags_2d_navigation"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_2D_NAVIGATION, Variant::INT>); |
| 173 | register_annotation(MethodInfo("@export_flags_3d_render"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_3D_RENDER, Variant::INT>); |
| 174 | register_annotation(MethodInfo("@export_flags_3d_physics"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_3D_PHYSICS, Variant::INT>); |
| 175 | register_annotation(MethodInfo("@export_flags_3d_navigation"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_3D_NAVIGATION, Variant::INT>); |
| 176 | register_annotation(MethodInfo("@export_flags_avoidance"), AnnotationInfo::VARIABLE, &GDScriptParser::export_annotations<PROPERTY_HINT_LAYERS_AVOIDANCE, Variant::INT>); |
| 177 | register_annotation(MethodInfo("@export_storage"), AnnotationInfo::VARIABLE, &GDScriptParser::export_storage_annotation); |
| 178 | register_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)); |
| 179 | register_annotation(MethodInfo("@export_tool_button", PropertyInfo(Variant::STRING, "text"), PropertyInfo(Variant::STRING, "icon")), AnnotationInfo::VARIABLE, &GDScriptParser::export_tool_button_annotation, varray("")); |
| 180 | // Export grouping annotations. |
| 181 | register_annotation(MethodInfo("@export_category", PropertyInfo(Variant::STRING, "name")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_CATEGORY>); |
| 182 | register_annotation(MethodInfo("@export_group", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "prefix")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_GROUP>, varray("")); |
| 183 | register_annotation(MethodInfo("@export_subgroup", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::STRING, "prefix")), AnnotationInfo::STANDALONE, &GDScriptParser::export_group_annotations<PROPERTY_USAGE_SUBGROUP>, varray("")); |
| 184 | // Warning annotations. |
| 185 | register_annotation(MethodInfo("@warning_ignore", PropertyInfo(Variant::STRING, "warning")), AnnotationInfo::CLASS_LEVEL | AnnotationInfo::STATEMENT, &GDScriptParser::warning_ignore_annotation, varray(), true); |
| 186 | register_annotation(MethodInfo("@warning_ignore_start", PropertyInfo(Variant::STRING, "warning")), AnnotationInfo::STANDALONE, &GDScriptParser::warning_ignore_region_annotations, varray(), true); |
| 187 | register_annotation(MethodInfo("@warning_ignore_restore", PropertyInfo(Variant::STRING, "warning")), AnnotationInfo::STANDALONE, &GDScriptParser::warning_ignore_region_annotations, varray(), true); |
| 188 | // Networking. |
| 189 | // Keep in sync with `rpc_annotation()` and `SceneRPCInterface::_parse_rpc_config()`. |
| 190 | register_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", "reliable", 0)); |
| 191 | } |
| 192 | |
| 193 | #ifdef DEBUG_ENABLED |
| 194 | for (int i = 0; i < GDScriptWarning::WARNING_MAX; i++) { |
| 195 | warning_ignore_start_lines[i] = INT_MAX; |
| 196 | } |
| 197 | #endif // DEBUG_ENABLED |
| 198 | |
| 199 | #ifdef TOOLS_ENABLED |
| 200 | if (unlikely(theme_color_names.is_empty())) { |
| 201 | // Vectors. |
| 202 | theme_color_names.insert("x", "axis_x_color"); |
| 203 | theme_color_names.insert("y", "axis_y_color"); |
| 204 | theme_color_names.insert("z", "axis_z_color"); |
| 205 | theme_color_names.insert("w", "axis_w_color"); |
| 206 | |
| 207 | // Color. |
| 208 | theme_color_names.insert("r", "axis_x_color"); |
| 209 | theme_color_names.insert("r8", "axis_x_color"); |
| 210 | theme_color_names.insert("g", "axis_y_color"); |
| 211 | theme_color_names.insert("g8", "axis_y_color"); |
| 212 | theme_color_names.insert("b", "axis_z_color"); |
| 213 | theme_color_names.insert("b8", "axis_z_color"); |
| 214 | theme_color_names.insert("a", "axis_w_color"); |
| 215 | theme_color_names.insert("a8", "axis_w_color"); |
| 216 | } |
| 217 | #endif // TOOLS_ENABLED |
| 218 | } |
| 219 | |
| 220 | GDScriptParser::~GDScriptParser() { |
| 221 | while (list != nullptr) { |
| 222 | Node *element = list; |
| 223 | list = list->next; |
| 224 | memdelete(element); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void GDScriptParser::clear() { |
| 229 | GDScriptParser tmp; |
| 230 | tmp = *this; |
| 231 | *this = GDScriptParser(); |
| 232 | } |
| 233 | |
| 234 | void GDScriptParser::push_error(const String &p_message, const Node *p_origin) { |
| 235 | // TODO: Improve error reporting by pointing at source code. |
| 236 | // TODO: Errors might point at more than one place at once (e.g. show previous declaration). |
| 237 | panic_mode = true; |
| 238 | // TODO: Improve positional information. |
| 239 | if (p_origin == nullptr) { |
| 240 | errors.push_back({ p_message, previous.start_line, previous.start_column }); |
| 241 | } else { |
| 242 | errors.push_back({ p_message, p_origin->start_line, p_origin->start_column }); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #ifdef DEBUG_ENABLED |
| 247 | void GDScriptParser::push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols) { |
| 248 | ERR_FAIL_NULL(p_source); |
| 249 | ERR_FAIL_INDEX(p_code, GDScriptWarning::WARNING_MAX); |
| 250 | |
| 251 | if (is_project_ignoring_warnings || is_script_ignoring_warnings) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | const GDScriptWarning::WarnLevel warn_level = warning_levels[p_code]; |
| 256 | if (warn_level == GDScriptWarning::IGNORE) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | PendingWarning pw; |
| 261 | pw.source = p_source; |
| 262 | pw.code = p_code; |
| 263 | pw.treated_as_error = warn_level == GDScriptWarning::ERROR; |
| 264 | pw.symbols = p_symbols; |
| 265 | |
| 266 | pending_warnings.push_back(pw); |
| 267 | } |
| 268 | |
| 269 | void GDScriptParser::apply_pending_warnings() { |
| 270 | for (const PendingWarning &pw : pending_warnings) { |
| 271 | if (warning_ignored_lines[pw.code].has(pw.source->start_line)) { |
| 272 | continue; |
| 273 | } |
| 274 | if (warning_ignore_start_lines[pw.code] <= pw.source->start_line) { |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | GDScriptWarning warning; |
| 279 | warning.code = pw.code; |
| 280 | warning.symbols = pw.symbols; |
| 281 | warning.start_line = pw.source->start_line; |
| 282 | warning.end_line = pw.source->end_line; |
| 283 | |
| 284 | if (pw.treated_as_error) { |
| 285 | push_error(warning.get_message() + String(" (Warning treated as error.)"), pw.source); |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | List<GDScriptWarning>::Element *before = nullptr; |
| 290 | for (List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) { |
| 291 | if (E->get().start_line > warning.start_line) { |
| 292 | break; |
| 293 | } |
| 294 | before = E; |
| 295 | } |
| 296 | if (before) { |
| 297 | warnings.insert_after(before, warning); |
| 298 | } else { |
| 299 | warnings.push_front(warning); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | pending_warnings.clear(); |
| 304 | } |
| 305 | |
| 306 | void GDScriptParser::evaluate_warning_directory_rules_for_script_path() { |
| 307 | is_script_ignoring_warnings = false; |
| 308 | for (const WarningDirectoryRule &rule : warning_directory_rules) { |
| 309 | if (script_path.begins_with(rule.directory_path)) { |
| 310 | switch (rule.decision) { |
| 311 | case WarningDirectoryRule::DECISION_EXCLUDE: |
| 312 | is_script_ignoring_warnings = true; |
| 313 | return; // Stop checking rules. |
| 314 | case WarningDirectoryRule::DECISION_INCLUDE: |
| 315 | is_script_ignoring_warnings = false; |
| 316 | return; // Stop checking rules. |
| 317 | case WarningDirectoryRule::DECISION_MAX: |
| 318 | return; // Unreachable. |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | #endif // DEBUG_ENABLED |
| 324 | |
| 325 | void GDScriptParser::override_completion_context(const Node *p_for_node, CompletionType p_type, Node *p_node, int p_argument) { |
| 326 | if (!for_completion) { |
| 327 | return; |
| 328 | } |
| 329 | if (p_for_node == nullptr || completion_context.node != p_for_node) { |
| 330 | return; |
| 331 | } |
| 332 | CompletionContext context; |
| 333 | context.type = p_type; |
| 334 | context.current_class = current_class; |
| 335 | context.current_function = current_function; |
| 336 | context.current_suite = current_suite; |
| 337 | context.current_line = tokenizer->get_cursor_line(); |
| 338 | context.current_argument = p_argument; |
| 339 | context.node = p_node; |
| 340 | context.parser = this; |
| 341 | if (!completion_call_stack.is_empty()) { |
| 342 | context.call = completion_call_stack.back()->get(); |
| 343 | } |
| 344 | completion_context = context; |
| 345 | } |
| 346 | |
| 347 | void GDScriptParser::make_completion_context(CompletionType p_type, Node *p_node, int p_argument, bool p_force) { |
| 348 | if (!for_completion || (!p_force && completion_context.type != COMPLETION_NONE)) { |
| 349 | return; |
| 350 | } |
| 351 | if (previous.cursor_place != GDScriptTokenizerText::CURSOR_MIDDLE && previous.cursor_place != GDScriptTokenizerText::CURSOR_END && current.cursor_place == GDScriptTokenizerText::CURSOR_NONE) { |
| 352 | return; |
| 353 | } |
| 354 | CompletionContext context; |
| 355 | context.type = p_type; |
| 356 | context.current_class = current_class; |
| 357 | context.current_function = current_function; |
| 358 | context.current_suite = current_suite; |
| 359 | context.current_line = tokenizer->get_cursor_line(); |
| 360 | context.current_argument = p_argument; |
| 361 | context.node = p_node; |
| 362 | context.parser = this; |
| 363 | if (!completion_call_stack.is_empty()) { |
| 364 | context.call = completion_call_stack.back()->get(); |
| 365 | } |
| 366 | completion_context = context; |
| 367 | } |
| 368 | |
| 369 | void GDScriptParser::make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force) { |
| 370 | if (!for_completion || (!p_force && completion_context.type != COMPLETION_NONE)) { |
| 371 | return; |
| 372 | } |
| 373 | if (previous.cursor_place != GDScriptTokenizerText::CURSOR_MIDDLE && previous.cursor_place != GDScriptTokenizerText::CURSOR_END && current.cursor_place == GDScriptTokenizerText::CURSOR_NONE) { |
| 374 | return; |
| 375 | } |
| 376 | CompletionContext context; |
| 377 | context.type = p_type; |
| 378 | context.current_class = current_class; |
| 379 | context.current_function = current_function; |
| 380 | context.current_suite = current_suite; |
| 381 | context.current_line = tokenizer->get_cursor_line(); |
| 382 | context.builtin_type = p_builtin_type; |
| 383 | context.parser = this; |
| 384 | if (!completion_call_stack.is_empty()) { |
| 385 | context.call = completion_call_stack.back()->get(); |
| 386 | } |
| 387 | completion_context = context; |
| 388 | } |
| 389 | |
| 390 | void GDScriptParser::push_completion_call(Node *p_call) { |
| 391 | if (!for_completion) { |
| 392 | return; |
| 393 | } |
| 394 | CompletionCall call; |
| 395 | call.call = p_call; |
| 396 | call.argument = 0; |
| 397 | completion_call_stack.push_back(call); |
| 398 | } |
| 399 | |
| 400 | void GDScriptParser::pop_completion_call() { |
| 401 | if (!for_completion) { |
| 402 | return; |
| 403 | } |
| 404 | ERR_FAIL_COND_MSG(completion_call_stack.is_empty(), "Trying to pop empty completion call stack"); |
| 405 | completion_call_stack.pop_back(); |
| 406 | } |
| 407 | |
| 408 | void GDScriptParser::set_last_completion_call_arg(int p_argument) { |
| 409 | if (!for_completion) { |
| 410 | return; |
| 411 | } |
| 412 | ERR_FAIL_COND_MSG(completion_call_stack.is_empty(), "Trying to set argument on empty completion call stack"); |
| 413 | completion_call_stack.back()->get().argument = p_argument; |
| 414 | } |
| 415 | |
| 416 | Error GDScriptParser::parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body) { |
| 417 | clear(); |
| 418 | |
| 419 | String source = p_source_code; |
| 420 | int cursor_line = -1; |
| 421 | int cursor_column = -1; |
| 422 | for_completion = p_for_completion; |
| 423 | parse_body = p_parse_body; |
| 424 | |
| 425 | int tab_size = 4; |
| 426 | #ifdef TOOLS_ENABLED |
| 427 | if (EditorSettings::get_singleton()) { |
| 428 | tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size"); |
| 429 | } |
| 430 | #endif // TOOLS_ENABLED |
| 431 | |
| 432 | if (p_for_completion) { |
| 433 | // Remove cursor sentinel char. |
| 434 | const Vector<String> lines = p_source_code.split("\n"); |
| 435 | cursor_line = 1; |
| 436 | cursor_column = 1; |
| 437 | for (int i = 0; i < lines.size(); i++) { |
| 438 | bool found = false; |
| 439 | const String &line = lines[i]; |
| 440 | for (int j = 0; j < line.size(); j++) { |
| 441 | if (line[j] == char32_t(0xFFFF)) { |
| 442 | found = true; |
| 443 | break; |
| 444 | } else if (line[j] == '\t') { |
| 445 | cursor_column += tab_size - 1; |
| 446 | } |
| 447 | cursor_column++; |
| 448 | } |
| 449 | if (found) { |
| 450 | break; |
| 451 | } |
| 452 | cursor_line++; |
| 453 | cursor_column = 1; |
| 454 | } |
| 455 | |
| 456 | source = source.replace_first(String::chr(0xFFFF), String()); |
| 457 | } |
| 458 | |
| 459 | GDScriptTokenizerText *text_tokenizer = memnew(GDScriptTokenizerText); |
| 460 | text_tokenizer->set_source_code(source); |
| 461 | |
| 462 | tokenizer = text_tokenizer; |
| 463 | tokenizer->set_cursor_position(cursor_line, cursor_column); |
| 464 | |
| 465 | script_path = p_script_path.simplify_path(); |
| 466 | |
| 467 | #ifdef DEBUG_ENABLED |
| 468 | evaluate_warning_directory_rules_for_script_path(); |
| 469 | #endif // DEBUG_ENABLED |
| 470 | |
| 471 | current = tokenizer->scan(); |
| 472 | // Avoid error or newline as the first token. |
| 473 | // The latter can mess with the parser when opening files filled exclusively with comments and newlines. |
| 474 | while (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::NEWLINE) { |
| 475 | if (current.type == GDScriptTokenizer::Token::ERROR) { |
| 476 | push_error(current.literal); |
| 477 | } |
| 478 | current = tokenizer->scan(); |
| 479 | } |
| 480 | |
| 481 | #ifdef DEBUG_ENABLED |
| 482 | // Warn about parsing an empty script file: |
| 483 | if (current.type == GDScriptTokenizer::Token::TK_EOF) { |
| 484 | // Create a dummy Node for the warning, pointing to the very beginning of the file |
| 485 | Node *nd = alloc_node<PassNode>(); |
| 486 | nd->start_line = 1; |
| 487 | nd->start_column = 0; |
| 488 | nd->end_line = 1; |
| 489 | push_warning(nd, GDScriptWarning::EMPTY_FILE); |
| 490 | } |
| 491 | #endif // DEBUG_ENABLED |
| 492 | |
| 493 | push_multiline(false); // Keep one for the whole parsing. |
| 494 | parse_program(); |
| 495 | pop_multiline(); |
| 496 | |
| 497 | #ifdef TOOLS_ENABLED |
| 498 | comment_data = tokenizer->get_comments(); |
| 499 | #endif // TOOLS_ENABLED |
| 500 | |
| 501 | memdelete(text_tokenizer); |
| 502 | tokenizer = nullptr; |
| 503 | |
| 504 | #ifdef DEBUG_ENABLED |
| 505 | if (multiline_stack.size() > 0) { |
| 506 | ERR_PRINT("Parser bug: Imbalanced multiline stack."); |
| 507 | } |
| 508 | #endif // DEBUG_ENABLED |
| 509 | |
| 510 | if (errors.is_empty()) { |
| 511 | return OK; |
| 512 | } else { |
| 513 | return ERR_PARSE_ERROR; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | Error GDScriptParser::parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path) { |
| 518 | GDScriptTokenizerBuffer *buffer_tokenizer = memnew(GDScriptTokenizerBuffer); |
| 519 | Error err = buffer_tokenizer->set_code_buffer(p_binary); |
| 520 | |
| 521 | if (err) { |
| 522 | memdelete(buffer_tokenizer); |
| 523 | return err; |
| 524 | } |
| 525 | |
| 526 | tokenizer = buffer_tokenizer; |
| 527 | |
| 528 | script_path = p_script_path.simplify_path(); |
| 529 | |
| 530 | #ifdef DEBUG_ENABLED |
| 531 | evaluate_warning_directory_rules_for_script_path(); |
| 532 | #endif // DEBUG_ENABLED |
| 533 | |
| 534 | current = tokenizer->scan(); |
| 535 | // Avoid error or newline as the first token. |
| 536 | // The latter can mess with the parser when opening files filled exclusively with comments and newlines. |
| 537 | while (current.type == GDScriptTokenizer::Token::ERROR || current.type == GDScriptTokenizer::Token::NEWLINE) { |
| 538 | if (current.type == GDScriptTokenizer::Token::ERROR) { |
| 539 | push_error(current.literal); |
| 540 | } |
| 541 | current = tokenizer->scan(); |
| 542 | } |
| 543 | |
| 544 | push_multiline(false); // Keep one for the whole parsing. |
| 545 | parse_program(); |
| 546 | pop_multiline(); |
| 547 | |
| 548 | memdelete(buffer_tokenizer); |
| 549 | tokenizer = nullptr; |
| 550 | |
| 551 | if (errors.is_empty()) { |
| 552 | return OK; |
| 553 | } else { |
| 554 | return ERR_PARSE_ERROR; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | GDScriptTokenizer::Token GDScriptParser::advance() { |
| 559 | lambda_ended = false; // Empty marker since we're past the end in any case. |
| 560 | |
| 561 | if (current.type == GDScriptTokenizer::Token::TK_EOF) { |
| 562 | ERR_FAIL_COND_V_MSG(current.type == GDScriptTokenizer::Token::TK_EOF, current, "GDScript parser bug: Trying to advance past the end of stream."); |
| 563 | } |
| 564 | previous = current; |
| 565 | current = tokenizer->scan(); |
| 566 | while (current.type == GDScriptTokenizer::Token::ERROR) { |
| 567 | push_error(current.literal); |
| 568 | current = tokenizer->scan(); |
| 569 | } |
| 570 | if (previous.type != GDScriptTokenizer::Token::DEDENT) { // `DEDENT` belongs to the next non-empty line. |
| 571 | for (Node *n : nodes_in_progress) { |
| 572 | update_extents(n); |
| 573 | } |
| 574 | } |
| 575 | return previous; |
| 576 | } |
| 577 | |
| 578 | bool GDScriptParser::match(GDScriptTokenizer::Token::Type p_token_type) { |
| 579 | if (!check(p_token_type)) { |
| 580 | return false; |
| 581 | } |
| 582 | advance(); |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | bool GDScriptParser::check(GDScriptTokenizer::Token::Type p_token_type) const { |
| 587 | if (p_token_type == GDScriptTokenizer::Token::IDENTIFIER) { |
| 588 | return current.is_identifier(); |
| 589 | } |
| 590 | return current.type == p_token_type; |
| 591 | } |
| 592 | |
| 593 | bool GDScriptParser::consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message) { |
| 594 | if (match(p_token_type)) { |
| 595 | return true; |
| 596 | } |
| 597 | push_error(p_error_message); |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | bool GDScriptParser::is_at_end() const { |
| 602 | return check(GDScriptTokenizer::Token::TK_EOF); |
| 603 | } |
| 604 | |
| 605 | void GDScriptParser::synchronize() { |
| 606 | panic_mode = false; |
| 607 | while (!is_at_end()) { |
| 608 | if (previous.type == GDScriptTokenizer::Token::NEWLINE || previous.type == GDScriptTokenizer::Token::SEMICOLON) { |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | switch (current.type) { |
| 613 | case GDScriptTokenizer::Token::CLASS: |
| 614 | case GDScriptTokenizer::Token::FUNC: |
| 615 | case GDScriptTokenizer::Token::STATIC: |
| 616 | case GDScriptTokenizer::Token::VAR: |
| 617 | case GDScriptTokenizer::Token::TK_CONST: |
| 618 | case GDScriptTokenizer::Token::SIGNAL: |
| 619 | //case GDScriptTokenizer::Token::IF: // Can also be inside expressions. |
| 620 | case GDScriptTokenizer::Token::FOR: |
| 621 | case GDScriptTokenizer::Token::WHILE: |
| 622 | case GDScriptTokenizer::Token::MATCH: |
| 623 | case GDScriptTokenizer::Token::RETURN: |
| 624 | case GDScriptTokenizer::Token::ANNOTATION: |
| 625 | return; |
| 626 | default: |
| 627 | // Do nothing. |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | advance(); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void GDScriptParser::push_multiline(bool p_state) { |
| 636 | multiline_stack.push_back(p_state); |
| 637 | tokenizer->set_multiline_mode(p_state); |
| 638 | if (p_state) { |
| 639 | // Consume potential whitespace tokens already waiting in line. |
| 640 | while (current.type == GDScriptTokenizer::Token::NEWLINE || current.type == GDScriptTokenizer::Token::INDENT || current.type == GDScriptTokenizer::Token::DEDENT) { |
| 641 | current = tokenizer->scan(); // Don't call advance() here, as we don't want to change the previous token. |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | void GDScriptParser::pop_multiline() { |
| 647 | ERR_FAIL_COND_MSG(multiline_stack.is_empty(), "Parser bug: trying to pop from multiline stack without available value."); |
| 648 | multiline_stack.pop_back(); |
| 649 | tokenizer->set_multiline_mode(multiline_stack.size() > 0 ? multiline_stack.back()->get() : false); |
| 650 | } |
| 651 | |
| 652 | bool GDScriptParser::is_statement_end_token() const { |
| 653 | return check(GDScriptTokenizer::Token::NEWLINE) || check(GDScriptTokenizer::Token::SEMICOLON) || check(GDScriptTokenizer::Token::TK_EOF); |
| 654 | } |
| 655 | |
| 656 | bool GDScriptParser::is_statement_end() const { |
| 657 | return lambda_ended || in_lambda || is_statement_end_token(); |
| 658 | } |
| 659 | |
| 660 | void GDScriptParser::end_statement(const String &p_context) { |
| 661 | bool found = false; |
| 662 | while (is_statement_end() && !is_at_end()) { |
| 663 | // Remove sequential newlines/semicolons. |
| 664 | if (is_statement_end_token()) { |
| 665 | // Only consume if this is an actual token. |
| 666 | advance(); |
| 667 | } else if (lambda_ended) { |
| 668 | lambda_ended = false; // Consume this "token". |
| 669 | found = true; |
| 670 | break; |
| 671 | } else { |
| 672 | if (!found) { |
| 673 | lambda_ended = true; // Mark the lambda as done since we found something else to end the statement. |
| 674 | found = true; |
| 675 | } |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | found = true; |
| 680 | } |
| 681 | if (!found && !is_at_end()) { |
| 682 | push_error(vformat(R"(Expected end of statement after %s, found "%s" instead.)", p_context, current.get_name())); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | void GDScriptParser::parse_program() { |
| 687 | head = alloc_node<ClassNode>(); |
| 688 | head->start_line = 1; |
| 689 | head->end_line = 1; |
| 690 | head->fqcn = GDScript::canonicalize_path(script_path); |
| 691 | current_class = head; |
| 692 | bool can_have_class_or_extends = true; |
| 693 | |
| 694 | #define PUSH_PENDING_ANNOTATIONS_TO_HEAD \ |
| 695 | if (!annotation_stack.is_empty()) { \ |
| 696 | for (AnnotationNode *annot : annotation_stack) { \ |
| 697 | head->annotations.push_back(annot); \ |
| 698 | } \ |
| 699 | annotation_stack.clear(); \ |
| 700 | } |
| 701 | |
| 702 | while (!check(GDScriptTokenizer::Token::TK_EOF)) { |
| 703 | if (match(GDScriptTokenizer::Token::ANNOTATION)) { |
| 704 | AnnotationNode *annotation = parse_annotation(AnnotationInfo::SCRIPT | AnnotationInfo::CLASS_LEVEL | AnnotationInfo::STANDALONE); |
| 705 | if (annotation != nullptr) { |
| 706 | if (annotation->applies_to(AnnotationInfo::CLASS)) { |
| 707 | // We do not know in advance what the annotation will be applied to: the `head` class or the subsequent inner class. |
| 708 | // If we encounter `class_name`, `extends` or pure `SCRIPT` annotation, then it's `head`, otherwise it's an inner class. |
| 709 | annotation_stack.push_back(annotation); |
| 710 | } else if (annotation->applies_to(AnnotationInfo::SCRIPT)) { |
| 711 | PUSH_PENDING_ANNOTATIONS_TO_HEAD; |
| 712 | if (annotation->name == SNAME("@tool") || annotation->name == SNAME("@icon") || annotation->name == SNAME("@static_unload")) { |
| 713 | // Some annotations need to be resolved and applied in the parser. |
| 714 | // The root class is not in any class, so `head->outer == nullptr`. |
| 715 | annotation->apply(this, head, nullptr); |
| 716 | } else { |
| 717 | head->annotations.push_back(annotation); |
| 718 | } |
| 719 | } else if (annotation->applies_to(AnnotationInfo::STANDALONE)) { |
| 720 | if (previous.type != GDScriptTokenizer::Token::NEWLINE) { |
| 721 | push_error(R"(Expected newline after a standalone annotation.)"); |
| 722 | } |
| 723 | if (annotation->name == SNAME("@export_category") || annotation->name == SNAME("@export_group") || annotation->name == SNAME("@export_subgroup")) { |
| 724 | head->add_member_group(annotation); |
| 725 | // This annotation must appear after script-level annotations and `class_name`/`extends`, |
| 726 | // so we stop looking for script-level stuff. |
| 727 | can_have_class_or_extends = false; |
| 728 | break; |
| 729 | } else if (annotation->name == SNAME("@warning_ignore_start") || annotation->name == SNAME("@warning_ignore_restore")) { |
| 730 | // Some annotations need to be resolved and applied in the parser. |
| 731 | annotation->apply(this, nullptr, nullptr); |
| 732 | } else { |
| 733 | push_error(R"(Unexpected standalone annotation.)"); |
| 734 | } |
| 735 | } else { |
| 736 | annotation_stack.push_back(annotation); |
| 737 | // This annotation must appear after script-level annotations and `class_name`/`extends`, |
| 738 | // so we stop looking for script-level stuff. |
| 739 | can_have_class_or_extends = false; |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | } else if (check(GDScriptTokenizer::Token::LITERAL) && current.literal.get_type() == Variant::STRING) { |
| 744 | // Allow strings in class body as multiline comments. |
| 745 | advance(); |
| 746 | if (!match(GDScriptTokenizer::Token::NEWLINE)) { |
| 747 | push_error("Expected newline after comment string."); |
| 748 | } |
| 749 | } else { |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (current.type == GDScriptTokenizer::Token::CLASS_NAME || current.type == GDScriptTokenizer::Token::EXTENDS) { |
| 755 | // Set range of the class to only start at extends or class_name if present. |
| 756 | reset_extents(head, current); |
| 757 | } |
| 758 | |
| 759 | while (can_have_class_or_extends) { |
| 760 | // Order here doesn't matter, but there should be only one of each at most. |
| 761 | switch (current.type) { |
| 762 | case GDScriptTokenizer::Token::CLASS_NAME: |
| 763 | PUSH_PENDING_ANNOTATIONS_TO_HEAD; |
| 764 | advance(); |
| 765 | if (head->identifier != nullptr) { |
| 766 | push_error(R"("class_name" can only be used once.)"); |
| 767 | } else { |
| 768 | parse_class_name(); |
| 769 | } |
| 770 | break; |
| 771 | case GDScriptTokenizer::Token::EXTENDS: |
| 772 | PUSH_PENDING_ANNOTATIONS_TO_HEAD; |
| 773 | advance(); |
| 774 | if (head->extends_used) { |
| 775 | push_error(R"("extends" can only be used once.)"); |
| 776 | } else { |
| 777 | parse_extends(); |
| 778 | end_statement("superclass"); |
| 779 | } |
| 780 | break; |
| 781 | case GDScriptTokenizer::Token::TK_EOF: |
| 782 | PUSH_PENDING_ANNOTATIONS_TO_HEAD; |
| 783 | can_have_class_or_extends = false; |
| 784 | break; |
| 785 | case GDScriptTokenizer::Token::LITERAL: |
| 786 | if (current.literal.get_type() == Variant::STRING) { |
| 787 | // Allow strings in class body as multiline comments. |
| 788 | advance(); |
| 789 | if (!match(GDScriptTokenizer::Token::NEWLINE)) { |
| 790 | push_error("Expected newline after comment string."); |
| 791 | } |
| 792 | break; |
| 793 | } |
| 794 | [[fallthrough]]; |
| 795 | default: |
| 796 | // No tokens are allowed between script annotations and class/extends. |
| 797 | can_have_class_or_extends = false; |
| 798 | break; |
| 799 | } |
| 800 | |
| 801 | if (panic_mode) { |
| 802 | synchronize(); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | #undef PUSH_PENDING_ANNOTATIONS_TO_HEAD |
| 807 | |
| 808 | for (AnnotationNode *&annotation : head->annotations) { |
| 809 | if (annotation->name == SNAME("@abstract")) { |
| 810 | // Some annotations need to be resolved and applied in the parser. |
| 811 | // The root class is not in any class, so `head->outer == nullptr`. |
| 812 | annotation->apply(this, head, nullptr); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // When the only thing needed is the class name, icon, and abstractness; we don't need to parse the whole file. |
| 817 | // It really speed up the call to `GDScriptLanguage::get_global_class_name()` especially for large script. |
| 818 | if (!parse_body) { |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | parse_class_body(true); |
| 823 | |
| 824 | head->end_line = current.end_line; |
| 825 | head->end_column = current.end_column; |
| 826 | |
| 827 | complete_extents(head); |
| 828 | |
| 829 | #ifdef TOOLS_ENABLED |
| 830 | const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments(); |
| 831 | |
| 832 | int max_line = head->end_line; |
| 833 | if (!head->members.is_empty()) { |
| 834 | max_line = MIN(max_script_doc_line, head->members[0].get_line() - 1); |
| 835 | } |
| 836 | |
| 837 | int line = 0; |
| 838 | while (line <= max_line) { |
| 839 | // Find the start. |
| 840 | if (comments.has(line) && comments[line].new_line && comments[line].comment.begins_with("##")) { |
| 841 | // Find the end. |
| 842 | while (line + 1 <= max_line && comments.has(line + 1) && comments[line + 1].new_line && comments[line + 1].comment.begins_with("##")) { |
| 843 | line++; |
| 844 | } |
| 845 | head->doc_data = parse_class_doc_comment(line); |
| 846 | break; |
| 847 | } |
| 848 | line++; |
| 849 | } |
| 850 | #endif // TOOLS_ENABLED |
| 851 | |
| 852 | if (!check(GDScriptTokenizer::Token::TK_EOF)) { |
| 853 | push_error("Expected end of file."); |
| 854 | } |
| 855 | |
| 856 | clear_unused_annotations(); |
| 857 | } |
| 858 | |
| 859 | Ref<GDScriptParserRef> GDScriptParser::get_depended_parser_for(const String &p_path) { |
| 860 | Ref<GDScriptParserRef> ref; |
| 861 | if (depended_parsers.has(p_path)) { |
| 862 | ref = depended_parsers[p_path]; |
| 863 | } else { |
| 864 | Error err = OK; |
| 865 | ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, script_path); |
| 866 | if (ref.is_valid()) { |
| 867 | depended_parsers[p_path] = ref; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return ref; |
| 872 | } |
| 873 | |
| 874 | const HashMap<String, Ref<GDScriptParserRef>> &GDScriptParser::get_depended_parsers() { |
| 875 | return depended_parsers; |
| 876 | } |
| 877 | |
| 878 | GDScriptParser::ClassNode *GDScriptParser::find_class(const String &p_qualified_name) const { |
| 879 | String first = p_qualified_name.get_slice("::", 0); |
| 880 | |
| 881 | Vector<String> class_names; |
| 882 | GDScriptParser::ClassNode *result = nullptr; |
| 883 | // Empty initial name means start at the head. |
| 884 | if (first.is_empty() || (head->identifier && first == head->identifier->name)) { |
| 885 | class_names = p_qualified_name.split("::"); |
| 886 | result = head; |
| 887 | } else if (p_qualified_name.begins_with(script_path)) { |
| 888 | // Script path could have a class path separator("::") in it. |
| 889 | class_names = p_qualified_name.trim_prefix(script_path).split("::"); |
| 890 | result = head; |
| 891 | } else if (head->has_member(first)) { |
| 892 | class_names = p_qualified_name.split("::"); |
| 893 | GDScriptParser::ClassNode::Member member = head->get_member(first); |
| 894 | if (member.type == GDScriptParser::ClassNode::Member::CLASS) { |
| 895 | result = member.m_class; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | // Starts at index 1 because index 0 was handled above. |
| 900 | for (int i = 1; result != nullptr && i < class_names.size(); i++) { |
| 901 | const String ¤t_name = class_names[i]; |
| 902 | GDScriptParser::ClassNode *next = nullptr; |
| 903 | if (result->has_member(current_name)) { |
| 904 | GDScriptParser::ClassNode::Member member = result->get_member(current_name); |
| 905 | if (member.type == GDScriptParser::ClassNode::Member::CLASS) { |
| 906 | next = member.m_class; |
| 907 | } |
| 908 | } |
| 909 | result = next; |
| 910 | } |
| 911 | |
| 912 | return result; |
| 913 | } |
| 914 | |
| 915 | bool GDScriptParser::has_class(const GDScriptParser::ClassNode *p_class) const { |
| 916 | if (head->fqcn.is_empty() && p_class->fqcn.get_slice("::", 0).is_empty()) { |
| 917 | return p_class == head; |
| 918 | } else if (p_class->fqcn.begins_with(head->fqcn)) { |
| 919 | return find_class(p_class->fqcn.trim_prefix(head->fqcn)) == p_class; |
| 920 | } |
| 921 | |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | GDScriptParser::ClassNode *GDScriptParser::parse_class(bool p_is_static) { |
| 926 | ClassNode *n_class = alloc_node<ClassNode>(); |
| 927 | |
| 928 | ClassNode *previous_class = current_class; |
| 929 | current_class = n_class; |
| 930 | n_class->outer = previous_class; |
| 931 | |
| 932 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier for the class name after "class".)")) { |
| 933 | n_class->identifier = parse_identifier(); |
| 934 | if (n_class->outer) { |
| 935 | String fqcn = n_class->outer->fqcn; |
| 936 | if (fqcn.is_empty()) { |
| 937 | fqcn = GDScript::canonicalize_path(script_path); |
| 938 | } |
| 939 | n_class->fqcn = fqcn + "::" + n_class->identifier->name; |
| 940 | } else { |
| 941 | n_class->fqcn = n_class->identifier->name; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (match(GDScriptTokenizer::Token::EXTENDS)) { |
| 946 | parse_extends(); |
| 947 | } |
| 948 | |
| 949 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after class declaration.)"); |
| 950 | |
| 951 | bool multiline = match(GDScriptTokenizer::Token::NEWLINE); |
| 952 | |
| 953 | if (multiline && !consume(GDScriptTokenizer::Token::INDENT, R"(Expected indented block after class declaration.)")) { |
| 954 | current_class = previous_class; |
| 955 | complete_extents(n_class); |
| 956 | return n_class; |
| 957 | } |
| 958 | |
| 959 | if (match(GDScriptTokenizer::Token::EXTENDS)) { |
| 960 | if (n_class->extends_used) { |
| 961 | push_error(R"(Cannot use "extends" more than once in the same class.)"); |
| 962 | } |
| 963 | parse_extends(); |
| 964 | end_statement("superclass"); |
| 965 | } |
| 966 | |
| 967 | parse_class_body(multiline); |
| 968 | complete_extents(n_class); |
| 969 | |
| 970 | if (multiline) { |
| 971 | consume(GDScriptTokenizer::Token::DEDENT, R"(Missing unindent at the end of the class body.)"); |
| 972 | } |
| 973 | |
| 974 | current_class = previous_class; |
| 975 | return n_class; |
| 976 | } |
| 977 | |
| 978 | void GDScriptParser::parse_class_name() { |
| 979 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier for the global class name after "class_name".)")) { |
| 980 | current_class->identifier = parse_identifier(); |
| 981 | current_class->fqcn = String(current_class->identifier->name); |
| 982 | } |
| 983 | |
| 984 | if (match(GDScriptTokenizer::Token::EXTENDS)) { |
| 985 | // Allow extends on the same line. |
| 986 | parse_extends(); |
| 987 | end_statement("superclass"); |
| 988 | } else { |
| 989 | end_statement("class_name statement"); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | void GDScriptParser::parse_extends() { |
| 994 | current_class->extends_used = true; |
| 995 | |
| 996 | int chain_index = 0; |
| 997 | |
| 998 | if (match(GDScriptTokenizer::Token::LITERAL)) { |
| 999 | if (previous.literal.get_type() != Variant::STRING) { |
| 1000 | push_error(vformat(R"(Only strings or identifiers can be used after "extends", found "%s" instead.)", Variant::get_type_name(previous.literal.get_type()))); |
| 1001 | } |
| 1002 | current_class->extends_path = previous.literal; |
| 1003 | |
| 1004 | if (!match(GDScriptTokenizer::Token::PERIOD)) { |
| 1005 | return; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | make_completion_context(COMPLETION_INHERIT_TYPE, current_class, chain_index++); |
| 1010 | |
| 1011 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected superclass name after "extends".)")) { |
| 1012 | return; |
| 1013 | } |
| 1014 | current_class->extends.push_back(parse_identifier()); |
| 1015 | |
| 1016 | while (match(GDScriptTokenizer::Token::PERIOD)) { |
| 1017 | make_completion_context(COMPLETION_INHERIT_TYPE, current_class, chain_index++); |
| 1018 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected superclass name after ".".)")) { |
| 1019 | return; |
| 1020 | } |
| 1021 | current_class->extends.push_back(parse_identifier()); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | template <typename T> |
| 1026 | void GDScriptParser::parse_class_member(T *(GDScriptParser::*p_parse_function)(bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_static) { |
| 1027 | advance(); |
| 1028 | |
| 1029 | // Consume annotations. |
| 1030 | List<AnnotationNode *> annotations; |
| 1031 | while (!annotation_stack.is_empty()) { |
| 1032 | AnnotationNode *last_annotation = annotation_stack.back()->get(); |
| 1033 | if (last_annotation->applies_to(p_target)) { |
| 1034 | annotations.push_front(last_annotation); |
| 1035 | annotation_stack.pop_back(); |
| 1036 | } else { |
| 1037 | push_error(vformat(R"(Annotation "%s" cannot be applied to a %s.)", last_annotation->name, p_member_kind)); |
| 1038 | clear_unused_annotations(); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | T *member = (this->*p_parse_function)(p_is_static); |
| 1043 | if (member == nullptr) { |
| 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | #ifdef TOOLS_ENABLED |
| 1048 | int doc_comment_line = member->start_line - 1; |
| 1049 | #endif // TOOLS_ENABLED |
| 1050 | |
| 1051 | for (AnnotationNode *&annotation : annotations) { |
| 1052 | member->annotations.push_back(annotation); |
| 1053 | #ifdef TOOLS_ENABLED |
| 1054 | if (annotation->start_line <= doc_comment_line) { |
| 1055 | doc_comment_line = annotation->start_line - 1; |
| 1056 | } |
| 1057 | #endif // TOOLS_ENABLED |
| 1058 | } |
| 1059 | |
| 1060 | #ifdef TOOLS_ENABLED |
| 1061 | if constexpr (std::is_same_v<T, ClassNode>) { |
| 1062 | if (has_comment(member->start_line, true)) { |
| 1063 | // Inline doc comment. |
| 1064 | member->doc_data = parse_class_doc_comment(member->start_line, true); |
| 1065 | } else if (has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) { |
| 1066 | // Normal doc comment. Don't check `min_member_doc_line` because a class ends parsing after its members. |
| 1067 | // This may not work correctly for cases like `var a; class B`, but it doesn't matter in practice. |
| 1068 | member->doc_data = parse_class_doc_comment(doc_comment_line); |
| 1069 | } |
| 1070 | } else { |
| 1071 | if (has_comment(member->start_line, true)) { |
| 1072 | // Inline doc comment. |
| 1073 | member->doc_data = parse_doc_comment(member->start_line, true); |
| 1074 | } else if (doc_comment_line >= min_member_doc_line && has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) { |
| 1075 | // Normal doc comment. |
| 1076 | member->doc_data = parse_doc_comment(doc_comment_line); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | min_member_doc_line = member->end_line + 1; // Prevent multiple members from using the same doc comment. |
| 1081 | #endif // TOOLS_ENABLED |
| 1082 | |
| 1083 | if (member->identifier != nullptr) { |
| 1084 | if (!((String)member->identifier->name).is_empty()) { // Enums may be unnamed. |
| 1085 | if (current_class->members_indices.has(member->identifier->name)) { |
| 1086 | push_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); |
| 1087 | } else { |
| 1088 | current_class->add_member(member); |
| 1089 | } |
| 1090 | } else { |
| 1091 | current_class->add_member(member); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | void GDScriptParser::parse_class_body(bool p_is_multiline) { |
| 1097 | bool class_end = false; |
| 1098 | bool next_is_static = false; |
| 1099 | while (!class_end && !is_at_end()) { |
| 1100 | GDScriptTokenizer::Token token = current; |
| 1101 | switch (token.type) { |
| 1102 | case GDScriptTokenizer::Token::VAR: |
| 1103 | parse_class_member(&GDScriptParser::parse_variable, AnnotationInfo::VARIABLE, "variable", next_is_static); |
| 1104 | if (next_is_static) { |
| 1105 | current_class->has_static_data = true; |
| 1106 | } |
| 1107 | break; |
| 1108 | case GDScriptTokenizer::Token::TK_CONST: |
| 1109 | parse_class_member(&GDScriptParser::parse_constant, AnnotationInfo::CONSTANT, "constant"); |
| 1110 | break; |
| 1111 | case GDScriptTokenizer::Token::SIGNAL: |
| 1112 | parse_class_member(&GDScriptParser::parse_signal, AnnotationInfo::SIGNAL, "signal"); |
| 1113 | break; |
| 1114 | case GDScriptTokenizer::Token::FUNC: |
| 1115 | parse_class_member(&GDScriptParser::parse_function, AnnotationInfo::FUNCTION, "function", next_is_static); |
| 1116 | break; |
| 1117 | case GDScriptTokenizer::Token::CLASS: |
| 1118 | parse_class_member(&GDScriptParser::parse_class, AnnotationInfo::CLASS, "class"); |
| 1119 | break; |
| 1120 | case GDScriptTokenizer::Token::ENUM: |
| 1121 | parse_class_member(&GDScriptParser::parse_enum, AnnotationInfo::NONE, "enum"); |
| 1122 | break; |
| 1123 | case GDScriptTokenizer::Token::STATIC: { |
| 1124 | advance(); |
| 1125 | next_is_static = true; |
| 1126 | if (!check(GDScriptTokenizer::Token::FUNC) && !check(GDScriptTokenizer::Token::VAR)) { |
| 1127 | push_error(R"(Expected "func" or "var" after "static".)"); |
| 1128 | } |
| 1129 | } break; |
| 1130 | case GDScriptTokenizer::Token::ANNOTATION: { |
| 1131 | advance(); |
| 1132 | |
| 1133 | // Check for class-level and standalone annotations. |
| 1134 | AnnotationNode *annotation = parse_annotation(AnnotationInfo::CLASS_LEVEL | AnnotationInfo::STANDALONE); |
| 1135 | if (annotation != nullptr) { |
| 1136 | if (annotation->applies_to(AnnotationInfo::STANDALONE)) { |
| 1137 | if (previous.type != GDScriptTokenizer::Token::NEWLINE) { |
| 1138 | push_error(R"(Expected newline after a standalone annotation.)"); |
| 1139 | } |
| 1140 | if (annotation->name == SNAME("@export_category") || annotation->name == SNAME("@export_group") || annotation->name == SNAME("@export_subgroup")) { |
| 1141 | current_class->add_member_group(annotation); |
| 1142 | } else if (annotation->name == SNAME("@warning_ignore_start") || annotation->name == SNAME("@warning_ignore_restore")) { |
| 1143 | // Some annotations need to be resolved and applied in the parser. |
| 1144 | annotation->apply(this, nullptr, nullptr); |
| 1145 | } else { |
| 1146 | push_error(R"(Unexpected standalone annotation.)"); |
| 1147 | } |
| 1148 | } else { // `AnnotationInfo::CLASS_LEVEL`. |
| 1149 | annotation_stack.push_back(annotation); |
| 1150 | } |
| 1151 | } |
| 1152 | break; |
| 1153 | } |
| 1154 | case GDScriptTokenizer::Token::PASS: |
| 1155 | advance(); |
| 1156 | end_statement(R"("pass")"); |
| 1157 | break; |
| 1158 | case GDScriptTokenizer::Token::DEDENT: |
| 1159 | class_end = true; |
| 1160 | break; |
| 1161 | case GDScriptTokenizer::Token::LITERAL: |
| 1162 | if (current.literal.get_type() == Variant::STRING) { |
| 1163 | // Allow strings in class body as multiline comments. |
| 1164 | advance(); |
| 1165 | if (!match(GDScriptTokenizer::Token::NEWLINE)) { |
| 1166 | push_error("Expected newline after comment string."); |
| 1167 | } |
| 1168 | break; |
| 1169 | } |
| 1170 | [[fallthrough]]; |
| 1171 | default: |
| 1172 | // Display a completion with identifiers. |
| 1173 | make_completion_context(COMPLETION_IDENTIFIER, nullptr); |
| 1174 | advance(); |
| 1175 | if (previous.get_identifier() == "export") { |
| 1176 | push_error(R"(The "export" keyword was removed in Godot 4. Use an export annotation ("@export", "@export_range", etc.) instead.)"); |
| 1177 | } else if (previous.get_identifier() == "tool") { |
| 1178 | push_error(R"(The "tool" keyword was removed in Godot 4. Use the "@tool" annotation instead.)"); |
| 1179 | } else if (previous.get_identifier() == "onready") { |
| 1180 | push_error(R"(The "onready" keyword was removed in Godot 4. Use the "@onready" annotation instead.)"); |
| 1181 | } else if (previous.get_identifier() == "remote") { |
| 1182 | push_error(R"(The "remote" keyword was removed in Godot 4. Use the "@rpc" annotation with "any_peer" instead.)"); |
| 1183 | } else if (previous.get_identifier() == "remotesync") { |
| 1184 | push_error(R"(The "remotesync" keyword was removed in Godot 4. Use the "@rpc" annotation with "any_peer" and "call_local" instead.)"); |
| 1185 | } else if (previous.get_identifier() == "puppet") { |
| 1186 | push_error(R"(The "puppet" keyword was removed in Godot 4. Use the "@rpc" annotation with "authority" instead.)"); |
| 1187 | } else if (previous.get_identifier() == "puppetsync") { |
| 1188 | push_error(R"(The "puppetsync" keyword was removed in Godot 4. Use the "@rpc" annotation with "authority" and "call_local" instead.)"); |
| 1189 | } else if (previous.get_identifier() == "master") { |
| 1190 | push_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.)"); |
| 1191 | } else if (previous.get_identifier() == "mastersync") { |
| 1192 | push_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.)"); |
| 1193 | } else { |
| 1194 | push_error(vformat(R"(Unexpected %s in class body.)", previous.get_debug_name())); |
| 1195 | } |
| 1196 | break; |
| 1197 | } |
| 1198 | if (token.type != GDScriptTokenizer::Token::STATIC) { |
| 1199 | next_is_static = false; |
| 1200 | } |
| 1201 | if (panic_mode) { |
| 1202 | synchronize(); |
| 1203 | } |
| 1204 | if (!p_is_multiline) { |
| 1205 | class_end = true; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static) { |
| 1211 | return parse_variable(p_is_static, true); |
| 1212 | } |
| 1213 | |
| 1214 | GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static, bool p_allow_property) { |
| 1215 | VariableNode *variable = alloc_node<VariableNode>(); |
| 1216 | |
| 1217 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected variable name after "var".)")) { |
| 1218 | complete_extents(variable); |
| 1219 | return nullptr; |
| 1220 | } |
| 1221 | |
| 1222 | variable->identifier = parse_identifier(); |
| 1223 | variable->export_info.name = variable->identifier->name; |
| 1224 | variable->is_static = p_is_static; |
| 1225 | |
| 1226 | if (match(GDScriptTokenizer::Token::COLON)) { |
| 1227 | if (check(GDScriptTokenizer::Token::NEWLINE)) { |
| 1228 | if (p_allow_property) { |
| 1229 | advance(); |
| 1230 | return parse_property(variable, true); |
| 1231 | } else { |
| 1232 | push_error(R"(Expected type after ":")"); |
| 1233 | complete_extents(variable); |
| 1234 | return nullptr; |
| 1235 | } |
| 1236 | } else if (check((GDScriptTokenizer::Token::EQUAL))) { |
| 1237 | // Infer type. |
| 1238 | variable->infer_datatype = true; |
| 1239 | } else { |
| 1240 | if (p_allow_property) { |
| 1241 | make_completion_context(COMPLETION_PROPERTY_DECLARATION_OR_TYPE, variable); |
| 1242 | if (check(GDScriptTokenizer::Token::IDENTIFIER)) { |
| 1243 | // Check if get or set. |
| 1244 | if (current.get_identifier() == "get" || current.get_identifier() == "set") { |
| 1245 | return parse_property(variable, false); |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | // Parse type. |
| 1251 | variable->datatype_specifier = parse_type(); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | if (match(GDScriptTokenizer::Token::EQUAL)) { |
| 1256 | // Initializer. |
| 1257 | variable->initializer = parse_expression(false); |
| 1258 | if (variable->initializer == nullptr) { |
| 1259 | push_error(R"(Expected expression for variable initial value after "=".)"); |
| 1260 | } |
| 1261 | variable->assignments++; |
| 1262 | } |
| 1263 | |
| 1264 | if (p_allow_property && match(GDScriptTokenizer::Token::COLON)) { |
| 1265 | if (match(GDScriptTokenizer::Token::NEWLINE)) { |
| 1266 | return parse_property(variable, true); |
| 1267 | } else { |
| 1268 | return parse_property(variable, false); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | complete_extents(variable); |
| 1273 | end_statement("variable declaration"); |
| 1274 | |
| 1275 | return variable; |
| 1276 | } |
| 1277 | |
| 1278 | GDScriptParser::VariableNode *GDScriptParser::parse_property(VariableNode *p_variable, bool p_need_indent) { |
| 1279 | if (p_need_indent) { |
| 1280 | if (!consume(GDScriptTokenizer::Token::INDENT, R"(Expected indented block for property after ":".)")) { |
| 1281 | complete_extents(p_variable); |
| 1282 | return nullptr; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | VariableNode *property = p_variable; |
| 1287 | |
| 1288 | make_completion_context(COMPLETION_PROPERTY_DECLARATION, property); |
| 1289 | |
| 1290 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected "get" or "set" for property declaration.)")) { |
| 1291 | complete_extents(p_variable); |
| 1292 | return nullptr; |
| 1293 | } |
| 1294 | |
| 1295 | IdentifierNode *function = parse_identifier(); |
| 1296 | |
| 1297 | if (check(GDScriptTokenizer::Token::EQUAL)) { |
| 1298 | p_variable->property = VariableNode::PROP_SETGET; |
| 1299 | } else { |
| 1300 | p_variable->property = VariableNode::PROP_INLINE; |
| 1301 | if (!p_need_indent) { |
| 1302 | push_error("Property with inline code must go to an indented block."); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | bool getter_used = false; |
| 1307 | bool setter_used = false; |
| 1308 | |
| 1309 | // Run with a loop because order doesn't matter. |
| 1310 | for (int i = 0; i < 2; i++) { |
| 1311 | if (function->name == SNAME("set")) { |
| 1312 | if (setter_used) { |
| 1313 | push_error(R"(Properties can only have one setter.)"); |
| 1314 | } else { |
| 1315 | parse_property_setter(property); |
| 1316 | setter_used = true; |
| 1317 | } |
| 1318 | } else if (function->name == SNAME("get")) { |
| 1319 | if (getter_used) { |
| 1320 | push_error(R"(Properties can only have one getter.)"); |
| 1321 | } else { |
| 1322 | parse_property_getter(property); |
| 1323 | getter_used = true; |
| 1324 | } |
| 1325 | } else { |
| 1326 | // TODO: Update message to only have the missing one if it's the case. |
| 1327 | push_error(R"(Expected "get" or "set" for property declaration.)"); |
| 1328 | } |
| 1329 | |
| 1330 | if (i == 0 && p_variable->property == VariableNode::PROP_SETGET) { |
| 1331 | if (match(GDScriptTokenizer::Token::COMMA)) { |
| 1332 | // Consume potential newline. |
| 1333 | if (match(GDScriptTokenizer::Token::NEWLINE)) { |
| 1334 | if (!p_need_indent) { |
| 1335 | push_error(R"(Inline setter/getter setting cannot span across multiple lines (use "\\"" if needed).)"); |
| 1336 | } |
| 1337 | } |
| 1338 | } else { |
| 1339 | break; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | if (!match(GDScriptTokenizer::Token::IDENTIFIER)) { |
| 1344 | break; |
| 1345 | } |
| 1346 | function = parse_identifier(); |
| 1347 | } |
| 1348 | complete_extents(p_variable); |
| 1349 | |
| 1350 | if (p_variable->property == VariableNode::PROP_SETGET) { |
| 1351 | end_statement("property declaration"); |
| 1352 | } |
| 1353 | |
| 1354 | if (p_need_indent) { |
| 1355 | consume(GDScriptTokenizer::Token::DEDENT, R"(Expected end of indented block for property.)"); |
| 1356 | } |
| 1357 | return property; |
| 1358 | } |
| 1359 | |
| 1360 | void GDScriptParser::parse_property_setter(VariableNode *p_variable) { |
| 1361 | switch (p_variable->property) { |
| 1362 | case VariableNode::PROP_INLINE: { |
| 1363 | FunctionNode *function = alloc_node<FunctionNode>(); |
| 1364 | IdentifierNode *identifier = alloc_node<IdentifierNode>(); |
| 1365 | complete_extents(identifier); |
| 1366 | identifier->name = "@" + p_variable->identifier->name + "_setter"; |
| 1367 | function->identifier = identifier; |
| 1368 | function->is_static = p_variable->is_static; |
| 1369 | |
| 1370 | consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after "set".)"); |
| 1371 | |
| 1372 | ParameterNode *parameter = alloc_node<ParameterNode>(); |
| 1373 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected parameter name after "(".)")) { |
| 1374 | reset_extents(parameter, previous); |
| 1375 | p_variable->setter_parameter = parse_identifier(); |
| 1376 | parameter->identifier = p_variable->setter_parameter; |
| 1377 | function->parameters_indices[parameter->identifier->name] = 0; |
| 1378 | function->parameters.push_back(parameter); |
| 1379 | } |
| 1380 | complete_extents(parameter); |
| 1381 | |
| 1382 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after parameter name.)*"); |
| 1383 | consume(GDScriptTokenizer::Token::COLON, R"*(Expected ":" after ")".)*"); |
| 1384 | |
| 1385 | FunctionNode *previous_function = current_function; |
| 1386 | current_function = function; |
| 1387 | if (p_variable->setter_parameter != nullptr) { |
| 1388 | SuiteNode *body = alloc_node<SuiteNode>(); |
| 1389 | body->add_local(parameter, function); |
| 1390 | function->body = parse_suite("setter declaration", body); |
| 1391 | p_variable->setter = function; |
| 1392 | } |
| 1393 | current_function = previous_function; |
| 1394 | complete_extents(function); |
| 1395 | break; |
| 1396 | } |
| 1397 | case VariableNode::PROP_SETGET: |
| 1398 | consume(GDScriptTokenizer::Token::EQUAL, R"(Expected "=" after "set")"); |
| 1399 | make_completion_context(COMPLETION_PROPERTY_METHOD, p_variable); |
| 1400 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected setter function name after "=".)")) { |
| 1401 | p_variable->setter_pointer = parse_identifier(); |
| 1402 | } |
| 1403 | break; |
| 1404 | case VariableNode::PROP_NONE: |
| 1405 | break; // Unreachable. |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | void GDScriptParser::parse_property_getter(VariableNode *p_variable) { |
| 1410 | switch (p_variable->property) { |
| 1411 | case VariableNode::PROP_INLINE: { |
| 1412 | FunctionNode *function = alloc_node<FunctionNode>(); |
| 1413 | |
| 1414 | if (match(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) { |
| 1415 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after "get(".)*"); |
| 1416 | consume(GDScriptTokenizer::Token::COLON, R"*(Expected ":" after "get()".)*"); |
| 1417 | } else { |
| 1418 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" or "(" after "get".)"); |
| 1419 | } |
| 1420 | |
| 1421 | IdentifierNode *identifier = alloc_node<IdentifierNode>(); |
| 1422 | complete_extents(identifier); |
| 1423 | identifier->name = "@" + p_variable->identifier->name + "_getter"; |
| 1424 | function->identifier = identifier; |
| 1425 | function->is_static = p_variable->is_static; |
| 1426 | |
| 1427 | FunctionNode *previous_function = current_function; |
| 1428 | current_function = function; |
| 1429 | |
| 1430 | SuiteNode *body = alloc_node<SuiteNode>(); |
| 1431 | function->body = parse_suite("getter declaration", body); |
| 1432 | p_variable->getter = function; |
| 1433 | |
| 1434 | current_function = previous_function; |
| 1435 | complete_extents(function); |
| 1436 | break; |
| 1437 | } |
| 1438 | case VariableNode::PROP_SETGET: |
| 1439 | consume(GDScriptTokenizer::Token::EQUAL, R"(Expected "=" after "get")"); |
| 1440 | make_completion_context(COMPLETION_PROPERTY_METHOD, p_variable); |
| 1441 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected getter function name after "=".)")) { |
| 1442 | p_variable->getter_pointer = parse_identifier(); |
| 1443 | } |
| 1444 | break; |
| 1445 | case VariableNode::PROP_NONE: |
| 1446 | break; // Unreachable. |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | GDScriptParser::ConstantNode *GDScriptParser::parse_constant(bool p_is_static) { |
| 1451 | ConstantNode *constant = alloc_node<ConstantNode>(); |
| 1452 | |
| 1453 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected constant name after "const".)")) { |
| 1454 | complete_extents(constant); |
| 1455 | return nullptr; |
| 1456 | } |
| 1457 | |
| 1458 | constant->identifier = parse_identifier(); |
| 1459 | |
| 1460 | if (match(GDScriptTokenizer::Token::COLON)) { |
| 1461 | if (check((GDScriptTokenizer::Token::EQUAL))) { |
| 1462 | // Infer type. |
| 1463 | constant->infer_datatype = true; |
| 1464 | } else { |
| 1465 | // Parse type. |
| 1466 | constant->datatype_specifier = parse_type(); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | if (consume(GDScriptTokenizer::Token::EQUAL, R"(Expected initializer after constant name.)")) { |
| 1471 | // Initializer. |
| 1472 | constant->initializer = parse_expression(false); |
| 1473 | |
| 1474 | if (constant->initializer == nullptr) { |
| 1475 | push_error(R"(Expected initializer expression for constant.)"); |
| 1476 | complete_extents(constant); |
| 1477 | return nullptr; |
| 1478 | } |
| 1479 | } else { |
| 1480 | complete_extents(constant); |
| 1481 | return nullptr; |
| 1482 | } |
| 1483 | |
| 1484 | complete_extents(constant); |
| 1485 | end_statement("constant declaration"); |
| 1486 | |
| 1487 | return constant; |
| 1488 | } |
| 1489 | |
| 1490 | GDScriptParser::ParameterNode *GDScriptParser::parse_parameter() { |
| 1491 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected parameter name.)")) { |
| 1492 | return nullptr; |
| 1493 | } |
| 1494 | |
| 1495 | ParameterNode *parameter = alloc_node<ParameterNode>(); |
| 1496 | parameter->identifier = parse_identifier(); |
| 1497 | |
| 1498 | if (match(GDScriptTokenizer::Token::COLON)) { |
| 1499 | if (check((GDScriptTokenizer::Token::EQUAL))) { |
| 1500 | // Infer type. |
| 1501 | parameter->infer_datatype = true; |
| 1502 | } else { |
| 1503 | // Parse type. |
| 1504 | make_completion_context(COMPLETION_TYPE_NAME, parameter); |
| 1505 | parameter->datatype_specifier = parse_type(); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | if (match(GDScriptTokenizer::Token::EQUAL)) { |
| 1510 | // Default value. |
| 1511 | parameter->initializer = parse_expression(false); |
| 1512 | } |
| 1513 | |
| 1514 | complete_extents(parameter); |
| 1515 | return parameter; |
| 1516 | } |
| 1517 | |
| 1518 | GDScriptParser::SignalNode *GDScriptParser::parse_signal(bool p_is_static) { |
| 1519 | SignalNode *signal = alloc_node<SignalNode>(); |
| 1520 | |
| 1521 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected signal name after "signal".)")) { |
| 1522 | complete_extents(signal); |
| 1523 | return nullptr; |
| 1524 | } |
| 1525 | |
| 1526 | signal->identifier = parse_identifier(); |
| 1527 | |
| 1528 | if (check(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) { |
| 1529 | push_multiline(true); |
| 1530 | advance(); |
| 1531 | do { |
| 1532 | if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { |
| 1533 | // Allow for trailing comma. |
| 1534 | break; |
| 1535 | } |
| 1536 | |
| 1537 | ParameterNode *parameter = parse_parameter(); |
| 1538 | if (parameter == nullptr) { |
| 1539 | push_error("Expected signal parameter name."); |
| 1540 | break; |
| 1541 | } |
| 1542 | if (parameter->initializer != nullptr) { |
| 1543 | push_error(R"(Signal parameters cannot have a default value.)"); |
| 1544 | } |
| 1545 | if (signal->parameters_indices.has(parameter->identifier->name)) { |
| 1546 | push_error(vformat(R"(Parameter with name "%s" was already declared for this signal.)", parameter->identifier->name)); |
| 1547 | } else { |
| 1548 | signal->parameters_indices[parameter->identifier->name] = signal->parameters.size(); |
| 1549 | signal->parameters.push_back(parameter); |
| 1550 | } |
| 1551 | } while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end()); |
| 1552 | |
| 1553 | pop_multiline(); |
| 1554 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after signal parameters.)*"); |
| 1555 | } |
| 1556 | |
| 1557 | complete_extents(signal); |
| 1558 | end_statement("signal declaration"); |
| 1559 | |
| 1560 | return signal; |
| 1561 | } |
| 1562 | |
| 1563 | GDScriptParser::EnumNode *GDScriptParser::parse_enum(bool p_is_static) { |
| 1564 | EnumNode *enum_node = alloc_node<EnumNode>(); |
| 1565 | bool named = false; |
| 1566 | |
| 1567 | if (match(GDScriptTokenizer::Token::IDENTIFIER)) { |
| 1568 | enum_node->identifier = parse_identifier(); |
| 1569 | named = true; |
| 1570 | } |
| 1571 | |
| 1572 | push_multiline(true); |
| 1573 | consume(GDScriptTokenizer::Token::BRACE_OPEN, vformat(R"(Expected "{" after %s.)", named ? "enum name" : R"("enum")")); |
| 1574 | #ifdef TOOLS_ENABLED |
| 1575 | int min_enum_value_doc_line = previous.end_line + 1; |
| 1576 | #endif |
| 1577 | |
| 1578 | HashMap<StringName, int> elements; |
| 1579 | |
| 1580 | #ifdef DEBUG_ENABLED |
| 1581 | List<MethodInfo> gdscript_funcs; |
| 1582 | GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs); |
| 1583 | #endif |
| 1584 | |
| 1585 | do { |
| 1586 | if (check(GDScriptTokenizer::Token::BRACE_CLOSE)) { |
| 1587 | break; // Allow trailing comma. |
| 1588 | } |
| 1589 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier for enum key.)")) { |
| 1590 | GDScriptParser::IdentifierNode *identifier = parse_identifier(); |
| 1591 | |
| 1592 | EnumNode::Value item; |
| 1593 | item.identifier = identifier; |
| 1594 | item.parent_enum = enum_node; |
| 1595 | item.line = previous.start_line; |
| 1596 | item.start_column = previous.start_column; |
| 1597 | item.end_column = previous.end_column; |
| 1598 | |
| 1599 | if (elements.has(item.identifier->name)) { |
| 1600 | push_error(vformat(R"(Name "%s" was already in this enum (at line %d).)", item.identifier->name, elements[item.identifier->name]), item.identifier); |
| 1601 | } else if (!named) { |
| 1602 | if (current_class->members_indices.has(item.identifier->name)) { |
| 1603 | push_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())); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | elements[item.identifier->name] = item.line; |
| 1608 | |
| 1609 | if (match(GDScriptTokenizer::Token::EQUAL)) { |
| 1610 | ExpressionNode *value = parse_expression(false); |
| 1611 | if (value == nullptr) { |
| 1612 | push_error(R"(Expected expression value after "=".)"); |
| 1613 | } |
| 1614 | item.custom_value = value; |
| 1615 | } |
| 1616 | |
| 1617 | item.index = enum_node->values.size(); |
| 1618 | enum_node->values.push_back(item); |
| 1619 | if (!named) { |
| 1620 | // Add as member of current class. |
| 1621 | current_class->add_member(item); |
| 1622 | } |
| 1623 | } |
| 1624 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 1625 | |
| 1626 | #ifdef TOOLS_ENABLED |
| 1627 | // Enum values documentation. |
| 1628 | for (int i = 0; i < enum_node->values.size(); i++) { |
| 1629 | int enum_value_line = enum_node->values[i].line; |
| 1630 | int doc_comment_line = enum_value_line - 1; |
| 1631 | |
| 1632 | MemberDocData doc_data; |
| 1633 | if (has_comment(enum_value_line, true)) { |
| 1634 | // Inline doc comment. |
| 1635 | if (i == enum_node->values.size() - 1 || enum_node->values[i + 1].line > enum_value_line) { |
| 1636 | doc_data = parse_doc_comment(enum_value_line, true); |
| 1637 | } |
| 1638 | } else if (doc_comment_line >= min_enum_value_doc_line && has_comment(doc_comment_line, true) && tokenizer->get_comments()[doc_comment_line].new_line) { |
| 1639 | // Normal doc comment. |
| 1640 | doc_data = parse_doc_comment(doc_comment_line); |
| 1641 | } |
| 1642 | |
| 1643 | if (named) { |
| 1644 | enum_node->values.write[i].doc_data = doc_data; |
| 1645 | } else { |
| 1646 | current_class->set_enum_value_doc_data(enum_node->values[i].identifier->name, doc_data); |
| 1647 | } |
| 1648 | |
| 1649 | min_enum_value_doc_line = enum_value_line + 1; // Prevent multiple enum values from using the same doc comment. |
| 1650 | } |
| 1651 | #endif // TOOLS_ENABLED |
| 1652 | |
| 1653 | pop_multiline(); |
| 1654 | consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected closing "}" for enum.)"); |
| 1655 | complete_extents(enum_node); |
| 1656 | end_statement("enum"); |
| 1657 | |
| 1658 | return enum_node; |
| 1659 | } |
| 1660 | |
| 1661 | bool GDScriptParser::parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type, int p_signature_start) { |
| 1662 | if (!check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE) && !is_at_end()) { |
| 1663 | bool default_used = false; |
| 1664 | do { |
| 1665 | if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { |
| 1666 | // Allow for trailing comma. |
| 1667 | break; |
| 1668 | } |
| 1669 | |
| 1670 | bool is_rest = false; |
| 1671 | if (match(GDScriptTokenizer::Token::PERIOD_PERIOD_PERIOD)) { |
| 1672 | is_rest = true; |
| 1673 | } |
| 1674 | |
| 1675 | ParameterNode *parameter = parse_parameter(); |
| 1676 | if (parameter == nullptr) { |
| 1677 | break; |
| 1678 | } |
| 1679 | |
| 1680 | if (p_function->is_vararg()) { |
| 1681 | push_error("Cannot have parameters after the rest parameter."); |
| 1682 | continue; |
| 1683 | } |
| 1684 | |
| 1685 | if (parameter->initializer != nullptr) { |
| 1686 | if (is_rest) { |
| 1687 | push_error("The rest parameter cannot have a default value."); |
| 1688 | continue; |
| 1689 | } |
| 1690 | default_used = true; |
| 1691 | } else { |
| 1692 | if (default_used && !is_rest) { |
| 1693 | push_error("Cannot have mandatory parameters after optional parameters."); |
| 1694 | continue; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | if (p_function->parameters_indices.has(parameter->identifier->name)) { |
| 1699 | push_error(vformat(R"(Parameter with name "%s" was already declared for this %s.)", parameter->identifier->name, p_type)); |
| 1700 | } else if (is_rest) { |
| 1701 | p_function->rest_parameter = parameter; |
| 1702 | p_body->add_local(parameter, current_function); |
| 1703 | } else { |
| 1704 | p_function->parameters_indices[parameter->identifier->name] = p_function->parameters.size(); |
| 1705 | p_function->parameters.push_back(parameter); |
| 1706 | p_body->add_local(parameter, current_function); |
| 1707 | } |
| 1708 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 1709 | } |
| 1710 | |
| 1711 | pop_multiline(); |
| 1712 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, vformat(R"*(Expected closing ")" after %s parameters.)*", p_type)); |
| 1713 | |
| 1714 | if (match(GDScriptTokenizer::Token::FORWARD_ARROW)) { |
| 1715 | make_completion_context(COMPLETION_TYPE_NAME_OR_VOID, p_function); |
| 1716 | p_function->return_type = parse_type(true); |
| 1717 | if (p_function->return_type == nullptr) { |
| 1718 | push_error(R"(Expected return type or "void" after "->".)"); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | if (!p_function->source_lambda && p_function->identifier && p_function->identifier->name == GDScriptLanguage::get_singleton()->strings._static_init) { |
| 1723 | if (!p_function->is_static) { |
| 1724 | push_error(R"(Static constructor must be declared static.)"); |
| 1725 | } |
| 1726 | if (!p_function->parameters.is_empty() || p_function->is_vararg()) { |
| 1727 | push_error(R"(Static constructor cannot have parameters.)"); |
| 1728 | } |
| 1729 | current_class->has_static_data = true; |
| 1730 | } |
| 1731 | |
| 1732 | #ifdef TOOLS_ENABLED |
| 1733 | if (p_type == "function" && p_signature_start != -1) { |
| 1734 | const int signature_end_pos = tokenizer->get_current_position() - 1; |
| 1735 | const String source_code = tokenizer->get_source_code(); |
| 1736 | p_function->signature = source_code.substr(p_signature_start, signature_end_pos - p_signature_start).strip_edges(false, true); |
| 1737 | } |
| 1738 | #endif // TOOLS_ENABLED |
| 1739 | |
| 1740 | // TODO: Improve token consumption so it synchronizes to a statement boundary. This way we can get into the function body with unrecognized tokens. |
| 1741 | if (p_type == "lambda") { |
| 1742 | return consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after lambda declaration.)"); |
| 1743 | } |
| 1744 | // The colon may not be present in the case of abstract functions. |
| 1745 | return match(GDScriptTokenizer::Token::COLON); |
| 1746 | } |
| 1747 | |
| 1748 | GDScriptParser::FunctionNode *GDScriptParser::parse_function(bool p_is_static) { |
| 1749 | FunctionNode *function = alloc_node<FunctionNode>(); |
| 1750 | function->is_static = p_is_static; |
| 1751 | |
| 1752 | make_completion_context(COMPLETION_OVERRIDE_METHOD, function); |
| 1753 | |
| 1754 | #ifdef TOOLS_ENABLED |
| 1755 | // The signature is something like `(a: int, b: int = 0) -> void`. |
| 1756 | // We start one token earlier, since the parser looks one token ahead. |
| 1757 | const int signature_start_pos = tokenizer->get_current_position(); |
| 1758 | #endif // TOOLS_ENABLED |
| 1759 | |
| 1760 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected function name after "func".)")) { |
| 1761 | complete_extents(function); |
| 1762 | return nullptr; |
| 1763 | } |
| 1764 | |
| 1765 | FunctionNode *previous_function = current_function; |
| 1766 | current_function = function; |
| 1767 | |
| 1768 | function->identifier = parse_identifier(); |
| 1769 | |
| 1770 | SuiteNode *body = alloc_node<SuiteNode>(); |
| 1771 | |
| 1772 | SuiteNode *previous_suite = current_suite; |
| 1773 | current_suite = body; |
| 1774 | |
| 1775 | push_multiline(true); |
| 1776 | consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after function name.)"); |
| 1777 | |
| 1778 | #ifdef TOOLS_ENABLED |
| 1779 | const bool has_body = parse_function_signature(function, body, "function", signature_start_pos); |
| 1780 | #else // !TOOLS_ENABLED |
| 1781 | const bool has_body = parse_function_signature(function, body, "function", -1); |
| 1782 | #endif // TOOLS_ENABLED |
| 1783 | |
| 1784 | current_suite = previous_suite; |
| 1785 | |
| 1786 | #ifdef TOOLS_ENABLED |
| 1787 | function->min_local_doc_line = previous.end_line + 1; |
| 1788 | #endif // TOOLS_ENABLED |
| 1789 | |
| 1790 | if (!has_body) { |
| 1791 | // Abstract functions do not have a body. |
| 1792 | end_statement("bodyless function declaration"); |
| 1793 | reset_extents(body, current); |
| 1794 | complete_extents(body); |
| 1795 | function->body = body; |
| 1796 | } else { |
| 1797 | function->body = parse_suite("function declaration", body); |
| 1798 | } |
| 1799 | |
| 1800 | current_function = previous_function; |
| 1801 | complete_extents(function); |
| 1802 | return function; |
| 1803 | } |
| 1804 | |
| 1805 | GDScriptParser::AnnotationNode *GDScriptParser::parse_annotation(uint32_t p_valid_targets) { |
| 1806 | AnnotationNode *annotation = alloc_node<AnnotationNode>(); |
| 1807 | |
| 1808 | annotation->name = previous.literal; |
| 1809 | |
| 1810 | make_completion_context(COMPLETION_ANNOTATION, annotation); |
| 1811 | |
| 1812 | bool valid = true; |
| 1813 | |
| 1814 | if (!valid_annotations.has(annotation->name)) { |
| 1815 | if (annotation->name == "@deprecated") { |
| 1816 | push_error(R"("@deprecated" annotation does not exist. Use "## @deprecated: Reason here." instead.)"); |
| 1817 | } else if (annotation->name == "@experimental") { |
| 1818 | push_error(R"("@experimental" annotation does not exist. Use "## @experimental: Reason here." instead.)"); |
| 1819 | } else if (annotation->name == "@tutorial") { |
| 1820 | push_error(R"("@tutorial" annotation does not exist. Use "## @tutorial(Title): https://example.com" instead.)"); |
| 1821 | } else { |
| 1822 | push_error(vformat(R"(Unrecognized annotation: "%s".)", annotation->name)); |
| 1823 | } |
| 1824 | valid = false; |
| 1825 | } |
| 1826 | |
| 1827 | if (valid) { |
| 1828 | annotation->info = &valid_annotations[annotation->name]; |
| 1829 | |
| 1830 | if (!annotation->applies_to(p_valid_targets)) { |
| 1831 | if (annotation->applies_to(AnnotationInfo::SCRIPT)) { |
| 1832 | push_error(vformat(R"(Annotation "%s" must be at the top of the script, before "extends" and "class_name".)", annotation->name)); |
| 1833 | } else { |
| 1834 | push_error(vformat(R"(Annotation "%s" is not allowed in this level.)", annotation->name)); |
| 1835 | } |
| 1836 | valid = false; |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | if (check(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) { |
| 1841 | push_multiline(true); |
| 1842 | advance(); |
| 1843 | // Arguments. |
| 1844 | push_completion_call(annotation); |
| 1845 | int argument_index = 0; |
| 1846 | do { |
| 1847 | make_completion_context(COMPLETION_ANNOTATION_ARGUMENTS, annotation, argument_index); |
| 1848 | set_last_completion_call_arg(argument_index); |
| 1849 | if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { |
| 1850 | // Allow for trailing comma. |
| 1851 | break; |
| 1852 | } |
| 1853 | |
| 1854 | ExpressionNode *argument = parse_expression(false); |
| 1855 | |
| 1856 | if (argument == nullptr) { |
| 1857 | push_error("Expected expression as the annotation argument."); |
| 1858 | valid = false; |
| 1859 | } else { |
| 1860 | annotation->arguments.push_back(argument); |
| 1861 | |
| 1862 | if (argument->type == Node::LITERAL) { |
| 1863 | override_completion_context(argument, COMPLETION_ANNOTATION_ARGUMENTS, annotation, argument_index); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | argument_index++; |
| 1868 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 1869 | |
| 1870 | pop_multiline(); |
| 1871 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after annotation arguments.)*"); |
| 1872 | pop_completion_call(); |
| 1873 | } |
| 1874 | complete_extents(annotation); |
| 1875 | |
| 1876 | match(GDScriptTokenizer::Token::NEWLINE); // Newline after annotation is optional. |
| 1877 | |
| 1878 | if (valid) { |
| 1879 | valid = validate_annotation_arguments(annotation); |
| 1880 | } |
| 1881 | |
| 1882 | return valid ? annotation : nullptr; |
| 1883 | } |
| 1884 | |
| 1885 | void GDScriptParser::clear_unused_annotations() { |
| 1886 | for (const AnnotationNode *annotation : annotation_stack) { |
| 1887 | push_error(vformat(R"(Annotation "%s" does not precede a valid target, so it will have no effect.)", annotation->name), annotation); |
| 1888 | } |
| 1889 | |
| 1890 | annotation_stack.clear(); |
| 1891 | } |
| 1892 | |
| 1893 | bool 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) { |
| 1894 | ERR_FAIL_COND_V_MSG(valid_annotations.has(p_info.name), false, vformat(R"(Annotation "%s" already registered.)", p_info.name)); |
| 1895 | |
| 1896 | AnnotationInfo new_annotation; |
| 1897 | new_annotation.info = p_info; |
| 1898 | new_annotation.info.default_arguments = p_default_arguments; |
| 1899 | if (p_is_vararg) { |
| 1900 | new_annotation.info.flags |= METHOD_FLAG_VARARG; |
| 1901 | } |
| 1902 | new_annotation.apply = p_apply; |
| 1903 | new_annotation.target_kind = p_target_kinds; |
| 1904 | |
| 1905 | valid_annotations[p_info.name] = new_annotation; |
| 1906 | return true; |
| 1907 | } |
| 1908 | |
| 1909 | GDScriptParser::SuiteNode *GDScriptParser::parse_suite(const String &p_context, SuiteNode *p_suite, bool p_for_lambda) { |
| 1910 | SuiteNode *suite = p_suite != nullptr ? p_suite : alloc_node<SuiteNode>(); |
| 1911 | suite->parent_block = current_suite; |
| 1912 | suite->parent_function = current_function; |
| 1913 | current_suite = suite; |
| 1914 | |
| 1915 | if (!p_for_lambda && suite->parent_block != nullptr && suite->parent_block->is_in_loop) { |
| 1916 | // Do not reset to false if true is set before calling parse_suite(). |
| 1917 | suite->is_in_loop = true; |
| 1918 | } |
| 1919 | |
| 1920 | bool multiline = false; |
| 1921 | |
| 1922 | if (match(GDScriptTokenizer::Token::NEWLINE)) { |
| 1923 | multiline = true; |
| 1924 | } |
| 1925 | |
| 1926 | if (multiline) { |
| 1927 | if (!consume(GDScriptTokenizer::Token::INDENT, vformat(R"(Expected indented block after %s.)", p_context))) { |
| 1928 | current_suite = suite->parent_block; |
| 1929 | complete_extents(suite); |
| 1930 | return suite; |
| 1931 | } |
| 1932 | } |
| 1933 | reset_extents(suite, current); |
| 1934 | |
| 1935 | int error_count = 0; |
| 1936 | |
| 1937 | do { |
| 1938 | if (is_at_end() || (!multiline && previous.type == GDScriptTokenizer::Token::SEMICOLON && check(GDScriptTokenizer::Token::NEWLINE))) { |
| 1939 | break; |
| 1940 | } |
| 1941 | Node *statement = parse_statement(); |
| 1942 | if (statement == nullptr) { |
| 1943 | if (error_count++ > 100) { |
| 1944 | push_error("Too many statement errors.", suite); |
| 1945 | break; |
| 1946 | } |
| 1947 | continue; |
| 1948 | } |
| 1949 | suite->statements.push_back(statement); |
| 1950 | |
| 1951 | // Register locals. |
| 1952 | switch (statement->type) { |
| 1953 | case Node::VARIABLE: { |
| 1954 | VariableNode *variable = static_cast<VariableNode *>(statement); |
| 1955 | const SuiteNode::Local &local = current_suite->get_local(variable->identifier->name); |
| 1956 | if (local.type != SuiteNode::Local::UNDEFINED) { |
| 1957 | push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", local.get_name(), variable->identifier->name), variable->identifier); |
| 1958 | } |
| 1959 | current_suite->add_local(variable, current_function); |
| 1960 | break; |
| 1961 | } |
| 1962 | case Node::CONSTANT: { |
| 1963 | ConstantNode *constant = static_cast<ConstantNode *>(statement); |
| 1964 | const SuiteNode::Local &local = current_suite->get_local(constant->identifier->name); |
| 1965 | if (local.type != SuiteNode::Local::UNDEFINED) { |
| 1966 | String name; |
| 1967 | if (local.type == SuiteNode::Local::CONSTANT) { |
| 1968 | name = "constant"; |
| 1969 | } else { |
| 1970 | name = "variable"; |
| 1971 | } |
| 1972 | push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", name, constant->identifier->name), constant->identifier); |
| 1973 | } |
| 1974 | current_suite->add_local(constant, current_function); |
| 1975 | break; |
| 1976 | } |
| 1977 | default: |
| 1978 | break; |
| 1979 | } |
| 1980 | |
| 1981 | } while ((multiline || previous.type == GDScriptTokenizer::Token::SEMICOLON) && !check(GDScriptTokenizer::Token::DEDENT) && !lambda_ended && !is_at_end()); |
| 1982 | |
| 1983 | complete_extents(suite); |
| 1984 | |
| 1985 | if (multiline) { |
| 1986 | if (!lambda_ended) { |
| 1987 | consume(GDScriptTokenizer::Token::DEDENT, vformat(R"(Missing unindent at the end of %s.)", p_context)); |
| 1988 | |
| 1989 | } else { |
| 1990 | match(GDScriptTokenizer::Token::DEDENT); |
| 1991 | } |
| 1992 | } else if (previous.type == GDScriptTokenizer::Token::SEMICOLON) { |
| 1993 | consume(GDScriptTokenizer::Token::NEWLINE, vformat(R"(Expected newline after ";" at the end of %s.)", p_context)); |
| 1994 | } |
| 1995 | |
| 1996 | if (p_for_lambda) { |
| 1997 | lambda_ended = true; |
| 1998 | } |
| 1999 | current_suite = suite->parent_block; |
| 2000 | return suite; |
| 2001 | } |
| 2002 | |
| 2003 | GDScriptParser::Node *GDScriptParser::parse_statement() { |
| 2004 | Node *result = nullptr; |
| 2005 | #ifdef DEBUG_ENABLED |
| 2006 | bool unreachable = current_suite->has_return && !current_suite->has_unreachable_code; |
| 2007 | #endif |
| 2008 | |
| 2009 | List<AnnotationNode *> annotations; |
| 2010 | if (current.type != GDScriptTokenizer::Token::ANNOTATION) { |
| 2011 | while (!annotation_stack.is_empty()) { |
| 2012 | AnnotationNode *last_annotation = annotation_stack.back()->get(); |
| 2013 | if (last_annotation->applies_to(AnnotationInfo::STATEMENT)) { |
| 2014 | annotations.push_front(last_annotation); |
| 2015 | annotation_stack.pop_back(); |
| 2016 | } else { |
| 2017 | push_error(vformat(R"(Annotation "%s" cannot be applied to a statement.)", last_annotation->name)); |
| 2018 | clear_unused_annotations(); |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | switch (current.type) { |
| 2024 | case GDScriptTokenizer::Token::PASS: |
| 2025 | advance(); |
| 2026 | result = alloc_node<PassNode>(); |
| 2027 | complete_extents(result); |
| 2028 | end_statement(R"("pass")"); |
| 2029 | break; |
| 2030 | case GDScriptTokenizer::Token::VAR: |
| 2031 | advance(); |
| 2032 | result = parse_variable(false, false); |
| 2033 | break; |
| 2034 | case GDScriptTokenizer::Token::TK_CONST: |
| 2035 | advance(); |
| 2036 | result = parse_constant(false); |
| 2037 | break; |
| 2038 | case GDScriptTokenizer::Token::IF: |
| 2039 | advance(); |
| 2040 | result = parse_if(); |
| 2041 | break; |
| 2042 | case GDScriptTokenizer::Token::FOR: |
| 2043 | advance(); |
| 2044 | result = parse_for(); |
| 2045 | break; |
| 2046 | case GDScriptTokenizer::Token::WHILE: |
| 2047 | advance(); |
| 2048 | result = parse_while(); |
| 2049 | break; |
| 2050 | case GDScriptTokenizer::Token::MATCH: |
| 2051 | advance(); |
| 2052 | result = parse_match(); |
| 2053 | break; |
| 2054 | case GDScriptTokenizer::Token::BREAK: |
| 2055 | advance(); |
| 2056 | result = parse_break(); |
| 2057 | break; |
| 2058 | case GDScriptTokenizer::Token::CONTINUE: |
| 2059 | advance(); |
| 2060 | result = parse_continue(); |
| 2061 | break; |
| 2062 | case GDScriptTokenizer::Token::RETURN: { |
| 2063 | advance(); |
| 2064 | ReturnNode *n_return = alloc_node<ReturnNode>(); |
| 2065 | if (!is_statement_end()) { |
| 2066 | if (current_function && (current_function->identifier->name == GDScriptLanguage::get_singleton()->strings._init || current_function->identifier->name == GDScriptLanguage::get_singleton()->strings._static_init)) { |
| 2067 | push_error(R"(Constructor cannot return a value.)"); |
| 2068 | } |
| 2069 | n_return->return_value = parse_expression(false); |
| 2070 | } else if (in_lambda && !is_statement_end_token()) { |
| 2071 | // Try to parse it anyway as this might not be the statement end in a lambda. |
| 2072 | // If this fails the expression will be nullptr, but that's the same as no return, so it's fine. |
| 2073 | n_return->return_value = parse_expression(false); |
| 2074 | } |
| 2075 | complete_extents(n_return); |
| 2076 | result = n_return; |
| 2077 | |
| 2078 | current_suite->has_return = true; |
| 2079 | |
| 2080 | end_statement("return statement"); |
| 2081 | break; |
| 2082 | } |
| 2083 | case GDScriptTokenizer::Token::BREAKPOINT: |
| 2084 | advance(); |
| 2085 | result = alloc_node<BreakpointNode>(); |
| 2086 | complete_extents(result); |
| 2087 | end_statement(R"("breakpoint")"); |
| 2088 | break; |
| 2089 | case GDScriptTokenizer::Token::ASSERT: |
| 2090 | advance(); |
| 2091 | result = parse_assert(); |
| 2092 | break; |
| 2093 | case GDScriptTokenizer::Token::ANNOTATION: { |
| 2094 | advance(); |
| 2095 | AnnotationNode *annotation = parse_annotation(AnnotationInfo::STATEMENT | AnnotationInfo::STANDALONE); |
| 2096 | if (annotation != nullptr) { |
| 2097 | if (annotation->applies_to(AnnotationInfo::STANDALONE)) { |
| 2098 | if (previous.type != GDScriptTokenizer::Token::NEWLINE) { |
| 2099 | push_error(R"(Expected newline after a standalone annotation.)"); |
| 2100 | } |
| 2101 | if (annotation->name == SNAME("@warning_ignore_start") || annotation->name == SNAME("@warning_ignore_restore")) { |
| 2102 | // Some annotations need to be resolved and applied in the parser. |
| 2103 | annotation->apply(this, nullptr, nullptr); |
| 2104 | } else { |
| 2105 | push_error(R"(Unexpected standalone annotation.)"); |
| 2106 | } |
| 2107 | } else { |
| 2108 | annotation_stack.push_back(annotation); |
| 2109 | } |
| 2110 | } |
| 2111 | break; |
| 2112 | } |
| 2113 | default: { |
| 2114 | // Expression statement. |
| 2115 | ExpressionNode *expression = parse_expression(true); // Allow assignment here. |
| 2116 | bool has_ended_lambda = false; |
| 2117 | if (expression == nullptr) { |
| 2118 | if (in_lambda) { |
| 2119 | // If it's not a valid expression beginning, it might be the continuation of the outer expression where this lambda is. |
| 2120 | lambda_ended = true; |
| 2121 | has_ended_lambda = true; |
| 2122 | } else { |
| 2123 | advance(); |
| 2124 | push_error(vformat(R"(Expected statement, found "%s" instead.)", previous.get_name())); |
| 2125 | } |
| 2126 | } else { |
| 2127 | end_statement("expression"); |
| 2128 | } |
| 2129 | lambda_ended = lambda_ended || has_ended_lambda; |
| 2130 | result = expression; |
| 2131 | |
| 2132 | #ifdef DEBUG_ENABLED |
| 2133 | if (expression != nullptr) { |
| 2134 | switch (expression->type) { |
| 2135 | case Node::ASSIGNMENT: |
| 2136 | case Node::AWAIT: |
| 2137 | case Node::CALL: |
| 2138 | // Fine. |
| 2139 | break; |
| 2140 | case Node::PRELOAD: |
| 2141 | // `preload` is a function-like keyword. |
| 2142 | push_warning(expression, GDScriptWarning::RETURN_VALUE_DISCARDED, "preload"); |
| 2143 | break; |
| 2144 | case Node::LAMBDA: |
| 2145 | // Standalone lambdas can't be used, so make this an error. |
| 2146 | push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression); |
| 2147 | break; |
| 2148 | case Node::LITERAL: |
| 2149 | // Allow strings as multiline comments. |
| 2150 | if (static_cast<GDScriptParser::LiteralNode *>(expression)->value.get_type() != Variant::STRING) { |
| 2151 | push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION); |
| 2152 | } |
| 2153 | break; |
| 2154 | case Node::TERNARY_OPERATOR: |
| 2155 | push_warning(expression, GDScriptWarning::STANDALONE_TERNARY); |
| 2156 | break; |
| 2157 | default: |
| 2158 | push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION); |
| 2159 | } |
| 2160 | } |
| 2161 | #endif |
| 2162 | break; |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | #ifdef TOOLS_ENABLED |
| 2167 | int doc_comment_line = 0; |
| 2168 | if (result != nullptr) { |
| 2169 | doc_comment_line = result->start_line - 1; |
| 2170 | } |
| 2171 | #endif // TOOLS_ENABLED |
| 2172 | |
| 2173 | if (result != nullptr && !annotations.is_empty()) { |
| 2174 | for (AnnotationNode *&annotation : annotations) { |
| 2175 | result->annotations.push_back(annotation); |
| 2176 | #ifdef TOOLS_ENABLED |
| 2177 | if (annotation->start_line <= doc_comment_line) { |
| 2178 | doc_comment_line = annotation->start_line - 1; |
| 2179 | } |
| 2180 | #endif // TOOLS_ENABLED |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | #ifdef TOOLS_ENABLED |
| 2185 | if (result != nullptr) { |
| 2186 | MemberDocData doc_data; |
| 2187 | if (has_comment(result->start_line, true)) { |
| 2188 | // Inline doc comment. |
| 2189 | doc_data = parse_doc_comment(result->start_line, true); |
| 2190 | } 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) { |
| 2191 | // Normal doc comment. |
| 2192 | doc_data = parse_doc_comment(doc_comment_line); |
| 2193 | } |
| 2194 | |
| 2195 | if (result->type == Node::CONSTANT) { |
| 2196 | static_cast<ConstantNode *>(result)->doc_data = doc_data; |
| 2197 | } else if (result->type == Node::VARIABLE) { |
| 2198 | static_cast<VariableNode *>(result)->doc_data = doc_data; |
| 2199 | } |
| 2200 | |
| 2201 | current_function->min_local_doc_line = result->end_line + 1; // Prevent multiple locals from using the same doc comment. |
| 2202 | } |
| 2203 | #endif // TOOLS_ENABLED |
| 2204 | |
| 2205 | #ifdef DEBUG_ENABLED |
| 2206 | if (unreachable && result != nullptr) { |
| 2207 | current_suite->has_unreachable_code = true; |
| 2208 | if (current_function) { |
| 2209 | push_warning(result, GDScriptWarning::UNREACHABLE_CODE, current_function->identifier ? current_function->identifier->name : "<anonymous lambda>"); |
| 2210 | } else { |
| 2211 | // TODO: Properties setters and getters with unreachable code are not being warned |
| 2212 | } |
| 2213 | } |
| 2214 | #endif |
| 2215 | |
| 2216 | if (panic_mode) { |
| 2217 | synchronize(); |
| 2218 | } |
| 2219 | |
| 2220 | return result; |
| 2221 | } |
| 2222 | |
| 2223 | GDScriptParser::AssertNode *GDScriptParser::parse_assert() { |
| 2224 | // TODO: Add assert message. |
| 2225 | AssertNode *assert = alloc_node<AssertNode>(); |
| 2226 | |
| 2227 | push_multiline(true); |
| 2228 | consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after "assert".)"); |
| 2229 | |
| 2230 | assert->condition = parse_expression(false); |
| 2231 | if (assert->condition == nullptr) { |
| 2232 | push_error("Expected expression to assert."); |
| 2233 | pop_multiline(); |
| 2234 | complete_extents(assert); |
| 2235 | return nullptr; |
| 2236 | } |
| 2237 | |
| 2238 | if (match(GDScriptTokenizer::Token::COMMA) && !check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { |
| 2239 | assert->message = parse_expression(false); |
| 2240 | if (assert->message == nullptr) { |
| 2241 | push_error(R"(Expected error message for assert after ",".)"); |
| 2242 | pop_multiline(); |
| 2243 | complete_extents(assert); |
| 2244 | return nullptr; |
| 2245 | } |
| 2246 | match(GDScriptTokenizer::Token::COMMA); |
| 2247 | } |
| 2248 | |
| 2249 | pop_multiline(); |
| 2250 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after assert expression.)*"); |
| 2251 | |
| 2252 | complete_extents(assert); |
| 2253 | end_statement(R"("assert")"); |
| 2254 | |
| 2255 | return assert; |
| 2256 | } |
| 2257 | |
| 2258 | GDScriptParser::BreakNode *GDScriptParser::parse_break() { |
| 2259 | if (!can_break) { |
| 2260 | push_error(R"(Cannot use "break" outside of a loop.)"); |
| 2261 | } |
| 2262 | BreakNode *break_node = alloc_node<BreakNode>(); |
| 2263 | complete_extents(break_node); |
| 2264 | end_statement(R"("break")"); |
| 2265 | return break_node; |
| 2266 | } |
| 2267 | |
| 2268 | GDScriptParser::ContinueNode *GDScriptParser::parse_continue() { |
| 2269 | if (!can_continue) { |
| 2270 | push_error(R"(Cannot use "continue" outside of a loop.)"); |
| 2271 | } |
| 2272 | current_suite->has_continue = true; |
| 2273 | ContinueNode *cont = alloc_node<ContinueNode>(); |
| 2274 | complete_extents(cont); |
| 2275 | end_statement(R"("continue")"); |
| 2276 | return cont; |
| 2277 | } |
| 2278 | |
| 2279 | GDScriptParser::ForNode *GDScriptParser::parse_for() { |
| 2280 | ForNode *n_for = alloc_node<ForNode>(); |
| 2281 | |
| 2282 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected loop variable name after "for".)")) { |
| 2283 | n_for->variable = parse_identifier(); |
| 2284 | } |
| 2285 | |
| 2286 | if (match(GDScriptTokenizer::Token::COLON)) { |
| 2287 | n_for->datatype_specifier = parse_type(); |
| 2288 | if (n_for->datatype_specifier == nullptr) { |
| 2289 | push_error(R"(Expected type specifier after ":".)"); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | if (n_for->datatype_specifier == nullptr) { |
| 2294 | consume(GDScriptTokenizer::Token::TK_IN, R"(Expected "in" or ":" after "for" variable name.)"); |
| 2295 | } else { |
| 2296 | consume(GDScriptTokenizer::Token::TK_IN, R"(Expected "in" after "for" variable type specifier.)"); |
| 2297 | } |
| 2298 | |
| 2299 | n_for->list = parse_expression(false); |
| 2300 | |
| 2301 | if (!n_for->list) { |
| 2302 | push_error(R"(Expected iterable after "in".)"); |
| 2303 | } |
| 2304 | |
| 2305 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "for" condition.)"); |
| 2306 | |
| 2307 | // Save break/continue state. |
| 2308 | bool could_break = can_break; |
| 2309 | bool could_continue = can_continue; |
| 2310 | |
| 2311 | // Allow break/continue. |
| 2312 | can_break = true; |
| 2313 | can_continue = true; |
| 2314 | |
| 2315 | SuiteNode *suite = alloc_node<SuiteNode>(); |
| 2316 | if (n_for->variable) { |
| 2317 | const SuiteNode::Local &local = current_suite->get_local(n_for->variable->name); |
| 2318 | if (local.type != SuiteNode::Local::UNDEFINED) { |
| 2319 | push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", local.get_name(), n_for->variable->name), n_for->variable); |
| 2320 | } |
| 2321 | suite->add_local(SuiteNode::Local(n_for->variable, current_function)); |
| 2322 | } |
| 2323 | suite->is_in_loop = true; |
| 2324 | n_for->loop = parse_suite(R"("for" block)", suite); |
| 2325 | complete_extents(n_for); |
| 2326 | |
| 2327 | // Reset break/continue state. |
| 2328 | can_break = could_break; |
| 2329 | can_continue = could_continue; |
| 2330 | |
| 2331 | return n_for; |
| 2332 | } |
| 2333 | |
| 2334 | GDScriptParser::IfNode *GDScriptParser::parse_if(const String &p_token) { |
| 2335 | IfNode *n_if = alloc_node<IfNode>(); |
| 2336 | |
| 2337 | n_if->condition = parse_expression(false); |
| 2338 | if (n_if->condition == nullptr) { |
| 2339 | push_error(vformat(R"(Expected conditional expression after "%s".)", p_token)); |
| 2340 | } |
| 2341 | |
| 2342 | consume(GDScriptTokenizer::Token::COLON, vformat(R"(Expected ":" after "%s" condition.)", p_token)); |
| 2343 | |
| 2344 | n_if->true_block = parse_suite(vformat(R"("%s" block)", p_token)); |
| 2345 | n_if->true_block->parent_if = n_if; |
| 2346 | |
| 2347 | if (n_if->true_block->has_continue) { |
| 2348 | current_suite->has_continue = true; |
| 2349 | } |
| 2350 | |
| 2351 | if (match(GDScriptTokenizer::Token::ELIF)) { |
| 2352 | SuiteNode *else_block = alloc_node<SuiteNode>(); |
| 2353 | else_block->parent_function = current_function; |
| 2354 | else_block->parent_block = current_suite; |
| 2355 | |
| 2356 | SuiteNode *previous_suite = current_suite; |
| 2357 | current_suite = else_block; |
| 2358 | |
| 2359 | IfNode *elif = parse_if("elif"); |
| 2360 | else_block->statements.push_back(elif); |
| 2361 | complete_extents(else_block); |
| 2362 | n_if->false_block = else_block; |
| 2363 | |
| 2364 | current_suite = previous_suite; |
| 2365 | } else if (match(GDScriptTokenizer::Token::ELSE)) { |
| 2366 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "else".)"); |
| 2367 | n_if->false_block = parse_suite(R"("else" block)"); |
| 2368 | } |
| 2369 | complete_extents(n_if); |
| 2370 | |
| 2371 | if (n_if->false_block != nullptr && n_if->false_block->has_return && n_if->true_block->has_return) { |
| 2372 | current_suite->has_return = true; |
| 2373 | } |
| 2374 | if (n_if->false_block != nullptr && n_if->false_block->has_continue) { |
| 2375 | current_suite->has_continue = true; |
| 2376 | } |
| 2377 | |
| 2378 | return n_if; |
| 2379 | } |
| 2380 | |
| 2381 | GDScriptParser::MatchNode *GDScriptParser::parse_match() { |
| 2382 | MatchNode *match_node = alloc_node<MatchNode>(); |
| 2383 | |
| 2384 | match_node->test = parse_expression(false); |
| 2385 | if (match_node->test == nullptr) { |
| 2386 | push_error(R"(Expected expression to test after "match".)"); |
| 2387 | } |
| 2388 | |
| 2389 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "match" expression.)"); |
| 2390 | consume(GDScriptTokenizer::Token::NEWLINE, R"(Expected a newline after "match" statement.)"); |
| 2391 | |
| 2392 | if (!consume(GDScriptTokenizer::Token::INDENT, R"(Expected an indented block after "match" statement.)")) { |
| 2393 | complete_extents(match_node); |
| 2394 | return match_node; |
| 2395 | } |
| 2396 | |
| 2397 | bool all_have_return = true; |
| 2398 | bool have_wildcard = false; |
| 2399 | |
| 2400 | List<AnnotationNode *> match_branch_annotation_stack; |
| 2401 | |
| 2402 | while (!check(GDScriptTokenizer::Token::DEDENT) && !is_at_end()) { |
| 2403 | if (match(GDScriptTokenizer::Token::PASS)) { |
| 2404 | consume(GDScriptTokenizer::Token::NEWLINE, R"(Expected newline after "pass".)"); |
| 2405 | continue; |
| 2406 | } |
| 2407 | |
| 2408 | if (match(GDScriptTokenizer::Token::ANNOTATION)) { |
| 2409 | AnnotationNode *annotation = parse_annotation(AnnotationInfo::STATEMENT); |
| 2410 | if (annotation == nullptr) { |
| 2411 | continue; |
| 2412 | } |
| 2413 | if (annotation->name != SNAME("@warning_ignore")) { |
| 2414 | push_error(vformat(R"(Annotation "%s" is not allowed in this level.)", annotation->name), annotation); |
| 2415 | continue; |
| 2416 | } |
| 2417 | match_branch_annotation_stack.push_back(annotation); |
| 2418 | continue; |
| 2419 | } |
| 2420 | |
| 2421 | MatchBranchNode *branch = parse_match_branch(); |
| 2422 | if (branch == nullptr) { |
| 2423 | advance(); |
| 2424 | continue; |
| 2425 | } |
| 2426 | |
| 2427 | for (AnnotationNode *annotation : match_branch_annotation_stack) { |
| 2428 | branch->annotations.push_back(annotation); |
| 2429 | } |
| 2430 | match_branch_annotation_stack.clear(); |
| 2431 | |
| 2432 | #ifdef DEBUG_ENABLED |
| 2433 | if (have_wildcard && !branch->patterns.is_empty()) { |
| 2434 | push_warning(branch->patterns[0], GDScriptWarning::UNREACHABLE_PATTERN); |
| 2435 | } |
| 2436 | #endif |
| 2437 | |
| 2438 | have_wildcard = have_wildcard || branch->has_wildcard; |
| 2439 | all_have_return = all_have_return && branch->block->has_return; |
| 2440 | match_node->branches.push_back(branch); |
| 2441 | } |
| 2442 | complete_extents(match_node); |
| 2443 | |
| 2444 | consume(GDScriptTokenizer::Token::DEDENT, R"(Expected an indented block after "match" statement.)"); |
| 2445 | |
| 2446 | if (all_have_return && have_wildcard) { |
| 2447 | current_suite->has_return = true; |
| 2448 | } |
| 2449 | |
| 2450 | for (const AnnotationNode *annotation : match_branch_annotation_stack) { |
| 2451 | push_error(vformat(R"(Annotation "%s" does not precede a valid target, so it will have no effect.)", annotation->name), annotation); |
| 2452 | } |
| 2453 | match_branch_annotation_stack.clear(); |
| 2454 | |
| 2455 | return match_node; |
| 2456 | } |
| 2457 | |
| 2458 | GDScriptParser::MatchBranchNode *GDScriptParser::parse_match_branch() { |
| 2459 | MatchBranchNode *branch = alloc_node<MatchBranchNode>(); |
| 2460 | reset_extents(branch, current); |
| 2461 | |
| 2462 | bool has_bind = false; |
| 2463 | |
| 2464 | do { |
| 2465 | PatternNode *pattern = parse_match_pattern(); |
| 2466 | if (pattern == nullptr) { |
| 2467 | continue; |
| 2468 | } |
| 2469 | if (pattern->binds.size() > 0) { |
| 2470 | has_bind = true; |
| 2471 | } |
| 2472 | if (branch->patterns.size() > 0 && has_bind) { |
| 2473 | push_error(R"(Cannot use a variable bind with multiple patterns.)"); |
| 2474 | } |
| 2475 | if (pattern->pattern_type == PatternNode::PT_REST) { |
| 2476 | push_error(R"(Rest pattern can only be used inside array and dictionary patterns.)"); |
| 2477 | } else if (pattern->pattern_type == PatternNode::PT_BIND || pattern->pattern_type == PatternNode::PT_WILDCARD) { |
| 2478 | branch->has_wildcard = true; |
| 2479 | } |
| 2480 | branch->patterns.push_back(pattern); |
| 2481 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 2482 | |
| 2483 | if (branch->patterns.is_empty()) { |
| 2484 | push_error(R"(No pattern found for "match" branch.)"); |
| 2485 | } |
| 2486 | |
| 2487 | bool has_guard = false; |
| 2488 | if (match(GDScriptTokenizer::Token::WHEN)) { |
| 2489 | // Pattern guard. |
| 2490 | // 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. |
| 2491 | branch->guard_body = alloc_node<SuiteNode>(); |
| 2492 | if (branch->patterns.size() > 0) { |
| 2493 | for (const KeyValue<StringName, IdentifierNode *> &E : branch->patterns[0]->binds) { |
| 2494 | SuiteNode::Local local(E.value, current_function); |
| 2495 | local.type = SuiteNode::Local::PATTERN_BIND; |
| 2496 | branch->guard_body->add_local(local); |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | SuiteNode *parent_block = current_suite; |
| 2501 | branch->guard_body->parent_block = parent_block; |
| 2502 | current_suite = branch->guard_body; |
| 2503 | |
| 2504 | ExpressionNode *guard = parse_expression(false); |
| 2505 | if (guard == nullptr) { |
| 2506 | push_error(R"(Expected expression for pattern guard after "when".)"); |
| 2507 | } else { |
| 2508 | branch->guard_body->statements.append(guard); |
| 2509 | } |
| 2510 | current_suite = parent_block; |
| 2511 | complete_extents(branch->guard_body); |
| 2512 | |
| 2513 | has_guard = true; |
| 2514 | branch->has_wildcard = false; // If it has a guard, the wildcard might still not match. |
| 2515 | } |
| 2516 | |
| 2517 | if (!consume(GDScriptTokenizer::Token::COLON, vformat(R"(Expected ":"%s after "match" %s.)", has_guard ? "" : R"( or "when")", has_guard ? "pattern guard" : "patterns"))) { |
| 2518 | branch->block = alloc_recovery_suite(); |
| 2519 | complete_extents(branch); |
| 2520 | // Consume the whole line and treat the next one as new match branch. |
| 2521 | while (current.type != GDScriptTokenizer::Token::NEWLINE && !is_at_end()) { |
| 2522 | advance(); |
| 2523 | } |
| 2524 | if (!is_at_end()) { |
| 2525 | advance(); |
| 2526 | } |
| 2527 | return branch; |
| 2528 | } |
| 2529 | |
| 2530 | SuiteNode *suite = alloc_node<SuiteNode>(); |
| 2531 | if (branch->patterns.size() > 0) { |
| 2532 | for (const KeyValue<StringName, IdentifierNode *> &E : branch->patterns[0]->binds) { |
| 2533 | SuiteNode::Local local(E.value, current_function); |
| 2534 | local.type = SuiteNode::Local::PATTERN_BIND; |
| 2535 | suite->add_local(local); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | branch->block = parse_suite("match pattern block", suite); |
| 2540 | complete_extents(branch); |
| 2541 | |
| 2542 | return branch; |
| 2543 | } |
| 2544 | |
| 2545 | GDScriptParser::PatternNode *GDScriptParser::parse_match_pattern(PatternNode *p_root_pattern) { |
| 2546 | PatternNode *pattern = alloc_node<PatternNode>(); |
| 2547 | reset_extents(pattern, current); |
| 2548 | |
| 2549 | switch (current.type) { |
| 2550 | case GDScriptTokenizer::Token::VAR: { |
| 2551 | // Bind. |
| 2552 | advance(); |
| 2553 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected bind name after "var".)")) { |
| 2554 | complete_extents(pattern); |
| 2555 | return nullptr; |
| 2556 | } |
| 2557 | pattern->pattern_type = PatternNode::PT_BIND; |
| 2558 | pattern->bind = parse_identifier(); |
| 2559 | |
| 2560 | PatternNode *root_pattern = p_root_pattern == nullptr ? pattern : p_root_pattern; |
| 2561 | |
| 2562 | if (p_root_pattern != nullptr) { |
| 2563 | if (p_root_pattern->has_bind(pattern->bind->name)) { |
| 2564 | push_error(vformat(R"(Bind variable name "%s" was already used in this pattern.)", pattern->bind->name)); |
| 2565 | complete_extents(pattern); |
| 2566 | return nullptr; |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | if (current_suite->has_local(pattern->bind->name)) { |
| 2571 | push_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)); |
| 2572 | complete_extents(pattern); |
| 2573 | return nullptr; |
| 2574 | } |
| 2575 | |
| 2576 | root_pattern->binds[pattern->bind->name] = pattern->bind; |
| 2577 | |
| 2578 | } break; |
| 2579 | case GDScriptTokenizer::Token::UNDERSCORE: |
| 2580 | // Wildcard. |
| 2581 | advance(); |
| 2582 | pattern->pattern_type = PatternNode::PT_WILDCARD; |
| 2583 | break; |
| 2584 | case GDScriptTokenizer::Token::PERIOD_PERIOD: |
| 2585 | // Rest. |
| 2586 | advance(); |
| 2587 | pattern->pattern_type = PatternNode::PT_REST; |
| 2588 | break; |
| 2589 | case GDScriptTokenizer::Token::BRACKET_OPEN: { |
| 2590 | // Array. |
| 2591 | push_multiline(true); |
| 2592 | advance(); |
| 2593 | pattern->pattern_type = PatternNode::PT_ARRAY; |
| 2594 | do { |
| 2595 | if (is_at_end() || check(GDScriptTokenizer::Token::BRACKET_CLOSE)) { |
| 2596 | break; |
| 2597 | } |
| 2598 | PatternNode *sub_pattern = parse_match_pattern(p_root_pattern != nullptr ? p_root_pattern : pattern); |
| 2599 | if (sub_pattern == nullptr) { |
| 2600 | continue; |
| 2601 | } |
| 2602 | if (pattern->rest_used) { |
| 2603 | push_error(R"(The ".." pattern must be the last element in the pattern array.)"); |
| 2604 | } else if (sub_pattern->pattern_type == PatternNode::PT_REST) { |
| 2605 | pattern->rest_used = true; |
| 2606 | } |
| 2607 | pattern->array.push_back(sub_pattern); |
| 2608 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 2609 | consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected "]" to close the array pattern.)"); |
| 2610 | pop_multiline(); |
| 2611 | break; |
| 2612 | } |
| 2613 | case GDScriptTokenizer::Token::BRACE_OPEN: { |
| 2614 | // Dictionary. |
| 2615 | push_multiline(true); |
| 2616 | advance(); |
| 2617 | pattern->pattern_type = PatternNode::PT_DICTIONARY; |
| 2618 | do { |
| 2619 | if (check(GDScriptTokenizer::Token::BRACE_CLOSE) || is_at_end()) { |
| 2620 | break; |
| 2621 | } |
| 2622 | if (match(GDScriptTokenizer::Token::PERIOD_PERIOD)) { |
| 2623 | // Rest. |
| 2624 | if (pattern->rest_used) { |
| 2625 | push_error(R"(The ".." pattern must be the last element in the pattern dictionary.)"); |
| 2626 | } else { |
| 2627 | PatternNode *sub_pattern = alloc_node<PatternNode>(); |
| 2628 | complete_extents(sub_pattern); |
| 2629 | sub_pattern->pattern_type = PatternNode::PT_REST; |
| 2630 | pattern->dictionary.push_back({ nullptr, sub_pattern }); |
| 2631 | pattern->rest_used = true; |
| 2632 | } |
| 2633 | } else { |
| 2634 | ExpressionNode *key = parse_expression(false); |
| 2635 | if (key == nullptr) { |
| 2636 | push_error(R"(Expected expression as key for dictionary pattern.)"); |
| 2637 | } |
| 2638 | if (match(GDScriptTokenizer::Token::COLON)) { |
| 2639 | // Value pattern. |
| 2640 | PatternNode *sub_pattern = parse_match_pattern(p_root_pattern != nullptr ? p_root_pattern : pattern); |
| 2641 | if (sub_pattern == nullptr) { |
| 2642 | continue; |
| 2643 | } |
| 2644 | if (pattern->rest_used) { |
| 2645 | push_error(R"(The ".." pattern must be the last element in the pattern dictionary.)"); |
| 2646 | } else if (sub_pattern->pattern_type == PatternNode::PT_REST) { |
| 2647 | push_error(R"(The ".." pattern cannot be used as a value.)"); |
| 2648 | } else { |
| 2649 | pattern->dictionary.push_back({ key, sub_pattern }); |
| 2650 | } |
| 2651 | } else { |
| 2652 | // Key match only. |
| 2653 | pattern->dictionary.push_back({ key, nullptr }); |
| 2654 | } |
| 2655 | } |
| 2656 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 2657 | consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected "}" to close the dictionary pattern.)"); |
| 2658 | pop_multiline(); |
| 2659 | break; |
| 2660 | } |
| 2661 | default: { |
| 2662 | // Expression. |
| 2663 | ExpressionNode *expression = parse_expression(false); |
| 2664 | if (expression == nullptr) { |
| 2665 | push_error(R"(Expected expression for match pattern.)"); |
| 2666 | complete_extents(pattern); |
| 2667 | return nullptr; |
| 2668 | } else { |
| 2669 | if (expression->type == GDScriptParser::Node::LITERAL) { |
| 2670 | pattern->pattern_type = PatternNode::PT_LITERAL; |
| 2671 | } else { |
| 2672 | pattern->pattern_type = PatternNode::PT_EXPRESSION; |
| 2673 | } |
| 2674 | pattern->expression = expression; |
| 2675 | } |
| 2676 | break; |
| 2677 | } |
| 2678 | } |
| 2679 | complete_extents(pattern); |
| 2680 | |
| 2681 | return pattern; |
| 2682 | } |
| 2683 | |
| 2684 | bool GDScriptParser::PatternNode::has_bind(const StringName &p_name) { |
| 2685 | return binds.has(p_name); |
| 2686 | } |
| 2687 | |
| 2688 | GDScriptParser::IdentifierNode *GDScriptParser::PatternNode::get_bind(const StringName &p_name) { |
| 2689 | return binds[p_name]; |
| 2690 | } |
| 2691 | |
| 2692 | GDScriptParser::WhileNode *GDScriptParser::parse_while() { |
| 2693 | WhileNode *n_while = alloc_node<WhileNode>(); |
| 2694 | |
| 2695 | n_while->condition = parse_expression(false); |
| 2696 | if (n_while->condition == nullptr) { |
| 2697 | push_error(R"(Expected conditional expression after "while".)"); |
| 2698 | } |
| 2699 | |
| 2700 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "while" condition.)"); |
| 2701 | |
| 2702 | // Save break/continue state. |
| 2703 | bool could_break = can_break; |
| 2704 | bool could_continue = can_continue; |
| 2705 | |
| 2706 | // Allow break/continue. |
| 2707 | can_break = true; |
| 2708 | can_continue = true; |
| 2709 | |
| 2710 | SuiteNode *suite = alloc_node<SuiteNode>(); |
| 2711 | suite->is_in_loop = true; |
| 2712 | n_while->loop = parse_suite(R"("while" block)", suite); |
| 2713 | complete_extents(n_while); |
| 2714 | |
| 2715 | // Reset break/continue state. |
| 2716 | can_break = could_break; |
| 2717 | can_continue = could_continue; |
| 2718 | |
| 2719 | return n_while; |
| 2720 | } |
| 2721 | |
| 2722 | GDScriptParser::ExpressionNode *GDScriptParser::parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign) { |
| 2723 | // Switch multiline mode on for grouping tokens. |
| 2724 | // Do this early to avoid the tokenizer generating whitespace tokens. |
| 2725 | switch (current.type) { |
| 2726 | case GDScriptTokenizer::Token::PARENTHESIS_OPEN: |
| 2727 | case GDScriptTokenizer::Token::BRACE_OPEN: |
| 2728 | case GDScriptTokenizer::Token::BRACKET_OPEN: |
| 2729 | push_multiline(true); |
| 2730 | break; |
| 2731 | default: |
| 2732 | break; // Nothing to do. |
| 2733 | } |
| 2734 | |
| 2735 | // Completion can appear whenever an expression is expected. |
| 2736 | make_completion_context(COMPLETION_IDENTIFIER, nullptr, -1, false); |
| 2737 | |
| 2738 | GDScriptTokenizer::Token token = current; |
| 2739 | GDScriptTokenizer::Token::Type token_type = token.type; |
| 2740 | if (token.is_identifier()) { |
| 2741 | // Allow keywords that can be treated as identifiers. |
| 2742 | token_type = GDScriptTokenizer::Token::IDENTIFIER; |
| 2743 | } |
| 2744 | ParseFunction prefix_rule = get_rule(token_type)->prefix; |
| 2745 | |
| 2746 | if (prefix_rule == nullptr) { |
| 2747 | // Expected expression. Let the caller give the proper error message. |
| 2748 | return nullptr; |
| 2749 | } |
| 2750 | |
| 2751 | advance(); // Only consume the token if there's a valid rule. |
| 2752 | |
| 2753 | // After a token was consumed, update the completion context regardless of a previously set context. |
| 2754 | |
| 2755 | ExpressionNode *previous_operand = (this->*prefix_rule)(nullptr, p_can_assign); |
| 2756 | |
| 2757 | #ifdef TOOLS_ENABLED |
| 2758 | // HACK: We can't create a context in parse_identifier since it is used in places were we don't want completion. |
| 2759 | if (previous_operand != nullptr && previous_operand->type == GDScriptParser::Node::IDENTIFIER && prefix_rule == static_cast<ParseFunction>(&GDScriptParser::parse_identifier)) { |
| 2760 | make_completion_context(COMPLETION_IDENTIFIER, previous_operand); |
| 2761 | } |
| 2762 | #endif |
| 2763 | |
| 2764 | while (p_precedence <= get_rule(current.type)->precedence) { |
| 2765 | if (previous_operand == nullptr || (p_stop_on_assign && current.type == GDScriptTokenizer::Token::EQUAL) || lambda_ended) { |
| 2766 | return previous_operand; |
| 2767 | } |
| 2768 | // Also switch multiline mode on here for infix operators. |
| 2769 | switch (current.type) { |
| 2770 | // case GDScriptTokenizer::Token::BRACE_OPEN: // Not an infix operator. |
| 2771 | case GDScriptTokenizer::Token::PARENTHESIS_OPEN: |
| 2772 | case GDScriptTokenizer::Token::BRACKET_OPEN: |
| 2773 | push_multiline(true); |
| 2774 | break; |
| 2775 | default: |
| 2776 | break; // Nothing to do. |
| 2777 | } |
| 2778 | token = advance(); |
| 2779 | ParseFunction infix_rule = get_rule(token.type)->infix; |
| 2780 | previous_operand = (this->*infix_rule)(previous_operand, p_can_assign); |
| 2781 | } |
| 2782 | |
| 2783 | return previous_operand; |
| 2784 | } |
| 2785 | |
| 2786 | GDScriptParser::ExpressionNode *GDScriptParser::parse_expression(bool p_can_assign, bool p_stop_on_assign) { |
| 2787 | return parse_precedence(PREC_ASSIGNMENT, p_can_assign, p_stop_on_assign); |
| 2788 | } |
| 2789 | |
| 2790 | GDScriptParser::IdentifierNode *GDScriptParser::parse_identifier() { |
| 2791 | IdentifierNode *identifier = static_cast<IdentifierNode *>(parse_identifier(nullptr, false)); |
| 2792 | #ifdef DEBUG_ENABLED |
| 2793 | // Check for spoofing here (if available in TextServer) since this isn't called inside expressions. This is only relevant for declarations. |
| 2794 | if (identifier && TS->has_feature(TextServer::FEATURE_UNICODE_SECURITY) && TS->spoof_check(identifier->name)) { |
| 2795 | push_warning(identifier, GDScriptWarning::CONFUSABLE_IDENTIFIER, identifier->name.operator String()); |
| 2796 | } |
| 2797 | #endif |
| 2798 | return identifier; |
| 2799 | } |
| 2800 | |
| 2801 | GDScriptParser::ExpressionNode *GDScriptParser::parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2802 | if (!previous.is_identifier()) { |
| 2803 | ERR_FAIL_V_MSG(nullptr, "Parser bug: parsing identifier node without identifier token."); |
| 2804 | } |
| 2805 | IdentifierNode *identifier = alloc_node<IdentifierNode>(); |
| 2806 | complete_extents(identifier); |
| 2807 | identifier->name = previous.get_identifier(); |
| 2808 | if (identifier->name.operator String().is_empty()) { |
| 2809 | print_line("Empty identifier found."); |
| 2810 | } |
| 2811 | identifier->suite = current_suite; |
| 2812 | |
| 2813 | if (current_suite != nullptr && current_suite->has_local(identifier->name)) { |
| 2814 | const SuiteNode::Local &declaration = current_suite->get_local(identifier->name); |
| 2815 | |
| 2816 | identifier->source_function = declaration.source_function; |
| 2817 | switch (declaration.type) { |
| 2818 | case SuiteNode::Local::CONSTANT: |
| 2819 | identifier->source = IdentifierNode::LOCAL_CONSTANT; |
| 2820 | identifier->constant_source = declaration.constant; |
| 2821 | declaration.constant->usages++; |
| 2822 | break; |
| 2823 | case SuiteNode::Local::VARIABLE: |
| 2824 | identifier->source = IdentifierNode::LOCAL_VARIABLE; |
| 2825 | identifier->variable_source = declaration.variable; |
| 2826 | declaration.variable->usages++; |
| 2827 | break; |
| 2828 | case SuiteNode::Local::PARAMETER: |
| 2829 | identifier->source = IdentifierNode::FUNCTION_PARAMETER; |
| 2830 | identifier->parameter_source = declaration.parameter; |
| 2831 | declaration.parameter->usages++; |
| 2832 | break; |
| 2833 | case SuiteNode::Local::FOR_VARIABLE: |
| 2834 | identifier->source = IdentifierNode::LOCAL_ITERATOR; |
| 2835 | identifier->bind_source = declaration.bind; |
| 2836 | declaration.bind->usages++; |
| 2837 | break; |
| 2838 | case SuiteNode::Local::PATTERN_BIND: |
| 2839 | identifier->source = IdentifierNode::LOCAL_BIND; |
| 2840 | identifier->bind_source = declaration.bind; |
| 2841 | declaration.bind->usages++; |
| 2842 | break; |
| 2843 | case SuiteNode::Local::UNDEFINED: |
| 2844 | ERR_FAIL_V_MSG(nullptr, "Undefined local found."); |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | return identifier; |
| 2849 | } |
| 2850 | |
| 2851 | GDScriptParser::LiteralNode *GDScriptParser::parse_literal() { |
| 2852 | return static_cast<LiteralNode *>(parse_literal(nullptr, false)); |
| 2853 | } |
| 2854 | |
| 2855 | GDScriptParser::ExpressionNode *GDScriptParser::parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2856 | if (previous.type != GDScriptTokenizer::Token::LITERAL) { |
| 2857 | push_error("Parser bug: parsing literal node without literal token."); |
| 2858 | ERR_FAIL_V_MSG(nullptr, "Parser bug: parsing literal node without literal token."); |
| 2859 | } |
| 2860 | |
| 2861 | LiteralNode *literal = alloc_node<LiteralNode>(); |
| 2862 | literal->value = previous.literal; |
| 2863 | reset_extents(literal, p_previous_operand); |
| 2864 | update_extents(literal); |
| 2865 | make_completion_context(COMPLETION_NONE, literal, -1); |
| 2866 | complete_extents(literal); |
| 2867 | return literal; |
| 2868 | } |
| 2869 | |
| 2870 | GDScriptParser::ExpressionNode *GDScriptParser::parse_self(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2871 | if (current_function && current_function->is_static) { |
| 2872 | push_error(R"(Cannot use "self" inside a static function.)"); |
| 2873 | } |
| 2874 | SelfNode *self = alloc_node<SelfNode>(); |
| 2875 | complete_extents(self); |
| 2876 | self->current_class = current_class; |
| 2877 | return self; |
| 2878 | } |
| 2879 | |
| 2880 | GDScriptParser::ExpressionNode *GDScriptParser::parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2881 | GDScriptTokenizer::Token::Type op_type = previous.type; |
| 2882 | LiteralNode *constant = alloc_node<LiteralNode>(); |
| 2883 | complete_extents(constant); |
| 2884 | |
| 2885 | switch (op_type) { |
| 2886 | case GDScriptTokenizer::Token::CONST_PI: |
| 2887 | constant->value = Math::PI; |
| 2888 | break; |
| 2889 | case GDScriptTokenizer::Token::CONST_TAU: |
| 2890 | constant->value = Math::TAU; |
| 2891 | break; |
| 2892 | case GDScriptTokenizer::Token::CONST_INF: |
| 2893 | constant->value = Math::INF; |
| 2894 | break; |
| 2895 | case GDScriptTokenizer::Token::CONST_NAN: |
| 2896 | constant->value = Math::NaN; |
| 2897 | break; |
| 2898 | default: |
| 2899 | return nullptr; // Unreachable. |
| 2900 | } |
| 2901 | |
| 2902 | return constant; |
| 2903 | } |
| 2904 | |
| 2905 | GDScriptParser::ExpressionNode *GDScriptParser::parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2906 | GDScriptTokenizer::Token::Type op_type = previous.type; |
| 2907 | UnaryOpNode *operation = alloc_node<UnaryOpNode>(); |
| 2908 | |
| 2909 | switch (op_type) { |
| 2910 | case GDScriptTokenizer::Token::MINUS: |
| 2911 | operation->operation = UnaryOpNode::OP_NEGATIVE; |
| 2912 | operation->variant_op = Variant::OP_NEGATE; |
| 2913 | operation->operand = parse_precedence(PREC_SIGN, false); |
| 2914 | if (operation->operand == nullptr) { |
| 2915 | push_error(R"(Expected expression after "-" operator.)"); |
| 2916 | } |
| 2917 | break; |
| 2918 | case GDScriptTokenizer::Token::PLUS: |
| 2919 | operation->operation = UnaryOpNode::OP_POSITIVE; |
| 2920 | operation->variant_op = Variant::OP_POSITIVE; |
| 2921 | operation->operand = parse_precedence(PREC_SIGN, false); |
| 2922 | if (operation->operand == nullptr) { |
| 2923 | push_error(R"(Expected expression after "+" operator.)"); |
| 2924 | } |
| 2925 | break; |
| 2926 | case GDScriptTokenizer::Token::TILDE: |
| 2927 | operation->operation = UnaryOpNode::OP_COMPLEMENT; |
| 2928 | operation->variant_op = Variant::OP_BIT_NEGATE; |
| 2929 | operation->operand = parse_precedence(PREC_BIT_NOT, false); |
| 2930 | if (operation->operand == nullptr) { |
| 2931 | push_error(R"(Expected expression after "~" operator.)"); |
| 2932 | } |
| 2933 | break; |
| 2934 | case GDScriptTokenizer::Token::NOT: |
| 2935 | case GDScriptTokenizer::Token::BANG: |
| 2936 | operation->operation = UnaryOpNode::OP_LOGIC_NOT; |
| 2937 | operation->variant_op = Variant::OP_NOT; |
| 2938 | operation->operand = parse_precedence(PREC_LOGIC_NOT, false); |
| 2939 | if (operation->operand == nullptr) { |
| 2940 | push_error(vformat(R"(Expected expression after "%s" operator.)", op_type == GDScriptTokenizer::Token::NOT ? "not" : "!")); |
| 2941 | } |
| 2942 | break; |
| 2943 | default: |
| 2944 | complete_extents(operation); |
| 2945 | return nullptr; // Unreachable. |
| 2946 | } |
| 2947 | complete_extents(operation); |
| 2948 | |
| 2949 | return operation; |
| 2950 | } |
| 2951 | |
| 2952 | GDScriptParser::ExpressionNode *GDScriptParser::parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2953 | // check that NOT is followed by IN by consuming it before calling parse_binary_operator which will only receive a plain IN |
| 2954 | UnaryOpNode *operation = alloc_node<UnaryOpNode>(); |
| 2955 | reset_extents(operation, p_previous_operand); |
| 2956 | update_extents(operation); |
| 2957 | consume(GDScriptTokenizer::Token::TK_IN, R"(Expected "in" after "not" in content-test operator.)"); |
| 2958 | ExpressionNode *in_operation = parse_binary_operator(p_previous_operand, p_can_assign); |
| 2959 | operation->operation = UnaryOpNode::OP_LOGIC_NOT; |
| 2960 | operation->variant_op = Variant::OP_NOT; |
| 2961 | operation->operand = in_operation; |
| 2962 | complete_extents(operation); |
| 2963 | return operation; |
| 2964 | } |
| 2965 | |
| 2966 | GDScriptParser::ExpressionNode *GDScriptParser::parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 2967 | GDScriptTokenizer::Token op = previous; |
| 2968 | BinaryOpNode *operation = alloc_node<BinaryOpNode>(); |
| 2969 | reset_extents(operation, p_previous_operand); |
| 2970 | update_extents(operation); |
| 2971 | |
| 2972 | Precedence precedence = (Precedence)(get_rule(op.type)->precedence + 1); |
| 2973 | operation->left_operand = p_previous_operand; |
| 2974 | operation->right_operand = parse_precedence(precedence, false); |
| 2975 | complete_extents(operation); |
| 2976 | |
| 2977 | if (operation->right_operand == nullptr) { |
| 2978 | push_error(vformat(R"(Expected expression after "%s" operator.)", op.get_name())); |
| 2979 | } |
| 2980 | |
| 2981 | // TODO: Also for unary, ternary, and assignment. |
| 2982 | switch (op.type) { |
| 2983 | case GDScriptTokenizer::Token::PLUS: |
| 2984 | operation->operation = BinaryOpNode::OP_ADDITION; |
| 2985 | operation->variant_op = Variant::OP_ADD; |
| 2986 | break; |
| 2987 | case GDScriptTokenizer::Token::MINUS: |
| 2988 | operation->operation = BinaryOpNode::OP_SUBTRACTION; |
| 2989 | operation->variant_op = Variant::OP_SUBTRACT; |
| 2990 | break; |
| 2991 | case GDScriptTokenizer::Token::STAR: |
| 2992 | operation->operation = BinaryOpNode::OP_MULTIPLICATION; |
| 2993 | operation->variant_op = Variant::OP_MULTIPLY; |
| 2994 | break; |
| 2995 | case GDScriptTokenizer::Token::SLASH: |
| 2996 | operation->operation = BinaryOpNode::OP_DIVISION; |
| 2997 | operation->variant_op = Variant::OP_DIVIDE; |
| 2998 | break; |
| 2999 | case GDScriptTokenizer::Token::PERCENT: |
| 3000 | operation->operation = BinaryOpNode::OP_MODULO; |
| 3001 | operation->variant_op = Variant::OP_MODULE; |
| 3002 | break; |
| 3003 | case GDScriptTokenizer::Token::STAR_STAR: |
| 3004 | operation->operation = BinaryOpNode::OP_POWER; |
| 3005 | operation->variant_op = Variant::OP_POWER; |
| 3006 | break; |
| 3007 | case GDScriptTokenizer::Token::LESS_LESS: |
| 3008 | operation->operation = BinaryOpNode::OP_BIT_LEFT_SHIFT; |
| 3009 | operation->variant_op = Variant::OP_SHIFT_LEFT; |
| 3010 | break; |
| 3011 | case GDScriptTokenizer::Token::GREATER_GREATER: |
| 3012 | operation->operation = BinaryOpNode::OP_BIT_RIGHT_SHIFT; |
| 3013 | operation->variant_op = Variant::OP_SHIFT_RIGHT; |
| 3014 | break; |
| 3015 | case GDScriptTokenizer::Token::AMPERSAND: |
| 3016 | operation->operation = BinaryOpNode::OP_BIT_AND; |
| 3017 | operation->variant_op = Variant::OP_BIT_AND; |
| 3018 | break; |
| 3019 | case GDScriptTokenizer::Token::PIPE: |
| 3020 | operation->operation = BinaryOpNode::OP_BIT_OR; |
| 3021 | operation->variant_op = Variant::OP_BIT_OR; |
| 3022 | break; |
| 3023 | case GDScriptTokenizer::Token::CARET: |
| 3024 | operation->operation = BinaryOpNode::OP_BIT_XOR; |
| 3025 | operation->variant_op = Variant::OP_BIT_XOR; |
| 3026 | break; |
| 3027 | case GDScriptTokenizer::Token::AND: |
| 3028 | case GDScriptTokenizer::Token::AMPERSAND_AMPERSAND: |
| 3029 | operation->operation = BinaryOpNode::OP_LOGIC_AND; |
| 3030 | operation->variant_op = Variant::OP_AND; |
| 3031 | break; |
| 3032 | case GDScriptTokenizer::Token::OR: |
| 3033 | case GDScriptTokenizer::Token::PIPE_PIPE: |
| 3034 | operation->operation = BinaryOpNode::OP_LOGIC_OR; |
| 3035 | operation->variant_op = Variant::OP_OR; |
| 3036 | break; |
| 3037 | case GDScriptTokenizer::Token::TK_IN: |
| 3038 | operation->operation = BinaryOpNode::OP_CONTENT_TEST; |
| 3039 | operation->variant_op = Variant::OP_IN; |
| 3040 | break; |
| 3041 | case GDScriptTokenizer::Token::EQUAL_EQUAL: |
| 3042 | operation->operation = BinaryOpNode::OP_COMP_EQUAL; |
| 3043 | operation->variant_op = Variant::OP_EQUAL; |
| 3044 | break; |
| 3045 | case GDScriptTokenizer::Token::BANG_EQUAL: |
| 3046 | operation->operation = BinaryOpNode::OP_COMP_NOT_EQUAL; |
| 3047 | operation->variant_op = Variant::OP_NOT_EQUAL; |
| 3048 | break; |
| 3049 | case GDScriptTokenizer::Token::LESS: |
| 3050 | operation->operation = BinaryOpNode::OP_COMP_LESS; |
| 3051 | operation->variant_op = Variant::OP_LESS; |
| 3052 | break; |
| 3053 | case GDScriptTokenizer::Token::LESS_EQUAL: |
| 3054 | operation->operation = BinaryOpNode::OP_COMP_LESS_EQUAL; |
| 3055 | operation->variant_op = Variant::OP_LESS_EQUAL; |
| 3056 | break; |
| 3057 | case GDScriptTokenizer::Token::GREATER: |
| 3058 | operation->operation = BinaryOpNode::OP_COMP_GREATER; |
| 3059 | operation->variant_op = Variant::OP_GREATER; |
| 3060 | break; |
| 3061 | case GDScriptTokenizer::Token::GREATER_EQUAL: |
| 3062 | operation->operation = BinaryOpNode::OP_COMP_GREATER_EQUAL; |
| 3063 | operation->variant_op = Variant::OP_GREATER_EQUAL; |
| 3064 | break; |
| 3065 | default: |
| 3066 | return nullptr; // Unreachable. |
| 3067 | } |
| 3068 | |
| 3069 | return operation; |
| 3070 | } |
| 3071 | |
| 3072 | GDScriptParser::ExpressionNode *GDScriptParser::parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3073 | // Only one ternary operation exists, so no abstraction here. |
| 3074 | TernaryOpNode *operation = alloc_node<TernaryOpNode>(); |
| 3075 | reset_extents(operation, p_previous_operand); |
| 3076 | update_extents(operation); |
| 3077 | |
| 3078 | operation->true_expr = p_previous_operand; |
| 3079 | operation->condition = parse_precedence(PREC_TERNARY, false); |
| 3080 | |
| 3081 | if (operation->condition == nullptr) { |
| 3082 | push_error(R"(Expected expression as ternary condition after "if".)"); |
| 3083 | } |
| 3084 | |
| 3085 | consume(GDScriptTokenizer::Token::ELSE, R"(Expected "else" after ternary operator condition.)"); |
| 3086 | |
| 3087 | operation->false_expr = parse_precedence(PREC_TERNARY, false); |
| 3088 | |
| 3089 | if (operation->false_expr == nullptr) { |
| 3090 | push_error(R"(Expected expression after "else".)"); |
| 3091 | } |
| 3092 | |
| 3093 | complete_extents(operation); |
| 3094 | return operation; |
| 3095 | } |
| 3096 | |
| 3097 | GDScriptParser::ExpressionNode *GDScriptParser::parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3098 | if (!p_can_assign) { |
| 3099 | push_error("Assignment is not allowed inside an expression."); |
| 3100 | return parse_expression(false); // Return the following expression. |
| 3101 | } |
| 3102 | if (p_previous_operand == nullptr) { |
| 3103 | return parse_expression(false); // Return the following expression. |
| 3104 | } |
| 3105 | |
| 3106 | switch (p_previous_operand->type) { |
| 3107 | case Node::IDENTIFIER: { |
| 3108 | #ifdef DEBUG_ENABLED |
| 3109 | // Get source to store assignment count. |
| 3110 | // Also remove one usage since assignment isn't usage. |
| 3111 | IdentifierNode *id = static_cast<IdentifierNode *>(p_previous_operand); |
| 3112 | switch (id->source) { |
| 3113 | case IdentifierNode::LOCAL_VARIABLE: |
| 3114 | id->variable_source->usages--; |
| 3115 | break; |
| 3116 | case IdentifierNode::LOCAL_CONSTANT: |
| 3117 | id->constant_source->usages--; |
| 3118 | break; |
| 3119 | case IdentifierNode::FUNCTION_PARAMETER: |
| 3120 | id->parameter_source->usages--; |
| 3121 | break; |
| 3122 | case IdentifierNode::LOCAL_ITERATOR: |
| 3123 | case IdentifierNode::LOCAL_BIND: |
| 3124 | id->bind_source->usages--; |
| 3125 | break; |
| 3126 | default: |
| 3127 | break; |
| 3128 | } |
| 3129 | #endif |
| 3130 | } break; |
| 3131 | case Node::SUBSCRIPT: |
| 3132 | // Okay. |
| 3133 | break; |
| 3134 | default: |
| 3135 | push_error(R"(Only identifier, attribute access, and subscription access can be used as assignment target.)"); |
| 3136 | return parse_expression(false); // Return the following expression. |
| 3137 | } |
| 3138 | |
| 3139 | AssignmentNode *assignment = alloc_node<AssignmentNode>(); |
| 3140 | reset_extents(assignment, p_previous_operand); |
| 3141 | update_extents(assignment); |
| 3142 | |
| 3143 | make_completion_context(COMPLETION_ASSIGN, assignment); |
| 3144 | switch (previous.type) { |
| 3145 | case GDScriptTokenizer::Token::EQUAL: |
| 3146 | assignment->operation = AssignmentNode::OP_NONE; |
| 3147 | assignment->variant_op = Variant::OP_MAX; |
| 3148 | break; |
| 3149 | case GDScriptTokenizer::Token::PLUS_EQUAL: |
| 3150 | assignment->operation = AssignmentNode::OP_ADDITION; |
| 3151 | assignment->variant_op = Variant::OP_ADD; |
| 3152 | break; |
| 3153 | case GDScriptTokenizer::Token::MINUS_EQUAL: |
| 3154 | assignment->operation = AssignmentNode::OP_SUBTRACTION; |
| 3155 | assignment->variant_op = Variant::OP_SUBTRACT; |
| 3156 | break; |
| 3157 | case GDScriptTokenizer::Token::STAR_EQUAL: |
| 3158 | assignment->operation = AssignmentNode::OP_MULTIPLICATION; |
| 3159 | assignment->variant_op = Variant::OP_MULTIPLY; |
| 3160 | break; |
| 3161 | case GDScriptTokenizer::Token::STAR_STAR_EQUAL: |
| 3162 | assignment->operation = AssignmentNode::OP_POWER; |
| 3163 | assignment->variant_op = Variant::OP_POWER; |
| 3164 | break; |
| 3165 | case GDScriptTokenizer::Token::SLASH_EQUAL: |
| 3166 | assignment->operation = AssignmentNode::OP_DIVISION; |
| 3167 | assignment->variant_op = Variant::OP_DIVIDE; |
| 3168 | break; |
| 3169 | case GDScriptTokenizer::Token::PERCENT_EQUAL: |
| 3170 | assignment->operation = AssignmentNode::OP_MODULO; |
| 3171 | assignment->variant_op = Variant::OP_MODULE; |
| 3172 | break; |
| 3173 | case GDScriptTokenizer::Token::LESS_LESS_EQUAL: |
| 3174 | assignment->operation = AssignmentNode::OP_BIT_SHIFT_LEFT; |
| 3175 | assignment->variant_op = Variant::OP_SHIFT_LEFT; |
| 3176 | break; |
| 3177 | case GDScriptTokenizer::Token::GREATER_GREATER_EQUAL: |
| 3178 | assignment->operation = AssignmentNode::OP_BIT_SHIFT_RIGHT; |
| 3179 | assignment->variant_op = Variant::OP_SHIFT_RIGHT; |
| 3180 | break; |
| 3181 | case GDScriptTokenizer::Token::AMPERSAND_EQUAL: |
| 3182 | assignment->operation = AssignmentNode::OP_BIT_AND; |
| 3183 | assignment->variant_op = Variant::OP_BIT_AND; |
| 3184 | break; |
| 3185 | case GDScriptTokenizer::Token::PIPE_EQUAL: |
| 3186 | assignment->operation = AssignmentNode::OP_BIT_OR; |
| 3187 | assignment->variant_op = Variant::OP_BIT_OR; |
| 3188 | break; |
| 3189 | case GDScriptTokenizer::Token::CARET_EQUAL: |
| 3190 | assignment->operation = AssignmentNode::OP_BIT_XOR; |
| 3191 | assignment->variant_op = Variant::OP_BIT_XOR; |
| 3192 | break; |
| 3193 | default: |
| 3194 | break; // Unreachable. |
| 3195 | } |
| 3196 | assignment->assignee = p_previous_operand; |
| 3197 | assignment->assigned_value = parse_expression(false); |
| 3198 | #ifdef TOOLS_ENABLED |
| 3199 | if (assignment->assigned_value != nullptr && assignment->assigned_value->type == GDScriptParser::Node::IDENTIFIER) { |
| 3200 | override_completion_context(assignment->assigned_value, COMPLETION_ASSIGN, assignment); |
| 3201 | } |
| 3202 | #endif |
| 3203 | if (assignment->assigned_value == nullptr) { |
| 3204 | push_error(R"(Expected an expression after "=".)"); |
| 3205 | } |
| 3206 | complete_extents(assignment); |
| 3207 | |
| 3208 | return assignment; |
| 3209 | } |
| 3210 | |
| 3211 | GDScriptParser::ExpressionNode *GDScriptParser::parse_await(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3212 | AwaitNode *await = alloc_node<AwaitNode>(); |
| 3213 | ExpressionNode *element = parse_precedence(PREC_AWAIT, false); |
| 3214 | if (element == nullptr) { |
| 3215 | push_error(R"(Expected signal or coroutine after "await".)"); |
| 3216 | } |
| 3217 | await->to_await = element; |
| 3218 | complete_extents(await); |
| 3219 | |
| 3220 | if (current_function) { // Might be null in a getter or setter. |
| 3221 | current_function->is_coroutine = true; |
| 3222 | } |
| 3223 | |
| 3224 | return await; |
| 3225 | } |
| 3226 | |
| 3227 | GDScriptParser::ExpressionNode *GDScriptParser::parse_array(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3228 | ArrayNode *array = alloc_node<ArrayNode>(); |
| 3229 | |
| 3230 | if (!check(GDScriptTokenizer::Token::BRACKET_CLOSE)) { |
| 3231 | do { |
| 3232 | if (check(GDScriptTokenizer::Token::BRACKET_CLOSE)) { |
| 3233 | // Allow for trailing comma. |
| 3234 | break; |
| 3235 | } |
| 3236 | |
| 3237 | ExpressionNode *element = parse_expression(false); |
| 3238 | if (element == nullptr) { |
| 3239 | push_error(R"(Expected expression as array element.)"); |
| 3240 | } else { |
| 3241 | array->elements.push_back(element); |
| 3242 | } |
| 3243 | } while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end()); |
| 3244 | } |
| 3245 | pop_multiline(); |
| 3246 | consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected closing "]" after array elements.)"); |
| 3247 | complete_extents(array); |
| 3248 | |
| 3249 | return array; |
| 3250 | } |
| 3251 | |
| 3252 | GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3253 | DictionaryNode *dictionary = alloc_node<DictionaryNode>(); |
| 3254 | |
| 3255 | bool decided_style = false; |
| 3256 | if (!check(GDScriptTokenizer::Token::BRACE_CLOSE)) { |
| 3257 | do { |
| 3258 | if (check(GDScriptTokenizer::Token::BRACE_CLOSE)) { |
| 3259 | // Allow for trailing comma. |
| 3260 | break; |
| 3261 | } |
| 3262 | |
| 3263 | // Key. |
| 3264 | ExpressionNode *key = parse_expression(false, true); // Stop on "=" so we can check for Lua table style. |
| 3265 | |
| 3266 | if (key == nullptr) { |
| 3267 | push_error(R"(Expected expression as dictionary key.)"); |
| 3268 | } |
| 3269 | |
| 3270 | if (!decided_style) { |
| 3271 | switch (current.type) { |
| 3272 | case GDScriptTokenizer::Token::COLON: |
| 3273 | dictionary->style = DictionaryNode::PYTHON_DICT; |
| 3274 | break; |
| 3275 | case GDScriptTokenizer::Token::EQUAL: |
| 3276 | dictionary->style = DictionaryNode::LUA_TABLE; |
| 3277 | break; |
| 3278 | default: |
| 3279 | push_error(R"(Expected ":" or "=" after dictionary key.)"); |
| 3280 | break; |
| 3281 | } |
| 3282 | decided_style = true; |
| 3283 | } |
| 3284 | |
| 3285 | switch (dictionary->style) { |
| 3286 | case DictionaryNode::LUA_TABLE: |
| 3287 | if (key != nullptr && key->type != Node::IDENTIFIER && key->type != Node::LITERAL) { |
| 3288 | push_error(R"(Expected identifier or string as Lua-style dictionary key (e.g "{ key = value }").)"); |
| 3289 | } |
| 3290 | if (key != nullptr && key->type == Node::LITERAL && static_cast<LiteralNode *>(key)->value.get_type() != Variant::STRING) { |
| 3291 | push_error(R"(Expected identifier or string as Lua-style dictionary key (e.g "{ key = value }").)"); |
| 3292 | } |
| 3293 | if (!match(GDScriptTokenizer::Token::EQUAL)) { |
| 3294 | if (match(GDScriptTokenizer::Token::COLON)) { |
| 3295 | push_error(R"(Expected "=" after dictionary key. Mixing dictionary styles is not allowed.)"); |
| 3296 | advance(); // Consume wrong separator anyway. |
| 3297 | } else { |
| 3298 | push_error(R"(Expected "=" after dictionary key.)"); |
| 3299 | } |
| 3300 | } |
| 3301 | if (key != nullptr) { |
| 3302 | key->is_constant = true; |
| 3303 | if (key->type == Node::IDENTIFIER) { |
| 3304 | key->reduced_value = static_cast<IdentifierNode *>(key)->name; |
| 3305 | } else if (key->type == Node::LITERAL) { |
| 3306 | key->reduced_value = StringName(static_cast<LiteralNode *>(key)->value.operator String()); |
| 3307 | } |
| 3308 | } |
| 3309 | break; |
| 3310 | case DictionaryNode::PYTHON_DICT: |
| 3311 | if (!match(GDScriptTokenizer::Token::COLON)) { |
| 3312 | if (match(GDScriptTokenizer::Token::EQUAL)) { |
| 3313 | push_error(R"(Expected ":" after dictionary key. Mixing dictionary styles is not allowed.)"); |
| 3314 | advance(); // Consume wrong separator anyway. |
| 3315 | } else { |
| 3316 | push_error(R"(Expected ":" after dictionary key.)"); |
| 3317 | } |
| 3318 | } |
| 3319 | break; |
| 3320 | } |
| 3321 | |
| 3322 | // Value. |
| 3323 | ExpressionNode *value = parse_expression(false); |
| 3324 | if (value == nullptr) { |
| 3325 | push_error(R"(Expected expression as dictionary value.)"); |
| 3326 | } |
| 3327 | |
| 3328 | if (key != nullptr && value != nullptr) { |
| 3329 | dictionary->elements.push_back({ key, value }); |
| 3330 | } |
| 3331 | |
| 3332 | // Do phrase level recovery by inserting an imaginary expression for missing keys or values. |
| 3333 | // This ensures the successfully parsed expression is part of the AST and can be analyzed. |
| 3334 | if (key != nullptr && value == nullptr) { |
| 3335 | LiteralNode *dummy = alloc_recovery_node<LiteralNode>(); |
| 3336 | dummy->value = Variant(); |
| 3337 | |
| 3338 | dictionary->elements.push_back({ key, dummy }); |
| 3339 | } else if (key == nullptr && value != nullptr) { |
| 3340 | LiteralNode *dummy = alloc_recovery_node<LiteralNode>(); |
| 3341 | dummy->value = Variant(); |
| 3342 | |
| 3343 | dictionary->elements.push_back({ dummy, value }); |
| 3344 | } |
| 3345 | |
| 3346 | } while (match(GDScriptTokenizer::Token::COMMA) && !is_at_end()); |
| 3347 | } |
| 3348 | pop_multiline(); |
| 3349 | consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected closing "}" after dictionary elements.)"); |
| 3350 | complete_extents(dictionary); |
| 3351 | |
| 3352 | return dictionary; |
| 3353 | } |
| 3354 | |
| 3355 | GDScriptParser::ExpressionNode *GDScriptParser::parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3356 | ExpressionNode *grouped = parse_expression(false); |
| 3357 | pop_multiline(); |
| 3358 | if (grouped == nullptr) { |
| 3359 | push_error(R"(Expected grouping expression.)"); |
| 3360 | } else { |
| 3361 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after grouping expression.)*"); |
| 3362 | } |
| 3363 | return grouped; |
| 3364 | } |
| 3365 | |
| 3366 | GDScriptParser::ExpressionNode *GDScriptParser::parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3367 | SubscriptNode *attribute = alloc_node<SubscriptNode>(); |
| 3368 | reset_extents(attribute, p_previous_operand); |
| 3369 | update_extents(attribute); |
| 3370 | |
| 3371 | if (for_completion) { |
| 3372 | bool is_builtin = false; |
| 3373 | if (p_previous_operand && p_previous_operand->type == Node::IDENTIFIER) { |
| 3374 | const IdentifierNode *id = static_cast<const IdentifierNode *>(p_previous_operand); |
| 3375 | Variant::Type builtin_type = get_builtin_type(id->name); |
| 3376 | if (builtin_type < Variant::VARIANT_MAX) { |
| 3377 | make_completion_context(COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD, builtin_type); |
| 3378 | is_builtin = true; |
| 3379 | } |
| 3380 | } |
| 3381 | if (!is_builtin) { |
| 3382 | make_completion_context(COMPLETION_ATTRIBUTE, attribute, -1); |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | attribute->base = p_previous_operand; |
| 3387 | |
| 3388 | if (current.is_node_name()) { |
| 3389 | current.type = GDScriptTokenizer::Token::IDENTIFIER; |
| 3390 | } |
| 3391 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected identifier after "." for attribute access.)")) { |
| 3392 | complete_extents(attribute); |
| 3393 | return attribute; |
| 3394 | } |
| 3395 | |
| 3396 | attribute->is_attribute = true; |
| 3397 | attribute->attribute = parse_identifier(); |
| 3398 | |
| 3399 | complete_extents(attribute); |
| 3400 | return attribute; |
| 3401 | } |
| 3402 | |
| 3403 | GDScriptParser::ExpressionNode *GDScriptParser::parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3404 | SubscriptNode *subscript = alloc_node<SubscriptNode>(); |
| 3405 | reset_extents(subscript, p_previous_operand); |
| 3406 | update_extents(subscript); |
| 3407 | |
| 3408 | make_completion_context(COMPLETION_SUBSCRIPT, subscript); |
| 3409 | |
| 3410 | subscript->base = p_previous_operand; |
| 3411 | subscript->index = parse_expression(false); |
| 3412 | |
| 3413 | #ifdef TOOLS_ENABLED |
| 3414 | if (subscript->index != nullptr && subscript->index->type == Node::LITERAL) { |
| 3415 | override_completion_context(subscript->index, COMPLETION_SUBSCRIPT, subscript); |
| 3416 | } |
| 3417 | #endif |
| 3418 | |
| 3419 | if (subscript->index == nullptr) { |
| 3420 | push_error(R"(Expected expression after "[".)"); |
| 3421 | } |
| 3422 | |
| 3423 | pop_multiline(); |
| 3424 | consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected "]" after subscription index.)"); |
| 3425 | complete_extents(subscript); |
| 3426 | |
| 3427 | return subscript; |
| 3428 | } |
| 3429 | |
| 3430 | GDScriptParser::ExpressionNode *GDScriptParser::parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3431 | CastNode *cast = alloc_node<CastNode>(); |
| 3432 | reset_extents(cast, p_previous_operand); |
| 3433 | update_extents(cast); |
| 3434 | |
| 3435 | cast->operand = p_previous_operand; |
| 3436 | cast->cast_type = parse_type(); |
| 3437 | complete_extents(cast); |
| 3438 | |
| 3439 | if (cast->cast_type == nullptr) { |
| 3440 | push_error(R"(Expected type specifier after "as".)"); |
| 3441 | return p_previous_operand; |
| 3442 | } |
| 3443 | |
| 3444 | return cast; |
| 3445 | } |
| 3446 | |
| 3447 | GDScriptParser::ExpressionNode *GDScriptParser::parse_call(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3448 | CallNode *call = alloc_node<CallNode>(); |
| 3449 | reset_extents(call, p_previous_operand); |
| 3450 | |
| 3451 | if (previous.type == GDScriptTokenizer::Token::SUPER) { |
| 3452 | // Super call. |
| 3453 | call->is_super = true; |
| 3454 | if (!check(GDScriptTokenizer::Token::PERIOD)) { |
| 3455 | make_completion_context(COMPLETION_SUPER, call); |
| 3456 | } |
| 3457 | push_multiline(true); |
| 3458 | if (match(GDScriptTokenizer::Token::PARENTHESIS_OPEN)) { |
| 3459 | // Implicit call to the parent method of the same name. |
| 3460 | if (current_function == nullptr) { |
| 3461 | push_error(R"(Cannot use implicit "super" call outside of a function.)"); |
| 3462 | pop_multiline(); |
| 3463 | complete_extents(call); |
| 3464 | return nullptr; |
| 3465 | } |
| 3466 | if (current_function->identifier) { |
| 3467 | call->function_name = current_function->identifier->name; |
| 3468 | } else { |
| 3469 | call->function_name = SNAME("<anonymous>"); |
| 3470 | } |
| 3471 | } else { |
| 3472 | consume(GDScriptTokenizer::Token::PERIOD, R"(Expected "." or "(" after "super".)"); |
| 3473 | make_completion_context(COMPLETION_SUPER_METHOD, call); |
| 3474 | if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected function name after ".".)")) { |
| 3475 | pop_multiline(); |
| 3476 | complete_extents(call); |
| 3477 | return nullptr; |
| 3478 | } |
| 3479 | IdentifierNode *identifier = parse_identifier(); |
| 3480 | call->callee = identifier; |
| 3481 | call->function_name = identifier->name; |
| 3482 | if (!consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after function name.)")) { |
| 3483 | pop_multiline(); |
| 3484 | complete_extents(call); |
| 3485 | return nullptr; |
| 3486 | } |
| 3487 | } |
| 3488 | } else { |
| 3489 | call->callee = p_previous_operand; |
| 3490 | |
| 3491 | if (call->callee == nullptr) { |
| 3492 | push_error(R"*(Cannot call on an expression. Use ".call()" if it's a Callable.)*"); |
| 3493 | } else if (call->callee->type == Node::IDENTIFIER) { |
| 3494 | call->function_name = static_cast<IdentifierNode *>(call->callee)->name; |
| 3495 | make_completion_context(COMPLETION_METHOD, call->callee); |
| 3496 | } else if (call->callee->type == Node::SUBSCRIPT) { |
| 3497 | SubscriptNode *attribute = static_cast<SubscriptNode *>(call->callee); |
| 3498 | if (attribute->is_attribute) { |
| 3499 | if (attribute->attribute) { |
| 3500 | call->function_name = attribute->attribute->name; |
| 3501 | } |
| 3502 | make_completion_context(COMPLETION_ATTRIBUTE_METHOD, call->callee); |
| 3503 | } else { |
| 3504 | // TODO: The analyzer can see if this is actually a Callable and give better error message. |
| 3505 | push_error(R"*(Cannot call on an expression. Use ".call()" if it's a Callable.)*"); |
| 3506 | } |
| 3507 | } else { |
| 3508 | push_error(R"*(Cannot call on an expression. Use ".call()" if it's a Callable.)*"); |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | // Arguments. |
| 3513 | CompletionType ct = COMPLETION_CALL_ARGUMENTS; |
| 3514 | if (call->function_name == SNAME("load")) { |
| 3515 | ct = COMPLETION_RESOURCE_PATH; |
| 3516 | } |
| 3517 | push_completion_call(call); |
| 3518 | int argument_index = 0; |
| 3519 | do { |
| 3520 | make_completion_context(ct, call, argument_index); |
| 3521 | set_last_completion_call_arg(argument_index); |
| 3522 | if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { |
| 3523 | // Allow for trailing comma. |
| 3524 | break; |
| 3525 | } |
| 3526 | ExpressionNode *argument = parse_expression(false); |
| 3527 | if (argument == nullptr) { |
| 3528 | push_error(R"(Expected expression as the function argument.)"); |
| 3529 | } else { |
| 3530 | call->arguments.push_back(argument); |
| 3531 | |
| 3532 | if (argument->type == Node::LITERAL) { |
| 3533 | override_completion_context(argument, ct, call, argument_index); |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | ct = COMPLETION_CALL_ARGUMENTS; |
| 3538 | argument_index++; |
| 3539 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 3540 | pop_completion_call(); |
| 3541 | |
| 3542 | pop_multiline(); |
| 3543 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after call arguments.)*"); |
| 3544 | complete_extents(call); |
| 3545 | |
| 3546 | return call; |
| 3547 | } |
| 3548 | |
| 3549 | GDScriptParser::ExpressionNode *GDScriptParser::parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3550 | // We want code completion after a DOLLAR even if the current code is invalid. |
| 3551 | make_completion_context(COMPLETION_GET_NODE, nullptr, -1); |
| 3552 | |
| 3553 | if (!current.is_node_name() && !check(GDScriptTokenizer::Token::LITERAL) && !check(GDScriptTokenizer::Token::SLASH) && !check(GDScriptTokenizer::Token::PERCENT)) { |
| 3554 | push_error(vformat(R"(Expected node path as string or identifier after "%s".)", previous.get_name())); |
| 3555 | return nullptr; |
| 3556 | } |
| 3557 | |
| 3558 | if (check(GDScriptTokenizer::Token::LITERAL)) { |
| 3559 | if (current.literal.get_type() != Variant::STRING) { |
| 3560 | push_error(vformat(R"(Expected node path as string or identifier after "%s".)", previous.get_name())); |
| 3561 | return nullptr; |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | GetNodeNode *get_node = alloc_node<GetNodeNode>(); |
| 3566 | |
| 3567 | // Store the last item in the path so the parser knows what to expect. |
| 3568 | // Allow allows more specific error messages. |
| 3569 | enum PathState { |
| 3570 | PATH_STATE_START, |
| 3571 | PATH_STATE_SLASH, |
| 3572 | PATH_STATE_PERCENT, |
| 3573 | PATH_STATE_NODE_NAME, |
| 3574 | } path_state = PATH_STATE_START; |
| 3575 | |
| 3576 | if (previous.type == GDScriptTokenizer::Token::DOLLAR) { |
| 3577 | // Detect initial slash, which will be handled in the loop if it matches. |
| 3578 | match(GDScriptTokenizer::Token::SLASH); |
| 3579 | } else { |
| 3580 | get_node->use_dollar = false; |
| 3581 | } |
| 3582 | |
| 3583 | int context_argument = 0; |
| 3584 | |
| 3585 | do { |
| 3586 | if (previous.type == GDScriptTokenizer::Token::PERCENT) { |
| 3587 | if (path_state != PATH_STATE_START && path_state != PATH_STATE_SLASH) { |
| 3588 | push_error(R"("%" is only valid in the beginning of a node name (either after "$" or after "/"))"); |
| 3589 | complete_extents(get_node); |
| 3590 | return nullptr; |
| 3591 | } |
| 3592 | |
| 3593 | get_node->full_path += "%"; |
| 3594 | |
| 3595 | path_state = PATH_STATE_PERCENT; |
| 3596 | } else if (previous.type == GDScriptTokenizer::Token::SLASH) { |
| 3597 | if (path_state != PATH_STATE_START && path_state != PATH_STATE_NODE_NAME) { |
| 3598 | push_error(R"("/" is only valid at the beginning of the path or after a node name.)"); |
| 3599 | complete_extents(get_node); |
| 3600 | return nullptr; |
| 3601 | } |
| 3602 | |
| 3603 | get_node->full_path += "/"; |
| 3604 | |
| 3605 | path_state = PATH_STATE_SLASH; |
| 3606 | } |
| 3607 | |
| 3608 | make_completion_context(COMPLETION_GET_NODE, get_node, context_argument++); |
| 3609 | |
| 3610 | if (match(GDScriptTokenizer::Token::LITERAL)) { |
| 3611 | if (previous.literal.get_type() != Variant::STRING) { |
| 3612 | String previous_token; |
| 3613 | switch (path_state) { |
| 3614 | case PATH_STATE_START: |
| 3615 | previous_token = "$"; |
| 3616 | break; |
| 3617 | case PATH_STATE_PERCENT: |
| 3618 | previous_token = "%"; |
| 3619 | break; |
| 3620 | case PATH_STATE_SLASH: |
| 3621 | previous_token = "/"; |
| 3622 | break; |
| 3623 | default: |
| 3624 | break; |
| 3625 | } |
| 3626 | push_error(vformat(R"(Expected node path as string or identifier after "%s".)", previous_token)); |
| 3627 | complete_extents(get_node); |
| 3628 | return nullptr; |
| 3629 | } |
| 3630 | |
| 3631 | get_node->full_path += previous.literal.operator String(); |
| 3632 | |
| 3633 | path_state = PATH_STATE_NODE_NAME; |
| 3634 | } else if (current.is_node_name()) { |
| 3635 | advance(); |
| 3636 | |
| 3637 | String identifier = previous.get_identifier(); |
| 3638 | #ifdef DEBUG_ENABLED |
| 3639 | // Check spoofing. |
| 3640 | if (TS->has_feature(TextServer::FEATURE_UNICODE_SECURITY) && TS->spoof_check(identifier)) { |
| 3641 | push_warning(get_node, GDScriptWarning::CONFUSABLE_IDENTIFIER, identifier); |
| 3642 | } |
| 3643 | #endif |
| 3644 | get_node->full_path += identifier; |
| 3645 | |
| 3646 | path_state = PATH_STATE_NODE_NAME; |
| 3647 | } else if (!check(GDScriptTokenizer::Token::SLASH) && !check(GDScriptTokenizer::Token::PERCENT)) { |
| 3648 | push_error(vformat(R"(Unexpected "%s" in node path.)", current.get_name())); |
| 3649 | complete_extents(get_node); |
| 3650 | return nullptr; |
| 3651 | } |
| 3652 | } while (match(GDScriptTokenizer::Token::SLASH) || match(GDScriptTokenizer::Token::PERCENT)); |
| 3653 | |
| 3654 | complete_extents(get_node); |
| 3655 | return get_node; |
| 3656 | } |
| 3657 | |
| 3658 | GDScriptParser::ExpressionNode *GDScriptParser::parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3659 | PreloadNode *preload = alloc_node<PreloadNode>(); |
| 3660 | preload->resolved_path = "<missing path>"; |
| 3661 | |
| 3662 | push_multiline(true); |
| 3663 | consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected "(" after "preload".)"); |
| 3664 | |
| 3665 | make_completion_context(COMPLETION_RESOURCE_PATH, preload); |
| 3666 | push_completion_call(preload); |
| 3667 | |
| 3668 | preload->path = parse_expression(false); |
| 3669 | |
| 3670 | if (preload->path == nullptr) { |
| 3671 | push_error(R"(Expected resource path after "(".)"); |
| 3672 | } else if (preload->path->type == Node::LITERAL) { |
| 3673 | override_completion_context(preload->path, COMPLETION_RESOURCE_PATH, preload); |
| 3674 | } |
| 3675 | |
| 3676 | pop_completion_call(); |
| 3677 | |
| 3678 | // Allow trailing comma. |
| 3679 | match(GDScriptTokenizer::Token::COMMA); |
| 3680 | |
| 3681 | pop_multiline(); |
| 3682 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected ")" after preload path.)*"); |
| 3683 | complete_extents(preload); |
| 3684 | |
| 3685 | return preload; |
| 3686 | } |
| 3687 | |
| 3688 | GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3689 | LambdaNode *lambda = alloc_node<LambdaNode>(); |
| 3690 | lambda->parent_function = current_function; |
| 3691 | lambda->parent_lambda = current_lambda; |
| 3692 | |
| 3693 | FunctionNode *function = alloc_node<FunctionNode>(); |
| 3694 | function->source_lambda = lambda; |
| 3695 | |
| 3696 | function->is_static = current_function != nullptr ? current_function->is_static : false; |
| 3697 | |
| 3698 | if (match(GDScriptTokenizer::Token::IDENTIFIER)) { |
| 3699 | function->identifier = parse_identifier(); |
| 3700 | } |
| 3701 | |
| 3702 | bool multiline_context = multiline_stack.back()->get(); |
| 3703 | |
| 3704 | push_completion_call(nullptr); |
| 3705 | |
| 3706 | // Reset the multiline stack since we don't want the multiline mode one in the lambda body. |
| 3707 | push_multiline(false); |
| 3708 | if (multiline_context) { |
| 3709 | tokenizer->push_expression_indented_block(); |
| 3710 | } |
| 3711 | |
| 3712 | push_multiline(true); // For the parameters. |
| 3713 | if (function->identifier) { |
| 3714 | consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after lambda name.)"); |
| 3715 | } else { |
| 3716 | consume(GDScriptTokenizer::Token::PARENTHESIS_OPEN, R"(Expected opening "(" after "func".)"); |
| 3717 | } |
| 3718 | |
| 3719 | FunctionNode *previous_function = current_function; |
| 3720 | current_function = function; |
| 3721 | |
| 3722 | LambdaNode *previous_lambda = current_lambda; |
| 3723 | current_lambda = lambda; |
| 3724 | |
| 3725 | SuiteNode *body = alloc_node<SuiteNode>(); |
| 3726 | body->parent_function = current_function; |
| 3727 | body->parent_block = current_suite; |
| 3728 | |
| 3729 | SuiteNode *previous_suite = current_suite; |
| 3730 | current_suite = body; |
| 3731 | |
| 3732 | parse_function_signature(function, body, "lambda", -1); |
| 3733 | |
| 3734 | current_suite = previous_suite; |
| 3735 | |
| 3736 | bool previous_in_lambda = in_lambda; |
| 3737 | in_lambda = true; |
| 3738 | |
| 3739 | // Save break/continue state. |
| 3740 | bool could_break = can_break; |
| 3741 | bool could_continue = can_continue; |
| 3742 | |
| 3743 | // Disallow break/continue. |
| 3744 | can_break = false; |
| 3745 | can_continue = false; |
| 3746 | |
| 3747 | function->body = parse_suite("lambda declaration", body, true); |
| 3748 | complete_extents(function); |
| 3749 | complete_extents(lambda); |
| 3750 | |
| 3751 | pop_multiline(); |
| 3752 | |
| 3753 | pop_completion_call(); |
| 3754 | |
| 3755 | if (multiline_context) { |
| 3756 | // If we're in multiline mode, we want to skip the spurious DEDENT and NEWLINE tokens. |
| 3757 | while (check(GDScriptTokenizer::Token::DEDENT) || check(GDScriptTokenizer::Token::INDENT) || check(GDScriptTokenizer::Token::NEWLINE)) { |
| 3758 | current = tokenizer->scan(); // Not advance() since we don't want to change the previous token. |
| 3759 | } |
| 3760 | tokenizer->pop_expression_indented_block(); |
| 3761 | } |
| 3762 | |
| 3763 | current_function = previous_function; |
| 3764 | current_lambda = previous_lambda; |
| 3765 | in_lambda = previous_in_lambda; |
| 3766 | lambda->function = function; |
| 3767 | |
| 3768 | // Reset break/continue state. |
| 3769 | can_break = could_break; |
| 3770 | can_continue = could_continue; |
| 3771 | |
| 3772 | return lambda; |
| 3773 | } |
| 3774 | |
| 3775 | GDScriptParser::ExpressionNode *GDScriptParser::parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3776 | // x is not int |
| 3777 | // ^ ^^^ ExpressionNode, TypeNode |
| 3778 | // ^^^^^^^^^^^^ TypeTestNode |
| 3779 | // ^^^^^^^^^^^^ UnaryOpNode |
| 3780 | UnaryOpNode *not_node = nullptr; |
| 3781 | if (match(GDScriptTokenizer::Token::NOT)) { |
| 3782 | not_node = alloc_node<UnaryOpNode>(); |
| 3783 | not_node->operation = UnaryOpNode::OP_LOGIC_NOT; |
| 3784 | not_node->variant_op = Variant::OP_NOT; |
| 3785 | reset_extents(not_node, p_previous_operand); |
| 3786 | update_extents(not_node); |
| 3787 | } |
| 3788 | |
| 3789 | TypeTestNode *type_test = alloc_node<TypeTestNode>(); |
| 3790 | reset_extents(type_test, p_previous_operand); |
| 3791 | update_extents(type_test); |
| 3792 | |
| 3793 | type_test->operand = p_previous_operand; |
| 3794 | type_test->test_type = parse_type(); |
| 3795 | complete_extents(type_test); |
| 3796 | |
| 3797 | if (not_node != nullptr) { |
| 3798 | not_node->operand = type_test; |
| 3799 | complete_extents(not_node); |
| 3800 | } |
| 3801 | |
| 3802 | if (type_test->test_type == nullptr) { |
| 3803 | if (not_node == nullptr) { |
| 3804 | push_error(R"(Expected type specifier after "is".)"); |
| 3805 | } else { |
| 3806 | push_error(R"(Expected type specifier after "is not".)"); |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | if (not_node != nullptr) { |
| 3811 | return not_node; |
| 3812 | } |
| 3813 | |
| 3814 | return type_test; |
| 3815 | } |
| 3816 | |
| 3817 | GDScriptParser::ExpressionNode *GDScriptParser::parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3818 | push_error(R"("yield" was removed in Godot 4. Use "await" instead.)"); |
| 3819 | return nullptr; |
| 3820 | } |
| 3821 | |
| 3822 | GDScriptParser::ExpressionNode *GDScriptParser::parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign) { |
| 3823 | // Just for better error messages. |
| 3824 | GDScriptTokenizer::Token::Type invalid = previous.type; |
| 3825 | |
| 3826 | switch (invalid) { |
| 3827 | case GDScriptTokenizer::Token::QUESTION_MARK: |
| 3828 | push_error(R"(Unexpected "?" in source. If you want a ternary operator, use "truthy_value if true_condition else falsy_value".)"); |
| 3829 | break; |
| 3830 | default: |
| 3831 | return nullptr; // Unreachable. |
| 3832 | } |
| 3833 | |
| 3834 | // Return the previous expression. |
| 3835 | return p_previous_operand; |
| 3836 | } |
| 3837 | |
| 3838 | GDScriptParser::TypeNode *GDScriptParser::parse_type(bool p_allow_void) { |
| 3839 | TypeNode *type = alloc_node<TypeNode>(); |
| 3840 | make_completion_context(p_allow_void ? COMPLETION_TYPE_NAME_OR_VOID : COMPLETION_TYPE_NAME, type); |
| 3841 | if (!match(GDScriptTokenizer::Token::IDENTIFIER)) { |
| 3842 | if (match(GDScriptTokenizer::Token::TK_VOID)) { |
| 3843 | if (p_allow_void) { |
| 3844 | complete_extents(type); |
| 3845 | TypeNode *void_type = type; |
| 3846 | return void_type; |
| 3847 | } else { |
| 3848 | push_error(R"("void" is only allowed for a function return type.)"); |
| 3849 | } |
| 3850 | } |
| 3851 | // Leave error message to the caller who knows the context. |
| 3852 | complete_extents(type); |
| 3853 | return nullptr; |
| 3854 | } |
| 3855 | |
| 3856 | IdentifierNode *type_element = parse_identifier(); |
| 3857 | |
| 3858 | type->type_chain.push_back(type_element); |
| 3859 | |
| 3860 | if (match(GDScriptTokenizer::Token::BRACKET_OPEN)) { |
| 3861 | // Typed collection (like Array[int], Dictionary[String, int]). |
| 3862 | bool first_pass = true; |
| 3863 | do { |
| 3864 | TypeNode *container_type = parse_type(false); // Don't allow void for element type. |
| 3865 | if (container_type == nullptr) { |
| 3866 | push_error(vformat(R"(Expected type for collection after "%s".)", first_pass ? "[" : ",")); |
| 3867 | complete_extents(type); |
| 3868 | type = nullptr; |
| 3869 | break; |
| 3870 | } else if (container_type->container_types.size() > 0) { |
| 3871 | push_error("Nested typed collections are not supported."); |
| 3872 | } else { |
| 3873 | type->container_types.append(container_type); |
| 3874 | } |
| 3875 | first_pass = false; |
| 3876 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 3877 | consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected closing "]" after collection type.)"); |
| 3878 | if (type != nullptr) { |
| 3879 | complete_extents(type); |
| 3880 | } |
| 3881 | return type; |
| 3882 | } |
| 3883 | |
| 3884 | int chain_index = 1; |
| 3885 | while (match(GDScriptTokenizer::Token::PERIOD)) { |
| 3886 | make_completion_context(COMPLETION_TYPE_ATTRIBUTE, type, chain_index++); |
| 3887 | if (consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected inner type name after ".".)")) { |
| 3888 | type_element = parse_identifier(); |
| 3889 | type->type_chain.push_back(type_element); |
| 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | complete_extents(type); |
| 3894 | return type; |
| 3895 | } |
| 3896 | |
| 3897 | #ifdef TOOLS_ENABLED |
| 3898 | enum DocLineState { |
| 3899 | DOC_LINE_NORMAL, |
| 3900 | DOC_LINE_IN_CODE, |
| 3901 | DOC_LINE_IN_CODEBLOCK, |
| 3902 | DOC_LINE_IN_KBD, |
| 3903 | }; |
| 3904 | |
| 3905 | static String _process_doc_line(const String &p_line, const String &p_text, const String &p_space_prefix, DocLineState &r_state) { |
| 3906 | String line = p_line; |
| 3907 | if (r_state == DOC_LINE_NORMAL) { |
| 3908 | line = line.strip_edges(true, false); |
| 3909 | } else { |
| 3910 | line = line.trim_prefix(p_space_prefix); |
| 3911 | } |
| 3912 | |
| 3913 | String line_join; |
| 3914 | if (!p_text.is_empty()) { |
| 3915 | if (r_state == DOC_LINE_NORMAL) { |
| 3916 | if (p_text.ends_with("[/codeblock]")) { |
| 3917 | line_join = "\n"; |
| 3918 | } else if (!p_text.ends_with("[br]")) { |
| 3919 | line_join = " "; |
| 3920 | } |
| 3921 | } else { |
| 3922 | line_join = "\n"; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | String result; |
| 3927 | int from = 0; |
| 3928 | int buffer_start = 0; |
| 3929 | const int len = line.length(); |
| 3930 | bool process = true; |
| 3931 | while (process) { |
| 3932 | switch (r_state) { |
| 3933 | case DOC_LINE_NORMAL: { |
| 3934 | int lb_pos = line.find_char('[', from); |
| 3935 | if (lb_pos < 0) { |
| 3936 | process = false; |
| 3937 | break; |
| 3938 | } |
| 3939 | int rb_pos = line.find_char(']', lb_pos + 1); |
| 3940 | if (rb_pos < 0) { |
| 3941 | process = false; |
| 3942 | break; |
| 3943 | } |
| 3944 | |
| 3945 | from = rb_pos + 1; |
| 3946 | |
| 3947 | String tag = line.substr(lb_pos + 1, rb_pos - lb_pos - 1); |
| 3948 | if (tag == "code" || tag.begins_with("code ")) { |
| 3949 | r_state = DOC_LINE_IN_CODE; |
| 3950 | } else if (tag == "codeblock" || tag.begins_with("codeblock ")) { |
| 3951 | if (lb_pos == 0) { |
| 3952 | line_join = "\n"; |
| 3953 | } else { |
| 3954 | result += line.substr(buffer_start, lb_pos - buffer_start) + '\n'; |
| 3955 | } |
| 3956 | result += "[" + tag + "]"; |
| 3957 | if (from < len) { |
| 3958 | result += '\n'; |
| 3959 | } |
| 3960 | |
| 3961 | r_state = DOC_LINE_IN_CODEBLOCK; |
| 3962 | buffer_start = from; |
| 3963 | } else if (tag == "kbd") { |
| 3964 | r_state = DOC_LINE_IN_KBD; |
| 3965 | } |
| 3966 | } break; |
| 3967 | case DOC_LINE_IN_CODE: { |
| 3968 | int pos = line.find("[/code]", from); |
| 3969 | if (pos < 0) { |
| 3970 | process = false; |
| 3971 | break; |
| 3972 | } |
| 3973 | |
| 3974 | from = pos + 7; // `len("[/code]")`. |
| 3975 | |
| 3976 | r_state = DOC_LINE_NORMAL; |
| 3977 | } break; |
| 3978 | case DOC_LINE_IN_CODEBLOCK: { |
| 3979 | int pos = line.find("[/codeblock]", from); |
| 3980 | if (pos < 0) { |
| 3981 | process = false; |
| 3982 | break; |
| 3983 | } |
| 3984 | |
| 3985 | from = pos + 12; // `len("[/codeblock]")`. |
| 3986 | |
| 3987 | if (pos == 0) { |
| 3988 | line_join = "\n"; |
| 3989 | } else { |
| 3990 | result += line.substr(buffer_start, pos - buffer_start) + '\n'; |
| 3991 | } |
| 3992 | result += "[/codeblock]"; |
| 3993 | if (from < len) { |
| 3994 | result += '\n'; |
| 3995 | } |
| 3996 | |
| 3997 | r_state = DOC_LINE_NORMAL; |
| 3998 | buffer_start = from; |
| 3999 | } break; |
| 4000 | case DOC_LINE_IN_KBD: { |
| 4001 | int pos = line.find("[/kbd]", from); |
| 4002 | if (pos < 0) { |
| 4003 | process = false; |
| 4004 | break; |
| 4005 | } |
| 4006 | |
| 4007 | from = pos + 6; // `len("[/kbd]")`. |
| 4008 | |
| 4009 | r_state = DOC_LINE_NORMAL; |
| 4010 | } break; |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | result += line.substr(buffer_start); |
| 4015 | if (r_state == DOC_LINE_NORMAL) { |
| 4016 | result = result.strip_edges(false, true); |
| 4017 | } |
| 4018 | |
| 4019 | return line_join + result; |
| 4020 | } |
| 4021 | |
| 4022 | bool GDScriptParser::has_comment(int p_line, bool p_must_be_doc) { |
| 4023 | bool has_comment = tokenizer->get_comments().has(p_line); |
| 4024 | // If there are no comments or if we don't care whether the comment |
| 4025 | // is a docstring, we have our result. |
| 4026 | if (!p_must_be_doc || !has_comment) { |
| 4027 | return has_comment; |
| 4028 | } |
| 4029 | |
| 4030 | return tokenizer->get_comments()[p_line].comment.begins_with("##"); |
| 4031 | } |
| 4032 | |
| 4033 | GDScriptParser::MemberDocData GDScriptParser::parse_doc_comment(int p_line, bool p_single_line) { |
| 4034 | ERR_FAIL_COND_V(!has_comment(p_line, true), MemberDocData()); |
| 4035 | |
| 4036 | const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments(); |
| 4037 | int line = p_line; |
| 4038 | |
| 4039 | if (!p_single_line) { |
| 4040 | while (comments.has(line - 1) && comments[line - 1].new_line && comments[line - 1].comment.begins_with("##")) { |
| 4041 | line--; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | max_script_doc_line = MIN(max_script_doc_line, line - 1); |
| 4046 | |
| 4047 | String space_prefix; |
| 4048 | { |
| 4049 | int i = 2; |
| 4050 | for (; i < comments[line].comment.length(); i++) { |
| 4051 | if (comments[line].comment[i] != ' ') { |
| 4052 | break; |
| 4053 | } |
| 4054 | } |
| 4055 | space_prefix = String(" ").repeat(i - 2); |
| 4056 | } |
| 4057 | |
| 4058 | DocLineState state = DOC_LINE_NORMAL; |
| 4059 | MemberDocData result; |
| 4060 | |
| 4061 | while (line <= p_line) { |
| 4062 | String doc_line = comments[line].comment.trim_prefix("##"); |
| 4063 | line++; |
| 4064 | |
| 4065 | if (state == DOC_LINE_NORMAL) { |
| 4066 | String stripped_line = doc_line.strip_edges(); |
| 4067 | if (stripped_line == "@deprecated" || stripped_line.begins_with("@deprecated:")) { |
| 4068 | result.is_deprecated = true; |
| 4069 | if (stripped_line.begins_with("@deprecated:")) { |
| 4070 | result.deprecated_message = stripped_line.trim_prefix("@deprecated:").strip_edges(); |
| 4071 | } |
| 4072 | continue; |
| 4073 | } else if (stripped_line == "@experimental" || stripped_line.begins_with("@experimental:")) { |
| 4074 | result.is_experimental = true; |
| 4075 | if (stripped_line.begins_with("@experimental:")) { |
| 4076 | result.experimental_message = stripped_line.trim_prefix("@experimental:").strip_edges(); |
| 4077 | } |
| 4078 | continue; |
| 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | result.description += _process_doc_line(doc_line, result.description, space_prefix, state); |
| 4083 | } |
| 4084 | |
| 4085 | return result; |
| 4086 | } |
| 4087 | |
| 4088 | GDScriptParser::ClassDocData GDScriptParser::parse_class_doc_comment(int p_line, bool p_single_line) { |
| 4089 | ERR_FAIL_COND_V(!has_comment(p_line, true), ClassDocData()); |
| 4090 | |
| 4091 | const HashMap<int, GDScriptTokenizer::CommentData> &comments = tokenizer->get_comments(); |
| 4092 | int line = p_line; |
| 4093 | |
| 4094 | if (!p_single_line) { |
| 4095 | while (comments.has(line - 1) && comments[line - 1].new_line && comments[line - 1].comment.begins_with("##")) { |
| 4096 | line--; |
| 4097 | } |
| 4098 | } |
| 4099 | |
| 4100 | max_script_doc_line = MIN(max_script_doc_line, line - 1); |
| 4101 | |
| 4102 | String space_prefix; |
| 4103 | { |
| 4104 | int i = 2; |
| 4105 | for (; i < comments[line].comment.length(); i++) { |
| 4106 | if (comments[line].comment[i] != ' ') { |
| 4107 | break; |
| 4108 | } |
| 4109 | } |
| 4110 | space_prefix = String(" ").repeat(i - 2); |
| 4111 | } |
| 4112 | |
| 4113 | DocLineState state = DOC_LINE_NORMAL; |
| 4114 | bool is_in_brief = true; |
| 4115 | ClassDocData result; |
| 4116 | |
| 4117 | while (line <= p_line) { |
| 4118 | String doc_line = comments[line].comment.trim_prefix("##"); |
| 4119 | line++; |
| 4120 | |
| 4121 | if (state == DOC_LINE_NORMAL) { |
| 4122 | String stripped_line = doc_line.strip_edges(); |
| 4123 | |
| 4124 | // A blank line separates the description from the brief. |
| 4125 | if (is_in_brief && !result.brief.is_empty() && stripped_line.is_empty()) { |
| 4126 | is_in_brief = false; |
| 4127 | continue; |
| 4128 | } |
| 4129 | |
| 4130 | if (stripped_line.begins_with("@tutorial")) { |
| 4131 | String title, link; |
| 4132 | |
| 4133 | int begin_scan = String("@tutorial").length(); |
| 4134 | if (begin_scan >= stripped_line.length()) { |
| 4135 | continue; // Invalid syntax. |
| 4136 | } |
| 4137 | |
| 4138 | if (stripped_line[begin_scan] == ':') { // No title. |
| 4139 | // Syntax: ## @tutorial: https://godotengine.org/ // The title argument is optional. |
| 4140 | title = ""; |
| 4141 | link = stripped_line.trim_prefix("@tutorial:").strip_edges(); |
| 4142 | } else { |
| 4143 | /* Syntax: |
| 4144 | * @tutorial ( The Title Here ) : https://the.url/ |
| 4145 | * ^ open ^ close ^ colon ^ url |
| 4146 | */ |
| 4147 | int open_bracket_pos = begin_scan, close_bracket_pos = 0; |
| 4148 | while (open_bracket_pos < stripped_line.length() && (stripped_line[open_bracket_pos] == ' ' || stripped_line[open_bracket_pos] == '\t')) { |
| 4149 | open_bracket_pos++; |
| 4150 | } |
| 4151 | if (open_bracket_pos == stripped_line.length() || stripped_line[open_bracket_pos++] != '(') { |
| 4152 | continue; // Invalid syntax. |
| 4153 | } |
| 4154 | close_bracket_pos = open_bracket_pos; |
| 4155 | while (close_bracket_pos < stripped_line.length() && stripped_line[close_bracket_pos] != ')') { |
| 4156 | close_bracket_pos++; |
| 4157 | } |
| 4158 | if (close_bracket_pos == stripped_line.length()) { |
| 4159 | continue; // Invalid syntax. |
| 4160 | } |
| 4161 | |
| 4162 | int colon_pos = close_bracket_pos + 1; |
| 4163 | while (colon_pos < stripped_line.length() && (stripped_line[colon_pos] == ' ' || stripped_line[colon_pos] == '\t')) { |
| 4164 | colon_pos++; |
| 4165 | } |
| 4166 | if (colon_pos == stripped_line.length() || stripped_line[colon_pos++] != ':') { |
| 4167 | continue; // Invalid syntax. |
| 4168 | } |
| 4169 | |
| 4170 | title = stripped_line.substr(open_bracket_pos, close_bracket_pos - open_bracket_pos).strip_edges(); |
| 4171 | link = stripped_line.substr(colon_pos).strip_edges(); |
| 4172 | } |
| 4173 | |
| 4174 | result.tutorials.append(Pair<String, String>(title, link)); |
| 4175 | continue; |
| 4176 | } else if (stripped_line == "@deprecated" || stripped_line.begins_with("@deprecated:")) { |
| 4177 | result.is_deprecated = true; |
| 4178 | if (stripped_line.begins_with("@deprecated:")) { |
| 4179 | result.deprecated_message = stripped_line.trim_prefix("@deprecated:").strip_edges(); |
| 4180 | } |
| 4181 | continue; |
| 4182 | } else if (stripped_line == "@experimental" || stripped_line.begins_with("@experimental:")) { |
| 4183 | result.is_experimental = true; |
| 4184 | if (stripped_line.begins_with("@experimental:")) { |
| 4185 | result.experimental_message = stripped_line.trim_prefix("@experimental:").strip_edges(); |
| 4186 | } |
| 4187 | continue; |
| 4188 | } |
| 4189 | } |
| 4190 | |
| 4191 | if (is_in_brief) { |
| 4192 | result.brief += _process_doc_line(doc_line, result.brief, space_prefix, state); |
| 4193 | } else { |
| 4194 | result.description += _process_doc_line(doc_line, result.description, space_prefix, state); |
| 4195 | } |
| 4196 | } |
| 4197 | |
| 4198 | return result; |
| 4199 | } |
| 4200 | #endif // TOOLS_ENABLED |
| 4201 | |
| 4202 | GDScriptParser::ParseRule *GDScriptParser::get_rule(GDScriptTokenizer::Token::Type p_token_type) { |
| 4203 | // Function table for expression parsing. |
| 4204 | // clang-format destroys the alignment here, so turn off for the table. |
| 4205 | /* clang-format off */ |
| 4206 | static ParseRule rules[] = { |
| 4207 | // PREFIX INFIX PRECEDENCE (for infix) |
| 4208 | { nullptr, nullptr, PREC_NONE }, // EMPTY, |
| 4209 | // Basic |
| 4210 | { nullptr, nullptr, PREC_NONE }, // ANNOTATION, |
| 4211 | { &GDScriptParser::parse_identifier, nullptr, PREC_NONE }, // IDENTIFIER, |
| 4212 | { &GDScriptParser::parse_literal, nullptr, PREC_NONE }, // LITERAL, |
| 4213 | // Comparison |
| 4214 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // LESS, |
| 4215 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // LESS_EQUAL, |
| 4216 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // GREATER, |
| 4217 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // GREATER_EQUAL, |
| 4218 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // EQUAL_EQUAL, |
| 4219 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_COMPARISON }, // BANG_EQUAL, |
| 4220 | // Logical |
| 4221 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_AND }, // AND, |
| 4222 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_OR }, // OR, |
| 4223 | { &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_not_in_operator, PREC_CONTENT_TEST }, // NOT, |
| 4224 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_AND }, // AMPERSAND_AMPERSAND, |
| 4225 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_LOGIC_OR }, // PIPE_PIPE, |
| 4226 | { &GDScriptParser::parse_unary_operator, nullptr, PREC_NONE }, // BANG, |
| 4227 | // Bitwise |
| 4228 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_AND }, // AMPERSAND, |
| 4229 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_OR }, // PIPE, |
| 4230 | { &GDScriptParser::parse_unary_operator, nullptr, PREC_NONE }, // TILDE, |
| 4231 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_XOR }, // CARET, |
| 4232 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_SHIFT }, // LESS_LESS, |
| 4233 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_BIT_SHIFT }, // GREATER_GREATER, |
| 4234 | // Math |
| 4235 | { &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_operator, PREC_ADDITION_SUBTRACTION }, // PLUS, |
| 4236 | { &GDScriptParser::parse_unary_operator, &GDScriptParser::parse_binary_operator, PREC_ADDITION_SUBTRACTION }, // MINUS, |
| 4237 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // STAR, |
| 4238 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_POWER }, // STAR_STAR, |
| 4239 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // SLASH, |
| 4240 | { &GDScriptParser::parse_get_node, &GDScriptParser::parse_binary_operator, PREC_FACTOR }, // PERCENT, |
| 4241 | // Assignment |
| 4242 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // EQUAL, |
| 4243 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PLUS_EQUAL, |
| 4244 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // MINUS_EQUAL, |
| 4245 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // STAR_EQUAL, |
| 4246 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // STAR_STAR_EQUAL, |
| 4247 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // SLASH_EQUAL, |
| 4248 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PERCENT_EQUAL, |
| 4249 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // LESS_LESS_EQUAL, |
| 4250 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // GREATER_GREATER_EQUAL, |
| 4251 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // AMPERSAND_EQUAL, |
| 4252 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // PIPE_EQUAL, |
| 4253 | { nullptr, &GDScriptParser::parse_assignment, PREC_ASSIGNMENT }, // CARET_EQUAL, |
| 4254 | // Control flow |
| 4255 | { nullptr, &GDScriptParser::parse_ternary_operator, PREC_TERNARY }, // IF, |
| 4256 | { nullptr, nullptr, PREC_NONE }, // ELIF, |
| 4257 | { nullptr, nullptr, PREC_NONE }, // ELSE, |
| 4258 | { nullptr, nullptr, PREC_NONE }, // FOR, |
| 4259 | { nullptr, nullptr, PREC_NONE }, // WHILE, |
| 4260 | { nullptr, nullptr, PREC_NONE }, // BREAK, |
| 4261 | { nullptr, nullptr, PREC_NONE }, // CONTINUE, |
| 4262 | { nullptr, nullptr, PREC_NONE }, // PASS, |
| 4263 | { nullptr, nullptr, PREC_NONE }, // RETURN, |
| 4264 | { nullptr, nullptr, PREC_NONE }, // MATCH, |
| 4265 | { nullptr, nullptr, PREC_NONE }, // WHEN, |
| 4266 | // Keywords |
| 4267 | { nullptr, &GDScriptParser::parse_cast, PREC_CAST }, // AS, |
| 4268 | { nullptr, nullptr, PREC_NONE }, // ASSERT, |
| 4269 | { &GDScriptParser::parse_await, nullptr, PREC_NONE }, // AWAIT, |
| 4270 | { nullptr, nullptr, PREC_NONE }, // BREAKPOINT, |
| 4271 | { nullptr, nullptr, PREC_NONE }, // CLASS, |
| 4272 | { nullptr, nullptr, PREC_NONE }, // CLASS_NAME, |
| 4273 | { nullptr, nullptr, PREC_NONE }, // TK_CONST, |
| 4274 | { nullptr, nullptr, PREC_NONE }, // ENUM, |
| 4275 | { nullptr, nullptr, PREC_NONE }, // EXTENDS, |
| 4276 | { &GDScriptParser::parse_lambda, nullptr, PREC_NONE }, // FUNC, |
| 4277 | { nullptr, &GDScriptParser::parse_binary_operator, PREC_CONTENT_TEST }, // TK_IN, |
| 4278 | { nullptr, &GDScriptParser::parse_type_test, PREC_TYPE_TEST }, // IS, |
| 4279 | { nullptr, nullptr, PREC_NONE }, // NAMESPACE, |
| 4280 | { &GDScriptParser::parse_preload, nullptr, PREC_NONE }, // PRELOAD, |
| 4281 | { &GDScriptParser::parse_self, nullptr, PREC_NONE }, // SELF, |
| 4282 | { nullptr, nullptr, PREC_NONE }, // SIGNAL, |
| 4283 | { nullptr, nullptr, PREC_NONE }, // STATIC, |
| 4284 | { &GDScriptParser::parse_call, nullptr, PREC_NONE }, // SUPER, |
| 4285 | { nullptr, nullptr, PREC_NONE }, // TRAIT, |
| 4286 | { nullptr, nullptr, PREC_NONE }, // VAR, |
| 4287 | { nullptr, nullptr, PREC_NONE }, // TK_VOID, |
| 4288 | { &GDScriptParser::parse_yield, nullptr, PREC_NONE }, // YIELD, |
| 4289 | // Punctuation |
| 4290 | { &GDScriptParser::parse_array, &GDScriptParser::parse_subscript, PREC_SUBSCRIPT }, // BRACKET_OPEN, |
| 4291 | { nullptr, nullptr, PREC_NONE }, // BRACKET_CLOSE, |
| 4292 | { &GDScriptParser::parse_dictionary, nullptr, PREC_NONE }, // BRACE_OPEN, |
| 4293 | { nullptr, nullptr, PREC_NONE }, // BRACE_CLOSE, |
| 4294 | { &GDScriptParser::parse_grouping, &GDScriptParser::parse_call, PREC_CALL }, // PARENTHESIS_OPEN, |
| 4295 | { nullptr, nullptr, PREC_NONE }, // PARENTHESIS_CLOSE, |
| 4296 | { nullptr, nullptr, PREC_NONE }, // COMMA, |
| 4297 | { nullptr, nullptr, PREC_NONE }, // SEMICOLON, |
| 4298 | { nullptr, &GDScriptParser::parse_attribute, PREC_ATTRIBUTE }, // PERIOD, |
| 4299 | { nullptr, nullptr, PREC_NONE }, // PERIOD_PERIOD, |
| 4300 | { nullptr, nullptr, PREC_NONE }, // PERIOD_PERIOD_PERIOD, |
| 4301 | { nullptr, nullptr, PREC_NONE }, // COLON, |
| 4302 | { &GDScriptParser::parse_get_node, nullptr, PREC_NONE }, // DOLLAR, |
| 4303 | { nullptr, nullptr, PREC_NONE }, // FORWARD_ARROW, |
| 4304 | { nullptr, nullptr, PREC_NONE }, // UNDERSCORE, |
| 4305 | // Whitespace |
| 4306 | { nullptr, nullptr, PREC_NONE }, // NEWLINE, |
| 4307 | { nullptr, nullptr, PREC_NONE }, // INDENT, |
| 4308 | { nullptr, nullptr, PREC_NONE }, // DEDENT, |
| 4309 | // Constants |
| 4310 | { &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_PI, |
| 4311 | { &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_TAU, |
| 4312 | { &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_INF, |
| 4313 | { &GDScriptParser::parse_builtin_constant, nullptr, PREC_NONE }, // CONST_NAN, |
| 4314 | // Error message improvement |
| 4315 | { nullptr, nullptr, PREC_NONE }, // VCS_CONFLICT_MARKER, |
| 4316 | { nullptr, nullptr, PREC_NONE }, // BACKTICK, |
| 4317 | { nullptr, &GDScriptParser::parse_invalid_token, PREC_CAST }, // QUESTION_MARK, |
| 4318 | // Special |
| 4319 | { nullptr, nullptr, PREC_NONE }, // ERROR, |
| 4320 | { nullptr, nullptr, PREC_NONE }, // TK_EOF, |
| 4321 | }; |
| 4322 | /* clang-format on */ |
| 4323 | // Avoid desync. |
| 4324 | static_assert(std_size(rules) == GDScriptTokenizer::Token::TK_MAX, "Amount of parse rules don't match the amount of token types."); |
| 4325 | |
| 4326 | // Let's assume this is never invalid, since nothing generates a TK_MAX. |
| 4327 | return &rules[p_token_type]; |
| 4328 | } |
| 4329 | |
| 4330 | bool GDScriptParser::SuiteNode::has_local(const StringName &p_name) const { |
| 4331 | if (locals_indices.has(p_name)) { |
| 4332 | return true; |
| 4333 | } |
| 4334 | if (parent_block != nullptr) { |
| 4335 | return parent_block->has_local(p_name); |
| 4336 | } |
| 4337 | return false; |
| 4338 | } |
| 4339 | |
| 4340 | const GDScriptParser::SuiteNode::Local &GDScriptParser::SuiteNode::get_local(const StringName &p_name) const { |
| 4341 | if (locals_indices.has(p_name)) { |
| 4342 | return locals[locals_indices[p_name]]; |
| 4343 | } |
| 4344 | if (parent_block != nullptr) { |
| 4345 | return parent_block->get_local(p_name); |
| 4346 | } |
| 4347 | return empty; |
| 4348 | } |
| 4349 | |
| 4350 | bool GDScriptParser::AnnotationNode::apply(GDScriptParser *p_this, Node *p_target, ClassNode *p_class) { |
| 4351 | if (is_applied) { |
| 4352 | return true; |
| 4353 | } |
| 4354 | is_applied = true; |
| 4355 | return (p_this->*(p_this->valid_annotations[name].apply))(this, p_target, p_class); |
| 4356 | } |
| 4357 | |
| 4358 | bool GDScriptParser::AnnotationNode::applies_to(uint32_t p_target_kinds) const { |
| 4359 | return (info->target_kind & p_target_kinds) > 0; |
| 4360 | } |
| 4361 | |
| 4362 | bool GDScriptParser::validate_annotation_arguments(AnnotationNode *p_annotation) { |
| 4363 | ERR_FAIL_COND_V_MSG(!valid_annotations.has(p_annotation->name), false, vformat(R"(Annotation "%s" not found to validate.)", p_annotation->name)); |
| 4364 | |
| 4365 | const MethodInfo &info = valid_annotations[p_annotation->name].info; |
| 4366 | |
| 4367 | if (((info.flags & METHOD_FLAG_VARARG) == 0) && p_annotation->arguments.size() > info.arguments.size()) { |
| 4368 | push_error(vformat(R"(Annotation "%s" requires at most %d arguments, but %d were given.)", p_annotation->name, info.arguments.size(), p_annotation->arguments.size())); |
| 4369 | return false; |
| 4370 | } |
| 4371 | |
| 4372 | if (p_annotation->arguments.size() < info.arguments.size() - info.default_arguments.size()) { |
| 4373 | push_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())); |
| 4374 | return false; |
| 4375 | } |
| 4376 | |
| 4377 | // Some annotations need to be resolved and applied in the parser. |
| 4378 | if (p_annotation->name == SNAME("@icon") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) { |
| 4379 | for (int i = 0; i < p_annotation->arguments.size(); i++) { |
| 4380 | ExpressionNode *argument = p_annotation->arguments[i]; |
| 4381 | |
| 4382 | if (argument->type != Node::LITERAL) { |
| 4383 | push_error(vformat(R"(Argument %d of annotation "%s" must be a string literal.)", i + 1, p_annotation->name), argument); |
| 4384 | return false; |
| 4385 | } |
| 4386 | |
| 4387 | Variant value = static_cast<LiteralNode *>(argument)->value; |
| 4388 | |
| 4389 | if (value.get_type() != Variant::STRING) { |
| 4390 | push_error(vformat(R"(Argument %d of annotation "%s" must be a string literal.)", i + 1, p_annotation->name), argument); |
| 4391 | return false; |
| 4392 | } |
| 4393 | |
| 4394 | p_annotation->resolved_arguments.push_back(value); |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | // For other annotations, see `GDScriptAnalyzer::resolve_annotation()`. |
| 4399 | |
| 4400 | return true; |
| 4401 | } |
| 4402 | |
| 4403 | bool GDScriptParser::tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4404 | #ifdef DEBUG_ENABLED |
| 4405 | if (_is_tool) { |
| 4406 | push_error(R"("@tool" annotation can only be used once.)", p_annotation); |
| 4407 | return false; |
| 4408 | } |
| 4409 | #endif // DEBUG_ENABLED |
| 4410 | _is_tool = true; |
| 4411 | return true; |
| 4412 | } |
| 4413 | |
| 4414 | bool GDScriptParser::icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4415 | ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, R"("@icon" annotation can only be applied to classes.)"); |
| 4416 | ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false); |
| 4417 | |
| 4418 | ClassNode *class_node = static_cast<ClassNode *>(p_target); |
| 4419 | String path = p_annotation->resolved_arguments[0]; |
| 4420 | |
| 4421 | #ifdef DEBUG_ENABLED |
| 4422 | if (!class_node->icon_path.is_empty()) { |
| 4423 | push_error(R"("@icon" annotation can only be used once.)", p_annotation); |
| 4424 | return false; |
| 4425 | } |
| 4426 | if (path.is_empty()) { |
| 4427 | push_error(R"("@icon" annotation argument must contain the path to the icon.)", p_annotation->arguments[0]); |
| 4428 | return false; |
| 4429 | } |
| 4430 | #endif // DEBUG_ENABLED |
| 4431 | |
| 4432 | class_node->icon_path = path; |
| 4433 | |
| 4434 | if (path.is_empty() || path.is_absolute_path()) { |
| 4435 | class_node->simplified_icon_path = path.simplify_path(); |
| 4436 | } else if (path.is_relative_path()) { |
| 4437 | class_node->simplified_icon_path = script_path.get_base_dir().path_join(path).simplify_path(); |
| 4438 | } else { |
| 4439 | class_node->simplified_icon_path = path; |
| 4440 | } |
| 4441 | |
| 4442 | return true; |
| 4443 | } |
| 4444 | |
| 4445 | bool GDScriptParser::static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4446 | ERR_FAIL_COND_V_MSG(p_target->type != Node::CLASS, false, vformat(R"("%s" annotation can only be applied to classes.)", p_annotation->name)); |
| 4447 | ClassNode *class_node = static_cast<ClassNode *>(p_target); |
| 4448 | if (class_node->annotated_static_unload) { |
| 4449 | push_error(vformat(R"("%s" annotation can only be used once per script.)", p_annotation->name), p_annotation); |
| 4450 | return false; |
| 4451 | } |
| 4452 | class_node->annotated_static_unload = true; |
| 4453 | return true; |
| 4454 | } |
| 4455 | |
| 4456 | bool GDScriptParser::abstract_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4457 | // NOTE: Use `p_target`, **not** `p_class`, because when `p_target` is a class then `p_class` refers to the outer class. |
| 4458 | if (p_target->type == Node::CLASS) { |
| 4459 | ClassNode *class_node = static_cast<ClassNode *>(p_target); |
| 4460 | if (class_node->is_abstract) { |
| 4461 | push_error(R"("@abstract" annotation can only be used once per class.)", p_annotation); |
| 4462 | return false; |
| 4463 | } |
| 4464 | class_node->is_abstract = true; |
| 4465 | return true; |
| 4466 | } |
| 4467 | if (p_target->type == Node::FUNCTION) { |
| 4468 | FunctionNode *function_node = static_cast<FunctionNode *>(p_target); |
| 4469 | if (function_node->is_static) { |
| 4470 | push_error(R"("@abstract" annotation cannot be applied to static functions.)", p_annotation); |
| 4471 | return false; |
| 4472 | } |
| 4473 | if (function_node->is_abstract) { |
| 4474 | push_error(R"("@abstract" annotation can only be used once per function.)", p_annotation); |
| 4475 | return false; |
| 4476 | } |
| 4477 | function_node->is_abstract = true; |
| 4478 | return true; |
| 4479 | } |
| 4480 | ERR_FAIL_V_MSG(false, R"("@abstract" annotation can only be applied to classes and functions.)"); |
| 4481 | } |
| 4482 | |
| 4483 | bool GDScriptParser::onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4484 | ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, R"("@onready" annotation can only be applied to class variables.)"); |
| 4485 | |
| 4486 | if (current_class && !ClassDB::is_parent_class(current_class->get_datatype().native_type, SNAME("Node"))) { |
| 4487 | push_error(R"("@onready" can only be used in classes that inherit "Node".)", p_annotation); |
| 4488 | return false; |
| 4489 | } |
| 4490 | |
| 4491 | VariableNode *variable = static_cast<VariableNode *>(p_target); |
| 4492 | if (variable->is_static) { |
| 4493 | push_error(R"("@onready" annotation cannot be applied to a static variable.)", p_annotation); |
| 4494 | return false; |
| 4495 | } |
| 4496 | if (variable->onready) { |
| 4497 | push_error(R"("@onready" annotation can only be used once per variable.)", p_annotation); |
| 4498 | return false; |
| 4499 | } |
| 4500 | variable->onready = true; |
| 4501 | current_class->onready_used = true; |
| 4502 | return true; |
| 4503 | } |
| 4504 | |
| 4505 | static String _get_annotation_error_string(const StringName &p_annotation_name, const Vector<Variant::Type> &p_expected_types, const GDScriptParser::DataType &p_provided_type) { |
| 4506 | Vector<String> types; |
| 4507 | for (int i = 0; i < p_expected_types.size(); i++) { |
| 4508 | const Variant::Type &type = p_expected_types[i]; |
| 4509 | types.push_back(Variant::get_type_name(type)); |
| 4510 | types.push_back("Array[" + Variant::get_type_name(type) + "]"); |
| 4511 | switch (type) { |
| 4512 | case Variant::INT: |
| 4513 | types.push_back("PackedByteArray"); |
| 4514 | types.push_back("PackedInt32Array"); |
| 4515 | types.push_back("PackedInt64Array"); |
| 4516 | break; |
| 4517 | case Variant::FLOAT: |
| 4518 | types.push_back("PackedFloat32Array"); |
| 4519 | types.push_back("PackedFloat64Array"); |
| 4520 | break; |
| 4521 | case Variant::STRING: |
| 4522 | types.push_back("PackedStringArray"); |
| 4523 | break; |
| 4524 | case Variant::VECTOR2: |
| 4525 | types.push_back("PackedVector2Array"); |
| 4526 | break; |
| 4527 | case Variant::VECTOR3: |
| 4528 | types.push_back("PackedVector3Array"); |
| 4529 | break; |
| 4530 | case Variant::COLOR: |
| 4531 | types.push_back("PackedColorArray"); |
| 4532 | break; |
| 4533 | case Variant::VECTOR4: |
| 4534 | types.push_back("PackedVector4Array"); |
| 4535 | break; |
| 4536 | default: |
| 4537 | break; |
| 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | String string; |
| 4542 | if (types.size() == 1) { |
| 4543 | string = types[0].quote(); |
| 4544 | } else if (types.size() == 2) { |
| 4545 | string = types[0].quote() + " or " + types[1].quote(); |
| 4546 | } else if (types.size() >= 3) { |
| 4547 | string = types[0].quote(); |
| 4548 | for (int i = 1; i < types.size() - 1; i++) { |
| 4549 | string += ", " + types[i].quote(); |
| 4550 | } |
| 4551 | string += ", or " + types[types.size() - 1].quote(); |
| 4552 | } |
| 4553 | |
| 4554 | return 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()); |
| 4555 | } |
| 4556 | |
| 4557 | static StringName _find_narrowest_native_or_global_class(const GDScriptParser::DataType &p_type) { |
| 4558 | switch (p_type.kind) { |
| 4559 | case GDScriptParser::DataType::NATIVE: { |
| 4560 | if (p_type.is_meta_type) { |
| 4561 | return Object::get_class_static(); // `GDScriptNativeClass` is not an exposed class. |
| 4562 | } |
| 4563 | return p_type.native_type; |
| 4564 | } break; |
| 4565 | case GDScriptParser::DataType::SCRIPT: { |
| 4566 | Ref<Script> script; |
| 4567 | if (p_type.script_type.is_valid()) { |
| 4568 | script = p_type.script_type; |
| 4569 | } else { |
| 4570 | script = ResourceLoader::load(p_type.script_path, SNAME("Script")); |
| 4571 | } |
| 4572 | |
| 4573 | if (p_type.is_meta_type) { |
| 4574 | return script.is_valid() ? script->get_class_name() : Script::get_class_static(); |
| 4575 | } |
| 4576 | if (script.is_null()) { |
| 4577 | return p_type.native_type; |
| 4578 | } |
| 4579 | if (script->get_global_name() != StringName()) { |
| 4580 | return script->get_global_name(); |
| 4581 | } |
| 4582 | |
| 4583 | Ref<Script> base_script = script->get_base_script(); |
| 4584 | if (base_script.is_null()) { |
| 4585 | return script->get_instance_base_type(); |
| 4586 | } |
| 4587 | |
| 4588 | GDScriptParser::DataType base_type; |
| 4589 | base_type.kind = GDScriptParser::DataType::SCRIPT; |
| 4590 | base_type.builtin_type = Variant::OBJECT; |
| 4591 | base_type.native_type = base_script->get_instance_base_type(); |
| 4592 | base_type.script_type = base_script; |
| 4593 | base_type.script_path = base_script->get_path(); |
| 4594 | |
| 4595 | return _find_narrowest_native_or_global_class(base_type); |
| 4596 | } break; |
| 4597 | case GDScriptParser::DataType::CLASS: { |
| 4598 | if (p_type.is_meta_type) { |
| 4599 | return GDScript::get_class_static(); |
| 4600 | } |
| 4601 | if (p_type.class_type == nullptr) { |
| 4602 | return p_type.native_type; |
| 4603 | } |
| 4604 | if (p_type.class_type->get_global_name() != StringName()) { |
| 4605 | return p_type.class_type->get_global_name(); |
| 4606 | } |
| 4607 | return _find_narrowest_native_or_global_class(p_type.class_type->base_type); |
| 4608 | } break; |
| 4609 | default: { |
| 4610 | ERR_FAIL_V(StringName()); |
| 4611 | } break; |
| 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | template <PropertyHint t_hint, Variant::Type t_type> |
| 4616 | bool GDScriptParser::export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4617 | ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); |
| 4618 | ERR_FAIL_NULL_V(p_class, false); |
| 4619 | |
| 4620 | VariableNode *variable = static_cast<VariableNode *>(p_target); |
| 4621 | if (variable->is_static) { |
| 4622 | push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation); |
| 4623 | return false; |
| 4624 | } |
| 4625 | if (variable->exported) { |
| 4626 | push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation); |
| 4627 | return false; |
| 4628 | } |
| 4629 | |
| 4630 | variable->exported = true; |
| 4631 | |
| 4632 | variable->export_info.type = t_type; |
| 4633 | variable->export_info.hint = t_hint; |
| 4634 | |
| 4635 | String hint_string; |
| 4636 | for (int i = 0; i < p_annotation->resolved_arguments.size(); i++) { |
| 4637 | String arg_string = String(p_annotation->resolved_arguments[i]); |
| 4638 | |
| 4639 | if (p_annotation->name != SNAME("@export_placeholder")) { |
| 4640 | if (arg_string.is_empty()) { |
| 4641 | push_error(vformat(R"(Argument %d of annotation "%s" is empty.)", i + 1, p_annotation->name), p_annotation->arguments[i]); |
| 4642 | return false; |
| 4643 | } |
| 4644 | if (arg_string.contains_char(',')) { |
| 4645 | push_error(vformat(R"(Argument %d of annotation "%s" contains a comma. Use separate arguments instead.)", i + 1, p_annotation->name), p_annotation->arguments[i]); |
| 4646 | return false; |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | // WARNING: Do not merge with the previous `if` because there `!=`, not `==`! |
| 4651 | if (p_annotation->name == SNAME("@export_flags")) { |
| 4652 | const int64_t max_flags = 32; |
| 4653 | Vector<String> t = arg_string.split(":", true, 1); |
| 4654 | if (t[0].is_empty()) { |
| 4655 | push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Expected flag name.)", i + 1), p_annotation->arguments[i]); |
| 4656 | return false; |
| 4657 | } |
| 4658 | if (t.size() == 2) { |
| 4659 | if (t[1].is_empty()) { |
| 4660 | push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": Expected flag value.)", i + 1), p_annotation->arguments[i]); |
| 4661 | return false; |
| 4662 | } |
| 4663 | if (!t[1].is_valid_int()) { |
| 4664 | push_error(vformat(R"(Invalid argument %d of annotation "@export_flags": The flag value must be a valid integer.)", i + 1), p_annotation->arguments[i]); |
| 4665 | return false; |
| 4666 | } |
| 4667 | int64_t value = t[1].to_int(); |
| 4668 | if (value < 1 || value >= (1LL << max_flags)) { |
| 4669 | push_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]); |
| 4670 | return false; |
| 4671 | } |
| 4672 | } else if (i >= max_flags) { |
| 4673 | push_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]); |
| 4674 | return false; |
| 4675 | } |
| 4676 | } else if (p_annotation->name == SNAME("@export_node_path")) { |
| 4677 | String native_class = arg_string; |
| 4678 | if (ScriptServer::is_global_class(arg_string)) { |
| 4679 | native_class = ScriptServer::get_global_class_native_base(arg_string); |
| 4680 | } |
| 4681 | if (!ClassDB::class_exists(native_class) || !ClassDB::is_class_exposed(native_class)) { |
| 4682 | push_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]); |
| 4683 | return false; |
| 4684 | } else if (!ClassDB::is_parent_class(native_class, SNAME("Node"))) { |
| 4685 | push_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]); |
| 4686 | return false; |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | if (i > 0) { |
| 4691 | hint_string += ","; |
| 4692 | } |
| 4693 | hint_string += arg_string; |
| 4694 | } |
| 4695 | variable->export_info.hint_string = hint_string; |
| 4696 | |
| 4697 | // This is called after the analyzer is done finding the type, so this should be set here. |
| 4698 | DataType export_type = variable->get_datatype(); |
| 4699 | |
| 4700 | // Use initializer type if specified type is `Variant`. |
| 4701 | if (export_type.is_variant() && variable->initializer != nullptr && variable->initializer->datatype.is_set()) { |
| 4702 | export_type = variable->initializer->get_datatype(); |
| 4703 | export_type.type_source = DataType::INFERRED; |
| 4704 | } |
| 4705 | |
| 4706 | const Variant::Type original_export_type_builtin = export_type.builtin_type; |
| 4707 | |
| 4708 | // Process array and packed array annotations on the element type. |
| 4709 | bool is_array = false; |
| 4710 | if (export_type.builtin_type == Variant::ARRAY && export_type.has_container_element_type(0)) { |
| 4711 | is_array = true; |
| 4712 | export_type = export_type.get_container_element_type(0); |
| 4713 | } else if (export_type.is_typed_container_type()) { |
| 4714 | is_array = true; |
| 4715 | export_type = export_type.get_typed_container_type(); |
| 4716 | export_type.type_source = variable->datatype.type_source; |
| 4717 | } |
| 4718 | |
| 4719 | bool is_dict = false; |
| 4720 | if (export_type.builtin_type == Variant::DICTIONARY && export_type.has_container_element_types()) { |
| 4721 | is_dict = true; |
| 4722 | DataType inner_type = export_type.get_container_element_type_or_variant(1); |
| 4723 | export_type = export_type.get_container_element_type_or_variant(0); |
| 4724 | export_type.set_container_element_type(0, inner_type); // Store earlier extracted value within key to separately parse after. |
| 4725 | } |
| 4726 | |
| 4727 | bool use_default_variable_type_check = true; |
| 4728 | |
| 4729 | if (p_annotation->name == SNAME("@export_range")) { |
| 4730 | if (export_type.builtin_type == Variant::INT) { |
| 4731 | variable->export_info.type = Variant::INT; |
| 4732 | } |
| 4733 | } else if (p_annotation->name == SNAME("@export_multiline")) { |
| 4734 | use_default_variable_type_check = false; |
| 4735 | |
| 4736 | if (export_type.builtin_type != Variant::STRING && export_type.builtin_type != Variant::DICTIONARY) { |
| 4737 | Vector<Variant::Type> expected_types = { Variant::STRING, Variant::DICTIONARY }; |
| 4738 | push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation); |
| 4739 | return false; |
| 4740 | } |
| 4741 | |
| 4742 | if (export_type.builtin_type == Variant::DICTIONARY) { |
| 4743 | variable->export_info.type = Variant::DICTIONARY; |
| 4744 | } |
| 4745 | } else if (p_annotation->name == SNAME("@export")) { |
| 4746 | use_default_variable_type_check = false; |
| 4747 | |
| 4748 | if (variable->datatype_specifier == nullptr && variable->initializer == nullptr) { |
| 4749 | push_error(R"(Cannot use simple "@export" annotation with variable without type or initializer, since type can't be inferred.)", p_annotation); |
| 4750 | return false; |
| 4751 | } |
| 4752 | |
| 4753 | if (export_type.has_no_type()) { |
| 4754 | push_error(R"(Cannot use simple "@export" annotation because the type of the initialized value can't be inferred.)", p_annotation); |
| 4755 | return false; |
| 4756 | } |
| 4757 | |
| 4758 | switch (export_type.kind) { |
| 4759 | case GDScriptParser::DataType::BUILTIN: |
| 4760 | variable->export_info.type = export_type.builtin_type; |
| 4761 | variable->export_info.hint = PROPERTY_HINT_NONE; |
| 4762 | variable->export_info.hint_string = String(); |
| 4763 | break; |
| 4764 | case GDScriptParser::DataType::NATIVE: |
| 4765 | case GDScriptParser::DataType::SCRIPT: |
| 4766 | case GDScriptParser::DataType::CLASS: { |
| 4767 | const StringName class_name = _find_narrowest_native_or_global_class(export_type); |
| 4768 | if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) { |
| 4769 | variable->export_info.type = Variant::OBJECT; |
| 4770 | variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE; |
| 4771 | variable->export_info.hint_string = class_name; |
| 4772 | } else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) { |
| 4773 | variable->export_info.type = Variant::OBJECT; |
| 4774 | variable->export_info.hint = PROPERTY_HINT_NODE_TYPE; |
| 4775 | variable->export_info.hint_string = class_name; |
| 4776 | } else { |
| 4777 | push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation); |
| 4778 | return false; |
| 4779 | } |
| 4780 | } break; |
| 4781 | case GDScriptParser::DataType::ENUM: { |
| 4782 | if (export_type.is_meta_type) { |
| 4783 | variable->export_info.type = Variant::DICTIONARY; |
| 4784 | } else { |
| 4785 | variable->export_info.type = Variant::INT; |
| 4786 | variable->export_info.hint = PROPERTY_HINT_ENUM; |
| 4787 | |
| 4788 | String enum_hint_string; |
| 4789 | bool first = true; |
| 4790 | for (const KeyValue<StringName, int64_t> &E : export_type.enum_values) { |
| 4791 | if (!first) { |
| 4792 | enum_hint_string += ","; |
| 4793 | } else { |
| 4794 | first = false; |
| 4795 | } |
| 4796 | enum_hint_string += E.key.operator String().capitalize().xml_escape(); |
| 4797 | enum_hint_string += ":"; |
| 4798 | enum_hint_string += String::num_int64(E.value).xml_escape(); |
| 4799 | } |
| 4800 | |
| 4801 | variable->export_info.hint_string = enum_hint_string; |
| 4802 | variable->export_info.usage |= PROPERTY_USAGE_CLASS_IS_ENUM; |
| 4803 | variable->export_info.class_name = String(export_type.native_type).replace("::", "."); |
| 4804 | } |
| 4805 | } break; |
| 4806 | case GDScriptParser::DataType::VARIANT: { |
| 4807 | if (export_type.is_variant()) { |
| 4808 | variable->export_info.type = Variant::NIL; |
| 4809 | variable->export_info.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; |
| 4810 | } |
| 4811 | } break; |
| 4812 | default: |
| 4813 | push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation); |
| 4814 | return false; |
| 4815 | } |
| 4816 | |
| 4817 | if (variable->export_info.hint == PROPERTY_HINT_NODE_TYPE && !ClassDB::is_parent_class(p_class->base_type.native_type, SNAME("Node"))) { |
| 4818 | push_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); |
| 4819 | return false; |
| 4820 | } |
| 4821 | |
| 4822 | if (is_dict) { |
| 4823 | String key_prefix = itos(variable->export_info.type); |
| 4824 | if (variable->export_info.hint) { |
| 4825 | key_prefix += "/" + itos(variable->export_info.hint); |
| 4826 | } |
| 4827 | key_prefix += ":" + variable->export_info.hint_string; |
| 4828 | |
| 4829 | // Now parse value. |
| 4830 | export_type = export_type.get_container_element_type(0); |
| 4831 | |
| 4832 | if (export_type.is_variant() || export_type.has_no_type()) { |
| 4833 | export_type.kind = GDScriptParser::DataType::BUILTIN; |
| 4834 | } |
| 4835 | switch (export_type.kind) { |
| 4836 | case GDScriptParser::DataType::BUILTIN: |
| 4837 | variable->export_info.type = export_type.builtin_type; |
| 4838 | variable->export_info.hint = PROPERTY_HINT_NONE; |
| 4839 | variable->export_info.hint_string = String(); |
| 4840 | break; |
| 4841 | case GDScriptParser::DataType::NATIVE: |
| 4842 | case GDScriptParser::DataType::SCRIPT: |
| 4843 | case GDScriptParser::DataType::CLASS: { |
| 4844 | const StringName class_name = _find_narrowest_native_or_global_class(export_type); |
| 4845 | if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) { |
| 4846 | variable->export_info.type = Variant::OBJECT; |
| 4847 | variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE; |
| 4848 | variable->export_info.hint_string = class_name; |
| 4849 | } else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) { |
| 4850 | variable->export_info.type = Variant::OBJECT; |
| 4851 | variable->export_info.hint = PROPERTY_HINT_NODE_TYPE; |
| 4852 | variable->export_info.hint_string = class_name; |
| 4853 | } else { |
| 4854 | push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation); |
| 4855 | return false; |
| 4856 | } |
| 4857 | } break; |
| 4858 | case GDScriptParser::DataType::ENUM: { |
| 4859 | if (export_type.is_meta_type) { |
| 4860 | variable->export_info.type = Variant::DICTIONARY; |
| 4861 | } else { |
| 4862 | variable->export_info.type = Variant::INT; |
| 4863 | variable->export_info.hint = PROPERTY_HINT_ENUM; |
| 4864 | |
| 4865 | String enum_hint_string; |
| 4866 | bool first = true; |
| 4867 | for (const KeyValue<StringName, int64_t> &E : export_type.enum_values) { |
| 4868 | if (!first) { |
| 4869 | enum_hint_string += ","; |
| 4870 | } else { |
| 4871 | first = false; |
| 4872 | } |
| 4873 | enum_hint_string += E.key.operator String().capitalize().xml_escape(); |
| 4874 | enum_hint_string += ":"; |
| 4875 | enum_hint_string += String::num_int64(E.value).xml_escape(); |
| 4876 | } |
| 4877 | |
| 4878 | variable->export_info.hint_string = enum_hint_string; |
| 4879 | variable->export_info.usage |= PROPERTY_USAGE_CLASS_IS_ENUM; |
| 4880 | variable->export_info.class_name = String(export_type.native_type).replace("::", "."); |
| 4881 | } |
| 4882 | } break; |
| 4883 | default: |
| 4884 | push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation); |
| 4885 | return false; |
| 4886 | } |
| 4887 | |
| 4888 | if (variable->export_info.hint == PROPERTY_HINT_NODE_TYPE && !ClassDB::is_parent_class(p_class->base_type.native_type, SNAME("Node"))) { |
| 4889 | push_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); |
| 4890 | return false; |
| 4891 | } |
| 4892 | |
| 4893 | String value_prefix = itos(variable->export_info.type); |
| 4894 | if (variable->export_info.hint) { |
| 4895 | value_prefix += "/" + itos(variable->export_info.hint); |
| 4896 | } |
| 4897 | value_prefix += ":" + variable->export_info.hint_string; |
| 4898 | |
| 4899 | variable->export_info.type = Variant::DICTIONARY; |
| 4900 | variable->export_info.hint = PROPERTY_HINT_TYPE_STRING; |
| 4901 | variable->export_info.hint_string = key_prefix + ";" + value_prefix; |
| 4902 | variable->export_info.usage = PROPERTY_USAGE_DEFAULT; |
| 4903 | variable->export_info.class_name = StringName(); |
| 4904 | } |
| 4905 | } else if (p_annotation->name == SNAME("@export_enum")) { |
| 4906 | use_default_variable_type_check = false; |
| 4907 | |
| 4908 | Variant::Type enum_type = Variant::INT; |
| 4909 | |
| 4910 | if (export_type.kind == DataType::BUILTIN && export_type.builtin_type == Variant::STRING) { |
| 4911 | enum_type = Variant::STRING; |
| 4912 | } |
| 4913 | |
| 4914 | variable->export_info.type = enum_type; |
| 4915 | |
| 4916 | if (!export_type.is_variant() && (export_type.kind != DataType::BUILTIN || export_type.builtin_type != enum_type)) { |
| 4917 | Vector<Variant::Type> expected_types = { Variant::INT, Variant::STRING }; |
| 4918 | push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation); |
| 4919 | return false; |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | if (use_default_variable_type_check) { |
| 4924 | // Validate variable type with export. |
| 4925 | if (!export_type.is_variant() && (export_type.kind != DataType::BUILTIN || export_type.builtin_type != t_type)) { |
| 4926 | // Allow float/int conversion. |
| 4927 | if ((t_type != Variant::FLOAT || export_type.builtin_type != Variant::INT) && (t_type != Variant::INT || export_type.builtin_type != Variant::FLOAT)) { |
| 4928 | Vector<Variant::Type> expected_types = { t_type }; |
| 4929 | push_error(_get_annotation_error_string(p_annotation->name, expected_types, variable->get_datatype()), p_annotation); |
| 4930 | return false; |
| 4931 | } |
| 4932 | } |
| 4933 | } |
| 4934 | |
| 4935 | if (is_array) { |
| 4936 | String hint_prefix = itos(variable->export_info.type); |
| 4937 | if (variable->export_info.hint) { |
| 4938 | hint_prefix += "/" + itos(variable->export_info.hint); |
| 4939 | } |
| 4940 | variable->export_info.type = original_export_type_builtin; |
| 4941 | variable->export_info.hint = PROPERTY_HINT_TYPE_STRING; |
| 4942 | variable->export_info.hint_string = hint_prefix + ":" + variable->export_info.hint_string; |
| 4943 | variable->export_info.usage = PROPERTY_USAGE_DEFAULT; |
| 4944 | variable->export_info.class_name = StringName(); |
| 4945 | } |
| 4946 | |
| 4947 | return true; |
| 4948 | } |
| 4949 | |
| 4950 | // For `@export_storage` and `@export_custom`, there is no need to check the variable type, argument values, |
| 4951 | // or handle array exports in a special way, so they are implemented as separate methods. |
| 4952 | |
| 4953 | bool GDScriptParser::export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4954 | ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); |
| 4955 | |
| 4956 | VariableNode *variable = static_cast<VariableNode *>(p_target); |
| 4957 | if (variable->is_static) { |
| 4958 | push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation); |
| 4959 | return false; |
| 4960 | } |
| 4961 | if (variable->exported) { |
| 4962 | push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation); |
| 4963 | return false; |
| 4964 | } |
| 4965 | |
| 4966 | variable->exported = true; |
| 4967 | |
| 4968 | // Save the info because the compiler uses export info for overwriting member info. |
| 4969 | variable->export_info = variable->get_datatype().to_property_info(variable->identifier->name); |
| 4970 | variable->export_info.usage |= PROPERTY_USAGE_STORAGE; |
| 4971 | |
| 4972 | return true; |
| 4973 | } |
| 4974 | |
| 4975 | bool GDScriptParser::export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 4976 | ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); |
| 4977 | ERR_FAIL_COND_V_MSG(p_annotation->resolved_arguments.size() < 2, false, R"(Annotation "@export_custom" requires 2 arguments.)"); |
| 4978 | |
| 4979 | VariableNode *variable = static_cast<VariableNode *>(p_target); |
| 4980 | if (variable->is_static) { |
| 4981 | push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation); |
| 4982 | return false; |
| 4983 | } |
| 4984 | if (variable->exported) { |
| 4985 | push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation); |
| 4986 | return false; |
| 4987 | } |
| 4988 | |
| 4989 | variable->exported = true; |
| 4990 | |
| 4991 | DataType export_type = variable->get_datatype(); |
| 4992 | |
| 4993 | variable->export_info.type = export_type.builtin_type; |
| 4994 | variable->export_info.hint = static_cast<PropertyHint>(p_annotation->resolved_arguments[0].operator int64_t()); |
| 4995 | variable->export_info.hint_string = p_annotation->resolved_arguments[1]; |
| 4996 | |
| 4997 | if (p_annotation->resolved_arguments.size() >= 3) { |
| 4998 | variable->export_info.usage = p_annotation->resolved_arguments[2].operator int64_t(); |
| 4999 | } |
| 5000 | return true; |
| 5001 | } |
| 5002 | |
| 5003 | bool GDScriptParser::export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 5004 | #ifdef TOOLS_ENABLED |
| 5005 | ERR_FAIL_COND_V_MSG(p_target->type != Node::VARIABLE, false, vformat(R"("%s" annotation can only be applied to variables.)", p_annotation->name)); |
| 5006 | ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false); |
| 5007 | |
| 5008 | if (!is_tool()) { |
| 5009 | push_error(R"(Tool buttons can only be used in tool scripts (add "@tool" to the top of the script).)", p_annotation); |
| 5010 | return false; |
| 5011 | } |
| 5012 | |
| 5013 | VariableNode *variable = static_cast<VariableNode *>(p_target); |
| 5014 | |
| 5015 | if (variable->is_static) { |
| 5016 | push_error(vformat(R"(Annotation "%s" cannot be applied to a static variable.)", p_annotation->name), p_annotation); |
| 5017 | return false; |
| 5018 | } |
| 5019 | if (variable->exported) { |
| 5020 | push_error(vformat(R"(Annotation "%s" cannot be used with another "@export" annotation.)", p_annotation->name), p_annotation); |
| 5021 | return false; |
| 5022 | } |
| 5023 | |
| 5024 | const DataType variable_type = variable->get_datatype(); |
| 5025 | if (!variable_type.is_variant() && variable_type.is_hard_type()) { |
| 5026 | if (variable_type.kind != DataType::BUILTIN || variable_type.builtin_type != Variant::CALLABLE) { |
| 5027 | push_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); |
| 5028 | return false; |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | variable->exported = true; |
| 5033 | |
| 5034 | // Build the hint string (format: `<text>[,<icon>]`). |
| 5035 | String hint_string = p_annotation->resolved_arguments[0].operator String(); // Button text. |
| 5036 | if (p_annotation->resolved_arguments.size() > 1) { |
| 5037 | hint_string += "," + p_annotation->resolved_arguments[1].operator String(); // Button icon. |
| 5038 | } |
| 5039 | |
| 5040 | variable->export_info.type = Variant::CALLABLE; |
| 5041 | variable->export_info.hint = PROPERTY_HINT_TOOL_BUTTON; |
| 5042 | variable->export_info.hint_string = hint_string; |
| 5043 | variable->export_info.usage = PROPERTY_USAGE_EDITOR; |
| 5044 | #endif // TOOLS_ENABLED |
| 5045 | |
| 5046 | return true; // Only available in editor. |
| 5047 | } |
| 5048 | |
| 5049 | template <PropertyUsageFlags t_usage> |
| 5050 | bool GDScriptParser::export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 5051 | ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false); |
| 5052 | |
| 5053 | p_annotation->export_info.name = p_annotation->resolved_arguments[0]; |
| 5054 | |
| 5055 | switch (t_usage) { |
| 5056 | case PROPERTY_USAGE_CATEGORY: { |
| 5057 | p_annotation->export_info.usage = t_usage; |
| 5058 | } break; |
| 5059 | |
| 5060 | case PROPERTY_USAGE_GROUP: { |
| 5061 | p_annotation->export_info.usage = t_usage; |
| 5062 | if (p_annotation->resolved_arguments.size() == 2) { |
| 5063 | p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1]; |
| 5064 | } |
| 5065 | } break; |
| 5066 | |
| 5067 | case PROPERTY_USAGE_SUBGROUP: { |
| 5068 | p_annotation->export_info.usage = t_usage; |
| 5069 | if (p_annotation->resolved_arguments.size() == 2) { |
| 5070 | p_annotation->export_info.hint_string = p_annotation->resolved_arguments[1]; |
| 5071 | } |
| 5072 | } break; |
| 5073 | } |
| 5074 | |
| 5075 | return true; |
| 5076 | } |
| 5077 | |
| 5078 | bool GDScriptParser::warning_ignore_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 5079 | #ifdef DEBUG_ENABLED |
| 5080 | bool has_error = false; |
| 5081 | for (const Variant &warning_name : p_annotation->resolved_arguments) { |
| 5082 | GDScriptWarning::Code warning_code = GDScriptWarning::get_code_from_name(String(warning_name).to_upper()); |
| 5083 | if (warning_code == GDScriptWarning::WARNING_MAX) { |
| 5084 | push_error(vformat(R"(Invalid warning name: "%s".)", warning_name), p_annotation); |
| 5085 | has_error = true; |
| 5086 | } else { |
| 5087 | int start_line = p_annotation->start_line; |
| 5088 | int end_line = p_target->end_line; |
| 5089 | |
| 5090 | switch (p_target->type) { |
| 5091 | #define SIMPLE_CASE(m_type, m_class, m_property) \ |
| 5092 | case m_type: { \ |
| 5093 | m_class *node = static_cast<m_class *>(p_target); \ |
| 5094 | if (node->m_property == nullptr) { \ |
| 5095 | end_line = node->start_line; \ |
| 5096 | } else { \ |
| 5097 | end_line = node->m_property->end_line; \ |
| 5098 | } \ |
| 5099 | } break; |
| 5100 | |
| 5101 | // Can contain properties (set/get). |
| 5102 | SIMPLE_CASE(Node::VARIABLE, VariableNode, initializer) |
| 5103 | |
| 5104 | // Contain bodies. |
| 5105 | SIMPLE_CASE(Node::FOR, ForNode, list) |
| 5106 | SIMPLE_CASE(Node::IF, IfNode, condition) |
| 5107 | SIMPLE_CASE(Node::MATCH, MatchNode, test) |
| 5108 | SIMPLE_CASE(Node::WHILE, WhileNode, condition) |
| 5109 | #undef SIMPLE_CASE |
| 5110 | |
| 5111 | case Node::CLASS: { |
| 5112 | end_line = p_target->start_line; |
| 5113 | for (const AnnotationNode *annotation : p_target->annotations) { |
| 5114 | start_line = MIN(start_line, annotation->start_line); |
| 5115 | end_line = MAX(end_line, annotation->end_line); |
| 5116 | } |
| 5117 | } break; |
| 5118 | |
| 5119 | case Node::FUNCTION: { |
| 5120 | FunctionNode *function = static_cast<FunctionNode *>(p_target); |
| 5121 | end_line = function->start_line; |
| 5122 | for (int i = 0; i < function->parameters.size(); i++) { |
| 5123 | end_line = MAX(end_line, function->parameters[i]->end_line); |
| 5124 | if (function->parameters[i]->initializer != nullptr) { |
| 5125 | end_line = MAX(end_line, function->parameters[i]->initializer->end_line); |
| 5126 | } |
| 5127 | } |
| 5128 | } break; |
| 5129 | |
| 5130 | case Node::MATCH_BRANCH: { |
| 5131 | MatchBranchNode *branch = static_cast<MatchBranchNode *>(p_target); |
| 5132 | end_line = branch->start_line; |
| 5133 | for (int i = 0; i < branch->patterns.size(); i++) { |
| 5134 | end_line = MAX(end_line, branch->patterns[i]->end_line); |
| 5135 | } |
| 5136 | } break; |
| 5137 | |
| 5138 | default: { |
| 5139 | } break; |
| 5140 | } |
| 5141 | |
| 5142 | end_line = MAX(start_line, end_line); // Prevent infinite loop. |
| 5143 | for (int line = start_line; line <= end_line; line++) { |
| 5144 | warning_ignored_lines[warning_code].insert(line); |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | return !has_error; |
| 5149 | #else // !DEBUG_ENABLED |
| 5150 | // Only available in debug builds. |
| 5151 | return true; |
| 5152 | #endif // DEBUG_ENABLED |
| 5153 | } |
| 5154 | |
| 5155 | bool GDScriptParser::warning_ignore_region_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 5156 | #ifdef DEBUG_ENABLED |
| 5157 | bool has_error = false; |
| 5158 | const bool is_start = p_annotation->name == SNAME("@warning_ignore_start"); |
| 5159 | for (const Variant &warning_name : p_annotation->resolved_arguments) { |
| 5160 | GDScriptWarning::Code warning_code = GDScriptWarning::get_code_from_name(String(warning_name).to_upper()); |
| 5161 | if (warning_code == GDScriptWarning::WARNING_MAX) { |
| 5162 | push_error(vformat(R"(Invalid warning name: "%s".)", warning_name), p_annotation); |
| 5163 | has_error = true; |
| 5164 | continue; |
| 5165 | } |
| 5166 | if (is_start) { |
| 5167 | if (warning_ignore_start_lines[warning_code] != INT_MAX) { |
| 5168 | push_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); |
| 5169 | has_error = true; |
| 5170 | continue; |
| 5171 | } |
| 5172 | warning_ignore_start_lines[warning_code] = p_annotation->start_line; |
| 5173 | } else { |
| 5174 | if (warning_ignore_start_lines[warning_code] == INT_MAX) { |
| 5175 | push_error(vformat(R"(Warning "%s" is not being ignored by "@warning_ignore_start".)", String(warning_name).to_upper()), p_annotation); |
| 5176 | has_error = true; |
| 5177 | continue; |
| 5178 | } |
| 5179 | const int start_line = warning_ignore_start_lines[warning_code]; |
| 5180 | const int end_line = MAX(start_line, p_annotation->start_line); // Prevent infinite loop. |
| 5181 | for (int i = start_line; i <= end_line; i++) { |
| 5182 | warning_ignored_lines[warning_code].insert(i); |
| 5183 | } |
| 5184 | warning_ignore_start_lines[warning_code] = INT_MAX; |
| 5185 | } |
| 5186 | } |
| 5187 | return !has_error; |
| 5188 | #else // !DEBUG_ENABLED |
| 5189 | // Only available in debug builds. |
| 5190 | return true; |
| 5191 | #endif // DEBUG_ENABLED |
| 5192 | } |
| 5193 | |
| 5194 | bool GDScriptParser::rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class) { |
| 5195 | ERR_FAIL_COND_V_MSG(p_target->type != Node::FUNCTION, false, vformat(R"("%s" annotation can only be applied to functions.)", p_annotation->name)); |
| 5196 | |
| 5197 | FunctionNode *function = static_cast<FunctionNode *>(p_target); |
| 5198 | if (function->rpc_config.get_type() != Variant::NIL) { |
| 5199 | push_error(R"(RPC annotations can only be used once per function.)", p_annotation); |
| 5200 | return false; |
| 5201 | } |
| 5202 | |
| 5203 | // Default values should match the annotation registration defaults and `SceneRPCInterface::_parse_rpc_config()`. |
| 5204 | Dictionary rpc_config; |
| 5205 | rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY; |
| 5206 | if (!p_annotation->resolved_arguments.is_empty()) { |
| 5207 | unsigned char locality_args = 0; |
| 5208 | unsigned char permission_args = 0; |
| 5209 | unsigned char transfer_mode_args = 0; |
| 5210 | |
| 5211 | for (int i = 0; i < p_annotation->resolved_arguments.size(); i++) { |
| 5212 | if (i == 3) { |
| 5213 | rpc_config["channel"] = p_annotation->resolved_arguments[i].operator int(); |
| 5214 | continue; |
| 5215 | } |
| 5216 | |
| 5217 | String arg = p_annotation->resolved_arguments[i].operator String(); |
| 5218 | if (arg == "call_local") { |
| 5219 | locality_args++; |
| 5220 | rpc_config["call_local"] = true; |
| 5221 | } else if (arg == "call_remote") { |
| 5222 | locality_args++; |
| 5223 | rpc_config["call_local"] = false; |
| 5224 | } else if (arg == "any_peer") { |
| 5225 | permission_args++; |
| 5226 | rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER; |
| 5227 | } else if (arg == "authority") { |
| 5228 | permission_args++; |
| 5229 | rpc_config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY; |
| 5230 | } else if (arg == "reliable") { |
| 5231 | transfer_mode_args++; |
| 5232 | rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE; |
| 5233 | } else if (arg == "unreliable") { |
| 5234 | transfer_mode_args++; |
| 5235 | rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_UNRELIABLE; |
| 5236 | } else if (arg == "unreliable_ordered") { |
| 5237 | transfer_mode_args++; |
| 5238 | rpc_config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_UNRELIABLE_ORDERED; |
| 5239 | } else { |
| 5240 | push_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); |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | if (locality_args > 1) { |
| 5245 | push_error(R"(Invalid RPC config. The locality ("call_local"/"call_remote") must be specified no more than once.)", p_annotation); |
| 5246 | } else if (permission_args > 1) { |
| 5247 | push_error(R"(Invalid RPC config. The permission ("any_peer"/"authority") must be specified no more than once.)", p_annotation); |
| 5248 | } else if (transfer_mode_args > 1) { |
| 5249 | push_error(R"(Invalid RPC config. The transfer mode ("reliable"/"unreliable"/"unreliable_ordered") must be specified no more than once.)", p_annotation); |
| 5250 | } |
| 5251 | } |
| 5252 | function->rpc_config = rpc_config; |
| 5253 | return true; |
| 5254 | } |
| 5255 | |
| 5256 | GDScriptParser::DataType GDScriptParser::SuiteNode::Local::get_datatype() const { |
| 5257 | switch (type) { |
| 5258 | case CONSTANT: |
| 5259 | return constant->get_datatype(); |
| 5260 | case VARIABLE: |
| 5261 | return variable->get_datatype(); |
| 5262 | case PARAMETER: |
| 5263 | return parameter->get_datatype(); |
| 5264 | case FOR_VARIABLE: |
| 5265 | case PATTERN_BIND: |
| 5266 | return bind->get_datatype(); |
| 5267 | case UNDEFINED: |
| 5268 | return DataType(); |
| 5269 | } |
| 5270 | return DataType(); |
| 5271 | } |
| 5272 | |
| 5273 | String GDScriptParser::SuiteNode::Local::get_name() const { |
| 5274 | switch (type) { |
| 5275 | case SuiteNode::Local::PARAMETER: |
| 5276 | return "parameter"; |
| 5277 | case SuiteNode::Local::CONSTANT: |
| 5278 | return "constant"; |
| 5279 | case SuiteNode::Local::VARIABLE: |
| 5280 | return "variable"; |
| 5281 | case SuiteNode::Local::FOR_VARIABLE: |
| 5282 | return "for loop iterator"; |
| 5283 | case SuiteNode::Local::PATTERN_BIND: |
| 5284 | return "pattern bind"; |
| 5285 | case SuiteNode::Local::UNDEFINED: |
| 5286 | return "<undefined>"; |
| 5287 | default: |
| 5288 | return String(); |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | String GDScriptParser::DataType::to_string() const { |
| 5293 | switch (kind) { |
| 5294 | case VARIANT: |
| 5295 | return "Variant"; |
| 5296 | case BUILTIN: |
| 5297 | if (builtin_type == Variant::NIL) { |
| 5298 | return "null"; |
| 5299 | } |
| 5300 | if (builtin_type == Variant::ARRAY && has_container_element_type(0)) { |
| 5301 | return vformat("Array[%s]", get_container_element_type(0).to_string()); |
| 5302 | } |
| 5303 | if (builtin_type == Variant::DICTIONARY && has_container_element_types()) { |
| 5304 | return vformat("Dictionary[%s, %s]", get_container_element_type_or_variant(0).to_string(), get_container_element_type_or_variant(1).to_string()); |
| 5305 | } |
| 5306 | return Variant::get_type_name(builtin_type); |
| 5307 | case NATIVE: |
| 5308 | if (is_meta_type) { |
| 5309 | return GDScriptNativeClass::get_class_static(); |
| 5310 | } |
| 5311 | return native_type.operator String(); |
| 5312 | case CLASS: |
| 5313 | if (class_type->identifier != nullptr) { |
| 5314 | return class_type->identifier->name.operator String(); |
| 5315 | } |
| 5316 | return class_type->fqcn; |
| 5317 | case SCRIPT: { |
| 5318 | if (is_meta_type) { |
| 5319 | return script_type.is_valid() ? script_type->get_class_name().operator String() : ""; |
| 5320 | } |
| 5321 | String name = script_type.is_valid() ? script_type->get_name() : ""; |
| 5322 | if (!name.is_empty()) { |
| 5323 | return name; |
| 5324 | } |
| 5325 | name = script_path; |
| 5326 | if (!name.is_empty()) { |
| 5327 | return name; |
| 5328 | } |
| 5329 | return native_type.operator String(); |
| 5330 | } |
| 5331 | case ENUM: { |
| 5332 | // native_type contains either the native class defining the enum |
| 5333 | // or the fully qualified class name of the script defining the enum |
| 5334 | return String(native_type).get_file(); // Remove path, keep filename |
| 5335 | } |
| 5336 | case RESOLVING: |
| 5337 | case UNRESOLVED: |
| 5338 | return "<unresolved type>"; |
| 5339 | } |
| 5340 | |
| 5341 | ERR_FAIL_V_MSG("<unresolved type>", "Kind set outside the enum range."); |
| 5342 | } |
| 5343 | |
| 5344 | PropertyInfo GDScriptParser::DataType::to_property_info(const String &p_name) const { |
| 5345 | PropertyInfo result; |
| 5346 | result.name = p_name; |
| 5347 | result.usage = PROPERTY_USAGE_NONE; |
| 5348 | |
| 5349 | if (!is_hard_type()) { |
| 5350 | result.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; |
| 5351 | return result; |
| 5352 | } |
| 5353 | |
| 5354 | switch (kind) { |
| 5355 | case BUILTIN: |
| 5356 | result.type = builtin_type; |
| 5357 | if (builtin_type == Variant::ARRAY && has_container_element_type(0)) { |
| 5358 | const DataType elem_type = get_container_element_type(0); |
| 5359 | switch (elem_type.kind) { |
| 5360 | case BUILTIN: |
| 5361 | result.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 5362 | result.hint_string = Variant::get_type_name(elem_type.builtin_type); |
| 5363 | break; |
| 5364 | case NATIVE: |
| 5365 | result.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 5366 | result.hint_string = elem_type.native_type; |
| 5367 | break; |
| 5368 | case SCRIPT: |
| 5369 | result.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 5370 | if (elem_type.script_type.is_valid() && elem_type.script_type->get_global_name() != StringName()) { |
| 5371 | result.hint_string = elem_type.script_type->get_global_name(); |
| 5372 | } else { |
| 5373 | result.hint_string = elem_type.native_type; |
| 5374 | } |
| 5375 | break; |
| 5376 | case CLASS: |
| 5377 | result.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 5378 | if (elem_type.class_type != nullptr && elem_type.class_type->get_global_name() != StringName()) { |
| 5379 | result.hint_string = elem_type.class_type->get_global_name(); |
| 5380 | } else { |
| 5381 | result.hint_string = elem_type.native_type; |
| 5382 | } |
| 5383 | break; |
| 5384 | case ENUM: |
| 5385 | result.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 5386 | result.hint_string = String(elem_type.native_type).replace("::", "."); |
| 5387 | break; |
| 5388 | case VARIANT: |
| 5389 | case RESOLVING: |
| 5390 | case UNRESOLVED: |
| 5391 | break; |
| 5392 | } |
| 5393 | } else if (builtin_type == Variant::DICTIONARY && has_container_element_types()) { |
| 5394 | const DataType key_type = get_container_element_type_or_variant(0); |
| 5395 | const DataType value_type = get_container_element_type_or_variant(1); |
| 5396 | if ((key_type.kind == VARIANT && value_type.kind == VARIANT) || key_type.kind == RESOLVING || |
| 5397 | key_type.kind == UNRESOLVED || value_type.kind == RESOLVING || value_type.kind == UNRESOLVED) { |
| 5398 | break; |
| 5399 | } |
| 5400 | String key_hint, value_hint; |
| 5401 | switch (key_type.kind) { |
| 5402 | case BUILTIN: |
| 5403 | key_hint = Variant::get_type_name(key_type.builtin_type); |
| 5404 | break; |
| 5405 | case NATIVE: |
| 5406 | key_hint = key_type.native_type; |
| 5407 | break; |
| 5408 | case SCRIPT: |
| 5409 | if (key_type.script_type.is_valid() && key_type.script_type->get_global_name() != StringName()) { |
| 5410 | key_hint = key_type.script_type->get_global_name(); |
| 5411 | } else { |
| 5412 | key_hint = key_type.native_type; |
| 5413 | } |
| 5414 | break; |
| 5415 | case CLASS: |
| 5416 | if (key_type.class_type != nullptr && key_type.class_type->get_global_name() != StringName()) { |
| 5417 | key_hint = key_type.class_type->get_global_name(); |
| 5418 | } else { |
| 5419 | key_hint = key_type.native_type; |
| 5420 | } |
| 5421 | break; |
| 5422 | case ENUM: |
| 5423 | key_hint = String(key_type.native_type).replace("::", "."); |
| 5424 | break; |
| 5425 | default: |
| 5426 | key_hint = "Variant"; |
| 5427 | break; |
| 5428 | } |
| 5429 | switch (value_type.kind) { |
| 5430 | case BUILTIN: |
| 5431 | value_hint = Variant::get_type_name(value_type.builtin_type); |
| 5432 | break; |
| 5433 | case NATIVE: |
| 5434 | value_hint = value_type.native_type; |
| 5435 | break; |
| 5436 | case SCRIPT: |
| 5437 | if (value_type.script_type.is_valid() && value_type.script_type->get_global_name() != StringName()) { |
| 5438 | value_hint = value_type.script_type->get_global_name(); |
| 5439 | } else { |
| 5440 | value_hint = value_type.native_type; |
| 5441 | } |
| 5442 | break; |
| 5443 | case CLASS: |
| 5444 | if (value_type.class_type != nullptr && value_type.class_type->get_global_name() != StringName()) { |
| 5445 | value_hint = value_type.class_type->get_global_name(); |
| 5446 | } else { |
| 5447 | value_hint = value_type.native_type; |
| 5448 | } |
| 5449 | break; |
| 5450 | case ENUM: |
| 5451 | value_hint = String(value_type.native_type).replace("::", "."); |
| 5452 | break; |
| 5453 | default: |
| 5454 | value_hint = "Variant"; |
| 5455 | break; |
| 5456 | } |
| 5457 | result.hint = PROPERTY_HINT_DICTIONARY_TYPE; |
| 5458 | result.hint_string = key_hint + ";" + value_hint; |
| 5459 | } |
| 5460 | break; |
| 5461 | case NATIVE: |
| 5462 | result.type = Variant::OBJECT; |
| 5463 | if (is_meta_type) { |
| 5464 | result.class_name = GDScriptNativeClass::get_class_static(); |
| 5465 | } else { |
| 5466 | result.class_name = native_type; |
| 5467 | } |
| 5468 | break; |
| 5469 | case SCRIPT: |
| 5470 | result.type = Variant::OBJECT; |
| 5471 | if (is_meta_type) { |
| 5472 | result.class_name = script_type.is_valid() ? script_type->get_class_name() : Script::get_class_static(); |
| 5473 | } else if (script_type.is_valid() && script_type->get_global_name() != StringName()) { |
| 5474 | result.class_name = script_type->get_global_name(); |
| 5475 | } else { |
| 5476 | result.class_name = native_type; |
| 5477 | } |
| 5478 | break; |
| 5479 | case CLASS: |
| 5480 | result.type = Variant::OBJECT; |
| 5481 | if (is_meta_type) { |
| 5482 | result.class_name = GDScript::get_class_static(); |
| 5483 | } else if (class_type != nullptr && class_type->get_global_name() != StringName()) { |
| 5484 | result.class_name = class_type->get_global_name(); |
| 5485 | } else { |
| 5486 | result.class_name = native_type; |
| 5487 | } |
| 5488 | break; |
| 5489 | case ENUM: |
| 5490 | if (is_meta_type) { |
| 5491 | result.type = Variant::DICTIONARY; |
| 5492 | } else { |
| 5493 | result.type = Variant::INT; |
| 5494 | result.usage |= PROPERTY_USAGE_CLASS_IS_ENUM; |
| 5495 | result.class_name = String(native_type).replace("::", "."); |
| 5496 | } |
| 5497 | break; |
| 5498 | case VARIANT: |
| 5499 | case RESOLVING: |
| 5500 | case UNRESOLVED: |
| 5501 | result.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; |
| 5502 | break; |
| 5503 | } |
| 5504 | |
| 5505 | return result; |
| 5506 | } |
| 5507 | |
| 5508 | static Variant::Type _variant_type_to_typed_array_element_type(Variant::Type p_type) { |
| 5509 | switch (p_type) { |
| 5510 | case Variant::PACKED_BYTE_ARRAY: |
| 5511 | case Variant::PACKED_INT32_ARRAY: |
| 5512 | case Variant::PACKED_INT64_ARRAY: |
| 5513 | return Variant::INT; |
| 5514 | case Variant::PACKED_FLOAT32_ARRAY: |
| 5515 | case Variant::PACKED_FLOAT64_ARRAY: |
| 5516 | return Variant::FLOAT; |
| 5517 | case Variant::PACKED_STRING_ARRAY: |
| 5518 | return Variant::STRING; |
| 5519 | case Variant::PACKED_VECTOR2_ARRAY: |
| 5520 | return Variant::VECTOR2; |
| 5521 | case Variant::PACKED_VECTOR3_ARRAY: |
| 5522 | return Variant::VECTOR3; |
| 5523 | case Variant::PACKED_COLOR_ARRAY: |
| 5524 | return Variant::COLOR; |
| 5525 | case Variant::PACKED_VECTOR4_ARRAY: |
| 5526 | return Variant::VECTOR4; |
| 5527 | default: |
| 5528 | return Variant::NIL; |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | bool GDScriptParser::DataType::is_typed_container_type() const { |
| 5533 | return kind == GDScriptParser::DataType::BUILTIN && _variant_type_to_typed_array_element_type(builtin_type) != Variant::NIL; |
| 5534 | } |
| 5535 | |
| 5536 | GDScriptParser::DataType GDScriptParser::DataType::get_typed_container_type() const { |
| 5537 | GDScriptParser::DataType type; |
| 5538 | type.kind = GDScriptParser::DataType::BUILTIN; |
| 5539 | type.builtin_type = _variant_type_to_typed_array_element_type(builtin_type); |
| 5540 | return type; |
| 5541 | } |
| 5542 | |
| 5543 | bool GDScriptParser::DataType::can_reference(const GDScriptParser::DataType &p_other) const { |
| 5544 | if (p_other.is_meta_type) { |
| 5545 | return false; |
| 5546 | } else if (builtin_type != p_other.builtin_type) { |
| 5547 | return false; |
| 5548 | } else if (builtin_type != Variant::OBJECT) { |
| 5549 | return true; |
| 5550 | } |
| 5551 | |
| 5552 | if (native_type == StringName()) { |
| 5553 | return true; |
| 5554 | } else if (p_other.native_type == StringName()) { |
| 5555 | return false; |
| 5556 | } else if (native_type != p_other.native_type && !ClassDB::is_parent_class(p_other.native_type, native_type)) { |
| 5557 | return false; |
| 5558 | } |
| 5559 | |
| 5560 | Ref<Script> script = script_type; |
| 5561 | if (kind == GDScriptParser::DataType::CLASS && script.is_null()) { |
| 5562 | Error err = OK; |
| 5563 | Ref<GDScript> scr = GDScriptCache::get_shallow_script(script_path, err); |
| 5564 | ERR_FAIL_COND_V_MSG(err, false, vformat(R"(Error while getting cache for script "%s".)", script_path)); |
| 5565 | script.reference_ptr(scr->find_class(class_type->fqcn)); |
| 5566 | } |
| 5567 | |
| 5568 | Ref<Script> script_other = p_other.script_type; |
| 5569 | if (p_other.kind == GDScriptParser::DataType::CLASS && script_other.is_null()) { |
| 5570 | Error err = OK; |
| 5571 | Ref<GDScript> scr = GDScriptCache::get_shallow_script(p_other.script_path, err); |
| 5572 | ERR_FAIL_COND_V_MSG(err, false, vformat(R"(Error while getting cache for script "%s".)", p_other.script_path)); |
| 5573 | script_other.reference_ptr(scr->find_class(p_other.class_type->fqcn)); |
| 5574 | } |
| 5575 | |
| 5576 | if (script.is_null()) { |
| 5577 | return true; |
| 5578 | } else if (script_other.is_null()) { |
| 5579 | return false; |
| 5580 | } else if (script != script_other && !script_other->inherits_script(script)) { |
| 5581 | return false; |
| 5582 | } |
| 5583 | |
| 5584 | return true; |
| 5585 | } |
| 5586 | |
| 5587 | void GDScriptParser::complete_extents(Node *p_node) { |
| 5588 | while (!nodes_in_progress.is_empty() && nodes_in_progress.back()->get() != p_node) { |
| 5589 | ERR_PRINT("Parser bug: Mismatch in extents tracking stack."); |
| 5590 | nodes_in_progress.pop_back(); |
| 5591 | } |
| 5592 | if (nodes_in_progress.is_empty()) { |
| 5593 | ERR_PRINT("Parser bug: Extents tracking stack is empty."); |
| 5594 | } else { |
| 5595 | nodes_in_progress.pop_back(); |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | void GDScriptParser::update_extents(Node *p_node) { |
| 5600 | p_node->end_line = previous.end_line; |
| 5601 | p_node->end_column = previous.end_column; |
| 5602 | } |
| 5603 | |
| 5604 | void GDScriptParser::reset_extents(Node *p_node, GDScriptTokenizer::Token p_token) { |
| 5605 | p_node->start_line = p_token.start_line; |
| 5606 | p_node->end_line = p_token.end_line; |
| 5607 | p_node->start_column = p_token.start_column; |
| 5608 | p_node->end_column = p_token.end_column; |
| 5609 | } |
| 5610 | |
| 5611 | void GDScriptParser::reset_extents(Node *p_node, Node *p_from) { |
| 5612 | if (p_from == nullptr) { |
| 5613 | return; |
| 5614 | } |
| 5615 | p_node->start_line = p_from->start_line; |
| 5616 | p_node->end_line = p_from->end_line; |
| 5617 | p_node->start_column = p_from->start_column; |
| 5618 | p_node->end_column = p_from->end_column; |
| 5619 | } |
| 5620 | |
| 5621 | /*---------- PRETTY PRINT FOR DEBUG ----------*/ |
| 5622 | |
| 5623 | #ifdef DEBUG_ENABLED |
| 5624 | |
| 5625 | void GDScriptParser::TreePrinter::increase_indent() { |
| 5626 | indent_level++; |
| 5627 | indent = ""; |
| 5628 | for (int i = 0; i < indent_level * 4; i++) { |
| 5629 | if (i % 4 == 0) { |
| 5630 | indent += "|"; |
| 5631 | } else { |
| 5632 | indent += " "; |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | void GDScriptParser::TreePrinter::decrease_indent() { |
| 5638 | indent_level--; |
| 5639 | indent = ""; |
| 5640 | for (int i = 0; i < indent_level * 4; i++) { |
| 5641 | if (i % 4 == 0) { |
| 5642 | indent += "|"; |
| 5643 | } else { |
| 5644 | indent += " "; |
| 5645 | } |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | void GDScriptParser::TreePrinter::push_line(const String &p_line) { |
| 5650 | if (!p_line.is_empty()) { |
| 5651 | push_text(p_line); |
| 5652 | } |
| 5653 | printed += "\n"; |
| 5654 | pending_indent = true; |
| 5655 | } |
| 5656 | |
| 5657 | void GDScriptParser::TreePrinter::push_text(const String &p_text) { |
| 5658 | if (pending_indent) { |
| 5659 | printed += indent; |
| 5660 | pending_indent = false; |
| 5661 | } |
| 5662 | printed += p_text; |
| 5663 | } |
| 5664 | |
| 5665 | void GDScriptParser::TreePrinter::print_annotation(const AnnotationNode *p_annotation) { |
| 5666 | push_text(p_annotation->name); |
| 5667 | push_text(" ("); |
| 5668 | for (int i = 0; i < p_annotation->arguments.size(); i++) { |
| 5669 | if (i > 0) { |
| 5670 | push_text(" , "); |
| 5671 | } |
| 5672 | print_expression(p_annotation->arguments[i]); |
| 5673 | } |
| 5674 | push_line(")"); |
| 5675 | } |
| 5676 | |
| 5677 | void GDScriptParser::TreePrinter::print_array(ArrayNode *p_array) { |
| 5678 | push_text("[ "); |
| 5679 | for (int i = 0; i < p_array->elements.size(); i++) { |
| 5680 | if (i > 0) { |
| 5681 | push_text(" , "); |
| 5682 | } |
| 5683 | print_expression(p_array->elements[i]); |
| 5684 | } |
| 5685 | push_text(" ]"); |
| 5686 | } |
| 5687 | |
| 5688 | void GDScriptParser::TreePrinter::print_assert(AssertNode *p_assert) { |
| 5689 | push_text("Assert ( "); |
| 5690 | print_expression(p_assert->condition); |
| 5691 | push_line(" )"); |
| 5692 | } |
| 5693 | |
| 5694 | void GDScriptParser::TreePrinter::print_assignment(AssignmentNode *p_assignment) { |
| 5695 | switch (p_assignment->assignee->type) { |
| 5696 | case Node::IDENTIFIER: |
| 5697 | print_identifier(static_cast<IdentifierNode *>(p_assignment->assignee)); |
| 5698 | break; |
| 5699 | case Node::SUBSCRIPT: |
| 5700 | print_subscript(static_cast<SubscriptNode *>(p_assignment->assignee)); |
| 5701 | break; |
| 5702 | default: |
| 5703 | break; // Unreachable. |
| 5704 | } |
| 5705 | |
| 5706 | push_text(" "); |
| 5707 | switch (p_assignment->operation) { |
| 5708 | case AssignmentNode::OP_ADDITION: |
| 5709 | push_text("+"); |
| 5710 | break; |
| 5711 | case AssignmentNode::OP_SUBTRACTION: |
| 5712 | push_text("-"); |
| 5713 | break; |
| 5714 | case AssignmentNode::OP_MULTIPLICATION: |
| 5715 | push_text("*"); |
| 5716 | break; |
| 5717 | case AssignmentNode::OP_DIVISION: |
| 5718 | push_text("/"); |
| 5719 | break; |
| 5720 | case AssignmentNode::OP_MODULO: |
| 5721 | push_text("%"); |
| 5722 | break; |
| 5723 | case AssignmentNode::OP_POWER: |
| 5724 | push_text("**"); |
| 5725 | break; |
| 5726 | case AssignmentNode::OP_BIT_SHIFT_LEFT: |
| 5727 | push_text("<<"); |
| 5728 | break; |
| 5729 | case AssignmentNode::OP_BIT_SHIFT_RIGHT: |
| 5730 | push_text(">>"); |
| 5731 | break; |
| 5732 | case AssignmentNode::OP_BIT_AND: |
| 5733 | push_text("&"); |
| 5734 | break; |
| 5735 | case AssignmentNode::OP_BIT_OR: |
| 5736 | push_text("|"); |
| 5737 | break; |
| 5738 | case AssignmentNode::OP_BIT_XOR: |
| 5739 | push_text("^"); |
| 5740 | break; |
| 5741 | case AssignmentNode::OP_NONE: |
| 5742 | break; |
| 5743 | } |
| 5744 | push_text("= "); |
| 5745 | print_expression(p_assignment->assigned_value); |
| 5746 | push_line(); |
| 5747 | } |
| 5748 | |
| 5749 | void GDScriptParser::TreePrinter::print_await(AwaitNode *p_await) { |
| 5750 | push_text("Await "); |
| 5751 | print_expression(p_await->to_await); |
| 5752 | } |
| 5753 | |
| 5754 | void GDScriptParser::TreePrinter::print_binary_op(BinaryOpNode *p_binary_op) { |
| 5755 | // Surround in parenthesis for disambiguation. |
| 5756 | push_text("("); |
| 5757 | print_expression(p_binary_op->left_operand); |
| 5758 | switch (p_binary_op->operation) { |
| 5759 | case BinaryOpNode::OP_ADDITION: |
| 5760 | push_text(" + "); |
| 5761 | break; |
| 5762 | case BinaryOpNode::OP_SUBTRACTION: |
| 5763 | push_text(" - "); |
| 5764 | break; |
| 5765 | case BinaryOpNode::OP_MULTIPLICATION: |
| 5766 | push_text(" * "); |
| 5767 | break; |
| 5768 | case BinaryOpNode::OP_DIVISION: |
| 5769 | push_text(" / "); |
| 5770 | break; |
| 5771 | case BinaryOpNode::OP_MODULO: |
| 5772 | push_text(" % "); |
| 5773 | break; |
| 5774 | case BinaryOpNode::OP_POWER: |
| 5775 | push_text(" ** "); |
| 5776 | break; |
| 5777 | case BinaryOpNode::OP_BIT_LEFT_SHIFT: |
| 5778 | push_text(" << "); |
| 5779 | break; |
| 5780 | case BinaryOpNode::OP_BIT_RIGHT_SHIFT: |
| 5781 | push_text(" >> "); |
| 5782 | break; |
| 5783 | case BinaryOpNode::OP_BIT_AND: |
| 5784 | push_text(" & "); |
| 5785 | break; |
| 5786 | case BinaryOpNode::OP_BIT_OR: |
| 5787 | push_text(" | "); |
| 5788 | break; |
| 5789 | case BinaryOpNode::OP_BIT_XOR: |
| 5790 | push_text(" ^ "); |
| 5791 | break; |
| 5792 | case BinaryOpNode::OP_LOGIC_AND: |
| 5793 | push_text(" AND "); |
| 5794 | break; |
| 5795 | case BinaryOpNode::OP_LOGIC_OR: |
| 5796 | push_text(" OR "); |
| 5797 | break; |
| 5798 | case BinaryOpNode::OP_CONTENT_TEST: |
| 5799 | push_text(" IN "); |
| 5800 | break; |
| 5801 | case BinaryOpNode::OP_COMP_EQUAL: |
| 5802 | push_text(" == "); |
| 5803 | break; |
| 5804 | case BinaryOpNode::OP_COMP_NOT_EQUAL: |
| 5805 | push_text(" != "); |
| 5806 | break; |
| 5807 | case BinaryOpNode::OP_COMP_LESS: |
| 5808 | push_text(" < "); |
| 5809 | break; |
| 5810 | case BinaryOpNode::OP_COMP_LESS_EQUAL: |
| 5811 | push_text(" <= "); |
| 5812 | break; |
| 5813 | case BinaryOpNode::OP_COMP_GREATER: |
| 5814 | push_text(" > "); |
| 5815 | break; |
| 5816 | case BinaryOpNode::OP_COMP_GREATER_EQUAL: |
| 5817 | push_text(" >= "); |
| 5818 | break; |
| 5819 | } |
| 5820 | print_expression(p_binary_op->right_operand); |
| 5821 | // Surround in parenthesis for disambiguation. |
| 5822 | push_text(")"); |
| 5823 | } |
| 5824 | |
| 5825 | void GDScriptParser::TreePrinter::print_call(CallNode *p_call) { |
| 5826 | if (p_call->is_super) { |
| 5827 | push_text("super"); |
| 5828 | if (p_call->callee != nullptr) { |
| 5829 | push_text("."); |
| 5830 | print_expression(p_call->callee); |
| 5831 | } |
| 5832 | } else { |
| 5833 | print_expression(p_call->callee); |
| 5834 | } |
| 5835 | push_text("( "); |
| 5836 | for (int i = 0; i < p_call->arguments.size(); i++) { |
| 5837 | if (i > 0) { |
| 5838 | push_text(" , "); |
| 5839 | } |
| 5840 | print_expression(p_call->arguments[i]); |
| 5841 | } |
| 5842 | push_text(" )"); |
| 5843 | } |
| 5844 | |
| 5845 | void GDScriptParser::TreePrinter::print_cast(CastNode *p_cast) { |
| 5846 | print_expression(p_cast->operand); |
| 5847 | push_text(" AS "); |
| 5848 | print_type(p_cast->cast_type); |
| 5849 | } |
| 5850 | |
| 5851 | void GDScriptParser::TreePrinter::print_class(ClassNode *p_class) { |
| 5852 | for (const AnnotationNode *E : p_class->annotations) { |
| 5853 | print_annotation(E); |
| 5854 | } |
| 5855 | push_text("Class "); |
| 5856 | if (p_class->identifier == nullptr) { |
| 5857 | push_text("<unnamed>"); |
| 5858 | } else { |
| 5859 | print_identifier(p_class->identifier); |
| 5860 | } |
| 5861 | |
| 5862 | if (p_class->extends_used) { |
| 5863 | bool first = true; |
| 5864 | push_text(" Extends "); |
| 5865 | if (!p_class->extends_path.is_empty()) { |
| 5866 | push_text(vformat(R"("%s")", p_class->extends_path)); |
| 5867 | first = false; |
| 5868 | } |
| 5869 | for (int i = 0; i < p_class->extends.size(); i++) { |
| 5870 | if (!first) { |
| 5871 | push_text("."); |
| 5872 | } else { |
| 5873 | first = false; |
| 5874 | } |
| 5875 | push_text(p_class->extends[i]->name); |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | push_line(" :"); |
| 5880 | |
| 5881 | increase_indent(); |
| 5882 | |
| 5883 | for (int i = 0; i < p_class->members.size(); i++) { |
| 5884 | const ClassNode::Member &m = p_class->members[i]; |
| 5885 | |
| 5886 | switch (m.type) { |
| 5887 | case ClassNode::Member::CLASS: |
| 5888 | print_class(m.m_class); |
| 5889 | break; |
| 5890 | case ClassNode::Member::VARIABLE: |
| 5891 | print_variable(m.variable); |
| 5892 | break; |
| 5893 | case ClassNode::Member::CONSTANT: |
| 5894 | print_constant(m.constant); |
| 5895 | break; |
| 5896 | case ClassNode::Member::SIGNAL: |
| 5897 | print_signal(m.signal); |
| 5898 | break; |
| 5899 | case ClassNode::Member::FUNCTION: |
| 5900 | print_function(m.function); |
| 5901 | break; |
| 5902 | case ClassNode::Member::ENUM: |
| 5903 | print_enum(m.m_enum); |
| 5904 | break; |
| 5905 | case ClassNode::Member::ENUM_VALUE: |
| 5906 | break; // Nothing. Will be printed by enum. |
| 5907 | case ClassNode::Member::GROUP: |
| 5908 | break; // Nothing. Groups are only used by inspector. |
| 5909 | case ClassNode::Member::UNDEFINED: |
| 5910 | push_line("<unknown member>"); |
| 5911 | break; |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | decrease_indent(); |
| 5916 | } |
| 5917 | |
| 5918 | void GDScriptParser::TreePrinter::print_constant(ConstantNode *p_constant) { |
| 5919 | push_text("Constant "); |
| 5920 | print_identifier(p_constant->identifier); |
| 5921 | |
| 5922 | increase_indent(); |
| 5923 | |
| 5924 | push_line(); |
| 5925 | push_text("= "); |
| 5926 | if (p_constant->initializer == nullptr) { |
| 5927 | push_text("<missing value>"); |
| 5928 | } else { |
| 5929 | print_expression(p_constant->initializer); |
| 5930 | } |
| 5931 | decrease_indent(); |
| 5932 | push_line(); |
| 5933 | } |
| 5934 | |
| 5935 | void GDScriptParser::TreePrinter::print_dictionary(DictionaryNode *p_dictionary) { |
| 5936 | push_line("{"); |
| 5937 | increase_indent(); |
| 5938 | for (int i = 0; i < p_dictionary->elements.size(); i++) { |
| 5939 | print_expression(p_dictionary->elements[i].key); |
| 5940 | if (p_dictionary->style == DictionaryNode::PYTHON_DICT) { |
| 5941 | push_text(" : "); |
| 5942 | } else { |
| 5943 | push_text(" = "); |
| 5944 | } |
| 5945 | print_expression(p_dictionary->elements[i].value); |
| 5946 | push_line(" ,"); |
| 5947 | } |
| 5948 | decrease_indent(); |
| 5949 | push_text("}"); |
| 5950 | } |
| 5951 | |
| 5952 | void GDScriptParser::TreePrinter::print_expression(ExpressionNode *p_expression) { |
| 5953 | if (p_expression == nullptr) { |
| 5954 | push_text("<invalid expression>"); |
| 5955 | return; |
| 5956 | } |
| 5957 | switch (p_expression->type) { |
| 5958 | case Node::ARRAY: |
| 5959 | print_array(static_cast<ArrayNode *>(p_expression)); |
| 5960 | break; |
| 5961 | case Node::ASSIGNMENT: |
| 5962 | print_assignment(static_cast<AssignmentNode *>(p_expression)); |
| 5963 | break; |
| 5964 | case Node::AWAIT: |
| 5965 | print_await(static_cast<AwaitNode *>(p_expression)); |
| 5966 | break; |
| 5967 | case Node::BINARY_OPERATOR: |
| 5968 | print_binary_op(static_cast<BinaryOpNode *>(p_expression)); |
| 5969 | break; |
| 5970 | case Node::CALL: |
| 5971 | print_call(static_cast<CallNode *>(p_expression)); |
| 5972 | break; |
| 5973 | case Node::CAST: |
| 5974 | print_cast(static_cast<CastNode *>(p_expression)); |
| 5975 | break; |
| 5976 | case Node::DICTIONARY: |
| 5977 | print_dictionary(static_cast<DictionaryNode *>(p_expression)); |
| 5978 | break; |
| 5979 | case Node::GET_NODE: |
| 5980 | print_get_node(static_cast<GetNodeNode *>(p_expression)); |
| 5981 | break; |
| 5982 | case Node::IDENTIFIER: |
| 5983 | print_identifier(static_cast<IdentifierNode *>(p_expression)); |
| 5984 | break; |
| 5985 | case Node::LAMBDA: |
| 5986 | print_lambda(static_cast<LambdaNode *>(p_expression)); |
| 5987 | break; |
| 5988 | case Node::LITERAL: |
| 5989 | print_literal(static_cast<LiteralNode *>(p_expression)); |
| 5990 | break; |
| 5991 | case Node::PRELOAD: |
| 5992 | print_preload(static_cast<PreloadNode *>(p_expression)); |
| 5993 | break; |
| 5994 | case Node::SELF: |
| 5995 | print_self(static_cast<SelfNode *>(p_expression)); |
| 5996 | break; |
| 5997 | case Node::SUBSCRIPT: |
| 5998 | print_subscript(static_cast<SubscriptNode *>(p_expression)); |
| 5999 | break; |
| 6000 | case Node::TERNARY_OPERATOR: |
| 6001 | print_ternary_op(static_cast<TernaryOpNode *>(p_expression)); |
| 6002 | break; |
| 6003 | case Node::TYPE_TEST: |
| 6004 | print_type_test(static_cast<TypeTestNode *>(p_expression)); |
| 6005 | break; |
| 6006 | case Node::UNARY_OPERATOR: |
| 6007 | print_unary_op(static_cast<UnaryOpNode *>(p_expression)); |
| 6008 | break; |
| 6009 | default: |
| 6010 | push_text(vformat("<unknown expression %d>", p_expression->type)); |
| 6011 | break; |
| 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | void GDScriptParser::TreePrinter::print_enum(EnumNode *p_enum) { |
| 6016 | push_text("Enum "); |
| 6017 | if (p_enum->identifier != nullptr) { |
| 6018 | print_identifier(p_enum->identifier); |
| 6019 | } else { |
| 6020 | push_text("<unnamed>"); |
| 6021 | } |
| 6022 | |
| 6023 | push_line(" {"); |
| 6024 | increase_indent(); |
| 6025 | for (int i = 0; i < p_enum->values.size(); i++) { |
| 6026 | const EnumNode::Value &item = p_enum->values[i]; |
| 6027 | print_identifier(item.identifier); |
| 6028 | push_text(" = "); |
| 6029 | push_text(itos(item.value)); |
| 6030 | push_line(" ,"); |
| 6031 | } |
| 6032 | decrease_indent(); |
| 6033 | push_line("}"); |
| 6034 | } |
| 6035 | |
| 6036 | void GDScriptParser::TreePrinter::print_for(ForNode *p_for) { |
| 6037 | push_text("For "); |
| 6038 | print_identifier(p_for->variable); |
| 6039 | push_text(" IN "); |
| 6040 | print_expression(p_for->list); |
| 6041 | push_line(" :"); |
| 6042 | |
| 6043 | increase_indent(); |
| 6044 | |
| 6045 | print_suite(p_for->loop); |
| 6046 | |
| 6047 | decrease_indent(); |
| 6048 | } |
| 6049 | |
| 6050 | void GDScriptParser::TreePrinter::print_function(FunctionNode *p_function, const String &p_context) { |
| 6051 | for (const AnnotationNode *E : p_function->annotations) { |
| 6052 | print_annotation(E); |
| 6053 | } |
| 6054 | if (p_function->is_static) { |
| 6055 | push_text("Static "); |
| 6056 | } |
| 6057 | push_text(p_context); |
| 6058 | push_text(" "); |
| 6059 | if (p_function->identifier) { |
| 6060 | print_identifier(p_function->identifier); |
| 6061 | } else { |
| 6062 | push_text("<anonymous>"); |
| 6063 | } |
| 6064 | push_text("( "); |
| 6065 | for (int i = 0; i < p_function->parameters.size(); i++) { |
| 6066 | if (i > 0) { |
| 6067 | push_text(" , "); |
| 6068 | } |
| 6069 | print_parameter(p_function->parameters[i]); |
| 6070 | } |
| 6071 | push_line(" ) :"); |
| 6072 | increase_indent(); |
| 6073 | print_suite(p_function->body); |
| 6074 | decrease_indent(); |
| 6075 | } |
| 6076 | |
| 6077 | void GDScriptParser::TreePrinter::print_get_node(GetNodeNode *p_get_node) { |
| 6078 | if (p_get_node->use_dollar) { |
| 6079 | push_text("$"); |
| 6080 | } |
| 6081 | push_text(p_get_node->full_path); |
| 6082 | } |
| 6083 | |
| 6084 | void GDScriptParser::TreePrinter::print_identifier(IdentifierNode *p_identifier) { |
| 6085 | if (p_identifier != nullptr) { |
| 6086 | push_text(p_identifier->name); |
| 6087 | } else { |
| 6088 | push_text("<invalid identifier>"); |
| 6089 | } |
| 6090 | } |
| 6091 | |
| 6092 | void GDScriptParser::TreePrinter::print_if(IfNode *p_if, bool p_is_elif) { |
| 6093 | if (p_is_elif) { |
| 6094 | push_text("Elif "); |
| 6095 | } else { |
| 6096 | push_text("If "); |
| 6097 | } |
| 6098 | print_expression(p_if->condition); |
| 6099 | push_line(" :"); |
| 6100 | |
| 6101 | increase_indent(); |
| 6102 | print_suite(p_if->true_block); |
| 6103 | decrease_indent(); |
| 6104 | |
| 6105 | // FIXME: Properly detect "elif" blocks. |
| 6106 | if (p_if->false_block != nullptr) { |
| 6107 | push_line("Else :"); |
| 6108 | increase_indent(); |
| 6109 | print_suite(p_if->false_block); |
| 6110 | decrease_indent(); |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | void GDScriptParser::TreePrinter::print_lambda(LambdaNode *p_lambda) { |
| 6115 | print_function(p_lambda->function, "Lambda"); |
| 6116 | push_text("| captures [ "); |
| 6117 | for (int i = 0; i < p_lambda->captures.size(); i++) { |
| 6118 | if (i > 0) { |
| 6119 | push_text(" , "); |
| 6120 | } |
| 6121 | push_text(p_lambda->captures[i]->name.operator String()); |
| 6122 | } |
| 6123 | push_line(" ]"); |
| 6124 | } |
| 6125 | |
| 6126 | void GDScriptParser::TreePrinter::print_literal(LiteralNode *p_literal) { |
| 6127 | // Prefix for string types. |
| 6128 | switch (p_literal->value.get_type()) { |
| 6129 | case Variant::NODE_PATH: |
| 6130 | push_text("^\""); |
| 6131 | break; |
| 6132 | case Variant::STRING: |
| 6133 | push_text("\""); |
| 6134 | break; |
| 6135 | case Variant::STRING_NAME: |
| 6136 | push_text("&\""); |
| 6137 | break; |
| 6138 | default: |
| 6139 | break; |
| 6140 | } |
| 6141 | push_text(p_literal->value); |
| 6142 | // Suffix for string types. |
| 6143 | switch (p_literal->value.get_type()) { |
| 6144 | case Variant::NODE_PATH: |
| 6145 | case Variant::STRING: |
| 6146 | case Variant::STRING_NAME: |
| 6147 | push_text("\""); |
| 6148 | break; |
| 6149 | default: |
| 6150 | break; |
| 6151 | } |
| 6152 | } |
| 6153 | |
| 6154 | void GDScriptParser::TreePrinter::print_match(MatchNode *p_match) { |
| 6155 | push_text("Match "); |
| 6156 | print_expression(p_match->test); |
| 6157 | push_line(" :"); |
| 6158 | |
| 6159 | increase_indent(); |
| 6160 | for (int i = 0; i < p_match->branches.size(); i++) { |
| 6161 | print_match_branch(p_match->branches[i]); |
| 6162 | } |
| 6163 | decrease_indent(); |
| 6164 | } |
| 6165 | |
| 6166 | void GDScriptParser::TreePrinter::print_match_branch(MatchBranchNode *p_match_branch) { |
| 6167 | for (int i = 0; i < p_match_branch->patterns.size(); i++) { |
| 6168 | if (i > 0) { |
| 6169 | push_text(" , "); |
| 6170 | } |
| 6171 | print_match_pattern(p_match_branch->patterns[i]); |
| 6172 | } |
| 6173 | |
| 6174 | push_line(" :"); |
| 6175 | |
| 6176 | increase_indent(); |
| 6177 | print_suite(p_match_branch->block); |
| 6178 | decrease_indent(); |
| 6179 | } |
| 6180 | |
| 6181 | void GDScriptParser::TreePrinter::print_match_pattern(PatternNode *p_match_pattern) { |
| 6182 | switch (p_match_pattern->pattern_type) { |
| 6183 | case PatternNode::PT_LITERAL: |
| 6184 | print_literal(p_match_pattern->literal); |
| 6185 | break; |
| 6186 | case PatternNode::PT_WILDCARD: |
| 6187 | push_text("_"); |
| 6188 | break; |
| 6189 | case PatternNode::PT_REST: |
| 6190 | push_text(".."); |
| 6191 | break; |
| 6192 | case PatternNode::PT_BIND: |
| 6193 | push_text("Var "); |
| 6194 | print_identifier(p_match_pattern->bind); |
| 6195 | break; |
| 6196 | case PatternNode::PT_EXPRESSION: |
| 6197 | print_expression(p_match_pattern->expression); |
| 6198 | break; |
| 6199 | case PatternNode::PT_ARRAY: |
| 6200 | push_text("[ "); |
| 6201 | for (int i = 0; i < p_match_pattern->array.size(); i++) { |
| 6202 | if (i > 0) { |
| 6203 | push_text(" , "); |
| 6204 | } |
| 6205 | print_match_pattern(p_match_pattern->array[i]); |
| 6206 | } |
| 6207 | push_text(" ]"); |
| 6208 | break; |
| 6209 | case PatternNode::PT_DICTIONARY: |
| 6210 | push_text("{ "); |
| 6211 | for (int i = 0; i < p_match_pattern->dictionary.size(); i++) { |
| 6212 | if (i > 0) { |
| 6213 | push_text(" , "); |
| 6214 | } |
| 6215 | if (p_match_pattern->dictionary[i].key != nullptr) { |
| 6216 | // Key can be null for rest pattern. |
| 6217 | print_expression(p_match_pattern->dictionary[i].key); |
| 6218 | push_text(" : "); |
| 6219 | } |
| 6220 | print_match_pattern(p_match_pattern->dictionary[i].value_pattern); |
| 6221 | } |
| 6222 | push_text(" }"); |
| 6223 | break; |
| 6224 | } |
| 6225 | } |
| 6226 | |
| 6227 | void GDScriptParser::TreePrinter::print_parameter(ParameterNode *p_parameter) { |
| 6228 | print_identifier(p_parameter->identifier); |
| 6229 | if (p_parameter->datatype_specifier != nullptr) { |
| 6230 | push_text(" : "); |
| 6231 | print_type(p_parameter->datatype_specifier); |
| 6232 | } |
| 6233 | if (p_parameter->initializer != nullptr) { |
| 6234 | push_text(" = "); |
| 6235 | print_expression(p_parameter->initializer); |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | void GDScriptParser::TreePrinter::print_preload(PreloadNode *p_preload) { |
| 6240 | push_text(R"(Preload ( ")"); |
| 6241 | push_text(p_preload->resolved_path); |
| 6242 | push_text(R"(" )"); |
| 6243 | } |
| 6244 | |
| 6245 | void GDScriptParser::TreePrinter::print_return(ReturnNode *p_return) { |
| 6246 | push_text("Return"); |
| 6247 | if (p_return->return_value != nullptr) { |
| 6248 | push_text(" "); |
| 6249 | print_expression(p_return->return_value); |
| 6250 | } |
| 6251 | push_line(); |
| 6252 | } |
| 6253 | |
| 6254 | void GDScriptParser::TreePrinter::print_self(SelfNode *p_self) { |
| 6255 | push_text("Self("); |
| 6256 | if (p_self->current_class->identifier != nullptr) { |
| 6257 | print_identifier(p_self->current_class->identifier); |
| 6258 | } else { |
| 6259 | push_text("<main class>"); |
| 6260 | } |
| 6261 | push_text(")"); |
| 6262 | } |
| 6263 | |
| 6264 | void GDScriptParser::TreePrinter::print_signal(SignalNode *p_signal) { |
| 6265 | push_text("Signal "); |
| 6266 | print_identifier(p_signal->identifier); |
| 6267 | push_text("( "); |
| 6268 | for (int i = 0; i < p_signal->parameters.size(); i++) { |
| 6269 | print_parameter(p_signal->parameters[i]); |
| 6270 | } |
| 6271 | push_line(" )"); |
| 6272 | } |
| 6273 | |
| 6274 | void GDScriptParser::TreePrinter::print_subscript(SubscriptNode *p_subscript) { |
| 6275 | print_expression(p_subscript->base); |
| 6276 | if (p_subscript->is_attribute) { |
| 6277 | push_text("."); |
| 6278 | print_identifier(p_subscript->attribute); |
| 6279 | } else { |
| 6280 | push_text("[ "); |
| 6281 | print_expression(p_subscript->index); |
| 6282 | push_text(" ]"); |
| 6283 | } |
| 6284 | } |
| 6285 | |
| 6286 | void GDScriptParser::TreePrinter::print_statement(Node *p_statement) { |
| 6287 | switch (p_statement->type) { |
| 6288 | case Node::ASSERT: |
| 6289 | print_assert(static_cast<AssertNode *>(p_statement)); |
| 6290 | break; |
| 6291 | case Node::VARIABLE: |
| 6292 | print_variable(static_cast<VariableNode *>(p_statement)); |
| 6293 | break; |
| 6294 | case Node::CONSTANT: |
| 6295 | print_constant(static_cast<ConstantNode *>(p_statement)); |
| 6296 | break; |
| 6297 | case Node::IF: |
| 6298 | print_if(static_cast<IfNode *>(p_statement)); |
| 6299 | break; |
| 6300 | case Node::FOR: |
| 6301 | print_for(static_cast<ForNode *>(p_statement)); |
| 6302 | break; |
| 6303 | case Node::WHILE: |
| 6304 | print_while(static_cast<WhileNode *>(p_statement)); |
| 6305 | break; |
| 6306 | case Node::MATCH: |
| 6307 | print_match(static_cast<MatchNode *>(p_statement)); |
| 6308 | break; |
| 6309 | case Node::RETURN: |
| 6310 | print_return(static_cast<ReturnNode *>(p_statement)); |
| 6311 | break; |
| 6312 | case Node::BREAK: |
| 6313 | push_line("Break"); |
| 6314 | break; |
| 6315 | case Node::CONTINUE: |
| 6316 | push_line("Continue"); |
| 6317 | break; |
| 6318 | case Node::PASS: |
| 6319 | push_line("Pass"); |
| 6320 | break; |
| 6321 | case Node::BREAKPOINT: |
| 6322 | push_line("Breakpoint"); |
| 6323 | break; |
| 6324 | case Node::ASSIGNMENT: |
| 6325 | print_assignment(static_cast<AssignmentNode *>(p_statement)); |
| 6326 | break; |
| 6327 | default: |
| 6328 | if (p_statement->is_expression()) { |
| 6329 | print_expression(static_cast<ExpressionNode *>(p_statement)); |
| 6330 | push_line(); |
| 6331 | } else { |
| 6332 | push_line(vformat("<unknown statement %d>", p_statement->type)); |
| 6333 | } |
| 6334 | break; |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | void GDScriptParser::TreePrinter::print_suite(SuiteNode *p_suite) { |
| 6339 | for (int i = 0; i < p_suite->statements.size(); i++) { |
| 6340 | print_statement(p_suite->statements[i]); |
| 6341 | } |
| 6342 | } |
| 6343 | |
| 6344 | void GDScriptParser::TreePrinter::print_ternary_op(TernaryOpNode *p_ternary_op) { |
| 6345 | // Surround in parenthesis for disambiguation. |
| 6346 | push_text("("); |
| 6347 | print_expression(p_ternary_op->true_expr); |
| 6348 | push_text(") IF ("); |
| 6349 | print_expression(p_ternary_op->condition); |
| 6350 | push_text(") ELSE ("); |
| 6351 | print_expression(p_ternary_op->false_expr); |
| 6352 | push_text(")"); |
| 6353 | } |
| 6354 | |
| 6355 | void GDScriptParser::TreePrinter::print_type(TypeNode *p_type) { |
| 6356 | if (p_type->type_chain.is_empty()) { |
| 6357 | push_text("Void"); |
| 6358 | } else { |
| 6359 | for (int i = 0; i < p_type->type_chain.size(); i++) { |
| 6360 | if (i > 0) { |
| 6361 | push_text("."); |
| 6362 | } |
| 6363 | print_identifier(p_type->type_chain[i]); |
| 6364 | } |
| 6365 | } |
| 6366 | } |
| 6367 | |
| 6368 | void GDScriptParser::TreePrinter::print_type_test(TypeTestNode *p_test) { |
| 6369 | print_expression(p_test->operand); |
| 6370 | push_text(" IS "); |
| 6371 | print_type(p_test->test_type); |
| 6372 | } |
| 6373 | |
| 6374 | void GDScriptParser::TreePrinter::print_unary_op(UnaryOpNode *p_unary_op) { |
| 6375 | // Surround in parenthesis for disambiguation. |
| 6376 | push_text("("); |
| 6377 | switch (p_unary_op->operation) { |
| 6378 | case UnaryOpNode::OP_POSITIVE: |
| 6379 | push_text("+"); |
| 6380 | break; |
| 6381 | case UnaryOpNode::OP_NEGATIVE: |
| 6382 | push_text("-"); |
| 6383 | break; |
| 6384 | case UnaryOpNode::OP_LOGIC_NOT: |
| 6385 | push_text("NOT"); |
| 6386 | break; |
| 6387 | case UnaryOpNode::OP_COMPLEMENT: |
| 6388 | push_text("~"); |
| 6389 | break; |
| 6390 | } |
| 6391 | print_expression(p_unary_op->operand); |
| 6392 | // Surround in parenthesis for disambiguation. |
| 6393 | push_text(")"); |
| 6394 | } |
| 6395 | |
| 6396 | void GDScriptParser::TreePrinter::print_variable(VariableNode *p_variable) { |
| 6397 | for (const AnnotationNode *E : p_variable->annotations) { |
| 6398 | print_annotation(E); |
| 6399 | } |
| 6400 | |
| 6401 | if (p_variable->is_static) { |
| 6402 | push_text("Static "); |
| 6403 | } |
| 6404 | push_text("Variable "); |
| 6405 | print_identifier(p_variable->identifier); |
| 6406 | |
| 6407 | push_text(" : "); |
| 6408 | if (p_variable->datatype_specifier != nullptr) { |
| 6409 | print_type(p_variable->datatype_specifier); |
| 6410 | } else if (p_variable->infer_datatype) { |
| 6411 | push_text("<inferred type>"); |
| 6412 | } else { |
| 6413 | push_text("Variant"); |
| 6414 | } |
| 6415 | |
| 6416 | increase_indent(); |
| 6417 | |
| 6418 | push_line(); |
| 6419 | push_text("= "); |
| 6420 | if (p_variable->initializer == nullptr) { |
| 6421 | push_text("<default value>"); |
| 6422 | } else { |
| 6423 | print_expression(p_variable->initializer); |
| 6424 | } |
| 6425 | push_line(); |
| 6426 | |
| 6427 | if (p_variable->property != VariableNode::PROP_NONE) { |
| 6428 | if (p_variable->getter != nullptr) { |
| 6429 | push_text("Get"); |
| 6430 | if (p_variable->property == VariableNode::PROP_INLINE) { |
| 6431 | push_line(":"); |
| 6432 | increase_indent(); |
| 6433 | print_suite(p_variable->getter->body); |
| 6434 | decrease_indent(); |
| 6435 | } else { |
| 6436 | push_line(" ="); |
| 6437 | increase_indent(); |
| 6438 | print_identifier(p_variable->getter_pointer); |
| 6439 | push_line(); |
| 6440 | decrease_indent(); |
| 6441 | } |
| 6442 | } |
| 6443 | if (p_variable->setter != nullptr) { |
| 6444 | push_text("Set ("); |
| 6445 | if (p_variable->property == VariableNode::PROP_INLINE) { |
| 6446 | if (p_variable->setter_parameter != nullptr) { |
| 6447 | print_identifier(p_variable->setter_parameter); |
| 6448 | } else { |
| 6449 | push_text("<missing>"); |
| 6450 | } |
| 6451 | push_line("):"); |
| 6452 | increase_indent(); |
| 6453 | print_suite(p_variable->setter->body); |
| 6454 | decrease_indent(); |
| 6455 | } else { |
| 6456 | push_line(" ="); |
| 6457 | increase_indent(); |
| 6458 | print_identifier(p_variable->setter_pointer); |
| 6459 | push_line(); |
| 6460 | decrease_indent(); |
| 6461 | } |
| 6462 | } |
| 6463 | } |
| 6464 | |
| 6465 | decrease_indent(); |
| 6466 | push_line(); |
| 6467 | } |
| 6468 | |
| 6469 | void GDScriptParser::TreePrinter::print_while(WhileNode *p_while) { |
| 6470 | push_text("While "); |
| 6471 | print_expression(p_while->condition); |
| 6472 | push_line(" :"); |
| 6473 | |
| 6474 | increase_indent(); |
| 6475 | print_suite(p_while->loop); |
| 6476 | decrease_indent(); |
| 6477 | } |
| 6478 | |
| 6479 | void GDScriptParser::TreePrinter::print_tree(const GDScriptParser &p_parser) { |
| 6480 | ClassNode *class_tree = p_parser.get_tree(); |
| 6481 | ERR_FAIL_NULL_MSG(class_tree, "Parse the code before printing the parse tree."); |
| 6482 | |
| 6483 | if (p_parser.is_tool()) { |
| 6484 | push_line("@tool"); |
| 6485 | } |
| 6486 | if (!class_tree->icon_path.is_empty()) { |
| 6487 | push_text(R"(@icon (")"); |
| 6488 | push_text(class_tree->icon_path); |
| 6489 | push_line("\")"); |
| 6490 | } |
| 6491 | print_class(class_tree); |
| 6492 | |
| 6493 | print_line(String(printed)); |
| 6494 | } |
| 6495 | |
| 6496 | #endif // DEBUG_ENABLED |