gdscript_parser.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. /**************************************************************************/
  2. /* gdscript_parser.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GDSCRIPT_PARSER_H
  31. #define GDSCRIPT_PARSER_H
  32. #include "gdscript_cache.h"
  33. #include "gdscript_tokenizer.h"
  34. #ifdef DEBUG_ENABLED
  35. #include "gdscript_warning.h"
  36. #endif
  37. #include "core/io/resource.h"
  38. #include "core/object/ref_counted.h"
  39. #include "core/object/script_language.h"
  40. #include "core/string/string_name.h"
  41. #include "core/string/ustring.h"
  42. #include "core/templates/hash_map.h"
  43. #include "core/templates/list.h"
  44. #include "core/templates/rb_map.h"
  45. #include "core/templates/vector.h"
  46. #include "core/variant/variant.h"
  47. #ifdef DEBUG_ENABLED
  48. #include "core/string/string_builder.h"
  49. #endif
  50. class GDScriptParser {
  51. struct AnnotationInfo;
  52. public:
  53. // Forward-declare all parser nodes, to avoid ordering issues.
  54. struct AnnotationNode;
  55. struct ArrayNode;
  56. struct AssertNode;
  57. struct AssignableNode;
  58. struct AssignmentNode;
  59. struct AwaitNode;
  60. struct BinaryOpNode;
  61. struct BreakNode;
  62. struct BreakpointNode;
  63. struct CallNode;
  64. struct CastNode;
  65. struct ClassNode;
  66. struct ConstantNode;
  67. struct ContinueNode;
  68. struct DictionaryNode;
  69. struct EnumNode;
  70. struct ExpressionNode;
  71. struct ForNode;
  72. struct FunctionNode;
  73. struct GetNodeNode;
  74. struct IdentifierNode;
  75. struct IfNode;
  76. struct LambdaNode;
  77. struct LiteralNode;
  78. struct MatchNode;
  79. struct MatchBranchNode;
  80. struct ParameterNode;
  81. struct PassNode;
  82. struct PatternNode;
  83. struct PreloadNode;
  84. struct ReturnNode;
  85. struct SelfNode;
  86. struct SignalNode;
  87. struct SubscriptNode;
  88. struct SuiteNode;
  89. struct TernaryOpNode;
  90. struct TypeNode;
  91. struct TypeTestNode;
  92. struct UnaryOpNode;
  93. struct VariableNode;
  94. struct WhileNode;
  95. class DataType {
  96. public:
  97. Vector<DataType> container_element_types;
  98. enum Kind {
  99. BUILTIN,
  100. NATIVE,
  101. SCRIPT,
  102. CLASS, // GDScript.
  103. ENUM, // Enumeration.
  104. VARIANT, // Can be any type.
  105. RESOLVING, // Currently resolving.
  106. UNRESOLVED,
  107. };
  108. Kind kind = UNRESOLVED;
  109. enum TypeSource {
  110. UNDETECTED, // Can be any type.
  111. INFERRED, // Has inferred type, but still dynamic.
  112. ANNOTATED_EXPLICIT, // Has a specific type annotated.
  113. ANNOTATED_INFERRED, // Has a static type but comes from the assigned value.
  114. };
  115. TypeSource type_source = UNDETECTED;
  116. bool is_constant = false;
  117. bool is_read_only = false;
  118. bool is_meta_type = false;
  119. bool is_pseudo_type = false; // For global names that can't be used standalone.
  120. bool is_coroutine = false; // For function calls.
  121. Variant::Type builtin_type = Variant::NIL;
  122. StringName native_type;
  123. StringName enum_type; // Enum name or the value name in an enum.
  124. Ref<Script> script_type;
  125. String script_path;
  126. ClassNode *class_type = nullptr;
  127. MethodInfo method_info; // For callable/signals.
  128. HashMap<StringName, int64_t> enum_values; // For enums.
  129. _FORCE_INLINE_ bool is_set() const { return kind != RESOLVING && kind != UNRESOLVED; }
  130. _FORCE_INLINE_ bool is_resolving() const { return kind == RESOLVING; }
  131. _FORCE_INLINE_ bool has_no_type() const { return type_source == UNDETECTED; }
  132. _FORCE_INLINE_ bool is_variant() const { return kind == VARIANT || kind == RESOLVING || kind == UNRESOLVED; }
  133. _FORCE_INLINE_ bool is_hard_type() const { return type_source > INFERRED; }
  134. String to_string() const;
  135. _FORCE_INLINE_ String to_string_strict() const { return is_hard_type() ? to_string() : "Variant"; }
  136. PropertyInfo to_property_info(const String &p_name) const;
  137. _FORCE_INLINE_ static DataType get_variant_type() { // Default DataType for container elements.
  138. DataType datatype;
  139. datatype.kind = VARIANT;
  140. datatype.type_source = INFERRED;
  141. return datatype;
  142. }
  143. _FORCE_INLINE_ void set_container_element_type(int p_index, const DataType &p_type) {
  144. ERR_FAIL_COND(p_index < 0);
  145. while (p_index >= container_element_types.size()) {
  146. container_element_types.push_back(get_variant_type());
  147. }
  148. container_element_types.write[p_index] = DataType(p_type);
  149. }
  150. _FORCE_INLINE_ int get_container_element_type_count() const {
  151. return container_element_types.size();
  152. }
  153. _FORCE_INLINE_ DataType get_container_element_type(int p_index) const {
  154. ERR_FAIL_INDEX_V(p_index, container_element_types.size(), get_variant_type());
  155. return container_element_types[p_index];
  156. }
  157. _FORCE_INLINE_ DataType get_container_element_type_or_variant(int p_index) const {
  158. if (p_index < 0 || p_index >= container_element_types.size()) {
  159. return get_variant_type();
  160. }
  161. return container_element_types[p_index];
  162. }
  163. _FORCE_INLINE_ bool has_container_element_type(int p_index) const {
  164. return p_index >= 0 && p_index < container_element_types.size();
  165. }
  166. _FORCE_INLINE_ bool has_container_element_types() const {
  167. return !container_element_types.is_empty();
  168. }
  169. bool is_typed_container_type() const;
  170. GDScriptParser::DataType get_typed_container_type() const;
  171. bool can_reference(const DataType &p_other) const;
  172. bool operator==(const DataType &p_other) const {
  173. if (type_source == UNDETECTED || p_other.type_source == UNDETECTED) {
  174. return true; // Can be considered equal for parsing purposes.
  175. }
  176. if (type_source == INFERRED || p_other.type_source == INFERRED) {
  177. return true; // Can be considered equal for parsing purposes.
  178. }
  179. if (kind != p_other.kind) {
  180. return false;
  181. }
  182. switch (kind) {
  183. case VARIANT:
  184. return true; // All variants are the same.
  185. case BUILTIN:
  186. return builtin_type == p_other.builtin_type;
  187. case NATIVE:
  188. case ENUM: // Enums use native_type to identify the enum and its base class.
  189. return native_type == p_other.native_type;
  190. case SCRIPT:
  191. return script_type == p_other.script_type;
  192. case CLASS:
  193. return class_type == p_other.class_type || class_type->fqcn == p_other.class_type->fqcn;
  194. case RESOLVING:
  195. case UNRESOLVED:
  196. break;
  197. }
  198. return false;
  199. }
  200. bool operator!=(const DataType &p_other) const {
  201. return !(*this == p_other);
  202. }
  203. void operator=(const DataType &p_other) {
  204. kind = p_other.kind;
  205. type_source = p_other.type_source;
  206. is_read_only = p_other.is_read_only;
  207. is_constant = p_other.is_constant;
  208. is_meta_type = p_other.is_meta_type;
  209. is_pseudo_type = p_other.is_pseudo_type;
  210. is_coroutine = p_other.is_coroutine;
  211. builtin_type = p_other.builtin_type;
  212. native_type = p_other.native_type;
  213. enum_type = p_other.enum_type;
  214. script_type = p_other.script_type;
  215. script_path = p_other.script_path;
  216. class_type = p_other.class_type;
  217. method_info = p_other.method_info;
  218. enum_values = p_other.enum_values;
  219. container_element_types = p_other.container_element_types;
  220. }
  221. DataType() = default;
  222. DataType(const DataType &p_other) {
  223. *this = p_other;
  224. }
  225. ~DataType() {}
  226. };
  227. struct ParserError {
  228. // TODO: Do I really need a "type"?
  229. // enum Type {
  230. // NO_ERROR,
  231. // EMPTY_FILE,
  232. // CLASS_NAME_USED_TWICE,
  233. // EXTENDS_USED_TWICE,
  234. // EXPECTED_END_STATEMENT,
  235. // };
  236. // Type type = NO_ERROR;
  237. String message;
  238. int line = 0, column = 0;
  239. };
  240. #ifdef TOOLS_ENABLED
  241. struct ClassDocData {
  242. String brief;
  243. String description;
  244. Vector<Pair<String, String>> tutorials;
  245. bool is_deprecated = false;
  246. String deprecated_message;
  247. bool is_experimental = false;
  248. String experimental_message;
  249. };
  250. struct MemberDocData {
  251. String description;
  252. bool is_deprecated = false;
  253. String deprecated_message;
  254. bool is_experimental = false;
  255. String experimental_message;
  256. };
  257. #endif // TOOLS_ENABLED
  258. struct Node {
  259. enum Type {
  260. NONE,
  261. ANNOTATION,
  262. ARRAY,
  263. ASSERT,
  264. ASSIGNMENT,
  265. AWAIT,
  266. BINARY_OPERATOR,
  267. BREAK,
  268. BREAKPOINT,
  269. CALL,
  270. CAST,
  271. CLASS,
  272. CONSTANT,
  273. CONTINUE,
  274. DICTIONARY,
  275. ENUM,
  276. FOR,
  277. FUNCTION,
  278. GET_NODE,
  279. IDENTIFIER,
  280. IF,
  281. LAMBDA,
  282. LITERAL,
  283. MATCH,
  284. MATCH_BRANCH,
  285. PARAMETER,
  286. PASS,
  287. PATTERN,
  288. PRELOAD,
  289. RETURN,
  290. SELF,
  291. SIGNAL,
  292. SUBSCRIPT,
  293. SUITE,
  294. TERNARY_OPERATOR,
  295. TYPE,
  296. TYPE_TEST,
  297. UNARY_OPERATOR,
  298. VARIABLE,
  299. WHILE,
  300. };
  301. Type type = NONE;
  302. int start_line = 0, end_line = 0;
  303. int start_column = 0, end_column = 0;
  304. int leftmost_column = 0, rightmost_column = 0;
  305. Node *next = nullptr;
  306. List<AnnotationNode *> annotations;
  307. DataType datatype;
  308. virtual DataType get_datatype() const { return datatype; }
  309. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  310. virtual bool is_expression() const { return false; }
  311. virtual ~Node() {}
  312. };
  313. struct ExpressionNode : public Node {
  314. // Base type for all expression kinds.
  315. bool reduced = false;
  316. bool is_constant = false;
  317. Variant reduced_value;
  318. virtual bool is_expression() const override { return true; }
  319. virtual ~ExpressionNode() {}
  320. protected:
  321. ExpressionNode() {}
  322. };
  323. struct AnnotationNode : public Node {
  324. StringName name;
  325. Vector<ExpressionNode *> arguments;
  326. Vector<Variant> resolved_arguments;
  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. #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. HashSet<int> unsafe_lines;
  1185. #endif
  1186. GDScriptTokenizer *tokenizer = nullptr;
  1187. GDScriptTokenizer::Token previous;
  1188. GDScriptTokenizer::Token current;
  1189. ClassNode *current_class = nullptr;
  1190. FunctionNode *current_function = nullptr;
  1191. LambdaNode *current_lambda = nullptr;
  1192. SuiteNode *current_suite = nullptr;
  1193. CompletionContext completion_context;
  1194. CompletionCall completion_call;
  1195. List<CompletionCall> completion_call_stack;
  1196. bool passed_cursor = false;
  1197. bool in_lambda = false;
  1198. bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
  1199. typedef bool (GDScriptParser::*AnnotationAction)(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1200. struct AnnotationInfo {
  1201. enum TargetKind {
  1202. NONE = 0,
  1203. SCRIPT = 1 << 0,
  1204. CLASS = 1 << 1,
  1205. VARIABLE = 1 << 2,
  1206. CONSTANT = 1 << 3,
  1207. SIGNAL = 1 << 4,
  1208. FUNCTION = 1 << 5,
  1209. STATEMENT = 1 << 6,
  1210. STANDALONE = 1 << 7,
  1211. CLASS_LEVEL = CLASS | VARIABLE | CONSTANT | SIGNAL | FUNCTION,
  1212. };
  1213. uint32_t target_kind = 0; // Flags.
  1214. AnnotationAction apply = nullptr;
  1215. MethodInfo info;
  1216. };
  1217. static HashMap<StringName, AnnotationInfo> valid_annotations;
  1218. List<AnnotationNode *> annotation_stack;
  1219. typedef ExpressionNode *(GDScriptParser::*ParseFunction)(ExpressionNode *p_previous_operand, bool p_can_assign);
  1220. // Higher value means higher precedence (i.e. is evaluated first).
  1221. enum Precedence {
  1222. PREC_NONE,
  1223. PREC_ASSIGNMENT,
  1224. PREC_CAST,
  1225. PREC_TERNARY,
  1226. PREC_LOGIC_OR,
  1227. PREC_LOGIC_AND,
  1228. PREC_LOGIC_NOT,
  1229. PREC_CONTENT_TEST,
  1230. PREC_COMPARISON,
  1231. PREC_BIT_OR,
  1232. PREC_BIT_XOR,
  1233. PREC_BIT_AND,
  1234. PREC_BIT_SHIFT,
  1235. PREC_ADDITION_SUBTRACTION,
  1236. PREC_FACTOR,
  1237. PREC_SIGN,
  1238. PREC_BIT_NOT,
  1239. PREC_POWER,
  1240. PREC_TYPE_TEST,
  1241. PREC_AWAIT,
  1242. PREC_CALL,
  1243. PREC_ATTRIBUTE,
  1244. PREC_SUBSCRIPT,
  1245. PREC_PRIMARY,
  1246. };
  1247. struct ParseRule {
  1248. ParseFunction prefix = nullptr;
  1249. ParseFunction infix = nullptr;
  1250. Precedence precedence = PREC_NONE;
  1251. };
  1252. static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
  1253. List<Node *> nodes_in_progress;
  1254. void complete_extents(Node *p_node);
  1255. void update_extents(Node *p_node);
  1256. void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
  1257. void reset_extents(Node *p_node, Node *p_from);
  1258. template <typename T>
  1259. T *alloc_node() {
  1260. T *node = memnew(T);
  1261. node->next = list;
  1262. list = node;
  1263. reset_extents(node, previous);
  1264. nodes_in_progress.push_back(node);
  1265. return node;
  1266. }
  1267. void clear();
  1268. void push_error(const String &p_message, const Node *p_origin = nullptr);
  1269. #ifdef DEBUG_ENABLED
  1270. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
  1271. template <typename... Symbols>
  1272. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Symbols &...p_symbols) {
  1273. push_warning(p_source, p_code, Vector<String>{ p_symbols... });
  1274. }
  1275. void apply_pending_warnings();
  1276. #endif
  1277. // Setting p_force to false will prevent the completion context from being update if a context was already set before.
  1278. // This should only be done when we push context before we consumed any tokens for the corresponding structure.
  1279. // See parse_precedence for an example.
  1280. void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = true);
  1281. void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = true);
  1282. // In some cases it might become necessary to alter the completion context after parsing a subexpression.
  1283. // For example to not override COMPLETE_CALL_ARGUMENTS with COMPLETION_NONE from string literals.
  1284. void override_completion_context(const Node *p_for_node, CompletionType p_type, Node *p_node, int p_argument = -1);
  1285. void push_completion_call(Node *p_call);
  1286. void pop_completion_call();
  1287. void set_last_completion_call_arg(int p_argument);
  1288. GDScriptTokenizer::Token advance();
  1289. bool match(GDScriptTokenizer::Token::Type p_token_type);
  1290. bool check(GDScriptTokenizer::Token::Type p_token_type) const;
  1291. bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
  1292. bool is_at_end() const;
  1293. bool is_statement_end_token() const;
  1294. bool is_statement_end() const;
  1295. void end_statement(const String &p_context);
  1296. void synchronize();
  1297. void push_multiline(bool p_state);
  1298. void pop_multiline();
  1299. // Main blocks.
  1300. void parse_program();
  1301. ClassNode *parse_class(bool p_is_static);
  1302. void parse_class_name();
  1303. void parse_extends();
  1304. void parse_class_body(bool p_is_multiline);
  1305. template <typename T>
  1306. void parse_class_member(T *(GDScriptParser::*p_parse_function)(bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_static = false);
  1307. SignalNode *parse_signal(bool p_is_static);
  1308. EnumNode *parse_enum(bool p_is_static);
  1309. ParameterNode *parse_parameter();
  1310. FunctionNode *parse_function(bool p_is_static);
  1311. void parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type);
  1312. SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr, bool p_for_lambda = false);
  1313. // Annotations
  1314. AnnotationNode *parse_annotation(uint32_t p_valid_targets);
  1315. 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);
  1316. bool validate_annotation_arguments(AnnotationNode *p_annotation);
  1317. void clear_unused_annotations();
  1318. bool tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1319. bool icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1320. bool onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1321. template <PropertyHint t_hint, Variant::Type t_type>
  1322. bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1323. bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1324. bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1325. bool export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1326. template <PropertyUsageFlags t_usage>
  1327. bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1328. bool warning_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1329. bool rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1330. bool static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1331. // Statements.
  1332. Node *parse_statement();
  1333. VariableNode *parse_variable(bool p_is_static);
  1334. VariableNode *parse_variable(bool p_is_static, bool p_allow_property);
  1335. VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
  1336. void parse_property_getter(VariableNode *p_variable);
  1337. void parse_property_setter(VariableNode *p_variable);
  1338. ConstantNode *parse_constant(bool p_is_static);
  1339. AssertNode *parse_assert();
  1340. BreakNode *parse_break();
  1341. ContinueNode *parse_continue();
  1342. ForNode *parse_for();
  1343. IfNode *parse_if(const String &p_token = "if");
  1344. MatchNode *parse_match();
  1345. MatchBranchNode *parse_match_branch();
  1346. PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
  1347. WhileNode *parse_while();
  1348. // Expressions.
  1349. ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
  1350. ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
  1351. ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
  1352. LiteralNode *parse_literal();
  1353. ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
  1354. ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
  1355. IdentifierNode *parse_identifier();
  1356. ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
  1357. ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1358. ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1359. ExpressionNode *parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1360. ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1361. ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
  1362. ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
  1363. ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
  1364. ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
  1365. ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
  1366. ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
  1367. ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
  1368. ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
  1369. ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
  1370. ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
  1371. ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
  1372. ExpressionNode *parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign);
  1373. ExpressionNode *parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign);
  1374. ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
  1375. ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
  1376. TypeNode *parse_type(bool p_allow_void = false);
  1377. #ifdef TOOLS_ENABLED
  1378. int max_script_doc_line = INT_MAX;
  1379. int min_member_doc_line = 1;
  1380. bool has_comment(int p_line, bool p_must_be_doc = false);
  1381. MemberDocData parse_doc_comment(int p_line, bool p_single_line = false);
  1382. ClassDocData parse_class_doc_comment(int p_line, bool p_single_line = false);
  1383. #endif // TOOLS_ENABLED
  1384. public:
  1385. Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body = true);
  1386. Error parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path);
  1387. ClassNode *get_tree() const { return head; }
  1388. bool is_tool() const { return _is_tool; }
  1389. Ref<GDScriptParserRef> get_depended_parser_for(const String &p_path);
  1390. const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers();
  1391. ClassNode *find_class(const String &p_qualified_name) const;
  1392. bool has_class(const GDScriptParser::ClassNode *p_class) const;
  1393. static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`.
  1394. CompletionContext get_completion_context() const { return completion_context; }
  1395. CompletionCall get_completion_call() const { return completion_call; }
  1396. void get_annotation_list(List<MethodInfo> *r_annotations) const;
  1397. bool annotation_exists(const String &p_annotation_name) const;
  1398. const List<ParserError> &get_errors() const { return errors; }
  1399. const List<String> get_dependencies() const {
  1400. // TODO: Keep track of deps.
  1401. return List<String>();
  1402. }
  1403. #ifdef DEBUG_ENABLED
  1404. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  1405. const HashSet<int> &get_unsafe_lines() const { return unsafe_lines; }
  1406. int get_last_line_number() const { return current.end_line; }
  1407. #endif
  1408. #ifdef TOOLS_ENABLED
  1409. static HashMap<String, String> theme_color_names;
  1410. HashMap<int, GDScriptTokenizer::CommentData> comment_data;
  1411. #endif // TOOLS_ENABLED
  1412. GDScriptParser();
  1413. ~GDScriptParser();
  1414. #ifdef DEBUG_ENABLED
  1415. class TreePrinter {
  1416. int indent_level = 0;
  1417. String indent;
  1418. StringBuilder printed;
  1419. bool pending_indent = false;
  1420. void increase_indent();
  1421. void decrease_indent();
  1422. void push_line(const String &p_line = String());
  1423. void push_text(const String &p_text);
  1424. void print_annotation(const AnnotationNode *p_annotation);
  1425. void print_array(ArrayNode *p_array);
  1426. void print_assert(AssertNode *p_assert);
  1427. void print_assignment(AssignmentNode *p_assignment);
  1428. void print_await(AwaitNode *p_await);
  1429. void print_binary_op(BinaryOpNode *p_binary_op);
  1430. void print_call(CallNode *p_call);
  1431. void print_cast(CastNode *p_cast);
  1432. void print_class(ClassNode *p_class);
  1433. void print_constant(ConstantNode *p_constant);
  1434. void print_dictionary(DictionaryNode *p_dictionary);
  1435. void print_expression(ExpressionNode *p_expression);
  1436. void print_enum(EnumNode *p_enum);
  1437. void print_for(ForNode *p_for);
  1438. void print_function(FunctionNode *p_function, const String &p_context = "Function");
  1439. void print_get_node(GetNodeNode *p_get_node);
  1440. void print_if(IfNode *p_if, bool p_is_elif = false);
  1441. void print_identifier(IdentifierNode *p_identifier);
  1442. void print_lambda(LambdaNode *p_lambda);
  1443. void print_literal(LiteralNode *p_literal);
  1444. void print_match(MatchNode *p_match);
  1445. void print_match_branch(MatchBranchNode *p_match_branch);
  1446. void print_match_pattern(PatternNode *p_match_pattern);
  1447. void print_parameter(ParameterNode *p_parameter);
  1448. void print_preload(PreloadNode *p_preload);
  1449. void print_return(ReturnNode *p_return);
  1450. void print_self(SelfNode *p_self);
  1451. void print_signal(SignalNode *p_signal);
  1452. void print_statement(Node *p_statement);
  1453. void print_subscript(SubscriptNode *p_subscript);
  1454. void print_suite(SuiteNode *p_suite);
  1455. void print_ternary_op(TernaryOpNode *p_ternary_op);
  1456. void print_type(TypeNode *p_type);
  1457. void print_type_test(TypeTestNode *p_type_test);
  1458. void print_unary_op(UnaryOpNode *p_unary_op);
  1459. void print_variable(VariableNode *p_variable);
  1460. void print_while(WhileNode *p_while);
  1461. public:
  1462. void print_tree(const GDScriptParser &p_parser);
  1463. };
  1464. #endif // DEBUG_ENABLED
  1465. static void cleanup();
  1466. };
  1467. #endif // GDSCRIPT_PARSER_H