gdscript_parser.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. int get_required_argument_count() { return arguments.size() - default_values.size(); }
  199. FunctionNode() {
  200. type = TYPE_FUNCTION;
  201. _static = false;
  202. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  203. has_yield = false;
  204. has_unreachable_code = false;
  205. }
  206. };
  207. struct BlockNode : public Node {
  208. ClassNode *parent_class;
  209. BlockNode *parent_block;
  210. List<Node *> statements;
  211. Map<StringName, LocalVarNode *> variables;
  212. bool has_return;
  213. Node *if_condition; //tiny hack to improve code completion on if () blocks
  214. //the following is useful for code completion
  215. List<BlockNode *> sub_blocks;
  216. int end_line;
  217. BlockNode() {
  218. if_condition = NULL;
  219. type = TYPE_BLOCK;
  220. end_line = -1;
  221. parent_block = NULL;
  222. parent_class = NULL;
  223. has_return = false;
  224. }
  225. };
  226. struct TypeNode : public Node {
  227. Variant::Type vtype;
  228. TypeNode() { type = TYPE_TYPE; }
  229. };
  230. struct BuiltInFunctionNode : public Node {
  231. GDScriptFunctions::Function function;
  232. BuiltInFunctionNode() { type = TYPE_BUILT_IN_FUNCTION; }
  233. };
  234. struct IdentifierNode : public Node {
  235. StringName name;
  236. BlockNode *declared_block; // Simplify lookup by checking if it is declared locally
  237. DataType datatype;
  238. virtual DataType get_datatype() const { return datatype; }
  239. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  240. IdentifierNode() {
  241. type = TYPE_IDENTIFIER;
  242. declared_block = NULL;
  243. }
  244. };
  245. struct LocalVarNode : public Node {
  246. StringName name;
  247. Node *assign;
  248. OperatorNode *assign_op;
  249. int assignments;
  250. int usages;
  251. DataType datatype;
  252. virtual DataType get_datatype() const { return datatype; }
  253. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  254. LocalVarNode() {
  255. type = TYPE_LOCAL_VAR;
  256. assign = NULL;
  257. assign_op = NULL;
  258. assignments = 0;
  259. usages = 0;
  260. }
  261. };
  262. struct ConstantNode : public Node {
  263. Variant value;
  264. DataType datatype;
  265. virtual DataType get_datatype() const { return datatype; }
  266. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  267. ConstantNode() { type = TYPE_CONSTANT; }
  268. };
  269. struct ArrayNode : public Node {
  270. Vector<Node *> elements;
  271. DataType datatype;
  272. virtual DataType get_datatype() const { return datatype; }
  273. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  274. ArrayNode() {
  275. type = TYPE_ARRAY;
  276. datatype.has_type = true;
  277. datatype.kind = DataType::BUILTIN;
  278. datatype.builtin_type = Variant::ARRAY;
  279. }
  280. };
  281. struct DictionaryNode : public Node {
  282. struct Pair {
  283. Node *key;
  284. Node *value;
  285. };
  286. Vector<Pair> elements;
  287. DataType datatype;
  288. virtual DataType get_datatype() const { return datatype; }
  289. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  290. DictionaryNode() {
  291. type = TYPE_DICTIONARY;
  292. datatype.has_type = true;
  293. datatype.kind = DataType::BUILTIN;
  294. datatype.builtin_type = Variant::DICTIONARY;
  295. }
  296. };
  297. struct SelfNode : public Node {
  298. SelfNode() { type = TYPE_SELF; }
  299. };
  300. struct OperatorNode : public Node {
  301. enum Operator {
  302. //call/constructor operator
  303. OP_CALL,
  304. OP_PARENT_CALL,
  305. OP_YIELD,
  306. OP_IS,
  307. OP_IS_BUILTIN,
  308. //indexing operator
  309. OP_INDEX,
  310. OP_INDEX_NAMED,
  311. //unary operators
  312. OP_NEG,
  313. OP_POS,
  314. OP_NOT,
  315. OP_BIT_INVERT,
  316. //binary operators (in precedence order)
  317. OP_IN,
  318. OP_EQUAL,
  319. OP_NOT_EQUAL,
  320. OP_LESS,
  321. OP_LESS_EQUAL,
  322. OP_GREATER,
  323. OP_GREATER_EQUAL,
  324. OP_AND,
  325. OP_OR,
  326. OP_ADD,
  327. OP_SUB,
  328. OP_MUL,
  329. OP_DIV,
  330. OP_MOD,
  331. OP_SHIFT_LEFT,
  332. OP_SHIFT_RIGHT,
  333. OP_INIT_ASSIGN,
  334. OP_ASSIGN,
  335. OP_ASSIGN_ADD,
  336. OP_ASSIGN_SUB,
  337. OP_ASSIGN_MUL,
  338. OP_ASSIGN_DIV,
  339. OP_ASSIGN_MOD,
  340. OP_ASSIGN_SHIFT_LEFT,
  341. OP_ASSIGN_SHIFT_RIGHT,
  342. OP_ASSIGN_BIT_AND,
  343. OP_ASSIGN_BIT_OR,
  344. OP_ASSIGN_BIT_XOR,
  345. OP_BIT_AND,
  346. OP_BIT_OR,
  347. OP_BIT_XOR,
  348. //ternary operators
  349. OP_TERNARY_IF,
  350. OP_TERNARY_ELSE,
  351. };
  352. Operator op;
  353. Vector<Node *> arguments;
  354. DataType datatype;
  355. virtual DataType get_datatype() const { return datatype; }
  356. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  357. OperatorNode() { type = TYPE_OPERATOR; }
  358. };
  359. struct PatternNode : public Node {
  360. enum PatternType {
  361. PT_CONSTANT,
  362. PT_BIND,
  363. PT_DICTIONARY,
  364. PT_ARRAY,
  365. PT_IGNORE_REST,
  366. PT_WILDCARD
  367. };
  368. PatternType pt_type;
  369. Node *constant;
  370. StringName bind;
  371. Map<ConstantNode *, PatternNode *> dictionary;
  372. Vector<PatternNode *> array;
  373. };
  374. struct PatternBranchNode : public Node {
  375. Vector<PatternNode *> patterns;
  376. BlockNode *body;
  377. };
  378. struct MatchNode : public Node {
  379. Node *val_to_match;
  380. Vector<PatternBranchNode *> branches;
  381. struct CompiledPatternBranch {
  382. Node *compiled_pattern;
  383. BlockNode *body;
  384. };
  385. Vector<CompiledPatternBranch> compiled_pattern_branches;
  386. };
  387. struct ControlFlowNode : public Node {
  388. enum CFType {
  389. CF_IF,
  390. CF_FOR,
  391. CF_WHILE,
  392. CF_BREAK,
  393. CF_CONTINUE,
  394. CF_RETURN,
  395. CF_MATCH
  396. };
  397. CFType cf_type;
  398. Vector<Node *> arguments;
  399. BlockNode *body;
  400. BlockNode *body_else;
  401. MatchNode *match;
  402. ControlFlowNode *_else; //used for if
  403. ControlFlowNode() {
  404. type = TYPE_CONTROL_FLOW;
  405. cf_type = CF_IF;
  406. body = NULL;
  407. body_else = NULL;
  408. }
  409. };
  410. struct CastNode : public Node {
  411. Node *source_node;
  412. DataType cast_type;
  413. DataType return_type;
  414. virtual DataType get_datatype() const { return return_type; }
  415. virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; }
  416. CastNode() { type = TYPE_CAST; }
  417. };
  418. struct AssertNode : public Node {
  419. Node *condition;
  420. AssertNode() { type = TYPE_ASSERT; }
  421. };
  422. struct BreakpointNode : public Node {
  423. BreakpointNode() { type = TYPE_BREAKPOINT; }
  424. };
  425. struct NewLineNode : public Node {
  426. NewLineNode() { type = TYPE_NEWLINE; }
  427. };
  428. struct Expression {
  429. bool is_op;
  430. union {
  431. OperatorNode::Operator op;
  432. Node *node;
  433. };
  434. };
  435. enum CompletionType {
  436. COMPLETION_NONE,
  437. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  438. COMPLETION_GET_NODE,
  439. COMPLETION_FUNCTION,
  440. COMPLETION_IDENTIFIER,
  441. COMPLETION_PARENT_FUNCTION,
  442. COMPLETION_METHOD,
  443. COMPLETION_CALL_ARGUMENTS,
  444. COMPLETION_RESOURCE_PATH,
  445. COMPLETION_INDEX,
  446. COMPLETION_VIRTUAL_FUNC,
  447. COMPLETION_YIELD,
  448. COMPLETION_ASSIGN,
  449. COMPLETION_TYPE_HINT,
  450. COMPLETION_TYPE_HINT_INDEX,
  451. };
  452. private:
  453. GDScriptTokenizer *tokenizer;
  454. Node *head;
  455. Node *list;
  456. template <class T>
  457. T *alloc_node();
  458. bool validating;
  459. bool for_completion;
  460. int parenthesis;
  461. bool error_set;
  462. String error;
  463. int error_line;
  464. int error_column;
  465. bool check_types;
  466. bool dependencies_only;
  467. List<String> dependencies;
  468. #ifdef DEBUG_ENABLED
  469. Set<int> *safe_lines;
  470. #endif // DEBUG_ENABLED
  471. #ifdef DEBUG_ENABLED
  472. List<GDScriptWarning> warnings;
  473. #endif // DEBUG_ENABLED
  474. int pending_newline;
  475. List<int> tab_level;
  476. String base_path;
  477. String self_path;
  478. ClassNode *current_class;
  479. FunctionNode *current_function;
  480. BlockNode *current_block;
  481. bool _get_completable_identifier(CompletionType p_type, StringName &identifier);
  482. void _make_completable_call(int p_arg);
  483. CompletionType completion_type;
  484. StringName completion_cursor;
  485. Variant::Type completion_built_in_constant;
  486. Node *completion_node;
  487. ClassNode *completion_class;
  488. FunctionNode *completion_function;
  489. BlockNode *completion_block;
  490. int completion_line;
  491. int completion_argument;
  492. bool completion_found;
  493. bool completion_ident_is_call;
  494. PropertyInfo current_export;
  495. MultiplayerAPI::RPCMode rpc_mode;
  496. void _set_error(const String &p_error, int p_line = -1, int p_column = -1);
  497. #ifdef DEBUG_ENABLED
  498. 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());
  499. void _add_warning(int p_code, int p_line, const Vector<String> &p_symbols);
  500. #endif // DEBUG_ENABLED
  501. bool _recover_from_completion();
  502. bool _parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete = false);
  503. bool _enter_indent_block(BlockNode *p_block = NULL);
  504. bool _parse_newline();
  505. Node *_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign = false, bool p_parsing_constant = false);
  506. Node *_reduce_expression(Node *p_node, bool p_to_const = false);
  507. Node *_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const = false, bool p_allow_assign = false);
  508. PatternNode *_parse_pattern(bool p_static);
  509. void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static);
  510. void _transform_match_statment(MatchNode *p_match_statement);
  511. void _generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings);
  512. void _parse_block(BlockNode *p_block, bool p_static);
  513. void _parse_extends(ClassNode *p_class);
  514. void _parse_class(ClassNode *p_class);
  515. bool _end_statement();
  516. void _determine_inheritance(ClassNode *p_class);
  517. bool _parse_type(DataType &r_type, bool p_can_be_void = false);
  518. DataType _resolve_type(const DataType &p_source, int p_line);
  519. DataType _type_from_variant(const Variant &p_value) const;
  520. DataType _type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant = true) const;
  521. DataType _type_from_gdtype(const GDScriptDataType &p_gdtype) const;
  522. DataType _get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const;
  523. Variant::Operator _get_variant_operation(const OperatorNode::Operator &p_op) const;
  524. 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;
  525. bool _get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const;
  526. bool _is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion = false) const;
  527. DataType _reduce_node_type(Node *p_node);
  528. DataType _reduce_function_call_type(const OperatorNode *p_call);
  529. DataType _reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line, bool p_is_indexing);
  530. void _check_class_level_types(ClassNode *p_class);
  531. void _check_class_blocks_types(ClassNode *p_class);
  532. void _check_function_types(FunctionNode *p_function);
  533. void _check_block_types(BlockNode *p_block);
  534. _FORCE_INLINE_ void _mark_line_as_safe(int p_line) const {
  535. #ifdef DEBUG_ENABLED
  536. if (safe_lines) safe_lines->insert(p_line);
  537. #endif // DEBUG_ENABLED
  538. }
  539. _FORCE_INLINE_ void _mark_line_as_unsafe(int p_line) const {
  540. #ifdef DEBUG_ENABLED
  541. if (safe_lines) safe_lines->erase(p_line);
  542. #endif // DEBUG_ENABLED
  543. }
  544. Error _parse(const String &p_base_path);
  545. public:
  546. String get_error() const;
  547. int get_error_line() const;
  548. int get_error_column() const;
  549. #ifdef DEBUG_ENABLED
  550. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  551. #endif // DEBUG_ENABLED
  552. 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, bool p_dependencies_only = false);
  553. Error parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path = "", const String &p_self_path = "");
  554. bool is_tool_script() const;
  555. const Node *get_parse_tree() const;
  556. //completion info
  557. CompletionType get_completion_type();
  558. StringName get_completion_cursor();
  559. int get_completion_line();
  560. Variant::Type get_completion_built_in_constant();
  561. Node *get_completion_node();
  562. ClassNode *get_completion_class();
  563. BlockNode *get_completion_block();
  564. FunctionNode *get_completion_function();
  565. int get_completion_argument_index();
  566. int get_completion_identifier_is_function();
  567. const List<String> &get_dependencies() const { return dependencies; }
  568. void clear();
  569. GDScriptParser();
  570. ~GDScriptParser();
  571. };
  572. #endif // GDSCRIPT_PARSER_H