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