gdscript_parser.h 16 KB

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