gdscript_tokenizer.cpp 40 KB

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