gdscript_parser.h 41 KB

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