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_tokeniser.hxx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_TOKENISER_HXX
  18. #define RSPAMD_CSS_TOKENISER_HXX
  19. #include <string_view>
  20. #include <utility>
  21. #include <variant>
  22. #include <list>
  23. #include <functional>
  24. #include <cstdint>
  25. #include "mem_pool.h"
  26. namespace rspamd::css {
  27. struct css_parser_token_placeholder {}; /* For empty tokens */
  28. struct css_parser_token {
  29. enum class token_type : std::uint8_t {
  30. whitespace_token,
  31. ident_token,
  32. function_token,
  33. at_keyword_token,
  34. hash_token,
  35. string_token,
  36. number_token,
  37. url_token,
  38. cdo_token, /* xml open comment */
  39. cdc_token, /* xml close comment */
  40. delim_token,
  41. obrace_token, /* ( */
  42. ebrace_token, /* ) */
  43. osqbrace_token, /* [ */
  44. esqbrace_token, /* ] */
  45. ocurlbrace_token, /* { */
  46. ecurlbrace_token, /* } */
  47. comma_token,
  48. colon_token,
  49. semicolon_token,
  50. eof_token,
  51. };
  52. enum class dim_type : std::uint8_t {
  53. dim_px = 0,
  54. dim_em,
  55. dim_rem,
  56. dim_ex,
  57. dim_wv,
  58. dim_wh,
  59. dim_vmax,
  60. dim_vmin,
  61. dim_pt,
  62. dim_cm,
  63. dim_mm,
  64. dim_in,
  65. dim_pc,
  66. dim_max,
  67. };
  68. static const std::uint8_t default_flags = 0;
  69. static const std::uint8_t flag_bad_string = (1u << 0u);
  70. static const std::uint8_t number_dimension = (1u << 1u);
  71. static const std::uint8_t number_percent = (1u << 2u);
  72. static const std::uint8_t flag_bad_dimension = (1u << 3u);
  73. using value_type = std::variant<std::string_view, /* For strings and string like tokens */
  74. char, /* For delimiters (might need to move to unicode point) */
  75. float, /* For numeric stuff */
  76. css_parser_token_placeholder /* For general no token stuff */
  77. >;
  78. /* Typed storage */
  79. value_type value;
  80. int lineno;
  81. token_type type;
  82. std::uint8_t flags = default_flags;
  83. dim_type dimension_type;
  84. css_parser_token() = delete;
  85. explicit css_parser_token(token_type type, const value_type &value)
  86. : value(value), type(type)
  87. {
  88. }
  89. css_parser_token(css_parser_token &&other) = default;
  90. css_parser_token(const css_parser_token &token) = default;
  91. auto operator=(css_parser_token &&other) -> css_parser_token & = default;
  92. auto adjust_dim(const css_parser_token &dim_token) -> bool;
  93. auto get_string_or_default(const std::string_view &def) const -> std::string_view
  94. {
  95. if (std::holds_alternative<std::string_view>(value)) {
  96. return std::get<std::string_view>(value);
  97. }
  98. else if (std::holds_alternative<char>(value)) {
  99. return std::string_view(&std::get<char>(value), 1);
  100. }
  101. return def;
  102. }
  103. auto get_delim() const -> char
  104. {
  105. if (std::holds_alternative<char>(value)) {
  106. return std::get<char>(value);
  107. }
  108. return (char) -1;
  109. }
  110. auto get_number_or_default(float def) const -> float
  111. {
  112. if (std::holds_alternative<float>(value)) {
  113. auto dbl = std::get<float>(value);
  114. if (flags & css_parser_token::number_percent) {
  115. dbl /= 100.0;
  116. }
  117. return dbl;
  118. }
  119. return def;
  120. }
  121. auto get_normal_number_or_default(float def) const -> float
  122. {
  123. if (std::holds_alternative<float>(value)) {
  124. auto dbl = std::get<float>(value);
  125. if (flags & css_parser_token::number_percent) {
  126. dbl /= 100.0;
  127. }
  128. if (dbl < 0) {
  129. return 0.0;
  130. }
  131. else if (dbl > 1.0) {
  132. return 1.0;
  133. }
  134. return dbl;
  135. }
  136. return def;
  137. }
  138. /* Debugging routines */
  139. constexpr auto get_token_type() -> const char *;
  140. /* This function might be slow */
  141. auto debug_token_str() -> std::string;
  142. };
  143. static auto css_parser_eof_token(void) -> const css_parser_token &
  144. {
  145. static css_parser_token eof_tok{
  146. css_parser_token::token_type::eof_token,
  147. css_parser_token_placeholder()};
  148. return eof_tok;
  149. }
  150. /* Ensure that parser tokens are simple enough */
  151. /*
  152. * compiler must implement P0602 "variant and optional should propagate copy/move triviality"
  153. * This is broken on gcc < 8!
  154. */
  155. static_assert(std::is_trivially_copyable_v<css_parser_token>);
  156. class css_tokeniser {
  157. public:
  158. css_tokeniser() = delete;
  159. css_tokeniser(rspamd_mempool_t *pool, const std::string_view &sv)
  160. : input(sv), offset(0), pool(pool)
  161. {
  162. }
  163. auto next_token(void) -> struct css_parser_token;
  164. auto pushback_token(const struct css_parser_token &t) const -> void
  165. {
  166. backlog.push_back(t);
  167. }
  168. private:
  169. std::string_view input;
  170. std::size_t offset;
  171. rspamd_mempool_t *pool;
  172. mutable std::list<css_parser_token> backlog;
  173. auto consume_number() -> struct css_parser_token;
  174. auto consume_ident(bool allow_number = false) -> struct css_parser_token;
  175. };
  176. }// namespace rspamd::css
  177. #endif//RSPAMD_CSS_TOKENISER_HXX