gdscript_parser.h 41 KB

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