gdscript_parser.h 32 KB

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