gdscript_tokenizer.cpp 42 KB

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