Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions checker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cc_library(
deps = [
":type_check_issue",
"//common:ast",
"//common:source",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down
5 changes: 4 additions & 1 deletion checker/type_checker_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ absl::Status TypeCheckerBuilder::AddLibrary(CheckerLibrary library) {
return absl::AlreadyExistsError(
absl::StrCat("library '", library.id, "' already exists"));
}
absl::Status status = library.options(*this);
if (!library.configure) {
return absl::OkStatus();
}
absl::Status status = library.configure(*this);

libraries_.push_back(std::move(library));
return status;
Expand Down
9 changes: 5 additions & 4 deletions checker/type_checker_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
descriptor_pool,
const CheckerOptions& options = {});

using ConfigureBuilderCallback =
absl::AnyInvocable<absl::Status(TypeCheckerBuilder&)>;
// Functional implementation to apply the library features to a
// TypeCheckerBuilder.
using TypeCheckerBuilderConfigurer =
absl::AnyInvocable<absl::Status(TypeCheckerBuilder&) const>;

struct CheckerLibrary {
// Optional identifier to avoid collisions re-adding the same declarations.
// If id is empty, it is not considered.
std::string id;
// Functional implementation applying the library features to the builder.
ConfigureBuilderCallback options;
TypeCheckerBuilderConfigurer configure;
};

// Builder for TypeChecker instances.
Expand Down
13 changes: 13 additions & 0 deletions checker/validation_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "absl/types/span.h"
#include "checker/type_check_issue.h"
#include "common/ast.h"
#include "common/source.h"

namespace cel {

Expand Down Expand Up @@ -56,9 +57,21 @@ class ValidationResult {

absl::Span<const TypeCheckIssue> GetIssues() const { return issues_; }

// The source expression may optionally be set if it is available.
absl::Nullable<const cel::Source*> GetSource() const { return source_.get(); }

void SetSource(std::unique_ptr<Source> source) {
source_ = std::move(source);
}

absl::Nullable<std::unique_ptr<cel::Source>> ReleaseSource() {
return std::move(source_);
}

private:
absl::Nullable<std::unique_ptr<Ast>> ast_;
std::vector<TypeCheckIssue> issues_;
absl::Nullable<std::unique_ptr<Source>> source_;
};

} // namespace cel
Expand Down
78 changes: 78 additions & 0 deletions compiler/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2024 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.

package(default_visibility = ["//visibility:public"])

cc_library(
name = "compiler",
hdrs = ["compiler.h"],
deps = [
"//checker:checker_options",
"//checker:type_checker_builder",
"//checker:validation_result",
"//parser:options",
"//parser:parser_interface",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
name = "compiler_factory",
srcs = ["compiler_factory.cc"],
hdrs = ["compiler_factory.h"],
deps = [
":compiler",
"//checker:type_checker",
"//checker:type_checker_builder",
"//checker:validation_result",
"//common:source",
"//internal:noop_delete",
"//internal:status_macros",
"//parser",
"//parser:parser_interface",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "compiler_factory_test",
srcs = ["compiler_factory_test.cc"],
deps = [
":compiler",
":compiler_factory",
"//checker:optional",
"//checker:standard_library",
"//checker:type_check_issue",
"//checker:validation_result",
"//common:decl",
"//common:type",
"//internal:testing",
"//internal:testing_descriptor_pool",
"//parser:macro",
"//parser:parser_interface",
"//testutil:baseline_tests",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_protobuf//:protobuf",
],
)
117 changes: 117 additions & 0 deletions compiler/compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2024 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.

#ifndef THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_
#define THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_

#include <memory>
#include <string>
#include <utility>

#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "checker/checker_options.h"
#include "checker/type_checker_builder.h"
#include "checker/validation_result.h"
#include "parser/options.h"
#include "parser/parser_interface.h"

namespace cel {

class Compiler;
class CompilerBuilder;

// Callable for configuring a ParserBuilder.
using ParserBuilderConfigurer =
absl::AnyInvocable<absl::Status(ParserBuilder&) const>;

// A CompilerLibrary represents a package of CEL configuration that can be
// added to a Compiler.
//
// It may contain either or both of a Parser configuration and a
// TypeChecker configuration.
struct CompilerLibrary {
// Optional identifier to avoid collisions re-adding the same library.
// If id is empty, it is not considered.
std::string id;
// Optional callback for configuring the parser.
ParserBuilderConfigurer configure_parser;
// Optional callback for configuring the type checker.
TypeCheckerBuilderConfigurer configure_checker;

CompilerLibrary(std::string id, ParserBuilderConfigurer configure_parser,
TypeCheckerBuilderConfigurer configure_checker = nullptr)
: id(std::move(id)),
configure_parser(std::move(configure_parser)),
configure_checker(std::move(configure_checker)) {}

CompilerLibrary(std::string id,
TypeCheckerBuilderConfigurer configure_checker)
: id(std::move(id)),
configure_parser(std::move(nullptr)),
configure_checker(std::move(configure_checker)) {}

// Convenience conversion from the CheckerLibrary type.
// NOLINTNEXTLINE(google-explicit-constructor)
CompilerLibrary(CheckerLibrary checker_library)
: id(std::move(checker_library.id)),
configure_parser(nullptr),
configure_checker(std::move(checker_library.configure)) {}
};

// General options for configuring the underlying parser and checker.
struct CompilerOptions {
ParserOptions parser_options;
CheckerOptions checker_options;
};

// Interface for CEL CompilerBuilder objects.
//
// Builder implementations are thread hostile, but should create
// thread-compatible Compiler instances.
class CompilerBuilder {
public:
virtual ~CompilerBuilder() = default;

virtual absl::Status AddLibrary(cel::CompilerLibrary library) = 0;

virtual TypeCheckerBuilder& GetCheckerBuilder() = 0;
virtual ParserBuilder& GetParserBuilder() = 0;

virtual absl::StatusOr<std::unique_ptr<Compiler>> Build() && = 0;
};

// Interface for CEL Compiler objects.
//
// For CEL, compilation is the process of bundling the parse and type-check
// passes.
//
// Compiler instances should be thread-compatible.
class Compiler {
public:
virtual ~Compiler() = default;

virtual absl::StatusOr<ValidationResult> Compile(
absl::string_view source, absl::string_view description) const = 0;

absl::StatusOr<ValidationResult> Compile(absl::string_view source) const {
return Compile(source, "<input>");
}
};

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_
Loading