gd_parser.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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-2017 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_POS,
  174. OP_NOT,
  175. OP_BIT_INVERT,
  176. OP_PREINC,
  177. OP_PREDEC,
  178. OP_INC,
  179. OP_DEC,
  180. //binary operators (in precedence order)
  181. OP_IN,
  182. OP_EQUAL,
  183. OP_NOT_EQUAL,
  184. OP_LESS,
  185. OP_LESS_EQUAL,
  186. OP_GREATER,
  187. OP_GREATER_EQUAL,
  188. OP_AND,
  189. OP_OR,
  190. OP_ADD,
  191. OP_SUB,
  192. OP_MUL,
  193. OP_DIV,
  194. OP_MOD,
  195. OP_SHIFT_LEFT,
  196. OP_SHIFT_RIGHT,
  197. OP_INIT_ASSIGN,
  198. OP_ASSIGN,
  199. OP_ASSIGN_ADD,
  200. OP_ASSIGN_SUB,
  201. OP_ASSIGN_MUL,
  202. OP_ASSIGN_DIV,
  203. OP_ASSIGN_MOD,
  204. OP_ASSIGN_SHIFT_LEFT,
  205. OP_ASSIGN_SHIFT_RIGHT,
  206. OP_ASSIGN_BIT_AND,
  207. OP_ASSIGN_BIT_OR,
  208. OP_ASSIGN_BIT_XOR,
  209. OP_BIT_AND,
  210. OP_BIT_OR,
  211. OP_BIT_XOR,
  212. //ternary operators
  213. OP_TERNARY_IF,
  214. OP_TERNARY_ELSE,
  215. };
  216. Operator op;
  217. Vector<Node*> arguments;
  218. OperatorNode() { type=TYPE_OPERATOR; }
  219. };
  220. struct PatternNode : public Node {
  221. enum PatternType {
  222. PT_CONSTANT,
  223. PT_BIND,
  224. PT_DICTIONARY,
  225. PT_ARRAY,
  226. PT_IGNORE_REST,
  227. PT_WILDCARD
  228. };
  229. PatternType pt_type;
  230. Node *constant;
  231. StringName bind;
  232. Map<ConstantNode*, PatternNode*> dictionary;
  233. Vector<PatternNode*> array;
  234. };
  235. struct PatternBranchNode : public Node {
  236. Vector<PatternNode*> patterns;
  237. BlockNode *body;
  238. };
  239. struct MatchNode : public Node {
  240. Node *val_to_match;
  241. Vector<PatternBranchNode*> branches;
  242. struct CompiledPatternBranch {
  243. Node *compiled_pattern;
  244. BlockNode *body;
  245. };
  246. Vector<CompiledPatternBranch> compiled_pattern_branches;
  247. };
  248. struct ControlFlowNode : public Node {
  249. enum CFType {
  250. CF_IF,
  251. CF_FOR,
  252. CF_WHILE,
  253. CF_SWITCH,
  254. CF_BREAK,
  255. CF_CONTINUE,
  256. CF_RETURN,
  257. CF_MATCH
  258. };
  259. CFType cf_type;
  260. Vector<Node*> arguments;
  261. BlockNode *body;
  262. BlockNode *body_else;
  263. MatchNode *match;
  264. ControlFlowNode *_else; //used for if
  265. ControlFlowNode() { type=TYPE_CONTROL_FLOW; cf_type=CF_IF; body=NULL; body_else=NULL;}
  266. };
  267. struct AssertNode : public Node {
  268. Node* condition;
  269. AssertNode() { type=TYPE_ASSERT; }
  270. };
  271. struct BreakpointNode : public Node {
  272. BreakpointNode() { type=TYPE_BREAKPOINT; }
  273. };
  274. struct NewLineNode : public Node {
  275. NewLineNode() { type=TYPE_NEWLINE; }
  276. };
  277. struct Expression {
  278. bool is_op;
  279. union {
  280. OperatorNode::Operator op;
  281. Node *node;
  282. };
  283. };
  284. /*
  285. struct OperatorNode : public Node {
  286. DataType return_cache;
  287. Operator op;
  288. Vector<Node*> arguments;
  289. virtual DataType get_datatype() const { return return_cache; }
  290. OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
  291. };
  292. struct VariableNode : public Node {
  293. DataType datatype_cache;
  294. StringName name;
  295. virtual DataType get_datatype() const { return datatype_cache; }
  296. VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; }
  297. };
  298. struct ConstantNode : public Node {
  299. DataType datatype;
  300. Variant value;
  301. virtual DataType get_datatype() const { return datatype; }
  302. ConstantNode() { type=TYPE_CONSTANT; }
  303. };
  304. struct BlockNode : public Node {
  305. Map<StringName,DataType> variables;
  306. List<Node*> statements;
  307. BlockNode() { type=TYPE_BLOCK; }
  308. };
  309. struct ControlFlowNode : public Node {
  310. FlowOperation flow_op;
  311. Vector<Node*> statements;
  312. ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
  313. };
  314. struct MemberNode : public Node {
  315. DataType datatype;
  316. StringName name;
  317. Node* owner;
  318. virtual DataType get_datatype() const { return datatype; }
  319. MemberNode() { type=TYPE_MEMBER; }
  320. };
  321. struct ProgramNode : public Node {
  322. struct Function {
  323. StringName name;
  324. FunctionNode*function;
  325. };
  326. Map<StringName,DataType> builtin_variables;
  327. Map<StringName,DataType> preexisting_variables;
  328. Vector<Function> functions;
  329. BlockNode *body;
  330. ProgramNode() { type=TYPE_PROGRAM; }
  331. };
  332. */
  333. enum CompletionType {
  334. COMPLETION_NONE,
  335. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  336. COMPLETION_GET_NODE,
  337. COMPLETION_FUNCTION,
  338. COMPLETION_IDENTIFIER,
  339. COMPLETION_PARENT_FUNCTION,
  340. COMPLETION_METHOD,
  341. COMPLETION_CALL_ARGUMENTS,
  342. COMPLETION_INDEX,
  343. COMPLETION_VIRTUAL_FUNC,
  344. COMPLETION_YIELD,
  345. };
  346. private:
  347. GDTokenizer *tokenizer;
  348. Node *head;
  349. Node *list;
  350. template<class T>
  351. T* alloc_node();
  352. bool validating;
  353. bool for_completion;
  354. int parenthesis;
  355. bool error_set;
  356. String error;
  357. int error_line;
  358. int error_column;
  359. int pending_newline;
  360. List<int> tab_level;
  361. String base_path;
  362. String self_path;
  363. ClassNode *current_class;
  364. FunctionNode *current_function;
  365. BlockNode *current_block;
  366. bool _get_completable_identifier(CompletionType p_type,StringName& identifier);
  367. void _make_completable_call(int p_arg);
  368. CompletionType completion_type;
  369. StringName completion_cursor;
  370. bool completion_static;
  371. Variant::Type completion_built_in_constant;
  372. Node *completion_node;
  373. ClassNode *completion_class;
  374. FunctionNode *completion_function;
  375. BlockNode *completion_block;
  376. int completion_line;
  377. int completion_argument;
  378. bool completion_found;
  379. bool completion_ident_is_call;
  380. PropertyInfo current_export;
  381. ScriptInstance::RPCMode rpc_mode;
  382. void _set_error(const String& p_error, int p_line=-1, int p_column=-1);
  383. bool _recover_from_completion();
  384. bool _parse_arguments(Node* p_parent, Vector<Node*>& p_args, bool p_static, bool p_can_codecomplete=false);
  385. bool _enter_indent_block(BlockNode *p_block=NULL);
  386. bool _parse_newline();
  387. Node* _parse_expression(Node *p_parent, bool p_static, bool p_allow_assign=false, bool p_parsing_constant=false);
  388. Node* _reduce_expression(Node *p_node,bool p_to_const=false);
  389. Node* _parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const=false,bool p_allow_assign=false);
  390. PatternNode *_parse_pattern(bool p_static);
  391. void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode*> &p_branches, bool p_static);
  392. void _transform_match_statment(BlockNode *p_block, MatchNode *p_match_statement);
  393. void _generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node*> &p_bindings);
  394. void _parse_block(BlockNode *p_block,bool p_static);
  395. void _parse_extends(ClassNode *p_class);
  396. void _parse_class(ClassNode *p_class);
  397. bool _end_statement();
  398. Error _parse(const String& p_base_path);
  399. public:
  400. String get_error() const;
  401. int get_error_line() const;
  402. int get_error_column() const;
  403. 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);
  404. Error parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path="",const String& p_self_path="");
  405. bool is_tool_script() const;
  406. const Node *get_parse_tree() const;
  407. //completion info
  408. CompletionType get_completion_type();
  409. StringName get_completion_cursor();
  410. int get_completion_line();
  411. Variant::Type get_completion_built_in_constant();
  412. Node *get_completion_node();
  413. ClassNode *get_completion_class();
  414. BlockNode *get_completion_block();
  415. FunctionNode *get_completion_function();
  416. int get_completion_argument_index();
  417. int get_completion_identifier_is_function();
  418. void clear();
  419. GDParser();
  420. ~GDParser();
  421. };
  422. #endif // PARSER_H