-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcompiler.h
More file actions
195 lines (165 loc) · 6.71 KB
/
Copy pathcompiler.h
File metadata and controls
195 lines (165 loc) · 6.71 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
// 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/base/nullability.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.h"
#include "checker/type_checker_builder.h"
#include "checker/validation_result.h"
#include "common/source.h"
#include "parser/options.h"
#include "parser/parser_interface.h"
#include "validator/validator.h"
#include "google/protobuf/arena.h"
namespace cel {
class Compiler;
class CompilerBuilder;
// 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.
//
// Note: if a related CompilerLibrary exists, prefer to use that to
// include expected parser configuration.
static CompilerLibrary FromCheckerLibrary(CheckerLibrary checker_library) {
return CompilerLibrary(std::move(checker_library.id),
/*configure_parser=*/nullptr,
std::move(checker_library.configure));
}
// For backwards compatibility. To be removed.
// NOLINTNEXTLINE(google-explicit-constructor)
CompilerLibrary(CheckerLibrary checker_library)
: id(std::move(checker_library.id)),
configure_parser(nullptr),
configure_checker(std::move(checker_library.configure)) {}
};
struct CompilerLibrarySubset {
// The id of the library to subset. Only one subset can be applied per
// library id.
//
// Must be non-empty.
std::string library_id;
ParserLibrarySubset::MacroPredicate should_include_macro;
TypeCheckerSubset::FunctionPredicate should_include_overload;
// TODO(uncreated-issue/71): to faithfully report the subset back, we need to track
// the default (include or exclude) behavior for each of the predicates.
};
// General options for configuring the underlying parser and checker.
struct CompilerOptions {
ParserOptions parser_options;
CheckerOptions checker_options;
// If true, parse errors will be adapted to issues where possible.
bool adapt_parser_errors = false;
};
// Interface for CEL CompilerBuilder objects.
//
// Builder implementations do not provide any synchronization themselves,
// but create thread-compatible Compiler instances.
class CompilerBuilder {
public:
virtual ~CompilerBuilder() = default;
virtual absl::Status AddLibrary(CompilerLibrary library) = 0;
virtual absl::Status AddLibrarySubset(CompilerLibrarySubset subset) = 0;
virtual TypeCheckerBuilder& GetCheckerBuilder() = 0;
virtual ParserBuilder& GetParserBuilder() = 0;
virtual Validator& GetValidator() = 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;
absl::StatusOr<ValidationResult> Compile(
const Source& source, google::protobuf::Arena* absl_nullable arena) const {
return CompileImpl(source, arena);
}
absl::StatusOr<ValidationResult> Compile(const Source& source) const {
return CompileImpl(source, nullptr);
}
absl::StatusOr<ValidationResult> Compile(
absl::string_view source, absl::string_view description,
google::protobuf::Arena* absl_nullable arena) const;
absl::StatusOr<ValidationResult> Compile(absl::string_view source) const {
return Compile(source, "<input>", nullptr);
}
absl::StatusOr<ValidationResult> Compile(
absl::string_view source, absl::string_view description) const {
return Compile(source, description, nullptr);
}
// Accessor for the underlying type checker.
virtual const TypeChecker& GetTypeChecker() const = 0;
// Accessor for the underlying parser.
virtual const Parser& GetParser() const = 0;
// Accessor for the underlying validator.
virtual const Validator& GetValidator() const = 0;
// Returns a builder initialized with the configuration of this compiler.
//
// The returned builder is a copy of the validated environment and may
// behave differently than the builder that created this compiler.
//
// The returned builder does not share state with the compiler and may be
// modified independently.
virtual std::unique_ptr<CompilerBuilder> ToBuilder() const = 0;
protected:
virtual absl::StatusOr<ValidationResult> CompileImpl(
const Source& source, google::protobuf::Arena* absl_nullable arena) const = 0;
};
inline absl::StatusOr<ValidationResult> Compiler::Compile(
absl::string_view source, absl::string_view description,
google::protobuf::Arena* absl_nullable arena) const {
absl::StatusOr<SourcePtr> source_obj =
NewSource(source, std::string(description));
if (!source_obj.ok()) {
return source_obj.status();
}
absl::StatusOr<ValidationResult> result = CompileImpl(**source_obj, arena);
if (result.ok()) {
result->SetSource(std::move(*source_obj));
}
return result;
}
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_