-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCelCompilerImpl.java
More file actions
334 lines (284 loc) · 9.94 KB
/
Copy pathCelCompilerImpl.java
File metadata and controls
334 lines (284 loc) · 9.94 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2022 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 dev.cel.compiler;
import static com.google.common.base.Preconditions.checkNotNull;
import dev.cel.expr.Decl;
import dev.cel.expr.Type;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import dev.cel.checker.CelChecker;
import dev.cel.checker.CelCheckerBuilder;
import dev.cel.checker.CelStandardDeclarations;
import dev.cel.checker.ProtoTypeMask;
import dev.cel.checker.TypeProvider;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelContainer;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelSource;
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.types.CelProtoTypes;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.parser.CelMacro;
import dev.cel.parser.CelParser;
import dev.cel.parser.CelParserBuilder;
import dev.cel.parser.CelStandardMacro;
import java.util.Arrays;
/**
* CelCompiler implementation which uses either the legacy or modernized CEL-Java stack to offer a
* stream-lined expression parse/type-check experience, via a single {@code compile} method.
*
* <p>CEL Library Internals. Do Not Use. Consumers should use factories, such as {@link
* CelCompilerFactory} instead to instantiate a compiler.
*/
@Immutable
@Internal
public final class CelCompilerImpl implements CelCompiler, EnvVisitable {
private final CelParser parser;
private final CelChecker checker;
@Override
public CelValidationResult parse(String expression, String description) {
return parser.parse(expression, description);
}
@Override
public CelValidationResult parse(CelSource source) {
return parser.parse(source);
}
@Override
public CelValidationResult check(CelAbstractSyntaxTree ast) {
return checker.check(ast);
}
@Override
public CelTypeProvider getTypeProvider() {
return checker.getTypeProvider();
}
@Override
public void accept(EnvVisitor envVisitor) {
if (checker instanceof EnvVisitable) {
((EnvVisitable) checker).accept(envVisitor);
}
if (parser instanceof EnvVisitable) {
((EnvVisitable) parser).accept(envVisitor);
}
}
@Override
public CelCompilerBuilder toCompilerBuilder() {
return newBuilder(toParserBuilder(), toCheckerBuilder());
}
@Override
public CelCheckerBuilder toCheckerBuilder() {
return checker.toCheckerBuilder();
}
@Override
public CelParserBuilder toParserBuilder() {
return parser.toParserBuilder();
}
/** Combines a prebuilt {@link CelParser} and {@link CelChecker} into {@link CelCompilerImpl}. */
static CelCompilerImpl combine(CelParser parser, CelChecker checker) {
return new CelCompilerImpl(parser, checker);
}
/**
* Create a new builder for constructing a {@code CelCompiler} instance.
*
* <p>By default, {@link CelOptions#DEFAULT} are enabled, as is the CEL standard environment.
*/
public static CelCompilerBuilder newBuilder(
CelParserBuilder parserBuilder, CelCheckerBuilder checkerBuilder) {
return new Builder(parserBuilder, checkerBuilder);
}
/** Builder for {@code CelCompilerImpl} */
public static final class Builder implements CelCompilerBuilder {
private final CelParserBuilder parserBuilder;
private final CelCheckerBuilder checkerBuilder;
@Override
public CelCompilerBuilder setOptions(CelOptions options) {
parserBuilder.setOptions(options);
checkerBuilder.setOptions(options);
return this;
}
@Override
public CelCompilerBuilder setStandardMacros(CelStandardMacro... macros) {
parserBuilder.setStandardMacros(macros);
return this;
}
@Override
public CelCompilerBuilder setStandardMacros(Iterable<CelStandardMacro> macros) {
parserBuilder.setStandardMacros(macros);
return this;
}
@Override
public CelCompilerBuilder addMacros(CelMacro... macros) {
checkNotNull(macros);
return addMacros(Arrays.asList(macros));
}
@Override
public CelCompilerBuilder addMacros(Iterable<CelMacro> macros) {
checkNotNull(macros);
parserBuilder.addMacros(macros);
return this;
}
@Override
public CelCompilerBuilder setContainer(String container) {
checkerBuilder.setContainer(container);
return this;
}
@Override
public CelCompilerBuilder setContainer(CelContainer container) {
checkerBuilder.setContainer(container);
return this;
}
@Override
public CelContainer container() {
return checkerBuilder.container();
}
@Override
public CelCompilerBuilder addVar(String name, Type type) {
return addVar(name, CelProtoTypes.typeToCelType(type));
}
@Override
public CelCompilerBuilder addVar(String name, CelType type) {
return addVarDeclarations(CelVarDecl.newVarDeclaration(name, type));
}
@Override
public CelCompilerBuilder addDeclarations(Decl... declarations) {
checkerBuilder.addDeclarations(declarations);
return this;
}
@Override
public CelCompilerBuilder addDeclarations(Iterable<Decl> declarations) {
checkerBuilder.addDeclarations(declarations);
return this;
}
@Override
public CelCompilerBuilder addFunctionDeclarations(CelFunctionDecl... celFunctionDecls) {
checkerBuilder.addFunctionDeclarations(celFunctionDecls);
return this;
}
@Override
public CelCompilerBuilder addFunctionDeclarations(Iterable<CelFunctionDecl> celFunctionDecls) {
checkerBuilder.addFunctionDeclarations(celFunctionDecls);
return this;
}
@Override
public CelCompilerBuilder addVarDeclarations(CelVarDecl... celVarDecls) {
checkerBuilder.addVarDeclarations(celVarDecls);
return this;
}
@Override
public CelCompilerBuilder addVarDeclarations(Iterable<CelVarDecl> celVarDecls) {
checkerBuilder.addVarDeclarations(celVarDecls);
return this;
}
@Override
public CelCompilerBuilder addProtoTypeMasks(ProtoTypeMask... typeMasks) {
checkerBuilder.addProtoTypeMasks(typeMasks);
return this;
}
@Override
public CelCompilerBuilder addProtoTypeMasks(Iterable<ProtoTypeMask> typeMasks) {
checkerBuilder.addProtoTypeMasks(typeMasks);
return this;
}
@Override
public CelCompilerBuilder setResultType(CelType resultType) {
checkNotNull(resultType);
return setProtoResultType(CelProtoTypes.celTypeToType(resultType));
}
@Override
public CelCompilerBuilder setProtoResultType(Type resultType) {
checkerBuilder.setProtoResultType(resultType);
return this;
}
@Override
@Deprecated
public CelCompilerBuilder setTypeProvider(TypeProvider typeProvider) {
checkerBuilder.setTypeProvider(typeProvider);
return this;
}
@Override
public CelCompilerBuilder setTypeProvider(CelTypeProvider celTypeProvider) {
checkerBuilder.setTypeProvider(celTypeProvider);
return this;
}
@Override
public CelCompilerBuilder addMessageTypes(Descriptor... descriptors) {
checkerBuilder.addMessageTypes(descriptors);
return this;
}
@Override
public CelCompilerBuilder addMessageTypes(Iterable<Descriptor> descriptors) {
checkerBuilder.addMessageTypes(descriptors);
return this;
}
@Override
public CelCompilerBuilder addFileTypes(FileDescriptor... fileDescriptors) {
checkerBuilder.addFileTypes(fileDescriptors);
return this;
}
@Override
public CelCompilerBuilder addFileTypes(Iterable<FileDescriptor> fileDescriptors) {
checkerBuilder.addFileTypes(fileDescriptors);
return this;
}
@Override
public CelCompilerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
checkerBuilder.addFileTypes(fileDescriptorSet);
return this;
}
@Override
public CelCompilerBuilder setStandardEnvironmentEnabled(boolean value) {
checkerBuilder.setStandardEnvironmentEnabled(value);
return this;
}
@Override
public CelCompilerBuilder setStandardDeclarations(
CelStandardDeclarations standardDeclarations) {
checkerBuilder.setStandardDeclarations(standardDeclarations);
return this;
}
@Override
public CelCompilerBuilder addLibraries(CelCompilerLibrary... libraries) {
checkNotNull(libraries);
return this.addLibraries(Arrays.asList(libraries));
}
@Override
public CelCompilerBuilder addLibraries(Iterable<? extends CelCompilerLibrary> libraries) {
checkNotNull(libraries);
parserBuilder.addLibraries(libraries);
checkerBuilder.addLibraries(libraries);
return this;
}
@Override
@CheckReturnValue
public CelCompilerImpl build() {
return new CelCompilerImpl(parserBuilder.build(), checkerBuilder.build());
}
private Builder(CelParserBuilder parserBuilder, CelCheckerBuilder checkerBuilder) {
this.parserBuilder = parserBuilder;
this.checkerBuilder = checkerBuilder;
}
}
private CelCompilerImpl(CelParser parser, CelChecker checker) {
this.parser = parser;
this.checker = checker;
}
}