gdscript_parser.h 44 KB

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