gdscript_parser.h 38 KB

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