Lexer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. dbg() << "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. dbg() << "Incremented line number, now at: line " << m_line_number << ", column 1";
  191. } else {
  192. dbg() << "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 + 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() const
  287. {
  288. return match('/', '/') || match('<', '!', '-', '-') || match('-', '-', '>');
  289. }
  290. bool Lexer::is_block_comment_start() const
  291. {
  292. return match('/', '*');
  293. }
  294. bool Lexer::is_block_comment_end() const
  295. {
  296. return match('*', '/');
  297. }
  298. bool Lexer::is_numeric_literal_start() const
  299. {
  300. return isdigit(m_current_char) || (m_current_char == '.' && m_position < m_source.length() && isdigit(m_source[m_position]));
  301. }
  302. bool Lexer::slash_means_division() const
  303. {
  304. auto type = m_current_token.type();
  305. return type == TokenType::BigIntLiteral
  306. || type == TokenType::BoolLiteral
  307. || type == TokenType::BracketClose
  308. || type == TokenType::CurlyClose
  309. || type == TokenType::Identifier
  310. || type == TokenType::NullLiteral
  311. || type == TokenType::NumericLiteral
  312. || type == TokenType::ParenClose
  313. || type == TokenType::RegexLiteral
  314. || type == TokenType::StringLiteral
  315. || type == TokenType::TemplateLiteralEnd
  316. || type == TokenType::This;
  317. }
  318. Token Lexer::next()
  319. {
  320. size_t trivia_start = m_position;
  321. auto in_template = !m_template_states.is_empty();
  322. if (!in_template || m_template_states.last().in_expr) {
  323. // consume whitespace and comments
  324. while (true) {
  325. if (isspace(m_current_char) || is_line_terminator()) {
  326. do {
  327. consume();
  328. } while (isspace(m_current_char) || is_line_terminator());
  329. } else if (is_line_comment_start()) {
  330. consume();
  331. do {
  332. consume();
  333. } while (!is_eof() && !is_line_terminator());
  334. } else if (is_block_comment_start()) {
  335. consume();
  336. do {
  337. consume();
  338. } while (!is_eof() && !is_block_comment_end());
  339. consume(); // consume *
  340. consume(); // consume /
  341. } else {
  342. break;
  343. }
  344. }
  345. }
  346. size_t value_start = m_position;
  347. size_t value_start_line_number = m_line_number;
  348. size_t value_start_column_number = m_line_column;
  349. auto token_type = TokenType::Invalid;
  350. // This is being used to communicate info about invalid tokens to the parser, which then
  351. // can turn that into more specific error messages - instead of us having to make up a
  352. // bunch of Invalid* tokens (bad numeric literals, unterminated comments etc.)
  353. String token_message;
  354. if (m_current_token.type() == TokenType::RegexLiteral && !is_eof() && isalpha(m_current_char)) {
  355. token_type = TokenType::RegexFlags;
  356. while (!is_eof() && isalpha(m_current_char))
  357. consume();
  358. } else if (m_current_char == '`') {
  359. consume();
  360. if (!in_template) {
  361. token_type = TokenType::TemplateLiteralStart;
  362. m_template_states.append({ false, 0 });
  363. } else {
  364. if (m_template_states.last().in_expr) {
  365. m_template_states.append({ false, 0 });
  366. token_type = TokenType::TemplateLiteralStart;
  367. } else {
  368. m_template_states.take_last();
  369. token_type = TokenType::TemplateLiteralEnd;
  370. }
  371. }
  372. } else if (in_template && m_template_states.last().in_expr && m_template_states.last().open_bracket_count == 0 && m_current_char == '}') {
  373. consume();
  374. token_type = TokenType::TemplateLiteralExprEnd;
  375. m_template_states.last().in_expr = false;
  376. } else if (in_template && !m_template_states.last().in_expr) {
  377. if (is_eof()) {
  378. token_type = TokenType::UnterminatedTemplateLiteral;
  379. m_template_states.take_last();
  380. } else if (match('$', '{')) {
  381. token_type = TokenType::TemplateLiteralExprStart;
  382. consume();
  383. consume();
  384. m_template_states.last().in_expr = true;
  385. } else {
  386. while (!match('$', '{') && m_current_char != '`' && !is_eof()) {
  387. if (match('\\', '$') || match('\\', '`'))
  388. consume();
  389. consume();
  390. }
  391. token_type = TokenType::TemplateLiteralString;
  392. }
  393. } else if (is_identifier_start()) {
  394. // identifier or keyword
  395. do {
  396. consume();
  397. } while (is_identifier_middle());
  398. StringView value = m_source.substring_view(value_start - 1, m_position - value_start);
  399. auto it = s_keywords.find(value.hash(), [&](auto& entry) { return entry.key == value; });
  400. if (it == s_keywords.end()) {
  401. token_type = TokenType::Identifier;
  402. } else {
  403. token_type = it->value;
  404. }
  405. } else if (is_numeric_literal_start()) {
  406. token_type = TokenType::NumericLiteral;
  407. bool is_invalid_numeric_literal = false;
  408. if (m_current_char == '0') {
  409. consume();
  410. if (m_current_char == '.') {
  411. // decimal
  412. consume();
  413. while (isdigit(m_current_char))
  414. consume();
  415. if (m_current_char == 'e' || m_current_char == 'E')
  416. is_invalid_numeric_literal = !consume_exponent();
  417. } else if (m_current_char == 'e' || m_current_char == 'E') {
  418. is_invalid_numeric_literal = !consume_exponent();
  419. } else if (m_current_char == 'o' || m_current_char == 'O') {
  420. // octal
  421. is_invalid_numeric_literal = !consume_octal_number();
  422. } else if (m_current_char == 'b' || m_current_char == 'B') {
  423. // binary
  424. is_invalid_numeric_literal = !consume_binary_number();
  425. } else if (m_current_char == 'x' || m_current_char == 'X') {
  426. // hexadecimal
  427. is_invalid_numeric_literal = !consume_hexadecimal_number();
  428. } else if (m_current_char == 'n') {
  429. consume();
  430. token_type = TokenType::BigIntLiteral;
  431. } else if (isdigit(m_current_char)) {
  432. // octal without '0o' prefix. Forbidden in 'strict mode'
  433. do {
  434. consume();
  435. } while (isdigit(m_current_char));
  436. }
  437. } else {
  438. // 1...9 or period
  439. while (isdigit(m_current_char))
  440. consume();
  441. if (m_current_char == 'n') {
  442. consume();
  443. token_type = TokenType::BigIntLiteral;
  444. } else {
  445. if (m_current_char == '.') {
  446. consume();
  447. while (isdigit(m_current_char))
  448. consume();
  449. }
  450. if (m_current_char == 'e' || m_current_char == 'E')
  451. is_invalid_numeric_literal = !consume_exponent();
  452. }
  453. }
  454. if (is_invalid_numeric_literal)
  455. token_type = TokenType::Invalid;
  456. } else if (m_current_char == '"' || m_current_char == '\'') {
  457. char stop_char = m_current_char;
  458. consume();
  459. // Note: LS/PS line terminators are allowed in string literals.
  460. while (m_current_char != stop_char && m_current_char != '\r' && m_current_char != '\n' && !is_eof()) {
  461. if (m_current_char == '\\') {
  462. consume();
  463. }
  464. consume();
  465. }
  466. if (m_current_char != stop_char) {
  467. token_type = TokenType::UnterminatedStringLiteral;
  468. } else {
  469. consume();
  470. token_type = TokenType::StringLiteral;
  471. }
  472. } else if (m_current_char == '/' && !slash_means_division()) {
  473. consume();
  474. token_type = TokenType::RegexLiteral;
  475. while (!is_eof()) {
  476. if (m_current_char == '[') {
  477. m_regex_is_in_character_class = true;
  478. } else if (m_current_char == ']') {
  479. m_regex_is_in_character_class = false;
  480. } else if (!m_regex_is_in_character_class && m_current_char == '/') {
  481. break;
  482. }
  483. if (match('\\', '/') || match('\\', '[') || match('\\', '\\') || (m_regex_is_in_character_class && match('\\', ']')))
  484. consume();
  485. consume();
  486. }
  487. if (is_eof()) {
  488. token_type = TokenType::UnterminatedRegexLiteral;
  489. } else {
  490. consume();
  491. }
  492. } else if (m_current_char == EOF) {
  493. token_type = TokenType::Eof;
  494. } else {
  495. // There is only one four-char operator: >>>=
  496. bool found_four_char_token = false;
  497. if (match('>', '>', '>', '=')) {
  498. found_four_char_token = true;
  499. consume();
  500. consume();
  501. consume();
  502. consume();
  503. token_type = TokenType::UnsignedShiftRightEquals;
  504. }
  505. bool found_three_char_token = false;
  506. if (!found_four_char_token && m_position + 1 < m_source.length()) {
  507. auto three_chars_view = m_source.substring_view(m_position - 1, 3);
  508. auto it = s_three_char_tokens.find(three_chars_view.hash(), [&](auto& entry) { return entry.key == three_chars_view; });
  509. if (it != s_three_char_tokens.end()) {
  510. found_three_char_token = true;
  511. consume();
  512. consume();
  513. consume();
  514. token_type = it->value;
  515. }
  516. }
  517. bool found_two_char_token = false;
  518. if (!found_four_char_token && !found_three_char_token && m_position < m_source.length()) {
  519. auto two_chars_view = m_source.substring_view(m_position - 1, 2);
  520. auto it = s_two_char_tokens.find(two_chars_view.hash(), [&](auto& entry) { return entry.key == two_chars_view; });
  521. if (it != s_two_char_tokens.end()) {
  522. found_two_char_token = true;
  523. consume();
  524. consume();
  525. token_type = it->value;
  526. }
  527. }
  528. bool found_one_char_token = false;
  529. if (!found_four_char_token && !found_three_char_token && !found_two_char_token) {
  530. auto it = s_single_char_tokens.find(m_current_char);
  531. if (it != s_single_char_tokens.end()) {
  532. found_one_char_token = true;
  533. consume();
  534. token_type = it->value;
  535. }
  536. }
  537. if (!found_four_char_token && !found_three_char_token && !found_two_char_token && !found_one_char_token) {
  538. consume();
  539. token_type = TokenType::Invalid;
  540. }
  541. }
  542. if (!m_template_states.is_empty() && m_template_states.last().in_expr) {
  543. if (token_type == TokenType::CurlyOpen) {
  544. m_template_states.last().open_bracket_count++;
  545. } else if (token_type == TokenType::CurlyClose) {
  546. m_template_states.last().open_bracket_count--;
  547. }
  548. }
  549. m_current_token = Token(
  550. token_type,
  551. token_message,
  552. m_source.substring_view(trivia_start - 1, value_start - trivia_start),
  553. m_source.substring_view(value_start - 1, m_position - value_start),
  554. value_start_line_number,
  555. value_start_column_number);
  556. #ifdef LEXER_DEBUG
  557. dbg() << "------------------------------";
  558. dbg() << "Token: " << m_current_token.name();
  559. dbg() << "Trivia: _" << m_current_token.trivia() << "_";
  560. dbg() << "Value: _" << m_current_token.value() << "_";
  561. dbg() << "Line: " << m_current_token.line_number() << ", Column: " << m_current_token.line_column();
  562. dbg() << "------------------------------";
  563. #endif
  564. return m_current_token;
  565. }
  566. }