diff --git a/common/expr_factory.h b/common/expr_factory.h index dd8e6ed25..c8a9b831f 100644 --- a/common/expr_factory.h +++ b/common/expr_factory.h @@ -179,9 +179,9 @@ class ExprFactory { return expr; } - Expr NewAccuIdent(ExprId id) { - return NewIdent(id, kAccumulatorVariableName); - } + absl::string_view AccuVarName() { return accu_var_; } + + Expr NewAccuIdent(ExprId id) { return NewIdent(id, AccuVarName()); } template ::value>, @@ -356,7 +356,10 @@ class ExprFactory { friend class MacroExprFactory; friend class ParserMacroExprFactory; - ExprFactory() = default; + ExprFactory() : accu_var_(kAccumulatorVariableName) {} + explicit ExprFactory(absl::string_view accu_var) : accu_var_(accu_var) {} + + std::string accu_var_; }; } // namespace cel diff --git a/parser/BUILD b/parser/BUILD index 11cf95f85..aeb65342a 100644 --- a/parser/BUILD +++ b/parser/BUILD @@ -38,6 +38,7 @@ cc_library( "//base/ast_internal:expr", "//common:ast", "//common:constant", + "//common:expr", "//common:expr_factory", "//common:operators", "//common:source", diff --git a/parser/macro.cc b/parser/macro.cc index b11dca5db..db2fef502 100644 --- a/parser/macro.cc +++ b/parser/macro.cc @@ -103,7 +103,7 @@ absl::optional ExpandAllMacro(MacroExprFactory& factory, Expr& target, std::move(args[1])); auto result = factory.NewAccuIdent(); return factory.NewComprehension(args[0].ident_expr().name(), - std::move(target), kAccumulatorVariableName, + std::move(target), factory.AccuVarName(), std::move(init), std::move(condition), std::move(step), std::move(result)); } @@ -136,7 +136,7 @@ absl::optional ExpandExistsMacro(MacroExprFactory& factory, Expr& target, std::move(args[1])); auto result = factory.NewAccuIdent(); return factory.NewComprehension(args[0].ident_expr().name(), - std::move(target), kAccumulatorVariableName, + std::move(target), factory.AccuVarName(), std::move(init), std::move(condition), std::move(step), std::move(result)); } @@ -172,7 +172,7 @@ absl::optional ExpandExistsOneMacro(MacroExprFactory& factory, auto result = factory.NewCall(CelOperator::EQUALS, factory.NewAccuIdent(), factory.NewIntConst(1)); return factory.NewComprehension(args[0].ident_expr().name(), - std::move(target), kAccumulatorVariableName, + std::move(target), factory.AccuVarName(), std::move(init), std::move(condition), std::move(step), std::move(result)); } @@ -204,7 +204,7 @@ absl::optional ExpandMap2Macro(MacroExprFactory& factory, Expr& target, CelOperator::ADD, factory.NewAccuIdent(), factory.NewList(factory.NewListElement(std::move(args[1])))); return factory.NewComprehension(args[0].ident_expr().name(), - std::move(target), kAccumulatorVariableName, + std::move(target), factory.AccuVarName(), std::move(init), std::move(condition), std::move(step), factory.NewAccuIdent()); } @@ -237,7 +237,7 @@ absl::optional ExpandMap3Macro(MacroExprFactory& factory, Expr& target, step = factory.NewCall(CelOperator::CONDITIONAL, std::move(args[1]), std::move(step), factory.NewAccuIdent()); return factory.NewComprehension(args[0].ident_expr().name(), - std::move(target), kAccumulatorVariableName, + std::move(target), factory.AccuVarName(), std::move(init), std::move(condition), std::move(step), factory.NewAccuIdent()); } @@ -272,7 +272,7 @@ absl::optional ExpandFilterMacro(MacroExprFactory& factory, Expr& target, step = factory.NewCall(CelOperator::CONDITIONAL, std::move(args[1]), std::move(step), factory.NewAccuIdent()); return factory.NewComprehension(std::move(name), std::move(target), - kAccumulatorVariableName, std::move(init), + factory.AccuVarName(), std::move(init), std::move(condition), std::move(step), factory.NewAccuIdent()); } diff --git a/parser/macro_expr_factory.h b/parser/macro_expr_factory.h index 291bccdb0..83e322d4e 100644 --- a/parser/macro_expr_factory.h +++ b/parser/macro_expr_factory.h @@ -107,6 +107,8 @@ class MacroExprFactory : protected ExprFactory { return NewIdent(NextId(), std::move(name)); } + absl::string_view AccuVarName() { return ExprFactory::AccuVarName(); } + ABSL_MUST_USE_RESULT Expr NewAccuIdent() { return NewAccuIdent(NextId()); } template