gdscript_tokenizer.cpp 44 KB

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