gdscript_parser.h 42 KB

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