Tokenizer.cpp 44 KB

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