Tokenizer.cpp 46 KB

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