gdscript_tokenizer.h 8.7 KB

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