Skip to content
Open
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
2 changes: 2 additions & 0 deletions bundle/src/main/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ java_library(
name = "environment",
srcs = [
"CelEnvironment.java",
"TypeSpecifierParser.java",
],
tags = [
],
Expand All @@ -111,6 +112,7 @@ java_library(
"//common:container",
"//common:options",
"//common:source",
"//common/formats:parser_context",
"//common/types",
"//common/types:type_providers",
"//compiler:compiler_builder",
Expand Down
13 changes: 13 additions & 0 deletions bundle/src/main/java/dev/cel/bundle/CelEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,19 @@ public static TypeDecl create(String name) {
return newBuilder().setName(name).build();
}

/**
* Parses a type specifier shorthand string (e.g. {@code "list<int>"}, {@code "map<string,
* dyn>"}, {@code "list<~T>"}) into a {@link TypeDecl}.
*/
public static TypeDecl parse(String typeSpecifier) {
return TypeSpecifierParser.parse(typeSpecifier);
}

/** Creates a new {@link TypeDecl} representing a type parameter with the provided name. */
public static TypeDecl ofTypeParam(String typeParamName) {
return newBuilder().setName(typeParamName).setIsTypeParam(true).build();
}

public static TypeDecl.Builder newBuilder() {
return new AutoValue_CelEnvironment_TypeDecl.Builder().setIsTypeParam(false);
}
Expand Down
75 changes: 61 additions & 14 deletions bundle/src/main/java/dev/cel/bundle/CelEnvironmentYamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import static dev.cel.common.formats.YamlHelper.newString;
import static dev.cel.common.formats.YamlHelper.parseYamlSource;
import static dev.cel.common.formats.YamlHelper.validateYamlType;
import static java.util.Collections.singletonList;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
import dev.cel.bundle.CelEnvironment.Alias;
import dev.cel.bundle.CelEnvironment.ContextVariable;
import dev.cel.bundle.CelEnvironment.ExtensionConfig;
Expand Down Expand Up @@ -60,7 +60,7 @@
*/
public final class CelEnvironmentYamlParser {
// Sentinel values to be returned for various declarations when parsing failure is encountered.
private static final TypeDecl ERROR_TYPE_DECL = TypeDecl.create(ERROR);
static final TypeDecl ERROR_TYPE_DECL = TypeSpecifierParser.ERROR_TYPE_DECL;
private static final VariableDecl ERROR_VARIABLE_DECL =
VariableDecl.create(ERROR, ERROR_TYPE_DECL);
private static final FunctionDecl ERROR_FUNCTION_DECL =
Expand All @@ -71,9 +71,44 @@ public final class CelEnvironmentYamlParser {
private static final Alias ERROR_ALIAS =
Alias.newBuilder().setAlias(ERROR).setQualifiedName(ERROR).build();

/** Generates a new instance of {@code CelEnvironmentYamlParser}. */
private final boolean enableTypeSpecifiers;

/** Generates a new instance of {@code CelEnvironmentYamlParser} with default options. */
public static CelEnvironmentYamlParser newInstance() {
return new CelEnvironmentYamlParser();
return newBuilder().build();
}

/** Creates a new builder to configure and construct a {@link CelEnvironmentYamlParser}. */
public static Builder newBuilder() {
return new Builder();
}

/** Builder for {@link CelEnvironmentYamlParser}. */
public static final class Builder {
private boolean enableTypeSpecifiers = false;

/**
* Configures the parser to allow for shorthand type specifiers (e.g. {@code "list<int>"},
* {@code "map<string, dyn>"}, {@code "list<~T>"}) in addition to structured mapping
* declarations.
*/
@CanIgnoreReturnValue
public Builder enableTypeSpecifiers(boolean enable) {
this.enableTypeSpecifiers = enable;
return this;
}

/** Builds a new instance of {@link CelEnvironmentYamlParser}. */
@CheckReturnValue
public CelEnvironmentYamlParser build() {
return new CelEnvironmentYamlParser(enableTypeSpecifiers);
}

private Builder() {}
}

private CelEnvironmentYamlParser(boolean enableTypeSpecifiers) {
this.enableTypeSpecifiers = enableTypeSpecifiers;
}

/** Parsers the input {@code environmentYamlSource} and returns a {@link CelEnvironment}. */
Expand Down Expand Up @@ -335,6 +370,7 @@ private ContextVariable parseContextVariable(ParserContext<Node> ctx, Node node)
Node valueNode = nodeTuple.getValueNode();
String keyName = ((ScalarNode) keyNode).getValue();
switch (keyName) {
case "type":
case "type_name":
typeName = newString(ctx, valueNode);
break;
Expand Down Expand Up @@ -478,7 +514,7 @@ private FunctionDecl parseFunction(ParserContext<Node> ctx, Node node) {
return builder.build();
}

private static ImmutableSet<OverloadDecl> parseOverloads(ParserContext<Node> ctx, Node node) {
private ImmutableSet<OverloadDecl> parseOverloads(ParserContext<Node> ctx, Node node) {
long listId = ctx.collectMetadata(node);
ImmutableSet.Builder<OverloadDecl> overloadSetBuilder = ImmutableSet.builder();
if (!assertYamlType(ctx, listId, node, YamlNodeType.LIST)) {
Expand Down Expand Up @@ -553,8 +589,7 @@ private static ImmutableList<String> parseOverloadExamples(ParserContext<Node> c
return builder.build();
}

private static ImmutableList<TypeDecl> parseOverloadArguments(
ParserContext<Node> ctx, Node node) {
private ImmutableList<TypeDecl> parseOverloadArguments(ParserContext<Node> ctx, Node node) {
long listValueId = ctx.collectMetadata(node);
if (!assertYamlType(ctx, listValueId, node, YamlNodeType.LIST)) {
return ImmutableList.of();
Expand Down Expand Up @@ -791,7 +826,7 @@ private static ImmutableSet<OverloadSelector> parseFunctionOverloadsSelector(
}

@CanIgnoreReturnValue
private static TypeDecl.Builder parseInlinedTypeDecl(
private TypeDecl.Builder parseInlinedTypeDecl(
ParserContext<Node> ctx, long keyId, Node keyNode, Node valueNode, TypeDecl.Builder builder) {
if (!assertYamlType(ctx, keyId, keyNode, YamlNodeType.STRING, YamlNodeType.TEXT)) {
return builder;
Expand All @@ -800,24 +835,38 @@ private static TypeDecl.Builder parseInlinedTypeDecl(
// Create a synthetic node to make this behave as if a `type: ` parent node actually exists.
MappingNode mapNode =
new MappingNode(
Tag.MAP, /* value= */ singletonList(new NodeTuple(keyNode, valueNode)), FlowStyle.AUTO);
Tag.MAP,
/* value= */ ImmutableList.of(new NodeTuple(keyNode, valueNode)),
FlowStyle.AUTO);

return parseTypeDeclFields(ctx, mapNode, builder);
}

private static TypeDecl parseTypeDecl(ParserContext<Node> ctx, Node node) {
TypeDecl.Builder builder = TypeDecl.newBuilder();
private TypeDecl parseTypeDecl(ParserContext<Node> ctx, Node node) {
long id = ctx.collectMetadata(node);
if (enableTypeSpecifiers) {
if (validateYamlType(node, YamlNodeType.STRING, YamlNodeType.TEXT)) {
return TypeSpecifierParser.parse(ctx, id, newString(ctx, node));
}
if (validateYamlType(node, YamlNodeType.MAP)) {
TypeDecl.Builder builder = TypeDecl.newBuilder();
return parseTypeDeclFields(ctx, (MappingNode) node, builder).build();
}
assertYamlType(ctx, id, node, YamlNodeType.STRING, YamlNodeType.TEXT, YamlNodeType.MAP);
return ERROR_TYPE_DECL;
}

if (!assertYamlType(ctx, id, node, YamlNodeType.MAP)) {
return ERROR_TYPE_DECL;
}

TypeDecl.Builder builder = TypeDecl.newBuilder();
MappingNode mapNode = (MappingNode) node;
return parseTypeDeclFields(ctx, mapNode, builder).build();
}

@CanIgnoreReturnValue
private static TypeDecl.Builder parseTypeDeclFields(
private TypeDecl.Builder parseTypeDeclFields(
ParserContext<Node> ctx, MappingNode mapNode, TypeDecl.Builder builder) {
for (NodeTuple nodeTuple : mapNode.getValue()) {
Node keyNode = nodeTuple.getKeyNode();
Expand Down Expand Up @@ -943,6 +992,4 @@ private CelEnvironment.Builder parseConfig(ParserContext<Node> ctx, Node node) {
return builder;
}
}

private CelEnvironmentYamlParser() {}
}
200 changes: 200 additions & 0 deletions bundle/src/main/java/dev/cel/bundle/TypeSpecifierParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.bundle;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import dev.cel.bundle.CelEnvironment.TypeDecl;
import dev.cel.common.formats.ParserContext;

/**
* Parses a type specifier shorthand string (e.g. {@code "map<string, int>"}, {@code "list<~T>"},
* {@code "int"}) into a {@link TypeDecl}.
*/
final class TypeSpecifierParser {
private static final int MAX_RECURSION_DEPTH = 64;
static final TypeDecl ERROR_TYPE_DECL = TypeDecl.create("*error*");

private final String text;
private final int length;
private int pos;

static TypeDecl parse(String text) {
checkNotNull(text);
TypeSpecifierParser parser = new TypeSpecifierParser(text);
return parser.parse();
}

static TypeDecl parse(ParserContext<?> ctx, long nodeId, String text) {
checkNotNull(ctx);
checkNotNull(text);
try {
return parse(text);
} catch (IllegalArgumentException e) {
ctx.reportError(nodeId, e.getMessage());
return ERROR_TYPE_DECL;
}
}

private TypeDecl parse() {
TypeDecl res = parseTypeElem(0);
skipWhitespace();
if (pos < length) {
throw new IllegalArgumentException(
String.format(
"unexpected character '%c' at position %d in %s",
text.charAt(pos), pos, formatQuoted(text)));
}
return res;
}

private TypeSpecifierParser(String text) {
this.text = text;
this.length = text.length();
this.pos = 0;
}

private TypeDecl parseTypeElem(int depth) {
if (depth > MAX_RECURSION_DEPTH) {
throw new IllegalArgumentException(
String.format("exceeded maximum type specifier recursion depth at position %d", pos));
}
skipWhitespace();
if (pos < length && text.charAt(pos) == '~') {
pos++; // consume '~'
String id = parseTypeParamIdent();
return TypeDecl.ofTypeParam(id);
}
return parseConcreteType(depth);
}

private TypeDecl parseConcreteType(int depth) {
String id = parseNamespaceIdentifier();
skipWhitespace();
if (pos < length && text.charAt(pos) == '<') {
pos++; // consume '<'
ImmutableList.Builder<TypeDecl> params = ImmutableList.builder();
while (true) {
TypeDecl param = parseTypeElem(depth + 1);
params.add(param);
skipWhitespace();
if (pos < length && text.charAt(pos) == ',') {
pos++; // consume ','
continue;
}
if (pos < length && text.charAt(pos) == '>') {
pos++; // consume '>'
break;
}
throw new IllegalArgumentException(
String.format("expected ',' or '>' at position %d", pos));
}
return TypeDecl.newBuilder().setName(id).addParams(params.build()).build();
}
return TypeDecl.create(id);
}

private String parseNamespaceIdentifier() {
StringBuilder id = new StringBuilder();
while (pos < length && text.charAt(pos) != '<') {
char c = text.charAt(pos);
if (c == '.') {
id.append('.');
pos++; // consume '.'
}
String ident = parseIdentifier();
id.append(ident);
if (pos < length && text.charAt(pos) != '.') {
break;
}
}
String identifier = id.toString();
if (identifier.isEmpty()) {
throw new IllegalArgumentException(String.format("missing identifier at position %d", pos));
}
return identifier;
}

private String parseIdentifier() {
if (pos >= length) {
throw new IllegalArgumentException("unexpected end of input");
}
int start = pos;
while (pos < length) {
char c = text.charAt(pos);
boolean isValid = (pos == start) ? (isAlpha(c) || c == '_') : (isAlphaNumeric(c) || c == '_');
if (isValid) {
pos++;
continue;
}
if (pos == start) {
throw new IllegalArgumentException(
String.format("identifier is expected, but '%c' was found at position %d", c, pos));
}
break;
}
return text.substring(start, pos);
}

private String parseTypeParamIdent() {
if (pos >= length) {
throw new IllegalArgumentException("unexpected end of input");
}
char c = text.charAt(pos);
if (c < 'A' || c > 'Z') {
throw new IllegalArgumentException(
String.format(
"invalid type parameter identifier '%c' at position %d, must be a single character"
+ " from A-Z",
c, pos));
}
pos++;
if (pos < length) {
char next = text.charAt(pos);
if (isAlphaNumeric(next) || next == '_') {
throw new IllegalArgumentException(
String.format(
"invalid type parameter identifier '%c' at position %d, must be a single character"
+ " from A-Z",
next, pos));
}
}
return String.valueOf(c);
}

private void skipWhitespace() {
while (pos < length) {
char c = text.charAt(pos);
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
pos++;
} else {
break;
}
}
}

private static boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

private static boolean isAlphaNumeric(char c) {
return isAlpha(c) || (c >= '0' && c <= '9');
}

private static String formatQuoted(String s) {
return "\"" + s + "\"";
}
}
Loading
Loading