gdscript_tokenizer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* gdscript_tokenizer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "gdscript_functions.h"
  33. #include "string_db.h"
  34. #include "ustring.h"
  35. #include "variant.h"
  36. #include "vmap.h"
  37. class GDScriptTokenizer {
  38. public:
  39. enum Token {
  40. TK_EMPTY,
  41. TK_IDENTIFIER,
  42. TK_CONSTANT,
  43. TK_SELF,
  44. TK_BUILT_IN_TYPE,
  45. TK_BUILT_IN_FUNC,
  46. TK_OP_IN,
  47. TK_OP_EQUAL,
  48. TK_OP_NOT_EQUAL,
  49. TK_OP_LESS,
  50. TK_OP_LESS_EQUAL,
  51. TK_OP_GREATER,
  52. TK_OP_GREATER_EQUAL,
  53. TK_OP_AND,
  54. TK_OP_OR,
  55. TK_OP_NOT,
  56. TK_OP_ADD,
  57. TK_OP_SUB,
  58. TK_OP_MUL,
  59. TK_OP_DIV,
  60. TK_OP_MOD,
  61. TK_OP_SHIFT_LEFT,
  62. TK_OP_SHIFT_RIGHT,
  63. TK_OP_ASSIGN,
  64. TK_OP_ASSIGN_ADD,
  65. TK_OP_ASSIGN_SUB,
  66. TK_OP_ASSIGN_MUL,
  67. TK_OP_ASSIGN_DIV,
  68. TK_OP_ASSIGN_MOD,
  69. TK_OP_ASSIGN_SHIFT_LEFT,
  70. TK_OP_ASSIGN_SHIFT_RIGHT,
  71. TK_OP_ASSIGN_BIT_AND,
  72. TK_OP_ASSIGN_BIT_OR,
  73. TK_OP_ASSIGN_BIT_XOR,
  74. TK_OP_BIT_AND,
  75. TK_OP_BIT_OR,
  76. TK_OP_BIT_XOR,
  77. TK_OP_BIT_INVERT,
  78. //TK_OP_PLUS_PLUS,
  79. //TK_OP_MINUS_MINUS,
  80. TK_CF_IF,
  81. TK_CF_ELIF,
  82. TK_CF_ELSE,
  83. TK_CF_FOR,
  84. TK_CF_DO,
  85. TK_CF_WHILE,
  86. TK_CF_SWITCH,
  87. TK_CF_CASE,
  88. TK_CF_BREAK,
  89. TK_CF_CONTINUE,
  90. TK_CF_PASS,
  91. TK_CF_RETURN,
  92. TK_CF_MATCH,
  93. TK_PR_FUNCTION,
  94. TK_PR_CLASS,
  95. TK_PR_CLASS_NAME,
  96. TK_PR_EXTENDS,
  97. TK_PR_IS,
  98. TK_PR_ONREADY,
  99. TK_PR_TOOL,
  100. TK_PR_STATIC,
  101. TK_PR_EXPORT,
  102. TK_PR_SETGET,
  103. TK_PR_CONST,
  104. TK_PR_VAR,
  105. TK_PR_AS,
  106. TK_PR_VOID,
  107. TK_PR_ENUM,
  108. TK_PR_PRELOAD,
  109. TK_PR_ASSERT,
  110. TK_PR_YIELD,
  111. TK_PR_SIGNAL,
  112. TK_PR_BREAKPOINT,
  113. TK_PR_REMOTE,
  114. TK_PR_SYNC,
  115. TK_PR_MASTER,
  116. TK_PR_SLAVE,
  117. TK_PR_REMOTESYNC,
  118. TK_PR_MASTERSYNC,
  119. TK_PR_SLAVESYNC,
  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. public:
  152. static const char *get_token_name(Token p_token);
  153. bool is_token_literal(int p_offset = 0, bool variable_safe = false) const;
  154. StringName get_token_literal(int p_offset = 0) const;
  155. virtual const Variant &get_token_constant(int p_offset = 0) const = 0;
  156. virtual Token get_token(int p_offset = 0) const = 0;
  157. virtual StringName get_token_identifier(int p_offset = 0) const = 0;
  158. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const = 0;
  159. virtual Variant::Type get_token_type(int p_offset = 0) const = 0;
  160. virtual int get_token_line(int p_offset = 0) const = 0;
  161. virtual int get_token_column(int p_offset = 0) const = 0;
  162. virtual int get_token_line_indent(int p_offset = 0) const = 0;
  163. virtual String get_token_error(int p_offset = 0) const = 0;
  164. virtual void advance(int p_amount = 1) = 0;
  165. virtual ~GDScriptTokenizer(){};
  166. };
  167. class GDScriptTokenizerText : public GDScriptTokenizer {
  168. enum {
  169. MAX_LOOKAHEAD = 4,
  170. TK_RB_SIZE = MAX_LOOKAHEAD * 2 + 1
  171. };
  172. struct TokenData {
  173. Token type;
  174. StringName identifier; //for identifier types
  175. Variant constant; //for constant types
  176. union {
  177. Variant::Type vtype; //for type types
  178. GDScriptFunctions::Function func; //function for built in functions
  179. };
  180. int line, col;
  181. TokenData() {
  182. type = TK_EMPTY;
  183. line = col = 0;
  184. vtype = Variant::NIL;
  185. }
  186. };
  187. void _make_token(Token p_type);
  188. void _make_newline(int p_spaces = 0);
  189. void _make_identifier(const StringName &p_identifier);
  190. void _make_built_in_func(GDScriptFunctions::Function p_func);
  191. void _make_constant(const Variant &p_constant);
  192. void _make_type(const Variant::Type &p_type);
  193. void _make_error(const String &p_error);
  194. String code;
  195. int len;
  196. int code_pos;
  197. const CharType *_code;
  198. int line;
  199. int column;
  200. TokenData tk_rb[TK_RB_SIZE * 2 + 1];
  201. int tk_rb_pos;
  202. String last_error;
  203. bool error_flag;
  204. void _advance();
  205. public:
  206. void set_code(const String &p_code);
  207. virtual Token get_token(int p_offset = 0) const;
  208. virtual StringName get_token_identifier(int p_offset = 0) const;
  209. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  210. virtual Variant::Type get_token_type(int p_offset = 0) const;
  211. virtual int get_token_line(int p_offset = 0) const;
  212. virtual int get_token_column(int p_offset = 0) const;
  213. virtual int get_token_line_indent(int p_offset = 0) const;
  214. virtual const Variant &get_token_constant(int p_offset = 0) const;
  215. virtual String get_token_error(int p_offset = 0) const;
  216. virtual void advance(int p_amount = 1);
  217. };
  218. class GDScriptTokenizerBuffer : public GDScriptTokenizer {
  219. enum {
  220. TOKEN_BYTE_MASK = 0x80,
  221. TOKEN_BITS = 8,
  222. TOKEN_MASK = (1 << TOKEN_BITS) - 1,
  223. TOKEN_LINE_BITS = 24,
  224. TOKEN_LINE_MASK = (1 << TOKEN_LINE_BITS) - 1,
  225. };
  226. Vector<StringName> identifiers;
  227. Vector<Variant> constants;
  228. VMap<uint32_t, uint32_t> lines;
  229. Vector<uint32_t> tokens;
  230. Variant nil;
  231. int token;
  232. public:
  233. Error set_code_buffer(const Vector<uint8_t> &p_buffer);
  234. static Vector<uint8_t> parse_code_string(const String &p_code);
  235. virtual Token get_token(int p_offset = 0) const;
  236. virtual StringName get_token_identifier(int p_offset = 0) const;
  237. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  238. virtual Variant::Type get_token_type(int p_offset = 0) const;
  239. virtual int get_token_line(int p_offset = 0) const;
  240. virtual int get_token_column(int p_offset = 0) const;
  241. virtual int get_token_line_indent(int p_offset = 0) const;
  242. virtual const Variant &get_token_constant(int p_offset = 0) const;
  243. virtual String get_token_error(int p_offset = 0) const;
  244. virtual void advance(int p_amount = 1);
  245. GDScriptTokenizerBuffer();
  246. };
  247. #endif // GDSCRIPT_TOKENIZER_H