|
| 1 | +// Copyright 2026 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.navigation; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | + |
| 19 | +import com.google.errorprone.annotations.CheckReturnValue; |
| 20 | +import dev.cel.common.ast.CelExpr.ExprKind.Kind; |
| 21 | +import dev.cel.common.ast.Expression; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +/** Utility class for common AST navigation and scoping inspections on {@link BaseNavigableExpr}. */ |
| 27 | +@CheckReturnValue |
| 28 | +public final class CelNavigableExprUtil { |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns true if {@code variableName} is in scope and shadowed by an enclosing comprehension |
| 32 | + * above {@code expr}. |
| 33 | + */ |
| 34 | + public static boolean isVariableShadowed(BaseNavigableExpr<?> expr, String variableName) { |
| 35 | + return areVariablesShadowed(expr, Collections.singleton(variableName)); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns true if any of {@code variableNames} is in scope and shadowed by an enclosing |
| 40 | + * comprehension above {@code expr}. |
| 41 | + */ |
| 42 | + public static boolean areVariablesShadowed( |
| 43 | + BaseNavigableExpr<?> expr, Collection<String> variableNames) { |
| 44 | + checkNotNull(expr); |
| 45 | + checkNotNull(variableNames); |
| 46 | + if (variableNames.isEmpty()) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + BaseNavigableExpr<?> curr = expr; |
| 50 | + Optional<? extends BaseNavigableExpr<?>> maybeParent = curr.parent(); |
| 51 | + while (maybeParent.isPresent()) { |
| 52 | + BaseNavigableExpr<?> parent = maybeParent.get(); |
| 53 | + if (parent.getKind() == Kind.COMPREHENSION) { |
| 54 | + Expression.Comprehension<?> comp = parent.expr().comprehension(); |
| 55 | + long currId = curr.id(); |
| 56 | + long iterRangeId = comp.iterRange().id(); |
| 57 | + long accuInitId = comp.accuInit().id(); |
| 58 | + long resultId = comp.result().id(); |
| 59 | + |
| 60 | + if (currId != iterRangeId && currId != accuInitId) { |
| 61 | + if (currId == resultId) { |
| 62 | + if (variableNames.contains(comp.accuVar())) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + } else { |
| 66 | + if (variableNames.contains(comp.iterVar()) |
| 67 | + || variableNames.contains(comp.iterVar2()) |
| 68 | + || variableNames.contains(comp.accuVar())) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + curr = parent; |
| 75 | + maybeParent = parent.parent(); |
| 76 | + } |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns true if {@code expr} is an {@code IDENT} node that references a variable declared by an |
| 82 | + * enclosing comprehension. |
| 83 | + */ |
| 84 | + public static boolean isComprehensionVariable(BaseNavigableExpr<?> expr) { |
| 85 | + checkNotNull(expr); |
| 86 | + return expr.getKind() == Kind.IDENT |
| 87 | + && areVariablesShadowed(expr, Collections.singleton(expr.expr().ident().name())); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns true if {@code expr} or any identifier within {@code expr} references a variable |
| 92 | + * declared by an enclosing comprehension. |
| 93 | + */ |
| 94 | + public static boolean hasComprehensionVariable(BaseNavigableExpr<?> expr) { |
| 95 | + checkNotNull(expr); |
| 96 | + return expr.allNodes() |
| 97 | + .filter(node -> node.getKind() == Kind.IDENT) |
| 98 | + .anyMatch(CelNavigableExprUtil::isComprehensionVariable); |
| 99 | + } |
| 100 | + |
| 101 | + private CelNavigableExprUtil() {} |
| 102 | +} |
0 commit comments