|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.common; |
| 16 | + |
| 17 | +import dev.cel.common.ast.CelMutableExpr; |
| 18 | +import dev.cel.common.ast.CelMutableExprConverter; |
| 19 | +import dev.cel.common.ast.CelReference; |
| 20 | +import dev.cel.common.types.CelType; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +/** |
| 26 | + * An abstract representation of CEL Abstract Syntax tree that allows mutation in any of its |
| 27 | + * properties. This class is semantically the same as that of the immutable {@link |
| 28 | + * CelAbstractSyntaxTree}. |
| 29 | + * |
| 30 | + * <p>This should only be used within optimizers to augment an AST. |
| 31 | + */ |
| 32 | +public final class CelMutableAst { |
| 33 | + private final CelMutableExpr mutatedExpr; |
| 34 | + private final CelMutableSource source; |
| 35 | + private final Map<Long, CelReference> references; |
| 36 | + private final Map<Long, CelType> types; |
| 37 | + |
| 38 | + /** Returns the underlying {@link CelMutableExpr} representation of the abstract syntax tree. */ |
| 39 | + public CelMutableExpr expr() { |
| 40 | + return mutatedExpr; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the {@link CelMutableSource} that was used during construction of the abstract syntax |
| 45 | + * tree. |
| 46 | + */ |
| 47 | + public CelMutableSource source() { |
| 48 | + return source; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the resolved reference to a declaration at expression ID for a type-checked AST. |
| 53 | + * |
| 54 | + * @return Optional of {@link CelReference} or {@link Optional#empty} if the reference does not |
| 55 | + * exist at the ID. |
| 56 | + */ |
| 57 | + public Optional<CelReference> getReference(long exprId) { |
| 58 | + return Optional.ofNullable(references.get(exprId)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the type of the expression node for a type-checked AST. |
| 63 | + * |
| 64 | + * @return Optional of {@link CelType} or {@link Optional#empty} if the type does not exist at the |
| 65 | + * ID. |
| 66 | + */ |
| 67 | + public Optional<CelType> getType(long exprId) { |
| 68 | + return Optional.ofNullable(types.get(exprId)); |
| 69 | + } |
| 70 | + |
| 71 | + /** Converts this mutable AST into a parsed {@link CelAbstractSyntaxTree}. */ |
| 72 | + public CelAbstractSyntaxTree toParsedAst() { |
| 73 | + return CelAbstractSyntaxTree.newParsedAst( |
| 74 | + CelMutableExprConverter.fromMutableExpr(mutatedExpr), source.toCelSource()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Constructs an instance of {@link CelMutableAst} with the incoming {@link |
| 79 | + * CelAbstractSyntaxTree}. |
| 80 | + */ |
| 81 | + public static CelMutableAst fromCelAst(CelAbstractSyntaxTree ast) { |
| 82 | + return new CelMutableAst( |
| 83 | + CelMutableExprConverter.fromCelExpr(ast.getExpr()), |
| 84 | + CelMutableSource.fromCelSource(ast.getSource()), |
| 85 | + ast.getReferenceMap(), |
| 86 | + ast.getTypeMap()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Constructs an instance of {@link CelMutableAst} with the mutable expression and its source |
| 91 | + * builder. |
| 92 | + */ |
| 93 | + public static CelMutableAst of(CelMutableExpr mutableExpr, CelMutableSource mutableSource) { |
| 94 | + return new CelMutableAst(mutableExpr, mutableSource); |
| 95 | + } |
| 96 | + |
| 97 | + private CelMutableAst(CelMutableExpr mutatedExpr, CelMutableSource mutableSource) { |
| 98 | + this(mutatedExpr, mutableSource, new HashMap<>(), new HashMap<>()); |
| 99 | + } |
| 100 | + |
| 101 | + private CelMutableAst( |
| 102 | + CelMutableExpr mutatedExpr, |
| 103 | + CelMutableSource mutableSource, |
| 104 | + Map<Long, CelReference> references, |
| 105 | + Map<Long, CelType> types) { |
| 106 | + this.mutatedExpr = mutatedExpr; |
| 107 | + this.source = mutableSource; |
| 108 | + this.references = new HashMap<>(references); |
| 109 | + this.types = new HashMap<>(types); |
| 110 | + } |
| 111 | +} |
0 commit comments