]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Css: Add some debug methods
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 28 Jan 2021 16:13:31 +0000 (16:13 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 28 Jan 2021 16:13:31 +0000 (16:13 +0000)
src/libserver/css/css_parser.cxx
src/libserver/css/css_tokeniser.cxx
src/libserver/css/css_tokeniser.hxx

index 68f03cdfab278c0fc04bea8a1ac5a5278625c287..54ccccd23cbe3c1ec3978c811a4873cda56c6c93 100644 (file)
@@ -101,10 +101,9 @@ bool css_parser::consume_input(const std::string_view &sv)
                case css_parser_token::token_type::eof_token:
                        eof = true;
                        break;
-               case css_parser_token::token_type::whitespace_token:
-               case css_parser_token::token_type::cdc_token:
-               case css_parser_token::token_type::cdo_token:
+               default:
                        /* Ignore tokens */
+                       msg_debug_css("got token: %s", next_token.debug_token_str().c_str());
                        break;
                }
        }
index 3d1749913d54191395ece0d8085b8b160a1361a0..60ff297b303b072adf44060cb8cd39d28b66a3f2 100644 (file)
@@ -706,4 +706,107 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token
        return make_token<css_parser_token::token_type::eof_token>();
 }
 
+constexpr auto css_parser_token::get_token_type() -> const char *
+{
+       const char *ret = "unknown";
+
+       switch(type) {
+       case token_type::whitespace_token:
+               ret = "whitespace";
+               break;
+       case token_type::ident_token:
+               ret = "ident";
+               break;
+       case token_type::function_token:
+               ret = "function";
+               break;
+       case token_type::at_keyword_token:
+               ret = "atkeyword";
+               break;
+       case token_type::hash_token:
+               ret = "hash";
+               break;
+       case token_type::string_token:
+               ret = "string";
+               break;
+       case token_type::number_token:
+               ret = "number";
+               break;
+       case token_type::url_token:
+               ret = "url";
+               break;
+       case token_type::cdo_token: /* xml open comment */
+               ret = "cdo";
+               break;
+       case token_type::cdc_token: /* xml close comment */
+               ret = "cdc";
+               break;
+       case token_type::delim_token:
+               ret = "delim";
+               break;
+       case token_type::obrace_token: /* ( */
+               ret = "obrace";
+               break;
+       case token_type::ebrace_token: /* ) */
+               ret = "ebrace";
+               break;
+       case token_type::osqbrace_token: /* [ */
+               ret = "osqbrace";
+               break;
+       case token_type::esqbrace_token: /* ] */
+               ret = "esqbrace";
+               break;
+       case token_type::ocurlbrace_token: /* { */
+               ret = "ocurlbrace";
+               break;
+       case token_type::ecurlbrace_token: /* } */
+               ret = "ecurlbrace";
+               break;
+       case token_type::comma_token:
+               ret = "comma";
+               break;
+       case token_type::colon_token:
+               ret = "colon";
+               break;
+       case token_type::semicolon_token:
+               ret = "semicolon";
+               break;
+       case token_type::eof_token:
+               ret = "eof";
+               break;
+       }
+
+       return ret;
+}
+
+
+auto css_parser_token::debug_token_str() -> std::string
+{
+       const auto *token_type_str = get_token_type();
+       std::string ret = token_type_str;
+
+       if (std::holds_alternative<std::string_view>(value)) {
+               ret += "; value=";
+               ret += std::get<std::string_view>(value);
+       }
+       else if (std::holds_alternative<double>(value)) {
+               ret += "; value=";
+               ret += std::to_string(std::get<double>(value));
+       }
+       else if (std::holds_alternative<char>(value)) {
+               ret += "; value=";
+               ret += std::get<char>(value);
+       }
+
+       if ((flags & (~number_dimension)) != default_flags) {
+               ret += "; flags=" + std::to_string(flags);
+       }
+
+       if (flags & number_dimension) {
+               ret += "; dim=" + std::to_string(static_cast<int>(dim_type));
+       }
+
+       return ret; /* Copy elision */
+}
+
 }
\ No newline at end of file
index cd9c47cfd402aa77b3307e284961be06c650144b..b2da885006d6b4a58302d07a198020cec3679b9e 100644 (file)
@@ -91,6 +91,11 @@ struct css_parser_token {
        explicit css_parser_token(token_type type, const value_type &value) :
                        value(value), type(type) {}
        auto adjust_dim(const css_parser_token &dim_token) -> bool;
+
+       /* Debugging routines */
+       constexpr auto get_token_type() -> const char *;
+       /* This function might be slow */
+       auto debug_token_str() -> std::string;
 };
 
 /* Ensure that parser tokens are simple enough */