Parser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/SourceLocation.h>
  8. #include <LibWeb/CSS/Parser/AtStyleRule.h>
  9. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  10. #include <LibWeb/CSS/Parser/Parser.h>
  11. #include <LibWeb/CSS/Parser/QualifiedStyleRule.h>
  12. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  13. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  14. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  15. #include <LibWeb/CSS/Selector.h>
  16. #include <LibWeb/Dump.h>
  17. #define CSS_PARSER_TRACE 1
  18. static void log_parse_error(const SourceLocation& location = SourceLocation::current())
  19. {
  20. dbgln_if(CSS_PARSER_TRACE, "Parse error (CSS) {}", location);
  21. }
  22. namespace Web::CSS {
  23. Parser::Parser(const StringView& input, const String& encoding)
  24. : m_tokenizer(input, encoding)
  25. {
  26. m_tokens = m_tokenizer.parse();
  27. }
  28. Parser::~Parser()
  29. {
  30. }
  31. Token Parser::peek_token()
  32. {
  33. size_t next_offset = m_iterator_offset + 1;
  34. if (next_offset < m_tokens.size()) {
  35. return m_tokens.at(next_offset);
  36. }
  37. return m_tokens.at(m_iterator_offset);
  38. }
  39. Token Parser::next_token()
  40. {
  41. if (m_iterator_offset < (int)m_tokens.size()) {
  42. ++m_iterator_offset;
  43. }
  44. auto token = m_tokens.at(m_iterator_offset);
  45. return token;
  46. }
  47. Token Parser::current_token()
  48. {
  49. return m_tokens.at(m_iterator_offset);
  50. }
  51. Vector<QualifiedStyleRule> Parser::parse_as_stylesheet()
  52. {
  53. auto rules = consume_a_list_of_rules(true);
  54. dbgln("Printing rules:");
  55. for (auto& rule : rules) {
  56. dbgln("PRE:");
  57. for (auto& pre : rule.m_prelude) {
  58. dbgln("{}", pre.to_string());
  59. }
  60. dbgln("BLOCK:");
  61. dbgln("{}", rule.block().to_string());
  62. dbgln("");
  63. auto selectors = parse_selectors(rule.m_prelude);
  64. CSS::Selector selector = Selector(move(selectors));
  65. dump_selector(selector);
  66. }
  67. return rules;
  68. }
  69. Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleComponentValueRule> parts)
  70. {
  71. Vector<CSS::Selector::ComplexSelector> selectors;
  72. size_t index = 0;
  73. auto parse_simple_selector = [&]() -> Optional<CSS::Selector::SimpleSelector> {
  74. if (index >= parts.size())
  75. return {};
  76. auto& current_value = parts.at(index);
  77. index++;
  78. CSS::Selector::SimpleSelector::Type type;
  79. String value;
  80. // FIXME: Handle namespace prefixes.
  81. if (current_value.is(Token::TokenType::Delim) && current_value.token().delim() == "*") {
  82. // FIXME: Handle selectors like `*.foo`.
  83. type = CSS::Selector::SimpleSelector::Type::Universal;
  84. CSS::Selector::SimpleSelector result;
  85. result.type = type;
  86. return result;
  87. }
  88. if (current_value.is(Token::TokenType::Hash)) {
  89. if (current_value.token().m_hash_type != Token::HashType::Id) {
  90. dbgln("Selector contains hash token that is not an id: {}", current_value.to_string());
  91. return {};
  92. }
  93. type = CSS::Selector::SimpleSelector::Type::Id;
  94. value = current_value.token().m_value.to_string();
  95. } else if (current_value.is(Token::TokenType::Delim) && current_value.token().delim() == ".") {
  96. if (index >= parts.size())
  97. return {};
  98. current_value = parts.at(index);
  99. index++;
  100. if (!current_value.is(Token::TokenType::Ident)) {
  101. dbgln("Expected an ident after '.', got: {}", current_value.to_string());
  102. return {};
  103. }
  104. type = CSS::Selector::SimpleSelector::Type::Class;
  105. value = current_value.to_string();
  106. } else if (current_value.is(Token::TokenType::Delim) && current_value.token().delim() == "*") {
  107. type = CSS::Selector::SimpleSelector::Type::Universal;
  108. } else {
  109. type = CSS::Selector::SimpleSelector::Type::TagName;
  110. value = current_value.to_string().to_lowercase();
  111. }
  112. CSS::Selector::SimpleSelector simple_selector;
  113. simple_selector.type = type;
  114. simple_selector.value = value;
  115. if (index >= parts.size())
  116. return simple_selector;
  117. current_value = parts.at(index);
  118. index++;
  119. // FIXME: Attribute selectors want to be their own Selector::SimpleSelector::Type according to the spec.
  120. if (current_value.is_block() && current_value.block().is_square()) {
  121. Vector<StyleComponentValueRule> const& attribute_parts = current_value.block().values();
  122. // FIXME: Handle namespace prefix for attribute name.
  123. auto& attribute_part = attribute_parts.first();
  124. if (!attribute_part.is(Token::TokenType::Ident)) {
  125. dbgln("Expected ident for attribute name, got: '{}'", attribute_part.to_string());
  126. return {};
  127. }
  128. simple_selector.attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute;
  129. simple_selector.attribute_name = attribute_part.token().ident();
  130. size_t attribute_index = 1;
  131. while (attribute_parts.at(attribute_index).is(Token::TokenType::Whitespace)) {
  132. attribute_index++;
  133. if (attribute_index >= attribute_parts.size())
  134. return simple_selector;
  135. }
  136. auto& delim_part = attribute_parts.at(attribute_index);
  137. if (!delim_part.is(Token::TokenType::Delim)) {
  138. dbgln("Expected a delim for attribute comparison, got: '{}'", delim_part.to_string());
  139. return {};
  140. }
  141. if (delim_part.token().delim() == "=") {
  142. simple_selector.attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch;
  143. attribute_index++;
  144. } else {
  145. attribute_index++;
  146. auto& delim_second_part = attribute_parts.at(attribute_index);
  147. if (!(delim_part.is(Token::TokenType::Delim) && delim_part.token().delim() == "=")) {
  148. dbgln("Expected a double delim for attribute comparison, got: '{}{}'", delim_part.to_string(), delim_second_part.to_string());
  149. return {};
  150. }
  151. if (delim_part.token().delim() == "~") {
  152. simple_selector.attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::Contains;
  153. attribute_index++;
  154. }
  155. if (delim_part.token().delim() == "|") {
  156. simple_selector.attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::StartsWith;
  157. attribute_index++;
  158. }
  159. }
  160. while (attribute_parts.at(attribute_index).is(Token::TokenType::Whitespace)) {
  161. attribute_index++;
  162. if (attribute_index >= attribute_parts.size()) {
  163. dbgln("Attribute selector ended without a value to match.");
  164. return {};
  165. }
  166. }
  167. auto& value_part = attribute_parts.at(attribute_index);
  168. if (!value_part.is(Token::TokenType::Ident) && !value_part.is(Token::TokenType::String)) {
  169. dbgln("Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_string());
  170. return {};
  171. }
  172. simple_selector.attribute_value = value_part.token().is_ident() ? value_part.token().ident() : value_part.token().string();
  173. // FIXME: Handle case-sensitivity suffixes. https://www.w3.org/TR/selectors-4/#attribute-case
  174. return simple_selector;
  175. }
  176. // FIXME: Pseudo-class selectors want to be their own Selector::SimpleSelector::Type according to the spec.
  177. if (current_value.is(Token::TokenType::Colon)) {
  178. bool is_pseudo = false;
  179. current_value = parts.at(index);
  180. index++;
  181. if (index >= parts.size())
  182. return {};
  183. if (current_value.is(Token::TokenType::Colon)) {
  184. is_pseudo = true;
  185. current_value = parts.at(index);
  186. index++;
  187. if (index >= parts.size())
  188. return {};
  189. }
  190. // Ignore for now, otherwise we produce a "false positive" selector
  191. // and apply styles to the element itself, not its pseudo element
  192. if (is_pseudo)
  193. return {};
  194. current_value = parts.at(index);
  195. index++;
  196. if (current_value.is(Token::TokenType::Ident)) {
  197. auto pseudo_name = current_value.token().ident();
  198. if (pseudo_name.equals_ignoring_case("link")) {
  199. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Link;
  200. } else if (pseudo_name.equals_ignoring_case("visited")) {
  201. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Visited;
  202. } else if (pseudo_name.equals_ignoring_case("active")) {
  203. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Active;
  204. } else if (pseudo_name.equals_ignoring_case("hover")) {
  205. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Hover;
  206. } else if (pseudo_name.equals_ignoring_case("focus")) {
  207. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Focus;
  208. } else if (pseudo_name.equals_ignoring_case("first-child")) {
  209. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::FirstChild;
  210. } else if (pseudo_name.equals_ignoring_case("last-child")) {
  211. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::LastChild;
  212. } else if (pseudo_name.equals_ignoring_case("only-child")) {
  213. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::OnlyChild;
  214. } else if (pseudo_name.equals_ignoring_case("empty")) {
  215. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Empty;
  216. } else if (pseudo_name.equals_ignoring_case("root")) {
  217. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Root;
  218. } else if (pseudo_name.equals_ignoring_case("first-of-type")) {
  219. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::FirstOfType;
  220. } else if (pseudo_name.equals_ignoring_case("last-of-type")) {
  221. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::LastOfType;
  222. } else if (pseudo_name.equals_ignoring_case("before")) {
  223. simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::Before;
  224. } else if (pseudo_name.equals_ignoring_case("after")) {
  225. simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::After;
  226. } else if (pseudo_name.equals_ignoring_case("disabled")) {
  227. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Disabled;
  228. } else if (pseudo_name.equals_ignoring_case("enabled")) {
  229. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Enabled;
  230. } else if (pseudo_name.equals_ignoring_case("checked")) {
  231. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Checked;
  232. } else {
  233. dbgln("Unknown pseudo class: '{}'", pseudo_name);
  234. return simple_selector;
  235. }
  236. } else if (current_value.is_function()) {
  237. auto& pseudo_function = current_value.function();
  238. if (pseudo_function.name().equals_ignoring_case("nth-child")) {
  239. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthChild;
  240. simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(pseudo_function.values_as_string());
  241. } else if (pseudo_function.name().equals_ignoring_case("nth-last-child")) {
  242. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthLastChild;
  243. simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(pseudo_function.values_as_string());
  244. } else if (pseudo_function.name().equals_ignoring_case("not")) {
  245. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Not;
  246. simple_selector.not_selector = pseudo_function.values_as_string();
  247. } else {
  248. dbgln("Unknown pseudo class: '{}'()", pseudo_function.name());
  249. return simple_selector;
  250. }
  251. } else {
  252. dbgln("Unexpected Block in pseudo-class name, expected a function or identifier. '{}'", current_value.to_string());
  253. return simple_selector;
  254. }
  255. }
  256. return simple_selector;
  257. };
  258. auto parse_complex_selector = [&]() -> Optional<CSS::Selector::ComplexSelector> {
  259. auto relation = CSS::Selector::ComplexSelector::Relation::Descendant;
  260. auto current_value = parts.at(index);
  261. if (current_value.is(Token::TokenType::Delim)) {
  262. auto delim = current_value.token().delim();
  263. if (is_combinator(delim)) {
  264. if (delim == ">") {
  265. relation = CSS::Selector::ComplexSelector::Relation::ImmediateChild;
  266. } else if (delim == "+") {
  267. relation = CSS::Selector::ComplexSelector::Relation::AdjacentSibling;
  268. } else if (delim == "~") {
  269. relation = CSS::Selector::ComplexSelector::Relation::GeneralSibling;
  270. } else if (delim == "||") {
  271. relation = CSS::Selector::ComplexSelector::Relation::Column;
  272. }
  273. index++;
  274. }
  275. }
  276. Vector<CSS::Selector::SimpleSelector> simple_selectors;
  277. for (;;) {
  278. auto component = parse_simple_selector();
  279. if (!component.has_value())
  280. break;
  281. simple_selectors.append(component.value());
  282. }
  283. if (simple_selectors.is_empty())
  284. return {};
  285. return CSS::Selector::ComplexSelector { relation, move(simple_selectors) };
  286. };
  287. for (;;) {
  288. auto complex = parse_complex_selector();
  289. if (complex.has_value())
  290. selectors.append(complex.value());
  291. if (index >= parts.size())
  292. break;
  293. auto current_value = parts.at(index);
  294. if (current_value.is(Token::TokenType::Comma))
  295. break;
  296. index++;
  297. }
  298. if (selectors.is_empty())
  299. return {};
  300. selectors.first().relation = CSS::Selector::ComplexSelector::Relation::None;
  301. return selectors;
  302. }
  303. void Parser::dump_all_tokens()
  304. {
  305. dbgln("Dumping all tokens:");
  306. for (auto& token : m_tokens)
  307. dbgln("{}", token.to_string());
  308. }
  309. void Parser::reconsume_current_input_token()
  310. {
  311. --m_iterator_offset;
  312. }
  313. bool Parser::is_combinator(String input)
  314. {
  315. return input == ">" || input == "+" || input == "~" || input == "||";
  316. }
  317. Vector<QualifiedStyleRule> Parser::consume_a_list_of_rules(bool top_level)
  318. {
  319. Vector<QualifiedStyleRule> rules;
  320. for (;;) {
  321. auto token = next_token();
  322. if (token.is_whitespace()) {
  323. continue;
  324. }
  325. if (token.is_eof()) {
  326. break;
  327. }
  328. if (token.is_cdo() || token.is_cdc()) {
  329. if (top_level) {
  330. continue;
  331. }
  332. reconsume_current_input_token();
  333. auto maybe_qualified = consume_a_qualified_rule();
  334. if (maybe_qualified.has_value()) {
  335. rules.append(maybe_qualified.value());
  336. }
  337. continue;
  338. }
  339. if (token.is_at()) {
  340. reconsume_current_input_token();
  341. rules.append(consume_an_at_rule());
  342. continue;
  343. }
  344. reconsume_current_input_token();
  345. auto maybe_qualified = consume_a_qualified_rule();
  346. if (maybe_qualified.has_value()) {
  347. rules.append(maybe_qualified.value());
  348. }
  349. }
  350. return rules;
  351. }
  352. AtStyleRule Parser::consume_an_at_rule()
  353. {
  354. auto initial = next_token();
  355. AtStyleRule rule;
  356. rule.m_name = initial.m_value.to_string();
  357. for (;;) {
  358. auto token = next_token();
  359. if (token.is_semicolon()) {
  360. return rule;
  361. }
  362. if (token.is_eof()) {
  363. log_parse_error();
  364. return rule;
  365. }
  366. if (token.is_open_curly()) {
  367. rule.m_block = consume_a_simple_block();
  368. return rule;
  369. }
  370. // how is "simple block with an associated token of <{-token>" a valid token?
  371. reconsume_current_input_token();
  372. auto value = consume_a_component_value();
  373. rule.m_prelude.append(value);
  374. }
  375. }
  376. Optional<QualifiedStyleRule> Parser::consume_a_qualified_rule()
  377. {
  378. QualifiedStyleRule rule;
  379. for (;;) {
  380. auto token = next_token();
  381. if (token.is_eof()) {
  382. log_parse_error();
  383. return {};
  384. }
  385. if (token.is_open_curly()) {
  386. rule.m_block = consume_a_simple_block();
  387. return rule;
  388. }
  389. // how is "simple block with an associated token of <{-token>" a valid token?
  390. reconsume_current_input_token();
  391. auto value = consume_a_component_value();
  392. rule.m_prelude.append(value);
  393. }
  394. return rule;
  395. }
  396. StyleComponentValueRule Parser::consume_a_component_value()
  397. {
  398. auto token = next_token();
  399. if (token.is_open_curly() || token.is_open_square() || token.is_open_paren()) {
  400. auto component = StyleComponentValueRule(StyleComponentValueRule::ComponentType::Block);
  401. component.m_block = consume_a_simple_block();
  402. return component;
  403. }
  404. if (token.is_function()) {
  405. auto component = StyleComponentValueRule(StyleComponentValueRule::ComponentType::Function);
  406. component.m_function = consume_a_function();
  407. return component;
  408. }
  409. auto component = StyleComponentValueRule(StyleComponentValueRule::ComponentType::Token);
  410. component.m_token = token;
  411. return component;
  412. }
  413. NonnullRefPtr<StyleBlockRule> Parser::consume_a_simple_block()
  414. {
  415. auto ending_token = current_token().mirror_variant();
  416. NonnullRefPtr<StyleBlockRule> block = create<StyleBlockRule>();
  417. block->m_token = current_token();
  418. for (;;) {
  419. auto token = next_token();
  420. if (token.m_type == ending_token) {
  421. return block;
  422. }
  423. if (token.is_eof()) {
  424. log_parse_error();
  425. return block;
  426. }
  427. reconsume_current_input_token();
  428. auto value = consume_a_component_value();
  429. if (value.m_type == StyleComponentValueRule::ComponentType::Token) {
  430. if (value.m_token.is_whitespace()) {
  431. continue;
  432. }
  433. }
  434. block->m_values.append(value);
  435. }
  436. }
  437. NonnullRefPtr<StyleFunctionRule> Parser::consume_a_function()
  438. {
  439. NonnullRefPtr<StyleFunctionRule> function = create<StyleFunctionRule>(current_token().m_value.to_string());
  440. for (;;) {
  441. auto token = next_token();
  442. if (token.is_close_paren()) {
  443. return function;
  444. }
  445. if (token.is_eof()) {
  446. log_parse_error();
  447. return function;
  448. }
  449. reconsume_current_input_token();
  450. auto value = consume_a_component_value();
  451. if (value.m_type == StyleComponentValueRule::ComponentType::Token) {
  452. if (value.m_token.is_whitespace()) {
  453. continue;
  454. }
  455. }
  456. function->m_values.append(value.to_string());
  457. }
  458. return function;
  459. }
  460. Optional<StyleDeclarationRule> Parser::consume_a_declaration(Vector<StyleComponentValueRule>)
  461. {
  462. TODO();
  463. }
  464. Optional<StyleDeclarationRule> Parser::consume_a_declaration()
  465. {
  466. auto token = next_token();
  467. StyleDeclarationRule declaration;
  468. declaration.m_name = token.m_value.to_string();
  469. for (;;) {
  470. if (!peek_token().is_whitespace()) {
  471. break;
  472. }
  473. next_token();
  474. }
  475. auto colon = next_token();
  476. if (!colon.is_colon()) {
  477. log_parse_error();
  478. return {};
  479. }
  480. for (;;) {
  481. if (!peek_token().is_whitespace()) {
  482. break;
  483. }
  484. next_token();
  485. }
  486. for (;;) {
  487. if (peek_token().is_eof()) {
  488. break;
  489. }
  490. declaration.m_values.append(consume_a_component_value());
  491. }
  492. auto second_last = declaration.m_values.at(declaration.m_values.size() - 2);
  493. auto last = declaration.m_values.at(declaration.m_values.size() - 1);
  494. if (second_last.m_type == StyleComponentValueRule::ComponentType::Token && last.m_type == StyleComponentValueRule::ComponentType::Token) {
  495. auto last_token = last.m_token;
  496. auto second_last_token = second_last.m_token;
  497. if (second_last_token.is_delim() && second_last_token.m_value.to_string().equals_ignoring_case("!")) {
  498. if (last_token.is_ident() && last_token.m_value.to_string().equals_ignoring_case("important")) {
  499. declaration.m_values.remove(declaration.m_values.size() - 2);
  500. declaration.m_values.remove(declaration.m_values.size() - 1);
  501. declaration.m_important = true;
  502. }
  503. }
  504. }
  505. for (;;) {
  506. auto maybe_whitespace = declaration.m_values.at(declaration.m_values.size() - 1);
  507. if (!(maybe_whitespace.m_type == StyleComponentValueRule::ComponentType::Token && maybe_whitespace.m_token.is_whitespace())) {
  508. break;
  509. }
  510. declaration.m_values.remove(declaration.m_values.size() - 1);
  511. }
  512. return declaration;
  513. }
  514. Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations()
  515. {
  516. Vector<DeclarationOrAtRule> list;
  517. for (;;) {
  518. auto token = next_token();
  519. if (token.is_whitespace() || token.is_semicolon()) {
  520. continue;
  521. }
  522. if (token.is_eof()) {
  523. return list;
  524. }
  525. if (token.is_at()) {
  526. reconsume_current_input_token();
  527. list.append(DeclarationOrAtRule(consume_an_at_rule()));
  528. continue;
  529. }
  530. if (token.is_ident()) {
  531. Vector<StyleComponentValueRule> temp;
  532. auto component = StyleComponentValueRule(StyleComponentValueRule::ComponentType::Token);
  533. component.m_token = token;
  534. temp.append(component);
  535. for (;;) {
  536. auto peek = peek_token();
  537. if (peek.is_semicolon() || peek.is_eof()) {
  538. break;
  539. }
  540. temp.append(consume_a_component_value());
  541. }
  542. auto maybe_declaration = consume_a_declaration(temp);
  543. if (maybe_declaration.has_value()) {
  544. list.append(DeclarationOrAtRule(maybe_declaration.value()));
  545. }
  546. }
  547. log_parse_error();
  548. reconsume_current_input_token();
  549. auto peek = peek_token();
  550. if (!(peek.is_semicolon() || peek.is_eof())) {
  551. consume_a_component_value();
  552. }
  553. }
  554. return list;
  555. }
  556. Optional<QualifiedStyleRule> Parser::parse_as_rule()
  557. {
  558. Optional<QualifiedStyleRule> rule;
  559. for (;;) {
  560. auto maybe_whitespace = peek_token();
  561. if (!maybe_whitespace.is_whitespace()) {
  562. break;
  563. }
  564. next_token();
  565. }
  566. auto token = peek_token();
  567. if (token.is_eof()) {
  568. return {};
  569. }
  570. if (token.is_at()) {
  571. rule = consume_an_at_rule();
  572. } else {
  573. rule = consume_a_qualified_rule();
  574. }
  575. for (;;) {
  576. auto maybe_whitespace = peek_token();
  577. if (!maybe_whitespace.is_whitespace()) {
  578. break;
  579. }
  580. next_token();
  581. }
  582. auto maybe_eof = peek_token();
  583. if (maybe_eof.is_eof()) {
  584. return rule;
  585. }
  586. return {};
  587. }
  588. Vector<QualifiedStyleRule> Parser::parse_as_list_of_rules()
  589. {
  590. return consume_a_list_of_rules(false);
  591. }
  592. Optional<StyleDeclarationRule> Parser::parse_as_declaration()
  593. {
  594. for (;;) {
  595. auto maybe_whitespace = peek_token();
  596. if (!maybe_whitespace.is_whitespace()) {
  597. break;
  598. }
  599. next_token();
  600. }
  601. auto token = peek_token();
  602. if (!token.is_ident()) {
  603. return {};
  604. }
  605. return consume_a_declaration();
  606. }
  607. Vector<DeclarationOrAtRule> Parser::parse_as_list_of_declarations()
  608. {
  609. return consume_a_list_of_declarations();
  610. }
  611. Optional<StyleComponentValueRule> Parser::parse_as_component_value()
  612. {
  613. for (;;) {
  614. auto maybe_whitespace = peek_token();
  615. if (!maybe_whitespace.is_whitespace()) {
  616. break;
  617. }
  618. next_token();
  619. }
  620. auto token = peek_token();
  621. if (token.is_eof()) {
  622. return {};
  623. }
  624. auto value = consume_a_component_value();
  625. for (;;) {
  626. auto maybe_whitespace = peek_token();
  627. if (!maybe_whitespace.is_whitespace()) {
  628. break;
  629. }
  630. next_token();
  631. }
  632. auto maybe_eof = peek_token();
  633. if (maybe_eof.is_eof()) {
  634. return value;
  635. }
  636. return {};
  637. }
  638. Vector<StyleComponentValueRule> Parser::parse_as_list_of_component_values()
  639. {
  640. Vector<StyleComponentValueRule> rules;
  641. for (;;) {
  642. if (peek_token().is_eof()) {
  643. break;
  644. }
  645. rules.append(consume_a_component_value());
  646. }
  647. return rules;
  648. }
  649. Vector<StyleComponentValueRule> Parser::parse_as_list_of_comma_separated_component_values()
  650. {
  651. Vector<StyleComponentValueRule> rules;
  652. for (;;) {
  653. rules.append(consume_a_component_value());
  654. if (peek_token().is_comma())
  655. continue;
  656. if (peek_token().is_eof())
  657. break;
  658. }
  659. return rules;
  660. }
  661. }