gdscript_tokenizer.cpp 37 KB

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