gd_tokenizer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. class GDTokenizer {
  36. public:
  37. enum Token {
  38. TK_EMPTY,
  39. TK_IDENTIFIER,
  40. TK_CONSTANT,
  41. TK_SELF,
  42. TK_BUILT_IN_TYPE,
  43. TK_BUILT_IN_FUNC,
  44. TK_OP_IN,
  45. TK_OP_EQUAL,
  46. TK_OP_NOT_EQUAL,
  47. TK_OP_LESS,
  48. TK_OP_LESS_EQUAL,
  49. TK_OP_GREATER,
  50. TK_OP_GREATER_EQUAL,
  51. TK_OP_AND,
  52. TK_OP_OR,
  53. TK_OP_NOT,
  54. TK_OP_ADD,
  55. TK_OP_SUB,
  56. TK_OP_MUL,
  57. TK_OP_DIV,
  58. TK_OP_MOD,
  59. TK_OP_SHIFT_LEFT,
  60. TK_OP_SHIFT_RIGHT,
  61. TK_OP_ASSIGN,
  62. TK_OP_ASSIGN_ADD,
  63. TK_OP_ASSIGN_SUB,
  64. TK_OP_ASSIGN_MUL,
  65. TK_OP_ASSIGN_DIV,
  66. TK_OP_ASSIGN_MOD,
  67. TK_OP_ASSIGN_SHIFT_LEFT,
  68. TK_OP_ASSIGN_SHIFT_RIGHT,
  69. TK_OP_ASSIGN_BIT_AND,
  70. TK_OP_ASSIGN_BIT_OR,
  71. TK_OP_ASSIGN_BIT_XOR,
  72. TK_OP_BIT_AND,
  73. TK_OP_BIT_OR,
  74. TK_OP_BIT_XOR,
  75. TK_OP_BIT_INVERT,
  76. //TK_OP_PLUS_PLUS,
  77. //TK_OP_MINUS_MINUS,
  78. TK_CF_IF,
  79. TK_CF_ELIF,
  80. TK_CF_ELSE,
  81. TK_CF_FOR,
  82. TK_CF_DO,
  83. TK_CF_WHILE,
  84. TK_CF_SWITCH,
  85. TK_CF_CASE,
  86. TK_CF_BREAK,
  87. TK_CF_CONTINUE,
  88. TK_CF_PASS,
  89. TK_CF_RETURN,
  90. TK_PR_FUNCTION,
  91. TK_PR_CLASS,
  92. TK_PR_EXTENDS,
  93. TK_PR_TOOL,
  94. TK_PR_STATIC,
  95. TK_PR_EXPORT,
  96. TK_PR_CONST,
  97. TK_PR_VAR,
  98. TK_PR_PRELOAD,
  99. TK_PR_ASSERT,
  100. TK_BRACKET_OPEN,
  101. TK_BRACKET_CLOSE,
  102. TK_CURLY_BRACKET_OPEN,
  103. TK_CURLY_BRACKET_CLOSE,
  104. TK_PARENTHESIS_OPEN,
  105. TK_PARENTHESIS_CLOSE,
  106. TK_COMMA,
  107. TK_SEMICOLON,
  108. TK_PERIOD,
  109. TK_QUESTION_MARK,
  110. TK_COLON,
  111. TK_NEWLINE,
  112. TK_ERROR,
  113. TK_EOF,
  114. TK_MAX
  115. };
  116. private:
  117. static const char* token_names[TK_MAX];
  118. enum {
  119. MAX_LOOKAHEAD=4,
  120. TK_RB_SIZE=MAX_LOOKAHEAD*2+1
  121. };
  122. struct TokenData {
  123. Token type;
  124. StringName identifier; //for identifier types
  125. Variant constant; //for constant types
  126. union {
  127. Variant::Type vtype; //for type types
  128. GDFunctions::Function func; //function for built in functions
  129. };
  130. int line,col;
  131. TokenData() { type = TK_EMPTY; line=col=0; vtype=Variant::NIL; }
  132. };
  133. void _make_token(Token p_type);
  134. void _make_newline(int p_spaces=0);
  135. void _make_identifier(const StringName& p_identifier);
  136. void _make_built_in_func(GDFunctions::Function p_func);
  137. void _make_constant(const Variant& p_constant);
  138. void _make_type(const Variant::Type& p_type);
  139. void _make_error(const String& p_error);
  140. String code;
  141. int len;
  142. int code_pos;
  143. const CharType *_code;
  144. int line;
  145. int column;
  146. TokenData tk_rb[TK_RB_SIZE*2+1];
  147. int tk_rb_pos;
  148. String last_error;
  149. bool error_flag;
  150. void _advance();
  151. public:
  152. static const char *get_token_name(Token p_token);
  153. void set_code(const String& p_code);
  154. Token get_token(int p_offset=0) const;
  155. const Variant& get_token_constant(int p_offset=0) const;
  156. StringName get_token_identifier(int p_offset=0) const;
  157. GDFunctions::Function get_token_built_in_func(int p_offset=0) const;
  158. Variant::Type get_token_type(int p_offset=0) const;
  159. int get_token_line(int p_offset=0) const;
  160. int get_token_column(int p_offset=0) const;
  161. int get_token_line_indent(int p_offset=0) const;
  162. String get_token_error(int p_offset=0) const;
  163. void advance(int p_amount=1);
  164. };
  165. #endif // TOKENIZER_H