gdscript_parser.h 44 KB

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