gdscript_parser.h 43 KB

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