forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr_printer.cc
More file actions
293 lines (263 loc) · 6.96 KB
/
Copy pathexpr_printer.cc
File metadata and controls
293 lines (263 loc) · 6.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
282
283
284
285
286
287
288
289
290
291
292
293
#include "testutil/expr_printer.h"
#include <string>
#include "absl/strings/str_format.h"
#include "common/escaping.h"
namespace google {
namespace api {
namespace expr {
namespace testutil {
namespace {
using ::google::api::expr::v1alpha1::Expr;
class EmptyAdorner : public ExpressionAdorner {
public:
~EmptyAdorner() {}
std::string adorn(const Expr& e) const { return ""; }
std::string adorn(const Expr::CreateStruct::Entry& e) const {
return "";
}
};
const EmptyAdorner the_empty_adorner;
class Writer {
public:
Writer(const ExpressionAdorner& adorner)
: adorner_(adorner), line_start_(true), indent_(0) {}
void appendExpr(const Expr& e) {
switch (e.expr_kind_case()) {
case Expr::kConstExpr:
append(formatLiteral(e.const_expr()));
break;
case Expr::kIdentExpr:
append(e.ident_expr().name());
break;
case Expr::kSelectExpr:
appendSelect(e.select_expr());
break;
case Expr::kCallExpr:
appendCall(e.call_expr());
break;
case Expr::kListExpr:
appendList(e.list_expr());
break;
case Expr::kStructExpr:
appendStruct(e.struct_expr());
break;
case Expr::kComprehensionExpr:
appendComprehension(e.comprehension_expr());
break;
default:
break;
}
appendAdorn(e);
}
void appendSelect(const Expr::Select& sel) {
appendExpr(sel.operand());
append(".");
append(sel.field());
if (sel.test_only()) {
append("~test-only~");
}
}
void appendCall(const Expr::Call& call) {
if (call.has_target()) {
appendExpr(call.target());
s_ += ".";
}
append(call.function());
append("(");
if (call.args_size() > 0) {
addIndent();
appendLine();
for (int i = 0; i < call.args_size(); ++i) {
const auto& arg = call.args(i);
if (i > 0) {
append(",");
appendLine();
}
appendExpr(arg);
}
removeIndent();
appendLine();
}
append(")");
}
void appendList(const Expr::CreateList& list) {
append("[");
if (list.elements_size() > 0) {
appendLine();
addIndent();
for (int i = 0; i < list.elements_size(); ++i) {
const auto& elem = list.elements(i);
if (i > 0) {
append(",");
appendLine();
}
appendExpr(elem);
}
removeIndent();
appendLine();
}
append("]");
}
void appendStruct(const Expr::CreateStruct& obj) {
if (obj.message_name().empty()) {
appendMap(obj);
} else {
appendObject(obj);
}
}
void appendMap(const Expr::CreateStruct& obj) {
append("{");
if (obj.entries_size() > 0) {
appendLine();
addIndent();
for (int i = 0; i < obj.entries_size(); ++i) {
const auto& entry = obj.entries(i);
if (i > 0) {
append(",");
appendLine();
}
appendExpr(entry.map_key());
append(":");
appendExpr(entry.value());
appendAdorn(entry);
}
removeIndent();
appendLine();
}
append("}");
}
void appendObject(const Expr::CreateStruct& obj) {
append(obj.message_name());
append("{");
if (obj.entries_size() > 0) {
appendLine();
addIndent();
for (int i = 0; i < obj.entries_size(); ++i) {
const auto& entry = obj.entries(i);
if (i > 0) {
append(",");
appendLine();
}
append(entry.field_key());
append(":");
appendExpr(entry.value());
appendAdorn(entry);
}
removeIndent();
appendLine();
}
append("}");
}
void appendComprehension(const Expr::Comprehension& comprehension) {
append("__comprehension__(");
addIndent();
appendLine();
append("// Variable");
appendLine();
append(comprehension.iter_var());
append(",");
appendLine();
append("// Target");
appendLine();
appendExpr(comprehension.iter_range());
append(",");
appendLine();
append("// Accumulator");
appendLine();
append(comprehension.accu_var());
append(",");
appendLine();
append("// Init");
appendLine();
appendExpr(comprehension.accu_init());
append(",");
appendLine();
append("// LoopCondition");
appendLine();
appendExpr(comprehension.loop_condition());
append(",");
appendLine();
append("// LoopStep");
appendLine();
appendExpr(comprehension.loop_step());
append(",");
appendLine();
append("// Result");
appendLine();
appendExpr(comprehension.result());
append(")");
removeIndent();
}
void appendAdorn(const Expr& e) { append(adorner_.adorn(e)); }
void appendAdorn(const Expr::CreateStruct::Entry& e) {
append(adorner_.adorn(e));
}
void append(const std::string& s) {
if (line_start_) {
line_start_ = false;
for (int i = 0; i < indent_; ++i) {
s_ += " ";
}
}
s_ += s;
}
void appendLine() {
s_ += "\n";
line_start_ = true;
}
void addIndent() { indent_ += 1; }
void removeIndent() {
if (indent_ > 0) {
indent_ -= 1;
}
}
std::string formatLiteral(const google::api::expr::v1alpha1::Constant& c) {
switch (c.constant_kind_case()) {
case google::api::expr::v1alpha1::Constant::kBoolValue:
return absl::StrFormat("%s", c.bool_value() ? "true" : "false");
case google::api::expr::v1alpha1::Constant::kBytesValue:
return absl::StrFormat("b\"%s\"", c.bytes_value());
case google::api::expr::v1alpha1::Constant::kDoubleValue: {
std::string s = absl::StrFormat("%f", c.double_value());
// remove trailing zeros, i.e., convert 1.600000 to just 1.6 without
// forcing a specific precision. There seems to be no flag to get this
// directly from absl::StrFormat.
auto idx = std::find_if_not(s.rbegin(), s.rend(),
[](const char c) { return c == '0'; });
s.erase(idx.base(), s.end());
return s;
}
case google::api::expr::v1alpha1::Constant::kInt64Value:
return absl::StrFormat("%d", c.int64_value());
case google::api::expr::v1alpha1::Constant::kStringValue:
return parser::escapeAndQuote(c.string_value());
case google::api::expr::v1alpha1::Constant::kUint64Value:
return absl::StrFormat("%uu", c.uint64_value());
case google::api::expr::v1alpha1::Constant::kNullValue:
return "null";
default:
return "<<ERROR>>";
}
}
std::string print(const Expr& expr) {
appendExpr(expr);
return s_;
}
private:
std::string s_;
const ExpressionAdorner& adorner_;
bool line_start_;
int indent_;
};
} // namespace
const ExpressionAdorner& empty_adorner() {
return the_empty_adorner;
}
std::string ExprPrinter::print(const Expr& expr) const {
Writer w(adorner_);
return w.print(expr);
}
} // namespace testutil
} // namespace expr
} // namespace api
} // namespace google