gd_tokenizer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* gd_tokenizer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef GD_TOKENIZER_H
  30. #define GD_TOKENIZER_H
  31. #include "ustring.h"
  32. #include "variant.h"
  33. #include "string_db.h"
  34. #include "gd_functions.h"
  35. #include "vmap.h"
  36. class GDTokenizer {
  37. public:
  38. enum Token {
  39. TK_EMPTY,
  40. TK_IDENTIFIER,
  41. TK_CONSTANT,
  42. TK_SELF,
  43. TK_BUILT_IN_TYPE,
  44. TK_BUILT_IN_FUNC,
  45. TK_OP_IN,
  46. TK_OP_EQUAL,
  47. TK_OP_NOT_EQUAL,
  48. TK_OP_LESS,
  49. TK_OP_LESS_EQUAL,
  50. TK_OP_GREATER,
  51. TK_OP_GREATER_EQUAL,
  52. TK_OP_AND,
  53. TK_OP_OR,
  54. TK_OP_NOT,
  55. TK_OP_ADD,
  56. TK_OP_SUB,
  57. TK_OP_MUL,
  58. TK_OP_DIV,
  59. TK_OP_MOD,
  60. TK_OP_SHIFT_LEFT,
  61. TK_OP_SHIFT_RIGHT,
  62. TK_OP_ASSIGN,
  63. TK_OP_ASSIGN_ADD,
  64. TK_OP_ASSIGN_SUB,
  65. TK_OP_ASSIGN_MUL,
  66. TK_OP_ASSIGN_DIV,
  67. TK_OP_ASSIGN_MOD,
  68. TK_OP_ASSIGN_SHIFT_LEFT,
  69. TK_OP_ASSIGN_SHIFT_RIGHT,
  70. TK_OP_ASSIGN_BIT_AND,
  71. TK_OP_ASSIGN_BIT_OR,
  72. TK_OP_ASSIGN_BIT_XOR,
  73. TK_OP_BIT_AND,
  74. TK_OP_BIT_OR,
  75. TK_OP_BIT_XOR,
  76. TK_OP_BIT_INVERT,
  77. //TK_OP_PLUS_PLUS,
  78. //TK_OP_MINUS_MINUS,
  79. TK_CF_IF,
  80. TK_CF_ELIF,
  81. TK_CF_ELSE,
  82. TK_CF_FOR,
  83. TK_CF_DO,
  84. TK_CF_WHILE,
  85. TK_CF_SWITCH,
  86. TK_CF_CASE,
  87. TK_CF_BREAK,
  88. TK_CF_CONTINUE,
  89. TK_CF_PASS,
  90. TK_CF_RETURN,
  91. TK_PR_FUNCTION,
  92. TK_PR_CLASS,
  93. TK_PR_EXTENDS,
  94. TK_PR_TOOL,
  95. TK_PR_STATIC,
  96. TK_PR_EXPORT,
  97. TK_PR_SETGET,
  98. TK_PR_CONST,
  99. TK_PR_VAR,
  100. TK_PR_PRELOAD,
  101. TK_PR_ASSERT,
  102. TK_PR_YIELD,
  103. TK_BRACKET_OPEN,
  104. TK_BRACKET_CLOSE,
  105. TK_CURLY_BRACKET_OPEN,
  106. TK_CURLY_BRACKET_CLOSE,
  107. TK_PARENTHESIS_OPEN,
  108. TK_PARENTHESIS_CLOSE,
  109. TK_COMMA,
  110. TK_SEMICOLON,
  111. TK_PERIOD,
  112. TK_QUESTION_MARK,
  113. TK_COLON,
  114. TK_NEWLINE,
  115. TK_ERROR,
  116. TK_EOF,
  117. TK_CURSOR, //used for code completion
  118. TK_MAX
  119. };
  120. protected:
  121. enum StringMode {
  122. STRING_SINGLE_QUOTE,
  123. STRING_DOUBLE_QUOTE,
  124. STRING_MULTILINE
  125. };
  126. static const char* token_names[TK_MAX];
  127. public:
  128. static const char *get_token_name(Token p_token);
  129. virtual const Variant& get_token_constant(int p_offset=0) const=0;
  130. virtual Token get_token(int p_offset=0) const=0;
  131. virtual StringName get_token_identifier(int p_offset=0) const=0;
  132. virtual GDFunctions::Function get_token_built_in_func(int p_offset=0) const=0;
  133. virtual Variant::Type get_token_type(int p_offset=0) const=0;
  134. virtual int get_token_line(int p_offset=0) const=0;
  135. virtual int get_token_column(int p_offset=0) const=0;
  136. virtual int get_token_line_indent(int p_offset=0) const=0;
  137. virtual String get_token_error(int p_offset=0) const=0;
  138. virtual void advance(int p_amount=1)=0;
  139. virtual ~GDTokenizer(){};
  140. };
  141. class GDTokenizerText : public GDTokenizer {
  142. enum {
  143. MAX_LOOKAHEAD=4,
  144. TK_RB_SIZE=MAX_LOOKAHEAD*2+1
  145. };
  146. struct TokenData {
  147. Token type;
  148. StringName identifier; //for identifier types
  149. Variant constant; //for constant types
  150. union {
  151. Variant::Type vtype; //for type types
  152. GDFunctions::Function func; //function for built in functions
  153. };
  154. int line,col;
  155. TokenData() { type = TK_EMPTY; line=col=0; vtype=Variant::NIL; }
  156. };
  157. void _make_token(Token p_type);
  158. void _make_newline(int p_spaces=0);
  159. void _make_identifier(const StringName& p_identifier);
  160. void _make_built_in_func(GDFunctions::Function p_func);
  161. void _make_constant(const Variant& p_constant);
  162. void _make_type(const Variant::Type& p_type);
  163. void _make_error(const String& p_error);
  164. String code;
  165. int len;
  166. int code_pos;
  167. const CharType *_code;
  168. int line;
  169. int column;
  170. TokenData tk_rb[TK_RB_SIZE*2+1];
  171. int tk_rb_pos;
  172. String last_error;
  173. bool error_flag;
  174. void _advance();
  175. public:
  176. void set_code(const String& p_code);
  177. virtual Token get_token(int p_offset=0) const;
  178. virtual StringName get_token_identifier(int p_offset=0) const;
  179. virtual GDFunctions::Function get_token_built_in_func(int p_offset=0) const;
  180. virtual Variant::Type get_token_type(int p_offset=0) const;
  181. virtual int get_token_line(int p_offset=0) const;
  182. virtual int get_token_column(int p_offset=0) const;
  183. virtual int get_token_line_indent(int p_offset=0) const;
  184. virtual const Variant& get_token_constant(int p_offset=0) const;
  185. virtual String get_token_error(int p_offset=0) const;
  186. virtual void advance(int p_amount=1);
  187. };
  188. class GDTokenizerBuffer : public GDTokenizer {
  189. enum {
  190. TOKEN_BYTE_MASK=0x80,
  191. TOKEN_BITS=8,
  192. TOKEN_MASK=(1<<TOKEN_BITS)-1,
  193. TOKEN_LINE_BITS=24,
  194. TOKEN_LINE_MASK=(1<<TOKEN_LINE_BITS)-1,
  195. };
  196. Vector<StringName> identifiers;
  197. Vector<Variant> constants;
  198. VMap<uint32_t,uint32_t> lines;
  199. Vector<uint32_t> tokens;
  200. Variant nil;
  201. int token;
  202. public:
  203. Error set_code_buffer(const Vector<uint8_t> & p_buffer);
  204. static Vector<uint8_t> parse_code_string(const String& p_code);
  205. virtual Token get_token(int p_offset=0) const;
  206. virtual StringName get_token_identifier(int p_offset=0) const;
  207. virtual GDFunctions::Function get_token_built_in_func(int p_offset=0) const;
  208. virtual Variant::Type get_token_type(int p_offset=0) const;
  209. virtual int get_token_line(int p_offset=0) const;
  210. virtual int get_token_column(int p_offset=0) const;
  211. virtual int get_token_line_indent(int p_offset=0) const;
  212. virtual const Variant& get_token_constant(int p_offset=0) const;
  213. virtual String get_token_error(int p_offset=0) const;
  214. virtual void advance(int p_amount=1);
  215. GDTokenizerBuffer();
  216. };
  217. #endif // TOKENIZER_H