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