gd_tokenizer.h 7.1 KB

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