gdscript_parser.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. /*************************************************************************/
  2. /* gdscript_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. LiteralNode *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_ADDITION_SUBTRACTION,
  1028. PREC_FACTOR,
  1029. PREC_SIGN,
  1030. PREC_BIT_NOT,
  1031. PREC_TYPE_TEST,
  1032. PREC_AWAIT,
  1033. PREC_CALL,
  1034. PREC_ATTRIBUTE,
  1035. PREC_SUBSCRIPT,
  1036. PREC_PRIMARY,
  1037. };
  1038. struct ParseRule {
  1039. ParseFunction prefix = nullptr;
  1040. ParseFunction infix = nullptr;
  1041. Precedence precedence = PREC_NONE;
  1042. };
  1043. static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
  1044. template <class T>
  1045. T *alloc_node() {
  1046. T *node = memnew(T);
  1047. node->next = list;
  1048. list = node;
  1049. // TODO: Properly set positions for all nodes.
  1050. node->start_line = previous.start_line;
  1051. node->end_line = previous.end_line;
  1052. node->start_column = previous.start_column;
  1053. node->end_column = previous.end_column;
  1054. node->leftmost_column = previous.leftmost_column;
  1055. node->rightmost_column = previous.rightmost_column;
  1056. return node;
  1057. }
  1058. void clear();
  1059. void push_error(const String &p_message, const Node *p_origin = nullptr);
  1060. #ifdef DEBUG_ENABLED
  1061. 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());
  1062. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
  1063. #endif
  1064. void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = false);
  1065. void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = false);
  1066. void push_completion_call(Node *p_call);
  1067. void pop_completion_call();
  1068. void set_last_completion_call_arg(int p_argument);
  1069. GDScriptTokenizer::Token advance();
  1070. bool match(GDScriptTokenizer::Token::Type p_token_type);
  1071. bool check(GDScriptTokenizer::Token::Type p_token_type);
  1072. bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
  1073. bool is_at_end();
  1074. bool is_statement_end();
  1075. void end_statement(const String &p_context);
  1076. void synchronize();
  1077. void push_multiline(bool p_state);
  1078. void pop_multiline();
  1079. // Main blocks.
  1080. void parse_program();
  1081. ClassNode *parse_class();
  1082. void parse_class_name();
  1083. void parse_extends();
  1084. void parse_class_body();
  1085. template <class T>
  1086. void parse_class_member(T *(GDScriptParser::*p_parse_function)(), AnnotationInfo::TargetKind p_target, const String &p_member_kind);
  1087. SignalNode *parse_signal();
  1088. EnumNode *parse_enum();
  1089. ParameterNode *parse_parameter();
  1090. FunctionNode *parse_function();
  1091. SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr);
  1092. // Annotations
  1093. AnnotationNode *parse_annotation(uint32_t p_valid_targets);
  1094. 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);
  1095. bool validate_annotation_arguments(AnnotationNode *p_annotation);
  1096. void clear_unused_annotations();
  1097. bool tool_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1098. bool icon_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1099. bool onready_annotation(const AnnotationNode *p_annotation, Node *p_target);
  1100. template <PropertyHint t_hint, Variant::Type t_type>
  1101. bool export_annotations(const AnnotationNode *p_annotation, Node *p_target);
  1102. bool warning_annotations(const AnnotationNode *p_annotation, Node *p_target);
  1103. template <MultiplayerAPI::RPCMode t_mode>
  1104. bool network_annotations(const AnnotationNode *p_annotation, Node *p_target);
  1105. // Statements.
  1106. Node *parse_statement();
  1107. VariableNode *parse_variable();
  1108. VariableNode *parse_variable(bool p_allow_property);
  1109. VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
  1110. void parse_property_getter(VariableNode *p_variable);
  1111. void parse_property_setter(VariableNode *p_variable);
  1112. ConstantNode *parse_constant();
  1113. AssertNode *parse_assert();
  1114. BreakNode *parse_break();
  1115. ContinueNode *parse_continue();
  1116. ForNode *parse_for();
  1117. IfNode *parse_if(const String &p_token = "if");
  1118. MatchNode *parse_match();
  1119. MatchBranchNode *parse_match_branch();
  1120. PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
  1121. WhileNode *parse_while();
  1122. // Expressions.
  1123. ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
  1124. ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
  1125. ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
  1126. LiteralNode *parse_literal();
  1127. ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
  1128. ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
  1129. IdentifierNode *parse_identifier();
  1130. ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
  1131. ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1132. ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1133. ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1134. ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
  1135. ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
  1136. ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
  1137. ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
  1138. ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
  1139. ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
  1140. ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
  1141. ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
  1142. ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
  1143. ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
  1144. ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
  1145. ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
  1146. TypeNode *parse_type(bool p_allow_void = false);
  1147. #ifdef TOOLS_ENABLED
  1148. // Doc comments.
  1149. int class_doc_line = 0x7FFFFFFF;
  1150. bool has_comment(int p_line);
  1151. String get_doc_comment(int p_line, bool p_single_line = false);
  1152. void get_class_doc_comment(int p_line, String &p_brief, String &p_desc, Vector<Pair<String, String>> &p_tutorials, bool p_inner_class);
  1153. #endif // TOOLS_ENABLED
  1154. public:
  1155. Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion);
  1156. ClassNode *get_tree() const { return head; }
  1157. bool is_tool() const { return _is_tool; }
  1158. static Variant::Type get_builtin_type(const StringName &p_type);
  1159. CompletionContext get_completion_context() const { return completion_context; }
  1160. CompletionCall get_completion_call() const { return completion_call; }
  1161. void get_annotation_list(List<MethodInfo> *r_annotations) const;
  1162. const List<ParserError> &get_errors() const { return errors; }
  1163. const List<String> get_dependencies() const {
  1164. // TODO: Keep track of deps.
  1165. return List<String>();
  1166. }
  1167. #ifdef DEBUG_ENABLED
  1168. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  1169. const Set<int> &get_unsafe_lines() const { return unsafe_lines; }
  1170. int get_last_line_number() const { return current.end_line; }
  1171. #endif
  1172. GDScriptParser();
  1173. ~GDScriptParser();
  1174. #ifdef DEBUG_ENABLED
  1175. class TreePrinter {
  1176. int indent_level = 0;
  1177. String indent;
  1178. StringBuilder printed;
  1179. bool pending_indent = false;
  1180. void increase_indent();
  1181. void decrease_indent();
  1182. void push_line(const String &p_line = String());
  1183. void push_text(const String &p_text);
  1184. void print_annotation(AnnotationNode *p_annotation);
  1185. void print_array(ArrayNode *p_array);
  1186. void print_assert(AssertNode *p_assert);
  1187. void print_assignment(AssignmentNode *p_assignment);
  1188. void print_await(AwaitNode *p_await);
  1189. void print_binary_op(BinaryOpNode *p_binary_op);
  1190. void print_call(CallNode *p_call);
  1191. void print_cast(CastNode *p_cast);
  1192. void print_class(ClassNode *p_class);
  1193. void print_constant(ConstantNode *p_constant);
  1194. void print_dictionary(DictionaryNode *p_dictionary);
  1195. void print_expression(ExpressionNode *p_expression);
  1196. void print_enum(EnumNode *p_enum);
  1197. void print_for(ForNode *p_for);
  1198. void print_function(FunctionNode *p_function);
  1199. void print_get_node(GetNodeNode *p_get_node);
  1200. void print_if(IfNode *p_if, bool p_is_elif = false);
  1201. void print_identifier(IdentifierNode *p_identifier);
  1202. void print_literal(LiteralNode *p_literal);
  1203. void print_match(MatchNode *p_match);
  1204. void print_match_branch(MatchBranchNode *p_match_branch);
  1205. void print_match_pattern(PatternNode *p_match_pattern);
  1206. void print_parameter(ParameterNode *p_parameter);
  1207. void print_preload(PreloadNode *p_preload);
  1208. void print_return(ReturnNode *p_return);
  1209. void print_self(SelfNode *p_self);
  1210. void print_signal(SignalNode *p_signal);
  1211. void print_statement(Node *p_statement);
  1212. void print_subscript(SubscriptNode *p_subscript);
  1213. void print_suite(SuiteNode *p_suite);
  1214. void print_type(TypeNode *p_type);
  1215. void print_ternary_op(TernaryOpNode *p_ternary_op);
  1216. void print_unary_op(UnaryOpNode *p_unary_op);
  1217. void print_variable(VariableNode *p_variable);
  1218. void print_while(WhileNode *p_while);
  1219. public:
  1220. void print_tree(const GDScriptParser &p_parser);
  1221. };
  1222. #endif // DEBUG_ENABLED
  1223. static void cleanup();
  1224. };
  1225. #endif // GDSCRIPT_PARSER_H