Tokenizer.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. && (((is_plus_sign(maybe_exp.second) || is_hyphen_minus(maybe_exp.second)) && is_ascii_digit(maybe_exp.third))
  448. || (is_ascii_digit(maybe_exp.second)))) {
  449. // 1. Consume them.
  450. // 2. Append them to repr.
  451. if (is_plus_sign(maybe_exp.second) || is_hyphen_minus(maybe_exp.second)) {
  452. if (is_ascii_digit(maybe_exp.third)) {
  453. repr.append_code_point(next_code_point());
  454. repr.append_code_point(next_code_point());
  455. repr.append_code_point(next_code_point());
  456. }
  457. } else if (is_ascii_digit(maybe_exp.second)) {
  458. repr.append_code_point(next_code_point());
  459. repr.append_code_point(next_code_point());
  460. }
  461. // 3. Set type to "number".
  462. type = Number::Type::Number;
  463. // 4. While the next input code point is a digit, consume it and append it to repr.
  464. for (;;) {
  465. auto digits = peek_code_point();
  466. if (!is_ascii_digit(digits))
  467. break;
  468. repr.append_code_point(next_code_point());
  469. }
  470. }
  471. // 6. Convert repr to a number, and set the value to the returned value.
  472. auto value = convert_a_string_to_a_number(repr.string_view());
  473. // 7. Return value and type.
  474. if (type == Number::Type::Integer && has_explicit_sign)
  475. return Number { Number::Type::IntegerWithExplicitSign, value };
  476. return Number { type, value };
  477. }
  478. // https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
  479. float Tokenizer::convert_a_string_to_a_number(StringView string)
  480. {
  481. // FIXME: We already found the whole part, fraction part and exponent during
  482. // validation, we could probably skip
  483. return string.to_float(AK::TrimWhitespace::No).release_value();
  484. }
  485. // https://www.w3.org/TR/css-syntax-3/#consume-name
  486. ErrorOr<FlyString> Tokenizer::consume_an_ident_sequence()
  487. {
  488. // This section describes how to consume an ident sequence from a stream of code points.
  489. // It returns a string containing the largest name that can be formed from adjacent
  490. // code points in the stream, starting from the first.
  491. //
  492. // Note: This algorithm does not do the verification of the first few code points that
  493. // are necessary to ensure the returned code points would constitute an <ident-token>.
  494. // If that is the intended use, ensure that the stream starts with an ident sequence before
  495. // calling this algorithm.
  496. // Let result initially be an empty string.
  497. StringBuilder result;
  498. // Repeatedly consume the next input code point from the stream:
  499. for (;;) {
  500. auto input = next_code_point();
  501. if (is_eof(input))
  502. break;
  503. // name code point
  504. if (is_ident_code_point(input)) {
  505. // Append the code point to result.
  506. TRY(result.try_append_code_point(input));
  507. continue;
  508. }
  509. // the stream starts with a valid escape
  510. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  511. // Consume an escaped code point. Append the returned code point to result.
  512. TRY(result.try_append_code_point(consume_escaped_code_point()));
  513. continue;
  514. }
  515. // anything else
  516. // Reconsume the current input code point. Return result.
  517. reconsume_current_input_code_point();
  518. break;
  519. }
  520. return result.to_fly_string();
  521. }
  522. // https://www.w3.org/TR/css-syntax-3/#consume-url-token
  523. ErrorOr<Token> Tokenizer::consume_a_url_token()
  524. {
  525. // This section describes how to consume a url token from a stream of code points.
  526. // It returns either a <url-token> or a <bad-url-token>.
  527. //
  528. // Note: This algorithm assumes that the initial "url(" has already been consumed.
  529. // This algorithm also assumes that it’s being called to consume an "unquoted" value,
  530. // like url(foo). A quoted value, like url("foo"), is parsed as a <function-token>.
  531. // Consume an ident-like token automatically handles this distinction; this algorithm
  532. // shouldn’t be called directly otherwise.
  533. // 1. Initially create a <url-token> with its value set to the empty string.
  534. auto token = create_new_token(Token::Type::Url);
  535. StringBuilder builder;
  536. // 2. Consume as much whitespace as possible.
  537. consume_as_much_whitespace_as_possible();
  538. auto make_token = [&]() -> ErrorOr<Token> {
  539. token.m_value = TRY(FlyString::from_utf8(builder.string_view()));
  540. return token;
  541. };
  542. // 3. Repeatedly consume the next input code point from the stream:
  543. for (;;) {
  544. auto input = next_code_point();
  545. // U+0029 RIGHT PARENTHESIS ())
  546. if (is_right_paren(input)) {
  547. // Return the <url-token>.
  548. return make_token();
  549. }
  550. // EOF
  551. if (is_eof(input)) {
  552. // This is a parse error. Return the <url-token>.
  553. log_parse_error();
  554. return make_token();
  555. }
  556. // whitespace
  557. if (is_whitespace(input)) {
  558. // Consume as much whitespace as possible.
  559. consume_as_much_whitespace_as_possible();
  560. // If the next input code point is U+0029 RIGHT PARENTHESIS ()) or EOF, consume it
  561. // and return the <url-token> (if EOF was encountered, this is a parse error);
  562. input = peek_code_point();
  563. if (is_right_paren(input)) {
  564. (void)next_code_point();
  565. return make_token();
  566. }
  567. if (is_eof(input)) {
  568. (void)next_code_point();
  569. log_parse_error();
  570. return make_token();
  571. }
  572. // otherwise, consume the remnants of a bad url, create a <bad-url-token>, and return it.
  573. consume_the_remnants_of_a_bad_url();
  574. return create_new_token(Token::Type::BadUrl);
  575. }
  576. // U+0022 QUOTATION MARK (")
  577. // U+0027 APOSTROPHE (')
  578. // U+0028 LEFT PARENTHESIS (()
  579. // non-printable code point
  580. if (is_quotation_mark(input) || is_apostrophe(input) || is_left_paren(input) || is_non_printable(input)) {
  581. // This is a parse error. Consume the remnants of a bad url, create a <bad-url-token>, and return it.
  582. log_parse_error();
  583. consume_the_remnants_of_a_bad_url();
  584. return create_new_token(Token::Type::BadUrl);
  585. }
  586. // U+005C REVERSE SOLIDUS (\)
  587. if (is_reverse_solidus(input)) {
  588. // If the stream starts with a valid escape,
  589. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  590. // consume an escaped code point and append the returned code point to the <url-token>’s value.
  591. builder.append_code_point(consume_escaped_code_point());
  592. continue;
  593. } else {
  594. // Otherwise, this is a parse error.
  595. log_parse_error();
  596. // Consume the remnants of a bad url, create a <bad-url-token>, and return it.
  597. consume_the_remnants_of_a_bad_url();
  598. return create_new_token(Token::Type::BadUrl);
  599. }
  600. }
  601. // anything else
  602. // Append the current input code point to the <url-token>’s value.
  603. builder.append_code_point(input);
  604. }
  605. }
  606. // https://www.w3.org/TR/css-syntax-3/#consume-remnants-of-bad-url
  607. void Tokenizer::consume_the_remnants_of_a_bad_url()
  608. {
  609. // This section describes how to consume the remnants of a bad url from a stream of code points,
  610. // "cleaning up" after the tokenizer realizes that it’s in the middle of a <bad-url-token> rather
  611. // than a <url-token>. It returns nothing; its sole use is to consume enough of the input stream
  612. // to reach a recovery point where normal tokenizing can resume.
  613. // Repeatedly consume the next input code point from the stream:
  614. for (;;) {
  615. auto input = next_code_point();
  616. // U+0029 RIGHT PARENTHESIS ())
  617. // EOF
  618. if (is_eof(input) || is_right_paren(input)) {
  619. // Return.
  620. return;
  621. }
  622. // the input stream starts with a valid escape
  623. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  624. // Consume an escaped code point.
  625. // This allows an escaped right parenthesis ("\)") to be encountered without ending
  626. // the <bad-url-token>. This is otherwise identical to the "anything else" clause.
  627. (void)consume_escaped_code_point();
  628. }
  629. // anything else
  630. // Do nothing.
  631. }
  632. }
  633. void Tokenizer::consume_as_much_whitespace_as_possible()
  634. {
  635. while (is_whitespace(peek_code_point())) {
  636. (void)next_code_point();
  637. }
  638. }
  639. void Tokenizer::reconsume_current_input_code_point()
  640. {
  641. m_utf8_iterator = m_prev_utf8_iterator;
  642. m_position = m_prev_position;
  643. }
  644. // https://www.w3.org/TR/css-syntax-3/#consume-numeric-token
  645. ErrorOr<Token> Tokenizer::consume_a_numeric_token()
  646. {
  647. // This section describes how to consume a numeric token from a stream of code points.
  648. // It returns either a <number-token>, <percentage-token>, or <dimension-token>.
  649. // Consume a number and let number be the result.
  650. auto number = consume_a_number();
  651. // If the next 3 input code points would start an ident sequence, then:
  652. if (would_start_an_ident_sequence(peek_triplet())) {
  653. // 1. Create a <dimension-token> with the same value and type flag as number,
  654. // and a unit set initially to the empty string.
  655. auto token = create_new_token(Token::Type::Dimension);
  656. token.m_number_value = number;
  657. // 2. Consume an ident sequence. Set the <dimension-token>’s unit to the returned value.
  658. auto unit = TRY(consume_an_ident_sequence());
  659. VERIFY(!unit.is_empty());
  660. // NOTE: We intentionally store this in the `value`, to save space.
  661. token.m_value = move(unit);
  662. // 3. Return the <dimension-token>.
  663. return token;
  664. }
  665. // Otherwise, if the next input code point is U+0025 PERCENTAGE SIGN (%), consume it.
  666. if (is_percent(peek_code_point())) {
  667. (void)next_code_point();
  668. // Create a <percentage-token> with the same value as number, and return it.
  669. auto token = create_new_token(Token::Type::Percentage);
  670. token.m_number_value = number;
  671. return token;
  672. }
  673. // Otherwise, create a <number-token> with the same value and type flag as number, and return it.
  674. auto token = create_new_token(Token::Type::Number);
  675. token.m_number_value = number;
  676. return token;
  677. }
  678. // https://www.w3.org/TR/css-syntax-3/#starts-with-a-number
  679. bool Tokenizer::would_start_a_number(U32Triplet values)
  680. {
  681. // This section describes how to check if three code points would start a number.
  682. // The algorithm described here can be called explicitly with three code points,
  683. // or can be called with the input stream itself. In the latter case, the three
  684. // code points in question are the current input code point and the next two input
  685. // code points, in that order.
  686. //
  687. // Note: This algorithm will not consume any additional code points.
  688. // Look at the first code point:
  689. // U+002B PLUS SIGN (+)
  690. // U+002D HYPHEN-MINUS (-)
  691. if (is_plus_sign(values.first) || is_hyphen_minus(values.first)) {
  692. // If the second code point is a digit, return true.
  693. if (is_ascii_digit(values.second))
  694. return true;
  695. // Otherwise, if the second code point is a U+002E FULL STOP (.) and the third
  696. // code point is a digit, return true.
  697. if (is_full_stop(values.second) && is_ascii_digit(values.third))
  698. return true;
  699. // Otherwise, return false.
  700. return false;
  701. }
  702. // U+002E FULL STOP (.)
  703. if (is_full_stop(values.first))
  704. // If the second code point is a digit, return true. Otherwise, return false.
  705. return is_ascii_digit(values.second);
  706. // digit
  707. if (is_ascii_digit(values.first))
  708. // Return true.
  709. return true;
  710. // anything else
  711. // Return false.
  712. return false;
  713. }
  714. // https://www.w3.org/TR/css-syntax-3/#starts-with-a-valid-escape
  715. bool Tokenizer::is_valid_escape_sequence(U32Twin values)
  716. {
  717. // This section describes how to check if two code points are a valid escape.
  718. // The algorithm described here can be called explicitly with two code points,
  719. // or can be called with the input stream itself. In the latter case, the two
  720. // code points in question are the current input code point and the next input
  721. // code point, in that order.
  722. //
  723. // Note: This algorithm will not consume any additional code point.
  724. // If the first code point is not U+005C REVERSE SOLIDUS (\), return false.
  725. if (!is_reverse_solidus(values.first))
  726. return false;
  727. // Otherwise, if the second code point is a newline, return false.
  728. if (is_newline(values.second))
  729. return false;
  730. // Otherwise, return true.
  731. return true;
  732. }
  733. // https://www.w3.org/TR/css-syntax-3/#would-start-an-identifier
  734. bool Tokenizer::would_start_an_ident_sequence(U32Triplet values)
  735. {
  736. // This section describes how to check if three code points would start an ident sequence.
  737. // The algorithm described here can be called explicitly with three code points, or
  738. // can be called with the input stream itself. In the latter case, the three code
  739. // points in question are the current input code point and the next two input code
  740. // points, in that order.
  741. //
  742. // Note: This algorithm will not consume any additional code points.
  743. // Look at the first code point:
  744. // U+002D HYPHEN-MINUS
  745. if (is_hyphen_minus(values.first)) {
  746. // If the second code point is a name-start code point or a U+002D HYPHEN-MINUS,
  747. // or the second and third code points are a valid escape, return true.
  748. if (is_ident_start_code_point(values.second) || is_hyphen_minus(values.second) || is_valid_escape_sequence(values.to_twin_23()))
  749. return true;
  750. // Otherwise, return false.
  751. return false;
  752. }
  753. // name-start code point
  754. if (is_ident_start_code_point(values.first)) {
  755. // Return true.
  756. return true;
  757. }
  758. // U+005C REVERSE SOLIDUS (\)
  759. if (is_reverse_solidus(values.first)) {
  760. // If the first and second code points are a valid escape, return true.
  761. if (is_valid_escape_sequence(values.to_twin_12()))
  762. return true;
  763. // Otherwise, return false.
  764. return false;
  765. }
  766. // anything else
  767. // Return false.
  768. return false;
  769. }
  770. // https://www.w3.org/TR/css-syntax-3/#consume-string-token
  771. ErrorOr<Token> Tokenizer::consume_string_token(u32 ending_code_point)
  772. {
  773. // This section describes how to consume a string token from a stream of code points.
  774. // It returns either a <string-token> or <bad-string-token>.
  775. //
  776. // This algorithm may be called with an ending code point, which denotes the code point
  777. // that ends the string. If an ending code point is not specified, the current input
  778. // code point is used.
  779. // Initially create a <string-token> with its value set to the empty string.
  780. auto token = create_new_token(Token::Type::String);
  781. StringBuilder builder;
  782. auto make_token = [&]() -> ErrorOr<Token> {
  783. token.m_value = TRY(FlyString::from_utf8(builder.string_view()));
  784. return token;
  785. };
  786. // Repeatedly consume the next input code point from the stream:
  787. for (;;) {
  788. auto input = next_code_point();
  789. // ending code point
  790. if (input == ending_code_point)
  791. return make_token();
  792. // EOF
  793. if (is_eof(input)) {
  794. // This is a parse error. Return the <string-token>.
  795. log_parse_error();
  796. return make_token();
  797. }
  798. // newline
  799. if (is_newline(input)) {
  800. // This is a parse error. Reconsume the current input code point, create a
  801. // <bad-string-token>, and return it.
  802. reconsume_current_input_code_point();
  803. return create_new_token(Token::Type::BadString);
  804. }
  805. // U+005C REVERSE SOLIDUS (\)
  806. if (is_reverse_solidus(input)) {
  807. // If the next input code point is EOF, do nothing.
  808. auto next_input = peek_code_point();
  809. if (is_eof(next_input))
  810. continue;
  811. // Otherwise, if the next input code point is a newline, consume it.
  812. if (is_newline(next_input)) {
  813. (void)next_code_point();
  814. continue;
  815. }
  816. // Otherwise, (the stream starts with a valid escape) consume an escaped code
  817. // point and append the returned code point to the <string-token>’s value.
  818. auto escaped = consume_escaped_code_point();
  819. builder.append_code_point(escaped);
  820. continue;
  821. }
  822. // anything else
  823. // Append the current input code point to the <string-token>’s value.
  824. builder.append_code_point(input);
  825. }
  826. }
  827. // https://www.w3.org/TR/css-syntax-3/#consume-comment
  828. void Tokenizer::consume_comments()
  829. {
  830. // This section describes how to consume comments from a stream of code points.
  831. // It returns nothing.
  832. start:
  833. // If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A ASTERISK (*),
  834. // consume them and all following code points up to and including the first U+002A ASTERISK (*)
  835. // followed by a U+002F SOLIDUS (/), or up to an EOF code point. Return to the start of this step.
  836. //
  837. // If the preceding paragraph ended by consuming an EOF code point, this is a parse error.
  838. //
  839. // Return nothing.
  840. auto twin = peek_twin();
  841. if (!(is_solidus(twin.first) && is_asterisk(twin.second)))
  842. return;
  843. (void)next_code_point();
  844. (void)next_code_point();
  845. for (;;) {
  846. auto twin_inner = peek_twin();
  847. if (is_eof(twin_inner.first) || is_eof(twin_inner.second)) {
  848. log_parse_error();
  849. return;
  850. }
  851. if (is_asterisk(twin_inner.first) && is_solidus(twin_inner.second)) {
  852. (void)next_code_point();
  853. (void)next_code_point();
  854. goto start;
  855. }
  856. (void)next_code_point();
  857. }
  858. }
  859. // https://www.w3.org/TR/css-syntax-3/#consume-token
  860. ErrorOr<Token> Tokenizer::consume_a_token()
  861. {
  862. // This section describes how to consume a token from a stream of code points.
  863. // It will return a single token of any type.
  864. // Consume comments.
  865. consume_comments();
  866. // Consume the next input code point.
  867. auto input = next_code_point();
  868. // whitespace
  869. if (is_whitespace(input)) {
  870. dbgln_if(CSS_TOKENIZER_DEBUG, "is whitespace");
  871. // Consume as much whitespace as possible. Return a <whitespace-token>.
  872. consume_as_much_whitespace_as_possible();
  873. return create_new_token(Token::Type::Whitespace);
  874. }
  875. // U+0022 QUOTATION MARK (")
  876. if (is_quotation_mark(input)) {
  877. dbgln_if(CSS_TOKENIZER_DEBUG, "is quotation mark");
  878. // Consume a string token and return it.
  879. return consume_string_token(input);
  880. }
  881. // U+0023 NUMBER SIGN (#)
  882. if (is_number_sign(input)) {
  883. dbgln_if(CSS_TOKENIZER_DEBUG, "is number sign");
  884. // If the next input code point is an ident code point or the next two input code points
  885. // are a valid escape, then:
  886. auto next_input = peek_code_point();
  887. auto maybe_escape = peek_twin();
  888. if (is_ident_code_point(next_input) || is_valid_escape_sequence(maybe_escape)) {
  889. // 1. Create a <hash-token>.
  890. auto token = create_new_token(Token::Type::Hash);
  891. // 2. If the next 3 input code points would start an ident sequence, set the <hash-token>’s
  892. // type flag to "id".
  893. if (would_start_an_ident_sequence(peek_triplet()))
  894. token.m_hash_type = Token::HashType::Id;
  895. // 3. Consume an ident sequence, and set the <hash-token>’s value to the returned string.
  896. auto name = TRY(consume_an_ident_sequence());
  897. token.m_value = move(name);
  898. // 4. Return the <hash-token>.
  899. return token;
  900. }
  901. // Otherwise, return a <delim-token> with its value set to the current input code point.
  902. return create_value_token(Token::Type::Delim, input);
  903. }
  904. // U+0027 APOSTROPHE (')
  905. if (is_apostrophe(input)) {
  906. dbgln_if(CSS_TOKENIZER_DEBUG, "is apostrophe");
  907. // Consume a string token and return it.
  908. return consume_string_token(input);
  909. }
  910. // U+0028 LEFT PARENTHESIS (()
  911. if (is_left_paren(input)) {
  912. dbgln_if(CSS_TOKENIZER_DEBUG, "is left paren");
  913. // Return a <(-token>.
  914. return create_new_token(Token::Type::OpenParen);
  915. }
  916. // U+0029 RIGHT PARENTHESIS ())
  917. if (is_right_paren(input)) {
  918. dbgln_if(CSS_TOKENIZER_DEBUG, "is right paren");
  919. // Return a <)-token>.
  920. return create_new_token(Token::Type::CloseParen);
  921. }
  922. // U+002B PLUS SIGN (+)
  923. if (is_plus_sign(input)) {
  924. dbgln_if(CSS_TOKENIZER_DEBUG, "is plus sign");
  925. // If the input stream starts with a number, reconsume the current input code point,
  926. // consume a numeric token and return it.
  927. if (would_start_a_number(start_of_input_stream_triplet())) {
  928. reconsume_current_input_code_point();
  929. return consume_a_numeric_token();
  930. }
  931. // Otherwise, return a <delim-token> with its value set to the current input code point.
  932. return create_value_token(Token::Type::Delim, input);
  933. }
  934. // U+002C COMMA (,)
  935. if (is_comma(input)) {
  936. dbgln_if(CSS_TOKENIZER_DEBUG, "is comma");
  937. // Return a <comma-token>.
  938. return create_new_token(Token::Type::Comma);
  939. }
  940. // U+002D HYPHEN-MINUS (-)
  941. if (is_hyphen_minus(input)) {
  942. dbgln_if(CSS_TOKENIZER_DEBUG, "is hyphen minus");
  943. // If the input stream starts with a number, reconsume the current input code point,
  944. // consume a numeric token, and return it.
  945. if (would_start_a_number(start_of_input_stream_triplet())) {
  946. reconsume_current_input_code_point();
  947. return consume_a_numeric_token();
  948. }
  949. // Otherwise, if the next 2 input code points are U+002D HYPHEN-MINUS U+003E
  950. // GREATER-THAN SIGN (->), consume them and return a <CDC-token>.
  951. auto next_twin = peek_twin();
  952. if (is_hyphen_minus(next_twin.first) && is_greater_than_sign(next_twin.second)) {
  953. (void)next_code_point();
  954. (void)next_code_point();
  955. return create_new_token(Token::Type::CDC);
  956. }
  957. // Otherwise, if the input stream starts with an identifier, reconsume the current
  958. // input code point, consume an ident-like token, and return it.
  959. if (would_start_an_ident_sequence(start_of_input_stream_triplet())) {
  960. reconsume_current_input_code_point();
  961. return consume_an_ident_like_token();
  962. }
  963. // Otherwise, return a <delim-token> with its value set to the current input code point.
  964. return create_value_token(Token::Type::Delim, input);
  965. }
  966. // U+002E FULL STOP (.)
  967. if (is_full_stop(input)) {
  968. dbgln_if(CSS_TOKENIZER_DEBUG, "is full stop");
  969. // If the input stream starts with a number, reconsume the current input code point,
  970. // consume a numeric token, and return it.
  971. if (would_start_a_number(start_of_input_stream_triplet())) {
  972. reconsume_current_input_code_point();
  973. return consume_a_numeric_token();
  974. }
  975. // Otherwise, return a <delim-token> with its value set to the current input code point.
  976. return create_value_token(Token::Type::Delim, input);
  977. }
  978. // U+003A COLON (:)
  979. if (is_colon(input)) {
  980. dbgln_if(CSS_TOKENIZER_DEBUG, "is colon");
  981. // Return a <colon-token>.
  982. return create_new_token(Token::Type::Colon);
  983. }
  984. // U+003B SEMICOLON (;)
  985. if (is_semicolon(input)) {
  986. dbgln_if(CSS_TOKENIZER_DEBUG, "is semicolon");
  987. // Return a <semicolon-token>.
  988. return create_new_token(Token::Type::Semicolon);
  989. }
  990. // U+003C LESS-THAN SIGN (<)
  991. if (is_less_than_sign(input)) {
  992. dbgln_if(CSS_TOKENIZER_DEBUG, "is less than");
  993. // If the next 3 input code points are U+0021 EXCLAMATION MARK U+002D HYPHEN-MINUS
  994. // U+002D HYPHEN-MINUS (!--), consume them and return a <CDO-token>.
  995. auto maybe_cdo = peek_triplet();
  996. if (is_exclamation_mark(maybe_cdo.first) && is_hyphen_minus(maybe_cdo.second) && is_hyphen_minus(maybe_cdo.third)) {
  997. (void)next_code_point();
  998. (void)next_code_point();
  999. (void)next_code_point();
  1000. return create_new_token(Token::Type::CDO);
  1001. }
  1002. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1003. return create_value_token(Token::Type::Delim, input);
  1004. }
  1005. // U+0040 COMMERCIAL AT (@)
  1006. if (is_at(input)) {
  1007. dbgln_if(CSS_TOKENIZER_DEBUG, "is at");
  1008. // If the next 3 input code points would start an ident sequence, consume an ident sequence, create
  1009. // an <at-keyword-token> with its value set to the returned value, and return it.
  1010. if (would_start_an_ident_sequence(peek_triplet())) {
  1011. auto name = TRY(consume_an_ident_sequence());
  1012. return create_value_token(Token::Type::AtKeyword, move(name));
  1013. }
  1014. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1015. return create_value_token(Token::Type::Delim, input);
  1016. }
  1017. // U+005B LEFT SQUARE BRACKET ([)
  1018. if (is_open_square_bracket(input)) {
  1019. dbgln_if(CSS_TOKENIZER_DEBUG, "is open square");
  1020. // Return a <[-token>.
  1021. return create_new_token(Token::Type::OpenSquare);
  1022. }
  1023. // U+005C REVERSE SOLIDUS (\)
  1024. if (is_reverse_solidus(input)) {
  1025. dbgln_if(CSS_TOKENIZER_DEBUG, "is reverse solidus");
  1026. // If the input stream starts with a valid escape, reconsume the current input code point,
  1027. // consume an ident-like token, and return it.
  1028. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  1029. reconsume_current_input_code_point();
  1030. return consume_an_ident_like_token();
  1031. }
  1032. // Otherwise, this is a parse error. Return a <delim-token> with its value set to the
  1033. // current input code point.
  1034. log_parse_error();
  1035. return create_value_token(Token::Type::Delim, input);
  1036. }
  1037. // U+005D RIGHT SQUARE BRACKET (])
  1038. if (is_closed_square_bracket(input)) {
  1039. dbgln_if(CSS_TOKENIZER_DEBUG, "is closed square");
  1040. // Return a <]-token>.
  1041. return create_new_token(Token::Type::CloseSquare);
  1042. }
  1043. // U+007B LEFT CURLY BRACKET ({)
  1044. if (is_open_curly_bracket(input)) {
  1045. dbgln_if(CSS_TOKENIZER_DEBUG, "is open curly");
  1046. // Return a <{-token>.
  1047. return create_new_token(Token::Type::OpenCurly);
  1048. }
  1049. // U+007D RIGHT CURLY BRACKET (})
  1050. if (is_closed_curly_bracket(input)) {
  1051. dbgln_if(CSS_TOKENIZER_DEBUG, "is closed curly");
  1052. // Return a <}-token>.
  1053. return create_new_token(Token::Type::CloseCurly);
  1054. }
  1055. // digit
  1056. if (is_ascii_digit(input)) {
  1057. dbgln_if(CSS_TOKENIZER_DEBUG, "is digit");
  1058. // Reconsume the current input code point, consume a numeric token, and return it.
  1059. reconsume_current_input_code_point();
  1060. return consume_a_numeric_token();
  1061. }
  1062. // name-start code point
  1063. if (is_ident_start_code_point(input)) {
  1064. dbgln_if(CSS_TOKENIZER_DEBUG, "is name start");
  1065. // Reconsume the current input code point, consume an ident-like token, and return it.
  1066. reconsume_current_input_code_point();
  1067. return consume_an_ident_like_token();
  1068. }
  1069. // EOF
  1070. if (is_eof(input)) {
  1071. // Return an <EOF-token>.
  1072. return create_new_token(Token::Type::EndOfFile);
  1073. }
  1074. // anything else
  1075. dbgln_if(CSS_TOKENIZER_DEBUG, "is delimiter");
  1076. // Return a <delim-token> with its value set to the current input code point.
  1077. return create_value_token(Token::Type::Delim, input);
  1078. }
  1079. }