gdscript_parser.h 43 KB

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