Tokenizer.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. // U+FFFD REPLACEMENT CHARACTER (�)
  15. #define REPLACEMENT_CHARACTER 0xFFFD
  16. static constexpr 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. U32Twin Tokenizer::start_of_input_stream_twin()
  261. {
  262. U32Twin twin;
  263. // FIXME: Reconsuming just to read the current code point again is weird.
  264. reconsume_current_input_code_point();
  265. twin.first = next_code_point();
  266. twin.second = peek_code_point();
  267. return twin;
  268. }
  269. U32Triplet Tokenizer::start_of_input_stream_triplet()
  270. {
  271. U32Triplet triplet;
  272. // FIXME: Reconsuming just to read the current code point again is weird.
  273. reconsume_current_input_code_point();
  274. triplet.first = next_code_point();
  275. auto next_two = peek_twin();
  276. triplet.second = next_two.first;
  277. triplet.third = next_two.second;
  278. return triplet;
  279. }
  280. Token Tokenizer::create_new_token(Token::Type type)
  281. {
  282. Token token = {};
  283. token.m_type = type;
  284. return token;
  285. }
  286. Token Tokenizer::create_eof_token()
  287. {
  288. return create_new_token(Token::Type::EndOfFile);
  289. }
  290. Token Tokenizer::create_value_token(Token::Type type, String value)
  291. {
  292. Token token;
  293. token.m_type = type;
  294. token.m_value = move(value);
  295. return token;
  296. }
  297. Token Tokenizer::create_value_token(Token::Type type, u32 value)
  298. {
  299. Token token = {};
  300. token.m_type = type;
  301. // FIXME: Avoid temporary StringBuilder here
  302. StringBuilder builder;
  303. builder.append_code_point(value);
  304. token.m_value = builder.to_string();
  305. return token;
  306. }
  307. // https://www.w3.org/TR/css-syntax-3/#consume-escaped-code-point
  308. u32 Tokenizer::consume_escaped_code_point()
  309. {
  310. // This section describes how to consume an escaped code point.
  311. // It assumes that the U+005C REVERSE SOLIDUS (\) has already been consumed and that the next
  312. // input code point has already been verified to be part of a valid escape.
  313. // It will return a code point.
  314. // Consume the next input code point.
  315. auto input = next_code_point();
  316. // hex digit
  317. if (is_ascii_hex_digit(input)) {
  318. // Consume as many hex digits as possible, but no more than 5.
  319. // Note that this means 1-6 hex digits have been consumed in total.
  320. StringBuilder builder;
  321. builder.append_code_point(input);
  322. size_t counter = 0;
  323. while (is_ascii_hex_digit(peek_code_point()) && counter++ < 5) {
  324. builder.append_code_point(next_code_point());
  325. }
  326. // If the next input code point is whitespace, consume it as well.
  327. if (is_whitespace(peek_code_point())) {
  328. (void)next_code_point();
  329. }
  330. // Interpret the hex digits as a hexadecimal number.
  331. auto unhexed = strtoul(builder.to_string().characters(), nullptr, 16);
  332. // If this number is zero, or is for a surrogate, or is greater than the maximum allowed
  333. // code point, return U+FFFD REPLACEMENT CHARACTER (�).
  334. if (unhexed == 0 || is_unicode_surrogate(unhexed) || is_greater_than_maximum_allowed_code_point(unhexed)) {
  335. return REPLACEMENT_CHARACTER;
  336. }
  337. // Otherwise, return the code point with that value.
  338. return unhexed;
  339. }
  340. // EOF
  341. if (is_eof(input)) {
  342. // This is a parse error. Return U+FFFD REPLACEMENT CHARACTER (�).
  343. log_parse_error();
  344. return REPLACEMENT_CHARACTER;
  345. }
  346. // anything else
  347. // Return the current input code point.
  348. return input;
  349. }
  350. // https://www.w3.org/TR/css-syntax-3/#consume-ident-like-token
  351. Token Tokenizer::consume_an_ident_like_token()
  352. {
  353. // This section describes how to consume an ident-like token from a stream of code points.
  354. // It returns an <ident-token>, <function-token>, <url-token>, or <bad-url-token>.
  355. // Consume a name, and let string be the result.
  356. auto string = consume_a_name();
  357. // If string’s value is an ASCII case-insensitive match for "url", and the next input code
  358. // point is U+0028 LEFT PARENTHESIS ((), consume it.
  359. if (string.equals_ignoring_case("url") && is_left_paren(peek_code_point())) {
  360. (void)next_code_point();
  361. // While the next two input code points are whitespace, consume the next input code point.
  362. for (;;) {
  363. auto maybe_whitespace = peek_twin();
  364. if (!(is_whitespace(maybe_whitespace.first) && is_whitespace(maybe_whitespace.second))) {
  365. break;
  366. }
  367. (void)next_code_point();
  368. }
  369. // If the next one or two input code points are U+0022 QUOTATION MARK ("), U+0027 APOSTROPHE ('),
  370. // or whitespace followed by U+0022 QUOTATION MARK (") or U+0027 APOSTROPHE ('), then create a
  371. // <function-token> with its value set to string and return it.
  372. auto next_two = peek_twin();
  373. 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)))) {
  374. return create_value_token(Token::Type::Function, string);
  375. }
  376. // Otherwise, consume a url token, and return it.
  377. return consume_a_url_token();
  378. }
  379. // Otherwise, if the next input code point is U+0028 LEFT PARENTHESIS ((), consume it.
  380. if (is_left_paren(peek_code_point())) {
  381. (void)next_code_point();
  382. // Create a <function-token> with its value set to string and return it.
  383. return create_value_token(Token::Type::Function, string);
  384. }
  385. // Otherwise, create an <ident-token> with its value set to string and return it.
  386. return create_value_token(Token::Type::Ident, string);
  387. }
  388. // https://www.w3.org/TR/css-syntax-3/#consume-number
  389. Number Tokenizer::consume_a_number()
  390. {
  391. // This section describes how to consume a number from a stream of code points.
  392. // It returns a numeric value, and a type which is either "integer" or "number".
  393. //
  394. // Note: This algorithm does not do the verification of the first few code points
  395. // that are necessary to ensure a number can be obtained from the stream. Ensure
  396. // that the stream starts with a number before calling this algorithm.
  397. // Execute the following steps in order:
  398. // 1. Initially set type to "integer". Let repr be the empty string.
  399. StringBuilder repr;
  400. Number::Type type = Number::Type::Integer;
  401. // 2. If the next input code point is U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-),
  402. // consume it and append it to repr.
  403. bool has_explicit_sign = false;
  404. auto next_input = peek_code_point();
  405. if (is_plus_sign(next_input) || is_hyphen_minus(next_input)) {
  406. has_explicit_sign = true;
  407. repr.append_code_point(next_code_point());
  408. }
  409. // 3. While the next input code point is a digit, consume it and append it to repr.
  410. for (;;) {
  411. auto digits = peek_code_point();
  412. if (!is_ascii_digit(digits))
  413. break;
  414. repr.append_code_point(next_code_point());
  415. }
  416. // 4. If the next 2 input code points are U+002E FULL STOP (.) followed by a digit, then:
  417. auto maybe_number = peek_twin();
  418. if (is_full_stop(maybe_number.first) && is_ascii_digit(maybe_number.second)) {
  419. // 1. Consume them.
  420. // 2. Append them to repr.
  421. repr.append_code_point(next_code_point());
  422. repr.append_code_point(next_code_point());
  423. // 3. Set type to "number".
  424. type = Number::Type::Number;
  425. // 4. While the next input code point is a digit, consume it and append it to repr.
  426. for (;;) {
  427. auto digit = peek_code_point();
  428. if (!is_ascii_digit(digit))
  429. break;
  430. repr.append_code_point(next_code_point());
  431. }
  432. }
  433. // 5. If the next 2 or 3 input code points are U+0045 LATIN CAPITAL LETTER E (E) or
  434. // U+0065 LATIN SMALL LETTER E (e), optionally followed by U+002D HYPHEN-MINUS (-)
  435. // or U+002B PLUS SIGN (+), followed by a digit, then:
  436. auto maybe_exp = peek_triplet();
  437. if (is_E(maybe_exp.first) || is_e(maybe_exp.first)) {
  438. // 1. Consume them.
  439. // 2. Append them to repr.
  440. // FIXME: These conditions should be part of step 5 above.
  441. if (is_plus_sign(maybe_exp.second) || is_hyphen_minus(maybe_exp.second)) {
  442. if (is_ascii_digit(maybe_exp.third)) {
  443. repr.append_code_point(next_code_point());
  444. repr.append_code_point(next_code_point());
  445. repr.append_code_point(next_code_point());
  446. }
  447. } else if (is_ascii_digit(maybe_exp.second)) {
  448. repr.append_code_point(next_code_point());
  449. repr.append_code_point(next_code_point());
  450. }
  451. // 3. Set type to "number".
  452. type = Number::Type::Number;
  453. // 4. While the next input code point is a digit, consume it and append it to repr.
  454. for (;;) {
  455. auto digits = peek_code_point();
  456. if (!is_ascii_digit(digits))
  457. break;
  458. repr.append_code_point(next_code_point());
  459. }
  460. }
  461. // 6. Convert repr to a number, and set the value to the returned value.
  462. auto value = convert_a_string_to_a_number(repr.string_view());
  463. // 7. Return value and type.
  464. if (type == Number::Type::Integer && has_explicit_sign)
  465. return Number { Number::Type::IntegerWithExplicitSign, value };
  466. return Number { type, value };
  467. }
  468. // https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
  469. float Tokenizer::convert_a_string_to_a_number(StringView string)
  470. {
  471. auto code_point_at = [&](size_t index) -> u32 {
  472. if (index < string.length())
  473. return string[index];
  474. return TOKENIZER_EOF;
  475. };
  476. // This algorithm does not do any verification to ensure that the string contains only a number.
  477. // Ensure that the string contains only a valid CSS number before calling this algorithm.
  478. // Divide the string into seven components, in order from left to right:
  479. size_t position = 0;
  480. // 1. A sign: a single U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-), or the empty string.
  481. // Let s [sign] be the number -1 if the sign is U+002D HYPHEN-MINUS (-); otherwise, let s be the number 1.
  482. int sign = 1;
  483. if (is_plus_sign(code_point_at(position)) || is_hyphen_minus(code_point_at(position))) {
  484. sign = is_hyphen_minus(code_point_at(position)) ? -1 : 1;
  485. position++;
  486. }
  487. // 2. An integer part: zero or more digits.
  488. // If there is at least one digit, let i [integer_part] be the number formed by interpreting the digits
  489. // as a base-10 integer; otherwise, let i be the number 0.
  490. i64 integer_part = 0;
  491. while (is_ascii_digit(code_point_at(position))) {
  492. integer_part = (integer_part * 10) + (code_point_at(position) - '0');
  493. position++;
  494. }
  495. // 3. A decimal point: a single U+002E FULL STOP (.), or the empty string.
  496. if (is_full_stop(code_point_at(position)))
  497. position++;
  498. // 4. A fractional part: zero or more digits.
  499. // If there is at least one digit, let f [fractional_part] be the number formed by interpreting the digits
  500. // as a base-10 integer and d [fractional_digits] be the number of digits; otherwise, let f and d be the number 0.
  501. i64 fractional_part = 0;
  502. int fractional_digits = 0;
  503. while (is_ascii_digit(code_point_at(position))) {
  504. fractional_part = (fractional_part * 10) + (code_point_at(position) - '0');
  505. position++;
  506. fractional_digits++;
  507. }
  508. // 5. An exponent indicator: a single U+0045 LATIN CAPITAL LETTER E (E) or U+0065 LATIN SMALL LETTER E (e),
  509. // or the empty string.
  510. if (is_e(code_point_at(position)) || is_E(code_point_at(position)))
  511. position++;
  512. // 6. An exponent sign: a single U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-), or the empty string.
  513. // Let t [exponent_sign] be the number -1 if the sign is U+002D HYPHEN-MINUS (-); otherwise, let t be the number 1.
  514. int exponent_sign = 1;
  515. if (is_plus_sign(code_point_at(position)) || is_hyphen_minus(code_point_at(position))) {
  516. exponent_sign = is_hyphen_minus(code_point_at(position)) ? -1 : 1;
  517. position++;
  518. }
  519. // 7. An exponent: zero or more digits.
  520. // If there is at least one digit, let e [exponent] be the number formed by interpreting the digits as a
  521. // base-10 integer; otherwise, let e be the number 0.
  522. i64 exponent = 0;
  523. while (is_ascii_digit(code_point_at(position))) {
  524. exponent = (exponent * 10) + (code_point_at(position) - '0');
  525. position++;
  526. }
  527. // NOTE: We checked before calling this function that the string is a valid number,
  528. // so if there is anything at the end, something has gone wrong!
  529. VERIFY(position == string.length());
  530. // Return the number s·(i + f·10^-d)·10^te.
  531. return sign * (integer_part + fractional_part * powf(10, -fractional_digits)) * powf(10, exponent_sign * exponent);
  532. }
  533. // https://www.w3.org/TR/css-syntax-3/#consume-name
  534. String Tokenizer::consume_a_name()
  535. {
  536. // This section describes how to consume a name from a stream of code points.
  537. // It returns a string containing the largest name that can be formed from adjacent
  538. // code points in the stream, starting from the first.
  539. //
  540. // Note: This algorithm does not do the verification of the first few code points that
  541. // are necessary to ensure the returned code points would constitute an <ident-token>.
  542. // If that is the intended use, ensure that the stream starts with an identifier before
  543. // calling this algorithm.
  544. // Let result initially be an empty string.
  545. StringBuilder result;
  546. // Repeatedly consume the next input code point from the stream:
  547. for (;;) {
  548. auto input = next_code_point();
  549. if (is_eof(input))
  550. break;
  551. // name code point
  552. if (is_name_code_point(input)) {
  553. // Append the code point to result.
  554. result.append_code_point(input);
  555. continue;
  556. }
  557. // the stream starts with a valid escape
  558. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  559. // Consume an escaped code point. Append the returned code point to result.
  560. result.append_code_point(consume_escaped_code_point());
  561. continue;
  562. }
  563. // anything else
  564. // Reconsume the current input code point. Return result.
  565. reconsume_current_input_code_point();
  566. break;
  567. }
  568. return result.to_string();
  569. }
  570. // https://www.w3.org/TR/css-syntax-3/#consume-url-token
  571. Token Tokenizer::consume_a_url_token()
  572. {
  573. // This section describes how to consume a url token from a stream of code points.
  574. // It returns either a <url-token> or a <bad-url-token>.
  575. //
  576. // Note: This algorithm assumes that the initial "url(" has already been consumed.
  577. // This algorithm also assumes that it’s being called to consume an "unquoted" value,
  578. // like url(foo). A quoted value, like url("foo"), is parsed as a <function-token>.
  579. // Consume an ident-like token automatically handles this distinction; this algorithm
  580. // shouldn’t be called directly otherwise.
  581. // 1. Initially create a <url-token> with its value set to the empty string.
  582. auto token = create_new_token(Token::Type::Url);
  583. StringBuilder builder;
  584. // 2. Consume as much whitespace as possible.
  585. consume_as_much_whitespace_as_possible();
  586. auto make_token = [&]() {
  587. token.m_value = builder.to_string();
  588. return token;
  589. };
  590. // 3. Repeatedly consume the next input code point from the stream:
  591. for (;;) {
  592. auto input = next_code_point();
  593. // U+0029 RIGHT PARENTHESIS ())
  594. if (is_right_paren(input)) {
  595. // Return the <url-token>.
  596. return make_token();
  597. }
  598. // EOF
  599. if (is_eof(input)) {
  600. // This is a parse error. Return the <url-token>.
  601. log_parse_error();
  602. return make_token();
  603. }
  604. // whitespace
  605. if (is_whitespace(input)) {
  606. // Consume as much whitespace as possible.
  607. consume_as_much_whitespace_as_possible();
  608. // If the next input code point is U+0029 RIGHT PARENTHESIS ()) or EOF, consume it
  609. // and return the <url-token> (if EOF was encountered, this is a parse error);
  610. input = peek_code_point();
  611. if (is_right_paren(input)) {
  612. (void)next_code_point();
  613. return make_token();
  614. }
  615. if (is_eof(input)) {
  616. (void)next_code_point();
  617. log_parse_error();
  618. return make_token();
  619. }
  620. // otherwise, consume the remnants of a bad url, create a <bad-url-token>, and return it.
  621. consume_the_remnants_of_a_bad_url();
  622. return create_new_token(Token::Type::BadUrl);
  623. }
  624. // U+0022 QUOTATION MARK (")
  625. // U+0027 APOSTROPHE (')
  626. // U+0028 LEFT PARENTHESIS (()
  627. // non-printable code point
  628. if (is_quotation_mark(input) || is_apostrophe(input) || is_left_paren(input) || is_non_printable(input)) {
  629. // This is a parse error. Consume the remnants of a bad url, create a <bad-url-token>, and return it.
  630. log_parse_error();
  631. consume_the_remnants_of_a_bad_url();
  632. return create_new_token(Token::Type::BadUrl);
  633. }
  634. // U+005C REVERSE SOLIDUS (\)
  635. if (is_reverse_solidus(input)) {
  636. // If the stream starts with a valid escape,
  637. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  638. // consume an escaped code point and append the returned code point to the <url-token>’s value.
  639. builder.append_code_point(consume_escaped_code_point());
  640. continue;
  641. } else {
  642. // Otherwise, this is a parse error.
  643. log_parse_error();
  644. // Consume the remnants of a bad url, create a <bad-url-token>, and return it.
  645. consume_the_remnants_of_a_bad_url();
  646. return create_new_token(Token::Type::BadUrl);
  647. }
  648. }
  649. // anything else
  650. // Append the current input code point to the <url-token>’s value.
  651. builder.append_code_point(input);
  652. }
  653. }
  654. // https://www.w3.org/TR/css-syntax-3/#consume-remnants-of-bad-url
  655. void Tokenizer::consume_the_remnants_of_a_bad_url()
  656. {
  657. // This section describes how to consume the remnants of a bad url from a stream of code points,
  658. // "cleaning up" after the tokenizer realizes that it’s in the middle of a <bad-url-token> rather
  659. // than a <url-token>. It returns nothing; its sole use is to consume enough of the input stream
  660. // to reach a recovery point where normal tokenizing can resume.
  661. // Repeatedly consume the next input code point from the stream:
  662. for (;;) {
  663. auto input = next_code_point();
  664. // U+0029 RIGHT PARENTHESIS ())
  665. // EOF
  666. if (is_eof(input) || is_right_paren(input)) {
  667. // Return.
  668. return;
  669. }
  670. // the input stream starts with a valid escape
  671. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  672. // Consume an escaped code point.
  673. // This allows an escaped right parenthesis ("\)") to be encountered without ending
  674. // the <bad-url-token>. This is otherwise identical to the "anything else" clause.
  675. (void)consume_escaped_code_point();
  676. }
  677. // anything else
  678. // Do nothing.
  679. }
  680. }
  681. void Tokenizer::consume_as_much_whitespace_as_possible()
  682. {
  683. while (is_whitespace(peek_code_point())) {
  684. (void)next_code_point();
  685. }
  686. }
  687. void Tokenizer::reconsume_current_input_code_point()
  688. {
  689. m_utf8_iterator = m_prev_utf8_iterator;
  690. m_position = m_prev_position;
  691. }
  692. // https://www.w3.org/TR/css-syntax-3/#consume-numeric-token
  693. Token Tokenizer::consume_a_numeric_token()
  694. {
  695. // This section describes how to consume a numeric token from a stream of code points.
  696. // It returns either a <number-token>, <percentage-token>, or <dimension-token>.
  697. // Consume a number and let number be the result.
  698. auto number = consume_a_number();
  699. // If the next 3 input code points would start an identifier, then:
  700. if (would_start_an_identifier(peek_triplet())) {
  701. // 1. Create a <dimension-token> with the same value and type flag as number,
  702. // and a unit set initially to the empty string.
  703. auto token = create_new_token(Token::Type::Dimension);
  704. token.m_number_value = number;
  705. // 2. Consume a name. Set the <dimension-token>’s unit to the returned value.
  706. auto unit = consume_a_name();
  707. VERIFY(!unit.is_empty());
  708. // NOTE: We intentionally store this in the `value`, to save space.
  709. token.m_value = move(unit);
  710. // 3. Return the <dimension-token>.
  711. return token;
  712. }
  713. // Otherwise, if the next input code point is U+0025 PERCENTAGE SIGN (%), consume it.
  714. if (is_percent(peek_code_point())) {
  715. (void)next_code_point();
  716. // Create a <percentage-token> with the same value as number, and return it.
  717. auto token = create_new_token(Token::Type::Percentage);
  718. token.m_number_value = number;
  719. return token;
  720. }
  721. // Otherwise, create a <number-token> with the same value and type flag as number, and return it.
  722. auto token = create_new_token(Token::Type::Number);
  723. token.m_number_value = number;
  724. return token;
  725. }
  726. // https://www.w3.org/TR/css-syntax-3/#starts-with-a-number
  727. bool Tokenizer::would_start_a_number(U32Triplet values)
  728. {
  729. // This section describes how to check if three code points would start a number.
  730. // The algorithm described here can be called explicitly with three code points,
  731. // or can be called with the input stream itself. In the latter case, the three
  732. // code points in question are the current input code point and the next two input
  733. // code points, in that order.
  734. //
  735. // Note: This algorithm will not consume any additional code points.
  736. // Look at the first code point:
  737. // U+002B PLUS SIGN (+)
  738. // U+002D HYPHEN-MINUS (-)
  739. if (is_plus_sign(values.first) || is_hyphen_minus(values.first)) {
  740. // If the second code point is a digit, return true.
  741. if (is_ascii_digit(values.second))
  742. return true;
  743. // Otherwise, if the second code point is a U+002E FULL STOP (.) and the third
  744. // code point is a digit, return true.
  745. if (is_full_stop(values.second) && is_ascii_digit(values.third))
  746. return true;
  747. // Otherwise, return false.
  748. return false;
  749. }
  750. // U+002E FULL STOP (.)
  751. if (is_full_stop(values.first))
  752. // If the second code point is a digit, return true. Otherwise, return false.
  753. return is_ascii_digit(values.second);
  754. // digit
  755. if (is_ascii_digit(values.first))
  756. // Return true.
  757. return true;
  758. // anything else
  759. // Return false.
  760. return false;
  761. }
  762. // https://www.w3.org/TR/css-syntax-3/#starts-with-a-valid-escape
  763. bool Tokenizer::is_valid_escape_sequence(U32Twin values)
  764. {
  765. // This section describes how to check if two code points are a valid escape.
  766. // The algorithm described here can be called explicitly with two code points,
  767. // or can be called with the input stream itself. In the latter case, the two
  768. // code points in question are the current input code point and the next input
  769. // code point, in that order.
  770. //
  771. // Note: This algorithm will not consume any additional code point.
  772. // If the first code point is not U+005C REVERSE SOLIDUS (\), return false.
  773. if (!is_reverse_solidus(values.first))
  774. return false;
  775. // Otherwise, if the second code point is a newline, return false.
  776. if (is_newline(values.second))
  777. return false;
  778. // Otherwise, return true.
  779. return true;
  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. continue;
  869. }
  870. // anything else
  871. // Append the current input code point to the <string-token>’s value.
  872. builder.append_code_point(input);
  873. }
  874. }
  875. // https://www.w3.org/TR/css-syntax-3/#consume-comment
  876. void Tokenizer::consume_comments()
  877. {
  878. // This section describes how to consume comments from a stream of code points.
  879. // It returns nothing.
  880. start:
  881. // If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A ASTERISK (*),
  882. // consume them and all following code points up to and including the first U+002A ASTERISK (*)
  883. // followed by a U+002F SOLIDUS (/), or up to an EOF code point. Return to the start of this step.
  884. //
  885. // If the preceding paragraph ended by consuming an EOF code point, this is a parse error.
  886. //
  887. // Return nothing.
  888. auto twin = peek_twin();
  889. if (!(is_solidus(twin.first) && is_asterisk(twin.second)))
  890. return;
  891. (void)next_code_point();
  892. (void)next_code_point();
  893. for (;;) {
  894. auto twin_inner = peek_twin();
  895. if (is_eof(twin_inner.first) || is_eof(twin_inner.second)) {
  896. log_parse_error();
  897. return;
  898. }
  899. if (is_asterisk(twin_inner.first) && is_solidus(twin_inner.second)) {
  900. (void)next_code_point();
  901. (void)next_code_point();
  902. goto start;
  903. }
  904. (void)next_code_point();
  905. }
  906. }
  907. // https://www.w3.org/TR/css-syntax-3/#consume-token
  908. Token Tokenizer::consume_a_token()
  909. {
  910. // This section describes how to consume a token from a stream of code points.
  911. // It will return a single token of any type.
  912. // Consume comments.
  913. consume_comments();
  914. // Consume the next input code point.
  915. auto input = next_code_point();
  916. // whitespace
  917. if (is_whitespace(input)) {
  918. dbgln_if(CSS_TOKENIZER_DEBUG, "is whitespace");
  919. // Consume as much whitespace as possible. Return a <whitespace-token>.
  920. consume_as_much_whitespace_as_possible();
  921. return create_new_token(Token::Type::Whitespace);
  922. }
  923. // U+0022 QUOTATION MARK (")
  924. if (is_quotation_mark(input)) {
  925. dbgln_if(CSS_TOKENIZER_DEBUG, "is quotation mark");
  926. // Consume a string token and return it.
  927. return consume_string_token(input);
  928. }
  929. // U+0023 NUMBER SIGN (#)
  930. if (is_number_sign(input)) {
  931. dbgln_if(CSS_TOKENIZER_DEBUG, "is number sign");
  932. // If the next input code point is a name code point or the next two input code points
  933. // are a valid escape, then:
  934. auto next_input = peek_code_point();
  935. auto maybe_escape = peek_twin();
  936. if (is_name_code_point(next_input) || is_valid_escape_sequence(maybe_escape)) {
  937. // 1. Create a <hash-token>.
  938. auto token = create_new_token(Token::Type::Hash);
  939. // 2. If the next 3 input code points would start an identifier, set the <hash-token>’s
  940. // type flag to "id".
  941. if (would_start_an_identifier(peek_triplet()))
  942. token.m_hash_type = Token::HashType::Id;
  943. // 3. Consume a name, and set the <hash-token>’s value to the returned string.
  944. auto name = consume_a_name();
  945. token.m_value = move(name);
  946. // 4. Return the <hash-token>.
  947. return token;
  948. }
  949. // Otherwise, return a <delim-token> with its value set to the current input code point.
  950. return create_value_token(Token::Type::Delim, input);
  951. }
  952. // U+0027 APOSTROPHE (')
  953. if (is_apostrophe(input)) {
  954. dbgln_if(CSS_TOKENIZER_DEBUG, "is apostrophe");
  955. // Consume a string token and return it.
  956. return consume_string_token(input);
  957. }
  958. // U+0028 LEFT PARENTHESIS (()
  959. if (is_left_paren(input)) {
  960. dbgln_if(CSS_TOKENIZER_DEBUG, "is left paren");
  961. // Return a <(-token>.
  962. return create_new_token(Token::Type::OpenParen);
  963. }
  964. // U+0029 RIGHT PARENTHESIS ())
  965. if (is_right_paren(input)) {
  966. dbgln_if(CSS_TOKENIZER_DEBUG, "is right paren");
  967. // Return a <)-token>.
  968. return create_new_token(Token::Type::CloseParen);
  969. }
  970. // U+002B PLUS SIGN (+)
  971. if (is_plus_sign(input)) {
  972. dbgln_if(CSS_TOKENIZER_DEBUG, "is plus sign");
  973. // If the input stream starts with a number, reconsume the current input code point,
  974. // consume a numeric token and return it.
  975. if (would_start_a_number(start_of_input_stream_triplet())) {
  976. reconsume_current_input_code_point();
  977. return consume_a_numeric_token();
  978. }
  979. // Otherwise, return a <delim-token> with its value set to the current input code point.
  980. return create_value_token(Token::Type::Delim, input);
  981. }
  982. // U+002C COMMA (,)
  983. if (is_comma(input)) {
  984. dbgln_if(CSS_TOKENIZER_DEBUG, "is comma");
  985. // Return a <comma-token>.
  986. return create_new_token(Token::Type::Comma);
  987. }
  988. // U+002D HYPHEN-MINUS (-)
  989. if (is_hyphen_minus(input)) {
  990. dbgln_if(CSS_TOKENIZER_DEBUG, "is hyphen minus");
  991. // If the input stream starts with a number, reconsume the current input code point,
  992. // consume a numeric token, and return it.
  993. if (would_start_a_number(start_of_input_stream_triplet())) {
  994. reconsume_current_input_code_point();
  995. return consume_a_numeric_token();
  996. }
  997. // Otherwise, if the next 2 input code points are U+002D HYPHEN-MINUS U+003E
  998. // GREATER-THAN SIGN (->), consume them and return a <CDC-token>.
  999. auto next_twin = peek_twin();
  1000. if (is_hyphen_minus(next_twin.first) && is_greater_than_sign(next_twin.second)) {
  1001. (void)next_code_point();
  1002. (void)next_code_point();
  1003. return create_new_token(Token::Type::CDC);
  1004. }
  1005. // Otherwise, if the input stream starts with an identifier, reconsume the current
  1006. // input code point, consume an ident-like token, and return it.
  1007. if (would_start_an_identifier(start_of_input_stream_triplet())) {
  1008. reconsume_current_input_code_point();
  1009. return consume_an_ident_like_token();
  1010. }
  1011. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1012. return create_value_token(Token::Type::Delim, input);
  1013. }
  1014. // U+002E FULL STOP (.)
  1015. if (is_full_stop(input)) {
  1016. dbgln_if(CSS_TOKENIZER_DEBUG, "is full stop");
  1017. // If the input stream starts with a number, reconsume the current input code point,
  1018. // consume a numeric token, and return it.
  1019. if (would_start_a_number(start_of_input_stream_triplet())) {
  1020. reconsume_current_input_code_point();
  1021. return consume_a_numeric_token();
  1022. }
  1023. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1024. return create_value_token(Token::Type::Delim, input);
  1025. }
  1026. // U+003A COLON (:)
  1027. if (is_colon(input)) {
  1028. dbgln_if(CSS_TOKENIZER_DEBUG, "is colon");
  1029. // Return a <colon-token>.
  1030. return create_new_token(Token::Type::Colon);
  1031. }
  1032. // U+003B SEMICOLON (;)
  1033. if (is_semicolon(input)) {
  1034. dbgln_if(CSS_TOKENIZER_DEBUG, "is semicolon");
  1035. // Return a <semicolon-token>.
  1036. return create_new_token(Token::Type::Semicolon);
  1037. }
  1038. // U+003C LESS-THAN SIGN (<)
  1039. if (is_less_than_sign(input)) {
  1040. dbgln_if(CSS_TOKENIZER_DEBUG, "is less than");
  1041. // If the next 3 input code points are U+0021 EXCLAMATION MARK U+002D HYPHEN-MINUS
  1042. // U+002D HYPHEN-MINUS (!--), consume them and return a <CDO-token>.
  1043. auto maybe_cdo = peek_triplet();
  1044. if (is_exclamation_mark(maybe_cdo.first) && is_hyphen_minus(maybe_cdo.second) && is_hyphen_minus(maybe_cdo.third)) {
  1045. (void)next_code_point();
  1046. (void)next_code_point();
  1047. (void)next_code_point();
  1048. return create_new_token(Token::Type::CDO);
  1049. }
  1050. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1051. return create_value_token(Token::Type::Delim, input);
  1052. }
  1053. // U+0040 COMMERCIAL AT (@)
  1054. if (is_at(input)) {
  1055. dbgln_if(CSS_TOKENIZER_DEBUG, "is at");
  1056. // If the next 3 input code points would start an identifier, consume a name, create
  1057. // an <at-keyword-token> with its value set to the returned value, and return it.
  1058. if (would_start_an_identifier(peek_triplet())) {
  1059. auto name = consume_a_name();
  1060. return create_value_token(Token::Type::AtKeyword, name);
  1061. }
  1062. // Otherwise, return a <delim-token> with its value set to the current input code point.
  1063. return create_value_token(Token::Type::Delim, input);
  1064. }
  1065. // U+005B LEFT SQUARE BRACKET ([)
  1066. if (is_open_square_bracket(input)) {
  1067. dbgln_if(CSS_TOKENIZER_DEBUG, "is open square");
  1068. // Return a <[-token>.
  1069. return create_new_token(Token::Type::OpenSquare);
  1070. }
  1071. // U+005C REVERSE SOLIDUS (\)
  1072. if (is_reverse_solidus(input)) {
  1073. dbgln_if(CSS_TOKENIZER_DEBUG, "is reverse solidus");
  1074. // If the input stream starts with a valid escape, reconsume the current input code point,
  1075. // consume an ident-like token, and return it.
  1076. if (is_valid_escape_sequence(start_of_input_stream_twin())) {
  1077. reconsume_current_input_code_point();
  1078. return consume_an_ident_like_token();
  1079. }
  1080. // Otherwise, this is a parse error. Return a <delim-token> with its value set to the
  1081. // current input code point.
  1082. log_parse_error();
  1083. return create_value_token(Token::Type::Delim, input);
  1084. }
  1085. // U+005D RIGHT SQUARE BRACKET (])
  1086. if (is_closed_square_bracket(input)) {
  1087. dbgln_if(CSS_TOKENIZER_DEBUG, "is closed square");
  1088. // Return a <]-token>.
  1089. return create_new_token(Token::Type::CloseSquare);
  1090. }
  1091. // U+007B LEFT CURLY BRACKET ({)
  1092. if (is_open_curly_bracket(input)) {
  1093. dbgln_if(CSS_TOKENIZER_DEBUG, "is open curly");
  1094. // Return a <{-token>.
  1095. return create_new_token(Token::Type::OpenCurly);
  1096. }
  1097. // U+007D RIGHT CURLY BRACKET (})
  1098. if (is_closed_curly_bracket(input)) {
  1099. dbgln_if(CSS_TOKENIZER_DEBUG, "is closed curly");
  1100. // Return a <}-token>.
  1101. return create_new_token(Token::Type::CloseCurly);
  1102. }
  1103. // digit
  1104. if (is_ascii_digit(input)) {
  1105. dbgln_if(CSS_TOKENIZER_DEBUG, "is digit");
  1106. // Reconsume the current input code point, consume a numeric token, and return it.
  1107. reconsume_current_input_code_point();
  1108. return consume_a_numeric_token();
  1109. }
  1110. // name-start code point
  1111. if (is_name_start_code_point(input)) {
  1112. dbgln_if(CSS_TOKENIZER_DEBUG, "is name start");
  1113. // Reconsume the current input code point, consume an ident-like token, and return it.
  1114. reconsume_current_input_code_point();
  1115. return consume_an_ident_like_token();
  1116. }
  1117. // EOF
  1118. if (is_eof(input)) {
  1119. // Return an <EOF-token>.
  1120. return create_new_token(Token::Type::EndOfFile);
  1121. }
  1122. // anything else
  1123. dbgln_if(CSS_TOKENIZER_DEBUG, "is delimiter");
  1124. // Return a <delim-token> with its value set to the current input code point.
  1125. return create_value_token(Token::Type::Delim, input);
  1126. }
  1127. }