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. has_type(false),
  89. is_constant(false),
  90. is_meta_type(false),
  91. infer_type(false),
  92. may_yield(false),
  93. builtin_type(Variant::NIL),
  94. class_type(NULL) {}
  95. };
  96. struct Node {
  97. enum Type {
  98. TYPE_CLASS,
  99. TYPE_FUNCTION,
  100. TYPE_BUILT_IN_FUNCTION,
  101. TYPE_BLOCK,
  102. TYPE_IDENTIFIER,
  103. TYPE_TYPE,
  104. TYPE_CONSTANT,
  105. TYPE_ARRAY,
  106. TYPE_DICTIONARY,
  107. TYPE_SELF,
  108. TYPE_OPERATOR,
  109. TYPE_CONTROL_FLOW,
  110. TYPE_LOCAL_VAR,
  111. TYPE_CAST,
  112. TYPE_ASSERT,
  113. TYPE_BREAKPOINT,
  114. TYPE_NEWLINE,
  115. };
  116. Node *next;
  117. int line;
  118. int column;
  119. Type type;
  120. virtual DataType get_datatype() const { return DataType(); }
  121. virtual void set_datatype(const DataType &p_datatype) {}
  122. virtual ~Node() {}
  123. };
  124. struct FunctionNode;
  125. struct BlockNode;
  126. struct ConstantNode;
  127. struct LocalVarNode;
  128. struct OperatorNode;
  129. struct ClassNode : public Node {
  130. bool tool;
  131. StringName name;
  132. bool extends_used;
  133. StringName extends_file;
  134. Vector<StringName> extends_class;
  135. DataType base_type;
  136. String icon_path;
  137. struct Member {
  138. PropertyInfo _export;
  139. #ifdef TOOLS_ENABLED
  140. Variant default_value;
  141. #endif
  142. StringName identifier;
  143. DataType data_type;
  144. StringName setter;
  145. StringName getter;
  146. int line;
  147. Node *expression;
  148. OperatorNode *initial_assignment;
  149. MultiplayerAPI::RPCMode rpc_mode;
  150. int usages;
  151. };
  152. struct Constant {
  153. Node *expression;
  154. DataType type;
  155. };
  156. struct Signal {
  157. StringName name;
  158. Vector<StringName> arguments;
  159. int emissions;
  160. int line;
  161. };
  162. Vector<ClassNode *> subclasses;
  163. Vector<Member> variables;
  164. Map<StringName, Constant> constant_expressions;
  165. Vector<FunctionNode *> functions;
  166. Vector<FunctionNode *> static_functions;
  167. Vector<Signal> _signals;
  168. BlockNode *initializer;
  169. BlockNode *ready;
  170. ClassNode *owner;
  171. //Vector<Node*> initializers;
  172. int end_line;
  173. ClassNode() {
  174. tool = false;
  175. type = TYPE_CLASS;
  176. extends_used = false;
  177. end_line = -1;
  178. owner = NULL;
  179. }
  180. };
  181. struct FunctionNode : public Node {
  182. bool _static;
  183. MultiplayerAPI::RPCMode rpc_mode;
  184. bool has_yield;
  185. bool has_unreachable_code;
  186. StringName name;
  187. DataType return_type;
  188. Vector<StringName> arguments;
  189. Vector<DataType> argument_types;
  190. Vector<Node *> default_values;
  191. BlockNode *body;
  192. #ifdef DEBUG_ENABLED
  193. Vector<int> arguments_usage;
  194. #endif // DEBUG_ENABLED
  195. virtual DataType get_datatype() const { return return_type; }
  196. virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; }
  197. FunctionNode() {
  198. type = TYPE_FUNCTION;
  199. _static = false;
  200. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  201. has_yield = false;
  202. has_unreachable_code = false;
  203. }
  204. };
  205. struct BlockNode : public Node {
  206. ClassNode *parent_class;
  207. BlockNode *parent_block;
  208. List<Node *> statements;
  209. Map<StringName, LocalVarNode *> variables;
  210. bool has_return;
  211. Node *if_condition; //tiny hack to improve code completion on if () blocks
  212. //the following is useful for code completion
  213. List<BlockNode *> sub_blocks;
  214. int end_line;
  215. BlockNode() {
  216. if_condition = NULL;
  217. type = TYPE_BLOCK;
  218. end_line = -1;
  219. parent_block = NULL;
  220. parent_class = NULL;
  221. has_return = false;
  222. }
  223. };
  224. struct TypeNode : public Node {
  225. Variant::Type vtype;
  226. TypeNode() { type = TYPE_TYPE; }
  227. };
  228. struct BuiltInFunctionNode : public Node {
  229. GDScriptFunctions::Function function;
  230. BuiltInFunctionNode() { type = TYPE_BUILT_IN_FUNCTION; }
  231. };
  232. struct IdentifierNode : public Node {
  233. StringName name;
  234. BlockNode *declared_block; // Simplify lookup by checking if it is declared locally
  235. DataType datatype;
  236. virtual DataType get_datatype() const { return datatype; }
  237. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  238. IdentifierNode() {
  239. type = TYPE_IDENTIFIER;
  240. declared_block = NULL;
  241. }
  242. };
  243. struct LocalVarNode : public Node {
  244. StringName name;
  245. Node *assign;
  246. OperatorNode *assign_op;
  247. int assignments;
  248. int usages;
  249. DataType datatype;
  250. virtual DataType get_datatype() const { return datatype; }
  251. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  252. LocalVarNode() {
  253. type = TYPE_LOCAL_VAR;
  254. assign = NULL;
  255. assign_op = NULL;
  256. assignments = 0;
  257. usages = 0;
  258. }
  259. };
  260. struct ConstantNode : public Node {
  261. Variant value;
  262. DataType datatype;
  263. virtual DataType get_datatype() const { return datatype; }
  264. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  265. ConstantNode() { type = TYPE_CONSTANT; }
  266. };
  267. struct ArrayNode : public Node {
  268. Vector<Node *> elements;
  269. DataType datatype;
  270. virtual DataType get_datatype() const { return datatype; }
  271. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  272. ArrayNode() {
  273. type = TYPE_ARRAY;
  274. datatype.has_type = true;
  275. datatype.kind = DataType::BUILTIN;
  276. datatype.builtin_type = Variant::ARRAY;
  277. }
  278. };
  279. struct DictionaryNode : public Node {
  280. struct Pair {
  281. Node *key;
  282. Node *value;
  283. };
  284. Vector<Pair> elements;
  285. DataType datatype;
  286. virtual DataType get_datatype() const { return datatype; }
  287. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  288. DictionaryNode() {
  289. type = TYPE_DICTIONARY;
  290. datatype.has_type = true;
  291. datatype.kind = DataType::BUILTIN;
  292. datatype.builtin_type = Variant::DICTIONARY;
  293. }
  294. };
  295. struct SelfNode : public Node {
  296. SelfNode() { type = TYPE_SELF; }
  297. };
  298. struct OperatorNode : public Node {
  299. enum Operator {
  300. //call/constructor operator
  301. OP_CALL,
  302. OP_PARENT_CALL,
  303. OP_YIELD,
  304. OP_IS,
  305. OP_IS_BUILTIN,
  306. //indexing operator
  307. OP_INDEX,
  308. OP_INDEX_NAMED,
  309. //unary operators
  310. OP_NEG,
  311. OP_POS,
  312. OP_NOT,
  313. OP_BIT_INVERT,
  314. //binary operators (in precedence order)
  315. OP_IN,
  316. OP_EQUAL,
  317. OP_NOT_EQUAL,
  318. OP_LESS,
  319. OP_LESS_EQUAL,
  320. OP_GREATER,
  321. OP_GREATER_EQUAL,
  322. OP_AND,
  323. OP_OR,
  324. OP_ADD,
  325. OP_SUB,
  326. OP_MUL,
  327. OP_DIV,
  328. OP_MOD,
  329. OP_SHIFT_LEFT,
  330. OP_SHIFT_RIGHT,
  331. OP_INIT_ASSIGN,
  332. OP_ASSIGN,
  333. OP_ASSIGN_ADD,
  334. OP_ASSIGN_SUB,
  335. OP_ASSIGN_MUL,
  336. OP_ASSIGN_DIV,
  337. OP_ASSIGN_MOD,
  338. OP_ASSIGN_SHIFT_LEFT,
  339. OP_ASSIGN_SHIFT_RIGHT,
  340. OP_ASSIGN_BIT_AND,
  341. OP_ASSIGN_BIT_OR,
  342. OP_ASSIGN_BIT_XOR,
  343. OP_BIT_AND,
  344. OP_BIT_OR,
  345. OP_BIT_XOR,
  346. //ternary operators
  347. OP_TERNARY_IF,
  348. OP_TERNARY_ELSE,
  349. };
  350. Operator op;
  351. Vector<Node *> arguments;
  352. DataType datatype;
  353. virtual DataType get_datatype() const { return datatype; }
  354. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  355. OperatorNode() { type = TYPE_OPERATOR; }
  356. };
  357. struct PatternNode : public Node {
  358. enum PatternType {
  359. PT_CONSTANT,
  360. PT_BIND,
  361. PT_DICTIONARY,
  362. PT_ARRAY,
  363. PT_IGNORE_REST,
  364. PT_WILDCARD
  365. };
  366. PatternType pt_type;
  367. Node *constant;
  368. StringName bind;
  369. Map<ConstantNode *, PatternNode *> dictionary;
  370. Vector<PatternNode *> array;
  371. };
  372. struct PatternBranchNode : public Node {
  373. Vector<PatternNode *> patterns;
  374. BlockNode *body;
  375. };
  376. struct MatchNode : public Node {
  377. Node *val_to_match;
  378. Vector<PatternBranchNode *> branches;
  379. struct CompiledPatternBranch {
  380. Node *compiled_pattern;
  381. BlockNode *body;
  382. };
  383. Vector<CompiledPatternBranch> compiled_pattern_branches;
  384. };
  385. struct ControlFlowNode : public Node {
  386. enum CFType {
  387. CF_IF,
  388. CF_FOR,
  389. CF_WHILE,
  390. CF_SWITCH,
  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);
  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