gdscript_tokenizer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**************************************************************************/
  2. /* gdscript_tokenizer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/oa_hash_map.h"
  33. #include "core/pair.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_SYNC,
  114. TK_PR_MASTER,
  115. TK_PR_SLAVE, // Deprecated by TK_PR_PUPPET, to remove in 4.0
  116. TK_PR_PUPPET,
  117. TK_PR_REMOTESYNC,
  118. TK_PR_MASTERSYNC,
  119. TK_PR_PUPPETSYNC,
  120. TK_BRACKET_OPEN,
  121. TK_BRACKET_CLOSE,
  122. TK_CURLY_BRACKET_OPEN,
  123. TK_CURLY_BRACKET_CLOSE,
  124. TK_PARENTHESIS_OPEN,
  125. TK_PARENTHESIS_CLOSE,
  126. TK_COMMA,
  127. TK_SEMICOLON,
  128. TK_PERIOD,
  129. TK_QUESTION_MARK,
  130. TK_COLON,
  131. TK_DOLLAR,
  132. TK_FORWARD_ARROW,
  133. TK_NEWLINE,
  134. TK_CONST_PI,
  135. TK_CONST_TAU,
  136. TK_WILDCARD,
  137. TK_CONST_INF,
  138. TK_CONST_NAN,
  139. TK_ERROR,
  140. TK_EOF,
  141. TK_CURSOR, //used for code completion
  142. TK_MAX
  143. };
  144. protected:
  145. enum StringMode {
  146. STRING_SINGLE_QUOTE,
  147. STRING_DOUBLE_QUOTE,
  148. STRING_MULTILINE
  149. };
  150. static const char *token_names[TK_MAX];
  151. enum {
  152. TOKEN_HASH_TABLE_TYPE_START = 3,
  153. TOKEN_HASH_TABLE_BUILTIN_START = TOKEN_HASH_TABLE_TYPE_START + Variant::VARIANT_MAX,
  154. TOKEN_HASH_TABLE_KEYWORD_START = TOKEN_HASH_TABLE_BUILTIN_START + GDScriptFunctions::FUNC_MAX,
  155. };
  156. static OAHashMap<String, int> *token_hashtable;
  157. public:
  158. static const char *get_token_name(Token p_token);
  159. static void initialize();
  160. static void terminate();
  161. bool is_token_literal(int p_offset = 0, bool variable_safe = false) const;
  162. StringName get_token_literal(int p_offset = 0) const;
  163. virtual const Variant &get_token_constant(int p_offset = 0) const = 0;
  164. virtual Token get_token(int p_offset = 0) const = 0;
  165. virtual StringName get_token_identifier(int p_offset = 0) const = 0;
  166. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const = 0;
  167. virtual Variant::Type get_token_type(int p_offset = 0) const = 0;
  168. virtual int get_token_line(int p_offset = 0) const = 0;
  169. virtual int get_token_column(int p_offset = 0) const = 0;
  170. virtual int get_token_line_indent(int p_offset = 0) const = 0;
  171. virtual int get_token_line_tab_indent(int p_offset = 0) const = 0;
  172. virtual String get_token_error(int p_offset = 0) const = 0;
  173. virtual void advance(int p_amount = 1) = 0;
  174. #ifdef DEBUG_ENABLED
  175. virtual const Vector<Pair<int, String>> &get_warning_skips() const = 0;
  176. virtual const Set<String> &get_warning_global_skips() const = 0;
  177. virtual bool is_ignoring_warnings() const = 0;
  178. #endif // DEBUG_ENABLED
  179. virtual ~GDScriptTokenizer() {}
  180. };
  181. class GDScriptTokenizerText : public GDScriptTokenizer {
  182. enum {
  183. MAX_LOOKAHEAD = 4,
  184. TK_RB_SIZE = MAX_LOOKAHEAD * 2 + 1
  185. };
  186. struct TokenData {
  187. Token type;
  188. StringName identifier; //for identifier types
  189. Variant constant; //for constant types
  190. union {
  191. Variant::Type vtype; //for type types
  192. GDScriptFunctions::Function func; //function for built in functions
  193. int warning_code; //for warning skip
  194. };
  195. int line, col;
  196. TokenData() {
  197. type = TK_EMPTY;
  198. line = col = 0;
  199. vtype = Variant::NIL;
  200. }
  201. };
  202. void _make_token(Token p_type);
  203. void _make_newline(int p_indentation = 0, int p_tabs = 0);
  204. void _make_identifier(const StringName &p_identifier);
  205. void _make_built_in_func(GDScriptFunctions::Function p_func);
  206. void _make_constant(const Variant &p_constant);
  207. void _make_type(const Variant::Type &p_type);
  208. void _make_error(const String &p_error);
  209. String code;
  210. int len;
  211. int code_pos;
  212. const CharType *_code;
  213. int line;
  214. int column;
  215. TokenData tk_rb[TK_RB_SIZE * 2 + 1];
  216. int tk_rb_pos;
  217. String last_error;
  218. bool error_flag;
  219. #ifdef DEBUG_ENABLED
  220. Vector<Pair<int, String>> warning_skips;
  221. Set<String> warning_global_skips;
  222. bool ignore_warnings;
  223. #endif // DEBUG_ENABLED
  224. void _advance();
  225. bool _parse_identifier(const String &p_str);
  226. public:
  227. void set_code(const String &p_code);
  228. virtual Token get_token(int p_offset = 0) const;
  229. virtual StringName get_token_identifier(int p_offset = 0) const;
  230. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  231. virtual Variant::Type get_token_type(int p_offset = 0) const;
  232. virtual int get_token_line(int p_offset = 0) const;
  233. virtual int get_token_column(int p_offset = 0) const;
  234. virtual int get_token_line_indent(int p_offset = 0) const;
  235. virtual int get_token_line_tab_indent(int p_offset = 0) const;
  236. virtual const Variant &get_token_constant(int p_offset = 0) const;
  237. virtual String get_token_error(int p_offset = 0) const;
  238. virtual void advance(int p_amount = 1);
  239. #ifdef DEBUG_ENABLED
  240. virtual const Vector<Pair<int, String>> &get_warning_skips() const { return warning_skips; }
  241. virtual const Set<String> &get_warning_global_skips() const { return warning_global_skips; }
  242. virtual bool is_ignoring_warnings() const { return ignore_warnings; }
  243. #endif // DEBUG_ENABLED
  244. };
  245. class GDScriptTokenizerBuffer : public GDScriptTokenizer {
  246. enum {
  247. TOKEN_BYTE_MASK = 0x80,
  248. TOKEN_BITS = 8,
  249. TOKEN_MASK = (1 << TOKEN_BITS) - 1,
  250. TOKEN_LINE_BITS = 24,
  251. TOKEN_LINE_MASK = (1 << TOKEN_LINE_BITS) - 1,
  252. };
  253. Vector<StringName> identifiers;
  254. Vector<Variant> constants;
  255. VMap<uint32_t, uint32_t> lines;
  256. Vector<uint32_t> tokens;
  257. Variant nil;
  258. int token;
  259. public:
  260. Error set_code_buffer(const Vector<uint8_t> &p_buffer);
  261. static Vector<uint8_t> parse_code_string(const String &p_code);
  262. virtual Token get_token(int p_offset = 0) const;
  263. virtual StringName get_token_identifier(int p_offset = 0) const;
  264. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  265. virtual Variant::Type get_token_type(int p_offset = 0) const;
  266. virtual int get_token_line(int p_offset = 0) const;
  267. virtual int get_token_column(int p_offset = 0) const;
  268. virtual int get_token_line_indent(int p_offset = 0) const;
  269. virtual int get_token_line_tab_indent(int p_offset = 0) const { return 0; }
  270. virtual const Variant &get_token_constant(int p_offset = 0) const;
  271. virtual String get_token_error(int p_offset = 0) const;
  272. virtual void advance(int p_amount = 1);
  273. #ifdef DEBUG_ENABLED
  274. virtual const Vector<Pair<int, String>> &get_warning_skips() const {
  275. static Vector<Pair<int, String>> v;
  276. return v;
  277. }
  278. virtual const Set<String> &get_warning_global_skips() const {
  279. static Set<String> s;
  280. return s;
  281. }
  282. virtual bool is_ignoring_warnings() const { return true; }
  283. #endif // DEBUG_ENABLED
  284. GDScriptTokenizerBuffer();
  285. };
  286. #endif // GDSCRIPT_TOKENIZER_H