Skip to content

Commit

Permalink
parser: Refactor to pass translated STL strings to parser::lineno_str…
Browse files Browse the repository at this point in the history
…ing()

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.
  • Loading branch information
irydacea committed Feb 16, 2014
1 parent ba30b70 commit 6eb2e73
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/serialization/parser.cpp
Expand Up @@ -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_;
Expand Down Expand Up @@ -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")));
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down

0 comments on commit 6eb2e73

Please sign in to comment.