Skip to content

Commit f961bea

Browse files
jnthntatumcopybara-github
authored andcommitted
Add stringifiers for Navigable AST enums.
PiperOrigin-RevId: 588925530
1 parent a8736ee commit f961bea

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

tools/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ cc_library(
5151
"@com_google_absl//absl/container:flat_hash_map",
5252
"@com_google_absl//absl/log:absl_check",
5353
"@com_google_absl//absl/memory",
54+
"@com_google_absl//absl/strings",
55+
"@com_google_absl//absl/strings:string_view",
5456
"@com_google_absl//absl/types:span",
5557
"@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
5658
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
@@ -65,6 +67,8 @@ cc_test(
6567
"//base:builtins",
6668
"//internal:testing",
6769
"//parser",
70+
"@com_google_absl//absl/base",
71+
"@com_google_absl//absl/strings",
6872
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
6973
],
7074
)

tools/navigable_ast.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
#include <cstddef>
1818
#include <memory>
19+
#include <string>
1920
#include <utility>
2021
#include <vector>
2122

2223
#include "google/api/expr/v1alpha1/checked.pb.h"
2324
#include "absl/container/flat_hash_map.h"
2425
#include "absl/log/absl_check.h"
2526
#include "absl/memory/memory.h"
27+
#include "absl/strings/str_cat.h"
2628
#include "absl/types/span.h"
2729
#include "eval/public/ast_traverse.h"
2830
#include "eval/public/ast_visitor.h"
@@ -185,6 +187,64 @@ class NavigableExprBuilderVisitor
185187

186188
} // namespace
187189

