gdscript_tokenizer.h 8.6 KB

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