From 1441cca9c57289495edbed2e662a6eb894b72d6c Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 13 Mar 2018 13:45:15 -0500 Subject: [PATCH] Restore localization to tokenizer error strings Work around #4810 by retrieving localizations at runtime to avoid issues possibly caused by inserting into the static unordered_map during static initialization. Closes #810. --- src/parse_tree.cpp | 2 +- src/tokenizer.cpp | 28 ++++++++++++++++------------ src/tokenizer.h | 7 +++++-- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/parse_tree.cpp b/src/parse_tree.cpp index 9ddcebd09..db61b2a5f 100644 --- a/src/parse_tree.cpp +++ b/src/parse_tree.cpp @@ -671,7 +671,7 @@ void parse_ll_t::report_tokenizer_error(const tokenizer_t &tokenizer, const tok_ parse_error_code_t parse_error_code = tok.error->parser_error; this->parse_error_at_location(tok.offset, tok.length, tok.offset + tok.error_offset, parse_error_code, L"%ls", - tok.error->Message); + tok.error->Message()); } void parse_ll_t::parse_error_unexpected_token(const wchar_t *expected, parse_token_t token) { diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 91dd1ac8f..afd0903da 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -17,18 +17,22 @@ #include "wutil.h" // IWYU pragma: keep tokenizer_error *TOK_ERROR_NONE = new tokenizer_error(L""); -tokenizer_error *TOK_UNTERMINATED_QUOTE = new tokenizer_error(_(L"Unexpected end of string, quotes are not balanced"), parse_error_tokenizer_unterminated_quote); -tokenizer_error *TOK_UNTERMINATED_SUBSHELL = new tokenizer_error(_(L"Unexpected end of string, expecting ')'"), parse_error_tokenizer_unterminated_subshell); -tokenizer_error *TOK_UNTERMINATED_SLICE = new tokenizer_error(_(L"Unexpected end of string, square brackets do not match"), parse_error_tokenizer_unterminated_slice); -tokenizer_error *TOK_UNTERMINATED_ESCAPE = new tokenizer_error(_(L"Unexpected end of string, incomplete escape sequence"), parse_error_tokenizer_unterminated_escape); -tokenizer_error *TOK_INVALID_REDIRECT = new tokenizer_error(_(L"Invalid input/output redirection")); -tokenizer_error *TOK_INVALID_PIPE = new tokenizer_error(_(L"Cannot use stdin (fd 0) as pipe output")); -tokenizer_error *TOK_CLOSING_UNOPENED_SUBSHELL = new tokenizer_error(_(L"Unexpected ')' for unopened parenthesis")); -tokenizer_error *TOK_ILLEGAL_SLICE = new tokenizer_error(_(L"Unexpected '[' at this location")); -tokenizer_error *TOK_CLOSING_UNOPENED_BRACE = new tokenizer_error(_(L"Unexpected '}' for unopened brace expansion")); -tokenizer_error *TOK_UNTERMINATED_BRACE = new tokenizer_error(_(L"Unexpected end of string, incomplete parameter expansion")); -tokenizer_error *TOK_EXPECTED_PCLOSE_FOUND_BCLOSE = new tokenizer_error(_(L"Unexpected '}' found, expecting ')'")); -tokenizer_error *TOK_EXPECTED_BCLOSE_FOUND_PCLOSE = new tokenizer_error(_(L"Unexpected ')' found, expecting '}'")); +tokenizer_error *TOK_UNTERMINATED_QUOTE = new tokenizer_error((L"Unexpected end of string, quotes are not balanced"), parse_error_tokenizer_unterminated_quote); +tokenizer_error *TOK_UNTERMINATED_SUBSHELL = new tokenizer_error((L"Unexpected end of string, expecting ')'"), parse_error_tokenizer_unterminated_subshell); +tokenizer_error *TOK_UNTERMINATED_SLICE = new tokenizer_error((L"Unexpected end of string, square brackets do not match"), parse_error_tokenizer_unterminated_slice); +tokenizer_error *TOK_UNTERMINATED_ESCAPE = new tokenizer_error((L"Unexpected end of string, incomplete escape sequence"), parse_error_tokenizer_unterminated_escape); +tokenizer_error *TOK_INVALID_REDIRECT = new tokenizer_error((L"Invalid input/output redirection")); +tokenizer_error *TOK_INVALID_PIPE = new tokenizer_error((L"Cannot use stdin (fd 0) as pipe output")); +tokenizer_error *TOK_CLOSING_UNOPENED_SUBSHELL = new tokenizer_error((L"Unexpected ')' for unopened parenthesis")); +tokenizer_error *TOK_ILLEGAL_SLICE = new tokenizer_error((L"Unexpected '[' at this location")); +tokenizer_error *TOK_CLOSING_UNOPENED_BRACE = new tokenizer_error((L"Unexpected '}' for unopened brace expansion")); +tokenizer_error *TOK_UNTERMINATED_BRACE = new tokenizer_error((L"Unexpected end of string, incomplete parameter expansion")); +tokenizer_error *TOK_EXPECTED_PCLOSE_FOUND_BCLOSE = new tokenizer_error((L"Unexpected '}' found, expecting ')'")); +tokenizer_error *TOK_EXPECTED_BCLOSE_FOUND_PCLOSE = new tokenizer_error((L"Unexpected ')' found, expecting '}'")); + +const wchar_t *tokenizer_error::Message() const { + return _(_message); +} /// Return an error token and mark that we no longer have a next token. tok_t tokenizer_t::call_error(tokenizer_error *error_type, const wchar_t *token_start, diff --git a/src/tokenizer.h b/src/tokenizer.h index 8ce6618a7..f44ea2815 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -24,10 +24,13 @@ enum token_type { }; struct tokenizer_error { - const wchar_t *Message; +private: + const wchar_t *_message; +public: + const wchar_t *Message() const; enum parse_error_code_t parser_error; //the parser error associated with this tokenizer error tokenizer_error(const wchar_t *msg, enum parse_error_code_t perr = parse_error_tokenizer_other) - : Message(msg), parser_error(perr) {} + : _message(msg), parser_error(perr) {} tokenizer_error(const tokenizer_error&) = delete; };