gdscript_tokenizer.h 8.9 KB

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