2
0

gdscript_parser.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. //indexing operator
  304. OP_INDEX,
  305. OP_INDEX_NAMED,
  306. //unary operators
  307. OP_NEG,
  308. OP_POS,
  309. OP_NOT,
  310. OP_BIT_INVERT,
  311. //binary operators (in precedence order)
  312. OP_IN,
  313. OP_EQUAL,
  314. OP_NOT_EQUAL,
  315. OP_LESS,
  316. OP_LESS_EQUAL,
  317. OP_GREATER,
  318. OP_GREATER_EQUAL,
  319. OP_AND,
  320. OP_OR,
  321. OP_ADD,
  322. OP_SUB,
  323. OP_MUL,
  324. OP_DIV,
  325. OP_MOD,
  326. OP_SHIFT_LEFT,
  327. OP_SHIFT_RIGHT,
  328. OP_INIT_ASSIGN,
  329. OP_ASSIGN,
  330. OP_ASSIGN_ADD,
  331. OP_ASSIGN_SUB,
  332. OP_ASSIGN_MUL,
  333. OP_ASSIGN_DIV,
  334. OP_ASSIGN_MOD,
  335. OP_ASSIGN_SHIFT_LEFT,
  336. OP_ASSIGN_SHIFT_RIGHT,
  337. OP_ASSIGN_BIT_AND,
  338. OP_ASSIGN_BIT_OR,
  339. OP_ASSIGN_BIT_XOR,
  340. OP_BIT_AND,
  341. OP_BIT_OR,
  342. OP_BIT_XOR,
  343. //ternary operators
  344. OP_TERNARY_IF,
  345. OP_TERNARY_ELSE,
  346. };
  347. Operator op;
  348. Vector<Node *> arguments;
  349. DataType datatype;
  350. virtual DataType get_datatype() const { return datatype; }
  351. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  352. OperatorNode() { type = TYPE_OPERATOR; }
  353. };
  354. struct PatternNode : public Node {
  355. enum PatternType {
  356. PT_CONSTANT,
  357. PT_BIND,
  358. PT_DICTIONARY,
  359. PT_ARRAY,
  360. PT_IGNORE_REST,
  361. PT_WILDCARD
  362. };
  363. PatternType pt_type;
  364. Node *constant;
  365. StringName bind;
  366. Map<ConstantNode *, PatternNode *> dictionary;
  367. Vector<PatternNode *> array;
  368. };
  369. struct PatternBranchNode : public Node {
  370. Vector<PatternNode *> patterns;
  371. BlockNode *body;
  372. };
  373. struct MatchNode : public Node {
  374. Node *val_to_match;
  375. Vector<PatternBranchNode *> branches;
  376. struct CompiledPatternBranch {
  377. Node *compiled_pattern;
  378. BlockNode *body;
  379. };
  380. Vector<CompiledPatternBranch> compiled_pattern_branches;
  381. };
  382. struct ControlFlowNode : public Node {
  383. enum CFType {
  384. CF_IF,
  385. CF_FOR,
  386. CF_WHILE,
  387. CF_SWITCH,
  388. CF_BREAK,
  389. CF_CONTINUE,
  390. CF_RETURN,
  391. CF_MATCH
  392. };
  393. CFType cf_type;
  394. Vector<Node *> arguments;
  395. BlockNode *body;
  396. BlockNode *body_else;
  397. MatchNode *match;
  398. ControlFlowNode *_else; //used for if
  399. ControlFlowNode() {
  400. type = TYPE_CONTROL_FLOW;
  401. cf_type = CF_IF;
  402. body = NULL;
  403. body_else = NULL;
  404. }
  405. };
  406. struct CastNode : public Node {
  407. Node *source_node;
  408. DataType cast_type;
  409. DataType return_type;
  410. virtual DataType get_datatype() const { return return_type; }
  411. virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; }
  412. CastNode() { type = TYPE_CAST; }
  413. };
  414. struct AssertNode : public Node {
  415. Node *condition;
  416. AssertNode() { type = TYPE_ASSERT; }
  417. };
  418. struct BreakpointNode : public Node {
  419. BreakpointNode() { type = TYPE_BREAKPOINT; }
  420. };
  421. struct NewLineNode : public Node {
  422. NewLineNode() { type = TYPE_NEWLINE; }
  423. };
  424. struct Expression {
  425. bool is_op;
  426. union {
  427. OperatorNode::Operator op;
  428. Node *node;
  429. };
  430. };
  431. enum CompletionType {
  432. COMPLETION_NONE,
  433. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  434. COMPLETION_GET_NODE,
  435. COMPLETION_FUNCTION,
  436. COMPLETION_IDENTIFIER,
  437. COMPLETION_PARENT_FUNCTION,
  438. COMPLETION_METHOD,
  439. COMPLETION_CALL_ARGUMENTS,
  440. COMPLETION_RESOURCE_PATH,
  441. COMPLETION_INDEX,
  442. COMPLETION_VIRTUAL_FUNC,
  443. COMPLETION_YIELD,
  444. COMPLETION_ASSIGN,
  445. COMPLETION_TYPE_HINT,
  446. COMPLETION_TYPE_HINT_INDEX,
  447. };
  448. private:
  449. GDScriptTokenizer *tokenizer;
  450. Node *head;
  451. Node *list;
  452. template <class T>
  453. T *alloc_node();
  454. bool validating;
  455. bool for_completion;
  456. int parenthesis;
  457. bool error_set;
  458. String error;
  459. int error_line;
  460. int error_column;
  461. bool check_types;
  462. #ifdef DEBUG_ENABLED
  463. Set<int> *safe_lines;
  464. #endif // DEBUG_ENABLED
  465. #ifdef DEBUG_ENABLED
  466. List<GDScriptWarning> warnings;
  467. #endif // DEBUG_ENABLED
  468. int pending_newline;
  469. List<int> tab_level;
  470. String base_path;
  471. String self_path;
  472. ClassNode *current_class;
  473. FunctionNode *current_function;
  474. BlockNode *current_block;
  475. bool _get_completable_identifier(CompletionType p_type, StringName &identifier);
  476. void _make_completable_call(int p_arg);
  477. CompletionType completion_type;
  478. StringName completion_cursor;
  479. bool completion_static;
  480. Variant::Type completion_built_in_constant;
  481. Node *completion_node;
  482. ClassNode *completion_class;
  483. FunctionNode *completion_function;
  484. BlockNode *completion_block;
  485. int completion_line;
  486. int completion_argument;
  487. bool completion_found;
  488. bool completion_ident_is_call;
  489. PropertyInfo current_export;
  490. MultiplayerAPI::RPCMode rpc_mode;
  491. void _set_error(const String &p_error, int p_line = -1, int p_column = -1);
  492. #ifdef DEBUG_ENABLED
  493. 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());
  494. void _add_warning(int p_code, int p_line, const Vector<String> &p_symbols);
  495. #endif // DEBUG_ENABLED
  496. bool _recover_from_completion();
  497. bool _parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete = false);
  498. bool _enter_indent_block(BlockNode *p_block = NULL);
  499. bool _parse_newline();
  500. Node *_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign = false, bool p_parsing_constant = false);
  501. Node *_reduce_expression(Node *p_node, bool p_to_const = false);
  502. Node *_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const = false, bool p_allow_assign = false);
  503. PatternNode *_parse_pattern(bool p_static);
  504. void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static);
  505. void _transform_match_statment(MatchNode *p_match_statement);
  506. void _generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings);
  507. void _parse_block(BlockNode *p_block, bool p_static);
  508. void _parse_extends(ClassNode *p_class);
  509. void _parse_class(ClassNode *p_class);
  510. bool _end_statement();
  511. void _determine_inheritance(ClassNode *p_class);
  512. bool _parse_type(DataType &r_type, bool p_can_be_void = false);
  513. DataType _resolve_type(const DataType &p_source, int p_line);
  514. DataType _type_from_variant(const Variant &p_value) const;
  515. DataType _type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant = true) const;
  516. DataType _type_from_gdtype(const GDScriptDataType &p_gdtype) const;
  517. DataType _get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const;
  518. Variant::Operator _get_variant_operation(const OperatorNode::Operator &p_op) const;
  519. 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;
  520. bool _get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const;
  521. bool _is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion = false) const;
  522. DataType _reduce_node_type(Node *p_node);
  523. DataType _reduce_function_call_type(const OperatorNode *p_call);
  524. DataType _reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line);
  525. void _check_class_level_types(ClassNode *p_class);
  526. void _check_class_blocks_types(ClassNode *p_class);
  527. void _check_function_types(FunctionNode *p_function);
  528. void _check_block_types(BlockNode *p_block);
  529. _FORCE_INLINE_ void _mark_line_as_safe(int p_line) const {
  530. #ifdef DEBUG_ENABLED
  531. if (safe_lines) safe_lines->insert(p_line);
  532. #endif // DEBUG_ENABLED
  533. }
  534. _FORCE_INLINE_ void _mark_line_as_unsafe(int p_line) const {
  535. #ifdef DEBUG_ENABLED
  536. if (safe_lines) safe_lines->erase(p_line);
  537. #endif // DEBUG_ENABLED
  538. }
  539. Error _parse(const String &p_base_path);
  540. public:
  541. String get_error() const;
  542. int get_error_line() const;
  543. int get_error_column() const;
  544. #ifdef DEBUG_ENABLED
  545. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  546. #endif // DEBUG_ENABLED
  547. 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);
  548. Error parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path = "", const String &p_self_path = "");
  549. bool is_tool_script() const;
  550. const Node *get_parse_tree() const;
  551. //completion info
  552. CompletionType get_completion_type();
  553. StringName get_completion_cursor();
  554. int get_completion_line();
  555. Variant::Type get_completion_built_in_constant();
  556. Node *get_completion_node();
  557. ClassNode *get_completion_class();
  558. BlockNode *get_completion_block();
  559. FunctionNode *get_completion_function();
  560. int get_completion_argument_index();
  561. int get_completion_identifier_is_function();
  562. void clear();
  563. GDScriptParser();
  564. ~GDScriptParser();
  565. };
  566. #endif // GDSCRIPT_PARSER_H