Lexer.cpp 24 KB

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