gdscript_parser.h 43 KB

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