gdscript_parser.h 44 KB

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