gdscript_parser.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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. members_indices[p_annotation_node->export_info.name] = members.size();
  674. members.push_back(Member(p_annotation_node));
  675. }
  676. ClassNode() {
  677. type = CLASS;
  678. }
  679. };
  680. struct ConstantNode : public AssignableNode {
  681. #ifdef TOOLS_ENABLED
  682. String doc_description;
  683. #endif // TOOLS_ENABLED
  684. ConstantNode() {
  685. type = CONSTANT;
  686. }
  687. };
  688. struct ContinueNode : public Node {
  689. ContinueNode() {
  690. type = CONTINUE;
  691. }
  692. };
  693. struct DictionaryNode : public ExpressionNode {
  694. struct Pair {
  695. ExpressionNode *key = nullptr;
  696. ExpressionNode *value = nullptr;
  697. };
  698. Vector<Pair> elements;
  699. enum Style {
  700. LUA_TABLE,
  701. PYTHON_DICT,
  702. };
  703. Style style = PYTHON_DICT;
  704. DictionaryNode() {
  705. type = DICTIONARY;
  706. }
  707. };
  708. struct ForNode : public Node {
  709. IdentifierNode *variable = nullptr;
  710. ExpressionNode *list = nullptr;
  711. SuiteNode *loop = nullptr;
  712. ForNode() {
  713. type = FOR;
  714. }
  715. };
  716. struct FunctionNode : public Node {
  717. IdentifierNode *identifier = nullptr;
  718. Vector<ParameterNode *> parameters;
  719. HashMap<StringName, int> parameters_indices;
  720. TypeNode *return_type = nullptr;
  721. SuiteNode *body = nullptr;
  722. bool is_static = false;
  723. bool is_coroutine = false;
  724. Variant rpc_config;
  725. MethodInfo info;
  726. LambdaNode *source_lambda = nullptr;
  727. #ifdef TOOLS_ENABLED
  728. Vector<Variant> default_arg_values;
  729. String doc_description;
  730. #endif // TOOLS_ENABLED
  731. bool resolved_signature = false;
  732. bool resolved_body = false;
  733. FunctionNode() {
  734. type = FUNCTION;
  735. }
  736. };
  737. struct GetNodeNode : public ExpressionNode {
  738. String full_path;
  739. #ifdef DEBUG_ENABLED
  740. bool use_dollar = true;
  741. #endif
  742. GetNodeNode() {
  743. type = GET_NODE;
  744. }
  745. };
  746. struct IdentifierNode : public ExpressionNode {
  747. StringName name;
  748. #ifdef DEBUG_ENABLED
  749. SuiteNode *suite = nullptr; // The block in which the identifier is used.
  750. #endif
  751. enum Source {
  752. UNDEFINED_SOURCE,
  753. FUNCTION_PARAMETER,
  754. LOCAL_VARIABLE,
  755. LOCAL_CONSTANT,
  756. LOCAL_ITERATOR, // `for` loop iterator.
  757. LOCAL_BIND, // Pattern bind.
  758. MEMBER_VARIABLE,
  759. MEMBER_CONSTANT,
  760. MEMBER_FUNCTION,
  761. MEMBER_SIGNAL,
  762. MEMBER_CLASS,
  763. INHERITED_VARIABLE,
  764. STATIC_VARIABLE,
  765. };
  766. Source source = UNDEFINED_SOURCE;
  767. union {
  768. ParameterNode *parameter_source = nullptr;
  769. ConstantNode *constant_source;
  770. VariableNode *variable_source;
  771. IdentifierNode *bind_source;
  772. };
  773. FunctionNode *source_function = nullptr;
  774. int usages = 0; // Useful for binds/iterator variable.
  775. IdentifierNode() {
  776. type = IDENTIFIER;
  777. }
  778. };
  779. struct IfNode : public Node {
  780. ExpressionNode *condition = nullptr;
  781. SuiteNode *true_block = nullptr;
  782. SuiteNode *false_block = nullptr;
  783. IfNode() {
  784. type = IF;
  785. }
  786. };
  787. struct LambdaNode : public ExpressionNode {
  788. FunctionNode *function = nullptr;
  789. FunctionNode *parent_function = nullptr;
  790. Vector<IdentifierNode *> captures;
  791. HashMap<StringName, int> captures_indices;
  792. bool use_self = false;
  793. bool has_name() const {
  794. return function && function->identifier;
  795. }
  796. LambdaNode() {
  797. type = LAMBDA;
  798. }
  799. };
  800. struct LiteralNode : public ExpressionNode {
  801. Variant value;
  802. LiteralNode() {
  803. type = LITERAL;
  804. }
  805. };
  806. struct MatchNode : public Node {
  807. ExpressionNode *test = nullptr;
  808. Vector<MatchBranchNode *> branches;
  809. MatchNode() {
  810. type = MATCH;
  811. }
  812. };
  813. struct MatchBranchNode : public Node {
  814. Vector<PatternNode *> patterns;
  815. SuiteNode *block = nullptr;
  816. bool has_wildcard = false;
  817. MatchBranchNode() {
  818. type = MATCH_BRANCH;
  819. }
  820. };
  821. struct ParameterNode : public AssignableNode {
  822. ParameterNode() {
  823. type = PARAMETER;
  824. }
  825. };
  826. struct PassNode : public Node {
  827. PassNode() {
  828. type = PASS;
  829. }
  830. };
  831. struct PatternNode : public Node {
  832. enum Type {
  833. PT_LITERAL,
  834. PT_EXPRESSION,
  835. PT_BIND,
  836. PT_ARRAY,
  837. PT_DICTIONARY,
  838. PT_REST,
  839. PT_WILDCARD,
  840. };
  841. Type pattern_type = PT_LITERAL;
  842. union {
  843. LiteralNode *literal = nullptr;
  844. IdentifierNode *bind;
  845. ExpressionNode *expression;
  846. };
  847. Vector<PatternNode *> array;
  848. bool rest_used = false; // For array/dict patterns.
  849. struct Pair {
  850. ExpressionNode *key = nullptr;
  851. PatternNode *value_pattern = nullptr;
  852. };
  853. Vector<Pair> dictionary;
  854. HashMap<StringName, IdentifierNode *> binds;
  855. bool has_bind(const StringName &p_name);
  856. IdentifierNode *get_bind(const StringName &p_name);
  857. PatternNode() {
  858. type = PATTERN;
  859. }
  860. };
  861. struct PreloadNode : public ExpressionNode {
  862. ExpressionNode *path = nullptr;
  863. String resolved_path;
  864. Ref<Resource> resource;
  865. PreloadNode() {
  866. type = PRELOAD;
  867. }
  868. };
  869. struct ReturnNode : public Node {
  870. ExpressionNode *return_value = nullptr;
  871. bool void_return = false;
  872. ReturnNode() {
  873. type = RETURN;
  874. }
  875. };
  876. struct SelfNode : public ExpressionNode {
  877. ClassNode *current_class = nullptr;
  878. SelfNode() {
  879. type = SELF;
  880. }
  881. };
  882. struct SignalNode : public Node {
  883. IdentifierNode *identifier = nullptr;
  884. Vector<ParameterNode *> parameters;
  885. HashMap<StringName, int> parameters_indices;
  886. #ifdef TOOLS_ENABLED
  887. String doc_description;
  888. #endif // TOOLS_ENABLED
  889. SignalNode() {
  890. type = SIGNAL;
  891. }
  892. };
  893. struct SubscriptNode : public ExpressionNode {
  894. ExpressionNode *base = nullptr;
  895. union {
  896. ExpressionNode *index = nullptr;
  897. IdentifierNode *attribute;
  898. };
  899. bool is_attribute = false;
  900. SubscriptNode() {
  901. type = SUBSCRIPT;
  902. }
  903. };
  904. struct SuiteNode : public Node {
  905. SuiteNode *parent_block = nullptr;
  906. Vector<Node *> statements;
  907. struct Local {
  908. enum Type {
  909. UNDEFINED,
  910. CONSTANT,
  911. VARIABLE,
  912. PARAMETER,
  913. FOR_VARIABLE,
  914. PATTERN_BIND,
  915. };
  916. Type type = UNDEFINED;
  917. union {
  918. ConstantNode *constant = nullptr;
  919. VariableNode *variable;
  920. ParameterNode *parameter;
  921. IdentifierNode *bind;
  922. };
  923. StringName name;
  924. FunctionNode *source_function = nullptr;
  925. int start_line = 0, end_line = 0;
  926. int start_column = 0, end_column = 0;
  927. int leftmost_column = 0, rightmost_column = 0;
  928. DataType get_datatype() const;
  929. String get_name() const;
  930. Local() {}
  931. Local(ConstantNode *p_constant, FunctionNode *p_source_function) {
  932. type = CONSTANT;
  933. constant = p_constant;
  934. name = p_constant->identifier->name;
  935. source_function = p_source_function;
  936. start_line = p_constant->start_line;
  937. end_line = p_constant->end_line;
  938. start_column = p_constant->start_column;
  939. end_column = p_constant->end_column;
  940. leftmost_column = p_constant->leftmost_column;
  941. rightmost_column = p_constant->rightmost_column;
  942. }
  943. Local(VariableNode *p_variable, FunctionNode *p_source_function) {
  944. type = VARIABLE;
  945. variable = p_variable;
  946. name = p_variable->identifier->name;
  947. source_function = p_source_function;
  948. start_line = p_variable->start_line;
  949. end_line = p_variable->end_line;
  950. start_column = p_variable->start_column;
  951. end_column = p_variable->end_column;
  952. leftmost_column = p_variable->leftmost_column;
  953. rightmost_column = p_variable->rightmost_column;
  954. }
  955. Local(ParameterNode *p_parameter, FunctionNode *p_source_function) {
  956. type = PARAMETER;
  957. parameter = p_parameter;
  958. name = p_parameter->identifier->name;
  959. source_function = p_source_function;
  960. start_line = p_parameter->start_line;
  961. end_line = p_parameter->end_line;
  962. start_column = p_parameter->start_column;
  963. end_column = p_parameter->end_column;
  964. leftmost_column = p_parameter->leftmost_column;
  965. rightmost_column = p_parameter->rightmost_column;
  966. }
  967. Local(IdentifierNode *p_identifier, FunctionNode *p_source_function) {
  968. type = FOR_VARIABLE;
  969. bind = p_identifier;
  970. name = p_identifier->name;
  971. source_function = p_source_function;
  972. start_line = p_identifier->start_line;
  973. end_line = p_identifier->end_line;
  974. start_column = p_identifier->start_column;
  975. end_column = p_identifier->end_column;
  976. leftmost_column = p_identifier->leftmost_column;
  977. rightmost_column = p_identifier->rightmost_column;
  978. }
  979. };
  980. Local empty;
  981. Vector<Local> locals;
  982. HashMap<StringName, int> locals_indices;
  983. FunctionNode *parent_function = nullptr;
  984. IfNode *parent_if = nullptr;
  985. bool has_return = false;
  986. bool has_continue = false;
  987. bool has_unreachable_code = false; // Just so warnings aren't given more than once per block.
  988. bool is_in_loop = false; // The block is nested in a loop (directly or indirectly).
  989. bool has_local(const StringName &p_name) const;
  990. const Local &get_local(const StringName &p_name) const;
  991. template <class T>
  992. void add_local(T *p_local, FunctionNode *p_source_function) {
  993. locals_indices[p_local->identifier->name] = locals.size();
  994. locals.push_back(Local(p_local, p_source_function));
  995. }
  996. void add_local(const Local &p_local) {
  997. locals_indices[p_local.name] = locals.size();
  998. locals.push_back(p_local);
  999. }
  1000. SuiteNode() {
  1001. type = SUITE;
  1002. }
  1003. };
  1004. struct TernaryOpNode : public ExpressionNode {
  1005. // Only one ternary operation exists, so no abstraction here.
  1006. ExpressionNode *condition = nullptr;
  1007. ExpressionNode *true_expr = nullptr;
  1008. ExpressionNode *false_expr = nullptr;
  1009. TernaryOpNode() {
  1010. type = TERNARY_OPERATOR;
  1011. }
  1012. };
  1013. struct TypeNode : public Node {
  1014. Vector<IdentifierNode *> type_chain;
  1015. TypeNode *container_type = nullptr;
  1016. TypeNode() {
  1017. type = TYPE;
  1018. }
  1019. };
  1020. struct TypeTestNode : public ExpressionNode {
  1021. ExpressionNode *operand = nullptr;
  1022. TypeNode *test_type = nullptr;
  1023. DataType test_datatype;
  1024. TypeTestNode() {
  1025. type = TYPE_TEST;
  1026. }
  1027. };
  1028. struct UnaryOpNode : public ExpressionNode {
  1029. enum OpType {
  1030. OP_POSITIVE,
  1031. OP_NEGATIVE,
  1032. OP_COMPLEMENT,
  1033. OP_LOGIC_NOT,
  1034. };
  1035. OpType operation = OP_POSITIVE;
  1036. Variant::Operator variant_op = Variant::OP_MAX;
  1037. ExpressionNode *operand = nullptr;
  1038. UnaryOpNode() {
  1039. type = UNARY_OPERATOR;
  1040. }
  1041. };
  1042. struct VariableNode : public AssignableNode {
  1043. enum PropertyStyle {
  1044. PROP_NONE,
  1045. PROP_INLINE,
  1046. PROP_SETGET,
  1047. };
  1048. PropertyStyle property = PROP_NONE;
  1049. union {
  1050. FunctionNode *setter = nullptr;
  1051. IdentifierNode *setter_pointer;
  1052. };
  1053. IdentifierNode *setter_parameter = nullptr;
  1054. union {
  1055. FunctionNode *getter = nullptr;
  1056. IdentifierNode *getter_pointer;
  1057. };
  1058. bool exported = false;
  1059. bool onready = false;
  1060. PropertyInfo export_info;
  1061. int assignments = 0;
  1062. bool is_static = false;
  1063. #ifdef TOOLS_ENABLED
  1064. String doc_description;
  1065. #endif // TOOLS_ENABLED
  1066. VariableNode() {
  1067. type = VARIABLE;
  1068. }
  1069. };
  1070. struct WhileNode : public Node {
  1071. ExpressionNode *condition = nullptr;
  1072. SuiteNode *loop = nullptr;
  1073. WhileNode() {
  1074. type = WHILE;
  1075. }
  1076. };
  1077. enum CompletionType {
  1078. COMPLETION_NONE,
  1079. COMPLETION_ANNOTATION, // Annotation (following @).
  1080. COMPLETION_ANNOTATION_ARGUMENTS, // Annotation arguments hint.
  1081. COMPLETION_ASSIGN, // Assignment based on type (e.g. enum values).
  1082. COMPLETION_ATTRIBUTE, // After id.| to look for members.
  1083. COMPLETION_ATTRIBUTE_METHOD, // After id.| to look for methods.
  1084. 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).
  1085. COMPLETION_CALL_ARGUMENTS, // Complete with nodes, input actions, enum values (or usual expressions).
  1086. // TODO: COMPLETION_DECLARATION, // Potential declaration (var, const, func).
  1087. COMPLETION_GET_NODE, // Get node with $ notation.
  1088. COMPLETION_IDENTIFIER, // List available identifiers in scope.
  1089. COMPLETION_INHERIT_TYPE, // Type after extends. Exclude non-viable types (built-ins, enums, void). Includes subtypes using the argument index.
  1090. COMPLETION_METHOD, // List available methods in scope.
  1091. COMPLETION_OVERRIDE_METHOD, // Override implementation, also for native virtuals.
  1092. COMPLETION_PROPERTY_DECLARATION, // Property declaration (get, set).
  1093. COMPLETION_PROPERTY_DECLARATION_OR_TYPE, // Property declaration (get, set) or a type hint.
  1094. COMPLETION_PROPERTY_METHOD, // Property setter or getter (list available methods).
  1095. COMPLETION_RESOURCE_PATH, // For load/preload.
  1096. COMPLETION_SUBSCRIPT, // Inside id[|].
  1097. COMPLETION_SUPER_METHOD, // After super.
  1098. COMPLETION_TYPE_ATTRIBUTE, // Attribute in type name (Type.|).
  1099. COMPLETION_TYPE_NAME, // Name of type (after :).
  1100. COMPLETION_TYPE_NAME_OR_VOID, // Same as TYPE_NAME, but allows void (in function return type).
  1101. };
  1102. struct CompletionContext {
  1103. CompletionType type = COMPLETION_NONE;
  1104. ClassNode *current_class = nullptr;
  1105. FunctionNode *current_function = nullptr;
  1106. SuiteNode *current_suite = nullptr;
  1107. int current_line = -1;
  1108. int current_argument = -1;
  1109. Variant::Type builtin_type = Variant::VARIANT_MAX;
  1110. Node *node = nullptr;
  1111. Object *base = nullptr;
  1112. List<Ref<GDScriptParserRef>> dependent_parsers;
  1113. };
  1114. struct CompletionCall {
  1115. Node *call = nullptr;
  1116. int argument = -1;
  1117. };
  1118. private:
  1119. friend class GDScriptAnalyzer;
  1120. bool _is_tool = false;
  1121. String script_path;
  1122. bool for_completion = false;
  1123. bool panic_mode = false;
  1124. bool can_break = false;
  1125. bool can_continue = false;
  1126. List<bool> multiline_stack;
  1127. ClassNode *head = nullptr;
  1128. Node *list = nullptr;
  1129. List<ParserError> errors;
  1130. #ifdef DEBUG_ENABLED
  1131. bool is_ignoring_warnings = false;
  1132. List<GDScriptWarning> warnings;
  1133. HashSet<GDScriptWarning::Code> ignored_warnings;
  1134. HashSet<int> unsafe_lines;
  1135. #endif
  1136. GDScriptTokenizer tokenizer;
  1137. GDScriptTokenizer::Token previous;
  1138. GDScriptTokenizer::Token current;
  1139. ClassNode *current_class = nullptr;
  1140. FunctionNode *current_function = nullptr;
  1141. SuiteNode *current_suite = nullptr;
  1142. CompletionContext completion_context;
  1143. CompletionCall completion_call;
  1144. List<CompletionCall> completion_call_stack;
  1145. bool passed_cursor = false;
  1146. bool in_lambda = false;
  1147. bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
  1148. typedef bool (GDScriptParser::*AnnotationAction)(const AnnotationNode *p_annotation, Node *p_target);
  1149. struct AnnotationInfo {
  1150. enum TargetKind {
  1151. NONE = 0,
  1152. SCRIPT = 1 << 0,
  1153. CLASS = 1 << 1,
  1154. VARIABLE = 1 << 2,
  1155. CONSTANT = 1 << 3,
  1156. SIGNAL = 1 << 4,
  1157. FUNCTION = 1 << 5,
  1158. STATEMENT = 1 << 6,
  1159. STANDALONE = 1 << 7,
  1160. CLASS_LEVEL = CLASS | VARIABLE | FUNCTION,
  1161. };
  1162. uint32_t target_kind = 0; // Flags.
  1163. AnnotationAction apply = nullptr;
  1164. MethodInfo info;
  1165. };
  1166. HashMap<StringName, AnnotationInfo> valid_annotations;
  1167. List<AnnotationNode *> annotation_stack;
  1168. typedef ExpressionNode *(GDScriptParser::*ParseFunction)(ExpressionNode *p_previous_operand, bool p_can_assign);
  1169. // Higher value means higher precedence (i.e. is evaluated first).
  1170. enum Precedence {
  1171. PREC_NONE,
  1172. PREC_ASSIGNMENT,
  1173. PREC_CAST,
  1174. PREC_TERNARY,
  1175. PREC_LOGIC_OR,
  1176. PREC_LOGIC_AND,
  1177. PREC_LOGIC_NOT,
  1178. PREC_CONTENT_TEST,
  1179. PREC_COMPARISON,
  1180. PREC_BIT_OR,
  1181. PREC_BIT_XOR,
  1182. PREC_BIT_AND,
  1183. PREC_BIT_SHIFT,
  1184. PREC_ADDITION_SUBTRACTION,
  1185. PREC_FACTOR,
  1186. PREC_SIGN,
  1187. PREC_BIT_NOT,
  1188. PREC_POWER,
  1189. PREC_TYPE_TEST,
  1190. PREC_AWAIT,
  1191. PREC_CALL,
  1192. PREC_ATTRIBUTE,
  1193. PREC_SUBSCRIPT,
  1194. PREC_PRIMARY,
  1195. };
  1196. struct ParseRule {
  1197. ParseFunction prefix = nullptr;
  1198. ParseFunction infix = nullptr;
  1199. Precedence precedence = PREC_NONE;
  1200. };
  1201. static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
  1202. List<Node *> nodes_in_progress;
  1203. void complete_extents(Node *p_node);
  1204. void update_extents(Node *p_node);
  1205. void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
  1206. void reset_extents(Node *p_node, Node *p_from);
  1207. template <class T>
  1208. T *alloc_node() {
  1209. T *node = memnew(T);
  1210. node->next = list;
  1211. list = node;
  1212. reset_extents(node, previous);
  1213. nodes_in_progress.push_back(node);
  1214. return node;
  1215. }
  1216. void clear();
  1217. void push_error(const String &p_message, const Node *p_origin = nullptr);
  1218. #ifdef DEBUG_ENABLED
  1219. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
  1220. template <typename... Symbols>
  1221. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Symbols &...p_symbols) {
  1222. push_warning(p_source, p_code, Vector<String>{ p_symbols... });
  1223. }
  1224. #endif
  1225. void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = false);
  1226. void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = false);
  1227. void push_completion_call(Node *p_call);
  1228. void pop_completion_call();
  1229. void set_last_completion_call_arg(int p_argument);
  1230. GDScriptTokenizer::Token advance();
  1231. bool match(GDScriptTokenizer::Token::Type p_token_type);
  1232. bool check(GDScriptTokenizer::Token::Type p_token_type) const;
  1233. bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
  1234. bool is_at_end() const;
  1235. bool is_statement_end_token() const;
  1236. bool is_statement_end() const;
  1237. void end_statement(const String &p_context);
  1238. void synchronize();
  1239. void push_multiline(bool p_state);
  1240. void pop_multiline();
  1241. // Main blocks.
  1242. void parse_program();
  1243. ClassNode *parse_class(bool p_is_static);
  1244. void parse_class_name();
  1245. void parse_extends();
  1246. void parse_class_body(bool p_is_multiline);
  1247. template <class T>
  1248. void parse_class_member(T *(GDScriptParser::*p_parse_function)(bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_static = false);
  1249. SignalNode *parse_signal(bool p_is_static);
  1250. EnumNode *parse_enum(bool p_is_static);
  1251. ParameterNode *parse_parameter();
  1252. FunctionNode *parse_function(bool p_is_static);
  1253. void parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type);
  1254. SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr, bool p_for_lambda = false);
  1255. // Annotations
  1256. AnnotationNode *parse_annotation(uint32_t p_valid_targets);
  1257. 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);
  1258. bool validate_annotation_arguments(AnnotationNode *p_annotation);
  1259. void clear_unused_annotations();
  1260. bool tool_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1261. bool icon_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1262. bool onready_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1263. template <PropertyHint t_hint, Variant::Type t_type>
  1264. bool export_annotations(const AnnotationNode *p_annotation, Node *p_target);
  1265. template <PropertyUsageFlags t_usage>
  1266. bool export_group_annotations(const AnnotationNode *p_annotation, Node *p_target);
  1267. bool warning_annotations(const AnnotationNode *p_annotation, Node *p_target);
  1268. bool rpc_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1269. bool static_unload_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1270. // Statements.
  1271. Node *parse_statement();
  1272. VariableNode *parse_variable(bool p_is_static);
  1273. VariableNode *parse_variable(bool p_is_static, bool p_allow_property);
  1274. VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
  1275. void parse_property_getter(VariableNode *p_variable);
  1276. void parse_property_setter(VariableNode *p_variable);
  1277. ConstantNode *parse_constant(bool p_is_static);
  1278. AssertNode *parse_assert();
  1279. BreakNode *parse_break();
  1280. ContinueNode *parse_continue();
  1281. ForNode *parse_for();
  1282. IfNode *parse_if(const String &p_token = "if");
  1283. MatchNode *parse_match();
  1284. MatchBranchNode *parse_match_branch();
  1285. PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
  1286. WhileNode *parse_while();
  1287. // Expressions.
  1288. ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
  1289. ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
  1290. ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
  1291. LiteralNode *parse_literal();
  1292. ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
  1293. ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
  1294. IdentifierNode *parse_identifier();
  1295. ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
  1296. ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1297. ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1298. ExpressionNode *parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1299. ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1300. ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
  1301. ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
  1302. ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
  1303. ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
  1304. ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
  1305. ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
  1306. ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
  1307. ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
  1308. ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
  1309. ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
  1310. ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
  1311. ExpressionNode *parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign);
  1312. ExpressionNode *parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign);
  1313. ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
  1314. ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
  1315. TypeNode *parse_type(bool p_allow_void = false);
  1316. #ifdef TOOLS_ENABLED
  1317. // Doc comments.
  1318. int class_doc_line = 0x7FFFFFFF;
  1319. bool has_comment(int p_line, bool p_must_be_doc = false);
  1320. String get_doc_comment(int p_line, bool p_single_line = false);
  1321. void get_class_doc_comment(int p_line, String &p_brief, String &p_desc, Vector<Pair<String, String>> &p_tutorials, bool p_inner_class);
  1322. #endif // TOOLS_ENABLED
  1323. public:
  1324. Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion);
  1325. ClassNode *get_tree() const { return head; }
  1326. bool is_tool() const { return _is_tool; }
  1327. ClassNode *find_class(const String &p_qualified_name) const;
  1328. bool has_class(const GDScriptParser::ClassNode *p_class) const;
  1329. static Variant::Type get_builtin_type(const StringName &p_type);
  1330. CompletionContext get_completion_context() const { return completion_context; }
  1331. CompletionCall get_completion_call() const { return completion_call; }
  1332. void get_annotation_list(List<MethodInfo> *r_annotations) const;
  1333. bool annotation_exists(const String &p_annotation_name) const;
  1334. const List<ParserError> &get_errors() const { return errors; }
  1335. const List<String> get_dependencies() const {
  1336. // TODO: Keep track of deps.
  1337. return List<String>();
  1338. }
  1339. #ifdef DEBUG_ENABLED
  1340. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  1341. const HashSet<int> &get_unsafe_lines() const { return unsafe_lines; }
  1342. int get_last_line_number() const { return current.end_line; }
  1343. #endif
  1344. GDScriptParser();
  1345. ~GDScriptParser();
  1346. #ifdef DEBUG_ENABLED
  1347. class TreePrinter {
  1348. int indent_level = 0;
  1349. String indent;
  1350. StringBuilder printed;
  1351. bool pending_indent = false;
  1352. void increase_indent();
  1353. void decrease_indent();
  1354. void push_line(const String &p_line = String());
  1355. void push_text(const String &p_text);
  1356. void print_annotation(const AnnotationNode *p_annotation);
  1357. void print_array(ArrayNode *p_array);
  1358. void print_assert(AssertNode *p_assert);
  1359. void print_assignment(AssignmentNode *p_assignment);
  1360. void print_await(AwaitNode *p_await);
  1361. void print_binary_op(BinaryOpNode *p_binary_op);
  1362. void print_call(CallNode *p_call);
  1363. void print_cast(CastNode *p_cast);
  1364. void print_class(ClassNode *p_class);
  1365. void print_constant(ConstantNode *p_constant);
  1366. void print_dictionary(DictionaryNode *p_dictionary);
  1367. void print_expression(ExpressionNode *p_expression);
  1368. void print_enum(EnumNode *p_enum);
  1369. void print_for(ForNode *p_for);
  1370. void print_function(FunctionNode *p_function, const String &p_context = "Function");
  1371. void print_get_node(GetNodeNode *p_get_node);
  1372. void print_if(IfNode *p_if, bool p_is_elif = false);
  1373. void print_identifier(IdentifierNode *p_identifier);
  1374. void print_lambda(LambdaNode *p_lambda);
  1375. void print_literal(LiteralNode *p_literal);
  1376. void print_match(MatchNode *p_match);
  1377. void print_match_branch(MatchBranchNode *p_match_branch);
  1378. void print_match_pattern(PatternNode *p_match_pattern);
  1379. void print_parameter(ParameterNode *p_parameter);
  1380. void print_preload(PreloadNode *p_preload);
  1381. void print_return(ReturnNode *p_return);
  1382. void print_self(SelfNode *p_self);
  1383. void print_signal(SignalNode *p_signal);
  1384. void print_statement(Node *p_statement);
  1385. void print_subscript(SubscriptNode *p_subscript);
  1386. void print_suite(SuiteNode *p_suite);
  1387. void print_ternary_op(TernaryOpNode *p_ternary_op);
  1388. void print_type(TypeNode *p_type);
  1389. void print_type_test(TypeTestNode *p_type_test);
  1390. void print_unary_op(UnaryOpNode *p_unary_op);
  1391. void print_variable(VariableNode *p_variable);
  1392. void print_while(WhileNode *p_while);
  1393. public:
  1394. void print_tree(const GDScriptParser &p_parser);
  1395. };
  1396. #endif // DEBUG_ENABLED
  1397. static void cleanup();
  1398. };
  1399. #endif // GDSCRIPT_PARSER_H