gdscript_parser.h 45 KB

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