gdscript_parser.h 14 KB

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