|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_ |
| 16 | +#define THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_ |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <string> |
| 20 | +#include <utility> |
| 21 | + |
| 22 | +#include "absl/functional/any_invocable.h" |
| 23 | +#include "absl/status/status.h" |
| 24 | +#include "absl/status/statusor.h" |
| 25 | +#include "absl/strings/string_view.h" |
| 26 | +#include "checker/checker_options.h" |
| 27 | +#include "checker/type_checker_builder.h" |
| 28 | +#include "checker/validation_result.h" |
| 29 | +#include "parser/options.h" |
| 30 | +#include "parser/parser_interface.h" |
| 31 | + |
| 32 | +namespace cel { |
| 33 | + |
| 34 | +class Compiler; |
| 35 | +class CompilerBuilder; |
| 36 | + |
| 37 | +// Callable for configuring a ParserBuilder. |
| 38 | +using ParserBuilderConfigurer = |
| 39 | + absl::AnyInvocable<absl::Status(ParserBuilder&) const>; |
| 40 | + |
| 41 | +// A CompilerLibrary represents a package of CEL configuration that can be |
| 42 | +// added to a Compiler. |
| 43 | +// |
| 44 | +// It may contain either or both of a Parser configuration and a |
| 45 | +// TypeChecker configuration. |
| 46 | +struct CompilerLibrary { |
| 47 | + // Optional identifier to avoid collisions re-adding the same library. |
| 48 | + // If id is empty, it is not considered. |
| 49 | + std::string id; |
| 50 | + // Optional callback for configuring the parser. |
| 51 | + ParserBuilderConfigurer configure_parser; |
| 52 | + // Optional callback for configuring the type checker. |
| 53 | + TypeCheckerBuilderConfigurer configure_checker; |
| 54 | + |
| 55 | + CompilerLibrary(std::string id, ParserBuilderConfigurer configure_parser, |
| 56 | + TypeCheckerBuilderConfigurer configure_checker = nullptr) |
| 57 | + : id(std::move(id)), |
| 58 | + configure_parser(std::move(configure_parser)), |
| 59 | + configure_checker(std::move(configure_checker)) {} |
| 60 | + |
| 61 | + CompilerLibrary(std::string id, |
| 62 | + TypeCheckerBuilderConfigurer configure_checker) |
| 63 | + : id(std::move(id)), |
| 64 | + configure_parser(std::move(nullptr)), |
| 65 | + configure_checker(std::move(configure_checker)) {} |
| 66 | + |
| 67 | + // Convenience conversion from the CheckerLibrary type. |
| 68 | + // NOLINTNEXTLINE(google-explicit-constructor) |
| 69 | + CompilerLibrary(CheckerLibrary checker_library) |
| 70 | + : id(std::move(checker_library.id)), |
| 71 | + configure_parser(nullptr), |
| 72 | + configure_checker(std::move(checker_library.configure)) {} |
| 73 | +}; |
| 74 | + |
| 75 | +// General options for configuring the underlying parser and checker. |
| 76 | +struct CompilerOptions { |
| 77 | + ParserOptions parser_options; |
| 78 | + CheckerOptions checker_options; |
| 79 | +}; |
| 80 | + |
| 81 | +// Interface for CEL CompilerBuilder objects. |
| 82 | +// |
| 83 | +// Builder implementations are thread hostile, but should create |
| 84 | +// thread-compatible Compiler instances. |
| 85 | +class CompilerBuilder { |
| 86 | + public: |
| 87 | + virtual ~CompilerBuilder() = default; |
| 88 | + |
| 89 | + virtual absl::Status AddLibrary(cel::CompilerLibrary library) = 0; |
| 90 | + |
| 91 | + virtual TypeCheckerBuilder& GetCheckerBuilder() = 0; |
| 92 | + virtual ParserBuilder& GetParserBuilder() = 0; |
| 93 | + |
| 94 | + virtual absl::StatusOr<std::unique_ptr<Compiler>> Build() && = 0; |
| 95 | +}; |
| 96 | + |
| 97 | +// Interface for CEL Compiler objects. |
| 98 | +// |
| 99 | +// For CEL, compilation is the process of bundling the parse and type-check |
| 100 | +// passes. |
| 101 | +// |
| 102 | +// Compiler instances should be thread-compatible. |
| 103 | +class Compiler { |
| 104 | + public: |
| 105 | + virtual ~Compiler() = default; |
| 106 | + |
| 107 | + virtual absl::StatusOr<ValidationResult> Compile( |
| 108 | + absl::string_view source, absl::string_view description) const = 0; |
| 109 | + |
| 110 | + absl::StatusOr<ValidationResult> Compile(absl::string_view source) const { |
| 111 | + return Compile(source, "<input>"); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace cel |
| 116 | + |
| 117 | +#endif // THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_ |
0 commit comments