commit 2710590: [Project] Css: Add some debug methods

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Jan 28 16:14:10 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-01-28 16:13:31 +0000
URL: https://github.com/rspamd/rspamd/commit/2710590c92c1ad315657de69fb2a7672d8688f80 (HEAD -> master)

[Project] Css: Add some debug methods

---
 src/libserver/css/css_parser.cxx    |   5 +-
 src/libserver/css/css_tokeniser.cxx | 103 ++++++++++++++++++++++++++++++++++++
 src/libserver/css/css_tokeniser.hxx |   5 ++
 3 files changed, 110 insertions(+), 3 deletions(-)

diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx
index 68f03cdfa..54ccccd23 100644
--- a/src/libserver/css/css_parser.cxx
+++ b/src/libserver/css/css_parser.cxx
@@ -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;
 		}
 	}
diff --git a/src/libserver/css/css_tokeniser.cxx b/src/libserver/css/css_tokeniser.cxx
index 3d1749913..60ff297b3 100644
--- a/src/libserver/css/css_tokeniser.cxx
+++ b/src/libserver/css/css_tokeniser.cxx
@@ -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
diff --git a/src/libserver/css/css_tokeniser.hxx b/src/libserver/css/css_tokeniser.hxx
index cd9c47cfd..b2da88500 100644
--- a/src/libserver/css/css_tokeniser.hxx
+++ b/src/libserver/css/css_tokeniser.hxx
@@ -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 */


More information about the Commits mailing list