gd_parser.h 11 KB

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