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