gdscript_tokenizer.cpp 37 KB

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