Lexer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "Lexer.h"
  28. #include <AK/HashMap.h>
  29. #include <AK/StringBuilder.h>
  30. #include <ctype.h>
  31. #include <stdio.h>
  32. //#define LEXER_DEBUG
  33. namespace JS {
  34. HashMap<String, TokenType> Lexer::s_keywords;
  35. HashMap<String, TokenType> Lexer::s_three_char_tokens;
  36. HashMap<String, TokenType> Lexer::s_two_char_tokens;
  37. HashMap<char, TokenType> Lexer::s_single_char_tokens;
  38. Lexer::Lexer(StringView source)
  39. : m_source(source)
  40. , m_current_token(TokenType::Eof, {}, StringView(nullptr), StringView(nullptr), 0, 0)
  41. {
  42. if (s_keywords.is_empty()) {
  43. s_keywords.set("await", TokenType::Await);
  44. s_keywords.set("break", TokenType::Break);
  45. s_keywords.set("case", TokenType::Case);
  46. s_keywords.set("catch", TokenType::Catch);
  47. s_keywords.set("class", TokenType::Class);
  48. s_keywords.set("const", TokenType::Const);
  49. s_keywords.set("continue", TokenType::Continue);
  50. s_keywords.set("debugger", TokenType::Debugger);
  51. s_keywords.set("default", TokenType::Default);
  52. s_keywords.set("delete", TokenType::Delete);
  53. s_keywords.set("do", TokenType::Do);
  54. s_keywords.set("else", TokenType::Else);
  55. s_keywords.set("enum", TokenType::Enum);
  56. s_keywords.set("export", TokenType::Export);
  57. s_keywords.set("extends", TokenType::Extends);
  58. s_keywords.set("false", TokenType::BoolLiteral);
  59. s_keywords.set("finally", TokenType::Finally);
  60. s_keywords.set("for", TokenType::For);
  61. s_keywords.set("function", TokenType::Function);
  62. s_keywords.set("if", TokenType::If);
  63. s_keywords.set("import", TokenType::Import);
  64. s_keywords.set("in", TokenType::In);
  65. s_keywords.set("instanceof", TokenType::Instanceof);
  66. s_keywords.set("let", TokenType::Let);
  67. s_keywords.set("new", TokenType::New);
  68. s_keywords.set("null", TokenType::NullLiteral);
  69. s_keywords.set("return", TokenType::Return);
  70. s_keywords.set("super", TokenType::Super);
  71. s_keywords.set("switch", TokenType::Switch);
  72. s_keywords.set("this", TokenType::This);
  73. s_keywords.set("throw", TokenType::Throw);
  74. s_keywords.set("true", TokenType::BoolLiteral);
  75. s_keywords.set("try", TokenType::Try);
  76. s_keywords.set("typeof", TokenType::Typeof);
  77. s_keywords.set("var", TokenType::Var);
  78. s_keywords.set("void", TokenType::Void);
  79. s_keywords.set("while", TokenType::While);
  80. s_keywords.set("with", TokenType::With);
  81. s_keywords.set("yield", TokenType::Yield);
  82. }
  83. if (s_three_char_tokens.is_empty()) {
  84. s_three_char_tokens.set("===", TokenType::EqualsEqualsEquals);
  85. s_three_char_tokens.set("!==", TokenType::ExclamationMarkEqualsEquals);
  86. s_three_char_tokens.set("**=", TokenType::DoubleAsteriskEquals);
  87. s_three_char_tokens.set("<<=", TokenType::ShiftLeftEquals);
  88. s_three_char_tokens.set(">>=", TokenType::ShiftRightEquals);
  89. s_three_char_tokens.set("&&=", TokenType::DoubleAmpersandEquals);
  90. s_three_char_tokens.set("||=", TokenType::DoublePipeEquals);
  91. s_three_char_tokens.set("\?\?=", TokenType::DoubleQuestionMarkEquals);
  92. s_three_char_tokens.set(">>>", TokenType::UnsignedShiftRight);
  93. s_three_char_tokens.set("...", TokenType::TripleDot);
  94. }
  95. if (s_two_char_tokens.is_empty()) {
  96. s_two_char_tokens.set("=>", TokenType::Arrow);
  97. s_two_char_tokens.set("+=", TokenType::PlusEquals);
  98. s_two_char_tokens.set("-=", TokenType::MinusEquals);
  99. s_two_char_tokens.set("*=", TokenType::AsteriskEquals);
  100. s_two_char_tokens.set("/=", TokenType::SlashEquals);
  101. s_two_char_tokens.set("%=", TokenType::PercentEquals);
  102. s_two_char_tokens.set("&=", TokenType::AmpersandEquals);
  103. s_two_char_tokens.set("|=", TokenType::PipeEquals);
  104. s_two_char_tokens.set("^=", TokenType::CaretEquals);
  105. s_two_char_tokens.set("&&", TokenType::DoubleAmpersand);
  106. s_two_char_tokens.set("||", TokenType::DoublePipe);
  107. s_two_char_tokens.set("??", TokenType::DoubleQuestionMark);
  108. s_two_char_tokens.set("**", TokenType::DoubleAsterisk);
  109. s_two_char_tokens.set("==", TokenType::EqualsEquals);
  110. s_two_char_tokens.set("<=", TokenType::LessThanEquals);
  111. s_two_char_tokens.set(">=", TokenType::GreaterThanEquals);
  112. s_two_char_tokens.set("!=", TokenType::ExclamationMarkEquals);
  113. s_two_char_tokens.set("--", TokenType::MinusMinus);
  114. s_two_char_tokens.set("++", TokenType::PlusPlus);
  115. s_two_char_tokens.set("<<", TokenType::ShiftLeft);
  116. s_two_char_tokens.set(">>", TokenType::ShiftRight);
  117. s_two_char_tokens.set("?.", TokenType::QuestionMarkPeriod);
  118. }
  119. if (s_single_char_tokens.is_empty()) {
  120. s_single_char_tokens.set('&', TokenType::Ampersand);
  121. s_single_char_tokens.set('*', TokenType::Asterisk);
  122. s_single_char_tokens.set('[', TokenType::BracketOpen);
  123. s_single_char_tokens.set(']', TokenType::BracketClose);
  124. s_single_char_tokens.set('^', TokenType::Caret);
  125. s_single_char_tokens.set(':', TokenType::Colon);
  126. s_single_char_tokens.set(',', TokenType::Comma);
  127. s_single_char_tokens.set('{', TokenType::CurlyOpen);
  128. s_single_char_tokens.set('}', TokenType::CurlyClose);
  129. s_single_char_tokens.set('=', TokenType::Equals);
  130. s_single_char_tokens.set('!', TokenType::ExclamationMark);
  131. s_single_char_tokens.set('-', TokenType::Minus);
  132. s_single_char_tokens.set('(', TokenType::ParenOpen);
  133. s_single_char_tokens.set(')', TokenType::ParenClose);
  134. s_single_char_tokens.set('%', TokenType::Percent);
  135. s_single_char_tokens.set('.', TokenType::Period);
  136. s_single_char_tokens.set('|', TokenType::Pipe);
  137. s_single_char_tokens.set('+', TokenType::Plus);
  138. s_single_char_tokens.set('?', TokenType::QuestionMark);
  139. s_single_char_tokens.set(';', TokenType::Semicolon);
  140. s_single_char_tokens.set('/', TokenType::Slash);
  141. s_single_char_tokens.set('~', TokenType::Tilde);
  142. s_single_char_tokens.set('<', TokenType::LessThan);
  143. s_single_char_tokens.set('>', TokenType::GreaterThan);
  144. }
  145. consume();
  146. }
  147. void Lexer::consume()
  148. {
  149. auto did_reach_eof = [this] {
  150. if (m_position != m_source.length())
  151. return false;
  152. m_position++;
  153. m_line_column++;
  154. m_current_char = EOF;
  155. return true;
  156. };
  157. if (m_position > m_source.length())
  158. return;
  159. if (did_reach_eof())
  160. return;
  161. if (is_line_terminator()) {
  162. #ifdef LEXER_DEBUG
  163. String type;
  164. if (m_current_char == '\n')
  165. type = "LINE FEED";
  166. else if (m_current_char == '\r')
  167. type = "CARRIAGE RETURN";
  168. else if (m_source[m_position + 1] == (char)0xa8)
  169. type = "LINE SEPARATOR";
  170. else
  171. type = "PARAGRAPH SEPARATOR";
  172. dbgln("Found a line terminator: {}", type);
  173. #endif
  174. // This is a three-char line terminator, we need to increase m_position some more.
  175. // We might reach EOF and need to check again.
  176. if (m_current_char != '\n' && m_current_char != '\r') {
  177. m_position += 2;
  178. if (did_reach_eof())
  179. return;
  180. }
  181. // If the previous character is \r and the current one \n we already updated line number
  182. // and column - don't do it again. From https://tc39.es/ecma262/#sec-line-terminators:
  183. // The sequence <CR><LF> is commonly used as a line terminator.
  184. // It should be considered a single SourceCharacter for the purpose of reporting line numbers.
  185. auto second_char_of_crlf = m_position > 1 && m_source[m_position - 2] == '\r' && m_current_char == '\n';
  186. if (!second_char_of_crlf) {
  187. m_line_number++;
  188. m_line_column = 1;
  189. #ifdef LEXER_DEBUG
  190. dbgln("Incremented line number, now at: line {}, column 1", m_line_number);
  191. } else {
  192. dbgln("Previous was CR, this is LF - not incrementing line number again.");
  193. #endif
  194. }
  195. } else {
  196. m_line_column++;
  197. }
  198. m_current_char = m_source[m_position++];
  199. }
  200. bool Lexer::consume_exponent()
  201. {
  202. consume();
  203. if (m_current_char == '-' || m_current_char == '+')
  204. consume();
  205. if (!isdigit(m_current_char))
  206. return false;
  207. while (isdigit(m_current_char)) {
  208. consume();
  209. }
  210. return true;
  211. }
  212. bool Lexer::consume_octal_number()
  213. {
  214. consume();
  215. if (!(m_current_char >= '0' && m_current_char <= '7'))
  216. return false;
  217. while (m_current_char >= '0' && m_current_char <= '7') {
  218. consume();
  219. }
  220. return true;
  221. }
  222. bool Lexer::consume_hexadecimal_number()
  223. {
  224. consume();
  225. if (!isxdigit(m_current_char))
  226. return false;
  227. while (isxdigit(m_current_char))
  228. consume();
  229. return true;
  230. }
  231. bool Lexer::consume_binary_number()
  232. {
  233. consume();
  234. if (!(m_current_char == '0' || m_current_char == '1'))
  235. return false;
  236. while (m_current_char == '0' || m_current_char == '1')
  237. consume();
  238. return true;
  239. }
  240. bool Lexer::match(char a, char b) const
  241. {
  242. if (m_position >= m_source.length())
  243. return false;
  244. return m_current_char == a
  245. && m_source[m_position] == b;
  246. }
  247. bool Lexer::match(char a, char b, char c) const
  248. {
  249. if (m_position + 1 >= m_source.length())
  250. return false;
  251. return m_current_char == a
  252. && m_source[m_position] == b
  253. && m_source[m_position + 1] == c;
  254. }
  255. bool Lexer::match(char a, char b, char c, char d) const
  256. {
  257. if (m_position + 2 >= m_source.length())
  258. return false;
  259. return m_current_char == a
  260. && m_source[m_position] == b
  261. && m_source[m_position + 1] == c
  262. && m_source[m_position + 2] == d;
  263. }
  264. bool Lexer::is_eof() const
  265. {
  266. return m_current_char == EOF;
  267. }
  268. bool Lexer::is_line_terminator() const
  269. {
  270. if (m_current_char == '\n' || m_current_char == '\r')
  271. return true;
  272. if (m_position > 0 && m_position + 1 < m_source.length()) {
  273. auto three_chars_view = m_source.substring_view(m_position - 1, 3);
  274. return (three_chars_view == LINE_SEPARATOR) || (three_chars_view == PARAGRAPH_SEPARATOR);
  275. }
  276. return false;
  277. }
  278. bool Lexer::is_identifier_start() const
  279. {
  280. return isalpha(m_current_char) || m_current_char == '_' || m_current_char == '$';
  281. }
  282. bool Lexer::is_identifier_middle() const
  283. {
  284. return is_identifier_start() || isdigit(m_current_char);
  285. }
  286. bool Lexer::is_line_comment_start(bool line_has_token_yet) const
  287. {
  288. return match('/', '/')
  289. || match('<', '!', '-', '-')
  290. // "-->" is considered a line comment start if the current line is only whitespace and/or
  291. // other block comment(s); or in other words: the current line does not have a token or
  292. // ongoing line comment yet
  293. || (match('-', '-', '>') && !line_has_token_yet);
  294. }
  295. bool Lexer::is_block_comment_start() const
  296. {
  297. return match('/', '*');
  298. }
  299. bool Lexer::is_block_comment_end() const
  300. {
  301. return match('*', '/');
  302. }
  303. bool Lexer::is_numeric_literal_start() const
  304. {
  305. return isdigit(m_current_char) || (m_current_char == '.' && m_position < m_source.length() && isdigit(m_source[m_position]));
  306. }
  307. bool Lexer::slash_means_division() const
  308. {
  309. auto type = m_current_token.type();
  310. return type == TokenType::BigIntLiteral
  311. || type == TokenType::BoolLiteral
  312. || type == TokenType::BracketClose
  313. || type == TokenType::CurlyClose
  314. || type == TokenType::Identifier
  315. || type == TokenType::NullLiteral
  316. || type == TokenType::NumericLiteral
  317. || type == TokenType::ParenClose
  318. || type == TokenType::RegexLiteral
  319. || type == TokenType::StringLiteral
  320. || type == TokenType::TemplateLiteralEnd
  321. || type == TokenType::This;
  322. }
  323. Token Lexer::next()
  324. {
  325. size_t trivia_start = m_position;
  326. auto in_template = !m_template_states.is_empty();
  327. bool line_has_token_yet = m_line_column > 1;
  328. bool unterminated_comment = false;
  329. if (!in_template || m_template_states.last().in_expr) {
  330. // consume whitespace and comments
  331. while (true) {
  332. if (is_line_terminator()) {
  333. line_has_token_yet = false;
  334. do {
  335. consume();
  336. } while (is_line_terminator());
  337. } else if (isspace(m_current_char)) {
  338. do {
  339. consume();
  340. } while (isspace(m_current_char));
  341. } else if (is_line_comment_start(line_has_token_yet)) {
  342. consume();
  343. do {
  344. consume();
  345. } while (!is_eof() && !is_line_terminator());
  346. } else if (is_block_comment_start()) {
  347. consume();
  348. do {
  349. consume();
  350. } while (!is_eof() && !is_block_comment_end());
  351. if (is_eof())
  352. unterminated_comment = true;
  353. consume(); // consume *
  354. if (is_eof())
  355. unterminated_comment = true;
  356. consume(); // consume /
  357. } else {
  358. break;
  359. }
  360. }
  361. }
  362. size_t value_start = m_position;
  363. size_t value_start_line_number = m_line_number;
  364. size_t value_start_column_number = m_line_column;
  365. auto token_type = TokenType::Invalid;
  366. // This is being used to communicate info about invalid tokens to the parser, which then
  367. // can turn that into more specific error messages - instead of us having to make up a
  368. // bunch of Invalid* tokens (bad numeric literals, unterminated comments etc.)
  369. String token_message;
  370. if (m_current_token.type() == TokenType::RegexLiteral && !is_eof() && isalpha(m_current_char)) {
  371. token_type = TokenType::RegexFlags;
  372. while (!is_eof() && isalpha(m_current_char))
  373. consume();
  374. } else if (m_current_char == '`') {
  375. consume();
  376. if (!in_template) {
  377. token_type = TokenType::TemplateLiteralStart;
  378. m_template_states.append({ false, 0 });
  379. } else {
  380. if (m_template_states.last().in_expr) {
  381. m_template_states.append({ false, 0 });
  382. token_type = TokenType::TemplateLiteralStart;
  383. } else {
  384. m_template_states.take_last();
  385. token_type = TokenType::TemplateLiteralEnd;
  386. }
  387. }
  388. } else if (in_template && m_template_states.last().in_expr && m_template_states.last().open_bracket_count == 0 && m_current_char == '}') {
  389. consume();
  390. token_type = TokenType::TemplateLiteralExprEnd;
  391. m_template_states.last().in_expr = false;
  392. } else if (in_template && !m_template_states.last().in_expr) {
  393. if (is_eof()) {
  394. token_type = TokenType::UnterminatedTemplateLiteral;
  395. m_template_states.take_last();
  396. } else if (match('$', '{')) {
  397. token_type = TokenType::TemplateLiteralExprStart;
  398. consume();
  399. consume();
  400. m_template_states.last().in_expr = true;
  401. } else {
  402. while (!match('$', '{') && m_current_char != '`' && !is_eof()) {
  403. if (match('\\', '$') || match('\\', '`'))
  404. consume();
  405. consume();
  406. }
  407. if (is_eof() && !m_template_states.is_empty())
  408. token_type = TokenType::UnterminatedTemplateLiteral;
  409. else
  410. token_type = TokenType::TemplateLiteralString;
  411. }
  412. } else if (is_identifier_start()) {
  413. // identifier or keyword
  414. do {
  415. consume();
  416. } while (is_identifier_middle());
  417. StringView value = m_source.substring_view(value_start - 1, m_position - value_start);
  418. auto it = s_keywords.find(value.hash(), [&](auto& entry) { return entry.key == value; });
  419. if (it == s_keywords.end()) {
  420. token_type = TokenType::Identifier;
  421. } else {
  422. token_type = it->value;
  423. }
  424. } else if (is_numeric_literal_start()) {
  425. token_type = TokenType::NumericLiteral;
  426. bool is_invalid_numeric_literal = false;
  427. if (m_current_char == '0') {
  428. consume();
  429. if (m_current_char == '.') {
  430. // decimal
  431. consume();
  432. while (isdigit(m_current_char))
  433. consume();
  434. if (m_current_char == 'e' || m_current_char == 'E')
  435. is_invalid_numeric_literal = !consume_exponent();
  436. } else if (m_current_char == 'e' || m_current_char == 'E') {
  437. is_invalid_numeric_literal = !consume_exponent();
  438. } else if (m_current_char == 'o' || m_current_char == 'O') {
  439. // octal
  440. is_invalid_numeric_literal = !consume_octal_number();
  441. } else if (m_current_char == 'b' || m_current_char == 'B') {
  442. // binary
  443. is_invalid_numeric_literal = !consume_binary_number();
  444. } else if (m_current_char == 'x' || m_current_char == 'X') {
  445. // hexadecimal
  446. is_invalid_numeric_literal = !consume_hexadecimal_number();
  447. } else if (m_current_char == 'n') {
  448. consume();
  449. token_type = TokenType::BigIntLiteral;
  450. } else if (isdigit(m_current_char)) {
  451. // octal without '0o' prefix. Forbidden in 'strict mode'
  452. do {
  453. consume();
  454. } while (isdigit(m_current_char));
  455. }
  456. } else {
  457. // 1...9 or period
  458. while (isdigit(m_current_char))
  459. consume();
  460. if (m_current_char == 'n') {
  461. consume();
  462. token_type = TokenType::BigIntLiteral;
  463. } else {
  464. if (m_current_char == '.') {
  465. consume();
  466. while (isdigit(m_current_char))
  467. consume();
  468. }
  469. if (m_current_char == 'e' || m_current_char == 'E')
  470. is_invalid_numeric_literal = !consume_exponent();
  471. }
  472. }
  473. if (is_invalid_numeric_literal) {
  474. token_type = TokenType::Invalid;
  475. token_message = "Invalid numeric literal";
  476. }
  477. } else if (m_current_char == '"' || m_current_char == '\'') {
  478. char stop_char = m_current_char;
  479. consume();
  480. // Note: LS/PS line terminators are allowed in string literals.
  481. while (m_current_char != stop_char && m_current_char != '\r' && m_current_char != '\n' && !is_eof()) {
  482. if (m_current_char == '\\') {
  483. consume();
  484. }
  485. consume();
  486. }
  487. if (m_current_char != stop_char) {
  488. token_type = TokenType::UnterminatedStringLiteral;
  489. } else {
  490. consume();
  491. token_type = TokenType::StringLiteral;
  492. }
  493. } else if (m_current_char == '/' && !slash_means_division()) {
  494. consume();
  495. token_type = TokenType::RegexLiteral;
  496. while (!is_eof()) {
  497. if (m_current_char == '[') {
  498. m_regex_is_in_character_class = true;
  499. } else if (m_current_char == ']') {
  500. m_regex_is_in_character_class = false;
  501. } else if (!m_regex_is_in_character_class && m_current_char == '/') {
  502. break;
  503. }
  504. if (match('\\', '/') || match('\\', '[') || match('\\', '\\') || (m_regex_is_in_character_class && match('\\', ']')))
  505. consume();
  506. consume();
  507. }
  508. if (is_eof()) {
  509. token_type = TokenType::UnterminatedRegexLiteral;
  510. } else {
  511. consume();
  512. }
  513. } else if (m_current_char == EOF) {
  514. if (unterminated_comment) {
  515. token_type = TokenType::Invalid;
  516. token_message = "Unterminated multi-line comment";
  517. } else {
  518. token_type = TokenType::Eof;
  519. }
  520. } else {
  521. // There is only one four-char operator: >>>=
  522. bool found_four_char_token = false;
  523. if (match('>', '>', '>', '=')) {
  524. found_four_char_token = true;
  525. consume();
  526. consume();
  527. consume();
  528. consume();
  529. token_type = TokenType::UnsignedShiftRightEquals;
  530. }
  531. bool found_three_char_token = false;
  532. if (!found_four_char_token && m_position + 1 < m_source.length()) {
  533. auto three_chars_view = m_source.substring_view(m_position - 1, 3);
  534. auto it = s_three_char_tokens.find(three_chars_view.hash(), [&](auto& entry) { return entry.key == three_chars_view; });
  535. if (it != s_three_char_tokens.end()) {
  536. found_three_char_token = true;
  537. consume();
  538. consume();
  539. consume();
  540. token_type = it->value;
  541. }
  542. }
  543. bool found_two_char_token = false;
  544. if (!found_four_char_token && !found_three_char_token && m_position < m_source.length()) {
  545. auto two_chars_view = m_source.substring_view(m_position - 1, 2);
  546. auto it = s_two_char_tokens.find(two_chars_view.hash(), [&](auto& entry) { return entry.key == two_chars_view; });
  547. if (it != s_two_char_tokens.end()) {
  548. found_two_char_token = true;
  549. consume();
  550. consume();
  551. token_type = it->value;
  552. }
  553. }
  554. bool found_one_char_token = false;
  555. if (!found_four_char_token && !found_three_char_token && !found_two_char_token) {
  556. auto it = s_single_char_tokens.find(m_current_char);
  557. if (it != s_single_char_tokens.end()) {
  558. found_one_char_token = true;
  559. consume();
  560. token_type = it->value;
  561. }
  562. }
  563. if (!found_four_char_token && !found_three_char_token && !found_two_char_token && !found_one_char_token) {
  564. consume();
  565. token_type = TokenType::Invalid;
  566. }
  567. }
  568. if (!m_template_states.is_empty() && m_template_states.last().in_expr) {
  569. if (token_type == TokenType::CurlyOpen) {
  570. m_template_states.last().open_bracket_count++;
  571. } else if (token_type == TokenType::CurlyClose) {
  572. m_template_states.last().open_bracket_count--;
  573. }
  574. }
  575. m_current_token = Token(
  576. token_type,
  577. token_message,
  578. m_source.substring_view(trivia_start - 1, value_start - trivia_start),
  579. m_source.substring_view(value_start - 1, m_position - value_start),
  580. value_start_line_number,
  581. value_start_column_number);
  582. #ifdef LEXER_DEBUG
  583. dbgln("------------------------------");
  584. dbgln("Token: {}", m_current_token.name());
  585. dbgln("Trivia: _{}_", m_current_token.trivia());
  586. dbgln("Value: _{}_", m_current_token.value());
  587. dbgln("Line: {}, Column: {}", m_current_token.line_number(), m_current_token.line_column());
  588. dbgln("------------------------------");
  589. #endif
  590. return m_current_token;
  591. }
  592. }