Tokenizer.cpp 46 KB

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