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