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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "css_tokeniser.hxx"
  24. #include "css.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_selector,
  43. css_eof_block,
  44. };
  45. using consumed_block_ptr = std::unique_ptr<css_consumed_block>;
  46. struct css_function_block {
  47. css_parser_token function;
  48. std::vector<consumed_block_ptr> args;
  49. css_function_block(css_parser_token &&tok) :
  50. function(std::forward<css_parser_token>(tok)) {}
  51. auto as_string() const -> std::string_view {
  52. return function.get_string_or_default("");
  53. }
  54. static auto empty_function() -> const css_function_block & {
  55. static const css_function_block invalid(
  56. css_parser_token(css_parser_token::token_type::eof_token,
  57. css_parser_token_placeholder()));
  58. return invalid;
  59. }
  60. };
  61. css_consumed_block() : tag(parser_tag_type::css_eof_block) {}
  62. css_consumed_block(parser_tag_type tag) : tag(tag) {
  63. if (tag == parser_tag_type::css_top_block ||
  64. tag == parser_tag_type::css_qualified_rule ||
  65. tag == parser_tag_type::css_simple_block) {
  66. /* Pre-allocate content for known vector blocks */
  67. std::vector<consumed_block_ptr> vec;
  68. vec.reserve(4);
  69. content = std::move(vec);
  70. }
  71. }
  72. /* Construct a block from a single lexer token (for trivial blocks) */
  73. explicit css_consumed_block(parser_tag_type tag, css_parser_token &&tok) :
  74. tag(tag) {
  75. if (tag == parser_tag_type::css_function) {
  76. content = css_function_block{std::move(tok)};
  77. }
  78. else {
  79. content = std::move(tok);
  80. }
  81. }
  82. /* Attach a new block to the compound block, consuming block inside */
  83. auto attach_block(consumed_block_ptr &&block) -> bool;
  84. /* Attach a new argument to the compound function block, consuming block inside */
  85. auto add_function_argument(consumed_block_ptr &&block) -> bool;
  86. auto assign_token(css_parser_token &&tok) -> void {
  87. content = std::move(tok);
  88. }
  89. /* Empty blocks used to avoid type checks in loops */
  90. const inline static std::vector<consumed_block_ptr> empty_block_vec{};
  91. auto is_blocks_vec() const -> bool {
  92. return (std::holds_alternative<std::vector<consumed_block_ptr>>(content));
  93. }
  94. auto get_blocks_or_empty() const -> const std::vector<consumed_block_ptr>& {
  95. if (is_blocks_vec()) {
  96. return std::get<std::vector<consumed_block_ptr>>(content);
  97. }
  98. return empty_block_vec;
  99. }
  100. auto is_token() const -> bool {
  101. return (std::holds_alternative<css_parser_token>(content));
  102. }
  103. auto get_token_or_empty() const -> const css_parser_token& {
  104. if (is_token()) {
  105. return std::get<css_parser_token>(content);
  106. }
  107. return css_parser_eof_token();
  108. }
  109. auto is_function() const -> bool {
  110. return (std::holds_alternative<css_function_block>(content));
  111. }
  112. auto get_function_or_invalid() const -> const css_function_block& {
  113. if (is_function()) {
  114. return std::get<css_function_block>(content);
  115. }
  116. return css_function_block::empty_function();
  117. }
  118. auto size() const -> std::size_t {
  119. auto ret = 0;
  120. std::visit([&](auto& arg) {
  121. using T = std::decay_t<decltype(arg)>;
  122. if constexpr (std::is_same_v<T, std::vector<consumed_block_ptr>>) {
  123. /* Array of blocks */
  124. ret = arg.size();
  125. }
  126. else if constexpr (std::is_same_v<T, std::monostate>) {
  127. /* Empty block */
  128. ret = 0;
  129. }
  130. else {
  131. /* Single element block */
  132. ret = 1;
  133. }
  134. },
  135. content);
  136. return ret;
  137. }
  138. auto is_eof() -> bool {
  139. return tag == parser_tag_type::css_eof_block;
  140. }
  141. /* Debug methods */
  142. auto token_type_str(void) const -> const char *;
  143. auto debug_str(void) -> std::string;
  144. public:
  145. parser_tag_type tag;
  146. private:
  147. std::variant<std::monostate,
  148. std::vector<consumed_block_ptr>,
  149. css_parser_token,
  150. css_function_block> content;
  151. };
  152. extern const css_consumed_block css_parser_eof_block;
  153. using blocks_gen_functor = std::function<const css_consumed_block &(void)>;
  154. auto parse_css (rspamd_mempool_t *pool, const std::string_view &st) ->
  155. bool;
  156. }
  157. #endif //RSPAMD_CSS_PARSER_HXX