MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / ExpandMap2Macro

Function ExpandMap2Macro

parser/macro.cc:190–215  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

188}
189
190absl::optional<Expr> ExpandMap2Macro(MacroExprFactory& factory, Expr& target,
191 absl::Span<Expr> args) {
192 if (args.size() != 2) {
193 return factory.ReportError("map() requires 2 arguments");
194 }
195 if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
196 return factory.ReportErrorAt(
197 args[0], "map() variable name must be a simple identifier");
198 }
199 if (args[0].ident_expr().name() == kAccumulatorVariableName) {
200 return factory.ReportErrorAt(args[1],
201 absl::StrCat("map() variable name cannot be ",
202 kAccumulatorVariableName));
203 }
204 auto init = factory.NewList();
205 auto condition = factory.NewBoolConst(true);
206 auto accu_ref = factory.NewAccuIdent();
207 auto accu_update =
208 factory.NewList(factory.NewListElement(std::move(args[1])));
209 auto step = factory.NewCall(CelOperator::ADD, std::move(accu_ref),
210 std::move(accu_update));
211 return factory.NewComprehension(args[0].ident_expr().name(),
212 std::move(target), factory.AccuVarName(),
213 std::move(init), std::move(condition),
214 std::move(step), factory.NewAccuIdent());
215}
216
217Macro MakeMap2Macro() {
218 auto status_or_macro = Macro::Receiver(CelOperator::MAP, 2, ExpandMap2Macro);

Callers

nothing calls this directly

Calls 13

has_ident_exprMethod · 0.80
NewListElementMethod · 0.80
sizeMethod · 0.45
ReportErrorMethod · 0.45
emptyMethod · 0.45
nameMethod · 0.45
ReportErrorAtMethod · 0.45
NewListMethod · 0.45
NewBoolConstMethod · 0.45
NewAccuIdentMethod · 0.45
NewCallMethod · 0.45
NewComprehensionMethod · 0.45

Tested by

no test coverage detected