gdscript_tokenizer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*************************************************************************/
  2. /* gdscript_tokenizer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 GDSCRIPT_TOKENIZER_H
  31. #define GDSCRIPT_TOKENIZER_H
  32. #include "core/pair.h"
  33. #include "core/set.h"
  34. #include "core/string_name.h"
  35. #include "core/ustring.h"
  36. #include "core/variant.h"
  37. #include "core/vmap.h"
  38. #include "gdscript_functions.h"
  39. class GDScriptTokenizer {
  40. public:
  41. enum Token {
  42. TK_EMPTY,
  43. TK_IDENTIFIER,
  44. TK_CONSTANT,
  45. TK_SELF,
  46. TK_BUILT_IN_TYPE,
  47. TK_BUILT_IN_FUNC,
  48. TK_OP_IN,
  49. TK_OP_EQUAL,
  50. TK_OP_NOT_EQUAL,
  51. TK_OP_LESS,
  52. TK_OP_LESS_EQUAL,
  53. TK_OP_GREATER,
  54. TK_OP_GREATER_EQUAL,
  55. TK_OP_AND,
  56. TK_OP_OR,
  57. TK_OP_NOT,
  58. TK_OP_ADD,
  59. TK_OP_SUB,
  60. TK_OP_MUL,
  61. TK_OP_DIV,
  62. TK_OP_MOD,
  63. TK_OP_SHIFT_LEFT,
  64. TK_OP_SHIFT_RIGHT,
  65. TK_OP_ASSIGN,
  66. TK_OP_ASSIGN_ADD,
  67. TK_OP_ASSIGN_SUB,
  68. TK_OP_ASSIGN_MUL,
  69. TK_OP_ASSIGN_DIV,
  70. TK_OP_ASSIGN_MOD,
  71. TK_OP_ASSIGN_SHIFT_LEFT,
  72. TK_OP_ASSIGN_SHIFT_RIGHT,
  73. TK_OP_ASSIGN_BIT_AND,
  74. TK_OP_ASSIGN_BIT_OR,
  75. TK_OP_ASSIGN_BIT_XOR,
  76. TK_OP_BIT_AND,
  77. TK_OP_BIT_OR,
  78. TK_OP_BIT_XOR,
  79. TK_OP_BIT_INVERT,
  80. //TK_OP_PLUS_PLUS,
  81. //TK_OP_MINUS_MINUS,
  82. TK_CF_IF,
  83. TK_CF_ELIF,
  84. TK_CF_ELSE,
  85. TK_CF_FOR,
  86. TK_CF_WHILE,
  87. TK_CF_BREAK,
  88. TK_CF_CONTINUE,
  89. TK_CF_PASS,
  90. TK_CF_RETURN,
  91. TK_CF_MATCH,
  92. TK_PR_FUNCTION,
  93. TK_PR_CLASS,
  94. TK_PR_CLASS_NAME,
  95. TK_PR_EXTENDS,
  96. TK_PR_IS,
  97. TK_PR_ONREADY,
  98. TK_PR_TOOL,
  99. TK_PR_STATIC,
  100. TK_PR_EXPORT,
  101. TK_PR_SETGET,
  102. TK_PR_CONST,
  103. TK_PR_VAR,
  104. TK_PR_AS,
  105. TK_PR_VOID,
  106. TK_PR_ENUM,
  107. TK_PR_PRELOAD,
  108. TK_PR_ASSERT,
  109. TK_PR_YIELD,
  110. TK_PR_SIGNAL,
  111. TK_PR_BREAKPOINT,
  112. TK_PR_REMOTE,
  113. TK_PR_MASTER,
  114. TK_PR_PUPPET,
  115. TK_PR_REMOTESYNC,
  116. TK_PR_MASTERSYNC,
  117. TK_PR_PUPPETSYNC,
  118. TK_BRACKET_OPEN,
  119. TK_BRACKET_CLOSE,
  120. TK_CURLY_BRACKET_OPEN,
  121. TK_CURLY_BRACKET_CLOSE,
  122. TK_PARENTHESIS_OPEN,
  123. TK_PARENTHESIS_CLOSE,
  124. TK_COMMA,
  125. TK_SEMICOLON,
  126. TK_PERIOD,
  127. TK_QUESTION_MARK,
  128. TK_COLON,
  129. TK_DOLLAR,
  130. TK_FORWARD_ARROW,
  131. TK_NEWLINE,
  132. TK_CONST_PI,
  133. TK_CONST_TAU,
  134. TK_WILDCARD,
  135. TK_CONST_INF,
  136. TK_CONST_NAN,
  137. TK_ERROR,
  138. TK_EOF,
  139. TK_CURSOR, //used for code completion
  140. TK_MAX
  141. };
  142. protected:
  143. enum StringMode {
  144. STRING_SINGLE_QUOTE,
  145. STRING_DOUBLE_QUOTE,
  146. STRING_MULTILINE
  147. };
  148. static const char *token_names[TK_MAX];
  149. public:
  150. static const char *get_token_name(Token p_token);
  151. bool is_token_literal(int p_offset = 0, bool variable_safe = false) const;
  152. StringName get_token_literal(int p_offset = 0) const;
  153. virtual const Variant &get_token_constant(int p_offset = 0) const = 0;
  154. virtual Token get_token(int p_offset = 0) const = 0;
  155. virtual StringName get_token_identifier(int p_offset = 0) const = 0;
  156. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const = 0;
  157. virtual Variant::Type get_token_type(int p_offset = 0) const = 0;
  158. virtual int get_token_line(int p_offset = 0) const = 0;
  159. virtual int get_token_column(int p_offset = 0) const = 0;
  160. virtual int get_token_line_indent(int p_offset = 0) const = 0;
  161. virtual int get_token_line_tab_indent(int p_offset = 0) const = 0;
  162. virtual String get_token_error(int p_offset = 0) const = 0;
  163. virtual void advance(int p_amount = 1) = 0;
  164. #ifdef DEBUG_ENABLED
  165. virtual const Vector<Pair<int, String>> &get_warning_skips() const = 0;
  166. virtual const Set<String> &get_warning_global_skips() const = 0;
  167. virtual bool is_ignoring_warnings() const = 0;
  168. #endif // DEBUG_ENABLED
  169. virtual ~GDScriptTokenizer(){};
  170. };
  171. class GDScriptTokenizerText : public GDScriptTokenizer {
  172. enum {
  173. MAX_LOOKAHEAD = 4,
  174. TK_RB_SIZE = MAX_LOOKAHEAD * 2 + 1
  175. };
  176. struct TokenData {
  177. Token type;
  178. StringName identifier; //for identifier types
  179. Variant constant; //for constant types
  180. union {
  181. Variant::Type vtype; //for type types
  182. GDScriptFunctions::Function func; //function for built in functions
  183. int warning_code; //for warning skip
  184. };
  185. int line, col;
  186. TokenData() {
  187. type = TK_EMPTY;
  188. line = col = 0;
  189. vtype = Variant::NIL;
  190. }
  191. };
  192. void _make_token(Token p_type);
  193. void _make_newline(int p_indentation = 0, int p_tabs = 0);
  194. void _make_identifier(const StringName &p_identifier);
  195. void _make_built_in_func(GDScriptFunctions::Function p_func);
  196. void _make_constant(const Variant &p_constant);
  197. void _make_type(const Variant::Type &p_type);
  198. void _make_error(const String &p_error);
  199. String code;
  200. int len;
  201. int code_pos;
  202. const CharType *_code;
  203. int line;
  204. int column;
  205. TokenData tk_rb[TK_RB_SIZE * 2 + 1];
  206. int tk_rb_pos;
  207. String last_error;
  208. bool error_flag;
  209. #ifdef DEBUG_ENABLED
  210. Vector<Pair<int, String>> warning_skips;
  211. Set<String> warning_global_skips;
  212. bool ignore_warnings;
  213. #endif // DEBUG_ENABLED
  214. void _advance();
  215. public:
  216. void set_code(const String &p_code);
  217. virtual Token get_token(int p_offset = 0) const;
  218. virtual StringName get_token_identifier(int p_offset = 0) const;
  219. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  220. virtual Variant::Type get_token_type(int p_offset = 0) const;
  221. virtual int get_token_line(int p_offset = 0) const;
  222. virtual int get_token_column(int p_offset = 0) const;
  223. virtual int get_token_line_indent(int p_offset = 0) const;
  224. virtual int get_token_line_tab_indent(int p_offset = 0) const;
  225. virtual const Variant &get_token_constant(int p_offset = 0) const;
  226. virtual String get_token_error(int p_offset = 0) const;
  227. virtual void advance(int p_amount = 1);
  228. #ifdef DEBUG_ENABLED
  229. virtual const Vector<Pair<int, String>> &get_warning_skips() const { return warning_skips; }
  230. virtual const Set<String> &get_warning_global_skips() const { return warning_global_skips; }
  231. virtual bool is_ignoring_warnings() const { return ignore_warnings; }
  232. #endif // DEBUG_ENABLED
  233. };
  234. class GDScriptTokenizerBuffer : public GDScriptTokenizer {
  235. enum {
  236. TOKEN_BYTE_MASK = 0x80,
  237. TOKEN_BITS = 8,
  238. TOKEN_MASK = (1 << TOKEN_BITS) - 1,
  239. TOKEN_LINE_BITS = 24,
  240. TOKEN_LINE_MASK = (1 << TOKEN_LINE_BITS) - 1,
  241. };
  242. Vector<StringName> identifiers;
  243. Vector<Variant> constants;
  244. VMap<uint32_t, uint32_t> lines;
  245. Vector<uint32_t> tokens;
  246. Variant nil;
  247. int token;
  248. public:
  249. Error set_code_buffer(const Vector<uint8_t> &p_buffer);
  250. static Vector<uint8_t> parse_code_string(const String &p_code);
  251. virtual Token get_token(int p_offset = 0) const;
  252. virtual StringName get_token_identifier(int p_offset = 0) const;
  253. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  254. virtual Variant::Type get_token_type(int p_offset = 0) const;
  255. virtual int get_token_line(int p_offset = 0) const;
  256. virtual int get_token_column(int p_offset = 0) const;
  257. virtual int get_token_line_indent(int p_offset = 0) const;
  258. virtual int get_token_line_tab_indent(int p_offset = 0) const { return 0; }
  259. virtual const Variant &get_token_constant(int p_offset = 0) const;
  260. virtual String get_token_error(int p_offset = 0) const;
  261. virtual void advance(int p_amount = 1);
  262. #ifdef DEBUG_ENABLED
  263. virtual const Vector<Pair<int, String>> &get_warning_skips() const {
  264. static Vector<Pair<int, String>> v;
  265. return v;
  266. }
  267. virtual const Set<String> &get_warning_global_skips() const {
  268. static Set<String> s;
  269. return s;
  270. }
  271. virtual bool is_ignoring_warnings() const { return true; }
  272. #endif // DEBUG_ENABLED
  273. GDScriptTokenizerBuffer();
  274. };
  275. #endif // GDSCRIPT_TOKENIZER_H