-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCelSource.java
More file actions
389 lines (329 loc) · 12 KB
/
Copy pathCelSource.java
File metadata and controls
389 lines (329 loc) · 12 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// 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.common;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.internal.CelCodePointArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/** Represents the source content of an expression and related metadata. */
@Immutable
@AutoValue
public abstract class CelSource implements Source {
abstract CelCodePointArray codePoints();
abstract String description();
abstract ImmutableList<Integer> lineOffsets();
abstract ImmutableMap<Long, Integer> positions();
abstract ImmutableMap<Long, CelExpr> macroCalls();
abstract ImmutableSet<Extension> extensions();
@Override
public CelCodePointArray getContent() {
return codePoints();
}
@Override
public String getDescription() {
return description();
}
@Override
public ImmutableMap<Long, Integer> getPositionsMap() {
return positions();
}
/**
* Get the code point offsets (NOT code unit offsets) for new line characters '\n' within the
* source expression text.
*
* <p>NOTE: The indices point to the index just after the '\n' not the index of '\n' itself.
*/
public ImmutableList<Integer> getLineOffsets() {
return lineOffsets();
}
public ImmutableMap<Long, CelExpr> getMacroCalls() {
return macroCalls();
}
public ImmutableSet<Extension> getExtensions() {
return extensions();
}
/** See {@link #getLocationOffset(int, int)}. */
public Optional<Integer> getLocationOffset(CelSourceLocation location) {
checkNotNull(location);
return getLocationOffset(location.getLine(), location.getColumn());
}
/**
* Get the code point offset within the source expression text that corresponds with the {@code
* line} and {@code column}.
*
* @param line the line number starting from 1
* @param column the column number starting from 0
*/
public Optional<Integer> getLocationOffset(int line, int column) {
return getLocationOffsetImpl(lineOffsets(), line, column);
}
/**
* Get the line and column in the source expression text for the given code point {@code offset}.
*/
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
return CelSourceHelper.getOffsetLocation(codePoints(), offset);
}
@Override
public Optional<String> getSnippet(int line) {
return CelSourceHelper.getSnippet(codePoints(), line);
}
/**
* Get the code point offset within the source expression text that corresponds with the {@code
* line} and {@code column}.
*
* @param line the line number starting from 1
* @param column the column number starting from 0
*/
private static Optional<Integer> getLocationOffsetImpl(
List<Integer> lineOffsets, int line, int column) {
if (line <= 0 || column < 0) {
return Optional.empty();
}
int offset = CelSourceHelper.findLineOffset(lineOffsets, line);
if (offset == -1) {
return Optional.empty();
}
return Optional.of(offset + column);
}
public Builder toBuilder() {
return new Builder(codePoints(), lineOffsets())
.setDescription(description())
.addPositionsMap(positions())
.addAllExtensions(extensions())
.addAllMacroCalls(macroCalls());
}
public static Builder newBuilder() {
return new Builder();
}
public static Builder newBuilder(String text) {
return newBuilder(CelCodePointArray.fromString(text));
}
public static Builder newBuilder(CelCodePointArray codePoints) {
return newBuilder(codePoints, codePoints.lineOffsets());
}
public static Builder newBuilder(
CelCodePointArray codePoints, ImmutableList<Integer> lineOffsets) {
return new Builder(codePoints, lineOffsets);
}
/** Builder for {@link CelSource}. */
public static final class Builder {
private final CelCodePointArray codePoints;
private final List<Integer> lineOffsets;
private final Map<Long, Integer> positions;
private final Map<Long, CelExpr> macroCalls;
private final ImmutableSet.Builder<Extension> extensions;
private final boolean lineOffsetsAlreadyComputed;
private String description;
private Builder() {
this(CelCodePointArray.fromString(""), new ArrayList<>());
}
private Builder(CelCodePointArray codePoints, List<Integer> lineOffsets) {
this.codePoints = checkNotNull(codePoints);
this.lineOffsets = checkNotNull(lineOffsets);
this.positions = new HashMap<>();
this.macroCalls = new HashMap<>();
this.extensions = ImmutableSet.builder();
this.description = "";
this.lineOffsetsAlreadyComputed = !lineOffsets.isEmpty();
}
@CanIgnoreReturnValue
public Builder setDescription(String description) {
this.description = checkNotNull(description);
return this;
}
@CanIgnoreReturnValue
public Builder addLineOffsets(int lineOffset) {
checkArgument(lineOffset >= 0);
checkState(
!lineOffsetsAlreadyComputed,
"Line offsets were already been computed through the provided code points.");
lineOffsets.add(lineOffset);
return this;
}
@CanIgnoreReturnValue
public Builder addAllLineOffsets(Iterable<Integer> lineOffsets) {
for (int lineOffset : lineOffsets) {
addLineOffsets(lineOffset);
}
return this;
}
@CanIgnoreReturnValue
public Builder addPositionsMap(Map<Long, Integer> positionsMap) {
checkNotNull(positionsMap);
this.positions.putAll(positionsMap);
return this;
}
@CanIgnoreReturnValue
public Builder addPositions(long exprId, int position) {
this.positions.put(exprId, position);
return this;
}
@CanIgnoreReturnValue
public Builder removePositions(long exprId) {
this.positions.remove(exprId);
return this;
}
@CanIgnoreReturnValue
public Builder addMacroCalls(long exprId, CelExpr expr) {
this.macroCalls.put(exprId, expr);
return this;
}
@CanIgnoreReturnValue
public Builder addAllMacroCalls(Map<Long, CelExpr> macroCalls) {
this.macroCalls.putAll(macroCalls);
return this;
}
public ImmutableSet<Extension> getExtensions() {
return extensions.build();
}
/**
* Adds one or more {@link Extension}s to the source information. Extensions implement set
* semantics and deduped if same ones are provided.
*/
@CanIgnoreReturnValue
public Builder addAllExtensions(Iterable<? extends Extension> extensions) {
checkNotNull(extensions);
this.extensions.addAll(extensions);
return this;
}
/**
* Adds one or more {@link Extension}s to the source information. Extensions implement set
* semantics and deduped if same ones are provided.
*/
@CanIgnoreReturnValue
public Builder addAllExtensions(Extension... extensions) {
return addAllExtensions(Arrays.asList(extensions));
}
/** See {@link #getLocationOffset(int, int)}. */
public Optional<Integer> getLocationOffset(CelSourceLocation location) {
checkNotNull(location);
return getLocationOffset(location.getLine(), location.getColumn());
}
/**
* Get the code point offset within the source expression text that corresponds with the {@code
* line} and {@code column}.
*
* @param line the line number starting from 1
* @param column the column number starting from 0
*/
public Optional<Integer> getLocationOffset(int line, int column) {
return getLocationOffsetImpl(lineOffsets, line, column);
}
/**
* Get the line and column in the source expression text for the given code point {@code
* offset}.
*/
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
return CelSourceHelper.getOffsetLocation(codePoints, offset);
}
@CheckReturnValue
public Map<Long, Integer> getPositionsMap() {
return this.positions;
}
@CheckReturnValue
public Map<Long, CelExpr> getMacroCalls() {
return macroCalls;
}
@CheckReturnValue
public boolean containsMacroCalls(long exprId) {
return this.macroCalls.containsKey(exprId);
}
@CheckReturnValue
public CelSource build() {
return new AutoValue_CelSource(
codePoints,
description,
ImmutableList.copyOf(lineOffsets),
ImmutableMap.copyOf(positions),
ImmutableMap.copyOf(macroCalls),
extensions.build());
}
}
/**
* Tag for an extension that were used while parsing or type checking the source expression. For
* example, optimizations that require special runtime support may be specified. These are used to
* check feature support between components in separate implementations. This can be used to
* either skip redundant work or report an error if the extension is unsupported.
*/
@AutoValue
@Immutable
public abstract static class Extension {
/** Identifier for the extension. Example: constant_folding */
abstract String id();
/**
* Version info. May be skipped if it isn't meaningful for the extension. (for example
* constant_folding might always be v0.0).
*/
abstract Version version();
/**
* If set, the listed components must understand the extension for the expression to evaluate
* correctly.
*/
abstract ImmutableList<Component> affectedComponents();
/** Version of the extension */
@AutoValue
@Immutable
public abstract static class Version {
/**
* Major version changes indicate different required support level from the required
* components.
*/
abstract long major();
/**
* Minor version changes must not change the observed behavior from existing implementations,
* but may be provided informational.
*/
abstract long minor();
/** Create a new instance of Version with the provided major and minor values. */
public static Version of(long major, long minor) {
return new AutoValue_CelSource_Extension_Version(major, minor);
}
}
/** CEL component specifier. */
public enum Component {
/** Unspecified, default. */
COMPONENT_UNSPECIFIED,
/** Parser. Converts a CEL string to an AST. */
COMPONENT_PARSER,
/** Type checker. Checks that references in an AST are defined and types agree. */
COMPONENT_TYPE_CHECKER,
/** Runtime. Evaluates a parsed and optionally checked CEL AST against a context. */
COMPONENT_RUNTIME
}
@CheckReturnValue
public static Extension create(String id, Version version, Iterable<Component> components) {
checkNotNull(version);
checkNotNull(components);
return new AutoValue_CelSource_Extension(id, version, ImmutableList.copyOf(components));
}
@CheckReturnValue
public static Extension create(String id, Version version, Component... components) {
return create(id, version, Arrays.asList(components));
}
}
}