gdscript_parser.h 41 KB

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