gd_parser.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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-2014 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. };
  76. struct Constant {
  77. StringName identifier;
  78. Node *expression;
  79. };
  80. Vector<ClassNode*> subclasses;
  81. Vector<Member> variables;
  82. Vector<Constant> constant_expressions;
  83. Vector<FunctionNode*> functions;
  84. Vector<FunctionNode*> static_functions;
  85. BlockNode *initializer;
  86. //Vector<Node*> initializers;
  87. int end_line;
  88. ClassNode() { tool=false; type=TYPE_CLASS; extends_used=false; end_line=-1;}
  89. };
  90. struct FunctionNode : public Node {
  91. bool _static;
  92. StringName name;
  93. Vector<StringName> arguments;
  94. Vector<Node*> default_values;
  95. BlockNode *body;
  96. FunctionNode() { type=TYPE_FUNCTION; _static=false; }
  97. };
  98. struct BlockNode : public Node {
  99. Map<StringName,int> locals;
  100. List<Node*> statements;
  101. Vector<StringName> variables;
  102. Vector<int> variable_lines;
  103. //the following is useful for code completion
  104. List<BlockNode*> sub_blocks;
  105. int end_line;
  106. BlockNode() { type=TYPE_BLOCK; end_line=-1;}
  107. };
  108. struct TypeNode : public Node {
  109. Variant::Type vtype;
  110. TypeNode() { type=TYPE_TYPE; }
  111. };
  112. struct BuiltInFunctionNode : public Node {
  113. GDFunctions::Function function;
  114. BuiltInFunctionNode() { type=TYPE_BUILT_IN_FUNCTION; }
  115. };
  116. struct IdentifierNode : public Node {
  117. StringName name;
  118. IdentifierNode() { type=TYPE_IDENTIFIER; }
  119. };
  120. struct LocalVarNode : public Node {
  121. StringName name;
  122. Node *assign;
  123. LocalVarNode() { type=TYPE_LOCAL_VAR; assign=NULL;}
  124. };
  125. struct ConstantNode : public Node {
  126. Variant value;
  127. ConstantNode() { type=TYPE_CONSTANT; }
  128. };
  129. struct ArrayNode : public Node {
  130. Vector<Node*> elements;
  131. ArrayNode() { type=TYPE_ARRAY; }
  132. };
  133. struct DictionaryNode : public Node {
  134. struct Pair {
  135. Node *key;
  136. Node *value;
  137. };
  138. Vector<Pair> elements;
  139. DictionaryNode() { type=TYPE_DICTIONARY; }
  140. };
  141. struct SelfNode : public Node {
  142. SelfNode() { type=TYPE_SELF; }
  143. };
  144. struct OperatorNode : public Node {
  145. enum Operator {
  146. //call/constructor operator
  147. OP_CALL,
  148. OP_PARENT_CALL,
  149. OP_EXTENDS,
  150. //indexing operator
  151. OP_INDEX,
  152. OP_INDEX_NAMED,
  153. //unary operators
  154. OP_NEG,
  155. OP_NOT,
  156. OP_BIT_INVERT,
  157. OP_PREINC,
  158. OP_PREDEC,
  159. OP_INC,
  160. OP_DEC,
  161. //binary operators (in precedence order)
  162. OP_IN,
  163. OP_EQUAL,
  164. OP_NOT_EQUAL,
  165. OP_LESS,
  166. OP_LESS_EQUAL,
  167. OP_GREATER,
  168. OP_GREATER_EQUAL,
  169. OP_AND,
  170. OP_OR,
  171. OP_ADD,
  172. OP_SUB,
  173. OP_MUL,
  174. OP_DIV,
  175. OP_MOD,
  176. OP_SHIFT_LEFT,
  177. OP_SHIFT_RIGHT,
  178. OP_ASSIGN,
  179. OP_ASSIGN_ADD,
  180. OP_ASSIGN_SUB,
  181. OP_ASSIGN_MUL,
  182. OP_ASSIGN_DIV,
  183. OP_ASSIGN_MOD,
  184. OP_ASSIGN_SHIFT_LEFT,
  185. OP_ASSIGN_SHIFT_RIGHT,
  186. OP_ASSIGN_BIT_AND,
  187. OP_ASSIGN_BIT_OR,
  188. OP_ASSIGN_BIT_XOR,
  189. OP_BIT_AND,
  190. OP_BIT_OR,
  191. OP_BIT_XOR
  192. };
  193. Operator op;
  194. Vector<Node*> arguments;
  195. OperatorNode() { type=TYPE_OPERATOR; }
  196. };
  197. struct ControlFlowNode : public Node {
  198. enum CFType {
  199. CF_IF,
  200. CF_FOR,
  201. CF_WHILE,
  202. CF_SWITCH,
  203. CF_BREAK,
  204. CF_CONTINUE,
  205. CF_RETURN
  206. };
  207. CFType cf_type;
  208. Vector<Node*> arguments;
  209. BlockNode *body;
  210. BlockNode *body_else;
  211. ControlFlowNode *_else; //used for if
  212. ControlFlowNode() { type=TYPE_CONTROL_FLOW; cf_type=CF_IF; body=NULL; body_else=NULL;}
  213. };
  214. struct AssertNode : public Node {
  215. Node* condition;
  216. AssertNode() { type=TYPE_ASSERT; }
  217. };
  218. struct NewLineNode : public Node {
  219. int line;
  220. NewLineNode() { type=TYPE_NEWLINE; }
  221. };
  222. struct Expression {
  223. bool is_op;
  224. union {
  225. OperatorNode::Operator op;
  226. Node *node;
  227. };
  228. };
  229. /*
  230. struct OperatorNode : public Node {
  231. DataType return_cache;
  232. Operator op;
  233. Vector<Node*> arguments;
  234. virtual DataType get_datatype() const { return return_cache; }
  235. OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
  236. };
  237. struct VariableNode : public Node {
  238. DataType datatype_cache;
  239. StringName name;
  240. virtual DataType get_datatype() const { return datatype_cache; }
  241. VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; }
  242. };
  243. struct ConstantNode : public Node {
  244. DataType datatype;
  245. Variant value;
  246. virtual DataType get_datatype() const { return datatype; }
  247. ConstantNode() { type=TYPE_CONSTANT; }
  248. };
  249. struct BlockNode : public Node {
  250. Map<StringName,DataType> variables;
  251. List<Node*> statements;
  252. BlockNode() { type=TYPE_BLOCK; }
  253. };
  254. struct ControlFlowNode : public Node {
  255. FlowOperation flow_op;
  256. Vector<Node*> statements;
  257. ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
  258. };
  259. struct MemberNode : public Node {
  260. DataType datatype;
  261. StringName name;
  262. Node* owner;
  263. virtual DataType get_datatype() const { return datatype; }
  264. MemberNode() { type=TYPE_MEMBER; }
  265. };
  266. struct ProgramNode : public Node {
  267. struct Function {
  268. StringName name;
  269. FunctionNode*function;
  270. };
  271. Map<StringName,DataType> builtin_variables;
  272. Map<StringName,DataType> preexisting_variables;
  273. Vector<Function> functions;
  274. BlockNode *body;
  275. ProgramNode() { type=TYPE_PROGRAM; }
  276. };
  277. */
  278. private:
  279. GDTokenizer *tokenizer;
  280. Node *head;
  281. Node *list;
  282. template<class T>
  283. T* alloc_node();
  284. bool validating;
  285. int parenthesis;
  286. bool error_set;
  287. String error;
  288. int error_line;
  289. int error_column;
  290. int pending_newline;
  291. List<int> tab_level;
  292. String base_path;
  293. PropertyInfo current_export;
  294. void _set_error(const String& p_error, int p_line=-1, int p_column=-1);
  295. bool _parse_arguments(Node* p_parent,Vector<Node*>& p_args,bool p_static);
  296. bool _enter_indent_block(BlockNode *p_block=NULL);
  297. bool _parse_newline();
  298. Node* _parse_expression(Node *p_parent,bool p_static,bool p_allow_assign=false);
  299. Node* _reduce_expression(Node *p_node,bool p_to_const=false);
  300. Node* _parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const=false,bool p_allow_assign=false);
  301. void _parse_block(BlockNode *p_block,bool p_static);
  302. void _parse_extends(ClassNode *p_class);
  303. void _parse_class(ClassNode *p_class);
  304. bool _end_statement();
  305. Error _parse(const String& p_base_path);
  306. public:
  307. String get_error() const;
  308. int get_error_line() const;
  309. int get_error_column() const;
  310. Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false);
  311. Error parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path="");
  312. const Node *get_parse_tree() const;
  313. void clear();
  314. GDParser();
  315. ~GDParser();
  316. };
  317. #endif // PARSER_H