Lexer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. if (m_current_token.type() == TokenType::RegexLiteral && !is_eof() && isalpha(m_current_char)) {
  351. token_type = TokenType::RegexFlags;
  352. while (!is_eof() && isalpha(m_current_char))
  353. consume();
  354. } else if (m_current_char == '`') {
  355. consume();
  356. if (!in_template) {
  357. token_type = TokenType::TemplateLiteralStart;
  358. m_template_states.append({ false, 0 });
  359. } else {
  360. if (m_template_states.last().in_expr) {
  361. m_template_states.append({ false, 0 });
  362. token_type = TokenType::TemplateLiteralStart;
  363. } else {
  364. m_template_states.take_last();
  365. token_type = TokenType::TemplateLiteralEnd;
  366. }
  367. }
  368. } else if (in_template && m_template_states.last().in_expr && m_template_states.last().open_bracket_count == 0 && m_current_char == '}') {
  369. consume();
  370. token_type = TokenType::TemplateLiteralExprEnd;
  371. m_template_states.last().in_expr = false;
  372. } else if (in_template && !m_template_states.last().in_expr) {
  373. if (is_eof()) {
  374. token_type = TokenType::UnterminatedTemplateLiteral;
  375. m_template_states.take_last();
  376. } else if (match('$', '{')) {
  377. token_type = TokenType::TemplateLiteralExprStart;
  378. consume();
  379. consume();
  380. m_template_states.last().in_expr = true;
  381. } else {
  382. while (!match('$', '{') && m_current_char != '`' && !is_eof()) {
  383. if (match('\\', '$') || match('\\', '`'))
  384. consume();
  385. consume();
  386. }
  387. token_type = TokenType::TemplateLiteralString;
  388. }
  389. } else if (is_identifier_start()) {
  390. // identifier or keyword
  391. do {
  392. consume();
  393. } while (is_identifier_middle());
  394. StringView value = m_source.substring_view(value_start - 1, m_position - value_start);
  395. auto it = s_keywords.find(value.hash(), [&](auto& entry) { return entry.key == value; });
  396. if (it == s_keywords.end()) {
  397. token_type = TokenType::Identifier;
  398. } else {
  399. token_type = it->value;
  400. }
  401. } else if (is_numeric_literal_start()) {
  402. token_type = TokenType::NumericLiteral;
  403. bool is_invalid_numeric_literal = false;
  404. if (m_current_char == '0') {
  405. consume();
  406. if (m_current_char == '.') {
  407. // decimal
  408. consume();
  409. while (isdigit(m_current_char))
  410. consume();
  411. if (m_current_char == 'e' || m_current_char == 'E')
  412. is_invalid_numeric_literal = !consume_exponent();
  413. } else if (m_current_char == 'e' || m_current_char == 'E') {
  414. is_invalid_numeric_literal = !consume_exponent();
  415. } else if (m_current_char == 'o' || m_current_char == 'O') {
  416. // octal
  417. is_invalid_numeric_literal = !consume_octal_number();
  418. } else if (m_current_char == 'b' || m_current_char == 'B') {
  419. // binary
  420. is_invalid_numeric_literal = !consume_binary_number();
  421. } else if (m_current_char == 'x' || m_current_char == 'X') {
  422. // hexadecimal
  423. is_invalid_numeric_literal = !consume_hexadecimal_number();
  424. } else if (m_current_char == 'n') {
  425. consume();
  426. token_type = TokenType::BigIntLiteral;
  427. } else if (isdigit(m_current_char)) {
  428. // octal without '0o' prefix. Forbidden in 'strict mode'
  429. do {
  430. consume();
  431. } while (isdigit(m_current_char));
  432. }
  433. } else {
  434. // 1...9 or period
  435. while (isdigit(m_current_char))
  436. consume();
  437. if (m_current_char == 'n') {
  438. consume();
  439. token_type = TokenType::BigIntLiteral;
  440. } else {
  441. if (m_current_char == '.') {
  442. consume();
  443. while (isdigit(m_current_char))
  444. consume();
  445. }
  446. if (m_current_char == 'e' || m_current_char == 'E')
  447. is_invalid_numeric_literal = !consume_exponent();
  448. }
  449. }
  450. if (is_invalid_numeric_literal)
  451. token_type = TokenType::Invalid;
  452. } else if (m_current_char == '"' || m_current_char == '\'') {
  453. char stop_char = m_current_char;
  454. consume();
  455. // Note: LS/PS line terminators are allowed in string literals.
  456. while (m_current_char != stop_char && m_current_char != '\r' && m_current_char != '\n' && !is_eof()) {
  457. if (m_current_char == '\\') {
  458. consume();
  459. }
  460. consume();
  461. }
  462. if (m_current_char != stop_char) {
  463. token_type = TokenType::UnterminatedStringLiteral;
  464. } else {
  465. consume();
  466. token_type = TokenType::StringLiteral;
  467. }
  468. } else if (m_current_char == '/' && !slash_means_division()) {
  469. consume();
  470. token_type = TokenType::RegexLiteral;
  471. while (!is_eof()) {
  472. if (m_current_char == '[') {
  473. m_regex_is_in_character_class = true;
  474. } else if (m_current_char == ']') {
  475. m_regex_is_in_character_class = false;
  476. } else if (!m_regex_is_in_character_class && m_current_char == '/') {
  477. break;
  478. }
  479. if (match('\\', '/') || match('\\', '[') || match('\\', '\\') || (m_regex_is_in_character_class && match('\\', ']')))
  480. consume();
  481. consume();
  482. }
  483. if (is_eof()) {
  484. token_type = TokenType::UnterminatedRegexLiteral;
  485. } else {
  486. consume();
  487. }
  488. } else if (m_current_char == EOF) {
  489. token_type = TokenType::Eof;
  490. } else {
  491. // There is only one four-char operator: >>>=
  492. bool found_four_char_token = false;
  493. if (match('>', '>', '>', '=')) {
  494. found_four_char_token = true;
  495. consume();
  496. consume();
  497. consume();
  498. consume();
  499. token_type = TokenType::UnsignedShiftRightEquals;
  500. }
  501. bool found_three_char_token = false;
  502. if (!found_four_char_token && m_position + 1 < m_source.length()) {
  503. auto three_chars_view = m_source.substring_view(m_position - 1, 3);
  504. auto it = s_three_char_tokens.find(three_chars_view.hash(), [&](auto& entry) { return entry.key == three_chars_view; });
  505. if (it != s_three_char_tokens.end()) {
  506. found_three_char_token = true;
  507. consume();
  508. consume();
  509. consume();
  510. token_type = it->value;
  511. }
  512. }
  513. bool found_two_char_token = false;
  514. if (!found_four_char_token && !found_three_char_token && m_position < m_source.length()) {
  515. auto two_chars_view = m_source.substring_view(m_position - 1, 2);
  516. auto it = s_two_char_tokens.find(two_chars_view.hash(), [&](auto& entry) { return entry.key == two_chars_view; });
  517. if (it != s_two_char_tokens.end()) {
  518. found_two_char_token = true;
  519. consume();
  520. consume();
  521. token_type = it->value;
  522. }
  523. }
  524. bool found_one_char_token = false;
  525. if (!found_four_char_token && !found_three_char_token && !found_two_char_token) {
  526. auto it = s_single_char_tokens.find(m_current_char);
  527. if (it != s_single_char_tokens.end()) {
  528. found_one_char_token = true;
  529. consume();
  530. token_type = it->value;
  531. }
  532. }
  533. if (!found_four_char_token && !found_three_char_token && !found_two_char_token && !found_one_char_token) {
  534. consume();
  535. token_type = TokenType::Invalid;
  536. }
  537. }
  538. if (!m_template_states.is_empty() && m_template_states.last().in_expr) {
  539. if (token_type == TokenType::CurlyOpen) {
  540. m_template_states.last().open_bracket_count++;
  541. } else if (token_type == TokenType::CurlyClose) {
  542. m_template_states.last().open_bracket_count--;
  543. }
  544. }
  545. m_current_token = Token(
  546. token_type,
  547. m_source.substring_view(trivia_start - 1, value_start - trivia_start),
  548. m_source.substring_view(value_start - 1, m_position - value_start),
  549. value_start_line_number,
  550. value_start_column_number);
  551. #ifdef LEXER_DEBUG
  552. dbg() << "------------------------------";
  553. dbg() << "Token: " << m_current_token.name();
  554. dbg() << "Trivia: _" << m_current_token.trivia() << "_";
  555. dbg() << "Value: _" << m_current_token.value() << "_";
  556. dbg() << "Line: " << m_current_token.line_number() << ", Column: " << m_current_token.line_column();
  557. dbg() << "------------------------------";
  558. #endif
  559. return m_current_token;
  560. }
  561. }