forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_annotation_parser.cc
More file actions
281 lines (255 loc) · 9.96 KB
/
http_annotation_parser.cc
File metadata and controls
281 lines (255 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "generator/internal/http_annotation_parser.h"
#include "google/cloud/internal/make_status.h"
#include "google/cloud/log.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include <cassert>
#include <functional>
namespace google {
namespace cloud {
namespace generator_internal {
namespace {
template <typename T>
struct ParseSuccess {
T value;
std::size_t end; // where parsing finished
};
template <typename T>
ParseSuccess<T> MakeParseSuccess(T value, std::size_t end) {
return ParseSuccess<T>{std::move(value), end};
}
template <typename T>
using ParseResult = StatusOr<ParseSuccess<T>>;
Status MakeParseError(absl::string_view input, std::size_t offset,
absl::string_view expected,
internal::ErrorInfoBuilder builder) {
return internal::InvalidArgumentError(
absl::StrCat("error parsing path template, expected", expected,
" at offset ", offset, "\n", input, "\n",
std::string(offset, ' '), "^"),
std::move(builder));
}
using SegmentParser = std::function<ParseResult<PathTemplate::Segment>(
absl::string_view, std::size_t)>;
ParseResult<PathTemplate::Segments> ParseSegmentsImpl(
absl::string_view input, std::size_t offset, SegmentParser const& parser,
internal::ErrorInfoBuilder error_info) {
PathTemplate::Segments segments;
while (offset != input.size()) {
auto s = parser(input, offset);
if (!s) return std::move(s).status();
segments.push_back(
std::make_shared<PathTemplate::Segment>(std::move(s->value)));
offset = s->end;
if (input.size() == offset || input[offset] != '/') break;
++offset;
}
if (segments.empty()) {
return MakeParseError(input, offset, " segment", std::move(error_info));
}
return MakeParseSuccess(std::move(segments), offset);
}
ParseResult<PathTemplate::Segments> ParseSegments(absl::string_view input,
std::size_t offset);
ParseResult<PathTemplate::Segments> ParsePlainSegments(absl::string_view input,
std::size_t offset);
ParseResult<PathTemplate::Segment> ParsePlainSegment(absl::string_view input,
std::size_t offset);
ParseResult<PathTemplate::Segment> ParseSegment(absl::string_view input,
std::size_t offset);
ParseResult<PathTemplate::Segment> ParseVariable(absl::string_view input,
std::size_t offset);
ParseResult<PathTemplate::Segments> ParsePlainSegments(absl::string_view input,
std::size_t offset) {
return ParseSegmentsImpl(input, offset, ParsePlainSegment, GCP_ERROR_INFO());
}
ParseResult<PathTemplate::Segments> ParseSegments(absl::string_view input,
std::size_t offset) {
return ParseSegmentsImpl(input, offset, ParseSegment, GCP_ERROR_INFO());
}
ParseResult<PathTemplate::Segment> ParseLiteral(absl::string_view input,
std::size_t offset) {
auto constexpr kValidChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghiljkmnopqrstuvwxyz"
"0123456789"
"-._~"
"%" // We do not validate percent encoding
"!$&'()*+,;=";
auto candidate = input.substr(offset);
auto pos = candidate.find_first_not_of(kValidChars);
auto const end = pos == absl::string_view::npos ? input.size() : offset + pos;
if (end == offset) {
return MakeParseError(input, offset, " non-empty literal",
GCP_ERROR_INFO());
}
auto literal = candidate.substr(0, pos);
return MakeParseSuccess(PathTemplate::Segment{std::string(literal)}, end);
}
ParseResult<PathTemplate::Segment> ParseSegment(absl::string_view input,
std::size_t offset) {
auto candidate = input.substr(offset);
if (absl::StartsWith(candidate, "**")) {
return MakeParseSuccess(
PathTemplate::Segment{PathTemplate::MatchRecursive{}}, offset + 2);
}
if (absl::StartsWith(candidate, "*")) {
return MakeParseSuccess(PathTemplate::Segment{PathTemplate::Match{}},
offset + 1);
}
if (absl::StartsWith(candidate, "{")) return ParseVariable(input, offset);
return ParseLiteral(input, offset);
}
ParseResult<PathTemplate::Segment> ParsePlainSegment(absl::string_view input,
std::size_t offset) {
auto candidate = input.substr(offset);
if (absl::StartsWith(candidate, "**")) {
return MakeParseSuccess(
PathTemplate::Segment{PathTemplate::MatchRecursive{}}, offset + 2);
}
if (absl::StartsWith(candidate, "*")) {
return MakeParseSuccess(PathTemplate::Segment{PathTemplate::Match{}},
offset + 1);
}
if (absl::StartsWith(candidate, "{")) {
return MakeParseError(input, offset, " literal", GCP_ERROR_INFO());
}
return ParseLiteral(input, offset);
}
ParseResult<std::string> ParseIdent(absl::string_view input,
std::size_t offset) {
auto candidate = input.substr(offset);
auto constexpr kValidStartChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghiljkmnopqrstuvwxyz";
if (candidate.find_first_of(kValidStartChars) != 0) {
return MakeParseError(input, offset, " start of identifier",
GCP_ERROR_INFO());
}
auto constexpr kValidChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghiljkmnopqrstuvwxyz"
"0123456789"
"_";
auto pos = candidate.find_first_not_of(kValidChars);
auto ident = candidate.substr(0, pos);
auto const end = pos == absl::string_view::npos ? input.size() : offset + pos;
return MakeParseSuccess(std::string(ident), end);
}
ParseResult<std::string> ParseFieldPath(absl::string_view input,
std::size_t offset) {
std::string field_path;
char const* sep = "";
while (offset != input.size()) {
auto s = ParseIdent(input, offset);
if (!s) return std::move(s).status();
field_path.append(sep);
field_path.append(s->value);
offset = s->end;
if (input.size() == offset || input[offset] != '.') break;
++offset;
sep = ".";
}
if (field_path.empty()) {
return MakeParseError(input, offset, " identifier", GCP_ERROR_INFO());
}
return MakeParseSuccess(std::move(field_path), offset);
}
ParseResult<PathTemplate::Segment> ParseVariable(absl::string_view input,
std::size_t offset) {
assert(offset != input.size() && input[offset] == '{');
auto fp = ParseFieldPath(input, offset + 1);
if (!fp) return std::move(fp).status();
auto result = PathTemplate::Variable{std::move(fp->value), {}};
offset = fp->end;
if (input.size() == offset) {
return MakeParseError(input, offset, " closing brace", GCP_ERROR_INFO());
}
if (input[offset] == '}') {
return MakeParseSuccess(PathTemplate::Segment{std::move(result)},
offset + 1);
}
if (input[offset] != '=') {
return MakeParseError(input, offset, " `=` or `}`", GCP_ERROR_INFO());
}
auto ps = ParsePlainSegments(input, offset + 1);
if (!ps) return std::move(ps).status();
offset = ps->end;
if (input.size() == offset || input[offset] != '}') {
return MakeParseError(input, offset, " closing brace", GCP_ERROR_INFO());
}
result.segments = std::move(ps->value);
return MakeParseSuccess(PathTemplate::Segment{std::move(result)}, offset + 1);
}
void StreamSegments(std::ostream& os, PathTemplate::Segments const& segments) {
os << "[ ";
char const* sep = "";
for (auto const& s : segments) {
os << sep << *s;
sep = " / ";
}
os << " ]";
}
} // namespace
StatusOr<PathTemplate> ParsePathTemplate(absl::string_view input) {
if (input.empty() || input[0] != '/') {
return MakeParseError(input, 0, " '/'", GCP_ERROR_INFO());
}
auto s = ParseSegments(input, 1);
if (!s) return std::move(s).status();
auto offset = s->end;
if (input.size() == offset) return PathTemplate{std::move(s->value), {}};
if (input[offset] != ':') {
return MakeParseError(input, offset, " ':'", GCP_ERROR_INFO());
}
++offset;
auto v = ParseLiteral(input, offset);
if (!v) return std::move(v).status();
offset = v->end;
if (offset != input.size()) {
return MakeParseError(input, v->end, " end of input", GCP_ERROR_INFO());
}
return PathTemplate{std::move(s->value),
absl::get<std::string>(v->value.value)};
}
std::ostream& operator<<(std::ostream& os, PathTemplate::Segment const& rhs) {
struct Visitor {
std::ostream& os;
void operator()(PathTemplate::Match const&) { os << "*"; }
void operator()(PathTemplate::MatchRecursive const&) { os << "**"; }
void operator()(std::string const& s) { os << s; }
void operator()(PathTemplate::Variable const& v) {
os << "field_path=" << v.field_path;
if (v.field_path.empty()) return;
os << ", segments=";
StreamSegments(os, v.segments);
}
};
os << "{";
absl::visit(Visitor{os}, rhs.value);
os << "}";
return os;
}
std::ostream& operator<<(std::ostream& os, PathTemplate const& rhs) {
os << "{segments=";
StreamSegments(os, rhs.segments);
if (!rhs.verb.empty()) os << ", verb=" << rhs.verb;
os << "}";
return os;
}
} // namespace generator_internal
} // namespace cloud
} // namespace google