gdscript_tokenizer.cpp 40 KB

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