gdscript_tokenizer.cpp 40 KB

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