gdscript_tokenizer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*************************************************************************/
  2. /* gdscript_tokenizer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/list.h"
  33. #include "core/templates/map.h"
  34. #include "core/templates/set.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. SLASH,
  78. PERCENT,
  79. // Assignment
  80. EQUAL,
  81. PLUS_EQUAL,
  82. MINUS_EQUAL,
  83. 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. // Keywords
  103. AS,
  104. ASSERT,
  105. AWAIT,
  106. BREAKPOINT,
  107. CLASS,
  108. CLASS_NAME,
  109. CONST,
  110. ENUM,
  111. EXTENDS,
  112. FUNC,
  113. IN,
  114. IS,
  115. NAMESPACE,
  116. PRELOAD,
  117. SELF,
  118. SIGNAL,
  119. STATIC,
  120. SUPER,
  121. TRAIT,
  122. VAR,
  123. VOID,
  124. YIELD,
  125. // Punctuation
  126. BRACKET_OPEN,
  127. BRACKET_CLOSE,
  128. BRACE_OPEN,
  129. BRACE_CLOSE,
  130. PARENTHESIS_OPEN,
  131. PARENTHESIS_CLOSE,
  132. COMMA,
  133. SEMICOLON,
  134. PERIOD,
  135. PERIOD_PERIOD,
  136. COLON,
  137. DOLLAR,
  138. FORWARD_ARROW,
  139. UNDERSCORE,
  140. // Whitespace
  141. NEWLINE,
  142. INDENT,
  143. DEDENT,
  144. // Constants
  145. CONST_PI,
  146. CONST_TAU,
  147. CONST_INF,
  148. CONST_NAN,
  149. // Error message improvement
  150. VCS_CONFLICT_MARKER,
  151. BACKTICK,
  152. QUESTION_MARK,
  153. // Special
  154. ERROR,
  155. TK_EOF, // "EOF" is reserved
  156. TK_MAX
  157. };
  158. Type type = EMPTY;
  159. Variant literal;
  160. int start_line = 0, end_line = 0, start_column = 0, end_column = 0;
  161. int leftmost_column = 0, rightmost_column = 0; // Column span for multiline tokens.
  162. int cursor_position = -1;
  163. CursorPlace cursor_place = CURSOR_NONE;
  164. String source;
  165. const char *get_name() const;
  166. bool is_identifier() const;
  167. bool is_node_name() const;
  168. StringName get_identifier() const { return source; }
  169. Token(Type p_type) {
  170. type = p_type;
  171. }
  172. Token() {
  173. }
  174. };
  175. #ifdef TOOLS_ENABLED
  176. struct CommentData {
  177. String comment;
  178. bool new_line = false;
  179. CommentData() {}
  180. CommentData(const String &p_comment, bool p_new_line) {
  181. comment = p_comment;
  182. new_line = p_new_line;
  183. }
  184. };
  185. const Map<int, CommentData> &get_comments() const {
  186. return comments;
  187. }
  188. #endif // TOOLS_ENABLED
  189. private:
  190. String source;
  191. const char32_t *_source = nullptr;
  192. const char32_t *_current = nullptr;
  193. int line = -1, column = -1;
  194. int cursor_line = -1, cursor_column = -1;
  195. int tab_size = 4;
  196. // Keep track of multichar tokens.
  197. const char32_t *_start = nullptr;
  198. int start_line = 0, start_column = 0;
  199. int leftmost_column = 0, rightmost_column = 0;
  200. // Info cache.
  201. bool line_continuation = false; // Whether this line is a continuation of the previous, like when using '\'.
  202. bool multiline_mode = false;
  203. List<Token> error_stack;
  204. bool pending_newline = false;
  205. Token last_newline;
  206. int pending_indents = 0;
  207. List<int> indent_stack;
  208. List<List<int>> indent_stack_stack; // For lambdas, which require manipulating the indentation point.
  209. List<char32_t> paren_stack;
  210. char32_t indent_char = '\0';
  211. int position = 0;
  212. int length = 0;
  213. #ifdef TOOLS_ENABLED
  214. Map<int, CommentData> comments;
  215. #endif // TOOLS_ENABLED
  216. _FORCE_INLINE_ bool _is_at_end() { return position >= length; }
  217. _FORCE_INLINE_ char32_t _peek(int p_offset = 0) { return position + p_offset >= 0 && position + p_offset < length ? _current[p_offset] : '\0'; }
  218. int indent_level() const { return indent_stack.size(); }
  219. bool has_error() const { return !error_stack.is_empty(); }
  220. Token pop_error();
  221. char32_t _advance();
  222. void _skip_whitespace();
  223. void check_indent();
  224. Token make_error(const String &p_message);
  225. void push_error(const String &p_message);
  226. void push_error(const Token &p_error);
  227. Token make_paren_error(char32_t p_paren);
  228. Token make_token(Token::Type p_type);
  229. Token make_literal(const Variant &p_literal);
  230. Token make_identifier(const StringName &p_identifier);
  231. Token check_vcs_marker(char32_t p_test, Token::Type p_double_type);
  232. void push_paren(char32_t p_char);
  233. bool pop_paren(char32_t p_expected);
  234. void newline(bool p_make_token);
  235. Token number();
  236. Token potential_identifier();
  237. Token string();
  238. Token annotation();
  239. public:
  240. Token scan();
  241. void set_source_code(const String &p_source_code);
  242. int get_cursor_line() const;
  243. int get_cursor_column() const;
  244. void set_cursor_position(int p_line, int p_column);
  245. void set_multiline_mode(bool p_state);
  246. bool is_past_cursor() const;
  247. static String get_token_name(Token::Type p_token_type);
  248. void push_expression_indented_block(); // For lambdas, or blocks inside expressions.
  249. void pop_expression_indented_block(); // For lambdas, or blocks inside expressions.
  250. GDScriptTokenizer();
  251. };
  252. #endif