Skip to content

Commit

Permalink
parser: parser::error() now takes the position message format as an a…
Browse files Browse the repository at this point in the history
…rgument

The optional argument allows us to specify a nicer and more relevant
position message format for the two parser errors that involve more than
one file location: missing closing tags (expected location and opening
location) and invalid closing tags (opening location and (invalid)
closing location).

This makes it easier to tell what's going on even when confronted with a
kilometric preprocessor brace-substitution trail, e.g:

> Found invalid closing tag [/tag2] for tag [tag1]
> Opened at ~add-ons/My_Add-on/scenarios/24.cfg:1090
>     included from ~add-ons/My_Add-on/scenarios/24.cfg:120
>     included from ~add-ons/My_Add-on/_main.cfg:42
>     [...]
> Closed at ~add-ons/My_Add-on/scenarios/24.cfg:1098
>     included from ~add-ons/My_Add-on/scenarios/24.cfg:120
>     included from ~add-ons/My_Add-on/_main.cfg:42
>     [...]

Some day we will be able to make it easier to handle these situations by
ensuring the trail format is concise and relevant, but that day will not
come before 1.13.0. Messing further with the main ::lineno_string()
function (in preprocessor.cpp) would be risky at this point with 1.12.0
approaching.
  • Loading branch information
irydacea committed Feb 16, 2014
1 parent 6eb2e73 commit 996b31d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/serialization/parser.cpp
Expand Up @@ -67,7 +67,7 @@ class parser
const std::string &error_string,
const std::string &hint_string = "",
const std::string &debug_string = "");
void error(const std::string& message);
void error(const std::string& message, const std::string& pos_format = "");

config& cfg_;
tokenizer *tok_;
Expand Down Expand Up @@ -145,7 +145,7 @@ void parser::operator()()
ss << elements.top().start_line << " " << elements.top().file;
error(lineno_string(i18n_symbols, ss.str(),
_("Missing closing tag for tag [$tag]"),
_("at $pos")));
_("expected at $pos")), _("opened at $pos"));
}
}

Expand Down Expand Up @@ -209,7 +209,7 @@ void parser::parse_element()
ss << elements.top().start_line << " " << elements.top().file;
error(lineno_string(i18n_symbols, ss.str(),
_("Found invalid closing tag [/$tag2] for tag [$tag1]"),
_("at $pos")));
_("opened at $pos")), _("closed at $pos"));
}
if(validator_){
element & el= elements.top();
Expand Down Expand Up @@ -352,7 +352,7 @@ std::string parser::lineno_string(utils::string_map &i18n_symbols,
std::string result = error_string;

if(!hint_string.empty()) {
result += "\n " + hint_string;
result += '\n' + hint_string;
}

if(!debug_string.empty()) {
Expand All @@ -364,9 +364,13 @@ std::string parser::lineno_string(utils::string_map &i18n_symbols,
return result;
}

void parser::error(const std::string& error_type)
void parser::error(const std::string& error_type, const std::string& pos_format)
{
const std::string& hint_string = _("at $pos");
std::string hint_string = pos_format;

if(hint_string.empty()) {
hint_string = _("at $pos");
}

utils::string_map i18n_symbols;
i18n_symbols["error"] = error_type;
Expand Down

0 comments on commit 996b31d

Please sign in to comment.