gdscript_parser.h 42 KB

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