gdscript_parser.h 12 KB

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