gdscript_parser.h 31 KB

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