gdscript_parser.h 43 KB

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