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