gdscript_parser.h 43 KB

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