gdscript_parser.h 17 KB

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