gdscript_tokenizer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**************************************************************************/
  2. /* gdscript_tokenizer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/templates/hash_map.h"
  33. #include "core/templates/hash_set.h"
  34. #include "core/templates/list.h"
  35. #include "core/templates/vector.h"
  36. #include "core/variant/variant.h"
  37. class GDScriptTokenizer {
  38. public:
  39. enum CursorPlace {
  40. CURSOR_NONE,
  41. CURSOR_BEGINNING,
  42. CURSOR_MIDDLE,
  43. CURSOR_END,
  44. };
  45. struct Token {
  46. enum Type {
  47. EMPTY,
  48. // Basic
  49. ANNOTATION,
  50. IDENTIFIER,
  51. LITERAL,
  52. // Comparison
  53. LESS,
  54. LESS_EQUAL,
  55. GREATER,
  56. GREATER_EQUAL,
  57. EQUAL_EQUAL,
  58. BANG_EQUAL,
  59. // Logical
  60. AND,
  61. OR,
  62. NOT,
  63. AMPERSAND_AMPERSAND,
  64. PIPE_PIPE,
  65. BANG,
  66. // Bitwise
  67. AMPERSAND,
  68. PIPE,
  69. TILDE,
  70. CARET,
  71. LESS_LESS,
  72. GREATER_GREATER,
  73. // Math
  74. PLUS,
  75. MINUS,
  76. STAR,
  77. STAR_STAR,
  78. SLASH,
  79. PERCENT,
  80. // Assignment
  81. EQUAL,
  82. PLUS_EQUAL,
  83. MINUS_EQUAL,
  84. STAR_EQUAL,
  85. STAR_STAR_EQUAL,
  86. SLASH_EQUAL,
  87. PERCENT_EQUAL,
  88. LESS_LESS_EQUAL,
  89. GREATER_GREATER_EQUAL,
  90. AMPERSAND_EQUAL,
  91. PIPE_EQUAL,
  92. CARET_EQUAL,
  93. // Control flow
  94. IF,
  95. ELIF,
  96. ELSE,
  97. FOR,
  98. WHILE,
  99. BREAK,
  100. CONTINUE,
  101. PASS,
  102. RETURN,
  103. MATCH,
  104. // Keywords
  105. AS,
  106. ASSERT,
  107. AWAIT,
  108. BREAKPOINT,
  109. CLASS,
  110. CLASS_NAME,
  111. CONST,
  112. ENUM,
  113. EXTENDS,
  114. FUNC,
  115. IN,
  116. IS,
  117. NAMESPACE,
  118. PRELOAD,
  119. SELF,
  120. SIGNAL,
  121. STATIC,
  122. SUPER,
  123. TRAIT,
  124. VAR,
  125. VOID,
  126. YIELD,
  127. // Punctuation
  128. BRACKET_OPEN,
  129. BRACKET_CLOSE,
  130. BRACE_OPEN,
  131. BRACE_CLOSE,
  132. PARENTHESIS_OPEN,
  133. PARENTHESIS_CLOSE,
  134. COMMA,
  135. SEMICOLON,
  136. PERIOD,
  137. PERIOD_PERIOD,
  138. COLON,
  139. DOLLAR,
  140. FORWARD_ARROW,
  141. UNDERSCORE,
  142. // Whitespace
  143. NEWLINE,
  144. INDENT,
  145. DEDENT,
  146. // Constants
  147. CONST_PI,
  148. CONST_TAU,
  149. CONST_INF,
  150. CONST_NAN,
  151. // Error message improvement
  152. VCS_CONFLICT_MARKER,
  153. BACKTICK,
  154. QUESTION_MARK,
  155. // Special
  156. ERROR,
  157. TK_EOF, // "EOF" is reserved
  158. TK_MAX
  159. };
  160. Type type = EMPTY;
  161. Variant literal;
  162. int start_line = 0, end_line = 0, start_column = 0, end_column = 0;
  163. int leftmost_column = 0, rightmost_column = 0; // Column span for multiline tokens.
  164. int cursor_position = -1;
  165. CursorPlace cursor_place = CURSOR_NONE;
  166. String source;
  167. const char *get_name() const;
  168. bool can_precede_bin_op() const;
  169. bool is_identifier() const;
  170. bool is_node_name() const;
  171. StringName get_identifier() const { return source; }
  172. Token(Type p_type) {
  173. type = p_type;
  174. }
  175. Token() {
  176. }
  177. };
  178. #ifdef TOOLS_ENABLED
  179. struct CommentData {
  180. String comment;
  181. // true: Comment starts at beginning of line or after indentation.
  182. // false: Inline comment (starts after some code).
  183. bool new_line = false;
  184. CommentData() {}
  185. CommentData(const String &p_comment, bool p_new_line) {
  186. comment = p_comment;
  187. new_line = p_new_line;
  188. }
  189. };
  190. const HashMap<int, CommentData> &get_comments() const {
  191. return comments;
  192. }
  193. #endif // TOOLS_ENABLED
  194. private:
  195. String source;
  196. const char32_t *_source = nullptr;
  197. const char32_t *_current = nullptr;
  198. int line = -1, column = -1;
  199. int cursor_line = -1, cursor_column = -1;
  200. int tab_size = 4;
  201. // Keep track of multichar tokens.
  202. const char32_t *_start = nullptr;
  203. int start_line = 0, start_column = 0;
  204. int leftmost_column = 0, rightmost_column = 0;
  205. // Info cache.
  206. bool line_continuation = false; // Whether this line is a continuation of the previous, like when using '\'.
  207. bool multiline_mode = false;
  208. List<Token> error_stack;
  209. bool pending_newline = false;
  210. Token last_token;
  211. Token last_newline;
  212. int pending_indents = 0;
  213. List<int> indent_stack;
  214. List<List<int>> indent_stack_stack; // For lambdas, which require manipulating the indentation point.
  215. List<char32_t> paren_stack;
  216. char32_t indent_char = '\0';
  217. int position = 0;
  218. int length = 0;
  219. #ifdef DEBUG_ENABLED
  220. Vector<String> keyword_list;
  221. #endif // DEBUG_ENABLED
  222. #ifdef TOOLS_ENABLED
  223. HashMap<int, CommentData> comments;
  224. #endif // TOOLS_ENABLED
  225. _FORCE_INLINE_ bool _is_at_end() { return position >= length; }
  226. _FORCE_INLINE_ char32_t _peek(int p_offset = 0) { return position + p_offset >= 0 && position + p_offset < length ? _current[p_offset] : '\0'; }
  227. int indent_level() const { return indent_stack.size(); }
  228. bool has_error() const { return !error_stack.is_empty(); }
  229. Token pop_error();
  230. char32_t _advance();
  231. String _get_indent_char_name(char32_t ch);
  232. void _skip_whitespace();
  233. void check_indent();
  234. #ifdef DEBUG_ENABLED
  235. void make_keyword_list();
  236. #endif // DEBUG_ENABLED
  237. Token make_error(const String &p_message);
  238. void push_error(const String &p_message);
  239. void push_error(const Token &p_error);
  240. Token make_paren_error(char32_t p_paren);
  241. Token make_token(Token::Type p_type);
  242. Token make_literal(const Variant &p_literal);
  243. Token make_identifier(const StringName &p_identifier);
  244. Token check_vcs_marker(char32_t p_test, Token::Type p_double_type);
  245. void push_paren(char32_t p_char);
  246. bool pop_paren(char32_t p_expected);
  247. void newline(bool p_make_token);
  248. Token number();
  249. Token potential_identifier();
  250. Token string();
  251. Token annotation();
  252. public:
  253. Token scan();
  254. void set_source_code(const String &p_source_code);
  255. int get_cursor_line() const;
  256. int get_cursor_column() const;
  257. void set_cursor_position(int p_line, int p_column);
  258. void set_multiline_mode(bool p_state);
  259. bool is_past_cursor() const;
  260. static String get_token_name(Token::Type p_token_type);
  261. void push_expression_indented_block(); // For lambdas, or blocks inside expressions.
  262. void pop_expression_indented_block(); // For lambdas, or blocks inside expressions.
  263. GDScriptTokenizer();
  264. };
  265. #endif // GDSCRIPT_TOKENIZER_H