gdscript_parser.h 43 KB

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