gd_parser.h 11 KB

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