gdscript_tokenizer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_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_ENUM,
  105. TK_PR_PRELOAD,
  106. TK_PR_ASSERT,
  107. TK_PR_YIELD,
  108. TK_PR_SIGNAL,
  109. TK_PR_BREAKPOINT,
  110. TK_PR_REMOTE,
  111. TK_PR_SYNC,
  112. TK_PR_MASTER,
  113. TK_PR_SLAVE,
  114. TK_BRACKET_OPEN,
  115. TK_BRACKET_CLOSE,
  116. TK_CURLY_BRACKET_OPEN,
  117. TK_CURLY_BRACKET_CLOSE,
  118. TK_PARENTHESIS_OPEN,
  119. TK_PARENTHESIS_CLOSE,
  120. TK_COMMA,
  121. TK_SEMICOLON,
  122. TK_PERIOD,
  123. TK_QUESTION_MARK,
  124. TK_COLON,
  125. TK_DOLLAR,
  126. TK_NEWLINE,
  127. TK_CONST_PI,
  128. TK_CONST_TAU,
  129. TK_WILDCARD,
  130. TK_CONST_INF,
  131. TK_CONST_NAN,
  132. TK_ERROR,
  133. TK_EOF,
  134. TK_CURSOR, //used for code completion
  135. TK_MAX
  136. };
  137. protected:
  138. enum StringMode {
  139. STRING_SINGLE_QUOTE,
  140. STRING_DOUBLE_QUOTE,
  141. STRING_MULTILINE
  142. };
  143. static const char *token_names[TK_MAX];
  144. public:
  145. static const char *get_token_name(Token p_token);
  146. bool is_token_literal(int p_offset = 0, bool variable_safe = false) const;
  147. StringName get_token_literal(int p_offset = 0) const;
  148. virtual const Variant &get_token_constant(int p_offset = 0) const = 0;
  149. virtual Token get_token(int p_offset = 0) const = 0;
  150. virtual StringName get_token_identifier(int p_offset = 0) const = 0;
  151. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const = 0;
  152. virtual Variant::Type get_token_type(int p_offset = 0) const = 0;
  153. virtual int get_token_line(int p_offset = 0) const = 0;
  154. virtual int get_token_column(int p_offset = 0) const = 0;
  155. virtual int get_token_line_indent(int p_offset = 0) const = 0;
  156. virtual String get_token_error(int p_offset = 0) const = 0;
  157. virtual void advance(int p_amount = 1) = 0;
  158. virtual ~GDScriptTokenizer(){};
  159. };
  160. class GDScriptTokenizerText : public GDScriptTokenizer {
  161. enum {
  162. MAX_LOOKAHEAD = 4,
  163. TK_RB_SIZE = MAX_LOOKAHEAD * 2 + 1
  164. };
  165. struct TokenData {
  166. Token type;
  167. StringName identifier; //for identifier types
  168. Variant constant; //for constant types
  169. union {
  170. Variant::Type vtype; //for type types
  171. GDScriptFunctions::Function func; //function for built in functions
  172. };
  173. int line, col;
  174. TokenData() {
  175. type = TK_EMPTY;
  176. line = col = 0;
  177. vtype = Variant::NIL;
  178. }
  179. };
  180. void _make_token(Token p_type);
  181. void _make_newline(int p_spaces = 0);
  182. void _make_identifier(const StringName &p_identifier);
  183. void _make_built_in_func(GDScriptFunctions::Function p_func);
  184. void _make_constant(const Variant &p_constant);
  185. void _make_type(const Variant::Type &p_type);
  186. void _make_error(const String &p_error);
  187. String code;
  188. int len;
  189. int code_pos;
  190. const CharType *_code;
  191. int line;
  192. int column;
  193. TokenData tk_rb[TK_RB_SIZE * 2 + 1];
  194. int tk_rb_pos;
  195. String last_error;
  196. bool error_flag;
  197. void _advance();
  198. public:
  199. void set_code(const String &p_code);
  200. virtual Token get_token(int p_offset = 0) const;
  201. virtual StringName get_token_identifier(int p_offset = 0) const;
  202. virtual GDScriptFunctions::Function get_token_built_in_func(int p_offset = 0) const;
  203. virtual Variant::Type get_token_type(int p_offset = 0) const;
  204. virtual int get_token_line(int p_offset = 0) const;
  205. virtual int get_token_column(int p_offset = 0) const;
  206. virtual int get_token_line_indent(int p_offset = 0) const;
  207. virtual const Variant &get_token_constant(int p_offset = 0) const;
  208. virtual String get_token_error(int p_offset = 0) const;
  209. virtual void advance(int p_amount = 1);
  210. };
  211. class GDScriptTokenizerBuffer : public GDScriptTokenizer {
  212. enum {
  213. TOKEN_BYTE_MASK = 0x80,
  214. TOKEN_BITS = 8,
  215. TOKEN_MASK = (1 << TOKEN_BITS) - 1,
  216. TOKEN_LINE_BITS = 24,
  217. TOKEN_LINE_MASK = (1 << TOKEN_LINE_BITS) - 1,
  218. };
  219. Vector<StringName> identifiers;
  220. Vector<Variant> constants;
  221. VMap<uint32_t, uint32_t> lines;
  222. Vector<uint32_t> tokens;
  223. Variant nil;
  224. int token;
  225. public:
  226. Error set_code_buffer(const Vector<uint8_t> &p_buffer);
  227. static Vector<uint8_t> parse_code_string(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 const Variant &get_token_constant(int p_offset = 0) const;
  236. virtual String get_token_error(int p_offset = 0) const;
  237. virtual void advance(int p_amount = 1);
  238. GDScriptTokenizerBuffer();
  239. };
  240. #endif // GDSCRIPT_TOKENIZER_H