gd_parser.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*************************************************************************/
  2. /* gd_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef GD_PARSER_H
  30. #define GD_PARSER_H
  31. #include "gd_tokenizer.h"
  32. #include "gd_functions.h"
  33. #include "map.h"
  34. #include "object.h"
  35. class GDParser {
  36. public:
  37. struct Node {
  38. enum Type {
  39. TYPE_CLASS,
  40. TYPE_FUNCTION,
  41. TYPE_BUILT_IN_FUNCTION,
  42. TYPE_BLOCK,
  43. TYPE_IDENTIFIER,
  44. TYPE_TYPE,
  45. TYPE_CONSTANT,
  46. TYPE_ARRAY,
  47. TYPE_DICTIONARY,
  48. TYPE_SELF,
  49. TYPE_OPERATOR,
  50. TYPE_CONTROL_FLOW,
  51. TYPE_LOCAL_VAR,
  52. TYPE_ASSERT,
  53. TYPE_NEWLINE,
  54. };
  55. Node * next;
  56. int line;
  57. int column;
  58. Type type;
  59. virtual ~Node() {}
  60. };
  61. struct FunctionNode;
  62. struct BlockNode;
  63. struct ClassNode : public Node {
  64. bool tool;
  65. StringName name;
  66. bool extends_used;
  67. StringName extends_file;
  68. Vector<StringName> extends_class;
  69. struct Member {
  70. PropertyInfo _export;
  71. #ifdef TOOLS_ENABLED
  72. Variant default_value;
  73. #endif
  74. StringName identifier;
  75. StringName setter;
  76. StringName getter;
  77. int line;
  78. Node *expression;
  79. };
  80. struct Constant {
  81. StringName identifier;
  82. Node *expression;
  83. };
  84. Vector<ClassNode*> subclasses;
  85. Vector<Member> variables;
  86. Vector<Constant> constant_expressions;
  87. Vector<FunctionNode*> functions;
  88. Vector<FunctionNode*> static_functions;
  89. BlockNode *initializer;
  90. ClassNode *owner;
  91. //Vector<Node*> initializers;
  92. int end_line;
  93. ClassNode() { tool=false; type=TYPE_CLASS; extends_used=false; end_line=-1; owner=NULL;}
  94. };
  95. struct FunctionNode : public Node {
  96. bool _static;
  97. StringName name;
  98. Vector<StringName> arguments;
  99. Vector<Node*> default_values;
  100. BlockNode *body;
  101. FunctionNode() { type=TYPE_FUNCTION; _static=false; }
  102. };
  103. struct BlockNode : public Node {
  104. ClassNode *parent_class;
  105. BlockNode *parent_block;
  106. Map<StringName,int> locals;
  107. List<Node*> statements;
  108. Vector<StringName> variables;
  109. Vector<int> variable_lines;
  110. //the following is useful for code completion
  111. List<BlockNode*> sub_blocks;
  112. int end_line;
  113. BlockNode() { type=TYPE_BLOCK; end_line=-1; parent_block=NULL; parent_class=NULL; }
  114. };
  115. struct TypeNode : public Node {
  116. Variant::Type vtype;
  117. TypeNode() { type=TYPE_TYPE; }
  118. };
  119. struct BuiltInFunctionNode : public Node {
  120. GDFunctions::Function function;
  121. BuiltInFunctionNode() { type=TYPE_BUILT_IN_FUNCTION; }
  122. };
  123. struct IdentifierNode : public Node {
  124. StringName name;
  125. IdentifierNode() { type=TYPE_IDENTIFIER; }
  126. };
  127. struct LocalVarNode : public Node {
  128. StringName name;
  129. Node *assign;
  130. LocalVarNode() { type=TYPE_LOCAL_VAR; assign=NULL;}
  131. };
  132. struct ConstantNode : public Node {
  133. Variant value;
  134. ConstantNode() { type=TYPE_CONSTANT; }
  135. };
  136. struct ArrayNode : public Node {
  137. Vector<Node*> elements;
  138. ArrayNode() { type=TYPE_ARRAY; }
  139. };
  140. struct DictionaryNode : public Node {
  141. struct Pair {
  142. Node *key;
  143. Node *value;
  144. };
  145. Vector<Pair> elements;
  146. DictionaryNode() { type=TYPE_DICTIONARY; }
  147. };
  148. struct SelfNode : public Node {
  149. SelfNode() { type=TYPE_SELF; }
  150. };
  151. struct OperatorNode : public Node {
  152. enum Operator {
  153. //call/constructor operator
  154. OP_CALL,
  155. OP_PARENT_CALL,
  156. OP_YIELD,
  157. OP_EXTENDS,
  158. //indexing operator
  159. OP_INDEX,
  160. OP_INDEX_NAMED,
  161. //unary operators
  162. OP_NEG,
  163. OP_NOT,
  164. OP_BIT_INVERT,
  165. OP_PREINC,
  166. OP_PREDEC,
  167. OP_INC,
  168. OP_DEC,
  169. //binary operators (in precedence order)
  170. OP_IN,
  171. OP_EQUAL,
  172. OP_NOT_EQUAL,
  173. OP_LESS,
  174. OP_LESS_EQUAL,
  175. OP_GREATER,
  176. OP_GREATER_EQUAL,
  177. OP_AND,
  178. OP_OR,
  179. OP_ADD,
  180. OP_SUB,
  181. OP_MUL,
  182. OP_DIV,
  183. OP_MOD,
  184. OP_SHIFT_LEFT,
  185. OP_SHIFT_RIGHT,
  186. OP_INIT_ASSIGN,
  187. OP_ASSIGN,
  188. OP_ASSIGN_ADD,
  189. OP_ASSIGN_SUB,
  190. OP_ASSIGN_MUL,
  191. OP_ASSIGN_DIV,
  192. OP_ASSIGN_MOD,
  193. OP_ASSIGN_SHIFT_LEFT,
  194. OP_ASSIGN_SHIFT_RIGHT,
  195. OP_ASSIGN_BIT_AND,
  196. OP_ASSIGN_BIT_OR,
  197. OP_ASSIGN_BIT_XOR,
  198. OP_BIT_AND,
  199. OP_BIT_OR,
  200. OP_BIT_XOR,
  201. };
  202. Operator op;
  203. Vector<Node*> arguments;
  204. OperatorNode() { type=TYPE_OPERATOR; }
  205. };
  206. struct ControlFlowNode : public Node {
  207. enum CFType {
  208. CF_IF,
  209. CF_FOR,
  210. CF_WHILE,
  211. CF_SWITCH,
  212. CF_BREAK,
  213. CF_CONTINUE,
  214. CF_RETURN
  215. };
  216. CFType cf_type;
  217. Vector<Node*> arguments;
  218. BlockNode *body;
  219. BlockNode *body_else;
  220. ControlFlowNode *_else; //used for if
  221. ControlFlowNode() { type=TYPE_CONTROL_FLOW; cf_type=CF_IF; body=NULL; body_else=NULL;}
  222. };
  223. struct AssertNode : public Node {
  224. Node* condition;
  225. AssertNode() { type=TYPE_ASSERT; }
  226. };
  227. struct NewLineNode : public Node {
  228. int line;
  229. NewLineNode() { type=TYPE_NEWLINE; }
  230. };
  231. struct Expression {
  232. bool is_op;
  233. union {
  234. OperatorNode::Operator op;
  235. Node *node;
  236. };
  237. };
  238. /*
  239. struct OperatorNode : public Node {
  240. DataType return_cache;
  241. Operator op;
  242. Vector<Node*> arguments;
  243. virtual DataType get_datatype() const { return return_cache; }
  244. OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
  245. };
  246. struct VariableNode : public Node {
  247. DataType datatype_cache;
  248. StringName name;
  249. virtual DataType get_datatype() const { return datatype_cache; }
  250. VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; }
  251. };
  252. struct ConstantNode : public Node {
  253. DataType datatype;
  254. Variant value;
  255. virtual DataType get_datatype() const { return datatype; }
  256. ConstantNode() { type=TYPE_CONSTANT; }
  257. };
  258. struct BlockNode : public Node {
  259. Map<StringName,DataType> variables;
  260. List<Node*> statements;
  261. BlockNode() { type=TYPE_BLOCK; }
  262. };
  263. struct ControlFlowNode : public Node {
  264. FlowOperation flow_op;
  265. Vector<Node*> statements;
  266. ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
  267. };
  268. struct MemberNode : public Node {
  269. DataType datatype;
  270. StringName name;
  271. Node* owner;
  272. virtual DataType get_datatype() const { return datatype; }
  273. MemberNode() { type=TYPE_MEMBER; }
  274. };
  275. struct ProgramNode : public Node {
  276. struct Function {
  277. StringName name;
  278. FunctionNode*function;
  279. };
  280. Map<StringName,DataType> builtin_variables;
  281. Map<StringName,DataType> preexisting_variables;
  282. Vector<Function> functions;
  283. BlockNode *body;
  284. ProgramNode() { type=TYPE_PROGRAM; }
  285. };
  286. */
  287. enum CompletionType {
  288. COMPLETION_NONE,
  289. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  290. COMPLETION_FUNCTION,
  291. COMPLETION_IDENTIFIER,
  292. COMPLETION_PARENT_FUNCTION,
  293. COMPLETION_METHOD,
  294. COMPLETION_CALL_ARGUMENTS,
  295. COMPLETION_INDEX,
  296. COMPLETION_VIRTUAL_FUNC
  297. };
  298. private:
  299. GDTokenizer *tokenizer;
  300. Node *head;
  301. Node *list;
  302. template<class T>
  303. T* alloc_node();
  304. bool validating;
  305. int parenthesis;
  306. bool error_set;
  307. String error;
  308. int error_line;
  309. int error_column;
  310. int pending_newline;
  311. List<int> tab_level;
  312. String base_path;
  313. String self_path;
  314. ClassNode *current_class;
  315. FunctionNode *current_function;
  316. BlockNode *current_block;
  317. bool _get_completable_identifier(CompletionType p_type,StringName& identifier);
  318. void _make_completable_call(int p_arg);
  319. CompletionType completion_type;
  320. StringName completion_cursor;
  321. bool completion_static;
  322. Variant::Type completion_built_in_constant;
  323. Node *completion_node;
  324. ClassNode *completion_class;
  325. FunctionNode *completion_function;
  326. BlockNode *completion_block;
  327. int completion_line;
  328. int completion_argument;
  329. PropertyInfo current_export;
  330. void _set_error(const String& p_error, int p_line=-1, int p_column=-1);
  331. bool _parse_arguments(Node* p_parent, Vector<Node*>& p_args, bool p_static, bool p_can_codecomplete=false);
  332. bool _enter_indent_block(BlockNode *p_block=NULL);
  333. bool _parse_newline();
  334. Node* _parse_expression(Node *p_parent,bool p_static,bool p_allow_assign=false);
  335. Node* _reduce_expression(Node *p_node,bool p_to_const=false);
  336. Node* _parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const=false,bool p_allow_assign=false);
  337. void _parse_block(BlockNode *p_block,bool p_static);
  338. void _parse_extends(ClassNode *p_class);
  339. void _parse_class(ClassNode *p_class);
  340. bool _end_statement();
  341. Error _parse(const String& p_base_path);
  342. public:
  343. String get_error() const;
  344. int get_error_line() const;
  345. int get_error_column() const;
  346. Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false,const String& p_self_path="");
  347. Error parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path="",const String& p_self_path="");
  348. const Node *get_parse_tree() const;
  349. //completion info
  350. CompletionType get_completion_type();
  351. StringName get_completion_cursor();
  352. int get_completion_line();
  353. Variant::Type get_completion_built_in_constant();
  354. Node *get_completion_node();
  355. ClassNode *get_completion_class();
  356. BlockNode *get_completion_block();
  357. FunctionNode *get_completion_function();
  358. int get_completion_argument_index();
  359. void clear();
  360. GDParser();
  361. ~GDParser();
  362. };
  363. #endif // PARSER_H