gdscript_parser.h 39 KB

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