gdscript_parser.h 43 KB

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