gdscript_tokenizer.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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 == 0x200E || ch == 0x200F || (ch >= 0x202A && ch <= 0x202E) || (ch >= 0x2066 && ch <= 0x2069)) {
  730. Token error = make_error("Invisible text direction control character present in the string, escape it (\"\\u" + String::num_int64(ch, 16) + "\") to avoid confusion.");
  731. error.start_column = column;
  732. error.leftmost_column = error.start_column;
  733. error.end_column = column + 1;
  734. error.rightmost_column = error.end_column;
  735. push_error(error);
  736. }
  737. if (ch == '\\') {
  738. // Escape pattern.
  739. _advance();
  740. if (_is_at_end()) {
  741. return make_error("Unterminated string.");
  742. }
  743. // Grab escape character.
  744. char32_t code = _peek();
  745. _advance();
  746. if (_is_at_end()) {
  747. return make_error("Unterminated string.");
  748. }
  749. char32_t escaped = 0;
  750. bool valid_escape = true;
  751. switch (code) {
  752. case 'a':
  753. escaped = '\a';
  754. break;
  755. case 'b':
  756. escaped = '\b';
  757. break;
  758. case 'f':
  759. escaped = '\f';
  760. break;
  761. case 'n':
  762. escaped = '\n';
  763. break;
  764. case 'r':
  765. escaped = '\r';
  766. break;
  767. case 't':
  768. escaped = '\t';
  769. break;
  770. case 'v':
  771. escaped = '\v';
  772. break;
  773. case '\'':
  774. escaped = '\'';
  775. break;
  776. case '\"':
  777. escaped = '\"';
  778. break;
  779. case '\\':
  780. escaped = '\\';
  781. break;
  782. case 'u':
  783. // Hexadecimal sequence.
  784. for (int i = 0; i < 4; i++) {
  785. if (_is_at_end()) {
  786. return make_error("Unterminated string.");
  787. }
  788. char32_t digit = _peek();
  789. char32_t value = 0;
  790. if (digit >= '0' && digit <= '9') {
  791. value = digit - '0';
  792. } else if (digit >= 'a' && digit <= 'f') {
  793. value = digit - 'a';
  794. value += 10;
  795. } else if (digit >= 'A' && digit <= 'F') {
  796. value = digit - 'A';
  797. value += 10;
  798. } else {
  799. // Make error, but keep parsing the string.
  800. Token error = make_error("Invalid hexadecimal digit in unicode escape sequence.");
  801. error.start_column = column;
  802. error.leftmost_column = error.start_column;
  803. error.end_column = column + 1;
  804. error.rightmost_column = error.end_column;
  805. push_error(error);
  806. valid_escape = false;
  807. break;
  808. }
  809. escaped <<= 4;
  810. escaped |= value;
  811. _advance();
  812. }
  813. break;
  814. case '\r':
  815. if (_peek() != '\n') {
  816. // Carriage return without newline in string. (???)
  817. // Just add it to the string and keep going.
  818. result += ch;
  819. _advance();
  820. break;
  821. }
  822. [[fallthrough]];
  823. case '\n':
  824. // Escaping newline.
  825. newline(false);
  826. valid_escape = false; // Don't add to the string.
  827. break;
  828. default:
  829. Token error = make_error("Invalid escape in string.");
  830. error.start_column = column - 2;
  831. error.leftmost_column = error.start_column;
  832. push_error(error);
  833. valid_escape = false;
  834. break;
  835. }
  836. if (valid_escape) {
  837. result += escaped;
  838. }
  839. } else if (ch == quote_char) {
  840. _advance();
  841. if (is_multiline) {
  842. if (_peek() == quote_char && _peek(1) == quote_char) {
  843. // Ended the multiline string. Consume all quotes.
  844. _advance();
  845. _advance();
  846. break;
  847. } else {
  848. // Not a multiline string termination, add consumed quote.
  849. result += quote_char;
  850. }
  851. } else {
  852. // Ended single-line string.
  853. break;
  854. }
  855. } else {
  856. result += ch;
  857. _advance();
  858. if (ch == '\n') {
  859. newline(false);
  860. }
  861. }
  862. }
  863. // Make the literal.
  864. Variant string;
  865. switch (type) {
  866. case STRING_NAME:
  867. string = StringName(result);
  868. break;
  869. case STRING_NODEPATH:
  870. string = NodePath(result);
  871. break;
  872. case STRING_REGULAR:
  873. string = result;
  874. break;
  875. }
  876. return make_literal(string);
  877. }
  878. void GDScriptTokenizer::check_indent() {
  879. ERR_FAIL_COND_MSG(column != 1, "Checking tokenizer indentation in the middle of a line.");
  880. if (_is_at_end()) {
  881. // Send dedents for every indent level.
  882. pending_indents -= indent_level();
  883. indent_stack.clear();
  884. return;
  885. }
  886. for (;;) {
  887. char32_t current_indent_char = _peek();
  888. int indent_count = 0;
  889. if (current_indent_char != ' ' && current_indent_char != '\t' && current_indent_char != '\r' && current_indent_char != '\n' && current_indent_char != '#') {
  890. // First character of the line is not whitespace, so we clear all indentation levels.
  891. // Unless we are in a continuation or in multiline mode (inside expression).
  892. if (line_continuation || multiline_mode) {
  893. return;
  894. }
  895. pending_indents -= indent_level();
  896. indent_stack.clear();
  897. return;
  898. }
  899. if (_peek() == '\r') {
  900. _advance();
  901. if (_peek() != '\n') {
  902. push_error("Stray carriage return character in source code.");
  903. }
  904. }
  905. if (_peek() == '\n') {
  906. // Empty line, keep going.
  907. _advance();
  908. newline(false);
  909. continue;
  910. }
  911. // Check indent level.
  912. bool mixed = false;
  913. while (!_is_at_end()) {
  914. char32_t space = _peek();
  915. if (space == '\t') {
  916. // Consider individual tab columns.
  917. column += tab_size - 1;
  918. indent_count += tab_size;
  919. } else if (space == ' ') {
  920. indent_count += 1;
  921. } else {
  922. break;
  923. }
  924. mixed = mixed || space != current_indent_char;
  925. _advance();
  926. }
  927. if (mixed) {
  928. Token error = make_error("Mixed use of tabs and spaces for indentation.");
  929. error.start_line = line;
  930. error.start_column = 1;
  931. error.leftmost_column = 1;
  932. error.rightmost_column = column;
  933. push_error(error);
  934. }
  935. if (_is_at_end()) {
  936. // Reached the end with an empty line, so just dedent as much as needed.
  937. pending_indents -= indent_level();
  938. indent_stack.clear();
  939. return;
  940. }
  941. if (_peek() == '\r') {
  942. _advance();
  943. if (_peek() != '\n') {
  944. push_error("Stray carriage return character in source code.");
  945. }
  946. }
  947. if (_peek() == '\n') {
  948. // Empty line, keep going.
  949. _advance();
  950. newline(false);
  951. continue;
  952. }
  953. if (_peek() == '#') {
  954. // Comment. Advance to the next line.
  955. #ifdef TOOLS_ENABLED
  956. String comment;
  957. while (_peek() != '\n' && !_is_at_end()) {
  958. comment += _advance();
  959. }
  960. comments[line] = CommentData(comment, true);
  961. #else
  962. while (_peek() != '\n' && !_is_at_end()) {
  963. _advance();
  964. }
  965. #endif // TOOLS_ENABLED
  966. if (_is_at_end()) {
  967. // Reached the end with an empty line, so just dedent as much as needed.
  968. pending_indents -= indent_level();
  969. indent_stack.clear();
  970. return;
  971. }
  972. _advance(); // Consume '\n'.
  973. newline(false);
  974. continue;
  975. }
  976. if (line_continuation || multiline_mode) {
  977. // We cleared up all the whitespace at the beginning of the line.
  978. // But if this is a continuation or multiline mode and we don't want any indentation change.
  979. return;
  980. }
  981. // Check if indentation character is consistent.
  982. if (indent_char == '\0') {
  983. // First time indenting, choose character now.
  984. indent_char = current_indent_char;
  985. } else if (current_indent_char != indent_char) {
  986. Token error = make_error(vformat("Used %s character for indentation instead of %s as used before in the file.",
  987. _get_indent_char_name(current_indent_char), _get_indent_char_name(indent_char)));
  988. error.start_line = line;
  989. error.start_column = 1;
  990. error.leftmost_column = 1;
  991. error.rightmost_column = column;
  992. push_error(error);
  993. }
  994. // Now we can do actual indentation changes.
  995. // Check if indent or dedent.
  996. int previous_indent = 0;
  997. if (indent_level() > 0) {
  998. previous_indent = indent_stack.back()->get();
  999. }
  1000. if (indent_count == previous_indent) {
  1001. // No change in indentation.
  1002. return;
  1003. }
  1004. if (indent_count > previous_indent) {
  1005. // Indentation increased.
  1006. indent_stack.push_back(indent_count);
  1007. pending_indents++;
  1008. } else {
  1009. // Indentation decreased (dedent).
  1010. if (indent_level() == 0) {
  1011. push_error("Tokenizer bug: trying to dedent without previous indent.");
  1012. return;
  1013. }
  1014. while (indent_level() > 0 && indent_stack.back()->get() > indent_count) {
  1015. indent_stack.pop_back();
  1016. pending_indents--;
  1017. }
  1018. if ((indent_level() > 0 && indent_stack.back()->get() != indent_count) || (indent_level() == 0 && indent_count != 0)) {
  1019. // Mismatched indentation alignment.
  1020. Token error = make_error("Unindent doesn't match the previous indentation level.");
  1021. error.start_line = line;
  1022. error.start_column = 1;
  1023. error.leftmost_column = 1;
  1024. error.end_column = column + 1;
  1025. error.rightmost_column = column + 1;
  1026. push_error(error);
  1027. // Still, we'll be lenient and keep going, so keep this level in the stack.
  1028. indent_stack.push_back(indent_count);
  1029. }
  1030. }
  1031. break; // Get out of the loop in any case.
  1032. }
  1033. }
  1034. String GDScriptTokenizer::_get_indent_char_name(char32_t ch) {
  1035. ERR_FAIL_COND_V(ch != ' ' && ch != '\t', String(&ch, 1).c_escape());
  1036. return ch == ' ' ? "space" : "tab";
  1037. }
  1038. void GDScriptTokenizer::_skip_whitespace() {
  1039. if (pending_indents != 0) {
  1040. // Still have some indent/dedent tokens to give.
  1041. return;
  1042. }
  1043. bool is_bol = column == 1; // Beginning of line.
  1044. if (is_bol) {
  1045. check_indent();
  1046. return;
  1047. }
  1048. for (;;) {
  1049. char32_t c = _peek();
  1050. switch (c) {
  1051. case ' ':
  1052. _advance();
  1053. break;
  1054. case '\t':
  1055. _advance();
  1056. // Consider individual tab columns.
  1057. column += tab_size - 1;
  1058. break;
  1059. case '\r':
  1060. _advance(); // Consume either way.
  1061. if (_peek() != '\n') {
  1062. push_error("Stray carriage return character in source code.");
  1063. return;
  1064. }
  1065. break;
  1066. case '\n':
  1067. _advance();
  1068. newline(!is_bol); // Don't create new line token if line is empty.
  1069. check_indent();
  1070. break;
  1071. case '#': {
  1072. // Comment.
  1073. #ifdef TOOLS_ENABLED
  1074. String comment;
  1075. while (_peek() != '\n' && !_is_at_end()) {
  1076. comment += _advance();
  1077. }
  1078. comments[line] = CommentData(comment, is_bol);
  1079. #else
  1080. while (_peek() != '\n' && !_is_at_end()) {
  1081. _advance();
  1082. }
  1083. #endif // TOOLS_ENABLED
  1084. if (_is_at_end()) {
  1085. return;
  1086. }
  1087. _advance(); // Consume '\n'
  1088. newline(!is_bol);
  1089. check_indent();
  1090. } break;
  1091. default:
  1092. return;
  1093. }
  1094. }
  1095. }
  1096. GDScriptTokenizer::Token GDScriptTokenizer::scan() {
  1097. if (has_error()) {
  1098. return pop_error();
  1099. }
  1100. _skip_whitespace();
  1101. if (pending_newline) {
  1102. pending_newline = false;
  1103. if (!multiline_mode) {
  1104. // Don't return newline tokens on multiline mode.
  1105. return last_newline;
  1106. }
  1107. }
  1108. // Check for potential errors after skipping whitespace().
  1109. if (has_error()) {
  1110. return pop_error();
  1111. }
  1112. _start = _current;
  1113. start_line = line;
  1114. start_column = column;
  1115. leftmost_column = column;
  1116. rightmost_column = column;
  1117. if (pending_indents != 0) {
  1118. // Adjust position for indent.
  1119. _start -= start_column - 1;
  1120. start_column = 1;
  1121. leftmost_column = 1;
  1122. if (pending_indents > 0) {
  1123. // Indents.
  1124. pending_indents--;
  1125. return make_token(Token::INDENT);
  1126. } else {
  1127. // Dedents.
  1128. pending_indents++;
  1129. Token dedent = make_token(Token::DEDENT);
  1130. dedent.end_column += 1;
  1131. dedent.rightmost_column += 1;
  1132. return dedent;
  1133. }
  1134. }
  1135. if (_is_at_end()) {
  1136. return make_token(Token::TK_EOF);
  1137. }
  1138. const char32_t c = _advance();
  1139. if (c == '\\') {
  1140. // Line continuation with backslash.
  1141. if (_peek() == '\r') {
  1142. if (_peek(1) != '\n') {
  1143. return make_error("Unexpected carriage return character.");
  1144. }
  1145. _advance();
  1146. }
  1147. if (_peek() != '\n') {
  1148. return make_error("Expected new line after \"\\\".");
  1149. }
  1150. _advance();
  1151. newline(false);
  1152. line_continuation = true;
  1153. return scan(); // Recurse to get next token.
  1154. }
  1155. line_continuation = false;
  1156. if (_is_digit(c)) {
  1157. return number();
  1158. } else if (_is_alphanumeric(c)) {
  1159. return potential_identifier();
  1160. }
  1161. switch (c) {
  1162. // String literals.
  1163. case '"':
  1164. case '\'':
  1165. return string();
  1166. // Annotation.
  1167. case '@':
  1168. return annotation();
  1169. // Single characters.
  1170. case '~':
  1171. return make_token(Token::TILDE);
  1172. case ',':
  1173. return make_token(Token::COMMA);
  1174. case ':':
  1175. return make_token(Token::COLON);
  1176. case ';':
  1177. return make_token(Token::SEMICOLON);
  1178. case '$':
  1179. return make_token(Token::DOLLAR);
  1180. case '?':
  1181. return make_token(Token::QUESTION_MARK);
  1182. case '`':
  1183. return make_token(Token::BACKTICK);
  1184. // Parens.
  1185. case '(':
  1186. push_paren('(');
  1187. return make_token(Token::PARENTHESIS_OPEN);
  1188. case '[':
  1189. push_paren('[');
  1190. return make_token(Token::BRACKET_OPEN);
  1191. case '{':
  1192. push_paren('{');
  1193. return make_token(Token::BRACE_OPEN);
  1194. case ')':
  1195. if (!pop_paren('(')) {
  1196. return make_paren_error(c);
  1197. }
  1198. return make_token(Token::PARENTHESIS_CLOSE);
  1199. case ']':
  1200. if (!pop_paren('[')) {
  1201. return make_paren_error(c);
  1202. }
  1203. return make_token(Token::BRACKET_CLOSE);
  1204. case '}':
  1205. if (!pop_paren('{')) {
  1206. return make_paren_error(c);
  1207. }
  1208. return make_token(Token::BRACE_CLOSE);
  1209. // Double characters.
  1210. case '!':
  1211. if (_peek() == '=') {
  1212. _advance();
  1213. return make_token(Token::BANG_EQUAL);
  1214. } else {
  1215. return make_token(Token::BANG);
  1216. }
  1217. case '.':
  1218. if (_peek() == '.') {
  1219. _advance();
  1220. return make_token(Token::PERIOD_PERIOD);
  1221. } else if (_is_digit(_peek())) {
  1222. // Number starting with '.'.
  1223. return number();
  1224. } else {
  1225. return make_token(Token::PERIOD);
  1226. }
  1227. case '+':
  1228. if (_peek() == '=') {
  1229. _advance();
  1230. return make_token(Token::PLUS_EQUAL);
  1231. } else {
  1232. return make_token(Token::PLUS);
  1233. }
  1234. case '-':
  1235. if (_peek() == '=') {
  1236. _advance();
  1237. return make_token(Token::MINUS_EQUAL);
  1238. } else if (_peek() == '>') {
  1239. _advance();
  1240. return make_token(Token::FORWARD_ARROW);
  1241. } else {
  1242. return make_token(Token::MINUS);
  1243. }
  1244. case '*':
  1245. if (_peek() == '=') {
  1246. _advance();
  1247. return make_token(Token::STAR_EQUAL);
  1248. } else {
  1249. return make_token(Token::STAR);
  1250. }
  1251. case '/':
  1252. if (_peek() == '=') {
  1253. _advance();
  1254. return make_token(Token::SLASH_EQUAL);
  1255. } else {
  1256. return make_token(Token::SLASH);
  1257. }
  1258. case '%':
  1259. if (_peek() == '=') {
  1260. _advance();
  1261. return make_token(Token::PERCENT_EQUAL);
  1262. } else {
  1263. return make_token(Token::PERCENT);
  1264. }
  1265. case '^':
  1266. if (_peek() == '=') {
  1267. _advance();
  1268. return make_token(Token::CARET_EQUAL);
  1269. } else if (_peek() == '"' || _peek() == '\'') {
  1270. // Node path
  1271. return string();
  1272. } else {
  1273. return make_token(Token::CARET);
  1274. }
  1275. case '&':
  1276. if (_peek() == '&') {
  1277. _advance();
  1278. return make_token(Token::AMPERSAND_AMPERSAND);
  1279. } else if (_peek() == '=') {
  1280. _advance();
  1281. return make_token(Token::AMPERSAND_EQUAL);
  1282. } else if (_peek() == '"' || _peek() == '\'') {
  1283. // String Name
  1284. return string();
  1285. } else {
  1286. return make_token(Token::AMPERSAND);
  1287. }
  1288. case '|':
  1289. if (_peek() == '|') {
  1290. _advance();
  1291. return make_token(Token::PIPE_PIPE);
  1292. } else if (_peek() == '=') {
  1293. _advance();
  1294. return make_token(Token::PIPE_EQUAL);
  1295. } else {
  1296. return make_token(Token::PIPE);
  1297. }
  1298. // Potential VCS conflict markers.
  1299. case '=':
  1300. if (_peek() == '=') {
  1301. return check_vcs_marker('=', Token::EQUAL_EQUAL);
  1302. } else {
  1303. return make_token(Token::EQUAL);
  1304. }
  1305. case '<':
  1306. if (_peek() == '=') {
  1307. _advance();
  1308. return make_token(Token::LESS_EQUAL);
  1309. } else if (_peek() == '<') {
  1310. if (_peek(1) == '=') {
  1311. _advance();
  1312. _advance(); // Advance both '<' and '='
  1313. return make_token(Token::LESS_LESS_EQUAL);
  1314. } else {
  1315. return check_vcs_marker('<', Token::LESS_LESS);
  1316. }
  1317. } else {
  1318. return make_token(Token::LESS);
  1319. }
  1320. case '>':
  1321. if (_peek() == '=') {
  1322. _advance();
  1323. return make_token(Token::GREATER_EQUAL);
  1324. } else if (_peek() == '>') {
  1325. if (_peek(1) == '=') {
  1326. _advance();
  1327. _advance(); // Advance both '>' and '='
  1328. return make_token(Token::GREATER_GREATER_EQUAL);
  1329. } else {
  1330. return check_vcs_marker('>', Token::GREATER_GREATER);
  1331. }
  1332. } else {
  1333. return make_token(Token::GREATER);
  1334. }
  1335. default:
  1336. return make_error(vformat(R"(Unknown character "%s".")", String(&c, 1)));
  1337. }
  1338. }
  1339. GDScriptTokenizer::GDScriptTokenizer() {
  1340. #ifdef TOOLS_ENABLED
  1341. if (EditorSettings::get_singleton()) {
  1342. tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size");
  1343. }
  1344. #endif // TOOLS_ENABLED
  1345. }