gd_parser.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GD_PARSER_H
  31. #define GD_PARSER_H
  32. #include "gd_functions.h"
  33. #include "gd_tokenizer.h"
  34. #include "map.h"
  35. #include "object.h"
  36. #include "script_language.h"
  37. class GDParser {
  38. public:
  39. struct Node {
  40. enum Type {
  41. TYPE_CLASS,
  42. TYPE_FUNCTION,
  43. TYPE_BUILT_IN_FUNCTION,
  44. TYPE_BLOCK,
  45. TYPE_IDENTIFIER,
  46. TYPE_TYPE,
  47. TYPE_CONSTANT,
  48. TYPE_ARRAY,
  49. TYPE_DICTIONARY,
  50. TYPE_SELF,
  51. TYPE_OPERATOR,
  52. TYPE_CONTROL_FLOW,
  53. TYPE_LOCAL_VAR,
  54. TYPE_ASSERT,
  55. TYPE_BREAKPOINT,
  56. TYPE_NEWLINE,
  57. };
  58. Node *next;
  59. int line;
  60. int column;
  61. Type type;
  62. virtual ~Node() {}
  63. };
  64. struct FunctionNode;
  65. struct BlockNode;
  66. struct ClassNode : public Node {
  67. bool tool;
  68. StringName name;
  69. bool extends_used;
  70. StringName extends_file;
  71. Vector<StringName> extends_class;
  72. struct Member {
  73. PropertyInfo _export;
  74. #ifdef TOOLS_ENABLED
  75. Variant default_value;
  76. #endif
  77. StringName identifier;
  78. StringName setter;
  79. StringName getter;
  80. int line;
  81. Node *expression;
  82. ScriptInstance::RPCMode rpc_mode;
  83. };
  84. struct Constant {
  85. StringName identifier;
  86. Node *expression;
  87. };
  88. struct Signal {
  89. StringName name;
  90. Vector<StringName> arguments;
  91. };
  92. Vector<ClassNode *> subclasses;
  93. Vector<Member> variables;
  94. Vector<Constant> constant_expressions;
  95. Vector<FunctionNode *> functions;
  96. Vector<FunctionNode *> static_functions;
  97. Vector<Signal> _signals;
  98. BlockNode *initializer;
  99. BlockNode *ready;
  100. ClassNode *owner;
  101. //Vector<Node*> initializers;
  102. int end_line;
  103. ClassNode() {
  104. tool = false;
  105. type = TYPE_CLASS;
  106. extends_used = false;
  107. end_line = -1;
  108. owner = NULL;
  109. }
  110. };
  111. struct FunctionNode : public Node {
  112. bool _static;
  113. ScriptInstance::RPCMode rpc_mode;
  114. StringName name;
  115. Vector<StringName> arguments;
  116. Vector<Node *> default_values;
  117. BlockNode *body;
  118. FunctionNode() {
  119. type = TYPE_FUNCTION;
  120. _static = false;
  121. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  122. }
  123. };
  124. struct BlockNode : public Node {
  125. ClassNode *parent_class;
  126. BlockNode *parent_block;
  127. Map<StringName, int> locals;
  128. List<Node *> statements;
  129. Vector<StringName> variables;
  130. Vector<int> variable_lines;
  131. //the following is useful for code completion
  132. List<BlockNode *> sub_blocks;
  133. int end_line;
  134. BlockNode() {
  135. type = TYPE_BLOCK;
  136. end_line = -1;
  137. parent_block = NULL;
  138. parent_class = NULL;
  139. }
  140. };
  141. struct TypeNode : public Node {
  142. Variant::Type vtype;
  143. TypeNode() { type = TYPE_TYPE; }
  144. };
  145. struct BuiltInFunctionNode : public Node {
  146. GDFunctions::Function function;
  147. BuiltInFunctionNode() { type = TYPE_BUILT_IN_FUNCTION; }
  148. };
  149. struct IdentifierNode : public Node {
  150. StringName name;
  151. IdentifierNode() { type = TYPE_IDENTIFIER; }
  152. };
  153. struct LocalVarNode : public Node {
  154. StringName name;
  155. Node *assign;
  156. LocalVarNode() {
  157. type = TYPE_LOCAL_VAR;
  158. assign = NULL;
  159. }
  160. };
  161. struct ConstantNode : public Node {
  162. Variant value;
  163. ConstantNode() { type = TYPE_CONSTANT; }
  164. };
  165. struct ArrayNode : public Node {
  166. Vector<Node *> elements;
  167. ArrayNode() { type = TYPE_ARRAY; }
  168. };
  169. struct DictionaryNode : public Node {
  170. struct Pair {
  171. Node *key;
  172. Node *value;
  173. };
  174. Vector<Pair> elements;
  175. DictionaryNode() { type = TYPE_DICTIONARY; }
  176. };
  177. struct SelfNode : public Node {
  178. SelfNode() { type = TYPE_SELF; }
  179. };
  180. struct OperatorNode : public Node {
  181. enum Operator {
  182. //call/constructor operator
  183. OP_CALL,
  184. OP_PARENT_CALL,
  185. OP_YIELD,
  186. OP_IS,
  187. //indexing operator
  188. OP_INDEX,
  189. OP_INDEX_NAMED,
  190. //unary operators
  191. OP_NEG,
  192. OP_POS,
  193. OP_NOT,
  194. OP_BIT_INVERT,
  195. OP_PREINC,
  196. OP_PREDEC,
  197. OP_INC,
  198. OP_DEC,
  199. //binary operators (in precedence order)
  200. OP_IN,
  201. OP_EQUAL,
  202. OP_NOT_EQUAL,
  203. OP_LESS,
  204. OP_LESS_EQUAL,
  205. OP_GREATER,
  206. OP_GREATER_EQUAL,
  207. OP_AND,
  208. OP_OR,
  209. OP_ADD,
  210. OP_SUB,
  211. OP_MUL,
  212. OP_DIV,
  213. OP_MOD,
  214. OP_SHIFT_LEFT,
  215. OP_SHIFT_RIGHT,
  216. OP_INIT_ASSIGN,
  217. OP_ASSIGN,
  218. OP_ASSIGN_ADD,
  219. OP_ASSIGN_SUB,
  220. OP_ASSIGN_MUL,
  221. OP_ASSIGN_DIV,
  222. OP_ASSIGN_MOD,
  223. OP_ASSIGN_SHIFT_LEFT,
  224. OP_ASSIGN_SHIFT_RIGHT,
  225. OP_ASSIGN_BIT_AND,
  226. OP_ASSIGN_BIT_OR,
  227. OP_ASSIGN_BIT_XOR,
  228. OP_BIT_AND,
  229. OP_BIT_OR,
  230. OP_BIT_XOR,
  231. //ternary operators
  232. OP_TERNARY_IF,
  233. OP_TERNARY_ELSE,
  234. };
  235. Operator op;
  236. Vector<Node *> arguments;
  237. OperatorNode() { type = TYPE_OPERATOR; }
  238. };
  239. struct PatternNode : public Node {
  240. enum PatternType {
  241. PT_CONSTANT,
  242. PT_BIND,
  243. PT_DICTIONARY,
  244. PT_ARRAY,
  245. PT_IGNORE_REST,
  246. PT_WILDCARD
  247. };
  248. PatternType pt_type;
  249. Node *constant;
  250. StringName bind;
  251. Map<ConstantNode *, PatternNode *> dictionary;
  252. Vector<PatternNode *> array;
  253. };
  254. struct PatternBranchNode : public Node {
  255. Vector<PatternNode *> patterns;
  256. BlockNode *body;
  257. };
  258. struct MatchNode : public Node {
  259. Node *val_to_match;
  260. Vector<PatternBranchNode *> branches;
  261. struct CompiledPatternBranch {
  262. Node *compiled_pattern;
  263. BlockNode *body;
  264. };
  265. Vector<CompiledPatternBranch> compiled_pattern_branches;
  266. };
  267. struct ControlFlowNode : public Node {
  268. enum CFType {
  269. CF_IF,
  270. CF_FOR,
  271. CF_WHILE,
  272. CF_SWITCH,
  273. CF_BREAK,
  274. CF_CONTINUE,
  275. CF_RETURN,
  276. CF_MATCH
  277. };
  278. CFType cf_type;
  279. Vector<Node *> arguments;
  280. BlockNode *body;
  281. BlockNode *body_else;
  282. MatchNode *match;
  283. ControlFlowNode *_else; //used for if
  284. ControlFlowNode() {
  285. type = TYPE_CONTROL_FLOW;
  286. cf_type = CF_IF;
  287. body = NULL;
  288. body_else = NULL;
  289. }
  290. };
  291. struct AssertNode : public Node {
  292. Node *condition;
  293. AssertNode() { type = TYPE_ASSERT; }
  294. };
  295. struct BreakpointNode : public Node {
  296. BreakpointNode() { type = TYPE_BREAKPOINT; }
  297. };
  298. struct NewLineNode : public Node {
  299. NewLineNode() { type = TYPE_NEWLINE; }
  300. };
  301. struct Expression {
  302. bool is_op;
  303. union {
  304. OperatorNode::Operator op;
  305. Node *node;
  306. };
  307. };
  308. /*
  309. struct OperatorNode : public Node {
  310. DataType return_cache;
  311. Operator op;
  312. Vector<Node*> arguments;
  313. virtual DataType get_datatype() const { return return_cache; }
  314. OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; }
  315. };
  316. struct VariableNode : public Node {
  317. DataType datatype_cache;
  318. StringName name;
  319. virtual DataType get_datatype() const { return datatype_cache; }
  320. VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; }
  321. };
  322. struct ConstantNode : public Node {
  323. DataType datatype;
  324. Variant value;
  325. virtual DataType get_datatype() const { return datatype; }
  326. ConstantNode() { type=TYPE_CONSTANT; }
  327. };
  328. struct BlockNode : public Node {
  329. Map<StringName,DataType> variables;
  330. List<Node*> statements;
  331. BlockNode() { type=TYPE_BLOCK; }
  332. };
  333. struct ControlFlowNode : public Node {
  334. FlowOperation flow_op;
  335. Vector<Node*> statements;
  336. ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
  337. };
  338. struct MemberNode : public Node {
  339. DataType datatype;
  340. StringName name;
  341. Node* owner;
  342. virtual DataType get_datatype() const { return datatype; }
  343. MemberNode() { type=TYPE_MEMBER; }
  344. };
  345. struct ProgramNode : public Node {
  346. struct Function {
  347. StringName name;
  348. FunctionNode*function;
  349. };
  350. Map<StringName,DataType> builtin_variables;
  351. Map<StringName,DataType> preexisting_variables;
  352. Vector<Function> functions;
  353. BlockNode *body;
  354. ProgramNode() { type=TYPE_PROGRAM; }
  355. };
  356. */
  357. enum CompletionType {
  358. COMPLETION_NONE,
  359. COMPLETION_BUILT_IN_TYPE_CONSTANT,
  360. COMPLETION_GET_NODE,
  361. COMPLETION_FUNCTION,
  362. COMPLETION_IDENTIFIER,
  363. COMPLETION_PARENT_FUNCTION,
  364. COMPLETION_METHOD,
  365. COMPLETION_CALL_ARGUMENTS,
  366. COMPLETION_RESOURCE_PATH,
  367. COMPLETION_INDEX,
  368. COMPLETION_VIRTUAL_FUNC,
  369. COMPLETION_YIELD,
  370. COMPLETION_ASSIGN,
  371. };
  372. private:
  373. GDTokenizer *tokenizer;
  374. Node *head;
  375. Node *list;
  376. template <class T>
  377. T *alloc_node();
  378. bool validating;
  379. bool for_completion;
  380. int parenthesis;
  381. bool error_set;
  382. String error;
  383. int error_line;
  384. int error_column;
  385. int pending_newline;
  386. List<int> tab_level;
  387. String base_path;
  388. String self_path;
  389. ClassNode *current_class;
  390. FunctionNode *current_function;
  391. BlockNode *current_block;
  392. bool _get_completable_identifier(CompletionType p_type, StringName &identifier);
  393. void _make_completable_call(int p_arg);
  394. CompletionType completion_type;
  395. StringName completion_cursor;
  396. bool completion_static;
  397. Variant::Type completion_built_in_constant;
  398. Node *completion_node;
  399. ClassNode *completion_class;
  400. FunctionNode *completion_function;
  401. BlockNode *completion_block;
  402. int completion_line;
  403. int completion_argument;
  404. bool completion_found;
  405. bool completion_ident_is_call;
  406. PropertyInfo current_export;
  407. ScriptInstance::RPCMode rpc_mode;
  408. void _set_error(const String &p_error, int p_line = -1, int p_column = -1);
  409. bool _recover_from_completion();
  410. bool _parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete = false);
  411. bool _enter_indent_block(BlockNode *p_block = NULL);
  412. bool _parse_newline();
  413. Node *_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign = false, bool p_parsing_constant = false);
  414. Node *_reduce_expression(Node *p_node, bool p_to_const = false);
  415. Node *_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const = false, bool p_allow_assign = false);
  416. PatternNode *_parse_pattern(bool p_static);
  417. void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static);
  418. void _transform_match_statment(BlockNode *p_block, MatchNode *p_match_statement);
  419. void _generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings);
  420. void _parse_block(BlockNode *p_block, bool p_static);
  421. void _parse_extends(ClassNode *p_class);
  422. void _parse_class(ClassNode *p_class);
  423. bool _end_statement();
  424. Error _parse(const String &p_base_path);
  425. public:
  426. String get_error() const;
  427. int get_error_line() const;
  428. int get_error_column() const;
  429. 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);
  430. Error parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path = "", const String &p_self_path = "");
  431. bool is_tool_script() const;
  432. const Node *get_parse_tree() const;
  433. //completion info
  434. CompletionType get_completion_type();
  435. StringName get_completion_cursor();
  436. int get_completion_line();
  437. Variant::Type get_completion_built_in_constant();
  438. Node *get_completion_node();
  439. ClassNode *get_completion_class();
  440. BlockNode *get_completion_block();
  441. FunctionNode *get_completion_function();
  442. int get_completion_argument_index();
  443. int get_completion_identifier_is_function();
  444. void clear();
  445. GDParser();
  446. ~GDParser();
  447. };
  448. #endif // PARSER_H