gdscript_tokenizer.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*************************************************************************/
  2. /* gdscript_tokenizer.cpp */
  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. #include "gdscript_tokenizer.h"
  31. #include "core/error/error_macros.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_settings.h"
  34. #endif
  35. static const char *token_names[] = {
  36. "Empty", // EMPTY,
  37. // Basic
  38. "Annotation", // ANNOTATION
  39. "Identifier", // IDENTIFIER,
  40. "Literal", // LITERAL,
  41. // Comparison
  42. "<", // LESS,
  43. "<=", // LESS_EQUAL,
  44. ">", // GREATER,
  45. ">=", // GREATER_EQUAL,
  46. "==", // EQUAL_EQUAL,
  47. "!=", // BANG_EQUAL,
  48. // Logical
  49. "and", // AND,
  50. "or", // OR,
  51. "not", // NOT,
  52. "&&", // AMPERSAND_AMPERSAND,
  53. "||", // PIPE_PIPE,
  54. "!", // BANG,
  55. // Bitwise
  56. "&", // AMPERSAND,
  57. "|", // PIPE,
  58. "~", // TILDE,
  59. "^", // CARET,
  60. "<<", // LESS_LESS,
  61. ">>", // GREATER_GREATER,
  62. // Math
  63. "+", // PLUS,
  64. "-", // MINUS,
  65. "*", // STAR,
  66. "/", // SLASH,
  67. "%", // PERCENT,
  68. // Assignment
  69. "=", // EQUAL,
  70. "+=", // PLUS_EQUAL,
  71. "-=", // MINUS_EQUAL,
  72. "*=", // STAR_EQUAL,
  73. "/=", // SLASH_EQUAL,
  74. "%=", // PERCENT_EQUAL,
  75. "<<=", // LESS_LESS_EQUAL,
  76. ">>=", // GREATER_GREATER_EQUAL,
  77. "&=", // AMPERSAND_EQUAL,
  78. "|=", // PIPE_EQUAL,
  79. "^=", // CARET_EQUAL,
  80. // Control flow
  81. "if", // IF,
  82. "elif", // ELIF,
  83. "else", // ELSE,
  84. "for", // FOR,
  85. "while", // WHILE,
  86. "break", // BREAK,
  87. "continue", // CONTINUE,
  88. "pass", // PASS,
  89. "return", // RETURN,
  90. "match", // MATCH,
  91. // Keywords
  92. "as", // AS,
  93. "assert", // ASSERT,
  94. "await", // AWAIT,
  95. "breakpoint", // BREAKPOINT,
  96. "class", // CLASS,
  97. "class_name", // CLASS_NAME,
  98. "const", // CONST,
  99. "enum", // ENUM,
  100. "extends", // EXTENDS,
  101. "func", // FUNC,
  102. "in", // IN,
  103. "is", // IS,
  104. "namespace", // NAMESPACE
  105. "preload", // PRELOAD,
  106. "self", // SELF,
  107. "signal", // SIGNAL,
  108. "static", // STATIC,
  109. "super", // SUPER,
  110. "trait", // TRAIT,
  111. "var", // VAR,
  112. "void", // VOID,
  113. "yield", // YIELD,
  114. // Punctuation
  115. "[", // BRACKET_OPEN,
  116. "]", // BRACKET_CLOSE,
  117. "{", // BRACE_OPEN,
  118. "}", // BRACE_CLOSE,
  119. "(", // PARENTHESIS_OPEN,
  120. ")", // PARENTHESIS_CLOSE,
  121. ",", // COMMA,
  122. ";", // SEMICOLON,
  123. ".", // PERIOD,
  124. "..", // PERIOD_PERIOD,
  125. ":", // COLON,
  126. "$", // DOLLAR,
  127. "->", // FORWARD_ARROW,
  128. "_", // UNDERSCORE,
  129. // Whitespace
  130. "Newline", // NEWLINE,
  131. "Indent", // INDENT,
  132. "Dedent", // DEDENT,
  133. // Constants
  134. "PI", // CONST_PI,
  135. "TAU", // CONST_TAU,
  136. "INF", // CONST_INF,
  137. "NaN", // CONST_NAN,
  138. // Error message improvement
  139. "VCS conflict marker", // VCS_CONFLICT_MARKER,
  140. "`", // BACKTICK,
  141. "?", // QUESTION_MARK,
  142. // Special
  143. "Error", // ERROR,
  144. "End of file", // EOF,
  145. };
  146. // Avoid desync.
  147. static_assert(sizeof(token_names) / sizeof(token_names[0]) == GDScriptTokenizer::Token::TK_MAX, "Amount of token names don't match the amount of token types.");
  148. const char *GDScriptTokenizer::Token::get_name() const {
  149. ERR_FAIL_INDEX_V_MSG(type, TK_MAX, "<error>", "Using token type out of the enum.");
  150. return token_names[type];
  151. }
  152. bool GDScriptTokenizer::Token::is_identifier() const {
  153. // Note: Most keywords should not be recognized as identifiers.
  154. // These are only exceptions for stuff that already is on the engine's API.
  155. switch (type) {
  156. case IDENTIFIER:
  157. case MATCH: // Used in String.match().
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. bool GDScriptTokenizer::Token::is_node_name() const {
  164. // This is meant to allow keywords with the $ notation, but not as general identifiers.
  165. switch (type) {
  166. case IDENTIFIER:
  167. case AND:
  168. case AS:
  169. case ASSERT:
  170. case AWAIT:
  171. case BREAK:
  172. case BREAKPOINT:
  173. case CLASS_NAME:
  174. case CLASS:
  175. case CONST:
  176. case CONTINUE:
  177. case ELIF:
  178. case ELSE:
  179. case ENUM:
  180. case EXTENDS:
  181. case FOR:
  182. case FUNC:
  183. case IF:
  184. case IN:
  185. case IS:
  186. case MATCH:
  187. case NAMESPACE:
  188. case NOT:
  189. case OR:
  190. case PASS:
  191. case PRELOAD:
  192. case RETURN:
  193. case SELF:
  194. case SIGNAL:
  195. case STATIC:
  196. case SUPER:
  197. case TRAIT:
  198. case UNDERSCORE:
  199. case VAR:
  200. case VOID:
  201. case WHILE:
  202. case YIELD:
  203. return true;
  204. default:
  205. return false;
  206. }
  207. }
  208. String GDScriptTokenizer::get_token_name(Token::Type p_token_type) {
  209. ERR_FAIL_INDEX_V_MSG(p_token_type, Token::TK_MAX, "<error>", "Using token type out of the enum.");
  210. return token_names[p_token_type];
  211. }
  212. void GDScriptTokenizer::set_source_code(const String &p_source_code) {
  213. source = p_source_code;
  214. if (source.is_empty()) {
  215. _source = U"";
  216. } else {
  217. _source = source.ptr();
  218. }
  219. _current = _source;
  220. line = 1;
  221. column = 1;
  222. length = p_source_code.length();
  223. position = 0;
  224. }
  225. void GDScriptTokenizer::set_cursor_position(int p_line, int p_column) {
  226. cursor_line = p_line;
  227. cursor_column = p_column;
  228. }
  229. void GDScriptTokenizer::set_multiline_mode(bool p_state) {
  230. multiline_mode = p_state;
  231. }
  232. void GDScriptTokenizer::push_expression_indented_block() {
  233. indent_stack_stack.push_back(indent_stack);
  234. }
  235. void GDScriptTokenizer::pop_expression_indented_block() {
  236. ERR_FAIL_COND(indent_stack_stack.size() == 0);
  237. indent_stack = indent_stack_stack.back()->get();
  238. indent_stack_stack.pop_back();
  239. }
  240. int GDScriptTokenizer::get_cursor_line() const {
  241. return cursor_line;
  242. }
  243. int GDScriptTokenizer::get_cursor_column() const {
  244. return cursor_column;
  245. }
  246. bool GDScriptTokenizer::is_past_cursor() const {
  247. if (line < cursor_line) {
  248. return false;
  249. }
  250. if (line > cursor_line) {
  251. return true;
  252. }
  253. if (column < cursor_column) {
  254. return false;
  255. }
  256. return true;
  257. }
  258. char32_t GDScriptTokenizer::_advance() {
  259. if (unlikely(_is_at_end())) {
  260. return '\0';
  261. }
  262. _current++;
  263. column++;
  264. position++;
  265. if (column > rightmost_column) {
  266. rightmost_column = column;
  267. }
  268. if (unlikely(_is_at_end())) {
  269. // Add extra newline even if it's not there, to satisfy the parser.
  270. newline(true);
  271. // Also add needed unindent.
  272. check_indent();
  273. }
  274. return _peek(-1);
  275. }
  276. void GDScriptTokenizer::push_paren(char32_t p_char) {
  277. paren_stack.push_back(p_char);
  278. }
  279. bool GDScriptTokenizer::pop_paren(char32_t p_expected) {
  280. if (paren_stack.is_empty()) {
  281. return false;
  282. }
  283. char32_t actual = paren_stack.back()->get();
  284. paren_stack.pop_back();
  285. return actual == p_expected;
  286. }
  287. GDScriptTokenizer::Token GDScriptTokenizer::pop_error() {
  288. Token error = error_stack.back()->get();
  289. error_stack.pop_back();
  290. return error;
  291. }
  292. static bool _is_alphanumeric(char32_t c) {
  293. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  294. }
  295. static bool _is_digit(char32_t c) {
  296. return (c >= '0' && c <= '9');
  297. }
  298. static bool _is_hex_digit(char32_t c) {
  299. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  300. }
  301. static bool _is_binary_digit(char32_t c) {
  302. return (c == '0' || c == '1');
  303. }
  304. GDScriptTokenizer::Token GDScriptTokenizer::make_token(Token::Type p_type) {
  305. Token token(p_type);
  306. token.start_line = start_line;
  307. token.end_line = line;
  308. token.start_column = start_column;
  309. token.end_column = column;
  310. token.leftmost_column = leftmost_column;
  311. token.rightmost_column = rightmost_column;
  312. token.source = String(_start, _current - _start);
  313. if (p_type != Token::ERROR && cursor_line > -1) {
  314. // Also count whitespace after token.
  315. int offset = 0;
  316. while (_peek(offset) == ' ' || _peek(offset) == '\t') {
  317. offset++;
  318. }
  319. int last_column = column + offset;
  320. // Check cursor position in token.
  321. if (start_line == line) {
  322. // Single line token.
  323. if (cursor_line == start_line && cursor_column >= start_column && cursor_column <= last_column) {
  324. token.cursor_position = cursor_column - start_column;
  325. if (cursor_column == start_column) {
  326. token.cursor_place = CURSOR_BEGINNING;
  327. } else if (cursor_column < column) {
  328. token.cursor_place = CURSOR_MIDDLE;
  329. } else {
  330. token.cursor_place = CURSOR_END;
  331. }
  332. }
  333. } else {
  334. // Multi line token.
  335. if (cursor_line == start_line && cursor_column >= start_column) {
  336. // Is in first line.
  337. token.cursor_position = cursor_column - start_column;
  338. if (cursor_column == start_column) {
  339. token.cursor_place = CURSOR_BEGINNING;
  340. } else {
  341. token.cursor_place = CURSOR_MIDDLE;
  342. }
  343. } else if (cursor_line == line && cursor_column <= last_column) {
  344. // Is in last line.
  345. token.cursor_position = cursor_column - start_column;
  346. if (cursor_column < column) {
  347. token.cursor_place = CURSOR_MIDDLE;
  348. } else {
  349. token.cursor_place = CURSOR_END;
  350. }
  351. } else if (cursor_line > start_line && cursor_line < line) {
  352. // Is in middle line.
  353. token.cursor_position = CURSOR_MIDDLE;
  354. }
  355. }
  356. }
  357. return token;
  358. }
  359. GDScriptTokenizer::Token GDScriptTokenizer::make_literal(const Variant &p_literal) {
  360. Token token = make_token(Token::LITERAL);
  361. token.literal = p_literal;
  362. return token;
  363. }
  364. GDScriptTokenizer::Token GDScriptTokenizer::make_identifier(const StringName &p_identifier) {
  365. Token identifier = make_token(Token::IDENTIFIER);
  366. identifier.literal = p_identifier;
  367. return identifier;
  368. }
  369. GDScriptTokenizer::Token GDScriptTokenizer::make_error(const String &p_message) {
  370. Token error = make_token(Token::ERROR);
  371. error.literal = p_message;
  372. return error;
  373. }
  374. void GDScriptTokenizer::push_error(const String &p_message) {
  375. Token error = make_error(p_message);
  376. error_stack.push_back(error);
  377. }
  378. void GDScriptTokenizer::push_error(const Token &p_error) {
  379. error_stack.push_back(p_error);
  380. }
  381. GDScriptTokenizer::Token GDScriptTokenizer::make_paren_error(char32_t p_paren) {
  382. if (paren_stack.is_empty()) {
  383. return make_error(vformat("Closing \"%c\" doesn't have an opening counterpart.", p_paren));
  384. }
  385. Token error = make_error(vformat("Closing \"%c\" doesn't match the opening \"%c\".", p_paren, paren_stack.back()->get()));
  386. paren_stack.pop_back(); // Remove opening one anyway.
  387. return error;
  388. }
  389. GDScriptTokenizer::Token GDScriptTokenizer::check_vcs_marker(char32_t p_test, Token::Type p_double_type) {
  390. const char32_t *next = _current + 1;
  391. int chars = 2; // Two already matched.
  392. // Test before consuming characters, since we don't want to consume more than needed.
  393. while (*next == p_test) {
  394. chars++;
  395. next++;
  396. }
  397. if (chars >= 7) {
  398. // It is a VCS conflict marker.
  399. while (chars > 1) {
  400. // Consume all characters (first was already consumed by scan()).
  401. _advance();
  402. chars--;
  403. }
  404. return make_token(Token::VCS_CONFLICT_MARKER);
  405. } else {
  406. // It is only a regular double character token, so we consume the second character.
  407. _advance();
  408. return make_token(p_double_type);
  409. }
  410. }
  411. GDScriptTokenizer::Token GDScriptTokenizer::annotation() {
  412. if (!_is_alphanumeric(_peek())) {
  413. push_error("Expected annotation identifier after \"@\".");
  414. }
  415. while (_is_alphanumeric(_peek())) {
  416. // Consume all identifier characters.
  417. _advance();
  418. }
  419. Token annotation = make_token(Token::ANNOTATION);
  420. annotation.literal = StringName(annotation.source);
  421. return annotation;
  422. }
  423. GDScriptTokenizer::Token GDScriptTokenizer::potential_identifier() {
  424. #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \
  425. KEYWORD_GROUP('a') \
  426. KEYWORD("as", Token::AS) \
  427. KEYWORD("and", Token::AND) \
  428. KEYWORD("assert", Token::ASSERT) \
  429. KEYWORD("await", Token::AWAIT) \
  430. KEYWORD_GROUP('b') \
  431. KEYWORD("break", Token::BREAK) \
  432. KEYWORD("breakpoint", Token::BREAKPOINT) \
  433. KEYWORD_GROUP('c') \
  434. KEYWORD("class", Token::CLASS) \
  435. KEYWORD("class_name", Token::CLASS_NAME) \
  436. KEYWORD("const", Token::CONST) \
  437. KEYWORD("continue", Token::CONTINUE) \
  438. KEYWORD_GROUP('e') \
  439. KEYWORD("elif", Token::ELIF) \
  440. KEYWORD("else", Token::ELSE) \
  441. KEYWORD("enum", Token::ENUM) \
  442. KEYWORD("extends", Token::EXTENDS) \
  443. KEYWORD_GROUP('f') \
  444. KEYWORD("for", Token::FOR) \
  445. KEYWORD("func", Token::FUNC) \
  446. KEYWORD_GROUP('i') \
  447. KEYWORD("if", Token::IF) \
  448. KEYWORD("in", Token::IN) \
  449. KEYWORD("is", Token::IS) \
  450. KEYWORD_GROUP('m') \
  451. KEYWORD("match", Token::MATCH) \
  452. KEYWORD_GROUP('n') \
  453. KEYWORD("namespace", Token::NAMESPACE) \
  454. KEYWORD("not", Token::NOT) \
  455. KEYWORD_GROUP('o') \
  456. KEYWORD("or", Token::OR) \
  457. KEYWORD_GROUP('p') \
  458. KEYWORD("pass", Token::PASS) \
  459. KEYWORD("preload", Token::PRELOAD) \
  460. KEYWORD_GROUP('r') \
  461. KEYWORD("return", Token::RETURN) \
  462. KEYWORD_GROUP('s') \
  463. KEYWORD("self", Token::SELF) \
  464. KEYWORD("signal", Token::SIGNAL) \
  465. KEYWORD("static", Token::STATIC) \
  466. KEYWORD("super", Token::SUPER) \
  467. KEYWORD_GROUP('t') \
  468. KEYWORD("trait", Token::TRAIT) \
  469. KEYWORD_GROUP('v') \
  470. KEYWORD("var", Token::VAR) \
  471. KEYWORD("void", Token::VOID) \
  472. KEYWORD_GROUP('w') \
  473. KEYWORD("while", Token::WHILE) \
  474. KEYWORD_GROUP('y') \
  475. KEYWORD("yield", Token::YIELD) \
  476. KEYWORD_GROUP('I') \
  477. KEYWORD("INF", Token::CONST_INF) \
  478. KEYWORD_GROUP('N') \
  479. KEYWORD("NAN", Token::CONST_NAN) \
  480. KEYWORD_GROUP('P') \
  481. KEYWORD("PI", Token::CONST_PI) \
  482. KEYWORD_GROUP('T') \
  483. KEYWORD("TAU", Token::CONST_TAU)
  484. #define MIN_KEYWORD_LENGTH 2
  485. #define MAX_KEYWORD_LENGTH 10
  486. // Consume all alphanumeric characters.
  487. while (_is_alphanumeric(_peek())) {
  488. _advance();
  489. }
  490. int length = _current - _start;
  491. if (length == 1 && _peek(-1) == '_') {
  492. // Lone underscore.
  493. return make_token(Token::UNDERSCORE);
  494. }
  495. String name(_start, length);
  496. if (length < MIN_KEYWORD_LENGTH || length > MAX_KEYWORD_LENGTH) {
  497. // Cannot be a keyword, as the length doesn't match any.
  498. return make_identifier(name);
  499. }
  500. // Define some helper macros for the switch case.
  501. #define KEYWORD_GROUP_CASE(char) \
  502. break; \
  503. case char:
  504. #define KEYWORD(keyword, token_type) \
  505. { \
  506. const int keyword_length = sizeof(keyword) - 1; \
  507. static_assert(keyword_length <= MAX_KEYWORD_LENGTH, "There's a keyword longer than the defined maximum length"); \
  508. static_assert(keyword_length >= MIN_KEYWORD_LENGTH, "There's a keyword shorter than the defined minimum length"); \
  509. if (keyword_length == length && name == keyword) { \
  510. return make_token(token_type); \
  511. } \
  512. }
  513. // Find if it's a keyword.
  514. switch (_start[0]) {
  515. default:
  516. KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD)
  517. break;
  518. }
  519. // Check if it's a special literal
  520. if (length == 4) {
  521. if (name == "true") {
  522. return make_literal(true);
  523. } else if (name == "null") {
  524. return make_literal(Variant());
  525. }
  526. } else if (length == 5) {
  527. if (name == "false") {
  528. return make_literal(false);
  529. }
  530. }
  531. // Not a keyword, so must be an identifier.
  532. return make_identifier(name);
  533. #undef KEYWORDS
  534. #undef MIN_KEYWORD_LENGTH
  535. #undef MAX_KEYWORD_LENGTH
  536. #undef KEYWORD_GROUP_CASE
  537. #undef KEYWORD
  538. }
  539. void GDScriptTokenizer::newline(bool p_make_token) {
  540. // Don't overwrite previous newline, nor create if we want a line continuation.
  541. if (p_make_token && !pending_newline && !line_continuation) {
  542. Token newline(Token::NEWLINE);
  543. newline.start_line = line;
  544. newline.end_line = line;
  545. newline.start_column = column - 1;
  546. newline.end_column = column;
  547. newline.leftmost_column = newline.start_column;
  548. newline.rightmost_column = newline.end_column;
  549. pending_newline = true;
  550. last_newline = newline;
  551. }
  552. // Increment line/column counters.
  553. line++;
  554. column = 1;
  555. leftmost_column = 1;
  556. }
  557. GDScriptTokenizer::Token GDScriptTokenizer::number() {
  558. int base = 10;
  559. bool has_decimal = false;
  560. bool has_exponent = false;
  561. bool has_error = false;
  562. bool (*digit_check_func)(char32_t) = _is_digit;
  563. if (_peek(-1) == '.') {
  564. has_decimal = true;
  565. } else if (_peek(-1) == '0') {
  566. if (_peek() == 'x') {
  567. // Hexadecimal.
  568. base = 16;
  569. digit_check_func = _is_hex_digit;
  570. _advance();
  571. } else if (_peek() == 'b') {
  572. // Binary.
  573. base = 2;
  574. digit_check_func = _is_binary_digit;
  575. _advance();
  576. }
  577. }
  578. // Allow '_' to be used in a number, for readability.
  579. bool previous_was_underscore = false;
  580. while (digit_check_func(_peek()) || _peek() == '_') {
  581. if (_peek() == '_') {
  582. if (previous_was_underscore) {
  583. Token error = make_error(R"(Only one underscore can be used as a numeric separator.)");
  584. error.start_column = column;
  585. error.leftmost_column = column;
  586. error.end_column = column + 1;
  587. error.rightmost_column = column + 1;
  588. push_error(error);
  589. }
  590. previous_was_underscore = true;
  591. } else {
  592. previous_was_underscore = false;
  593. }
  594. _advance();
  595. }
  596. // It might be a ".." token (instead of decimal point) so we check if it's not.
  597. if (_peek() == '.' && _peek(1) != '.') {
  598. if (base == 10 && !has_decimal) {
  599. has_decimal = true;
  600. } else if (base == 10) {
  601. Token error = make_error("Cannot use a decimal point twice in a number.");
  602. error.start_column = column;
  603. error.leftmost_column = column;
  604. error.end_column = column + 1;
  605. error.rightmost_column = column + 1;
  606. push_error(error);
  607. has_error = true;
  608. } else if (base == 16) {
  609. Token error = make_error("Cannot use a decimal point in a hexadecimal number.");
  610. error.start_column = column;
  611. error.leftmost_column = column;
  612. error.end_column = column + 1;
  613. error.rightmost_column = column + 1;
  614. push_error(error);
  615. has_error = true;
  616. } else {
  617. Token error = make_error("Cannot use a decimal point in a binary number.");
  618. error.start_column = column;
  619. error.leftmost_column = column;
  620. error.end_column = column + 1;
  621. error.rightmost_column = column + 1;
  622. push_error(error);
  623. has_error = true;
  624. }
  625. if (!has_error) {
  626. _advance();
  627. // Consume decimal digits.
  628. while (_is_digit(_peek()) || _peek() == '_') {
  629. _advance();
  630. }
  631. }
  632. }
  633. if (base == 10) {
  634. if (_peek() == 'e' || _peek() == 'E') {
  635. has_exponent = true;
  636. _advance();
  637. if (_peek() == '+' || _peek() == '-') {
  638. // Exponent sign.
  639. _advance();
  640. }
  641. // Consume exponent digits.
  642. if (!_is_digit(_peek())) {
  643. Token error = make_error(R"(Expected exponent value after "e".)");
  644. error.start_column = column;
  645. error.leftmost_column = column;
  646. error.end_column = column + 1;
  647. error.rightmost_column = column + 1;
  648. push_error(error);
  649. }
  650. previous_was_underscore = false;
  651. while (_is_digit(_peek()) || _peek() == '_') {
  652. if (_peek() == '_') {
  653. if (previous_was_underscore) {
  654. Token error = make_error(R"(Only one underscore can be used as a numeric separator.)");
  655. error.start_column = column;
  656. error.leftmost_column = column;
  657. error.end_column = column + 1;
  658. error.rightmost_column = column + 1;
  659. push_error(error);
  660. }
  661. previous_was_underscore = true;
  662. } else {
  663. previous_was_underscore = false;
  664. }
  665. _advance();
  666. }
  667. }
  668. }
  669. // Detect extra decimal point.
  670. if (!has_error && has_decimal && _peek() == '.' && _peek(1) != '.') {
  671. Token error = make_error("Cannot use a decimal point twice in a number.");
  672. error.start_column = column;
  673. error.leftmost_column = column;
  674. error.end_column = column + 1;
  675. error.rightmost_column = column + 1;
  676. push_error(error);
  677. has_error = true;
  678. } else if (_is_alphanumeric(_peek())) {
  679. // Letter at the end of the number.
  680. push_error("Invalid numeric notation.");
  681. }
  682. // Create a string with the whole number.
  683. int length = _current - _start;
  684. String number = String(_start, length).replace("_", "");
  685. // Convert to the appropriate literal type.
  686. if (base == 16) {
  687. int64_t value = number.hex_to_int();
  688. return make_literal(value);
  689. } else if (base == 2) {
  690. int64_t value = number.bin_to_int();
  691. return make_literal(value);
  692. } else if (has_decimal || has_exponent) {
  693. double value = number.to_float();
  694. return make_literal(value);
  695. } else {
  696. int64_t value = number.to_int();
  697. return make_literal(value);
  698. }
  699. }
  700. GDScriptTokenizer::Token GDScriptTokenizer::string() {
  701. enum StringType {
  702. STRING_REGULAR,
  703. STRING_NAME,
  704. STRING_NODEPATH,
  705. };
  706. bool is_multiline = false;
  707. StringType type = STRING_REGULAR;
  708. if (_peek(-1) == '&') {
  709. type = STRING_NAME;
  710. _advance();
  711. } else if (_peek(-1) == '^') {
  712. type = STRING_NODEPATH;
  713. _advance();
  714. }
  715. char32_t quote_char = _peek(-1);
  716. if (_peek() == quote_char && _peek(1) == quote_char) {
  717. is_multiline = true;
  718. // Consume all quotes.
  719. _advance();
  720. _advance();
  721. }
  722. String result;
  723. for (;;) {
  724. // Consume actual string.
  725. if (_is_at_end()) {
  726. return make_error("Unterminated string.");
  727. }
  728. char32_t ch = _peek();
  729. if (ch == '\\') {
  730. // Escape pattern.
  731. _advance();
  732. if (_is_at_end()) {
  733. return make_error("Unterminated string.");
  734. }
  735. // Grab escape character.
  736. char32_t code = _peek();
  737. _advance();
  738. if (_is_at_end()) {
  739. return make_error("Unterminated string.");
  740. }
  741. char32_t escaped = 0;
  742. bool valid_escape = true;
  743. switch (code) {
  744. case 'a':
  745. escaped = '\a';
  746. break;
  747. case 'b':
  748. escaped = '\b';
  749. break;
  750. case 'f':
  751. escaped = '\f';
  752. break;
  753. case 'n':
  754. escaped = '\n';
  755. break;
  756. case 'r':
  757. escaped = '\r';
  758. break;
  759. case 't':
  760. escaped = '\t';
  761. break;
  762. case 'v':
  763. escaped = '\v';
  764. break;
  765. case '\'':
  766. escaped = '\'';
  767. break;
  768. case '\"':
  769. escaped = '\"';
  770. break;
  771. case '\\':
  772. escaped = '\\';
  773. break;
  774. case 'u':
  775. // Hexadecimal sequence.
  776. for (int i = 0; i < 4; i++) {
  777. if (_is_at_end()) {
  778. return make_error("Unterminated string.");
  779. }
  780. char32_t digit = _peek();
  781. char32_t value = 0;
  782. if (digit >= '0' && digit <= '9') {
  783. value = digit - '0';
  784. } else if (digit >= 'a' && digit <= 'f') {
  785. value = digit - 'a';
  786. value += 10;
  787. } else if (digit >= 'A' && digit <= 'F') {
  788. value = digit - 'A';
  789. value += 10;
  790. } else {
  791. // Make error, but keep parsing the string.
  792. Token error = make_error("Invalid hexadecimal digit in unicode escape sequence.");
  793. error.start_column = column;
  794. error.leftmost_column = error.start_column;
  795. error.end_column = column + 1;
  796. error.rightmost_column = error.end_column;
  797. push_error(error);
  798. valid_escape = false;
  799. break;
  800. }
  801. escaped <<= 4;
  802. escaped |= value;
  803. _advance();
  804. }
  805. break;
  806. case '\r':
  807. if (_peek() != '\n') {
  808. // Carriage return without newline in string. (???)
  809. // Just add it to the string and keep going.
  810. result += ch;
  811. _advance();
  812. break;
  813. }
  814. [[fallthrough]];
  815. case '\n':
  816. // Escaping newline.
  817. newline(false);
  818. valid_escape = false; // Don't add to the string.
  819. break;
  820. default:
  821. Token error = make_error("Invalid escape in string.");
  822. error.start_column = column - 2;
  823. error.leftmost_column = error.start_column;
  824. push_error(error);
  825. valid_escape = false;
  826. break;
  827. }
  828. if (valid_escape) {
  829. result += escaped;
  830. }
  831. } else if (ch == quote_char) {
  832. _advance();
  833. if (is_multiline) {
  834. if (_peek() == quote_char && _peek(1) == quote_char) {
  835. // Ended the multiline string. Consume all quotes.
  836. _advance();
  837. _advance();
  838. break;
  839. } else {
  840. // Not a multiline string termination, add consumed quote.
  841. result += quote_char;
  842. }
  843. } else {
  844. // Ended single-line string.
  845. break;
  846. }
  847. } else {
  848. result += ch;
  849. _advance();
  850. if (ch == '\n') {
  851. newline(false);
  852. }
  853. }
  854. }
  855. // Make the literal.
  856. Variant string;
  857. switch (type) {
  858. case STRING_NAME:
  859. string = StringName(result);
  860. break;
  861. case STRING_NODEPATH:
  862. string = NodePath(result);
  863. break;
  864. case STRING_REGULAR:
  865. string = result;
  866. break;
  867. }
  868. return make_literal(string);
  869. }
  870. void GDScriptTokenizer::check_indent() {
  871. ERR_FAIL_COND_MSG(column != 1, "Checking tokenizer indentation in the middle of a line.");
  872. if (_is_at_end()) {
  873. // Send dedents for every indent level.
  874. pending_indents -= indent_level();
  875. indent_stack.clear();
  876. return;
  877. }
  878. for (;;) {
  879. char32_t current_indent_char = _peek();
  880. int indent_count = 0;
  881. if (current_indent_char != ' ' && current_indent_char != '\t' && current_indent_char != '\r' && current_indent_char != '\n' && current_indent_char != '#') {
  882. // First character of the line is not whitespace, so we clear all indentation levels.
  883. // Unless we are in a continuation or in multiline mode (inside expression).
  884. if (line_continuation || multiline_mode) {
  885. return;
  886. }
  887. pending_indents -= indent_level();
  888. indent_stack.clear();
  889. return;
  890. }
  891. if (_peek() == '\r') {
  892. _advance();
  893. if (_peek() != '\n') {
  894. push_error("Stray carriage return character in source code.");
  895. }
  896. }
  897. if (_peek() == '\n') {
  898. // Empty line, keep going.
  899. _advance();
  900. newline(false);
  901. continue;
  902. }
  903. // Check indent level.
  904. bool mixed = false;
  905. while (!_is_at_end()) {
  906. char32_t space = _peek();
  907. if (space == '\t') {
  908. // Consider individual tab columns.
  909. column += tab_size - 1;
  910. indent_count += tab_size;
  911. } else if (space == ' ') {
  912. indent_count += 1;
  913. } else {
  914. break;
  915. }
  916. mixed = mixed || space != current_indent_char;
  917. _advance();
  918. }
  919. if (mixed) {
  920. Token error = make_error("Mixed use of tabs and spaces for indentation.");
  921. error.start_line = line;
  922. error.start_column = 1;
  923. error.leftmost_column = 1;
  924. error.rightmost_column = column;
  925. push_error(error);
  926. }
  927. if (_is_at_end()) {
  928. // Reached the end with an empty line, so just dedent as much as needed.
  929. pending_indents -= indent_level();
  930. indent_stack.clear();
  931. return;
  932. }
  933. if (_peek() == '\r') {
  934. _advance();
  935. if (_peek() != '\n') {
  936. push_error("Stray carriage return character in source code.");
  937. }
  938. }
  939. if (_peek() == '\n') {
  940. // Empty line, keep going.
  941. _advance();
  942. newline(false);
  943. continue;
  944. }
  945. if (_peek() == '#') {
  946. // Comment. Advance to the next line.
  947. #ifdef TOOLS_ENABLED
  948. String comment;
  949. while (_peek() != '\n' && !_is_at_end()) {
  950. comment += _advance();
  951. }
  952. comments[line] = CommentData(comment, true);
  953. #else
  954. while (_peek() != '\n' && !_is_at_end()) {
  955. _advance();
  956. }
  957. #endif // TOOLS_ENABLED
  958. if (_is_at_end()) {
  959. // Reached the end with an empty line, so just dedent as much as needed.
  960. pending_indents -= indent_level();
  961. indent_stack.clear();
  962. return;
  963. }
  964. _advance(); // Consume '\n'.
  965. newline(false);
  966. continue;
  967. }
  968. if (line_continuation || multiline_mode) {
  969. // We cleared up all the whitespace at the beginning of the line.
  970. // But if this is a continuation or multiline mode and we don't want any indentation change.
  971. return;
  972. }
  973. // Check if indentation character is consistent.
  974. if (indent_char == '\0') {
  975. // First time indenting, choose character now.
  976. indent_char = current_indent_char;
  977. } else if (current_indent_char != indent_char) {
  978. Token error = make_error(vformat("Used \"%s\" for indentation instead \"%s\" as used before in the file.", String(&current_indent_char, 1).c_escape(), String(&indent_char, 1).c_escape()));
  979. error.start_line = line;
  980. error.start_column = 1;
  981. error.leftmost_column = 1;
  982. error.rightmost_column = column;
  983. push_error(error);
  984. }
  985. // Now we can do actual indentation changes.
  986. // Check if indent or dedent.
  987. int previous_indent = 0;
  988. if (indent_level() > 0) {
  989. previous_indent = indent_stack.back()->get();
  990. }
  991. if (indent_count == previous_indent) {
  992. // No change in indentation.
  993. return;
  994. }
  995. if (indent_count > previous_indent) {
  996. // Indentation increased.
  997. indent_stack.push_back(indent_count);
  998. pending_indents++;
  999. } else {
  1000. // Indentation decreased (dedent).
  1001. if (indent_level() == 0) {
  1002. push_error("Tokenizer bug: trying to dedent without previous indent.");
  1003. return;
  1004. }
  1005. while (indent_level() > 0 && indent_stack.back()->get() > indent_count) {
  1006. indent_stack.pop_back();
  1007. pending_indents--;
  1008. }
  1009. if ((indent_level() > 0 && indent_stack.back()->get() != indent_count) || (indent_level() == 0 && indent_count != 0)) {
  1010. // Mismatched indentation alignment.
  1011. Token error = make_error("Unindent doesn't match the previous indentation level.");
  1012. error.start_line = line;
  1013. error.start_column = 1;
  1014. error.leftmost_column = 1;
  1015. error.end_column = column + 1;
  1016. error.rightmost_column = column + 1;
  1017. push_error(error);
  1018. // Still, we'll be lenient and keep going, so keep this level in the stack.
  1019. indent_stack.push_back(indent_count);
  1020. }
  1021. }
  1022. break; // Get out of the loop in any case.
  1023. }
  1024. }
  1025. void GDScriptTokenizer::_skip_whitespace() {
  1026. if (pending_indents != 0) {
  1027. // Still have some indent/dedent tokens to give.
  1028. return;
  1029. }
  1030. bool is_bol = column == 1; // Beginning of line.
  1031. if (is_bol) {
  1032. check_indent();
  1033. return;
  1034. }
  1035. for (;;) {
  1036. char32_t c = _peek();
  1037. switch (c) {
  1038. case ' ':
  1039. _advance();
  1040. break;
  1041. case '\t':
  1042. _advance();
  1043. // Consider individual tab columns.
  1044. column += tab_size - 1;
  1045. break;
  1046. case '\r':
  1047. _advance(); // Consume either way.
  1048. if (_peek() != '\n') {
  1049. push_error("Stray carriage return character in source code.");
  1050. return;
  1051. }
  1052. break;
  1053. case '\n':
  1054. _advance();
  1055. newline(!is_bol); // Don't create new line token if line is empty.
  1056. check_indent();
  1057. break;
  1058. case '#': {
  1059. // Comment.
  1060. #ifdef TOOLS_ENABLED
  1061. String comment;
  1062. while (_peek() != '\n' && !_is_at_end()) {
  1063. comment += _advance();
  1064. }
  1065. comments[line] = CommentData(comment, is_bol);
  1066. #else
  1067. while (_peek() != '\n' && !_is_at_end()) {
  1068. _advance();
  1069. }
  1070. #endif // TOOLS_ENABLED
  1071. if (_is_at_end()) {
  1072. return;
  1073. }
  1074. _advance(); // Consume '\n'
  1075. newline(!is_bol);
  1076. check_indent();
  1077. } break;
  1078. default:
  1079. return;
  1080. }
  1081. }
  1082. }
  1083. GDScriptTokenizer::Token GDScriptTokenizer::scan() {
  1084. if (has_error()) {
  1085. return pop_error();
  1086. }
  1087. _skip_whitespace();
  1088. if (pending_newline) {
  1089. pending_newline = false;
  1090. if (!multiline_mode) {
  1091. // Don't return newline tokens on multiline mode.
  1092. return last_newline;
  1093. }
  1094. }
  1095. // Check for potential errors after skipping whitespace().
  1096. if (has_error()) {
  1097. return pop_error();
  1098. }
  1099. _start = _current;
  1100. start_line = line;
  1101. start_column = column;
  1102. leftmost_column = column;
  1103. rightmost_column = column;
  1104. if (pending_indents != 0) {
  1105. // Adjust position for indent.
  1106. _start -= start_column - 1;
  1107. start_column = 1;
  1108. leftmost_column = 1;
  1109. if (pending_indents > 0) {
  1110. // Indents.
  1111. pending_indents--;
  1112. return make_token(Token::INDENT);
  1113. } else {
  1114. // Dedents.
  1115. pending_indents++;
  1116. Token dedent = make_token(Token::DEDENT);
  1117. dedent.end_column += 1;
  1118. dedent.rightmost_column += 1;
  1119. return dedent;
  1120. }
  1121. }
  1122. if (_is_at_end()) {
  1123. return make_token(Token::TK_EOF);
  1124. }
  1125. const char32_t c = _advance();
  1126. if (c == '\\') {
  1127. // Line continuation with backslash.
  1128. if (_peek() == '\r') {
  1129. if (_peek(1) != '\n') {
  1130. return make_error("Unexpected carriage return character.");
  1131. }
  1132. _advance();
  1133. }
  1134. if (_peek() != '\n') {
  1135. return make_error("Expected new line after \"\\\".");
  1136. }
  1137. _advance();
  1138. newline(false);
  1139. line_continuation = true;
  1140. return scan(); // Recurse to get next token.
  1141. }
  1142. line_continuation = false;
  1143. if (_is_digit(c)) {
  1144. return number();
  1145. } else if (_is_alphanumeric(c)) {
  1146. return potential_identifier();
  1147. }
  1148. switch (c) {
  1149. // String literals.
  1150. case '"':
  1151. case '\'':
  1152. return string();
  1153. // Annotation.
  1154. case '@':
  1155. return annotation();
  1156. // Single characters.
  1157. case '~':
  1158. return make_token(Token::TILDE);
  1159. case ',':
  1160. return make_token(Token::COMMA);
  1161. case ':':
  1162. return make_token(Token::COLON);
  1163. case ';':
  1164. return make_token(Token::SEMICOLON);
  1165. case '$':
  1166. return make_token(Token::DOLLAR);
  1167. case '?':
  1168. return make_token(Token::QUESTION_MARK);
  1169. case '`':
  1170. return make_token(Token::BACKTICK);
  1171. // Parens.
  1172. case '(':
  1173. push_paren('(');
  1174. return make_token(Token::PARENTHESIS_OPEN);
  1175. case '[':
  1176. push_paren('[');
  1177. return make_token(Token::BRACKET_OPEN);
  1178. case '{':
  1179. push_paren('{');
  1180. return make_token(Token::BRACE_OPEN);
  1181. case ')':
  1182. if (!pop_paren('(')) {
  1183. return make_paren_error(c);
  1184. }
  1185. return make_token(Token::PARENTHESIS_CLOSE);
  1186. case ']':
  1187. if (!pop_paren('[')) {
  1188. return make_paren_error(c);
  1189. }
  1190. return make_token(Token::BRACKET_CLOSE);
  1191. case '}':
  1192. if (!pop_paren('{')) {
  1193. return make_paren_error(c);
  1194. }
  1195. return make_token(Token::BRACE_CLOSE);
  1196. // Double characters.
  1197. case '!':
  1198. if (_peek() == '=') {
  1199. _advance();
  1200. return make_token(Token::BANG_EQUAL);
  1201. } else {
  1202. return make_token(Token::BANG);
  1203. }
  1204. case '.':
  1205. if (_peek() == '.') {
  1206. _advance();
  1207. return make_token(Token::PERIOD_PERIOD);
  1208. } else if (_is_digit(_peek())) {
  1209. // Number starting with '.'.
  1210. return number();
  1211. } else {
  1212. return make_token(Token::PERIOD);
  1213. }
  1214. case '+':
  1215. if (_peek() == '=') {
  1216. _advance();
  1217. return make_token(Token::PLUS_EQUAL);
  1218. } else {
  1219. return make_token(Token::PLUS);
  1220. }
  1221. case '-':
  1222. if (_peek() == '=') {
  1223. _advance();
  1224. return make_token(Token::MINUS_EQUAL);
  1225. } else if (_peek() == '>') {
  1226. _advance();
  1227. return make_token(Token::FORWARD_ARROW);
  1228. } else {
  1229. return make_token(Token::MINUS);
  1230. }
  1231. case '*':
  1232. if (_peek() == '=') {
  1233. _advance();
  1234. return make_token(Token::STAR_EQUAL);
  1235. } else {
  1236. return make_token(Token::STAR);
  1237. }
  1238. case '/':
  1239. if (_peek() == '=') {
  1240. _advance();
  1241. return make_token(Token::SLASH_EQUAL);
  1242. } else {
  1243. return make_token(Token::SLASH);
  1244. }
  1245. case '%':
  1246. if (_peek() == '=') {
  1247. _advance();
  1248. return make_token(Token::PERCENT_EQUAL);
  1249. } else {
  1250. return make_token(Token::PERCENT);
  1251. }
  1252. case '^':
  1253. if (_peek() == '=') {
  1254. _advance();
  1255. return make_token(Token::CARET_EQUAL);
  1256. } else if (_peek() == '"' || _peek() == '\'') {
  1257. // Node path
  1258. return string();
  1259. } else {
  1260. return make_token(Token::CARET);
  1261. }
  1262. case '&':
  1263. if (_peek() == '&') {
  1264. _advance();
  1265. return make_token(Token::AMPERSAND_AMPERSAND);
  1266. } else if (_peek() == '=') {
  1267. _advance();
  1268. return make_token(Token::AMPERSAND_EQUAL);
  1269. } else if (_peek() == '"' || _peek() == '\'') {
  1270. // String Name
  1271. return string();
  1272. } else {
  1273. return make_token(Token::AMPERSAND);
  1274. }
  1275. case '|':
  1276. if (_peek() == '|') {
  1277. _advance();
  1278. return make_token(Token::PIPE_PIPE);
  1279. } else if (_peek() == '=') {
  1280. _advance();
  1281. return make_token(Token::PIPE_EQUAL);
  1282. } else {
  1283. return make_token(Token::PIPE);
  1284. }
  1285. // Potential VCS conflict markers.
  1286. case '=':
  1287. if (_peek() == '=') {
  1288. return check_vcs_marker('=', Token::EQUAL_EQUAL);
  1289. } else {
  1290. return make_token(Token::EQUAL);
  1291. }
  1292. case '<':
  1293. if (_peek() == '=') {
  1294. _advance();
  1295. return make_token(Token::LESS_EQUAL);
  1296. } else if (_peek() == '<') {
  1297. if (_peek(1) == '=') {
  1298. _advance();
  1299. _advance(); // Advance both '<' and '='
  1300. return make_token(Token::LESS_LESS_EQUAL);
  1301. } else {
  1302. return check_vcs_marker('<', Token::LESS_LESS);
  1303. }
  1304. } else {
  1305. return make_token(Token::LESS);
  1306. }
  1307. case '>':
  1308. if (_peek() == '=') {
  1309. _advance();
  1310. return make_token(Token::GREATER_EQUAL);
  1311. } else if (_peek() == '>') {
  1312. if (_peek(1) == '=') {
  1313. _advance();
  1314. _advance(); // Advance both '>' and '='
  1315. return make_token(Token::GREATER_GREATER_EQUAL);
  1316. } else {
  1317. return check_vcs_marker('>', Token::GREATER_GREATER);
  1318. }
  1319. } else {
  1320. return make_token(Token::GREATER);
  1321. }
  1322. default:
  1323. return make_error(vformat(R"(Unknown character "%s".")", String(&c, 1)));
  1324. }
  1325. }
  1326. GDScriptTokenizer::GDScriptTokenizer() {
  1327. #ifdef TOOLS_ENABLED
  1328. if (EditorSettings::get_singleton()) {
  1329. tab_size = EditorSettings::get_singleton()->get_setting("text_editor/indent/size");
  1330. }
  1331. #endif // TOOLS_ENABLED
  1332. }