gdscript_parser.h 44 KB

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