gdscript_parser.h 41 KB

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