gdscript_parser.h 31 KB

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