Tokenizer.cpp 44 KB

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