190+
std::string ChildKindName(ChildKind kind) {
191+
switch (kind) {
192+
case ChildKind::kUnspecified:
193+
return "Unspecified";
194+
case ChildKind::kSelectOperand:
195+
return "SelectOperand";
196+
case ChildKind::kCallReceiver:
197+
return "CallReceiver";
198+
case ChildKind::kCallArg:
199+
return "CallArg";
200+
case ChildKind::kListElem:
201+
return "ListElem";
202+
case ChildKind::kMapKey:
203+
return "MapKey";
204+
case ChildKind::kMapValue:
205+
return "MapValue";
206+
case ChildKind::kStructValue:
207+
return "StructValue";
208+
case ChildKind::kComprehensionRange:
209+
return "ComprehensionRange";
210+
case ChildKind::kComprehensionInit:
211+
return "ComprehensionInit";
212+
case ChildKind::kComprehensionCondition:
213+
return "ComprehensionCondition";
214+
case ChildKind::kComprehensionLoopStep:
215+
return "ComprehensionLoopStep";
216+
case ChildKind::kComprensionResult:
217+
return "ComprehensionResult";
218+
default:
219+
return absl::StrCat("Unknown ChildKind ", static_cast<int>(kind));
220+
}
221+
}
222+
223+
std::string NodeKindName(NodeKind kind) {
224+
switch (kind) {
225+
case NodeKind::kUnspecified:
226+
return "Unspecified";
227+
case NodeKind::kConstant:
228+
return "Constant";
229+
case NodeKind::kIdent:
230+
return "Ident";
231+
case NodeKind::kSelect:
232+
return "Select";
233+
case NodeKind::kCall:
234+
return "Call";
235+
case NodeKind::kList:
236+
return "List";
237+
case NodeKind::kMap:
238+
return "Map";
239+
case NodeKind::kStruct:
240+
return "Struct";
241+
case NodeKind::kComprehension:
242+
return "Comprehension";
243+
default:
244+
return absl::StrCat("Unknown NodeKind ", static_cast<int>(kind));
245+
}
246+
}
247+
188248
int AstNode::child_index() const {
189249
if (data_.parent == nullptr) {
190250
return -1;

tools/navigable_ast.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstddef>
1919
#include <cstdint>
2020
#include <memory>
21+
#include <string>
2122
#include <utility>
2223
#include <vector>
2324

@@ -57,6 +58,24 @@ enum class NodeKind {
5758
kComprehension,
5859
};
5960

61+
// Human readable ChildKind name. Provided for test readability -- do not depend
62+
// on the specific values.
63+
std::string ChildKindName(ChildKind kind);
64+
65+
template <typename Sink>
66+
void AbslStringify(Sink& sink, ChildKind kind) {
67+
absl::Format(&sink, "%s", ChildKindName(kind));
68+
}
69+
70+
// Human readable NodeKind name. Provided for test readability -- do not depend
71+
// on the specific values.
72+
std::string NodeKindName(NodeKind kind);
73+
74+
template <typename Sink>
75+
void AbslStringify(Sink& sink, NodeKind kind) {
76+
absl::Format(&sink, "%s", NodeKindName(kind));
77+
}
78+
6079
class AstNode;
6180

6281
namespace tools_internal {

tools/navigable_ast_test.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <vector>
1919

2020
#include "google/api/expr/v1alpha1/syntax.pb.h"
21+
#include "absl/base/casts.h"
22+
#include "absl/strings/str_cat.h"
2123
#include "base/builtins.h"
2224
#include "internal/testing.h"
2325
#include "parser/parser.h"
@@ -340,5 +342,45 @@ TEST(NavigableAst, DescendantsPreorderCreateMap) {
340342
Pair(NodeKind::kConstant, ChildKind::kMapValue)));
341343
}
342344

345+
TEST(NodeKind, Stringify) {
346+
// Note: the specific values are not important or guaranteed to be stable,
347+
// they are only intended to make test outputs clearer.
348+
EXPECT_EQ(absl::StrCat(NodeKind::kConstant), "Constant");
349+
EXPECT_EQ(absl::StrCat(NodeKind::kIdent), "Ident");
350+
EXPECT_EQ(absl::StrCat(NodeKind::kSelect), "Select");
351+
EXPECT_EQ(absl::StrCat(NodeKind::kCall), "Call");
352+
EXPECT_EQ(absl::StrCat(NodeKind::kList), "List");
353+
EXPECT_EQ(absl::StrCat(NodeKind::kMap), "Map");
354+
EXPECT_EQ(absl::StrCat(NodeKind::kStruct), "Struct");
355+
EXPECT_EQ(absl::StrCat(NodeKind::kComprehension), "Comprehension");
356+
EXPECT_EQ(absl::StrCat(NodeKind::kUnspecified), "Unspecified");
357+
358+
EXPECT_EQ(absl::StrCat(absl::bit_cast<NodeKind>(255)),
359+
"Unknown NodeKind 255");
360+
}
361+
362+
TEST(ChildKind, Stringify) {
363+
// Note: the specific values are not important or guaranteed to be stable,
364+
// they are only intended to make test outputs clearer.
365+
EXPECT_EQ(absl::StrCat(ChildKind::kSelectOperand), "SelectOperand");
366+
EXPECT_EQ(absl::StrCat(ChildKind::kCallReceiver), "CallReceiver");
367+
EXPECT_EQ(absl::StrCat(ChildKind::kCallArg), "CallArg");
368+
EXPECT_EQ(absl::StrCat(ChildKind::kListElem), "ListElem");
369+
EXPECT_EQ(absl::StrCat(ChildKind::kMapKey), "MapKey");
370+
EXPECT_EQ(absl::StrCat(ChildKind::kMapValue), "MapValue");
371+
EXPECT_EQ(absl::StrCat(ChildKind::kStructValue), "StructValue");
372+
EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionRange), "ComprehensionRange");
373+
EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionInit), "ComprehensionInit");
374+
EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionCondition),
375+
"ComprehensionCondition");
376+
EXPECT_EQ(absl::StrCat(ChildKind::kComprehensionLoopStep),
377+
"ComprehensionLoopStep");
378+
EXPECT_EQ(absl::StrCat(ChildKind::kComprensionResult), "ComprehensionResult");
379+
EXPECT_EQ(absl::StrCat(ChildKind::kUnspecified), "Unspecified");
380+
381+
EXPECT_EQ(absl::StrCat(absl::bit_cast<ChildKind>(255)),
382+
"Unknown ChildKind 255");
383+
}
384+
343385
} // namespace
344386
} // namespace cel

0 commit comments

Comments
 (0)