gdscript_tokenizer.h 8.6 KB

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