gdscript_tokenizer.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 is_identifier() const;
  169. bool is_node_name() const;
  170. StringName get_identifier() const { return source; }
  171. Token(Type p_type) {
  172. type = p_type;
  173. }
  174. Token() {
  175. }
  176. };
  177. #ifdef TOOLS_ENABLED
  178. struct CommentData {
  179. String comment;
  180. bool new_line = false;
  181. CommentData() {}
  182. CommentData(const String &p_comment, bool p_new_line) {
  183. comment = p_comment;
  184. new_line = p_new_line;
  185. }
  186. };
  187. const HashMap<int, CommentData> &get_comments() const {
  188. return comments;
  189. }
  190. #endif // TOOLS_ENABLED
  191. private:
  192. String source;
  193. const char32_t *_source = nullptr;
  194. const char32_t *_current = nullptr;
  195. int line = -1, column = -1;
  196. int cursor_line = -1, cursor_column = -1;
  197. int tab_size = 4;
  198. // Keep track of multichar tokens.
  199. const char32_t *_start = nullptr;
  200. int start_line = 0, start_column = 0;
  201. int leftmost_column = 0, rightmost_column = 0;
  202. // Info cache.
  203. bool line_continuation = false; // Whether this line is a continuation of the previous, like when using '\'.
  204. bool multiline_mode = false;
  205. List<Token> error_stack;
  206. bool pending_newline = false;
  207. Token last_newline;
  208. int pending_indents = 0;
  209. List<int> indent_stack;
  210. List<List<int>> indent_stack_stack; // For lambdas, which require manipulating the indentation point.
  211. List<char32_t> paren_stack;
  212. char32_t indent_char = '\0';
  213. int position = 0;
  214. int length = 0;
  215. #ifdef DEBUG_ENABLED
  216. Vector<String> keyword_list;
  217. #endif // DEBUG_ENABLED
  218. #ifdef TOOLS_ENABLED
  219. HashMap<int, CommentData> comments;
  220. #endif // TOOLS_ENABLED
  221. _FORCE_INLINE_ bool _is_at_end() { return position >= length; }
  222. _FORCE_INLINE_ char32_t _peek(int p_offset = 0) { return position + p_offset >= 0 && position + p_offset < length ? _current[p_offset] : '\0'; }
  223. int indent_level() const { return indent_stack.size(); }
  224. bool has_error() const { return !error_stack.is_empty(); }
  225. Token pop_error();
  226. char32_t _advance();
  227. String _get_indent_char_name(char32_t ch);
  228. void _skip_whitespace();
  229. void check_indent();
  230. #ifdef DEBUG_ENABLED
  231. void make_keyword_list();
  232. #endif // DEBUG_ENABLED
  233. Token make_error(const String &p_message);
  234. void push_error(const String &p_message);
  235. void push_error(const Token &p_error);
  236. Token make_paren_error(char32_t p_paren);
  237. Token make_token(Token::Type p_type);
  238. Token make_literal(const Variant &p_literal);
  239. Token make_identifier(const StringName &p_identifier);
  240. Token check_vcs_marker(char32_t p_test, Token::Type p_double_type);
  241. void push_paren(char32_t p_char);
  242. bool pop_paren(char32_t p_expected);
  243. void newline(bool p_make_token);
  244. Token number();
  245. Token potential_identifier();
  246. Token string();
  247. Token annotation();
  248. public:
  249. Token scan();
  250. void set_source_code(const String &p_source_code);
  251. int get_cursor_line() const;
  252. int get_cursor_column() const;
  253. void set_cursor_position(int p_line, int p_column);
  254. void set_multiline_mode(bool p_state);
  255. bool is_past_cursor() const;
  256. static String get_token_name(Token::Type p_token_type);
  257. void push_expression_indented_block(); // For lambdas, or blocks inside expressions.
  258. void pop_expression_indented_block(); // For lambdas, or blocks inside expressions.
  259. GDScriptTokenizer();
  260. };
  261. #endif // GDSCRIPT_TOKENIZER_H