Skip to content

Commit 1c391a5

Browse files
jnthntatumcopybara-github
authored andcommitted
Add accessors for underlying parser / type checker on cel::Compiler interface.
PiperOrigin-RevId: 743192974
1 parent 1ca25a6 commit 1c391a5

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

compiler/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cc_library(
1919
hdrs = ["compiler.h"],
2020
deps = [
2121
"//checker:checker_options",
22+
"//checker:type_checker",
2223
"//checker:type_checker_builder",
2324
"//checker:validation_result",
2425
"//parser:options",
@@ -41,6 +42,7 @@ cc_library(
4142
"//checker:type_checker_builder_factory",
4243
"//checker:validation_result",
4344
"//common:source",
45+
"//common:type",
4446
"//internal:noop_delete",
4547
"//internal:status_macros",
4648
"//parser",
@@ -64,8 +66,10 @@ cc_test(
6466
"//checker:optional",
6567
"//checker:standard_library",
6668
"//checker:type_check_issue",
69+
"//checker:type_checker",
6770
"//checker:validation_result",
6871
"//common:decl",
72+
"//common:source",
6973
"//common:type",
7074
"//internal:testing",
7175
"//internal:testing_descriptor_pool",

compiler/compiler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/status/statusor.h"
2525
#include "absl/strings/string_view.h"
2626
#include "checker/checker_options.h"
27+
#include "checker/type_checker.h"
2728
#include "checker/type_checker_builder.h"
2829
#include "checker/validation_result.h"
2930
#include "parser/options.h"
@@ -110,6 +111,12 @@ class Compiler {
110111
absl::StatusOr<ValidationResult> Compile(absl::string_view source) const {
111112
return Compile(source, "<input>");
112113
}
114+
115+
// Accessor for the underlying type checker.
116+
virtual const TypeChecker& GetTypeChecker() const = 0;
117+
118+
// Accessor for the underlying parser.
119+
virtual const Parser& GetParser() const = 0;
113120
};
114121

115122
} // namespace cel

compiler/compiler_factory.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class CompilerImpl : public Compiler {
5858
return result;
5959
}
6060

61+
const TypeChecker& GetTypeChecker() const override { return *type_checker_; }
62+
const Parser& GetParser() const override { return *parser_; }
63+
6164
private:
6265
std::unique_ptr<TypeChecker> type_checker_;
6366
std::unique_ptr<Parser> parser_;

compiler/compiler_factory_test.cc

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include "checker/optional.h"
2424
#include "checker/standard_library.h"
2525
#include "checker/type_check_issue.h"
26+
#include "checker/type_checker.h"
2627
#include "checker/validation_result.h"
2728
#include "common/decl.h"
29+
#include "common/source.h"
2830
#include "common/type.h"
2931
#include "compiler/compiler.h"
3032
#include "internal/testing.h"
@@ -51,7 +53,6 @@ TEST(CompilerFactoryTest, Works) {
5153
NewCompilerBuilder(cel::internal::GetSharedTestingDescriptorPool()));
5254

5355
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
54-
builder->GetParserBuilder().GetOptions().enable_hidden_accumulator_var = true;
5556
ASSERT_OK_AND_ASSIGN(auto compiler, std::move(*builder).Build());
5657

5758
ASSERT_OK_AND_ASSIGN(
@@ -164,7 +165,52 @@ TEST(CompilerFactoryTest, ParserOptions) {
164165
ASSERT_THAT(compiler->Compile("a.?b.orValue('foo')"), IsOk());
165166
}
166167

167-
TEST(CompilerFactoryTest, DisableStandarMacros) {
168+
TEST(CompilerFactoryTest, GetParser) {
169+
ASSERT_OK_AND_ASSIGN(
170+
auto builder,
171+
NewCompilerBuilder(cel::internal::GetSharedTestingDescriptorPool()));
172+
173+
ASSERT_OK_AND_ASSIGN(auto compiler, std::move(*builder).Build());
174+
175+
const cel::Parser& parser = compiler->GetParser();
176+
177+
ASSERT_OK_AND_ASSIGN(auto source, cel::NewSource("Or(a, b)"));
178+
ASSERT_OK_AND_ASSIGN(auto ast, parser.Parse(*source));
179+
}
180+
181+
TEST(CompilerFactoryTest, GetTypeChecker) {
182+
ASSERT_OK_AND_ASSIGN(
183+
auto builder,
184+
NewCompilerBuilder(cel::internal::GetSharedTestingDescriptorPool()));
185+
186+
absl::Status s;
187+
s.Update(builder->GetCheckerBuilder().AddVariable(
188+
MakeVariableDecl("a", BoolType())));
189+
190+
s.Update(builder->GetCheckerBuilder().AddVariable(
191+
MakeVariableDecl("b", BoolType())));
192+
193+
ASSERT_OK_AND_ASSIGN(
194+
auto or_decl,
195+
MakeFunctionDecl("Or", MakeOverloadDecl("Or_bool_bool", BoolType(),
196+
BoolType(), BoolType())));
197+
s.Update(builder->GetCheckerBuilder().AddFunction(std::move(or_decl)));
198+
199+
ASSERT_THAT(s, IsOk());
200+
ASSERT_OK_AND_ASSIGN(auto compiler, std::move(*builder).Build());
201+
202+
const cel::Parser& parser = compiler->GetParser();
203+
204+
ASSERT_OK_AND_ASSIGN(auto source, cel::NewSource("Or(a, b)"));
205+
ASSERT_OK_AND_ASSIGN(auto ast, parser.Parse(*source));
206+
207+
const cel::TypeChecker& checker = compiler->GetTypeChecker();
208+
ASSERT_OK_AND_ASSIGN(cel::ValidationResult result,
209+
checker.Check(std::move(ast)));
210+
EXPECT_TRUE(result.IsValid());
211+
}
212+
213+
TEST(CompilerFactoryTest, DisableStandardMacros) {
168214
CompilerOptions options;
169215
options.parser_options.disable_standard_macros = true;
170216

0 commit comments

Comments
 (0)