gd_parser.h 11 KB

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