gdscript_parser.h 43 KB

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