gdscript_parser.h 40 KB

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