gd_parser.h 11 KB

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