gdscript_parser.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*************************************************************************/
  2. /* gdscript_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/map.h"
  33. #include "core/object.h"
  34. #include "core/script_language.h"
  35. #include "gdscript_functions.h"
  36. #include "gdscript_tokenizer.h"
  37. struct GDScriptDataType;
  38. struct GDScriptWarning;
  39. class GDScriptParser {
  40. public:
  41. struct ClassNode;
  42. struct DataType {
  43. enum {
  44. BUILTIN,
  45. NATIVE,
  46. SCRIPT,
  47. GDSCRIPT,
  48. CLASS,
  49. UNRESOLVED
  50. } kind;
  51. bool has_type;
  52. bool is_constant;
  53. bool is_meta_type; // Whether the value can be used as a type
  54. bool infer_type;
  55. bool may_yield; // For function calls
  56. Variant::Type builtin_type;
  57. StringName native_type;
  58. Ref<Script> script_type;
  59. ClassNode *class_type;
  60. String to_string() const;
  61. bool operator==(const DataType &other) const {
  62. if (!has_type || !other.has_type) {
  63. return true; // Can be considered equal for parsing purpose
  64. }
  65. if (kind != other.kind) {
  66. return false;
  67. }
  68. switch (kind) {
  69. case BUILTIN: {
  70. return builtin_type == other.builtin_type;
  71. } break;
  72. case NATIVE: {
  73. return native_type == other.native_type;
  74. } break;
  75. case GDSCRIPT:
  76. case SCRIPT: {
  77. return script_type == other.script_type;
  78. } break;
  79. case CLASS: {
  80. return class_type == other.class_type;
  81. } break;
  82. case UNRESOLVED: {
  83. } break;
  84. }
  85. return false;
  86. }
  87. DataType() :
  88. kind(UNRESOLVED),
  89. has_type(false),
  90. is_constant(false),
  91. is_meta_type(false),
  92. infer_type(false),
  93. may_yield(false),
  94. builtin_type(Variant::NIL),
  95. class_type(NULL) {}
  96. };
  97. struct Node {
  98. enum Type {
  99. TYPE_CLASS,
  100. TYPE_FUNCTION,
  101. TYPE_BUILT_IN_FUNCTION,
  102. TYPE_BLOCK,
  103. TYPE_IDENTIFIER,
  104. TYPE_TYPE,
  105. TYPE_CONSTANT,
  106. TYPE_ARRAY,
  107. TYPE_DICTIONARY,
  108. TYPE_SELF,
  109. TYPE_OPERATOR,
  110. TYPE_CONTROL_FLOW,
  111. TYPE_LOCAL_VAR,
  112. TYPE_CAST,
  113. TYPE_ASSERT,
  114. TYPE_BREAKPOINT,
  115. TYPE_NEWLINE,
  116. };
  117. Node *next;
  118. int line;
  119. int column;
  120. Type type;
  121. virtual DataType get_datatype() const { return DataType(); }
  122. virtual void set_datatype(const DataType &p_datatype) {}
  123. virtual ~Node() {}
  124. };
  125. struct FunctionNode;
  126. struct BlockNode;
  127. struct ConstantNode;
  128. struct LocalVarNode;
  129. struct OperatorNode;
  130. struct ClassNode : public Node {
  131. bool tool;
  132. StringName name;
  133. bool extends_used;
  134. StringName extends_file;
  135. Vector<StringName> extends_class;
  136. DataType base_type;
  137. String icon_path;
  138. struct Member {
  139. PropertyInfo _export;
  140. #ifdef TOOLS_ENABLED
  141. Variant default_value;
  142. #endif
  143. StringName identifier;
  144. DataType data_type;
  145. StringName setter;
  146. StringName getter;
  147. int line;
  148. Node *expression;
  149. OperatorNode *initial_assignment;
  150. MultiplayerAPI::RPCMode rpc_mode;
  151. int usages;
  152. };
  153. struct Constant {
  154. Node *expression;
  155. DataType type;
  156. };
  157. struct Signal {
  158. StringName name;
  159. Vector<StringName> arguments;
  160. int emissions;
  161. int line;
  162. };
  163. Vector<ClassNode *> subclasses;
  164. Vector<Member> variables;
  165. Map<StringName, Constant> constant_expressions;
  166. Vector<FunctionNode *> functions;
  167. Vector<FunctionNode *> static_functions;
  168. Vector<Signal> _signals;
  169. BlockNode *initializer;
  170. BlockNode *ready;
  171. ClassNode *owner;
  172. //Vector<Node*> initializers;
  173. int end_line;
  174. ClassNode() {
  175. tool = false;
  176. type = TYPE_CLASS;
  177. extends_used = false;
  178. end_line = -1;
  179. owner = NULL;
  180. }
  181. };
  182. struct FunctionNode : public Node {
  183. bool _static;
  184. MultiplayerAPI::RPCMode rpc_mode;
  185. bool has_yield;
  186. bool has_unreachable_code;
  187. StringName name;
  188. DataType return_type;
  189. Vector<StringName> arguments;
  190. Vector<DataType> argument_types;
  191. Vector<Node *> default_values;
  192. BlockNode *body;
  193. #ifdef DEBUG_ENABLED
  194. Vector<int> arguments_usage;
  195. #endif // DEBUG_ENABLED
  196. virtual DataType get_datatype() const { return return_type; }
  197. virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; }
  198. FunctionNode() {
  199. type = TYPE_FUNCTION;
  200. _static = false;
  201. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  202. has_yield = false;
  203. has_unreachable_code = false;
  204. }
  205. };
  206. struct BlockNode : public Node {
  207. ClassNode *parent_class;
  208. BlockNode *parent_block;
  209. List<Node *> statements;
  210. Map<StringName, LocalVarNode *> variables;
  211. bool has_return;
  212. Node *if_condition; //tiny hack to improve code completion on if () blocks
  213. //the following is useful for code completion
  214. List<BlockNode *> sub_blocks;
  215. int end_line;
  216. BlockNode() {
  217. if_condition = NULL;
  218. type = TYPE_BLOCK;
  219. end_line = -1;
  220. parent_block = NULL;
  221. parent_class = NULL;
  222. has_return = false;
  223. }
  224. };
  225. struct TypeNode : public Node {
  226. Variant::Type vtype;
  227. TypeNode() { type = TYPE_TYPE; }
  228. };
  229. struct BuiltInFunctionNode : public Node {
  230. GDScriptFunctions::Function function;
  231. BuiltInFunctionNode() { type = TYPE_BUILT_IN_FUNCTION; }
  232. };
  233. struct IdentifierNode : public Node {
  234. StringName name;
  235. BlockNode *declared_block; // Simplify lookup by checking if it is declared locally
  236. DataType datatype;
  237. virtual DataType get_datatype() const { return datatype; }
  238. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  239. IdentifierNode() {
  240. type = TYPE_IDENTIFIER;
  241. declared_block = NULL;
  242. }
  243. };
  244. struct LocalVarNode : public Node {
  245. StringName name;
  246. Node *assign;
  247. OperatorNode *assign_op;
  248. int assignments;
  249. int usages;
  250. DataType datatype;
  251. virtual DataType get_datatype() const { return datatype; }
  252. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  253. LocalVarNode() {
  254. type = TYPE_LOCAL_VAR;
  255. assign = NULL;
  256. assign_op = NULL;
  257. assignments = 0;
  258. usages = 0;
  259. }
  260. };
  261. struct ConstantNode : public Node {
  262. Variant value;
  263. DataType datatype;
  264. virtual DataType get_datatype() const { return datatype; }
  265. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  266. ConstantNode() { type = TYPE_CONSTANT; }
  267. };
  268. struct ArrayNode : public Node {
  269. Vector<Node *> elements;
  270. DataType datatype;
  271. virtual DataType get_datatype() const { return datatype; }
  272. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  273. ArrayNode() {
  274. type = TYPE_ARRAY;
  275. datatype.has_type = true;
  276. datatype.kind = DataType::BUILTIN;
  277. datatype.builtin_type = Variant::ARRAY;
  278. }
  279. };
  280. struct DictionaryNode : public Node {
  281. struct Pair {
  282. Node *key;
  283. Node *value;
  284. };
  285. Vector<Pair> elements;
  286. DataType datatype;
  287. virtual DataType get_datatype() const { return datatype; }
  288. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  289. DictionaryNode() {
  290. type = TYPE_DICTIONARY;
  291. datatype.has_type = true;
  292. datatype.kind = DataType::BUILTIN;
  293. datatype.builtin_type = Variant::DICTIONARY;
  294. }
  295. };
  296. struct SelfNode : public Node {
  297. SelfNode() { type = TYPE_SELF; }
  298. };
  299. struct OperatorNode : public Node {
  300. enum Operator {
  301. //call/constructor operator
  302. OP_CALL,
  303. OP_PARENT_CALL,
  304. OP_YIELD,
  305. OP_IS,
  306. OP_IS_BUILTIN,
  307. //indexing operator
  308. OP_INDEX,
  309. OP_INDEX_NAMED,
  310. //unary operators
  311. OP_NEG,
  312. OP_POS,
  313. OP_NOT,
  314. OP_BIT_INVERT,
  315. //binary operators (in precedence order)
  316. OP_IN,
  317. OP_EQUAL,
  318. OP_NOT_EQUAL,
  319. OP_LESS,
  320. OP_LESS_EQUAL,
  321. OP_GREATER,
  322. OP_GREATER_EQUAL,
  323. OP_AND,
  324. OP_OR,
  325. OP_ADD,
  326. OP_SUB,
  327. OP_MUL,
  328. OP_DIV,
  329. OP_MOD,
  330. OP_SHIFT_LEFT,
  331. OP_SHIFT_RIGHT,
  332. OP_INIT_ASSIGN,
  333. OP_ASSIGN,
  334. OP_ASSIGN_ADD,
  335. OP_ASSIGN_SUB,
  336. OP_ASSIGN_MUL,
  337. OP_ASSIGN_DIV,
  338. OP_ASSIGN_MOD,
  339. OP_ASSIGN_SHIFT_LEFT,
  340. OP_ASSIGN_SHIFT_RIGHT,
  341. OP_ASSIGN_BIT_AND,
  342. OP_ASSIGN_BIT_OR,
  343. OP_ASSIGN_BIT_XOR,
  344. OP_BIT_AND,
  345. OP_BIT_OR,
  346. OP_BIT_XOR,
  347. //ternary operators
  348. OP_TERNARY_IF,
  349. OP_TERNARY_ELSE,
  350. };
  351. Operator op;
  352. Vector<Node *> arguments;
  353. DataType datatype;
  354. virtual DataType get_datatype() const { return datatype; }
  355. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  356. OperatorNode() { type = TYPE_OPERATOR; }
  357. };
  358. struct PatternNode : public Node {
  359. enum PatternType {
  360. PT_CONSTANT,
  361. PT_BIND,
  362. PT_DICTIONARY,
  363. PT_ARRAY,
  364. PT_IGNORE_REST,
  365. PT_WILDCARD
  366. };
  367. PatternType pt_type;
  368. Node *constant;
  369. StringName bind;
  370. Map<ConstantNode *, PatternNode *> dictionary;
  371. Vector<PatternNode *> array;
  372. };
  373. struct PatternBranchNode : public Node {
  374. Vector<PatternNode *> patterns;
  375. BlockNode *body;
  376. };
  377. struct MatchNode : public Node {
  378. Node *val_to_match;
  379. Vector<PatternBranchNode *> branches;
  380. struct CompiledPatternBranch {
  381. Node *compiled_pattern;
  382. BlockNode *body;
  383. };
  384. Vector<CompiledPatternBranch> compiled_pattern_branches;
  385. };
  386. struct ControlFlowNode : public Node {
  387. enum CFType {
  388. CF_IF,
  389. CF_FOR,
  390. CF_WHILE,
  391. CF_BREAK,
  392. CF_CONTINUE,
  393. CF_RETURN,
  394. CF_MATCH
  395. };
  396. CFType cf_type;
  397. Vector<Node *> arguments;
  398. BlockNode *body;
  399. BlockNode *body_else;
  400. MatchNode *match;
  401. ControlFlowNode *_else; //used for if
  402. ControlFlowNode() {
  403. type = TYPE_CONTROL_FLOW;
  404. cf_type = CF_IF;
  405. body = NULL;
  406. body_else = NULL;
  407. }
  408. };
  409. struct CastNode : public Node {
  410. Node *source_node;
  411. DataType cast_type;
  412. DataType return_type;
  413. virtual DataType get_datatype() const { return return_type; }
  414. virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; }
  415. CastNode() { type = TYPE_CAST; }
  416. };
  417. struct AssertNode : public Node {
  418. Node *condition;
  419. AssertNode() { type = TYPE_ASSERT; }
  420. };
  421. struct BreakpointNode : public Node {
  422. BreakpointNode() { type = TYPE_BREAKPOINT; }
  423. };
  424. struct NewLineNode : public Node {
  425. NewLineNode() { type = TYPE_NEWLINE; }
  426. };
  427. struct Expression {
  428. bool is_op;
  429. union {
  430. OperatorNode::Operator op;
  431. Node *node;
  432. };
  433. };
  434. enum CompletionType {
  435. COMPLETION_NONE,
  436. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  437. COMPLETION_GET_NODE,
  438. COMPLETION_FUNCTION,
  439. COMPLETION_IDENTIFIER,
  440. COMPLETION_PARENT_FUNCTION,
  441. COMPLETION_METHOD,
  442. COMPLETION_CALL_ARGUMENTS,
  443. COMPLETION_RESOURCE_PATH,
  444. COMPLETION_INDEX,
  445. COMPLETION_VIRTUAL_FUNC,
  446. COMPLETION_YIELD,
  447. COMPLETION_ASSIGN,
  448. COMPLETION_TYPE_HINT,
  449. COMPLETION_TYPE_HINT_INDEX,
  450. };
  451. private:
  452. GDScriptTokenizer *tokenizer;
  453. Node *head;
  454. Node *list;
  455. template <class T>
  456. T *alloc_node();
  457. bool validating;
  458. bool for_completion;
  459. int parenthesis;
  460. bool error_set;
  461. String error;
  462. int error_line;
  463. int error_column;
  464. bool check_types;
  465. #ifdef DEBUG_ENABLED
  466. Set<int> *safe_lines;
  467. #endif // DEBUG_ENABLED
  468. #ifdef DEBUG_ENABLED
  469. List<GDScriptWarning> warnings;
  470. #endif // DEBUG_ENABLED
  471. int pending_newline;
  472. List<int> tab_level;
  473. String base_path;
  474. String self_path;
  475. ClassNode *current_class;
  476. FunctionNode *current_function;
  477. BlockNode *current_block;
  478. bool _get_completable_identifier(CompletionType p_type, StringName &identifier);
  479. void _make_completable_call(int p_arg);
  480. CompletionType completion_type;
  481. StringName completion_cursor;
  482. Variant::Type completion_built_in_constant;
  483. Node *completion_node;
  484. ClassNode *completion_class;
  485. FunctionNode *completion_function;
  486. BlockNode *completion_block;
  487. int completion_line;
  488. int completion_argument;
  489. bool completion_found;
  490. bool completion_ident_is_call;
  491. PropertyInfo current_export;
  492. MultiplayerAPI::RPCMode rpc_mode;
  493. void _set_error(const String &p_error, int p_line = -1, int p_column = -1);
  494. #ifdef DEBUG_ENABLED
  495. void _add_warning(int p_code, int p_line = -1, const String &p_symbol1 = String(), const String &p_symbol2 = String(), const String &p_symbol3 = String(), const String &p_symbol4 = String());
  496. void _add_warning(int p_code, int p_line, const Vector<String> &p_symbols);
  497. #endif // DEBUG_ENABLED
  498. bool _recover_from_completion();
  499. bool _parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete = false);
  500. bool _enter_indent_block(BlockNode *p_block = NULL);
  501. bool _parse_newline();
  502. Node *_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign = false, bool p_parsing_constant = false);
  503. Node *_reduce_expression(Node *p_node, bool p_to_const = false);
  504. Node *_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const = false, bool p_allow_assign = false);
  505. PatternNode *_parse_pattern(bool p_static);
  506. void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static);
  507. void _transform_match_statment(MatchNode *p_match_statement);
  508. void _generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings);
  509. void _parse_block(BlockNode *p_block, bool p_static);
  510. void _parse_extends(ClassNode *p_class);
  511. void _parse_class(ClassNode *p_class);
  512. bool _end_statement();
  513. void _determine_inheritance(ClassNode *p_class);
  514. bool _parse_type(DataType &r_type, bool p_can_be_void = false);
  515. DataType _resolve_type(const DataType &p_source, int p_line);
  516. DataType _type_from_variant(const Variant &p_value) const;
  517. DataType _type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant = true) const;
  518. DataType _type_from_gdtype(const GDScriptDataType &p_gdtype) const;
  519. DataType _get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const;
  520. Variant::Operator _get_variant_operation(const OperatorNode::Operator &p_op) const;
  521. bool _get_function_signature(DataType &p_base_type, const StringName &p_function, DataType &r_return_type, List<DataType> &r_arg_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) const;
  522. bool _get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const;
  523. bool _is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion = false) const;
  524. DataType _reduce_node_type(Node *p_node);
  525. DataType _reduce_function_call_type(const OperatorNode *p_call);
  526. DataType _reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line, bool p_is_indexing);
  527. void _check_class_level_types(ClassNode *p_class);
  528. void _check_class_blocks_types(ClassNode *p_class);
  529. void _check_function_types(FunctionNode *p_function);
  530. void _check_block_types(BlockNode *p_block);
  531. _FORCE_INLINE_ void _mark_line_as_safe(int p_line) const {
  532. #ifdef DEBUG_ENABLED
  533. if (safe_lines) safe_lines->insert(p_line);
  534. #endif // DEBUG_ENABLED
  535. }
  536. _FORCE_INLINE_ void _mark_line_as_unsafe(int p_line) const {
  537. #ifdef DEBUG_ENABLED
  538. if (safe_lines) safe_lines->erase(p_line);
  539. #endif // DEBUG_ENABLED
  540. }
  541. Error _parse(const String &p_base_path);
  542. public:
  543. String get_error() const;
  544. int get_error_line() const;
  545. int get_error_column() const;
  546. #ifdef DEBUG_ENABLED
  547. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  548. #endif // DEBUG_ENABLED
  549. Error parse(const String &p_code, const String &p_base_path = "", bool p_just_validate = false, const String &p_self_path = "", bool p_for_completion = false, Set<int> *r_safe_lines = NULL);
  550. Error parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path = "", const String &p_self_path = "");
  551. bool is_tool_script() const;
  552. const Node *get_parse_tree() const;
  553. //completion info
  554. CompletionType get_completion_type();
  555. StringName get_completion_cursor();
  556. int get_completion_line();
  557. Variant::Type get_completion_built_in_constant();
  558. Node *get_completion_node();
  559. ClassNode *get_completion_class();
  560. BlockNode *get_completion_block();
  561. FunctionNode *get_completion_function();
  562. int get_completion_argument_index();
  563. int get_completion_identifier_is_function();
  564. void clear();
  565. GDScriptParser();
  566. ~GDScriptParser();
  567. };
  568. #endif // GDSCRIPT_PARSER_H