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