gdscript_tokenizer.cpp 43 KB

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