gdscript_parser.h 47 KB

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