19#include "absl/strings/str_cat.h"
20#include "absl/strings/str_split.h"
21#include "google/protobuf/io/tokenizer.h"
22#include "google/protobuf/text_format.h"
27class TextFormatErrorCollector :
public google::protobuf::io::ErrorCollector {
29 struct CollectedError {
36 TextFormatErrorCollector() =
default;
37 ~TextFormatErrorCollector()
override =
default;
51 std::string RenderErrorMessage(
const absl::string_view
value) {
53 std::vector<std::string> value_lines = absl::StrSplit(
value,
'\n');
54 for (
const auto& error : collected_errors_) {
56 absl::StrAppend(&
message,
"warning: ");
58 absl::StrAppend(&
message, error.message,
"\n");
59 if (error.line >= 0 && error.line < value_lines.size()) {
60 const std::string& error_line = value_lines[error.line];
61 if (error.column >= 0 && error.column < error_line.size()) {
63 absl::StrAppend(&
message, error_line,
"\n");
64 absl::StrAppend(&
message, std::string(error.column,
' '),
"^\n");
72 std::vector<CollectedError> collected_errors_;
78 google::protobuf::Message*
const message_out,
79 std::string*
const error_out) {
80 TextFormatErrorCollector errors;
81 google::protobuf::TextFormat::Parser parser;
82 parser.RecordErrorsTo(&errors);
83 const bool success = parser.ParseFromString(std::string(text), message_out);
84 *error_out = errors.RenderErrorMessage(text);
In SWIG mode, we don't want anything besides these top-level includes.
bool ParseTextProtoForFlag(const absl::string_view text, google::protobuf::Message *const message_out, std::string *const error_out)