gdscript_parser.h 38 KB

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