gdscript_parser.h 13 KB

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