Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_parser.h
10277 views
1
/**************************************************************************/
2
/* gdscript_parser.h */
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
#pragma once
32
33
#include "gdscript_cache.h"
34
#include "gdscript_tokenizer.h"
35
36
#ifdef DEBUG_ENABLED
37
#include "gdscript_warning.h"
38
#endif
39
40
#include "core/io/resource.h"
41
#include "core/object/ref_counted.h"
42
#include "core/object/script_language.h"
43
#include "core/string/string_name.h"
44
#include "core/string/ustring.h"
45
#include "core/templates/hash_map.h"
46
#include "core/templates/list.h"
47
#include "core/templates/vector.h"
48
#include "core/variant/variant.h"
49
50
#ifdef DEBUG_ENABLED
51
#include "core/string/string_builder.h"
52
#endif
53
54
class GDScriptParser {
55
struct AnnotationInfo;
56
57
public:
58
// Forward-declare all parser nodes, to avoid ordering issues.
59
struct AnnotationNode;
60
struct ArrayNode;
61
struct AssertNode;
62
struct AssignableNode;
63
struct AssignmentNode;
64
struct AwaitNode;
65
struct BinaryOpNode;
66
struct BreakNode;
67
struct BreakpointNode;
68
struct CallNode;
69
struct CastNode;
70
struct ClassNode;
71
struct ConstantNode;
72
struct ContinueNode;
73
struct DictionaryNode;
74
struct EnumNode;
75
struct ExpressionNode;
76
struct ForNode;
77
struct FunctionNode;
78
struct GetNodeNode;
79
struct IdentifierNode;
80
struct IfNode;
81
struct LambdaNode;
82
struct LiteralNode;
83
struct MatchNode;
84
struct MatchBranchNode;
85
struct ParameterNode;
86
struct PassNode;
87
struct PatternNode;
88
struct PreloadNode;
89
struct ReturnNode;
90
struct SelfNode;
91
struct SignalNode;
92
struct SubscriptNode;
93
struct SuiteNode;
94
struct TernaryOpNode;
95
struct TypeNode;
96
struct TypeTestNode;
97
struct UnaryOpNode;
98
struct VariableNode;
99
struct WhileNode;
100
101
class DataType {
102
public:
103
Vector<DataType> container_element_types;
104
105
enum Kind {
106
BUILTIN,
107
NATIVE,
108
SCRIPT,
109
CLASS, // GDScript.
110
ENUM, // Enumeration.
111
VARIANT, // Can be any type.
112
RESOLVING, // Currently resolving.
113
UNRESOLVED,
114
};
115
Kind kind = UNRESOLVED;
116
117
enum TypeSource {
118
UNDETECTED, // Can be any type.
119
INFERRED, // Has inferred type, but still dynamic.
120
ANNOTATED_EXPLICIT, // Has a specific type annotated.
121
ANNOTATED_INFERRED, // Has a static type but comes from the assigned value.
122
};
123
TypeSource type_source = UNDETECTED;
124
125
bool is_constant = false;
126
bool is_read_only = false;
127
bool is_meta_type = false;
128
bool is_pseudo_type = false; // For global names that can't be used standalone.
129
bool is_coroutine = false; // For function calls.
130
131
Variant::Type builtin_type = Variant::NIL;
132
StringName native_type;
133
StringName enum_type; // Enum name or the value name in an enum.
134
Ref<Script> script_type;
135
String script_path;
136
ClassNode *class_type = nullptr;
137
138
MethodInfo method_info; // For callable/signals.
139
HashMap<StringName, int64_t> enum_values; // For enums.
140
141
_FORCE_INLINE_ bool is_set() const { return kind != RESOLVING && kind != UNRESOLVED; }
142
_FORCE_INLINE_ bool is_resolving() const { return kind == RESOLVING; }
143
_FORCE_INLINE_ bool has_no_type() const { return type_source == UNDETECTED; }
144
_FORCE_INLINE_ bool is_variant() const { return kind == VARIANT || kind == RESOLVING || kind == UNRESOLVED; }
145
_FORCE_INLINE_ bool is_hard_type() const { return type_source > INFERRED; }
146
147
String to_string() const;
148
_FORCE_INLINE_ String to_string_strict() const { return is_hard_type() ? to_string() : "Variant"; }
149
PropertyInfo to_property_info(const String &p_name) const;
150
151
_FORCE_INLINE_ static DataType get_variant_type() { // Default DataType for container elements.
152
DataType datatype;
153
datatype.kind = VARIANT;
154
datatype.type_source = INFERRED;
155
return datatype;
156
}
157
158
_FORCE_INLINE_ void set_container_element_type(int p_index, const DataType &p_type) {
159
ERR_FAIL_COND(p_index < 0);
160
while (p_index >= container_element_types.size()) {
161
container_element_types.push_back(get_variant_type());
162
}
163
container_element_types.write[p_index] = DataType(p_type);
164
}
165
166
_FORCE_INLINE_ int get_container_element_type_count() const {
167
return container_element_types.size();
168
}
169
170
_FORCE_INLINE_ DataType get_container_element_type(int p_index) const {
171
ERR_FAIL_INDEX_V(p_index, container_element_types.size(), get_variant_type());
172
return container_element_types[p_index];
173
}
174
175
_FORCE_INLINE_ DataType get_container_element_type_or_variant(int p_index) const {
176
if (p_index < 0 || p_index >= container_element_types.size()) {
177
return get_variant_type();
178
}
179
return container_element_types[p_index];
180
}
181
182
_FORCE_INLINE_ bool has_container_element_type(int p_index) const {
183
return p_index >= 0 && p_index < container_element_types.size();
184
}
185
186
_FORCE_INLINE_ bool has_container_element_types() const {
187
return !container_element_types.is_empty();
188
}
189
190
bool is_typed_container_type() const;
191
192
GDScriptParser::DataType get_typed_container_type() const;
193
194
bool can_reference(const DataType &p_other) const;
195
196
bool operator==(const DataType &p_other) const {
197
if (type_source == UNDETECTED || p_other.type_source == UNDETECTED) {
198
return true; // Can be considered equal for parsing purposes.
199
}
200
201
if (type_source == INFERRED || p_other.type_source == INFERRED) {
202
return true; // Can be considered equal for parsing purposes.
203
}
204
205
if (kind != p_other.kind) {
206
return false;
207
}
208
209
switch (kind) {
210
case VARIANT:
211
return true; // All variants are the same.
212
case BUILTIN:
213
return builtin_type == p_other.builtin_type;
214
case NATIVE:
215
case ENUM: // Enums use native_type to identify the enum and its base class.
216
return native_type == p_other.native_type;
217
case SCRIPT:
218
return script_type == p_other.script_type;
219
case CLASS:
220
return class_type == p_other.class_type || class_type->fqcn == p_other.class_type->fqcn;
221
case RESOLVING:
222
case UNRESOLVED:
223
break;
224
}
225
226
return false;
227
}
228
229
bool operator!=(const DataType &p_other) const {
230
return !(*this == p_other);
231
}
232
233
void operator=(const DataType &p_other) {
234
kind = p_other.kind;
235
type_source = p_other.type_source;
236
is_read_only = p_other.is_read_only;
237
is_constant = p_other.is_constant;
238
is_meta_type = p_other.is_meta_type;
239
is_pseudo_type = p_other.is_pseudo_type;
240
is_coroutine = p_other.is_coroutine;
241
builtin_type = p_other.builtin_type;
242
native_type = p_other.native_type;
243
enum_type = p_other.enum_type;
244
script_type = p_other.script_type;
245
script_path = p_other.script_path;
246
class_type = p_other.class_type;
247
method_info = p_other.method_info;
248
enum_values = p_other.enum_values;
249
container_element_types = p_other.container_element_types;
250
}
251
252
DataType() = default;
253
254
DataType(const DataType &p_other) {
255
*this = p_other;
256
}
257
258
~DataType() {}
259
};
260
261
struct ParserError {
262
// TODO: Do I really need a "type"?
263
// enum Type {
264
// NO_ERROR,
265
// EMPTY_FILE,
266
// CLASS_NAME_USED_TWICE,
267
// EXTENDS_USED_TWICE,
268
// EXPECTED_END_STATEMENT,
269
// };
270
// Type type = NO_ERROR;
271
String message;
272
int line = 0, column = 0;
273
};
274
275
#ifdef TOOLS_ENABLED
276
struct ClassDocData {
277
String brief;
278
String description;
279
Vector<Pair<String, String>> tutorials;
280
bool is_deprecated = false;
281
String deprecated_message;
282
bool is_experimental = false;
283
String experimental_message;
284
};
285
286
struct MemberDocData {
287
String description;
288
bool is_deprecated = false;
289
String deprecated_message;
290
bool is_experimental = false;
291
String experimental_message;
292
};
293
#endif // TOOLS_ENABLED
294
295
struct Node {
296
enum Type {
297
NONE,
298
ANNOTATION,
299
ARRAY,
300
ASSERT,
301
ASSIGNMENT,
302
AWAIT,
303
BINARY_OPERATOR,
304
BREAK,
305
BREAKPOINT,
306
CALL,
307
CAST,
308
CLASS,
309
CONSTANT,
310
CONTINUE,
311
DICTIONARY,
312
ENUM,
313
FOR,
314
FUNCTION,
315
GET_NODE,
316
IDENTIFIER,
317
IF,
318
LAMBDA,
319
LITERAL,
320
MATCH,
321
MATCH_BRANCH,
322
PARAMETER,
323
PASS,
324
PATTERN,
325
PRELOAD,
326
RETURN,
327
SELF,
328
SIGNAL,
329
SUBSCRIPT,
330
SUITE,
331
TERNARY_OPERATOR,
332
TYPE,
333
TYPE_TEST,
334
UNARY_OPERATOR,
335
VARIABLE,
336
WHILE,
337
};
338
339
Type type = NONE;
340
int start_line = 0, end_line = 0;
341
int start_column = 0, end_column = 0;
342
Node *next = nullptr;
343
List<AnnotationNode *> annotations;
344
345
DataType datatype;
346
347
virtual DataType get_datatype() const { return datatype; }
348
virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
349
350
virtual bool is_expression() const { return false; }
351
352
virtual ~Node() {}
353
};
354
355
struct ExpressionNode : public Node {
356
// Base type for all expression kinds.
357
bool reduced = false;
358
bool is_constant = false;
359
Variant reduced_value;
360
361
virtual bool is_expression() const override { return true; }
362
virtual ~ExpressionNode() {}
363
364
protected:
365
ExpressionNode() {}
366
};
367
368
struct AnnotationNode : public Node {
369
StringName name;
370
Vector<ExpressionNode *> arguments;
371
Vector<Variant> resolved_arguments;
372
373
/** Information of the annotation. Might be null for unknown annotations. */
374
AnnotationInfo *info = nullptr;
375
PropertyInfo export_info;
376
bool is_resolved = false;
377
bool is_applied = false;
378
379
bool apply(GDScriptParser *p_this, Node *p_target, ClassNode *p_class);
380
bool applies_to(uint32_t p_target_kinds) const;
381
382
AnnotationNode() {
383
type = ANNOTATION;
384
}
385
};
386
387
struct ArrayNode : public ExpressionNode {
388
Vector<ExpressionNode *> elements;
389
390
ArrayNode() {
391
type = ARRAY;
392
}
393
};
394
395
struct AssertNode : public Node {
396
ExpressionNode *condition = nullptr;
397
ExpressionNode *message = nullptr;
398
399
AssertNode() {
400
type = ASSERT;
401
}
402
};
403
404
struct AssignableNode : public Node {
405
IdentifierNode *identifier = nullptr;
406
ExpressionNode *initializer = nullptr;
407
TypeNode *datatype_specifier = nullptr;
408
bool infer_datatype = false;
409
bool use_conversion_assign = false;
410
int usages = 0;
411
412
virtual ~AssignableNode() {}
413
414
protected:
415
AssignableNode() {}
416
};
417
418
struct AssignmentNode : public ExpressionNode {
419
// Assignment is not really an expression but it's easier to parse as if it were.
420
enum Operation {
421
OP_NONE,
422
OP_ADDITION,
423
OP_SUBTRACTION,
424
OP_MULTIPLICATION,
425
OP_DIVISION,
426
OP_MODULO,
427
OP_POWER,
428
OP_BIT_SHIFT_LEFT,
429
OP_BIT_SHIFT_RIGHT,
430
OP_BIT_AND,
431
OP_BIT_OR,
432
OP_BIT_XOR,
433
};
434
435
Operation operation = OP_NONE;
436
Variant::Operator variant_op = Variant::OP_MAX;
437
ExpressionNode *assignee = nullptr;
438
ExpressionNode *assigned_value = nullptr;
439
bool use_conversion_assign = false;
440
441
AssignmentNode() {
442
type = ASSIGNMENT;
443
}
444
};
445
446
struct AwaitNode : public ExpressionNode {
447
ExpressionNode *to_await = nullptr;
448
449
AwaitNode() {
450
type = AWAIT;
451
}
452
};
453
454
struct BinaryOpNode : public ExpressionNode {
455
enum OpType {
456
OP_ADDITION,
457
OP_SUBTRACTION,
458
OP_MULTIPLICATION,
459
OP_DIVISION,
460
OP_MODULO,
461
OP_POWER,
462
OP_BIT_LEFT_SHIFT,
463
OP_BIT_RIGHT_SHIFT,
464
OP_BIT_AND,
465
OP_BIT_OR,
466
OP_BIT_XOR,
467
OP_LOGIC_AND,
468
OP_LOGIC_OR,
469
OP_CONTENT_TEST,
470
OP_COMP_EQUAL,
471
OP_COMP_NOT_EQUAL,
472
OP_COMP_LESS,
473
OP_COMP_LESS_EQUAL,
474
OP_COMP_GREATER,
475
OP_COMP_GREATER_EQUAL,
476
};
477
478
OpType operation = OpType::OP_ADDITION;
479
Variant::Operator variant_op = Variant::OP_MAX;
480
ExpressionNode *left_operand = nullptr;
481
ExpressionNode *right_operand = nullptr;
482
483
BinaryOpNode() {
484
type = BINARY_OPERATOR;
485
}
486
};
487
488
struct BreakNode : public Node {
489
BreakNode() {
490
type = BREAK;
491
}
492
};
493
494
struct BreakpointNode : public Node {
495
BreakpointNode() {
496
type = BREAKPOINT;
497
}
498
};
499
500
struct CallNode : public ExpressionNode {
501
ExpressionNode *callee = nullptr;
502
Vector<ExpressionNode *> arguments;
503
StringName function_name;
504
bool is_super = false;
505
bool is_static = false;
506
507
CallNode() {
508
type = CALL;
509
}
510
511
Type get_callee_type() const {
512
if (callee == nullptr) {
513
return Type::NONE;
514
} else {
515
return callee->type;
516
}
517
}
518
};
519
520
struct CastNode : public ExpressionNode {
521
ExpressionNode *operand = nullptr;
522
TypeNode *cast_type = nullptr;
523
524
CastNode() {
525
type = CAST;
526
}
527
};
528
529
struct EnumNode : public Node {
530
struct Value {
531
IdentifierNode *identifier = nullptr;
532
ExpressionNode *custom_value = nullptr;
533
EnumNode *parent_enum = nullptr;
534
int index = -1;
535
bool resolved = false;
536
int64_t value = 0;
537
int line = 0;
538
int start_column = 0;
539
int end_column = 0;
540
#ifdef TOOLS_ENABLED
541
MemberDocData doc_data;
542
#endif // TOOLS_ENABLED
543
};
544
545
IdentifierNode *identifier = nullptr;
546
Vector<Value> values;
547
Variant dictionary;
548
#ifdef TOOLS_ENABLED
549
MemberDocData doc_data;
550
#endif // TOOLS_ENABLED
551
552
EnumNode() {
553
type = ENUM;
554
}
555
};
556
557
struct ClassNode : public Node {
558
struct Member {
559
enum Type {
560
UNDEFINED,
561
CLASS,
562
CONSTANT,
563
FUNCTION,
564
SIGNAL,
565
VARIABLE,
566
ENUM,
567
ENUM_VALUE, // For unnamed enums.
568
GROUP, // For member grouping.
569
};
570
571
Type type = UNDEFINED;
572
573
union {
574
ClassNode *m_class = nullptr;
575
ConstantNode *constant;
576
FunctionNode *function;
577
SignalNode *signal;
578
VariableNode *variable;
579
EnumNode *m_enum;
580
AnnotationNode *annotation;
581
};
582
EnumNode::Value enum_value;
583
584
String get_name() const {
585
switch (type) {
586
case UNDEFINED:
587
return "<undefined member>";
588
case CLASS:
589
// All class-type members have an id.
590
return m_class->identifier->name;
591
case CONSTANT:
592
return constant->identifier->name;
593
case FUNCTION:
594
return function->identifier->name;
595
case SIGNAL:
596
return signal->identifier->name;
597
case VARIABLE:
598
return variable->identifier->name;
599
case ENUM:
600
// All enum-type members have an id.
601
return m_enum->identifier->name;
602
case ENUM_VALUE:
603
return enum_value.identifier->name;
604
case GROUP:
605
return annotation->export_info.name;
606
}
607
return "";
608
}
609
610
String get_type_name() const {
611
switch (type) {
612
case UNDEFINED:
613
return "???";
614
case CLASS:
615
return "class";
616
case CONSTANT:
617
return "constant";
618
case FUNCTION:
619
return "function";
620
case SIGNAL:
621
return "signal";
622
case VARIABLE:
623
return "variable";
624
case ENUM:
625
return "enum";
626
case ENUM_VALUE:
627
return "enum value";
628
case GROUP:
629
return "group";
630
}
631
return "";
632
}
633
634
int get_line() const {
635
switch (type) {
636
case CLASS:
637
return m_class->start_line;
638
case CONSTANT:
639
return constant->start_line;
640
case FUNCTION:
641
return function->start_line;
642
case VARIABLE:
643
return variable->start_line;
644
case ENUM_VALUE:
645
return enum_value.line;
646
case ENUM:
647
return m_enum->start_line;
648
case SIGNAL:
649
return signal->start_line;
650
case GROUP:
651
return annotation->start_line;
652
case UNDEFINED:
653
ERR_FAIL_V_MSG(-1, "Reaching undefined member type.");
654
}
655
ERR_FAIL_V_MSG(-1, "Reaching unhandled type.");
656
}
657
658
DataType get_datatype() const {
659
switch (type) {
660
case CLASS:
661
return m_class->get_datatype();
662
case CONSTANT:
663
return constant->get_datatype();
664
case FUNCTION:
665
return function->get_datatype();
666
case VARIABLE:
667
return variable->get_datatype();
668
case ENUM:
669
return m_enum->get_datatype();
670
case ENUM_VALUE:
671
return enum_value.identifier->get_datatype();
672
case SIGNAL:
673
return signal->get_datatype();
674
case GROUP:
675
return DataType();
676
case UNDEFINED:
677
return DataType();
678
}
679
ERR_FAIL_V_MSG(DataType(), "Reaching unhandled type.");
680
}
681
682
Node *get_source_node() const {
683
switch (type) {
684
case CLASS:
685
return m_class;
686
case CONSTANT:
687
return constant;
688
case FUNCTION:
689
return function;
690
case VARIABLE:
691
return variable;
692
case ENUM:
693
return m_enum;
694
case ENUM_VALUE:
695
return enum_value.identifier;
696
case SIGNAL:
697
return signal;
698
case GROUP:
699
return annotation;
700
case UNDEFINED:
701
return nullptr;
702
}
703
ERR_FAIL_V_MSG(nullptr, "Reaching unhandled type.");
704
}
705
706
Member() {}
707
708
Member(ClassNode *p_class) {
709
type = CLASS;
710
m_class = p_class;
711
}
712
Member(ConstantNode *p_constant) {
713
type = CONSTANT;
714
constant = p_constant;
715
}
716
Member(VariableNode *p_variable) {
717
type = VARIABLE;
718
variable = p_variable;
719
}
720
Member(SignalNode *p_signal) {
721
type = SIGNAL;
722
signal = p_signal;
723
}
724
Member(FunctionNode *p_function) {
725
type = FUNCTION;
726
function = p_function;
727
}
728
Member(EnumNode *p_enum) {
729
type = ENUM;
730
m_enum = p_enum;
731
}
732
Member(const EnumNode::Value &p_enum_value) {
733
type = ENUM_VALUE;
734
enum_value = p_enum_value;
735
}
736
Member(AnnotationNode *p_annotation) {
737
type = GROUP;
738
annotation = p_annotation;
739
}
740
};
741
742
IdentifierNode *identifier = nullptr;
743
String icon_path;
744
String simplified_icon_path;
745
Vector<Member> members;
746
HashMap<StringName, int> members_indices;
747
ClassNode *outer = nullptr;
748
bool extends_used = false;
749
bool onready_used = false;
750
bool is_abstract = false;
751
bool has_static_data = false;
752
bool annotated_static_unload = false;
753
String extends_path;
754
Vector<IdentifierNode *> extends; // List for indexing: extends A.B.C
755
DataType base_type;
756
String fqcn; // Fully-qualified class name. Identifies uniquely any class in the project.
757
#ifdef TOOLS_ENABLED
758
ClassDocData doc_data;
759
760
// EnumValue docs are parsed after itself, so we need a method to add/modify the doc property later.
761
void set_enum_value_doc_data(const StringName &p_name, const MemberDocData &p_doc_data) {
762
ERR_FAIL_INDEX(members_indices[p_name], members.size());
763
members.write[members_indices[p_name]].enum_value.doc_data = p_doc_data;
764
}
765
#endif // TOOLS_ENABLED
766
767
bool resolved_interface = false;
768
bool resolved_body = false;
769
770
StringName get_global_name() const {
771
return (outer == nullptr && identifier != nullptr) ? identifier->name : StringName();
772
}
773
774
Member get_member(const StringName &p_name) const {
775
return members[members_indices[p_name]];
776
}
777
bool has_member(const StringName &p_name) const {
778
return members_indices.has(p_name);
779
}
780
bool has_function(const StringName &p_name) const {
781
return has_member(p_name) && members[members_indices[p_name]].type == Member::FUNCTION;
782
}
783
template <typename T>
784
void add_member(T *p_member_node) {
785
members_indices[p_member_node->identifier->name] = members.size();
786
members.push_back(Member(p_member_node));
787
}
788
void add_member(const EnumNode::Value &p_enum_value) {
789
members_indices[p_enum_value.identifier->name] = members.size();
790
members.push_back(Member(p_enum_value));
791
}
792
void add_member_group(AnnotationNode *p_annotation_node) {
793
// Avoid name conflict. See GH-78252.
794
StringName name = vformat("@group_%d_%s", members.size(), p_annotation_node->export_info.name);
795
members_indices[name] = members.size();
796
members.push_back(Member(p_annotation_node));
797
}
798
799
ClassNode() {
800
type = CLASS;
801
}
802
};
803
804
struct ConstantNode : public AssignableNode {
805
#ifdef TOOLS_ENABLED
806
MemberDocData doc_data;
807
#endif // TOOLS_ENABLED
808
809
ConstantNode() {
810
type = CONSTANT;
811
}
812
};
813
814
struct ContinueNode : public Node {
815
ContinueNode() {
816
type = CONTINUE;
817
}
818
};
819
820
struct DictionaryNode : public ExpressionNode {
821
struct Pair {
822
ExpressionNode *key = nullptr;
823
ExpressionNode *value = nullptr;
824
};
825
Vector<Pair> elements;
826
827
enum Style {
828
LUA_TABLE,
829
PYTHON_DICT,
830
};
831
Style style = PYTHON_DICT;
832
833
DictionaryNode() {
834
type = DICTIONARY;
835
}
836
};
837
838
struct ForNode : public Node {
839
IdentifierNode *variable = nullptr;
840
TypeNode *datatype_specifier = nullptr;
841
bool use_conversion_assign = false;
842
ExpressionNode *list = nullptr;
843
SuiteNode *loop = nullptr;
844
845
ForNode() {
846
type = FOR;
847
}
848
};
849
850
struct FunctionNode : public Node {
851
IdentifierNode *identifier = nullptr;
852
Vector<ParameterNode *> parameters;
853
HashMap<StringName, int> parameters_indices;
854
ParameterNode *rest_parameter = nullptr;
855
TypeNode *return_type = nullptr;
856
SuiteNode *body = nullptr;
857
bool is_abstract = false;
858
bool is_static = false; // For lambdas it's determined in the analyzer.
859
bool is_coroutine = false;
860
Variant rpc_config;
861
MethodInfo info;
862
LambdaNode *source_lambda = nullptr;
863
Vector<Variant> default_arg_values;
864
#ifdef TOOLS_ENABLED
865
MemberDocData doc_data;
866
int min_local_doc_line = 0;
867
String signature; // For autocompletion.
868
#endif // TOOLS_ENABLED
869
870
bool resolved_signature = false;
871
bool resolved_body = false;
872
873
_FORCE_INLINE_ bool is_vararg() const { return rest_parameter != nullptr; }
874
875
FunctionNode() {
876
type = FUNCTION;
877
}
878
};
879
880
struct GetNodeNode : public ExpressionNode {
881
String full_path;
882
bool use_dollar = true;
883
884
GetNodeNode() {
885
type = GET_NODE;
886
}
887
};
888
889
struct IdentifierNode : public ExpressionNode {
890
StringName name;
891
SuiteNode *suite = nullptr; // The block in which the identifier is used.
892
893
enum Source {
894
UNDEFINED_SOURCE,
895
FUNCTION_PARAMETER,
896
LOCAL_VARIABLE,
897
LOCAL_CONSTANT,
898
LOCAL_ITERATOR, // `for` loop iterator.
899
LOCAL_BIND, // Pattern bind.
900
MEMBER_VARIABLE,
901
MEMBER_CONSTANT,
902
MEMBER_FUNCTION,
903
MEMBER_SIGNAL,
904
MEMBER_CLASS,
905
INHERITED_VARIABLE,
906
STATIC_VARIABLE,
907
NATIVE_CLASS,
908
};
909
Source source = UNDEFINED_SOURCE;
910
911
union {
912
ParameterNode *parameter_source = nullptr;
913
IdentifierNode *bind_source;
914
VariableNode *variable_source;
915
ConstantNode *constant_source;
916
SignalNode *signal_source;
917
FunctionNode *function_source;
918
};
919
bool function_source_is_static = false; // For non-GDScript scripts.
920
921
FunctionNode *source_function = nullptr; // TODO: Rename to disambiguate `function_source`.
922
923
int usages = 0; // Useful for binds/iterator variable.
924
925
IdentifierNode() {
926
type = IDENTIFIER;
927
}
928
};
929
930
struct IfNode : public Node {
931
ExpressionNode *condition = nullptr;
932
SuiteNode *true_block = nullptr;
933
SuiteNode *false_block = nullptr;
934
935
IfNode() {
936
type = IF;
937
}
938
};
939
940
struct LambdaNode : public ExpressionNode {
941
FunctionNode *function = nullptr;
942
FunctionNode *parent_function = nullptr;
943
LambdaNode *parent_lambda = nullptr;
944
Vector<IdentifierNode *> captures;
945
HashMap<StringName, int> captures_indices;
946
bool use_self = false;
947
948
bool has_name() const {
949
return function && function->identifier;
950
}
951
952
LambdaNode() {
953
type = LAMBDA;
954
}
955
};
956
957
struct LiteralNode : public ExpressionNode {
958
Variant value;
959
960
LiteralNode() {
961
type = LITERAL;
962
}
963
};
964
965
struct MatchNode : public Node {
966
ExpressionNode *test = nullptr;
967
Vector<MatchBranchNode *> branches;
968
969
MatchNode() {
970
type = MATCH;
971
}
972
};
973
974
struct MatchBranchNode : public Node {
975
Vector<PatternNode *> patterns;
976
SuiteNode *block = nullptr;
977
bool has_wildcard = false;
978
SuiteNode *guard_body = nullptr;
979
980
MatchBranchNode() {
981
type = MATCH_BRANCH;
982
}
983
};
984
985
struct ParameterNode : public AssignableNode {
986
ParameterNode() {
987
type = PARAMETER;
988
}
989
};
990
991
struct PassNode : public Node {
992
PassNode() {
993
type = PASS;
994
}
995
};
996
997
struct PatternNode : public Node {
998
enum Type {
999
PT_LITERAL,
1000
PT_EXPRESSION,
1001
PT_BIND,
1002
PT_ARRAY,
1003
PT_DICTIONARY,
1004
PT_REST,
1005
PT_WILDCARD,
1006
};
1007
Type pattern_type = PT_LITERAL;
1008
1009
union {
1010
LiteralNode *literal = nullptr;
1011
IdentifierNode *bind;
1012
ExpressionNode *expression;
1013
};
1014
Vector<PatternNode *> array;
1015
bool rest_used = false; // For array/dict patterns.
1016
1017
struct Pair {
1018
ExpressionNode *key = nullptr;
1019
PatternNode *value_pattern = nullptr;
1020
};
1021
Vector<Pair> dictionary;
1022
1023
HashMap<StringName, IdentifierNode *> binds;
1024
1025
bool has_bind(const StringName &p_name);
1026
IdentifierNode *get_bind(const StringName &p_name);
1027
1028
PatternNode() {
1029
type = PATTERN;
1030
}
1031
};
1032
struct PreloadNode : public ExpressionNode {
1033
ExpressionNode *path = nullptr;
1034
String resolved_path;
1035
Ref<Resource> resource;
1036
1037
PreloadNode() {
1038
type = PRELOAD;
1039
}
1040
};
1041
1042
struct ReturnNode : public Node {
1043
ExpressionNode *return_value = nullptr;
1044
bool void_return = false;
1045
1046
ReturnNode() {
1047
type = RETURN;
1048
}
1049
};
1050
1051
struct SelfNode : public ExpressionNode {
1052
ClassNode *current_class = nullptr;
1053
1054
SelfNode() {
1055
type = SELF;
1056
}
1057
};
1058
1059
struct SignalNode : public Node {
1060
IdentifierNode *identifier = nullptr;
1061
Vector<ParameterNode *> parameters;
1062
HashMap<StringName, int> parameters_indices;
1063
MethodInfo method_info;
1064
#ifdef TOOLS_ENABLED
1065
MemberDocData doc_data;
1066
#endif // TOOLS_ENABLED
1067
1068
int usages = 0;
1069
1070
SignalNode() {
1071
type = SIGNAL;
1072
}
1073
};
1074
1075
struct SubscriptNode : public ExpressionNode {
1076
ExpressionNode *base = nullptr;
1077
union {
1078
ExpressionNode *index = nullptr;
1079
IdentifierNode *attribute;
1080
};
1081
1082
bool is_attribute = false;
1083
1084
SubscriptNode() {
1085
type = SUBSCRIPT;
1086
}
1087
};
1088
1089
struct SuiteNode : public Node {
1090
SuiteNode *parent_block = nullptr;
1091
Vector<Node *> statements;
1092
struct Local {
1093
enum Type {
1094
UNDEFINED,
1095
CONSTANT,
1096
VARIABLE,
1097
PARAMETER,
1098
FOR_VARIABLE,
1099
PATTERN_BIND,
1100
};
1101
Type type = UNDEFINED;
1102
union {
1103
ConstantNode *constant = nullptr;
1104
VariableNode *variable;
1105
ParameterNode *parameter;
1106
IdentifierNode *bind;
1107
};
1108
StringName name;
1109
FunctionNode *source_function = nullptr;
1110
1111
int start_line = 0, end_line = 0;
1112
int start_column = 0, end_column = 0;
1113
1114
DataType get_datatype() const;
1115
String get_name() const;
1116
1117
Local() {}
1118
Local(ConstantNode *p_constant, FunctionNode *p_source_function) {
1119
type = CONSTANT;
1120
constant = p_constant;
1121
name = p_constant->identifier->name;
1122
source_function = p_source_function;
1123
1124
start_line = p_constant->start_line;
1125
end_line = p_constant->end_line;
1126
start_column = p_constant->start_column;
1127
end_column = p_constant->end_column;
1128
}
1129
Local(VariableNode *p_variable, FunctionNode *p_source_function) {
1130
type = VARIABLE;
1131
variable = p_variable;
1132
name = p_variable->identifier->name;
1133
source_function = p_source_function;
1134
1135
start_line = p_variable->start_line;
1136
end_line = p_variable->end_line;
1137
start_column = p_variable->start_column;
1138
end_column = p_variable->end_column;
1139
}
1140
Local(ParameterNode *p_parameter, FunctionNode *p_source_function) {
1141
type = PARAMETER;
1142
parameter = p_parameter;
1143
name = p_parameter->identifier->name;
1144
source_function = p_source_function;
1145
1146
start_line = p_parameter->start_line;
1147
end_line = p_parameter->end_line;
1148
start_column = p_parameter->start_column;
1149
end_column = p_parameter->end_column;
1150
}
1151
Local(IdentifierNode *p_identifier, FunctionNode *p_source_function) {
1152
type = FOR_VARIABLE;
1153
bind = p_identifier;
1154
name = p_identifier->name;
1155
source_function = p_source_function;
1156
1157
start_line = p_identifier->start_line;
1158
end_line = p_identifier->end_line;
1159
start_column = p_identifier->start_column;
1160
end_column = p_identifier->end_column;
1161
}
1162
};
1163
Local empty;
1164
Vector<Local> locals;
1165
HashMap<StringName, int> locals_indices;
1166
1167
FunctionNode *parent_function = nullptr;
1168
IfNode *parent_if = nullptr;
1169
1170
bool has_return = false;
1171
bool has_continue = false;
1172
bool has_unreachable_code = false; // Just so warnings aren't given more than once per block.
1173
bool is_in_loop = false; // The block is nested in a loop (directly or indirectly).
1174
1175
bool has_local(const StringName &p_name) const;
1176
const Local &get_local(const StringName &p_name) const;
1177
template <typename T>
1178
void add_local(T *p_local, FunctionNode *p_source_function) {
1179
locals_indices[p_local->identifier->name] = locals.size();
1180
locals.push_back(Local(p_local, p_source_function));
1181
}
1182
void add_local(const Local &p_local) {
1183
locals_indices[p_local.name] = locals.size();
1184
locals.push_back(p_local);
1185
}
1186
1187
SuiteNode() {
1188
type = SUITE;
1189
}
1190
};
1191
1192
struct TernaryOpNode : public ExpressionNode {
1193
// Only one ternary operation exists, so no abstraction here.
1194
ExpressionNode *condition = nullptr;
1195
ExpressionNode *true_expr = nullptr;
1196
ExpressionNode *false_expr = nullptr;
1197
1198
TernaryOpNode() {
1199
type = TERNARY_OPERATOR;
1200
}
1201
};
1202
1203
struct TypeNode : public Node {
1204
Vector<IdentifierNode *> type_chain;
1205
Vector<TypeNode *> container_types;
1206
1207
TypeNode *get_container_type_or_null(int p_index) const {
1208
return p_index >= 0 && p_index < container_types.size() ? container_types[p_index] : nullptr;
1209
}
1210
1211
TypeNode() {
1212
type = TYPE;
1213
}
1214
};
1215
1216
struct TypeTestNode : public ExpressionNode {
1217
ExpressionNode *operand = nullptr;
1218
TypeNode *test_type = nullptr;
1219
DataType test_datatype;
1220
1221
TypeTestNode() {
1222
type = TYPE_TEST;
1223
}
1224
};
1225
1226
struct UnaryOpNode : public ExpressionNode {
1227
enum OpType {
1228
OP_POSITIVE,
1229
OP_NEGATIVE,
1230
OP_COMPLEMENT,
1231
OP_LOGIC_NOT,
1232
};
1233
1234
OpType operation = OP_POSITIVE;
1235
Variant::Operator variant_op = Variant::OP_MAX;
1236
ExpressionNode *operand = nullptr;
1237
1238
UnaryOpNode() {
1239
type = UNARY_OPERATOR;
1240
}
1241
};
1242
1243
struct VariableNode : public AssignableNode {
1244
enum PropertyStyle {
1245
PROP_NONE,
1246
PROP_INLINE,
1247
PROP_SETGET,
1248
};
1249
1250
PropertyStyle property = PROP_NONE;
1251
union {
1252
FunctionNode *setter = nullptr;
1253
IdentifierNode *setter_pointer;
1254
};
1255
IdentifierNode *setter_parameter = nullptr;
1256
union {
1257
FunctionNode *getter = nullptr;
1258
IdentifierNode *getter_pointer;
1259
};
1260
1261
bool exported = false;
1262
bool onready = false;
1263
PropertyInfo export_info;
1264
int assignments = 0;
1265
bool is_static = false;
1266
#ifdef TOOLS_ENABLED
1267
MemberDocData doc_data;
1268
#endif // TOOLS_ENABLED
1269
1270
VariableNode() {
1271
type = VARIABLE;
1272
}
1273
};
1274
1275
struct WhileNode : public Node {
1276
ExpressionNode *condition = nullptr;
1277
SuiteNode *loop = nullptr;
1278
1279
WhileNode() {
1280
type = WHILE;
1281
}
1282
};
1283
1284
enum CompletionType {
1285
COMPLETION_NONE,
1286
COMPLETION_ANNOTATION, // Annotation (following @).
1287
COMPLETION_ANNOTATION_ARGUMENTS, // Annotation arguments hint.
1288
COMPLETION_ASSIGN, // Assignment based on type (e.g. enum values).
1289
COMPLETION_ATTRIBUTE, // After id.| to look for members.
1290
COMPLETION_ATTRIBUTE_METHOD, // After id.| to look for methods.
1291
COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD, // Constants inside a built-in type (e.g. Color.BLUE) or static methods (e.g. Color.html).
1292
COMPLETION_CALL_ARGUMENTS, // Complete with nodes, input actions, enum values (or usual expressions).
1293
// TODO: COMPLETION_DECLARATION, // Potential declaration (var, const, func).
1294
COMPLETION_GET_NODE, // Get node with $ notation.
1295
COMPLETION_IDENTIFIER, // List available identifiers in scope.
1296
COMPLETION_INHERIT_TYPE, // Type after extends. Exclude non-viable types (built-ins, enums, void). Includes subtypes using the argument index.
1297
COMPLETION_METHOD, // List available methods in scope.
1298
COMPLETION_OVERRIDE_METHOD, // Override implementation, also for native virtuals.
1299
COMPLETION_PROPERTY_DECLARATION, // Property declaration (get, set).
1300
COMPLETION_PROPERTY_DECLARATION_OR_TYPE, // Property declaration (get, set) or a type hint.
1301
COMPLETION_PROPERTY_METHOD, // Property setter or getter (list available methods).
1302
COMPLETION_RESOURCE_PATH, // For load/preload.
1303
COMPLETION_SUBSCRIPT, // Inside id[|].
1304
COMPLETION_SUPER, // super(), used for lookup.
1305
COMPLETION_SUPER_METHOD, // After super.
1306
COMPLETION_TYPE_ATTRIBUTE, // Attribute in type name (Type.|).
1307
COMPLETION_TYPE_NAME, // Name of type (after :).
1308
COMPLETION_TYPE_NAME_OR_VOID, // Same as TYPE_NAME, but allows void (in function return type).
1309
};
1310
1311
struct CompletionCall {
1312
Node *call = nullptr;
1313
int argument = -1;
1314
};
1315
1316
struct CompletionContext {
1317
CompletionType type = COMPLETION_NONE;
1318
ClassNode *current_class = nullptr;
1319
FunctionNode *current_function = nullptr;
1320
SuiteNode *current_suite = nullptr;
1321
int current_line = -1;
1322
union {
1323
int current_argument = -1;
1324
int type_chain_index;
1325
};
1326
Variant::Type builtin_type = Variant::VARIANT_MAX;
1327
Node *node = nullptr;
1328
Object *base = nullptr;
1329
GDScriptParser *parser = nullptr;
1330
CompletionCall call;
1331
};
1332
1333
private:
1334
friend class GDScriptAnalyzer;
1335
friend class GDScriptParserRef;
1336
1337
bool _is_tool = false;
1338
String script_path;
1339
bool for_completion = false;
1340
bool parse_body = true;
1341
bool panic_mode = false;
1342
bool can_break = false;
1343
bool can_continue = false;
1344
List<bool> multiline_stack;
1345
HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
1346
1347
ClassNode *head = nullptr;
1348
Node *list = nullptr;
1349
List<ParserError> errors;
1350
1351
#ifdef DEBUG_ENABLED
1352
struct PendingWarning {
1353
const Node *source = nullptr;
1354
GDScriptWarning::Code code = GDScriptWarning::WARNING_MAX;
1355
bool treated_as_error = false;
1356
Vector<String> symbols;
1357
};
1358
1359
bool is_ignoring_warnings = false;
1360
List<GDScriptWarning> warnings;
1361
List<PendingWarning> pending_warnings;
1362
HashSet<int> warning_ignored_lines[GDScriptWarning::WARNING_MAX];
1363
int warning_ignore_start_lines[GDScriptWarning::WARNING_MAX];
1364
HashSet<int> unsafe_lines;
1365
#endif
1366
1367
GDScriptTokenizer *tokenizer = nullptr;
1368
GDScriptTokenizer::Token previous;
1369
GDScriptTokenizer::Token current;
1370
1371
ClassNode *current_class = nullptr;
1372
FunctionNode *current_function = nullptr;
1373
LambdaNode *current_lambda = nullptr;
1374
SuiteNode *current_suite = nullptr;
1375
1376
CompletionContext completion_context;
1377
List<CompletionCall> completion_call_stack;
1378
bool in_lambda = false;
1379
bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
1380
1381
typedef bool (GDScriptParser::*AnnotationAction)(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1382
struct AnnotationInfo {
1383
enum TargetKind {
1384
NONE = 0,
1385
SCRIPT = 1 << 0,
1386
CLASS = 1 << 1,
1387
VARIABLE = 1 << 2,
1388
CONSTANT = 1 << 3,
1389
SIGNAL = 1 << 4,
1390
FUNCTION = 1 << 5,
1391
STATEMENT = 1 << 6,
1392
STANDALONE = 1 << 7,
1393
CLASS_LEVEL = CLASS | VARIABLE | CONSTANT | SIGNAL | FUNCTION,
1394
};
1395
uint32_t target_kind = 0; // Flags.
1396
AnnotationAction apply = nullptr;
1397
MethodInfo info;
1398
};
1399
static HashMap<StringName, AnnotationInfo> valid_annotations;
1400
List<AnnotationNode *> annotation_stack;
1401
1402
typedef ExpressionNode *(GDScriptParser::*ParseFunction)(ExpressionNode *p_previous_operand, bool p_can_assign);
1403
// Higher value means higher precedence (i.e. is evaluated first).
1404
enum Precedence {
1405
PREC_NONE,
1406
PREC_ASSIGNMENT,
1407
PREC_CAST,
1408
PREC_TERNARY,
1409
PREC_LOGIC_OR,
1410
PREC_LOGIC_AND,
1411
PREC_LOGIC_NOT,
1412
PREC_CONTENT_TEST,
1413
PREC_COMPARISON,
1414
PREC_BIT_OR,
1415
PREC_BIT_XOR,
1416
PREC_BIT_AND,
1417
PREC_BIT_SHIFT,
1418
PREC_ADDITION_SUBTRACTION,
1419
PREC_FACTOR,
1420
PREC_SIGN,
1421
PREC_BIT_NOT,
1422
PREC_POWER,
1423
PREC_TYPE_TEST,
1424
PREC_AWAIT,
1425
PREC_CALL,
1426
PREC_ATTRIBUTE,
1427
PREC_SUBSCRIPT,
1428
PREC_PRIMARY,
1429
};
1430
struct ParseRule {
1431
ParseFunction prefix = nullptr;
1432
ParseFunction infix = nullptr;
1433
Precedence precedence = PREC_NONE;
1434
};
1435
static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
1436
1437
List<Node *> nodes_in_progress;
1438
void complete_extents(Node *p_node);
1439
void update_extents(Node *p_node);
1440
void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
1441
void reset_extents(Node *p_node, Node *p_from);
1442
1443
template <typename T>
1444
T *alloc_node() {
1445
T *node = memnew(T);
1446
1447
node->next = list;
1448
list = node;
1449
1450
reset_extents(node, previous);
1451
nodes_in_progress.push_back(node);
1452
1453
return node;
1454
}
1455
1456
// Allocates a node for patching up the parse tree when an error occurred.
1457
// Such nodes don't track their extents as they don't relate to actual tokens.
1458
template <typename T>
1459
T *alloc_recovery_node() {
1460
T *node = memnew(T);
1461
node->next = list;
1462
list = node;
1463
1464
return node;
1465
}
1466
1467
SuiteNode *alloc_recovery_suite() {
1468
SuiteNode *suite = alloc_recovery_node<SuiteNode>();
1469
suite->parent_block = current_suite;
1470
suite->parent_function = current_function;
1471
suite->is_in_loop = current_suite->is_in_loop;
1472
return suite;
1473
}
1474
1475
void clear();
1476
void push_error(const String &p_message, const Node *p_origin = nullptr);
1477
#ifdef DEBUG_ENABLED
1478
void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
1479
template <typename... Symbols>
1480
void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Symbols &...p_symbols) {
1481
push_warning(p_source, p_code, Vector<String>{ p_symbols... });
1482
}
1483
void apply_pending_warnings();
1484
#endif
1485
// Setting p_force to false will prevent the completion context from being update if a context was already set before.
1486
// This should only be done when we push context before we consumed any tokens for the corresponding structure.
1487
// See parse_precedence for an example.
1488
void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = true);
1489
void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = true);
1490
// In some cases it might become necessary to alter the completion context after parsing a subexpression.
1491
// For example to not override COMPLETE_CALL_ARGUMENTS with COMPLETION_NONE from string literals.
1492
void override_completion_context(const Node *p_for_node, CompletionType p_type, Node *p_node, int p_argument = -1);
1493
void push_completion_call(Node *p_call);
1494
void pop_completion_call();
1495
void set_last_completion_call_arg(int p_argument);
1496
1497
GDScriptTokenizer::Token advance();
1498
bool match(GDScriptTokenizer::Token::Type p_token_type);
1499
bool check(GDScriptTokenizer::Token::Type p_token_type) const;
1500
bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
1501
bool is_at_end() const;
1502
bool is_statement_end_token() const;
1503
bool is_statement_end() const;
1504
void end_statement(const String &p_context);
1505
void synchronize();
1506
void push_multiline(bool p_state);
1507
void pop_multiline();
1508
1509
// Main blocks.
1510
void parse_program();
1511
ClassNode *parse_class(bool p_is_static);
1512
void parse_class_name();
1513
void parse_extends();
1514
void parse_class_body(bool p_is_multiline);
1515
template <typename T>
1516
void parse_class_member(T *(GDScriptParser::*p_parse_function)(bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_static = false);
1517
SignalNode *parse_signal(bool p_is_static);
1518
EnumNode *parse_enum(bool p_is_static);
1519
ParameterNode *parse_parameter();
1520
FunctionNode *parse_function(bool p_is_static);
1521
bool parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type, int p_signature_start);
1522
SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr, bool p_for_lambda = false);
1523
// Annotations
1524
AnnotationNode *parse_annotation(uint32_t p_valid_targets);
1525
static bool register_annotation(const MethodInfo &p_info, uint32_t p_target_kinds, AnnotationAction p_apply, const Vector<Variant> &p_default_arguments = Vector<Variant>(), bool p_is_vararg = false);
1526
bool validate_annotation_arguments(AnnotationNode *p_annotation);
1527
void clear_unused_annotations();
1528
bool tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1529
bool icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1530
bool static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1531
bool abstract_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1532
bool onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1533
template <PropertyHint t_hint, Variant::Type t_type>
1534
bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1535
bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1536
bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1537
bool export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1538
template <PropertyUsageFlags t_usage>
1539
bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1540
bool warning_ignore_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1541
bool warning_ignore_region_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1542
bool rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
1543
// Statements.
1544
Node *parse_statement();
1545
VariableNode *parse_variable(bool p_is_static);
1546
VariableNode *parse_variable(bool p_is_static, bool p_allow_property);
1547
VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
1548
void parse_property_getter(VariableNode *p_variable);
1549
void parse_property_setter(VariableNode *p_variable);
1550
ConstantNode *parse_constant(bool p_is_static);
1551
AssertNode *parse_assert();
1552
BreakNode *parse_break();
1553
ContinueNode *parse_continue();
1554
ForNode *parse_for();
1555
IfNode *parse_if(const String &p_token = "if");
1556
MatchNode *parse_match();
1557
MatchBranchNode *parse_match_branch();
1558
PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
1559
WhileNode *parse_while();
1560
// Expressions.
1561
ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
1562
ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
1563
ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
1564
LiteralNode *parse_literal();
1565
ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
1566
ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
1567
IdentifierNode *parse_identifier();
1568
ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
1569
ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
1570
ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
1571
ExpressionNode *parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
1572
ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
1573
ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
1574
ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
1575
ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
1576
ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
1577
ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
1578
ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
1579
ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
1580
ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
1581
ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
1582
ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
1583
ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
1584
ExpressionNode *parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign);
1585
ExpressionNode *parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign);
1586
ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
1587
ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
1588
TypeNode *parse_type(bool p_allow_void = false);
1589
1590
#ifdef TOOLS_ENABLED
1591
int max_script_doc_line = INT_MAX;
1592
int min_member_doc_line = 1;
1593
bool has_comment(int p_line, bool p_must_be_doc = false);
1594
MemberDocData parse_doc_comment(int p_line, bool p_single_line = false);
1595
ClassDocData parse_class_doc_comment(int p_line, bool p_single_line = false);
1596
#endif // TOOLS_ENABLED
1597
1598
public:
1599
Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body = true);
1600
Error parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path);
1601
ClassNode *get_tree() const { return head; }
1602
bool is_tool() const { return _is_tool; }
1603
Ref<GDScriptParserRef> get_depended_parser_for(const String &p_path);
1604
const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers();
1605
ClassNode *find_class(const String &p_qualified_name) const;
1606
bool has_class(const GDScriptParser::ClassNode *p_class) const;
1607
static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`.
1608
1609
CompletionContext get_completion_context() const { return completion_context; }
1610
void get_annotation_list(List<MethodInfo> *r_annotations) const;
1611
bool annotation_exists(const String &p_annotation_name) const;
1612
1613
const List<ParserError> &get_errors() const { return errors; }
1614
const List<String> get_dependencies() const {
1615
// TODO: Keep track of deps.
1616
return List<String>();
1617
}
1618
#ifdef DEBUG_ENABLED
1619
const List<GDScriptWarning> &get_warnings() const { return warnings; }
1620
const HashSet<int> &get_unsafe_lines() const { return unsafe_lines; }
1621
int get_last_line_number() const { return current.end_line; }
1622
#endif
1623
1624
#ifdef TOOLS_ENABLED
1625
static HashMap<String, String> theme_color_names;
1626
1627
HashMap<int, GDScriptTokenizer::CommentData> comment_data;
1628
#endif // TOOLS_ENABLED
1629
1630
GDScriptParser();
1631
~GDScriptParser();
1632
1633
#ifdef DEBUG_ENABLED
1634
class TreePrinter {
1635
int indent_level = 0;
1636
String indent;
1637
StringBuilder printed;
1638
bool pending_indent = false;
1639
1640
void increase_indent();
1641
void decrease_indent();
1642
void push_line(const String &p_line = String());
1643
void push_text(const String &p_text);
1644
1645
void print_annotation(const AnnotationNode *p_annotation);
1646
void print_array(ArrayNode *p_array);
1647
void print_assert(AssertNode *p_assert);
1648
void print_assignment(AssignmentNode *p_assignment);
1649
void print_await(AwaitNode *p_await);
1650
void print_binary_op(BinaryOpNode *p_binary_op);
1651
void print_call(CallNode *p_call);
1652
void print_cast(CastNode *p_cast);
1653
void print_class(ClassNode *p_class);
1654
void print_constant(ConstantNode *p_constant);
1655
void print_dictionary(DictionaryNode *p_dictionary);
1656
void print_expression(ExpressionNode *p_expression);
1657
void print_enum(EnumNode *p_enum);
1658
void print_for(ForNode *p_for);
1659
void print_function(FunctionNode *p_function, const String &p_context = "Function");
1660
void print_get_node(GetNodeNode *p_get_node);
1661
void print_if(IfNode *p_if, bool p_is_elif = false);
1662
void print_identifier(IdentifierNode *p_identifier);
1663
void print_lambda(LambdaNode *p_lambda);
1664
void print_literal(LiteralNode *p_literal);
1665
void print_match(MatchNode *p_match);
1666
void print_match_branch(MatchBranchNode *p_match_branch);
1667
void print_match_pattern(PatternNode *p_match_pattern);
1668
void print_parameter(ParameterNode *p_parameter);
1669
void print_preload(PreloadNode *p_preload);
1670
void print_return(ReturnNode *p_return);
1671
void print_self(SelfNode *p_self);
1672
void print_signal(SignalNode *p_signal);
1673
void print_statement(Node *p_statement);
1674
void print_subscript(SubscriptNode *p_subscript);
1675
void print_suite(SuiteNode *p_suite);
1676
void print_ternary_op(TernaryOpNode *p_ternary_op);
1677
void print_type(TypeNode *p_type);
1678
void print_type_test(TypeTestNode *p_type_test);
1679
void print_unary_op(UnaryOpNode *p_unary_op);
1680
void print_variable(VariableNode *p_variable);
1681
void print_while(WhileNode *p_while);
1682
1683
public:
1684
void print_tree(const GDScriptParser &p_parser);
1685
};
1686
#endif // DEBUG_ENABLED
1687
static void cleanup();
1688
};
1689
1690