gdscript_tokenizer.cpp 40 KB

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