gdscript_parser.h 43 KB

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