gdscript_parser.h 45 KB

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