Skip to content
Draft
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
5 changes: 3 additions & 2 deletions packages/cel-spec/src/testdata/to-debug-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
Expr_CreateList,
Expr_CreateStruct,
Expr_Comprehension,
ParsedExpr,
} from "../gen/cel/expr/syntax_pb.ts";

import type { Message } from "@bufbuild/protobuf";
Expand Down Expand Up @@ -56,11 +57,11 @@ const SPECIAL_ESCAPES: Map<number, string> = new Map([
* @private Caution: This functions requires ES2024 features.
*/
export function toDebugString(
expr: Expr,
expr: Expr | ParsedExpr,
adorner: Adorner = EmptyAdorner.singleton,
): string {
const writer = new Writer(adorner);
writer.buffer(expr);
writer.buffer(expr.$typeName == "cel.expr.Expr" ? expr : expr.expr);

return writer.toString();
}
Expand Down
142 changes: 136 additions & 6 deletions packages/cel/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export default class Builder {
return this.newCallExpr(offset, "has", [target]);
}

this.addMacroCall(target.id, "has", undefined, [target]);
target.exprKind.value.testOnly = true;
return target;
}
Expand Down Expand Up @@ -438,60 +439,189 @@ export default class Builder {
call.exprKind.value.target !== undefined &&
varName.exprKind?.case === "identExpr"
) {
let expanded: Expr | undefined;
if (callExpr.args.length === 2) {
switch (callExpr.function) {
case "exists":
return this.expandExistsMacro(
expanded = this.expandExistsMacro(
offset,
call.exprKind.value.target,
varName.exprKind.value.name,
call.exprKind.value.args[1],
);
break;
case "all":
return this.expandAllMacro(
expanded = this.expandAllMacro(
offset,
call.exprKind.value.target,
varName.exprKind.value.name,
call.exprKind.value.args[1],
);
break;
case "map":
return this.expandMapMacro(
expanded = this.expandMapMacro(
offset,
call.exprKind.value.target,
varName.exprKind.value.name,
call.exprKind.value.args[1],
);
break;
case "filter":
return this.expandFilterMacro(
expanded = this.expandFilterMacro(
offset,
call.exprKind.value.target,
varName.exprKind.value.name,
call.exprKind.value.args[1],
);
break;
case "exists_one":
case "existsOne":
return this.expandExistsOne(
expanded = this.expandExistsOne(
offset,
call.exprKind.value.target,
varName.exprKind.value.name,
call.exprKind.value.args[1],
);
break;
}
}
if (callExpr.args.length === 3 && callExpr.function == "map") {
return this.expandMapFilterMacro(
expanded = this.expandMapFilterMacro(
offset,
call.exprKind.value.target,
varName.exprKind.value.name,
call.exprKind.value.args[1],
call.exprKind.value.args[2],
);
}
if (expanded !== undefined) {
this.addMacroCall(
expanded.id,
callExpr.function,
callExpr.target,
callExpr.args,
);
return expanded;
}
}
}
return call;
}

addMacroCall(
exprId: bigint,
functionName: string,
target: Expr | undefined,
args: Expr[],
): void {
const macroArgs = args.map((arg) => this.buildMacroCallArg(arg));
let macroTarget = target;
if (target !== undefined) {
if (this.sourceInfo.macroCalls[target.id.toString()] !== undefined) {
macroTarget = {
$typeName: "cel.expr.Expr",
id: target.id,
exprKind: { case: undefined, value: undefined },
};
} else {
macroTarget = this.buildMacroCallArg(target);
}
}
const macroCall: Expr =
macroTarget !== undefined
? {
$typeName: "cel.expr.Expr",
id: 0n,
exprKind: {
case: "callExpr",
value: {
$typeName: "cel.expr.Expr.Call",
function: functionName,
target: macroTarget,
args: macroArgs,
},
},
}
: {
$typeName: "cel.expr.Expr",
id: 0n,
exprKind: {
case: "callExpr",
value: {
$typeName: "cel.expr.Expr.Call",
function: functionName,
args: macroArgs,
},
},
};
this.sourceInfo.macroCalls[exprId.toString()] = macroCall;
}

buildMacroCallArg(expr: Expr): Expr {
if (this.sourceInfo.macroCalls[expr.id.toString()] !== undefined) {
return {
$typeName: "cel.expr.Expr",
id: expr.id,
exprKind: { case: undefined, value: undefined },
};
}
if (expr.exprKind.case === "callExpr") {
const call = expr.exprKind.value;
const macroArgs = call.args.map((arg) => this.buildMacroCallArg(arg));
const macroTarget =
call.target !== undefined
? this.buildMacroCallArg(call.target)
: undefined;
return {
$typeName: "cel.expr.Expr",
id: expr.id,
exprKind: {
case: "callExpr",
value: {
$typeName: "cel.expr.Expr.Call",
function: call.function,
target: macroTarget,
args: macroArgs,
},
},
};
}
if (expr.exprKind.case === "selectExpr") {
const sel = expr.exprKind.value;
return {
$typeName: "cel.expr.Expr",
id: expr.id,
exprKind: {
case: "selectExpr",
value: {
$typeName: "cel.expr.Expr.Select",
operand: sel.operand
? this.buildMacroCallArg(sel.operand)
: undefined,
field: sel.field,
testOnly: sel.testOnly,
},
},
};
}
if (expr.exprKind.case === "listExpr") {
const list = expr.exprKind.value;
return {
$typeName: "cel.expr.Expr",
id: expr.id,
exprKind: {
case: "listExpr",
value: {
$typeName: "cel.expr.Expr.CreateList",
elements: list.elements.map((el) => this.buildMacroCallArg(el)),
optionalIndices: list.optionalIndices,
},
},
};
}
return expr;
}

newMapEntry(offset: number, key: Expr, value: Expr): Expr_CreateStruct_Entry {
return this.nextEntry(
offset,
Expand Down
11 changes: 9 additions & 2 deletions packages/cel/src/cel.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@
// limitations under the License.

{{
import type { Expr, Expr_CreateStruct_Entry } from "@bufbuild/cel-spec/cel/expr/syntax_pb.js";
import type { Expr, Expr_CreateStruct_Entry, SourceInfo } from "@bufbuild/cel-spec/cel/expr/syntax_pb.js";
import Builder from "./builder.js";
import LogicManager from "./logic-manager.js";
const builder = new Builder();
}}

{
const builder = new Builder();
}

Start
= expr:Expr
{ return { expr, sourceInfo: builder.sourceInfo } }

Expr
= or:ConditionalOr S
tail:TernaryTail?
Expand Down
1 change: 1 addition & 0 deletions packages/cel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ export type {
export { run } from "./run.js";
export { plan } from "./plan.js";
export { parse } from "./parse.js";
export { unparse } from "./unparse.js";
export { type CelEnv, celEnv } from "./env.js";
export { type CelFunc, celMethod, celFunc } from "./func.js";
18 changes: 10 additions & 8 deletions packages/cel/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {
ParsedExprSchema,
type ParsedExpr,
import type {
Expr,
ParsedExpr,
} from "@bufbuild/cel-spec/cel/expr/syntax_pb.js";
import { parse as internalParse } from "./parser.js";
import { create } from "@bufbuild/protobuf";

/**
* Parses a CEL expression string into an abstract syntax tree (AST) or
Expand All @@ -26,8 +25,11 @@ import { create } from "@bufbuild/protobuf";
* This is the first stage of CEL evaluation. The resulting ParsedExpr
* can be passed to plan() for execution planning.
*/
export function parse(expr: string): ParsedExpr {
return create(ParsedExprSchema, {
expr: internalParse(expr),
});
export function parse(expr: string): ParsedExpr & { expr: Expr } {
const result = internalParse(expr);
return {
$typeName: "cel.expr.ParsedExpr",
expr: result.expr,
sourceInfo: result.sourceInfo,
};
}
Loading
Loading