gdscript_parser.h 12 KB

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