gd_tokenizer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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-2014 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_CONST,
  98. TK_PR_VAR,
  99. TK_PR_PRELOAD,
  100. TK_PR_ASSERT,
  101. TK_BRACKET_OPEN,
  102. TK_BRACKET_CLOSE,
  103. TK_CURLY_BRACKET_OPEN,
  104. TK_CURLY_BRACKET_CLOSE,
  105. TK_PARENTHESIS_OPEN,
  106. TK_PARENTHESIS_CLOSE,
  107. TK_COMMA,
  108. TK_SEMICOLON,
  109. TK_PERIOD,
  110. TK_QUESTION_MARK,
  111. TK_COLON,
  112. TK_NEWLINE,
  113. TK_ERROR,
  114. TK_EOF,
  115. TK_MAX
  116. };
  117. protected:
  118. static const char* token_names[TK_MAX];
  119. public:
  120. static const char *get_token_name(Token p_token);
  121. virtual const Variant& get_token_constant(int p_offset=0) const=0;
  122. virtual Token get_token(int p_offset=0) const=0;
  123. virtual StringName get_token_identifier(int p_offset=0) const=0;
  124. virtual GDFunctions::Function get_token_built_in_func(int p_offset=0) const=0;
  125. virtual Variant::Type get_token_type(int p_offset=0) const=0;
  126. virtual int get_token_line(int p_offset=0) const=0;
  127. virtual int get_token_column(int p_offset=0) const=0;
  128. virtual int get_token_line_indent(int p_offset=0) const=0;
  129. virtual String get_token_error(int p_offset=0) const=0;
  130. virtual void advance(int p_amount=1)=0;
  131. virtual ~GDTokenizer(){};
  132. };
  133. class GDTokenizerText : public GDTokenizer {
  134. enum {
  135. MAX_LOOKAHEAD=4,
  136. TK_RB_SIZE=MAX_LOOKAHEAD*2+1
  137. };
  138. struct TokenData {
  139. Token type;
  140. StringName identifier; //for identifier types
  141. Variant constant; //for constant types
  142. union {
  143. Variant::Type vtype; //for type types
  144. GDFunctions::Function func; //function for built in functions
  145. };
  146. int line,col;
  147. TokenData() { type = TK_EMPTY; line=col=0; vtype=Variant::NIL; }
  148. };
  149. void _make_token(Token p_type);
  150. void _make_newline(int p_spaces=0);
  151. void _make_identifier(const StringName& p_identifier);
  152. void _make_built_in_func(GDFunctions::Function p_func);
  153. void _make_constant(const Variant& p_constant);
  154. void _make_type(const Variant::Type& p_type);
  155. void _make_error(const String& p_error);
  156. String code;
  157. int len;
  158. int code_pos;
  159. const CharType *_code;
  160. int line;
  161. int column;
  162. TokenData tk_rb[TK_RB_SIZE*2+1];
  163. int tk_rb_pos;
  164. String last_error;
  165. bool error_flag;
  166. void _advance();
  167. public:
  168. void set_code(const String& p_code);
  169. virtual Token get_token(int p_offset=0) const;
  170. virtual StringName get_token_identifier(int p_offset=0) const;
  171. virtual GDFunctions::Function get_token_built_in_func(int p_offset=0) const;
  172. virtual Variant::Type get_token_type(int p_offset=0) const;
  173. virtual int get_token_line(int p_offset=0) const;
  174. virtual int get_token_column(int p_offset=0) const;
  175. virtual int get_token_line_indent(int p_offset=0) const;
  176. virtual const Variant& get_token_constant(int p_offset=0) const;
  177. virtual String get_token_error(int p_offset=0) const;
  178. virtual void advance(int p_amount=1);
  179. };
  180. class GDTokenizerBuffer : public GDTokenizer {
  181. enum {
  182. TOKEN_BYTE_MASK=0x80,
  183. TOKEN_BITS=8,
  184. TOKEN_MASK=(1<<TOKEN_BITS)-1,
  185. TOKEN_LINE_BITS=24,
  186. TOKEN_LINE_MASK=(1<<TOKEN_LINE_BITS)-1,
  187. };
  188. Vector<StringName> identifiers;
  189. Vector<Variant> constants;
  190. VMap<uint32_t,uint32_t> lines;
  191. Vector<uint32_t> tokens;
  192. Variant nil;
  193. int token;
  194. public:
  195. Error set_code_buffer(const Vector<uint8_t> & p_buffer);
  196. static Vector<uint8_t> parse_code_string(const String& p_code);
  197. virtual Token get_token(int p_offset=0) const;
  198. virtual StringName get_token_identifier(int p_offset=0) const;
  199. virtual GDFunctions::Function get_token_built_in_func(int p_offset=0) const;
  200. virtual Variant::Type get_token_type(int p_offset=0) const;
  201. virtual int get_token_line(int p_offset=0) const;
  202. virtual int get_token_column(int p_offset=0) const;
  203. virtual int get_token_line_indent(int p_offset=0) const;
  204. virtual const Variant& get_token_constant(int p_offset=0) const;
  205. virtual String get_token_error(int p_offset=0) const;
  206. virtual void advance(int p_amount=1);
  207. GDTokenizerBuffer();
  208. };
  209. #endif // TOKENIZER_H