gdscript_parser.h 29 KB

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