gd_parser.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 PatternNode : public Node {
  220. enum PatternType {
  221. PT_CONSTANT,
  222. PT_BIND,
  223. PT_DICITIONARY,
  224. PT_ARRAY,
  225. PT_IGNORE_REST
  226. };
  227. PatternType pt_type;
  228. ConstantNode *constant;
  229. StringName bind;
  230. Map<ConstantNode*, PatternNode*> dictionary;
  231. Vector<PatternNode*> array;
  232. };
  233. struct PatternBranchNode : public Node {
  234. PatternNode *pattern;
  235. BlockNode *body;
  236. };
  237. struct ControlFlowNode : public Node {
  238. enum CFType {
  239. CF_IF,
  240. CF_FOR,
  241. CF_WHILE,
  242. CF_SWITCH,
  243. CF_BREAK,
  244. CF_CONTINUE,
  245. CF_RETURN,
  246. CF_MATCH
  247. };
  248. CFType cf_type;
  249. Vector<Node*> arguments;
  250. BlockNode *body;
  251. BlockNode *body_else;
  252. Vector<PatternBranchNode*> branches;
  253. ControlFlowNode *_else; //used for if
  254. ControlFlowNode() { type=TYPE_CONTROL_FLOW; cf_type=CF_IF; body=NULL; body_else=NULL;}
  255. };
  256. struct AssertNode : public Node {
  257. Node* condition;
  258. AssertNode() { type=TYPE_ASSERT; }
  259. };
  260. struct BreakpointNode : public Node {
  261. BreakpointNode() { type=TYPE_BREAKPOINT; }
  262. };
  263. struct NewLineNode : public Node {
  264. NewLineNode() { type=TYPE_NEWLINE; }
  265. };
  266. struct Expression {
  267. bool is_op;
  268. union {
  269. OperatorNode::Operator op;
  270. Node *node;
  271. };
  272. };
  273. /*
  274. struct OperatorNode : public Node {
  275. DataType return_cache;
  276. Operator op;
  277. Vector<Node*> arguments;
  278. virtual DataType get_datatype() const { return return_cache; }
  279. OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
  280. };
  281. struct VariableNode : public Node {
  282. DataType datatype_cache;
  283. StringName name;
  284. virtual DataType get_datatype() const { return datatype_cache; }
  285. VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; }
  286. };
  287. struct ConstantNode : public Node {
  288. DataType datatype;
  289. Variant value;
  290. virtual DataType get_datatype() const { return datatype; }
  291. ConstantNode() { type=TYPE_CONSTANT; }
  292. };
  293. struct BlockNode : public Node {
  294. Map<StringName,DataType> variables;
  295. List<Node*> statements;
  296. BlockNode() { type=TYPE_BLOCK; }
  297. };
  298. struct ControlFlowNode : public Node {
  299. FlowOperation flow_op;
  300. Vector<Node*> statements;
  301. ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
  302. };
  303. struct MemberNode : public Node {
  304. DataType datatype;
  305. StringName name;
  306. Node* owner;
  307. virtual DataType get_datatype() const { return datatype; }
  308. MemberNode() { type=TYPE_MEMBER; }
  309. };
  310. struct ProgramNode : public Node {
  311. struct Function {
  312. StringName name;
  313. FunctionNode*function;
  314. };
  315. Map<StringName,DataType> builtin_variables;
  316. Map<StringName,DataType> preexisting_variables;
  317. Vector<Function> functions;
  318. BlockNode *body;
  319. ProgramNode() { type=TYPE_PROGRAM; }
  320. };
  321. */
  322. enum CompletionType {
  323. COMPLETION_NONE,
  324. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  325. COMPLETION_FUNCTION,
  326. COMPLETION_IDENTIFIER,
  327. COMPLETION_PARENT_FUNCTION,
  328. COMPLETION_METHOD,
  329. COMPLETION_CALL_ARGUMENTS,
  330. COMPLETION_INDEX,
  331. COMPLETION_VIRTUAL_FUNC,
  332. COMPLETION_YIELD,
  333. };
  334. private:
  335. GDTokenizer *tokenizer;
  336. Node *head;
  337. Node *list;
  338. template<class T>
  339. T* alloc_node();
  340. bool validating;
  341. bool for_completion;
  342. int parenthesis;
  343. bool error_set;
  344. String error;
  345. int error_line;
  346. int error_column;
  347. int pending_newline;
  348. List<int> tab_level;
  349. String base_path;
  350. String self_path;
  351. ClassNode *current_class;
  352. FunctionNode *current_function;
  353. BlockNode *current_block;
  354. bool _get_completable_identifier(CompletionType p_type,StringName& identifier);
  355. void _make_completable_call(int p_arg);
  356. CompletionType completion_type;
  357. StringName completion_cursor;
  358. bool completion_static;
  359. Variant::Type completion_built_in_constant;
  360. Node *completion_node;
  361. ClassNode *completion_class;
  362. FunctionNode *completion_function;
  363. BlockNode *completion_block;
  364. int completion_line;
  365. int completion_argument;
  366. bool completion_found;
  367. bool completion_ident_is_call;
  368. PropertyInfo current_export;
  369. ScriptInstance::RPCMode rpc_mode;
  370. void _set_error(const String& p_error, int p_line=-1, int p_column=-1);
  371. bool _recover_from_completion();
  372. bool _parse_arguments(Node* p_parent, Vector<Node*>& p_args, bool p_static, bool p_can_codecomplete=false);
  373. bool _enter_indent_block(BlockNode *p_block=NULL);
  374. bool _parse_newline();
  375. Node* _parse_expression(Node *p_parent, bool p_static, bool p_allow_assign=false, bool p_parsing_constant=false);
  376. Node* _reduce_expression(Node *p_node,bool p_to_const=false);
  377. Node* _parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const=false,bool p_allow_assign=false);
  378. // TODO
  379. void _parse_pattern_block(Vector<PatternBranchNode*> &p_block, bool p_static);
  380. PatternNode *_parse_pattern(bool p_static);
  381. void _parse_block(BlockNode *p_block,bool p_static);
  382. void _parse_extends(ClassNode *p_class);
  383. void _parse_class(ClassNode *p_class);
  384. bool _end_statement();
  385. Error _parse(const String& p_base_path);
  386. public:
  387. String get_error() const;
  388. int get_error_line() const;
  389. int get_error_column() const;
  390. 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);
  391. Error parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path="",const String& p_self_path="");
  392. bool is_tool_script() const;
  393. const Node *get_parse_tree() const;
  394. //completion info
  395. CompletionType get_completion_type();
  396. StringName get_completion_cursor();
  397. int get_completion_line();
  398. Variant::Type get_completion_built_in_constant();
  399. Node *get_completion_node();
  400. ClassNode *get_completion_class();
  401. BlockNode *get_completion_block();
  402. FunctionNode *get_completion_function();
  403. int get_completion_argument_index();
  404. int get_completion_identifier_is_function();
  405. void clear();
  406. GDParser();
  407. ~GDParser();
  408. };
  409. #endif // PARSER_H