Tokenizer.cpp 46 KB

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