|
28 | 28 | import dev.cel.common.CelAbstractSyntaxTree; |
29 | 29 | import dev.cel.common.CelMutableAst; |
30 | 30 | import dev.cel.common.CelMutableSource; |
31 | | -import dev.cel.common.ast.CelConstant; |
32 | 31 | import dev.cel.common.ast.CelExpr.ExprKind.Kind; |
33 | 32 | import dev.cel.common.ast.CelExprIdGeneratorFactory; |
34 | 33 | import dev.cel.common.ast.CelExprIdGeneratorFactory.ExprIdGenerator; |
@@ -163,75 +162,6 @@ private CelMutableAst newCallAst( |
163 | 162 | return CelMutableAst.of(newCallExpr, combinedSource); |
164 | 163 | } |
165 | 164 |
|
166 | | - /** |
167 | | - * Generates a new bind macro using the provided initialization and result expression, then |
168 | | - * replaces the subtree using the new bind expr at the designated expr ID. |
169 | | - * |
170 | | - * <p>The bind call takes the format of: {@code cel.bind(varInit, varName, resultExpr)} |
171 | | - * |
172 | | - * @param ast Original AST to mutate. |
173 | | - * @param varName New variable name for the bind macro call. |
174 | | - * @param varInit Initialization expression to bind to the local variable. |
175 | | - * @param resultExpr Result expression |
176 | | - * @param exprIdToReplace Expression ID of the subtree that is getting replaced. |
177 | | - * @param populateMacroSource If true, populates the cel.bind macro source in the AST. |
178 | | - */ |
179 | | - public CelMutableAst replaceSubtreeWithNewBindMacro( |
180 | | - CelMutableAst ast, |
181 | | - String varName, |
182 | | - CelMutableAst varInit, |
183 | | - CelMutableExpr resultExpr, |
184 | | - long exprIdToReplace, |
185 | | - boolean populateMacroSource) { |
186 | | - // Stabilize incoming varInit AST to avoid collision with the main AST |
187 | | - long maxId = getMaxId(ast); |
188 | | - varInit = stabilizeAst(varInit, maxId); |
189 | | - StableIdGenerator stableIdGenerator = CelExprIdGeneratorFactory.newStableIdGenerator(maxId); |
190 | | - CelMutableExpr newBindMacroExpr = |
191 | | - newBindMacroExpr( |
192 | | - varName, varInit.expr(), CelMutableExpr.newInstance(resultExpr), stableIdGenerator); |
193 | | - CelMutableSource celSource = CelMutableSource.newInstance(); |
194 | | - if (populateMacroSource) { |
195 | | - CelMutableExpr newBindMacroSourceExpr = |
196 | | - newBindMacroSourceExpr(newBindMacroExpr, varName, stableIdGenerator); |
197 | | - // In situations where the existing AST already contains a macro call (ex: nested cel.binds), |
198 | | - // its macro source must be normalized to make it consistent with the newly generated bind |
199 | | - // macro. |
200 | | - celSource = combine(ast.source(), varInit.source()); |
201 | | - celSource = |
202 | | - normalizeMacroSource( |
203 | | - celSource, |
204 | | - -1, // Do not replace any of the subexpr in the macro map. |
205 | | - newBindMacroSourceExpr, |
206 | | - stableIdGenerator::renumberId); |
207 | | - celSource.addMacroCalls(newBindMacroExpr.id(), newBindMacroSourceExpr); |
208 | | - } |
209 | | - |
210 | | - CelMutableAst newBindAst = CelMutableAst.of(newBindMacroExpr, celSource); |
211 | | - |
212 | | - return replaceSubtree(ast, newBindAst, exprIdToReplace); |
213 | | - } |
214 | | - |
215 | | - /** |
216 | | - * See {@link #replaceSubtreeWithNewBindMacro(CelMutableAst, String, CelMutableAst, |
217 | | - * CelMutableExpr, long, boolean)}. |
218 | | - */ |
219 | | - public CelMutableAst replaceSubtreeWithNewBindMacro( |
220 | | - CelMutableAst ast, |
221 | | - String varName, |
222 | | - CelMutableExpr varInit, |
223 | | - CelMutableExpr resultExpr, |
224 | | - long exprIdToReplace, |
225 | | - boolean populateMacroSource) { |
226 | | - return replaceSubtreeWithNewBindMacro( |
227 | | - ast, |
228 | | - varName, |
229 | | - CelMutableAst.of(varInit, CelMutableSource.newInstance()), |
230 | | - resultExpr, |
231 | | - exprIdToReplace, |
232 | | - populateMacroSource); |
233 | | - } |
234 | | - |
235 | 165 | /** Renumbers all the expr IDs in the given AST in a consecutive manner starting from 1. */ |
236 | 166 | public CelMutableAst renumberIdsConsecutively(CelMutableAst mutableAst) { |
237 | 167 | StableIdGenerator stableIdGenerator = CelExprIdGeneratorFactory.newStableIdGenerator(0); |
@@ -688,46 +618,6 @@ private CelMutableSource mangleIdentsInMacroSource( |
688 | 618 | return newSource; |
689 | 619 | } |
690 | 620 |
|
691 | | - private CelMutableExpr newBindMacroExpr( |
692 | | - String varName, |
693 | | - CelMutableExpr varInit, |
694 | | - CelMutableExpr resultExpr, |
695 | | - StableIdGenerator stableIdGenerator) { |
696 | | - // Renumber incoming expression IDs in the init and result expression to avoid collision with |
697 | | - // the main AST. Existing IDs are memoized for a macro source sanitization pass at the end |
698 | | - // (e.g: inserting a bind macro to an existing macro expr) |
699 | | - varInit = renumberExprIds(stableIdGenerator::nextExprId, varInit); |
700 | | - resultExpr = renumberExprIds(stableIdGenerator::nextExprId, resultExpr); |
701 | | - |
702 | | - long iterRangeId = stableIdGenerator.nextExprId(); |
703 | | - long loopConditionId = stableIdGenerator.nextExprId(); |
704 | | - long loopStepId = stableIdGenerator.nextExprId(); |
705 | | - long comprehensionId = stableIdGenerator.nextExprId(); |
706 | | - |
707 | | - return CelMutableExpr.ofComprehension( |
708 | | - comprehensionId, |
709 | | - CelMutableComprehension.create( |
710 | | - "#unused", |
711 | | - CelMutableExpr.ofList(iterRangeId, CelMutableList.create()), |
712 | | - varName, |
713 | | - varInit, |
714 | | - CelMutableExpr.ofConstant(loopConditionId, CelConstant.ofValue(false)), |
715 | | - CelMutableExpr.ofIdent(loopStepId, varName), |
716 | | - resultExpr)); |
717 | | - } |
718 | | - |
719 | | - private CelMutableExpr newBindMacroSourceExpr( |
720 | | - CelMutableExpr bindMacroExpr, String varName, StableIdGenerator stableIdGenerator) { |
721 | | - return CelMutableExpr.ofCall( |
722 | | - 0, // Required sentinel value for macro call |
723 | | - CelMutableCall.create( |
724 | | - CelMutableExpr.ofIdent(stableIdGenerator.nextExprId(), "cel"), |
725 | | - "bind", |
726 | | - CelMutableExpr.ofIdent(stableIdGenerator.nextExprId(), varName), |
727 | | - bindMacroExpr.comprehension().accuInit(), |
728 | | - bindMacroExpr.comprehension().result())); |
729 | | - } |
730 | | - |
731 | 621 | private static CelMutableSource combine( |
732 | 622 | CelMutableSource celSource1, CelMutableSource celSource2) { |
733 | 623 | return CelMutableSource.newInstance() |
@@ -920,8 +810,7 @@ private static void unwrapListArgumentsInMacroCallExpr( |
920 | 810 | CelMutableCall existingMacroCall = newMacroCallExpr.call(); |
921 | 811 | CelMutableCall newMacroCall = |
922 | 812 | existingMacroCall.target().isPresent() |
923 | | - ? CelMutableCall.create(existingMacroCall.target().get(), |
924 | | - existingMacroCall.function()) |
| 813 | + ? CelMutableCall.create(existingMacroCall.target().get(), existingMacroCall.function()) |
925 | 814 | : CelMutableCall.create(existingMacroCall.function()); |
926 | 815 | newMacroCall.addArgs( |
927 | 816 | existingMacroCall.args().get(0)); // iter_var is first argument of the call by convention |
|
0 commit comments