Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal Changes #460

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 20 additions & 31 deletions common/src/main/java/dev/cel/common/CelAbstractSyntaxTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dev.cel.common;

import dev.cel.expr.Type;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
Expand All @@ -36,16 +37,17 @@
* <p>Note: Use {@link CelProtoAbstractSyntaxTree} if you need access to the protobuf equivalent
* ASTs, such as ParsedExpr and CheckedExpr from syntax.proto or checked.proto.
*/
@AutoValue
@Immutable
public final class CelAbstractSyntaxTree {
public abstract class CelAbstractSyntaxTree {

private final CelSource celSource;
abstract CelSource celSource();

private final CelExpr celExpr;
abstract CelExpr celExpr();

private final ImmutableMap<Long, CelReference> references;
abstract ImmutableMap<Long, CelReference> references();

private final ImmutableMap<Long, CelType> types;
abstract ImmutableMap<Long, CelType> types();

/**
* Constructs a new instance of CelAbstractSyntaxTree that represent a parsed expression.
Expand All @@ -54,7 +56,8 @@ public final class CelAbstractSyntaxTree {
* validating or optimizing an AST.
*/
public static CelAbstractSyntaxTree newParsedAst(CelExpr celExpr, CelSource celSource) {
return new CelAbstractSyntaxTree(celExpr, celSource);
return new AutoValue_CelAbstractSyntaxTree(
celSource, celExpr, ImmutableMap.of(), ImmutableMap.of());
}

/**
Expand All @@ -69,32 +72,18 @@ public static CelAbstractSyntaxTree newCheckedAst(
CelSource celSource,
Map<Long, CelReference> references,
Map<Long, CelType> types) {
return new CelAbstractSyntaxTree(celExpr, celSource, references, types);
}

private CelAbstractSyntaxTree(CelExpr celExpr, CelSource celSource) {
this(celExpr, celSource, ImmutableMap.of(), ImmutableMap.of());
}

private CelAbstractSyntaxTree(
CelExpr celExpr,
CelSource celSource,
Map<Long, CelReference> references,
Map<Long, CelType> types) {
this.celExpr = celExpr;
this.celSource = celSource;
this.references = ImmutableMap.copyOf(references);
this.types = ImmutableMap.copyOf(types);
return new AutoValue_CelAbstractSyntaxTree(
celSource, celExpr, ImmutableMap.copyOf(references), ImmutableMap.copyOf(types));
}

/** Returns the underlying {@link CelExpr} representation of the abstract syntax tree. */
public CelExpr getExpr() {
return celExpr;
return celExpr();
}

/** Tests whether the underlying abstract syntax tree has been type checked or not. */
public boolean isChecked() {
return !types.isEmpty();
return !types().isEmpty();
}

/**
Expand All @@ -117,23 +106,23 @@ public Type getProtoResultType() {
* Returns the {@link CelSource} that was used during construction of the abstract syntax tree.
*/
public CelSource getSource() {
return celSource;
return celSource();
}

public Optional<CelType> getType(long exprId) {
return Optional.ofNullable(types.get(exprId));
return Optional.ofNullable(types().get(exprId));
}

public ImmutableMap<Long, CelType> getTypeMap() {
return types;
return types();
}

public Optional<CelReference> getReference(long exprId) {
return Optional.ofNullable(references.get(exprId));
return Optional.ofNullable(references().get(exprId));
}

public ImmutableMap<Long, CelReference> getReferenceMap() {
return references;
return references();
}

public CelReference getReferenceOrThrow(long exprId) {
Expand All @@ -142,12 +131,12 @@ public CelReference getReferenceOrThrow(long exprId) {
}

Optional<CelConstant> findEnumValue(long exprId) {
CelReference ref = references.get(exprId);
CelReference ref = references().get(exprId);
return ref != null ? ref.value() : Optional.empty();
}

Optional<ImmutableList<String>> findOverloadIDs(long exprId) {
CelReference ref = references.get(exprId);
CelReference ref = references().get(exprId);
return ref != null && !ref.value().isPresent()
? Optional.of(ref.overloadIds())
: Optional.empty();
Expand Down
75 changes: 36 additions & 39 deletions common/src/main/java/dev/cel/common/CelSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,34 @@

/** Represents the source content of an expression and related metadata. */
@Immutable
public final class CelSource implements Source {

private final CelCodePointArray codePoints;
private final String description;
private final ImmutableList<Integer> lineOffsets;
private final ImmutableMap<Long, Integer> positions;
private final ImmutableMap<Long, CelExpr> macroCalls;
private final ImmutableSet<Extension> extensions;

private CelSource(Builder builder) {
this.codePoints = checkNotNull(builder.codePoints);
this.description = checkNotNull(builder.description);
this.positions = checkNotNull(ImmutableMap.copyOf(builder.positions));
this.lineOffsets = checkNotNull(ImmutableList.copyOf(builder.lineOffsets));
this.macroCalls = checkNotNull(ImmutableMap.copyOf(builder.macroCalls));
this.extensions = checkNotNull(builder.extensions.build());
}
@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;
return codePoints();
}

@Override
public String getDescription() {
return description;
return description();
}

@Override
public ImmutableMap<Long, Integer> getPositionsMap() {
return positions;
return positions();
}

/**
Expand All @@ -76,15 +73,15 @@ public ImmutableMap<Long, Integer> getPositionsMap() {
* <p>NOTE: The indices point to the index just after the '\n' not the index of '\n' itself.
*/
public ImmutableList<Integer> getLineOffsets() {
return lineOffsets;
return lineOffsets();
}

public ImmutableMap<Long, CelExpr> getMacroCalls() {
return macroCalls;
return macroCalls();
}

public ImmutableSet<Extension> getExtensions() {
return extensions;
return extensions();
}

/** See {@link #getLocationOffset(int, int)}. */
Expand All @@ -101,19 +98,19 @@ public Optional<Integer> getLocationOffset(CelSourceLocation location) {
* @param column the column number starting from 0
*/
public Optional<Integer> getLocationOffset(int line, int column) {
return getLocationOffsetImpl(lineOffsets, line, 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);
return CelSourceHelper.getOffsetLocation(codePoints(), offset);
}

@Override
public Optional<String> getSnippet(int line) {
return CelSourceHelper.getSnippet(codePoints, line);
return CelSourceHelper.getSnippet(codePoints(), line);
}

/**
Expand All @@ -136,11 +133,11 @@ private static Optional<Integer> getLocationOffsetImpl(
}

public Builder toBuilder() {
return new Builder(codePoints, lineOffsets)
.setDescription(description)
.addPositionsMap(positions)
.addAllExtensions(extensions)
.addAllMacroCalls(macroCalls);
return new Builder(codePoints(), lineOffsets())
.setDescription(description())
.addPositionsMap(positions())
.addAllExtensions(extensions())
.addAllMacroCalls(macroCalls());
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -236,12 +233,6 @@ public Builder addAllMacroCalls(Map<Long, CelExpr> macroCalls) {
return this;
}

@CanIgnoreReturnValue
public Builder clearMacroCall(long exprId) {
this.macroCalls.remove(exprId);
return this;
}

public ImmutableSet<Extension> getExtensions() {
return extensions.build();
}
Expand Down Expand Up @@ -308,7 +299,13 @@ public boolean containsMacroCalls(long exprId) {

@CheckReturnValue
public CelSource build() {
return new CelSource(this);
return new AutoValue_CelSource(
codePoints,
description,
ImmutableList.copyOf(lineOffsets),
ImmutableMap.copyOf(positions),
ImmutableMap.copyOf(macroCalls),
extensions.build());
}
}

Expand Down Expand Up @@ -369,7 +366,7 @@ public enum Component {
/** 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;
COMPONENT_RUNTIME
}

@CheckReturnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;

import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
Expand All @@ -32,50 +33,40 @@
@Immutable
@VisibleForTesting
@Internal
public final class BasicCodePointArray extends CelCodePointArray {
@AutoValue
@AutoValue.CopyAnnotations
@SuppressWarnings("Immutable") // char[] is not exposed externally, thus cannot be mutated.
public abstract class BasicCodePointArray extends CelCodePointArray {

@SuppressWarnings("Immutable")
private final char[] codePoints;
@SuppressWarnings("AutoValueImmutableFields")
abstract char[] codePoints();

private final int offset;
private final int size;
private final ImmutableList<Integer> lineOffsets;
abstract int offset();

BasicCodePointArray(char[] codePoints, int size, ImmutableList<Integer> lineOffsets) {
this(codePoints, 0, lineOffsets, size);
static BasicCodePointArray create(
char[] codePoints, int size, ImmutableList<Integer> lineOffsets) {
return create(codePoints, 0, lineOffsets, size);
}

BasicCodePointArray(char[] codePoints, int offset, ImmutableList<Integer> lineOffsets, int size) {
this.codePoints = checkNotNull(codePoints);
this.offset = offset;
this.size = size;
this.lineOffsets = lineOffsets;
static BasicCodePointArray create(
char[] codePoints, int offset, ImmutableList<Integer> lineOffsets, int size) {
return new AutoValue_BasicCodePointArray(size, checkNotNull(lineOffsets), codePoints, offset);
}

@Override
public BasicCodePointArray slice(int i, int j) {
checkPositionIndexes(i, j, size());
return new BasicCodePointArray(codePoints, offset + i, lineOffsets, j - i);
return create(codePoints(), offset() + i, lineOffsets(), j - i);
}

@Override
public int get(int index) {
checkElementIndex(index, size());
return codePoints[offset + index] & 0xffff;
return codePoints()[offset() + index] & 0xffff;
}

@Override
public int size() {
return size;
}

@Override
public ImmutableList<Integer> lineOffsets() {
return lineOffsets;
}

@Override
public String toString() {
return new String(codePoints, offset, size);
public final String toString() {
return new String(codePoints(), offset(), size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ public static CelCodePointArray fromString(String text) {
intArray[intIndex++] = codePoint;
}

return new SupplementalCodePointArray(
return SupplementalCodePointArray.create(
intArray, intIndex, lineOffsetContext.buildLineOffsets());
}

return new BasicCodePointArray(charArray, charIndex, lineOffsetContext.buildLineOffsets());
return BasicCodePointArray.create(
charArray, charIndex, lineOffsetContext.buildLineOffsets());
}
int[] intArray = new int[text.length()];
int intIndex = 0;
Expand All @@ -120,11 +121,11 @@ public static CelCodePointArray fromString(String text) {
intArray[intIndex++] = codePoint;
}

return new SupplementalCodePointArray(
return SupplementalCodePointArray.create(
intArray, intIndex, lineOffsetContext.buildLineOffsets());
}

return new Latin1CodePointArray(byteArray, byteIndex, lineOffsetContext.buildLineOffsets());
return Latin1CodePointArray.create(byteArray, byteIndex, lineOffsetContext.buildLineOffsets());
}

private static class LineOffsetContext {
Expand Down
Loading
Loading