Tokenizer.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/Debug.h>
  9. #include <AK/FloatingPointStringConversions.h>
  10. #include <AK/SourceLocation.h>
  11. #include <AK/Vector.h>
  12. #include <LibTextCodec/Decoder.h>
  13. #include <LibWeb/CSS/Parser/Tokenizer.h>
  14. #include <LibWeb/Infra/Strings.h>
  15. namespace Web::CSS::Parser {
  16. // U+FFFD REPLACEMENT CHARACTER (�)
  17. #define REPLACEMENT_CHARACTER 0xFFFD
  18. static constexpr u32 TOKENIZER_EOF = 0xFFFFFFFF;
  19. static inline void log_parse_error(SourceLocation const& location = SourceLocation::current())
  20. {
  21. dbgln_if(CSS_TOKENIZER_DEBUG, "Parse error (css tokenization) {} ", location);
  22. }
  23. static inline bool is_eof(u32 code_point)
  24. {
  25. return code_point == TOKENIZER_EOF;
  26. }
  27. static inline bool is_quotation_mark(u32 code_point)
  28. {
  29. return code_point == 0x22;
  30. }
  31. static inline bool is_greater_than_maximum_allowed_code_point(u32 code_point)
  32. {
  33. return code_point > 0x10FFFF;
  34. }
  35. static inline bool is_low_line(u32 code_point)
  36. {
  37. return code_point == 0x5F;
  38. }
  39. // https://www.w3.org/TR/css-syntax-3/#ident-start-code-point
  40. static inline bool is_ident_start_code_point(u32 code_point)
  41. {
  42. // FIXME: We use !is_ascii() for "non-ASCII code point" in the spec, but it's not quite right -
  43. // it treats EOF as a valid! The spec also lacks a definition of code point. For now, the
  44. // !is_eof() check is a hack, but it should work.
  45. return !is_eof(code_point) && (is_ascii_alpha(code_point) || !is_ascii(code_point) || is_low_line(code_point));
  46. }
  47. static inline bool is_hyphen_minus(u32 code_point)
  48. {
  49. return code_point == 0x2D;
  50. }
  51. // https://www.w3.org/TR/css-syntax-3/#ident-code-point
  52. static inline bool is_ident_code_point(u32 code_point)
  53. {
  54. return is_ident_start_code_point(code_point) || is_ascii_digit(code_point) || is_hyphen_minus(code_point);
  55. }
  56. static inline bool is_non_printable(u32 code_point)
  57. {
  58. return code_point <= 0x8 || code_point == 0xB || (code_point >= 0xE && code_point <= 0x1F) || code_point == 0x7F;
  59. }
  60. static inline bool is_number_sign(u32 code_point)
  61. {
  62. return code_point == 0x23;
  63. }
  64. static inline bool is_reverse_solidus(u32 code_point)
  65. {
  66. return code_point == 0x5C;
  67. }
  68. static inline bool is_apostrophe(u32 code_point)
  69. {
  70. return code_point == 0x27;
  71. }
  72. static inline bool is_left_paren(u32 code_point)
  73. {
  74. return code_point == 0x28;
  75. }
  76. static inline bool is_right_paren(u32 code_point)
  77. {
  78. return code_point == 0x29;
  79. }
  80. static inline bool is_plus_sign(u32 code_point)
  81. {
  82. return code_point == 0x2B;
  83. }
  84. static inline bool is_comma(u32 code_point)
  85. {
  86. return code_point == 0x2C;
  87. }
  88. static inline bool is_full_stop(u32 code_point)
  89. {
  90. return code_point == 0x2E;
  91. }
  92. static inline bool is_newline(u32 code_point)
  93. {
  94. return code_point == 0xA;
  95. }
  96. static inline bool is_asterisk(u32 code_point)
  97. {
  98. return code_point == 0x2A;
  99. }
  100. static inline bool is_solidus(u32 code_point)
  101. {
  102. return code_point == 0x2F;
  103. }
  104. static inline bool is_colon(u32 code_point)
  105. {
  106. return code_point == 0x3A;
  107. }
  108. static inline bool is_semicolon(u32 code_point)
  109. {
  110. return code_point == 0x3B;
  111. }
  112. static inline bool is_less_than_sign(u32 code_point)
  113. {
  114. return code_point == 0x3C;
  115. }
  116. static inline bool is_greater_than_sign(u32 code_point)
  117. {
  118. return code_point == 0x3E;
  119. }
  120. static inline bool is_at(u32 code_point)
  121. {
  122. return code_point == 0x40;
  123. }
  124. static inline bool is_open_square_bracket(u32 code_point)
  125. {
  126. return code_point == 0x5B;
  127. }
  128. static inline bool is_closed_square_bracket(u32 code_point)
  129. {
  130. return code_point == 0x5D;
  131. }
  132. static inline bool is_open_curly_bracket(u32 code_point)
  133. {
  134. return code_point == 0x7B;
  135. }
  136. static inline bool is_closed_curly_bracket(u32 code_point)
  137. {
  138. return code_point == 0x7D;
  139. }
  140. static inline bool is_whitespace(u32 code_point)
  141. {
  142. return code_point == 0x9 || code_point == 0xA || code_point == 0x20;
  143. }
  144. static inline bool is_percent(u32 code_point)
  145. {
  146. return code_point == 0x25;
  147. }
  148. static inline bool is_exclamation_mark(u32 code_point)
  149. {
  150. return code_point == 0x21;
  151. }
  152. static inline bool is_e(u32 code_point)
  153. {
  154. return code_point == 0x65;
  155. }
  156. static inline bool is_E(u32 code_point)
  157. {
  158. return code_point == 0x45;
  159. }
  160. Tokenizer::Tokenizer(StringView input, StringView encoding)
  161. {
  162. // https://www.w3.org/TR/css-syntax-3/#css-filter-code-points
  163. auto filter_code_points = [](StringView input, auto encoding) -> ErrorOr<String> {
  164. auto decoder = TextCodec::decoder_for(encoding);
  165. VERIFY(decoder.has_value());
  166. StringBuilder builder { input.length() };
  167. bool last_was_carriage_return = false;
  168. // To filter code points from a stream of (unfiltered) code points input:
  169. decoder->process(input, [&builder, &last_was_carriage_return](u32 code_point) {
  170. // Replace any U+000D CARRIAGE RETURN (CR) code points,
  171. // U+000C FORM FEED (FF) code points,
  172. // or pairs of U+000D CARRIAGE RETURN (CR) followed by U+000A LINE FEED (LF)
  173. // in input by a single U+000A LINE FEED (LF) code point.
  174. if (code_point == '\r') {
  175. if (last_was_carriage_return) {
  176. builder.append('\n');
  177. } else {
  178. last_was_carriage_return = true;
  179. }
  180. } else {
  181. if (last_was_carriage_return)
  182. builder.append('\n');
  183. if (code_point == '\n') {
  184. if (!last_was_carriage_return)
  185. builder.append('\n');
  186. } else if (code_point == '\f') {
  187. builder.append('\n');
  188. // Replace any U+0000 NULL or surrogate code points in input with U+FFFD REPLACEMENT CHARACTER (�).
  189. } else if (code_point == 0x00 || (code_point >= 0xD800 && code_point <= 0xDFFF)) {
  190. builder.append_code_point(REPLACEMENT_CHARACTER);
  191. } else {
  192. builder.append_code_point(code_point);
  193. }
  194. last_was_carriage_return = false;
  195. }
  196. });
  197. return builder.to_string();
  198. };
  199. m_decoded_input = filter_code_points(input, encoding).release_value_but_fixme_should_propagate_errors();
  200. m_utf8_view = Utf8View(m_decoded_input);
  201. m_utf8_iterator = m_utf8_view.begin();
  202. }
  203. Vector<Token> Tokenizer::parse()
  204. {
  205. Vector<Token> tokens;
  206. for (;;) {
  207. auto token_start = m_position;
  208. auto token = consume_a_token();
  209. token.m_start_position = token_start;
  210. token.m_end_position = m_position;
  211. tokens.append(token);
  212. if (token.is(Token::Type::EndOfFile)) {
  213. return tokens;
  214. }
  215. }
  216. }
  217. u32 Tokenizer::next_code_point()
  218. {
  219. if (m_utf8_iterator == m_utf8_view.end())
  220. return TOKENIZER_EOF;
  221. m_prev_utf8_iterator = m_utf8_iterator;
  222. ++m_utf8_iterator;
  223. auto code_point = *m_prev_utf8_iterator;
  224. m_prev_position = m_position;
  225. if (is_newline(code_point)) {
  226. m_position.line++;
  227. m_position.column = 0;
  228. } else {
  229. m_position.column++;
  230. }
  231. dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Next code_point: {:d}", code_point);
  232. return code_point;
  233. }
  234. u32 Tokenizer::peek_code_point(size_t offset) const
  235. {
  236. auto it = m_utf8_iterator;
  237. for (size_t i = 0; i < offset && it != m_utf8_view.end(); ++i)
  238. ++it;
  239. if (it == m_utf8_view.end())
  240. return TOKENIZER_EOF;
  241. dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Peek code_point: {:d}", *m_prev_utf8_iterator);
  242. return *it;
  243. }
  244. U32Twin Tokenizer::peek_twin() const
  245. {
  246. U32Twin values { TOKENIZER_EOF, TOKENIZER_EOF };
  247. auto it = m_utf8_iterator;
  248. for (size_t i = 0; i < 2 && it != m_utf8_view.end(); ++i) {
  249. values.set(i, *it);
  250. ++it;
  251. }
  252. dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Peek twin: {:d},{:d}", values.first, values.second);
  253. return values;
  254. }
  255. U32Triplet Tokenizer::peek_triplet() const
  256. {
  257. U32Triplet values { TOKENIZER_EOF, TOKENIZER_EOF, TOKENIZER_EOF };
  258. auto it = m_utf8_iterator;
  259. for (size_t i = 0; i < 3 && it != m_utf8_view.end(); ++i) {
  260. values.set(i, *it);
  261. ++it;
  262. }
  263. dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Peek triplet: {:d},{:d},{:d}", values.first, values.second, values.third);
  264. return values;
  265. }
  266. U32Twin Tokenizer::start_of_input_stream_twin()
  267. {
  268. U32Twin twin;
  269. // FIXME: Reconsuming just to read the current code point again is weird.
  270. reconsume_current_input_code_point();
  271. twin.first = next_code_point();
  272. twin.second = peek_code_point();
  273. return twin;
  274. }
  275. U32Triplet Tokenizer::start_of_input_stream_triplet()
  276. {
  277. U32Triplet triplet;
  278. // FIXME: Reconsuming just to read the current code point again is weird.
  279. reconsume_current_input_code_point();
  280. triplet.first = next_code_point();
  281. auto next_two = peek_twin();
  282. triplet.second = next_two.first;
  283. triplet.third = next_two.second;
  284. return triplet;
  285. }
  286. Token Tokenizer::create_new_token(Token::Type type)
  287. {
  288. Token token = {};
  289. token.m_type = type;
  290. return token;
  291. }
  292. Token Tokenizer::create_eof_token()
  293. {
  294. return create_new_token(Token::Type::EndOfFile);
  295. }
  296. Token Tokenizer::create_value_token(Token::Type type, FlyString&& value)
  297. {
  298. Token token;
  299. token.m_type = type;
  300. token.m_value = move(value);
  301. return token;
  302. }
  303. Token Tokenizer::create_value_token(Token::Type type, u32 value)
  304. {
  305. Token token = {};
  306. token.m_type = type;
  307. token.m_value = String::from_code_point(value);
  308. return token;
  309. }
  310. // https://www.w3.org/TR/css-syntax-3/#consume-escaped-code-point
  311. u32 Tokenizer::consume_escaped_code_point()
  312. {
  313. // This section describes how to consume an escaped code point.
  314. // It assumes that the U+005C REVERSE SOLIDUS (\) has already been consumed and that the next
  315. // input code point has already been verified to be part of a valid escape.
  316. // It will return a code point.
  317. // Consume the next input code point.
  318. auto input = next_code_point();
  319. // hex digit
  320. if (is_ascii_hex_digit(input)) {
  321. // Consume as many hex digits as possible, but no more than 5.
  322. // Note that this means 1-6 hex digits have been consumed in total.
  323. StringBuilder builder;
  324. builder.append_code_point(input);
  325. size_t counter = 0;
  326. while (is_ascii_hex_digit(peek_code_point()) && counter++ < 5) {
  327. builder.append_code_point(next_code_point());
  328. }
  329. // If the next input code point is whitespace, consume it as well.
  330. if (is_whitespace(peek_code_point())) {
  331. (void)next_code_point();
  332. }
  333. // Interpret the hex digits as a hexadecimal number.
  334. auto unhexed = AK::StringUtils::convert_to_uint_from_hex<u32>(builder.string_view()).value_or(0);
  335. // If this number is zero, or is for a surrogate, or is greater than the maximum allowed
  336. // code point, return U+FFFD REPLACEMENT CHARACTER (�).
  337. if (unhexed == 0 || is_unicode_surrogate(unhexed) || is_greater_than_maximum_allowed_code_point(unhexed)) {
  338. return REPLACEMENT_CHARACTER;
  339. }
  340. // Otherwise, return the code point with that value.
  341. return unhexed;
  342. }
  343. // EOF
  344. if (is_eof(input)) {
  345. // This is a parse error. Return U+FFFD REPLACEMENT CHARACTER (�).
  346. log_parse_error();
  347. return REPLACEMENT_CHARACTER;
  348. }
  349. // anything else
  350. // Return the current input code point.
  351. return input;
  352. }
  353. // https://www.w3.org/TR/css-syntax-3/#consume-ident-like-token
  354. Token Tokenizer::consume_an_ident_like_token()
  355. {
  356. // This section describes how to consume an ident-like token from a stream of code points.
  357. // It returns an <ident-token>, <function-token>, <url-token>, or <bad-url-token>.
  358. // Consume an ident sequence, and let string be the result.
  359. auto string = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors();
  360. // If string’s value is an ASCII case-insensitive match for "url", and the next input code
  361. // point is U+0028 LEFT PARENTHESIS ((), consume it.
  362. if (Infra::is_ascii_case_insensitive_match(string, "url"sv) && is_left_paren(peek_code_point())) {
  363. (void)next_code_point();
  364. // While the next two input code points are whitespace, consume the next input code point.
  365. for (;;) {
  366. auto maybe_whitespace = peek_twin();
  367. if (!(is_whitespace(maybe_whitespace.first) && is_whitespace(maybe_whitespace.second))) {
  368. break;
  369. }
  370. (void)next_code_point();
  371. }
  372. // If the next one or two input code points are U+0022 QUOTATION MARK ("), U+0027 APOSTROPHE ('),
  373. // or whitespace followed by U+0022 QUOTATION MARK (") or U+0027 APOSTROPHE ('), then create a
  374. // <function-token> with its value set to string and return it.
  375. auto next_two = peek_twin();
  376. if (is_quotation_mark(next_two.first) || is_apostrophe(next_two.first) || (is_whitespace(next_two.first) && (is_quotation_mark(next_two.second) || is_apostrophe(next_two.second)))) {
  377. return create_value_token(Token::Type::Function, move(string));
  378. }
  379. // Otherwise, consume a url token, and return it.
  380. return consume_a_url_token();
  381. }
  382. // Otherwise, if the next input code point is U+0028 LEFT PARENTHESIS ((), consume it.
  383. if (is_left_paren(peek_code_point())) {
  384. (void)next_code_point();
  385. // Create a <function-token> with its value set to string and return it.
  386. return create_value_token(Token::Type::Function, move(string));
  387. }
  388. // Otherwise, create an <ident-token> with its value set to string and return it.
  389. return create_value_token(Token::Type::Ident, move(string));
  390. }
  391. // https://www.w3.org/TR/css-syntax-3/#consume-number
  392. Number Tokenizer::consume_a_number()
  393. {
  394. // This section describes how to consume a number from a stream of code points.
  395. // It returns a numeric value, and a type which is either "integer" or "number".
  396. //
  397. // Note: This algorithm does not do the verification of the first few code points
  398. // that are necessary to ensure a number can be obtained from the stream. Ensure
  399. // that the stream starts with a number before calling this algorithm.
  400. // Execute the following steps in order:
  401. // 1. Initially set type to "integer". Let repr be the empty string.
  402. StringBuilder repr;
  403. Number::Type type = Number::Type::Integer;
  404. // 2. If the next input code point is U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-),
  405. // consume it and append it to repr.
  406. bool has_explicit_sign = false;
  407. auto next_input = peek_code_point();
  408. if (is_plus_sign(next_input) || is_hyphen_minus(next_input)) {
  409. has_explicit_sign = true;
  410. repr.append_code_point(next_code_point());
  411. }
  412. // 3. While the next input code point is a digit, consume it and append it to repr.
  413. for (;;) {
  414. auto digits = peek_code_point();
  415. if (!is_ascii_digit(digits))
  416. break;
  417. repr.append_code_point(next_code_point());
  418. }
  419. // 4. If the next 2 input code points are U+002E FULL STOP (.) followed by a digit, then:
  420. auto maybe_number = peek_twin();
  421. if (is_full_stop(maybe_number.first) && is_ascii_digit(maybe_number.second)) {
  422. // 1. Consume them.
  423. // 2. Append them to repr.
  424. repr.append_code_point(next_code_point());
  425. repr.append_code_point(next_code_point());
  426. // 3. Set type to "number".
  427. type = Number::Type::Number;
  428. // 4. While the next input code point is a digit, consume it and append it to repr.
  429. for (;;) {
  430. auto digit = peek_code_point();
  431. if (!is_ascii_digit(digit))
  432. break;
  433. repr.append_code_point(next_code_point());
  434. }
  435. }
  436. // 5. If the next 2 or 3 input code points are U+0045 LATIN CAPITAL LETTER E (E) or
  437. // U+0065 LATIN SMALL LETTER E (e), optionally followed by U+002D HYPHEN-MINUS (-)
  438. // or U+002B PLUS SIGN (+), followed by a digit, then:
  439. auto maybe_exp = peek_triplet();
  440. if (is_E(maybe_exp.first) || is_e(maybe_exp.first)) {
  441. // 1. Consume them.
  442. // 2. Append them to repr.
  443. // FIXME: These conditions should be part of step 5 above.
  444. if (is_plus_sign(maybe_exp.second) || is_hyphen_minus(maybe_exp.second)) {
  445. if (is_ascii_digit(maybe_exp.third)) {
  446. repr.append_code_point(next_code_point());
  447. repr.append_code_point(next_code_point());
  448. repr.append_code_point(next_code_point());
  449. }
  450. } else if (is_ascii_digit(maybe_exp.second)) {
  451. repr.append_code_point(next_code_point());
  452. repr.append_code_point(next_code_point());
  453. }
  454. // 3. Set type to "number".
  455. type = Number::Type::Number;
  456. // 4. While the next input code point is a digit, consume it and append it to repr.
  457. for (;;) {
  458. auto digits = peek_code_point();
  459. if (!is_ascii_digit(digits))
  460. break;
  461. repr.append_code_point(next_code_point());
  462. }
  463. }
  464. // 6. Convert repr to a number, and set the value to the returned value.
  465. auto value = convert_a_string_to_a_number(repr.string_view());
  466. // 7. Return value and type.
  467. if (type == Number::Type::Integer && has_explicit_sign)
  468. return Number { Number::Type::IntegerWithExplicitSign, value };
  469. return Number { type, value };
  470. }
  471. // https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
  472. float Tokenizer::convert_a_string_to_a_number(StringView string)
  473. {
  474. // FIXME: We already found the whole part, fraction part and exponent during
  475. // validation, we could probably skip
  476. return string.to_float(AK::TrimWhitespace::No).release_value();
  477. }
  478. // https://www.w3.org/TR/css-syntax-3/#consume-name
  479. ErrorOr<FlyString> Tokenizer::consume_an_ident_sequence()
  480. {
  481. // This section describes how to consume an ident sequence from a stream of code points.
  482. // It returns a string containing the largest name that can be formed from adjacent
  483. // code points in the stream, starting from the first.
  484. //
  485. // Note: This algorithm does not do the verification of the first few code points that
  486. // are necessary to ensure the returned code points would constitute an <ident-token>.
  487. // If that is the intended use, ensure that the stream starts with an ident sequence before
  488. // calling this algorithm.
  489. // Let result initially be an empty string.
  490. StringBuilder result;
  491. // Repeatedly consume the next input code point from the stream:
  492. for (;;) {
  493. auto input = next_code_point();
  494. if (is_eof(input))
  495. break;
  496. // name code point
  497. if (is_ident_code_point(input)) {
  498. // Append the code point to result.
  499. TRY(result.try_append_code_point(input));
  500. continue;
  501. }
  502. // the stream starts with a valid escape
  503. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  504. // Consume an escaped code point. Append the returned code point to result.
  505. TRY(result.try_append_code_point(consume_escaped_code_point()));
  506. continue;
  507. }
  508. // anything else
  509. // Reconsume the current input code point. Return result.
  510. reconsume_current_input_code_point();
  511. break;
  512. }
  513. return result.to_fly_string();
  514. }
  515. // https://www.w3.org/TR/css-syntax-3/#consume-url-token
  516. Token Tokenizer::consume_a_url_token()
  517. {
  518. // This section describes how to consume a url token from a stream of code points.
  519. // It returns either a <url-token> or a <bad-url-token>.
  520. //
  521. // Note: This algorithm assumes that the initial "url(" has already been consumed.
  522. // This algorithm also assumes that it’s being called to consume an "unquoted" value,
  523. // like url(foo). A quoted value, like url("foo"), is parsed as a <function-token>.
  524. // Consume an ident-like token automatically handles this distinction; this algorithm
  525. // shouldn’t be called directly otherwise.
  526. // 1. Initially create a <url-token> with its value set to the empty string.
  527. auto token = create_new_token(Token::Type::Url);
  528. StringBuilder builder;
  529. // 2. Consume as much whitespace as possible.
  530. consume_as_much_whitespace_as_possible();
  531. auto make_token = [&]() {
  532. token.m_value = FlyString::from_utf8(builder.string_view()).release_value_but_fixme_should_propagate_errors();
  533. return token;
  534. };
  535. // 3. Repeatedly consume the next input code point from the stream:
  536. for (;;) {
  537. auto input = next_code_point();
  538. // U+0029 RIGHT PARENTHESIS ())
  539. if (is_right_paren(input)) {
  540. // Return the <url-token>.
  541. return make_token();
  542. }
  543. // EOF
  544. if (is_eof(input)) {
  545. // This is a parse error. Return the <url-token>.
  546. log_parse_error();
  547. return make_token();
  548. }
  549. // whitespace
  550. if (is_whitespace(input)) {
  551. // Consume as much whitespace as possible.
  552. consume_as_much_whitespace_as_possible();
  553. // If the next input code point is U+0029 RIGHT PARENTHESIS ()) or EOF, consume it
  554. // and return the <url-token> (if EOF was encountered, this is a parse error);
  555. input = peek_code_point();
  556. if (is_right_paren(input)) {
  557. (void)next_code_point();
  558. return make_token();
  559. }
  560. if (is_eof(input)) {
  561. (void)next_code_point();
  562. log_parse_error();
  563. return make_token();
  564. }
  565. // otherwise, consume the remnants of a bad url, create a <bad-url-token>, and return it.
  566. consume_the_remnants_of_a_bad_url();
  567. return create_new_token(Token::Type::BadUrl);
  568. }
  569. // U+0022 QUOTATION MARK (")
  570. // U+0027 APOSTROPHE (')
  571. // U+0028 LEFT PARENTHESIS (()
  572. // non-printable code point
  573. if (is_quotation_mark(input) || is_apostrophe(input) || is_left_paren(input) || is_non_printable(input)) {
  574. // This is a parse error. Consume the remnants of a bad url, create a <bad-url-token>, and return it.
  575. log_parse_error();
  576. consume_the_remnants_of_a_bad_url();
  577. return create_new_token(Token::Type::BadUrl);
  578. }
  579. // U+005C REVERSE SOLIDUS (\)
  580. if (is_reverse_solidus(input)) {
  581. // If the stream starts with a valid escape,
  582. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  583. // consume an escaped code point and append the returned code point to the <url-token>’s value.
  584. builder.append_code_point(consume_escaped_code_point());
  585. continue;
  586. } else {
  587. // Otherwise, this is a parse error.
  588. log_parse_error();
  589. // Consume the remnants of a bad url, create a <bad-url-token>, and return it.
  590. consume_the_remnants_of_a_bad_url();
  591. return create_new_token(Token::Type::BadUrl);
  592. }
  593. }
  594. // anything else
  595. // Append the current input code point to the <url-token>’s value.
  596. builder.append_code_point(input);
  597. }
  598. }
  599. // https://www.w3.org/TR/css-syntax-3/#consume-remnants-of-bad-url
  600. void Tokenizer::consume_the_remnants_of_a_bad_url()
  601. {
  602. // This section describes how to consume the remnants of a bad url from a stream of code points,
  603. // "cleaning up" after the tokenizer realizes that it’s in the middle of a <bad-url-token> rather
  604. // than a <url-token>. It returns nothing; its sole use is to consume enough of the input stream
  605. // to reach a recovery point where normal tokenizing can resume.
  606. // Repeatedly consume the next input code point from the stream:
  607. for (;;) {
  608. auto input = next_code_point();
  609. // U+0029 RIGHT PARENTHESIS ())
  610. // EOF
  611. if (is_eof(input) || is_right_paren(input)) {
  612. // Return.
  613. return;
  614. }
  615. // the input stream starts with a valid escape
  616. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  617. // Consume an escaped code point.
  618. // This allows an escaped right parenthesis ("\)") to be encountered without ending
  619. // the <bad-url-token>. This is otherwise identical to the "anything else" clause.
  620. (void)consume_escaped_code_point();
  621. }
  622. // anything else
  623. // Do nothing.
  624. }
  625. }
  626. void Tokenizer::consume_as_much_whitespace_as_possible()
  627. {
  628. while (is_whitespace(peek_code_point())) {
  629. (void)next_code_point();
  630. }
  631. }
  632. void Tokenizer::reconsume_current_input_code_point()
  633. {
  634. m_utf8_iterator = m_prev_utf8_iterator;
  635. m_position = m_prev_position;
  636. }
  637. // https://www.w3.org/TR/css-syntax-3/#consume-numeric-token
  638. Token Tokenizer::consume_a_numeric_token()
  639. {
  640. // This section describes how to consume a numeric token from a stream of code points.
  641. // It returns either a <number-token>, <percentage-token>, or <dimension-token>.
  642. // Consume a number and let number be the result.
  643. auto number = consume_a_number();
  644. // If the next 3 input code points would start an ident sequence, then:
  645. if (would_start_an_ident_sequence(peek_triplet())) {
  646. // 1. Create a <dimension-token> with the same value and type flag as number,
  647. // and a unit set initially to the empty string.
  648. auto token = create_new_token(Token::Type::Dimension);
  649. token.m_number_value = number;
  650. // 2. Consume an ident sequence. Set the <dimension-token>’s unit to the returned value.
  651. auto unit = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors();
  652. VERIFY(!unit.is_empty());
  653. // NOTE: We intentionally store this in the `value`, to save space.
  654. token.m_value = move(unit);
  655. // 3. Return the <dimension-token>.
  656. return token;
  657. }
  658. // Otherwise, if the next input code point is U+0025 PERCENTAGE SIGN (%), consume it.
  659. if (is_percent(peek_code_point())) {
  660. (void)next_code_point();
  661. // Create a <percentage-token> with the same value as number, and return it.
  662. auto token = create_new_token(Token::Type::Percentage);
  663. token.m_number_value = number;
  664. return token;
  665. }
  666. // Otherwise, create a <number-token> with the same value and type flag as number, and return it.
  667. auto token = create_new_token(Token::Type::Number);
  668. token.m_number_value = number;
  669. return token;
  670. }
  671. // https://www.w3.org/TR/css-syntax-3/#starts-with-a-number
  672. bool Tokenizer::would_start_a_number(U32Triplet values)
  673. {
  674. // This section describes how to check if three code points would start a number.
  675. // The algorithm described here can be called explicitly with three code points,
  676. // or can be called with the input stream itself. In the latter case, the three
  677. // code points in question are the current input code point and the next two input
  678. // code points, in that order.
  679. //
  680. // Note: This algorithm will not consume any additional code points.
  681. // Look at the first code point:
  682. // U+002B PLUS SIGN (+)
  683. // U+002D HYPHEN-MINUS (-)
  684. if (is_plus_sign(values.first) || is_hyphen_minus(values.first)) {
  685. // If the second code point is a digit, return true.
  686. if (is_ascii_digit(values.second))
  687. return true;
  688. // Otherwise, if the second code point is a U+002E FULL STOP (.) and the third
  689. // code point is a digit, return true.
  690. if (is_full_stop(values.second) && is_ascii_digit(values.third))
  691. return true;
  692. // Otherwise, return false.
  693. return false;
  694. }
  695. // U+002E FULL STOP (.)
  696. if (is_full_stop(values.first))
  697. // If the second code point is a digit, return true. Otherwise, return false.
  698. return is_ascii_digit(values.second);
  699. // digit
  700. if (is_ascii_digit(values.first))
  701. // Return true.
  702. return true;
  703. // anything else
  704. // Return false.
  705. return false;
  706. }
  707. // https://www.w3.org/TR/css-syntax-3/#starts-with-a-valid-escape
  708. bool Tokenizer::is_valid_escape_sequence(U32Twin values)
  709. {
  710. // This section describes how to check if two code points are a valid escape.
  711. // The algorithm described here can be called explicitly with two code points,
  712. // or can be called with the input stream itself. In the latter case, the two
  713. // code points in question are the current input code point and the next input
  714. // code point, in that order.
  715. //
  716. // Note: This algorithm will not consume any additional code point.
  717. // If the first code point is not U+005C REVERSE SOLIDUS (\), return false.
  718. if (!is_reverse_solidus(values.first))
  719. return false;
  720. // Otherwise, if the second code point is a newline, return false.
  721. if (is_newline(values.second))
  722. return false;
  723. // Otherwise, return true.
  724. return true;
  725. }
  726. // https://www.w3.org/TR/css-syntax-3/#would-start-an-identifier
  727. bool Tokenizer::would_start_an_ident_sequence(U32Triplet values)
  728. {
  729. // This section describes how to check if three code points would start an ident sequence.
  730. // The algorithm described here can be called explicitly with three code points, or
  731. // can be called with the input stream itself. In the latter case, the three code
  732. // points in question are the current input code point and the next two input code
  733. // points, in that order.
  734. //
  735. // Note: This algorithm will not consume any additional code points.
  736. // Look at the first code point:
  737. // U+002D HYPHEN-MINUS
  738. if (is_hyphen_minus(values.first)) {
  739. // If the second code point is a name-start code point or a U+002D HYPHEN-MINUS,
  740. // or the second and third code points are a valid escape, return true.
  741. if (is_ident_start_code_point(values.second) || is_hyphen_minus(values.second) || is_valid_escape_sequence(values.to_twin_23()))
  742. return true;
  743. // Otherwise, return false.
  744. return false;
  745. }
  746. // name-start code point
  747. if (is_ident_start_code_point(values.first)) {
  748. // Return true.
  749. return true;
  750. }
  751. // U+005C REVERSE SOLIDUS (\)
  752. if (is_reverse_solidus(values.first)) {
  753. // If the first and second code points are a valid escape, return true.
  754. if (is_valid_escape_sequence(values.to_twin_12()))
  755. return true;
  756. // Otherwise, return false.
  757. return false;
  758. }
  759. // anything else
  760. // Return false.
  761. return false;
  762. }
  763. // https://www.w3.org/TR/css-syntax-3/#consume-string-token
  764. Token Tokenizer::consume_string_token(u32 ending_code_point)
  765. {
  766. // This section describes how to consume a string token from a stream of code points.
  767. // It returns either a <string-token> or <bad-string-token>.
  768. //
  769. // This algorithm may be called with an ending code point, which denotes the code point
  770. // that ends the string. If an ending code point is not specified, the current input
  771. // code point is used.
  772. // Initially create a <string-token> with its value set to the empty string.
  773. auto token = create_new_token(Token::Type::String);
  774. StringBuilder builder;
  775. auto make_token = [&]() {
  776. token.m_value = FlyString::from_utf8(builder.string_view()).release_value_but_fixme_should_propagate_errors();
  777. return token;
  778. };
  779. // Repeatedly consume the next input code point from the stream:
  780. for (;;) {
  781. auto input = next_code_point();
  782. // ending code point
  783. if (input == ending_code_point)
  784. return make_token();
  785. // EOF
  786. if (is_eof(input)) {
  787. // This is a parse error. Return the <string-token>.
  788. log_parse_error();
  789. return make_token();
  790. }
  791. // newline
  792. if (is_newline(input)) {
  793. // This is a parse error. Reconsume the current input code point, create a
  794. // <bad-string-token>, and return it.
  795. reconsume_current_input_code_point();
  796. return create_new_token(Token::Type::BadString);
  797. }
  798. // U+005C REVERSE SOLIDUS (\)
  799. if (is_reverse_solidus(input)) {
  800. // If the next input code point is EOF, do nothing.
  801. auto next_input = peek_code_point();
  802. if (is_eof(next_input))
  803. continue;
  804. // Otherwise, if the next input code point is a newline, consume it.
  805. if (is_newline(next_input)) {
  806. (void)next_code_point();
  807. continue;
  808. }
  809. // Otherwise, (the stream starts with a valid escape) consume an escaped code
  810. // point and append the returned code point to the <string-token>’s value.
  811. auto escaped = consume_escaped_code_point();
  812. builder.append_code_point(escaped);
  813. continue;
  814. }
  815. // anything else
  816. // Append the current input code point to the <string-token>’s value.
  817. builder.append_code_point(input);
  818. }
  819. }
  820. // https://www.w3.org/TR/css-syntax-3/#consume-comment
  821. void Tokenizer::consume_comments()
  822. {
  823. // This section describes how to consume comments from a stream of code points.
  824. // It returns nothing.
  825. start:
  826. // If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A ASTERISK (*),
  827. // consume them and all following code points up to and including the first U+002A ASTERISK (*)
  828. // followed by a U+002F SOLIDUS (/), or up to an EOF code point. Return to the start of this step.
  829. //
  830. // If the preceding paragraph ended by consuming an EOF code point, this is a parse error.
  831. //
  832. // Return nothing.
  833. auto twin = peek_twin();
  834. if (!(is_solidus(twin.first) && is_asterisk(twin.second)))
  835. return;
  836. (void)next_code_point();
  837. (void)next_code_point();
  838. for (;;) {
  839. auto twin_inner = peek_twin();
  840. if (is_eof(twin_inner.first) || is_eof(twin_inner.second)) {
  841. log_parse_error();
  842. return;
  843. }
  844. if (is_asterisk(twin_inner.first) && is_solidus(twin_inner.second)) {
  845. (void)next_code_point();
  846. (void)next_code_point();
  847. goto start;
  848. }
  849. (void)next_code_point();
  850. }
  851. }
  852. // https://www.w3.org/TR/css-syntax-3/#consume-token
  853. Token Tokenizer::consume_a_token()
  854. {
  855. // This section describes how to consume a token from a stream of code points.
  856. // It will return a single token of any type.
  857. // Consume comments.
  858. consume_comments();
  859. // Consume the next input code point.
  860. auto input = next_code_point();
  861. // whitespace
  862. if (is_whitespace(input)) {
  863. dbgln_if(CSS_TOKENIZER_DEBUG, "is whitespace");
  864. // Consume as much whitespace as possible. Return a <whitespace-token>.
  865. consume_as_much_whitespace_as_possible();
  866. return create_new_token(Token::Type::Whitespace);
  867. }
  868. // U+0022 QUOTATION MARK (")
  869. if (is_quotation_mark(input)) {
  870. dbgln_if(CSS_TOKENIZER_DEBUG, "is quotation mark");
  871. // Consume a string token and return it.
  872. return consume_string_token(input);
  873. }
  874. // U+0023 NUMBER SIGN (#)
  875. if (is_number_sign(input)) {
  876. dbgln_if(CSS_TOKENIZER_DEBUG, "is number sign");
  877. // If the next input code point is an ident code point or the next two input code points
  878. // are a valid escape, then:
  879. auto next_input = peek_code_point();
  880. auto maybe_escape = peek_twin();
  881. if (is_ident_code_point(next_input) || is_valid_escape_sequence(maybe_escape)) {
  882. // 1. Create a <hash-token>.
  883. auto token = create_new_token(Token::Type::Hash);
  884. // 2. If the next 3 input code points would start an ident sequence, set the <hash-token>’s
  885. // type flag to "id".
  886. if (would_start_an_ident_sequence(peek_triplet()))
  887. token.m_hash_type = Token::HashType::Id;
  888. // 3. Consume an ident sequence, and set the <hash-token>’s value to the returned string.
  889. auto name = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors();
  890. token.m_value = move(name);
  891. // 4. Return the <hash-token>.
  892. return token;
  893. }
  894. // Otherwise, return a <delim-token> with its value set to the current input code point.
  895. return create_value_token(Token::Type::Delim, input);
  896. }
  897. // U+0027 APOSTROPHE (')
  898. if (is_apostrophe(input)) {
  899. dbgln_if(CSS_TOKENIZER_DEBUG, "is apostrophe");
  900. // Consume a string token and return it.
  901. return consume_string_token(input);
  902. }
  903. // U+0028 LEFT PARENTHESIS (()
  904. if (is_left_paren(input)) {
  905. dbgln_if(CSS_TOKENIZER_DEBUG, "is left paren");
  906. // Return a <(-token>.
  907. return create_new_token(Token::Type::OpenParen);
  908. }
  909. // U+0029 RIGHT PARENTHESIS ())
  910. if (is_right_paren(input)) {
  911. dbgln_if(CSS_TOKENIZER_DEBUG, "is right paren");
  912. // Return a <)-token>.
  913. return create_new_token(Token::Type::CloseParen);
  914. }
  915. // U+002B PLUS SIGN (+)
  916. if (is_plus_sign(input)) {
  917. dbgln_if(CSS_TOKENIZER_DEBUG, "is plus sign");
  918. // If the input stream starts with a number, reconsume the current input code point,
  919. // consume a numeric token and return it.
  920. if (would_start_a_number(start_of_input_stream_triplet())) {
  921. reconsume_current_input_code_point();
  922. return consume_a_numeric_token();
  923. }
  924. // Otherwise, return a <delim-token> with its value set to the current input code point.
  925. return create_value_token(Token::Type::Delim, input);
  926. }
  927. // U+002C COMMA (,)
  928. if (is_comma(input)) {
  929. dbgln_if(CSS_TOKENIZER_DEBUG, "is comma");
  930. // Return a <comma-token>.
  931. return create_new_token(Token::Type::Comma);
  932. }
  933. // U+002D HYPHEN-MINUS (-)
  934. if (is_hyphen_minus(input)) {
  935. dbgln_if(CSS_TOKENIZER_DEBUG, "is hyphen minus");
  936. // If the input stream starts with a number, reconsume the current input code point,
  937. // consume a numeric token, and return it.
  938. if (would_start_a_number(start_of_input_stream_triplet())) {
  939. reconsume_current_input_code_point();
  940. return consume_a_numeric_token();
  941. }
  942. // Otherwise, if the next 2 input code points are U+002D HYPHEN-MINUS U+003E
  943. // GREATER-THAN SIGN (->), consume them and return a <CDC-token>.
  944. auto next_twin = peek_twin();
  945. if (is_hyphen_minus(next_twin.first) && is_greater_than_sign(next_twin.second)) {
  946. (void)next_code_point();
  947. (void)next_code_point();
  948. return create_new_token(Token::Type::CDC);
  949. }
  950. // Otherwise, if the input stream starts with an identifier, reconsume the current
  951. // input code point, consume an ident-like token, and return it.
  952. if (would_start_an_ident_sequence(start_of_input_stream_triplet())) {
  953. reconsume_current_input_code_point();
  954. return consume_an_ident_like_token();
  955. }
  956. // Otherwise, return a <delim-token> with its value set to the current input code point.
  957. return create_value_token(Token::Type::Delim, input);
  958. }
  959. // U+002E FULL STOP (.)
  960. if (is_full_stop(input)) {
  961. dbgln_if(CSS_TOKENIZER_DEBUG, "is full stop");
  962. // If the input stream starts with a number, reconsume the current input code point,
  963. // consume a numeric token, and return it.
  964. if (would_start_a_number(start_of_input_stream_triplet())) {
  965. reconsume_current_input_code_point();
  966. return consume_a_numeric_token();
  967. }
  968. // Otherwise, return a <delim-token> with its value set to the current input code point.
  969. return create_value_token(Token::Type::Delim, input);
  970. }
  971. // U+003A COLON (:)
  972. if (is_colon(input)) {
  973. dbgln_if(CSS_TOKENIZER_DEBUG, "is colon");
  974. // Return a <colon-token>.
  975. return create_new_token(Token::Type::Colon);
  976. }
  977. // U+003B SEMICOLON (;)
  978. if (is_semicolon(input)) {
  979. dbgln_if(CSS_TOKENIZER_DEBUG, "is semicolon");
  980. // Return a <semicolon-token>.
  981. return create_new_token(Token::Type::Semicolon);
  982. }
  983. // U+003C LESS-THAN SIGN (<)
  984. if (is_less_than_sign(input)) {
  985. dbgln_if(CSS_TOKENIZER_DEBUG, "is less than");
  986. // If the next 3 input code points are U+0021 EXCLAMATION MARK U+002D HYPHEN-MINUS
  987. // U+002D HYPHEN-MINUS (!--), consume them and return a <CDO-token>.
  988. auto maybe_cdo = peek_triplet();
  989. if (is_exclamation_mark(maybe_cdo.first) && is_hyphen_minus(maybe_cdo.second) && is_hyphen_minus(maybe_cdo.third)) {
  990. (void)next_code_point();
  991. (void)next_code_point();
  992. (void)next_code_point();
  993. return create_new_token(Token::Type::CDO);
  994. }
  995. // Otherwise, return a <delim-token> with its value set to the current input code point.
  996. return create_value_token(Token::Type::Delim, input);
  997. }
  998. // U+0040 COMMERCIAL AT (@)
  999. if (is_at(input)) {
  1000. dbgln_if(CSS_TOKENIZER_DEBUG, "is at");
  1001. // If the next 3 input code points would start an ident sequence, consume an ident sequence, create
  1002. // an <at-keyword-token> with its value set to the returned value, and return it.
  1003. if (would_start_an_ident_sequence(peek_triplet())) {
  1004. auto name = consume_an_ident_sequence().release_value_but_fixme_should_propagate_errors();
  1005. return create_value_token(Token::Type::AtKeyword, move(name));
  1006. }
  1007. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1008. return create_value_token(Token::Type::Delim, input);
  1009. }
  1010. // U+005B LEFT SQUARE BRACKET ([)
  1011. if (is_open_square_bracket(input)) {
  1012. dbgln_if(CSS_TOKENIZER_DEBUG, "is open square");
  1013. // Return a <[-token>.
  1014. return create_new_token(Token::Type::OpenSquare);
  1015. }
  1016. // U+005C REVERSE SOLIDUS (\)
  1017. if (is_reverse_solidus(input)) {
  1018. dbgln_if(CSS_TOKENIZER_DEBUG, "is reverse solidus");
  1019. // If the input stream starts with a valid escape, reconsume the current input code point,
  1020. // consume an ident-like token, and return it.
  1021. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  1022. reconsume_current_input_code_point();
  1023. return consume_an_ident_like_token();
  1024. }
  1025. // Otherwise, this is a parse error. Return a <delim-token> with its value set to the
  1026. // current input code point.
  1027. log_parse_error();
  1028. return create_value_token(Token::Type::Delim, input);
  1029. }
  1030. // U+005D RIGHT SQUARE BRACKET (])
  1031. if (is_closed_square_bracket(input)) {
  1032. dbgln_if(CSS_TOKENIZER_DEBUG, "is closed square");
  1033. // Return a <]-token>.
  1034. return create_new_token(Token::Type::CloseSquare);
  1035. }
  1036. // U+007B LEFT CURLY BRACKET ({)
  1037. if (is_open_curly_bracket(input)) {
  1038. dbgln_if(CSS_TOKENIZER_DEBUG, "is open curly");
  1039. // Return a <{-token>.
  1040. return create_new_token(Token::Type::OpenCurly);
  1041. }
  1042. // U+007D RIGHT CURLY BRACKET (})
  1043. if (is_closed_curly_bracket(input)) {
  1044. dbgln_if(CSS_TOKENIZER_DEBUG, "is closed curly");
  1045. // Return a <}-token>.
  1046. return create_new_token(Token::Type::CloseCurly);
  1047. }
  1048. // digit
  1049. if (is_ascii_digit(input)) {
  1050. dbgln_if(CSS_TOKENIZER_DEBUG, "is digit");
  1051. // Reconsume the current input code point, consume a numeric token, and return it.
  1052. reconsume_current_input_code_point();
  1053. return consume_a_numeric_token();
  1054. }
  1055. // name-start code point
  1056. if (is_ident_start_code_point(input)) {
  1057. dbgln_if(CSS_TOKENIZER_DEBUG, "is name start");
  1058. // Reconsume the current input code point, consume an ident-like token, and return it.
  1059. reconsume_current_input_code_point();
  1060. return consume_an_ident_like_token();
  1061. }
  1062. // EOF
  1063. if (is_eof(input)) {
  1064. // Return an <EOF-token>.
  1065. return create_new_token(Token::Type::EndOfFile);
  1066. }
  1067. // anything else
  1068. dbgln_if(CSS_TOKENIZER_DEBUG, "is delimiter");
  1069. // Return a <delim-token> with its value set to the current input code point.
  1070. return create_value_token(Token::Type::Delim, input);
  1071. }
  1072. }