Parser.cpp 21 KB

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