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