gdscript_parser.h 48 KB

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