gdscript_parser.h 41 KB

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