Parser.cpp 21 KB

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