Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parse_proto.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <string>
17#include <vector>
18
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"
23
24namespace operations_research {
25namespace {
26
27class TextFormatErrorCollector : public google::protobuf::io::ErrorCollector {
28 public:
29 struct CollectedError {
30 bool warning;
31 int line;
32 int column;
33 std::string message;
34 };
35
36 TextFormatErrorCollector() = default;
37 ~TextFormatErrorCollector() override = default;
38
39 void RecordError(const int line, const int column,
40 absl::string_view message) override {
41 collected_errors_.push_back(
42 {/*warning=*/false, line, column, std::string(message)});
43 }
44
45 void RecordWarning(const int line, const int column,
46 absl::string_view message) override {
47 collected_errors_.push_back(
48 {/*warning=*/true, line, column, std::string(message)});
49 }
50
51 // Returns a string listing each collected error. When an error is associated
52 // with a line number and column number that can be found in `value`, that
53 // error message is shown in context using a caret (^), like:
54 // { good_field: 1 error_field: "bad" }
55 // ^
56 std::string RenderErrorMessage(const absl::string_view value) {
57 std::string message;
58 std::vector<std::string> value_lines = absl::StrSplit(value, '\n');
59 for (const auto& error : collected_errors_) {
60 if (error.warning) {
61 absl::StrAppend(&message, "warning: ");
62 }
63 absl::StrAppend(&message, error.message, "\n");
64 if (error.line >= 0 && error.line < value_lines.size()) {
65 const std::string& error_line = value_lines[error.line];
66 if (error.column >= 0 && error.column < error_line.size()) {
67 // If possible, use ^ to point at error.column in error_line.
68 absl::StrAppend(&message, error_line, "\n");
69 absl::StrAppend(&message, std::string(error.column, ' '), "^\n");
70 }
71 }
72 }
73 return message;
74 }
75
76 private:
77 std::vector<CollectedError> collected_errors_;
78};
79
80} // namespace
81
82bool ParseTextProtoForFlag(const absl::string_view text,
83 google::protobuf::Message* const message_out,
84 std::string* const error_out) {
85 TextFormatErrorCollector errors;
86 google::protobuf::TextFormat::Parser parser;
87 parser.RecordErrorsTo(&errors);
88 const bool success = parser.ParseFromString(std::string(text), message_out);
89 *error_out = errors.RenderErrorMessage(text);
90 return success;
91}
92
93} // namespace operations_research
int64_t value
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)
bool warning
int line
int column
std::string message
Definition trace.cc:397