gdscript_parser.h 46 KB

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