gdscript_parser.h 41 KB

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