Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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