You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

css_parser.hxx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*-
  2. * Copyright 2021 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #ifndef RSPAMD_CSS_PARSER_HXX
  18. #define RSPAMD_CSS_PARSER_HXX
  19. #include <variant>
  20. #include <vector>
  21. #include <memory>
  22. #include <string>
  23. #include "function2/function2.hpp"
  24. #include "css_tokeniser.hxx"
  25. #include "parse_error.hxx"
  26. #include "contrib/expected/expected.hpp"
  27. #include "logger.h"
  28. namespace rspamd::css {
  29. /*
  30. * Represents a consumed token by a parser
  31. */
  32. class css_consumed_block {
  33. public:
  34. enum class parser_tag_type : std::uint8_t {
  35. css_top_block = 0,
  36. css_qualified_rule,
  37. css_at_rule,
  38. css_simple_block,
  39. css_function,
  40. css_function_arg,
  41. css_component,
  42. css_eof_block,
  43. };
  44. using consumed_block_ptr = std::unique_ptr<css_consumed_block>;
  45. struct css_function_block {
  46. css_parser_token function;
  47. std::vector<consumed_block_ptr> args;
  48. css_function_block(css_parser_token &&tok) :
  49. function(std::forward<css_parser_token>(tok)) {}
  50. auto as_string() const -> std::string_view {
  51. return function.get_string_or_default("");
  52. }
  53. static auto empty_function() -> const css_function_block & {
  54. static const css_function_block invalid(
  55. css_parser_token(css_parser_token::token_type::eof_token,
  56. css_parser_token_placeholder()));
  57. return invalid;
  58. }
  59. };
  60. css_consumed_block() : tag(parser_tag_type::css_eof_block) {}
  61. css_consumed_block(parser_tag_type tag) : tag(tag) {
  62. if (tag == parser_tag_type::css_top_block ||
  63. tag == parser_tag_type::css_qualified_rule ||
  64. tag == parser_tag_type::css_simple_block) {
  65. /* Pre-allocate content for known vector blocks */
  66. std::vector<consumed_block_ptr> vec;
  67. vec.reserve(4);
  68. content = std::move(vec);
  69. }
  70. }
  71. /* Construct a block from a single lexer token (for trivial blocks) */
  72. explicit css_consumed_block(parser_tag_type tag, css_parser_token &&tok) :
  73. tag(tag) {
  74. if (tag == parser_tag_type::css_function) {
  75. content = css_function_block{std::move(tok)};
  76. }
  77. else {
  78. content = std::move(tok);
  79. }
  80. }
  81. /* Attach a new block to the compound block, consuming block inside */
  82. auto attach_block(consumed_block_ptr &&block) -> bool;
  83. /* Attach a new argument to the compound function block, consuming block inside */
  84. auto add_function_argument(consumed_block_ptr &&block) -> bool;
  85. auto assign_token(css_parser_token &&tok) -> void {
  86. content = std::move(tok);
  87. }
  88. /* Empty blocks used to avoid type checks in loops */
  89. const inline static std::vector<consumed_block_ptr> empty_block_vec{};
  90. auto is_blocks_vec() const -> bool {
  91. return (std::holds_alternative<std::vector<consumed_block_ptr>>(content));
  92. }
  93. auto get_blocks_or_empty() const -> const std::vector<consumed_block_ptr>& {
  94. if (is_blocks_vec()) {
  95. return std::get<std::vector<consumed_block_ptr>>(content);
  96. }
  97. return empty_block_vec;
  98. }
  99. auto is_token() const -> bool {
  100. return (std::holds_alternative<css_parser_token>(content));
  101. }
  102. auto get_token_or_empty() const -> const css_parser_token& {
  103. if (is_token()) {
  104. return std::get<css_parser_token>(content);
  105. }
  106. return css_parser_eof_token();
  107. }
  108. auto is_function() const -> bool {
  109. return (std::holds_alternative<css_function_block>(content));
  110. }
  111. auto get_function_or_invalid() const -> const css_function_block& {
  112. if (is_function()) {
  113. return std::get<css_function_block>(content);
  114. }
  115. return css_function_block::empty_function();
  116. }
  117. auto size() const -> std::size_t {
  118. auto ret = 0;
  119. std::visit([&](auto& arg) {
  120. using T = std::decay_t<decltype(arg)>;
  121. if constexpr (std::is_same_v<T, std::vector<consumed_block_ptr>>) {
  122. /* Array of blocks */
  123. ret = arg.size();
  124. }
  125. else if constexpr (std::is_same_v<T, std::monostate>) {
  126. /* Empty block */
  127. ret = 0;
  128. }
  129. else {
  130. /* Single element block */
  131. ret = 1;
  132. }
  133. },
  134. content);
  135. return ret;
  136. }
  137. auto is_eof() -> bool {
  138. return tag == parser_tag_type::css_eof_block;
  139. }
  140. /* Debug methods */
  141. auto token_type_str(void) const -> const char *;
  142. auto debug_str(void) -> std::string;
  143. public:
  144. parser_tag_type tag;
  145. private:
  146. std::variant<std::monostate,
  147. std::vector<consumed_block_ptr>,
  148. css_parser_token,
  149. css_function_block> content;
  150. };
  151. extern const css_consumed_block css_parser_eof_block;
  152. using blocks_gen_functor = fu2::unique_function<const css_consumed_block &(void)>;
  153. class css_style_sheet;
  154. auto parse_css(rspamd_mempool_t *pool, const std::string_view &st) ->
  155. tl::expected<std::unique_ptr<css_style_sheet>, css_parse_error>;
  156. auto get_selectors_parser_functor(rspamd_mempool_t *pool,
  157. const std::string_view &st) -> blocks_gen_functor;
  158. }
  159. #endif //RSPAMD_CSS_PARSER_HXX