From 6eb2e7346c560ddf57ce928668b9999afca888d8 Mon Sep 17 00:00:00 2001 From: "Ignacio R. Morelle" Date: Sun, 16 Feb 2014 07:33:29 -0300 Subject: [PATCH] parser: Refactor to pass translated STL strings to parser::lineno_string() Passing translated strings instead of translatable strings makes a bunch of things easier: * We don't need to worry about forgetting to call gettext _() in lineno_string() (which, as a matter of fact, I forgot to do for hint_string, oops) * We can append and preprend stuff to the *translated* strings before passing to lineno_string() without breaking localization! Woo! Using STL strings instead of C strings also makes things easier and prettier. The negligible performance impact shouldn't be a concern here because landing in parser::lineno_string() or parser::error() is a worst-case situation since it only happens when the parser encounters an error and cannot continue. Also, parser::lineno_string() now takes an optional fifth argument with the tokenizer state message to append to the end of the full message and trail. We need that for the next commit. --- src/serialization/parser.cpp | 42 ++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/serialization/parser.cpp b/src/serialization/parser.cpp index 3afe8657a79a..832171b78ac6 100644 --- a/src/serialization/parser.cpp +++ b/src/serialization/parser.cpp @@ -64,7 +64,9 @@ class parser void parse_element(); void parse_variable(); std::string lineno_string(utils::string_map &map, std::string const &lineno, - const char *error_string, const char *hint_string = NULL); + const std::string &error_string, + const std::string &hint_string = "", + const std::string &debug_string = ""); void error(const std::string& message); config& cfg_; @@ -142,8 +144,8 @@ void parser::operator()() std::stringstream ss; ss << elements.top().start_line << " " << elements.top().file; error(lineno_string(i18n_symbols, ss.str(), - N_("Missing closing tag for tag [$tag]"), - N_("at $pos"))); + _("Missing closing tag for tag [$tag]"), + _("at $pos"))); } } @@ -206,8 +208,8 @@ void parser::parse_element() std::stringstream ss; ss << elements.top().start_line << " " << elements.top().file; error(lineno_string(i18n_symbols, ss.str(), - N_("Found invalid closing tag [/$tag2] for tag [$tag1]"), - N_("opened at $pos"))); + _("Found invalid closing tag [/$tag2] for tag [$tag1]"), + _("at $pos"))); } if(validator_){ element & el= elements.top(); @@ -342,15 +344,19 @@ void parser::parse_variable() */ std::string parser::lineno_string(utils::string_map &i18n_symbols, std::string const &lineno, - const char *error_string, - const char *hint_string) + std::string const &error_string, + std::string const &hint_string, + std::string const &debug_string) { i18n_symbols["pos"] = ::lineno_string(lineno); - std::string result = _(error_string); + std::string result = error_string; - if(hint_string != NULL) { - result += "\n "; - result += hint_string; + if(!hint_string.empty()) { + result += "\n " + hint_string; + } + + if(!debug_string.empty()) { + result += '\n' + debug_string; } BOOST_FOREACH(utils::string_map::value_type& var, i18n_symbols) @@ -360,28 +366,26 @@ std::string parser::lineno_string(utils::string_map &i18n_symbols, void parser::error(const std::string& error_type) { + const std::string& hint_string = _("at $pos"); + utils::string_map i18n_symbols; i18n_symbols["error"] = error_type; std::stringstream ss; ss << tok_->get_start_line() << " " << tok_->get_file(); - const char* const error_string = N_("$error"); - #ifdef DEBUG i18n_symbols["value"] = tok_->current_token().value; i18n_symbols["previous_value"] = tok_->previous_token().value; - const char* const hint_string = - N_("at $pos\n" - "Value: '$value' Previous: '$previous_value'"); + const std::string& tok_state = + _("Value: '$value' Previous: '$previous_value'"); #else - const char* const hint_string = - N_("at $pos"); + const std::string& tok_state = ""; #endif const std::string& message = - lineno_string(i18n_symbols, ss.str(), error_string, hint_string); + lineno_string(i18n_symbols, ss.str(), "$error", hint_string, tok_state); throw config::error(message); }