gdscript_parser.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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. String uid_string;
  655. Vector2i uid_lines = Vector2i(-1, -1);
  656. Vector<Member> members;
  657. HashMap<StringName, int> members_indices;
  658. ClassNode *outer = nullptr;
  659. bool extends_used = false;
  660. bool onready_used = false;
  661. bool has_static_data = false;
  662. bool annotated_static_unload = false;
  663. String extends_path;
  664. Vector<IdentifierNode *> extends; // List for indexing: extends A.B.C
  665. DataType base_type;
  666. String fqcn; // Fully-qualified class name. Identifies uniquely any class in the project.
  667. #ifdef TOOLS_ENABLED
  668. ClassDocData doc_data;
  669. // EnumValue docs are parsed after itself, so we need a method to add/modify the doc property later.
  670. void set_enum_value_doc_data(const StringName &p_name, const MemberDocData &p_doc_data) {
  671. ERR_FAIL_INDEX(members_indices[p_name], members.size());
  672. members.write[members_indices[p_name]].enum_value.doc_data = p_doc_data;
  673. }
  674. #endif // TOOLS_ENABLED
  675. bool resolved_interface = false;
  676. bool resolved_body = false;
  677. StringName get_global_name() const {
  678. return (outer == nullptr && identifier != nullptr) ? identifier->name : StringName();
  679. }
  680. Member get_member(const StringName &p_name) const {
  681. return members[members_indices[p_name]];
  682. }
  683. bool has_member(const StringName &p_name) const {
  684. return members_indices.has(p_name);
  685. }
  686. bool has_function(const StringName &p_name) const {
  687. return has_member(p_name) && members[members_indices[p_name]].type == Member::FUNCTION;
  688. }
  689. template <class T>
  690. void add_member(T *p_member_node) {
  691. members_indices[p_member_node->identifier->name] = members.size();
  692. members.push_back(Member(p_member_node));
  693. }
  694. void add_member(const EnumNode::Value &p_enum_value) {
  695. members_indices[p_enum_value.identifier->name] = members.size();
  696. members.push_back(Member(p_enum_value));
  697. }
  698. void add_member_group(AnnotationNode *p_annotation_node) {
  699. // Avoid name conflict. See GH-78252.
  700. StringName name = vformat("@group_%d_%s", members.size(), p_annotation_node->export_info.name);
  701. members_indices[name] = members.size();
  702. members.push_back(Member(p_annotation_node));
  703. }
  704. ClassNode() {
  705. type = CLASS;
  706. }
  707. };
  708. struct ConstantNode : public AssignableNode {
  709. #ifdef TOOLS_ENABLED
  710. MemberDocData doc_data;
  711. #endif // TOOLS_ENABLED
  712. ConstantNode() {
  713. type = CONSTANT;
  714. }
  715. };
  716. struct ContinueNode : public Node {
  717. ContinueNode() {
  718. type = CONTINUE;
  719. }
  720. };
  721. struct DictionaryNode : public ExpressionNode {
  722. struct Pair {
  723. ExpressionNode *key = nullptr;
  724. ExpressionNode *value = nullptr;
  725. };
  726. Vector<Pair> elements;
  727. enum Style {
  728. LUA_TABLE,
  729. PYTHON_DICT,
  730. };
  731. Style style = PYTHON_DICT;
  732. DictionaryNode() {
  733. type = DICTIONARY;
  734. }
  735. };
  736. struct ForNode : public Node {
  737. IdentifierNode *variable = nullptr;
  738. TypeNode *datatype_specifier = nullptr;
  739. bool use_conversion_assign = false;
  740. ExpressionNode *list = nullptr;
  741. SuiteNode *loop = nullptr;
  742. ForNode() {
  743. type = FOR;
  744. }
  745. };
  746. struct FunctionNode : public Node {
  747. IdentifierNode *identifier = nullptr;
  748. Vector<ParameterNode *> parameters;
  749. HashMap<StringName, int> parameters_indices;
  750. TypeNode *return_type = nullptr;
  751. SuiteNode *body = nullptr;
  752. bool is_static = false; // For lambdas it's determined in the analyzer.
  753. bool is_coroutine = false;
  754. Variant rpc_config;
  755. MethodInfo info;
  756. LambdaNode *source_lambda = nullptr;
  757. Vector<Variant> default_arg_values;
  758. #ifdef TOOLS_ENABLED
  759. MemberDocData doc_data;
  760. #endif // TOOLS_ENABLED
  761. bool resolved_signature = false;
  762. bool resolved_body = false;
  763. FunctionNode() {
  764. type = FUNCTION;
  765. }
  766. };
  767. struct GetNodeNode : public ExpressionNode {
  768. String full_path;
  769. bool use_dollar = true;
  770. GetNodeNode() {
  771. type = GET_NODE;
  772. }
  773. };
  774. struct IdentifierNode : public ExpressionNode {
  775. StringName name;
  776. SuiteNode *suite = nullptr; // The block in which the identifier is used.
  777. enum Source {
  778. UNDEFINED_SOURCE,
  779. FUNCTION_PARAMETER,
  780. LOCAL_VARIABLE,
  781. LOCAL_CONSTANT,
  782. LOCAL_ITERATOR, // `for` loop iterator.
  783. LOCAL_BIND, // Pattern bind.
  784. MEMBER_VARIABLE,
  785. MEMBER_CONSTANT,
  786. MEMBER_FUNCTION,
  787. MEMBER_SIGNAL,
  788. MEMBER_CLASS,
  789. INHERITED_VARIABLE,
  790. STATIC_VARIABLE,
  791. };
  792. Source source = UNDEFINED_SOURCE;
  793. union {
  794. ParameterNode *parameter_source = nullptr;
  795. ConstantNode *constant_source;
  796. VariableNode *variable_source;
  797. IdentifierNode *bind_source;
  798. };
  799. FunctionNode *source_function = nullptr;
  800. int usages = 0; // Useful for binds/iterator variable.
  801. IdentifierNode() {
  802. type = IDENTIFIER;
  803. }
  804. };
  805. struct IfNode : public Node {
  806. ExpressionNode *condition = nullptr;
  807. SuiteNode *true_block = nullptr;
  808. SuiteNode *false_block = nullptr;
  809. IfNode() {
  810. type = IF;
  811. }
  812. };
  813. struct LambdaNode : public ExpressionNode {
  814. FunctionNode *function = nullptr;
  815. FunctionNode *parent_function = nullptr;
  816. LambdaNode *parent_lambda = nullptr;
  817. Vector<IdentifierNode *> captures;
  818. HashMap<StringName, int> captures_indices;
  819. bool use_self = false;
  820. bool has_name() const {
  821. return function && function->identifier;
  822. }
  823. LambdaNode() {
  824. type = LAMBDA;
  825. }
  826. };
  827. struct LiteralNode : public ExpressionNode {
  828. Variant value;
  829. LiteralNode() {
  830. type = LITERAL;
  831. }
  832. };
  833. struct MatchNode : public Node {
  834. ExpressionNode *test = nullptr;
  835. Vector<MatchBranchNode *> branches;
  836. MatchNode() {
  837. type = MATCH;
  838. }
  839. };
  840. struct MatchBranchNode : public Node {
  841. Vector<PatternNode *> patterns;
  842. SuiteNode *block = nullptr;
  843. bool has_wildcard = false;
  844. SuiteNode *guard_body = nullptr;
  845. MatchBranchNode() {
  846. type = MATCH_BRANCH;
  847. }
  848. };
  849. struct ParameterNode : public AssignableNode {
  850. ParameterNode() {
  851. type = PARAMETER;
  852. }
  853. };
  854. struct PassNode : public Node {
  855. PassNode() {
  856. type = PASS;
  857. }
  858. };
  859. struct PatternNode : public Node {
  860. enum Type {
  861. PT_LITERAL,
  862. PT_EXPRESSION,
  863. PT_BIND,
  864. PT_ARRAY,
  865. PT_DICTIONARY,
  866. PT_REST,
  867. PT_WILDCARD,
  868. };
  869. Type pattern_type = PT_LITERAL;
  870. union {
  871. LiteralNode *literal = nullptr;
  872. IdentifierNode *bind;
  873. ExpressionNode *expression;
  874. };
  875. Vector<PatternNode *> array;
  876. bool rest_used = false; // For array/dict patterns.
  877. struct Pair {
  878. ExpressionNode *key = nullptr;
  879. PatternNode *value_pattern = nullptr;
  880. };
  881. Vector<Pair> dictionary;
  882. HashMap<StringName, IdentifierNode *> binds;
  883. bool has_bind(const StringName &p_name);
  884. IdentifierNode *get_bind(const StringName &p_name);
  885. PatternNode() {
  886. type = PATTERN;
  887. }
  888. };
  889. struct PreloadNode : public ExpressionNode {
  890. ExpressionNode *path = nullptr;
  891. String resolved_path;
  892. Ref<Resource> resource;
  893. PreloadNode() {
  894. type = PRELOAD;
  895. }
  896. };
  897. struct ReturnNode : public Node {
  898. ExpressionNode *return_value = nullptr;
  899. bool void_return = false;
  900. ReturnNode() {
  901. type = RETURN;
  902. }
  903. };
  904. struct SelfNode : public ExpressionNode {
  905. ClassNode *current_class = nullptr;
  906. SelfNode() {
  907. type = SELF;
  908. }
  909. };
  910. struct SignalNode : public Node {
  911. IdentifierNode *identifier = nullptr;
  912. Vector<ParameterNode *> parameters;
  913. HashMap<StringName, int> parameters_indices;
  914. MethodInfo method_info;
  915. #ifdef TOOLS_ENABLED
  916. MemberDocData doc_data;
  917. #endif // TOOLS_ENABLED
  918. SignalNode() {
  919. type = SIGNAL;
  920. }
  921. };
  922. struct SubscriptNode : public ExpressionNode {
  923. ExpressionNode *base = nullptr;
  924. union {
  925. ExpressionNode *index = nullptr;
  926. IdentifierNode *attribute;
  927. };
  928. bool is_attribute = false;
  929. SubscriptNode() {
  930. type = SUBSCRIPT;
  931. }
  932. };
  933. struct SuiteNode : public Node {
  934. SuiteNode *parent_block = nullptr;
  935. Vector<Node *> statements;
  936. struct Local {
  937. enum Type {
  938. UNDEFINED,
  939. CONSTANT,
  940. VARIABLE,
  941. PARAMETER,
  942. FOR_VARIABLE,
  943. PATTERN_BIND,
  944. };
  945. Type type = UNDEFINED;
  946. union {
  947. ConstantNode *constant = nullptr;
  948. VariableNode *variable;
  949. ParameterNode *parameter;
  950. IdentifierNode *bind;
  951. };
  952. StringName name;
  953. FunctionNode *source_function = nullptr;
  954. int start_line = 0, end_line = 0;
  955. int start_column = 0, end_column = 0;
  956. int leftmost_column = 0, rightmost_column = 0;
  957. DataType get_datatype() const;
  958. String get_name() const;
  959. Local() {}
  960. Local(ConstantNode *p_constant, FunctionNode *p_source_function) {
  961. type = CONSTANT;
  962. constant = p_constant;
  963. name = p_constant->identifier->name;
  964. source_function = p_source_function;
  965. start_line = p_constant->start_line;
  966. end_line = p_constant->end_line;
  967. start_column = p_constant->start_column;
  968. end_column = p_constant->end_column;
  969. leftmost_column = p_constant->leftmost_column;
  970. rightmost_column = p_constant->rightmost_column;
  971. }
  972. Local(VariableNode *p_variable, FunctionNode *p_source_function) {
  973. type = VARIABLE;
  974. variable = p_variable;
  975. name = p_variable->identifier->name;
  976. source_function = p_source_function;
  977. start_line = p_variable->start_line;
  978. end_line = p_variable->end_line;
  979. start_column = p_variable->start_column;
  980. end_column = p_variable->end_column;
  981. leftmost_column = p_variable->leftmost_column;
  982. rightmost_column = p_variable->rightmost_column;
  983. }
  984. Local(ParameterNode *p_parameter, FunctionNode *p_source_function) {
  985. type = PARAMETER;
  986. parameter = p_parameter;
  987. name = p_parameter->identifier->name;
  988. source_function = p_source_function;
  989. start_line = p_parameter->start_line;
  990. end_line = p_parameter->end_line;
  991. start_column = p_parameter->start_column;
  992. end_column = p_parameter->end_column;
  993. leftmost_column = p_parameter->leftmost_column;
  994. rightmost_column = p_parameter->rightmost_column;
  995. }
  996. Local(IdentifierNode *p_identifier, FunctionNode *p_source_function) {
  997. type = FOR_VARIABLE;
  998. bind = p_identifier;
  999. name = p_identifier->name;
  1000. source_function = p_source_function;
  1001. start_line = p_identifier->start_line;
  1002. end_line = p_identifier->end_line;
  1003. start_column = p_identifier->start_column;
  1004. end_column = p_identifier->end_column;
  1005. leftmost_column = p_identifier->leftmost_column;
  1006. rightmost_column = p_identifier->rightmost_column;
  1007. }
  1008. };
  1009. Local empty;
  1010. Vector<Local> locals;
  1011. HashMap<StringName, int> locals_indices;
  1012. FunctionNode *parent_function = nullptr;
  1013. IfNode *parent_if = nullptr;
  1014. bool has_return = false;
  1015. bool has_continue = false;
  1016. bool has_unreachable_code = false; // Just so warnings aren't given more than once per block.
  1017. bool is_in_loop = false; // The block is nested in a loop (directly or indirectly).
  1018. bool has_local(const StringName &p_name) const;
  1019. const Local &get_local(const StringName &p_name) const;
  1020. template <class T>
  1021. void add_local(T *p_local, FunctionNode *p_source_function) {
  1022. locals_indices[p_local->identifier->name] = locals.size();
  1023. locals.push_back(Local(p_local, p_source_function));
  1024. }
  1025. void add_local(const Local &p_local) {
  1026. locals_indices[p_local.name] = locals.size();
  1027. locals.push_back(p_local);
  1028. }
  1029. SuiteNode() {
  1030. type = SUITE;
  1031. }
  1032. };
  1033. struct TernaryOpNode : public ExpressionNode {
  1034. // Only one ternary operation exists, so no abstraction here.
  1035. ExpressionNode *condition = nullptr;
  1036. ExpressionNode *true_expr = nullptr;
  1037. ExpressionNode *false_expr = nullptr;
  1038. TernaryOpNode() {
  1039. type = TERNARY_OPERATOR;
  1040. }
  1041. };
  1042. struct TypeNode : public Node {
  1043. Vector<IdentifierNode *> type_chain;
  1044. Vector<TypeNode *> container_types;
  1045. TypeNode *get_container_type_or_null(int p_index) const {
  1046. return p_index >= 0 && p_index < container_types.size() ? container_types[p_index] : nullptr;
  1047. }
  1048. TypeNode() {
  1049. type = TYPE;
  1050. }
  1051. };
  1052. struct TypeTestNode : public ExpressionNode {
  1053. ExpressionNode *operand = nullptr;
  1054. TypeNode *test_type = nullptr;
  1055. DataType test_datatype;
  1056. TypeTestNode() {
  1057. type = TYPE_TEST;
  1058. }
  1059. };
  1060. struct UnaryOpNode : public ExpressionNode {
  1061. enum OpType {
  1062. OP_POSITIVE,
  1063. OP_NEGATIVE,
  1064. OP_COMPLEMENT,
  1065. OP_LOGIC_NOT,
  1066. };
  1067. OpType operation = OP_POSITIVE;
  1068. Variant::Operator variant_op = Variant::OP_MAX;
  1069. ExpressionNode *operand = nullptr;
  1070. UnaryOpNode() {
  1071. type = UNARY_OPERATOR;
  1072. }
  1073. };
  1074. struct VariableNode : public AssignableNode {
  1075. enum PropertyStyle {
  1076. PROP_NONE,
  1077. PROP_INLINE,
  1078. PROP_SETGET,
  1079. };
  1080. PropertyStyle property = PROP_NONE;
  1081. union {
  1082. FunctionNode *setter = nullptr;
  1083. IdentifierNode *setter_pointer;
  1084. };
  1085. IdentifierNode *setter_parameter = nullptr;
  1086. union {
  1087. FunctionNode *getter = nullptr;
  1088. IdentifierNode *getter_pointer;
  1089. };
  1090. bool exported = false;
  1091. bool onready = false;
  1092. PropertyInfo export_info;
  1093. int assignments = 0;
  1094. bool is_static = false;
  1095. #ifdef TOOLS_ENABLED
  1096. MemberDocData doc_data;
  1097. #endif // TOOLS_ENABLED
  1098. VariableNode() {
  1099. type = VARIABLE;
  1100. }
  1101. };
  1102. struct WhileNode : public Node {
  1103. ExpressionNode *condition = nullptr;
  1104. SuiteNode *loop = nullptr;
  1105. WhileNode() {
  1106. type = WHILE;
  1107. }
  1108. };
  1109. enum CompletionType {
  1110. COMPLETION_NONE,
  1111. COMPLETION_ANNOTATION, // Annotation (following @).
  1112. COMPLETION_ANNOTATION_ARGUMENTS, // Annotation arguments hint.
  1113. COMPLETION_ASSIGN, // Assignment based on type (e.g. enum values).
  1114. COMPLETION_ATTRIBUTE, // After id.| to look for members.
  1115. COMPLETION_ATTRIBUTE_METHOD, // After id.| to look for methods.
  1116. 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).
  1117. COMPLETION_CALL_ARGUMENTS, // Complete with nodes, input actions, enum values (or usual expressions).
  1118. // TODO: COMPLETION_DECLARATION, // Potential declaration (var, const, func).
  1119. COMPLETION_GET_NODE, // Get node with $ notation.
  1120. COMPLETION_IDENTIFIER, // List available identifiers in scope.
  1121. COMPLETION_INHERIT_TYPE, // Type after extends. Exclude non-viable types (built-ins, enums, void). Includes subtypes using the argument index.
  1122. COMPLETION_METHOD, // List available methods in scope.
  1123. COMPLETION_OVERRIDE_METHOD, // Override implementation, also for native virtuals.
  1124. COMPLETION_PROPERTY_DECLARATION, // Property declaration (get, set).
  1125. COMPLETION_PROPERTY_DECLARATION_OR_TYPE, // Property declaration (get, set) or a type hint.
  1126. COMPLETION_PROPERTY_METHOD, // Property setter or getter (list available methods).
  1127. COMPLETION_RESOURCE_PATH, // For load/preload.
  1128. COMPLETION_SUBSCRIPT, // Inside id[|].
  1129. COMPLETION_SUPER_METHOD, // After super.
  1130. COMPLETION_TYPE_ATTRIBUTE, // Attribute in type name (Type.|).
  1131. COMPLETION_TYPE_NAME, // Name of type (after :).
  1132. COMPLETION_TYPE_NAME_OR_VOID, // Same as TYPE_NAME, but allows void (in function return type).
  1133. };
  1134. struct CompletionContext {
  1135. CompletionType type = COMPLETION_NONE;
  1136. ClassNode *current_class = nullptr;
  1137. FunctionNode *current_function = nullptr;
  1138. SuiteNode *current_suite = nullptr;
  1139. int current_line = -1;
  1140. int current_argument = -1;
  1141. Variant::Type builtin_type = Variant::VARIANT_MAX;
  1142. Node *node = nullptr;
  1143. Object *base = nullptr;
  1144. List<Ref<GDScriptParserRef>> dependent_parsers;
  1145. };
  1146. struct CompletionCall {
  1147. Node *call = nullptr;
  1148. int argument = -1;
  1149. };
  1150. private:
  1151. friend class GDScriptAnalyzer;
  1152. bool _is_tool = false;
  1153. bool _has_uid = false;
  1154. String script_path;
  1155. bool for_completion = false;
  1156. bool panic_mode = false;
  1157. bool can_break = false;
  1158. bool can_continue = false;
  1159. List<bool> multiline_stack;
  1160. ClassNode *head = nullptr;
  1161. Node *list = nullptr;
  1162. List<ParserError> errors;
  1163. #ifdef DEBUG_ENABLED
  1164. bool is_ignoring_warnings = false;
  1165. List<GDScriptWarning> warnings;
  1166. HashSet<GDScriptWarning::Code> ignored_warnings;
  1167. HashSet<int> unsafe_lines;
  1168. #endif
  1169. GDScriptTokenizer tokenizer;
  1170. GDScriptTokenizer::Token previous;
  1171. GDScriptTokenizer::Token current;
  1172. ClassNode *current_class = nullptr;
  1173. FunctionNode *current_function = nullptr;
  1174. LambdaNode *current_lambda = nullptr;
  1175. SuiteNode *current_suite = nullptr;
  1176. CompletionContext completion_context;
  1177. CompletionCall completion_call;
  1178. List<CompletionCall> completion_call_stack;
  1179. bool passed_cursor = false;
  1180. bool in_lambda = false;
  1181. bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
  1182. typedef bool (GDScriptParser::*AnnotationAction)(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1183. struct AnnotationInfo {
  1184. enum TargetKind {
  1185. NONE = 0,
  1186. SCRIPT = 1 << 0,
  1187. CLASS = 1 << 1,
  1188. VARIABLE = 1 << 2,
  1189. CONSTANT = 1 << 3,
  1190. SIGNAL = 1 << 4,
  1191. FUNCTION = 1 << 5,
  1192. STATEMENT = 1 << 6,
  1193. STANDALONE = 1 << 7,
  1194. CLASS_LEVEL = CLASS | VARIABLE | FUNCTION,
  1195. };
  1196. uint32_t target_kind = 0; // Flags.
  1197. AnnotationAction apply = nullptr;
  1198. MethodInfo info;
  1199. };
  1200. static HashMap<StringName, AnnotationInfo> valid_annotations;
  1201. List<AnnotationNode *> annotation_stack;
  1202. typedef ExpressionNode *(GDScriptParser::*ParseFunction)(ExpressionNode *p_previous_operand, bool p_can_assign);
  1203. // Higher value means higher precedence (i.e. is evaluated first).
  1204. enum Precedence {
  1205. PREC_NONE,
  1206. PREC_ASSIGNMENT,
  1207. PREC_CAST,
  1208. PREC_TERNARY,
  1209. PREC_LOGIC_OR,
  1210. PREC_LOGIC_AND,
  1211. PREC_LOGIC_NOT,
  1212. PREC_CONTENT_TEST,
  1213. PREC_COMPARISON,
  1214. PREC_BIT_OR,
  1215. PREC_BIT_XOR,
  1216. PREC_BIT_AND,
  1217. PREC_BIT_SHIFT,
  1218. PREC_ADDITION_SUBTRACTION,
  1219. PREC_FACTOR,
  1220. PREC_SIGN,
  1221. PREC_BIT_NOT,
  1222. PREC_POWER,
  1223. PREC_TYPE_TEST,
  1224. PREC_AWAIT,
  1225. PREC_CALL,
  1226. PREC_ATTRIBUTE,
  1227. PREC_SUBSCRIPT,
  1228. PREC_PRIMARY,
  1229. };
  1230. struct ParseRule {
  1231. ParseFunction prefix = nullptr;
  1232. ParseFunction infix = nullptr;
  1233. Precedence precedence = PREC_NONE;
  1234. };
  1235. static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
  1236. List<Node *> nodes_in_progress;
  1237. void complete_extents(Node *p_node);
  1238. void update_extents(Node *p_node);
  1239. void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
  1240. void reset_extents(Node *p_node, Node *p_from);
  1241. template <class T>
  1242. T *alloc_node() {
  1243. T *node = memnew(T);
  1244. node->next = list;
  1245. list = node;
  1246. reset_extents(node, previous);
  1247. nodes_in_progress.push_back(node);
  1248. return node;
  1249. }
  1250. void clear();
  1251. void push_error(const String &p_message, const Node *p_origin = nullptr);
  1252. #ifdef DEBUG_ENABLED
  1253. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
  1254. template <typename... Symbols>
  1255. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Symbols &...p_symbols) {
  1256. push_warning(p_source, p_code, Vector<String>{ p_symbols... });
  1257. }
  1258. #endif
  1259. void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = false);
  1260. void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = false);
  1261. void push_completion_call(Node *p_call);
  1262. void pop_completion_call();
  1263. void set_last_completion_call_arg(int p_argument);
  1264. GDScriptTokenizer::Token advance();
  1265. bool match(GDScriptTokenizer::Token::Type p_token_type);
  1266. bool check(GDScriptTokenizer::Token::Type p_token_type) const;
  1267. bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
  1268. bool is_at_end() const;
  1269. bool is_statement_end_token() const;
  1270. bool is_statement_end() const;
  1271. void end_statement(const String &p_context);
  1272. void synchronize();
  1273. void push_multiline(bool p_state);
  1274. void pop_multiline();
  1275. // Main blocks.
  1276. void parse_program();
  1277. ClassNode *parse_class(bool p_is_static);
  1278. void parse_class_name();
  1279. void parse_extends();
  1280. void parse_class_body(bool p_is_multiline);
  1281. template <class T>
  1282. void parse_class_member(T *(GDScriptParser::*p_parse_function)(bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_static = false);
  1283. SignalNode *parse_signal(bool p_is_static);
  1284. EnumNode *parse_enum(bool p_is_static);
  1285. ParameterNode *parse_parameter();
  1286. FunctionNode *parse_function(bool p_is_static);
  1287. void parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type);
  1288. SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr, bool p_for_lambda = false);
  1289. // Annotations
  1290. AnnotationNode *parse_annotation(uint32_t p_valid_targets);
  1291. 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);
  1292. bool validate_annotation_arguments(AnnotationNode *p_annotation);
  1293. void clear_unused_annotations();
  1294. bool uid_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1295. bool tool_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1296. bool icon_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1297. bool onready_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1298. template <PropertyHint t_hint, Variant::Type t_type>
  1299. bool export_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1300. template <PropertyUsageFlags t_usage>
  1301. bool export_group_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1302. bool warning_annotations(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1303. bool rpc_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1304. bool static_unload_annotation(const AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1305. // Statements.
  1306. Node *parse_statement();
  1307. VariableNode *parse_variable(bool p_is_static);
  1308. VariableNode *parse_variable(bool p_is_static, bool p_allow_property);
  1309. VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
  1310. void parse_property_getter(VariableNode *p_variable);
  1311. void parse_property_setter(VariableNode *p_variable);
  1312. ConstantNode *parse_constant(bool p_is_static);
  1313. AssertNode *parse_assert();
  1314. BreakNode *parse_break();
  1315. ContinueNode *parse_continue();
  1316. ForNode *parse_for();
  1317. IfNode *parse_if(const String &p_token = "if");
  1318. MatchNode *parse_match();
  1319. MatchBranchNode *parse_match_branch();
  1320. PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
  1321. WhileNode *parse_while();
  1322. // Expressions.
  1323. ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
  1324. ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
  1325. ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
  1326. LiteralNode *parse_literal();
  1327. ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
  1328. ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
  1329. IdentifierNode *parse_identifier();
  1330. ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
  1331. ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1332. ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1333. ExpressionNode *parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1334. ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1335. ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
  1336. ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
  1337. ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
  1338. ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
  1339. ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
  1340. ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
  1341. ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
  1342. ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
  1343. ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
  1344. ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
  1345. ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
  1346. ExpressionNode *parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign);
  1347. ExpressionNode *parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign);
  1348. ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
  1349. ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
  1350. TypeNode *parse_type(bool p_allow_void = false);
  1351. #ifdef TOOLS_ENABLED
  1352. int max_script_doc_line = INT_MAX;
  1353. int min_member_doc_line = 1;
  1354. bool has_comment(int p_line, bool p_must_be_doc = false);
  1355. MemberDocData parse_doc_comment(int p_line, bool p_single_line = false);
  1356. ClassDocData parse_class_doc_comment(int p_line, bool p_single_line = false);
  1357. #endif // TOOLS_ENABLED
  1358. public:
  1359. Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion);
  1360. ClassNode *get_tree() const { return head; }
  1361. bool is_tool() const { return _is_tool; }
  1362. ClassNode *find_class(const String &p_qualified_name) const;
  1363. bool has_class(const GDScriptParser::ClassNode *p_class) const;
  1364. static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`.
  1365. CompletionContext get_completion_context() const { return completion_context; }
  1366. CompletionCall get_completion_call() const { return completion_call; }
  1367. void get_annotation_list(List<MethodInfo> *r_annotations) const;
  1368. bool annotation_exists(const String &p_annotation_name) const;
  1369. const List<ParserError> &get_errors() const { return errors; }
  1370. const List<String> get_dependencies() const {
  1371. // TODO: Keep track of deps.
  1372. return List<String>();
  1373. }
  1374. #ifdef DEBUG_ENABLED
  1375. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  1376. const HashSet<int> &get_unsafe_lines() const { return unsafe_lines; }
  1377. int get_last_line_number() const { return current.end_line; }
  1378. #endif
  1379. #ifdef TOOLS_ENABLED
  1380. static HashMap<String, String> theme_color_names;
  1381. #endif // TOOLS_ENABLED
  1382. GDScriptParser();
  1383. ~GDScriptParser();
  1384. #ifdef DEBUG_ENABLED
  1385. class TreePrinter {
  1386. int indent_level = 0;
  1387. String indent;
  1388. StringBuilder printed;
  1389. bool pending_indent = false;
  1390. void increase_indent();
  1391. void decrease_indent();
  1392. void push_line(const String &p_line = String());
  1393. void push_text(const String &p_text);
  1394. void print_annotation(const AnnotationNode *p_annotation);
  1395. void print_array(ArrayNode *p_array);
  1396. void print_assert(AssertNode *p_assert);
  1397. void print_assignment(AssignmentNode *p_assignment);
  1398. void print_await(AwaitNode *p_await);
  1399. void print_binary_op(BinaryOpNode *p_binary_op);
  1400. void print_call(CallNode *p_call);
  1401. void print_cast(CastNode *p_cast);
  1402. void print_class(ClassNode *p_class);
  1403. void print_constant(ConstantNode *p_constant);
  1404. void print_dictionary(DictionaryNode *p_dictionary);
  1405. void print_expression(ExpressionNode *p_expression);
  1406. void print_enum(EnumNode *p_enum);
  1407. void print_for(ForNode *p_for);
  1408. void print_function(FunctionNode *p_function, const String &p_context = "Function");
  1409. void print_get_node(GetNodeNode *p_get_node);
  1410. void print_if(IfNode *p_if, bool p_is_elif = false);
  1411. void print_identifier(IdentifierNode *p_identifier);
  1412. void print_lambda(LambdaNode *p_lambda);
  1413. void print_literal(LiteralNode *p_literal);
  1414. void print_match(MatchNode *p_match);
  1415. void print_match_branch(MatchBranchNode *p_match_branch);
  1416. void print_match_pattern(PatternNode *p_match_pattern);
  1417. void print_parameter(ParameterNode *p_parameter);
  1418. void print_preload(PreloadNode *p_preload);
  1419. void print_return(ReturnNode *p_return);
  1420. void print_self(SelfNode *p_self);
  1421. void print_signal(SignalNode *p_signal);
  1422. void print_statement(Node *p_statement);
  1423. void print_subscript(SubscriptNode *p_subscript);
  1424. void print_suite(SuiteNode *p_suite);
  1425. void print_ternary_op(TernaryOpNode *p_ternary_op);
  1426. void print_type(TypeNode *p_type);
  1427. void print_type_test(TypeTestNode *p_type_test);
  1428. void print_unary_op(UnaryOpNode *p_unary_op);
  1429. void print_variable(VariableNode *p_variable);
  1430. void print_while(WhileNode *p_while);
  1431. public:
  1432. void print_tree(const GDScriptParser &p_parser);
  1433. };
  1434. #endif // DEBUG_ENABLED
  1435. static void cleanup();
  1436. };
  1437. #endif // GDSCRIPT_PARSER_H