gdscript_parser.h 29 KB

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