Skip to content

Commit a313b27

Browse files
jnthntatumcopybara-github
authored andcommitted
Add option for changing the comprehension accumulator variable used by standard macros.
PiperOrigin-RevId: 707964918
1 parent 22b8dd4 commit a313b27

File tree

8 files changed

+305
-17
lines changed

8 files changed

+305
-17
lines changed

common/expr_factory.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ class ExprFactory {
179179
return expr;
180180
}
181181

182-
Expr NewAccuIdent(ExprId id) {
183-
return NewIdent(id, kAccumulatorVariableName);
184-
}
182+
absl::string_view AccuVarName() { return accu_var_; }
183+
184+
Expr NewAccuIdent(ExprId id) { return NewIdent(id, AccuVarName()); }
185185

186186
template <typename Operand, typename Field,
187187
typename = std::enable_if_t<IsExprLike<Operand>::value>,
@@ -356,7 +356,10 @@ class ExprFactory {
356356
friend class MacroExprFactory;
357357
friend class ParserMacroExprFactory;
358358

359-
ExprFactory() = default;
359+
ExprFactory() : accu_var_(kAccumulatorVariableName) {}
360+
explicit ExprFactory(absl::string_view accu_var) : accu_var_(accu_var) {}
361+
362+
std::string accu_var_;
360363
};
361364

362365
} // namespace cel

parser/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cc_library(
3838
"//base/ast_internal:expr",
3939
"//common:ast",
4040
"//common:constant",
41+
"//common:expr",
4142
"//common:expr_factory",
4243
"//common:operators",
4344
"//common:source",

parser/macro.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ absl::optional<Expr> ExpandAllMacro(MacroExprFactory& factory, Expr& target,
103103
std::move(args[1]));
104104
auto result = factory.NewAccuIdent();
105105
return factory.NewComprehension(args[0].ident_expr().name(),
106-
std::move(target), kAccumulatorVariableName,
106+
std::move(target), factory.AccuVarName(),
107107
std::move(init), std::move(condition),
108108
std::move(step), std::move(result));
109109
}
@@ -136,7 +136,7 @@ absl::optional<Expr> ExpandExistsMacro(MacroExprFactory& factory, Expr& target,
136136
std::move(args[1]));
137137
auto result = factory.NewAccuIdent();
138138
return factory.NewComprehension(args[0].ident_expr().name(),
139-
std::move(target), kAccumulatorVariableName,
139+
std::move(target), factory.AccuVarName(),
140140
std::move(init), std::move(condition),
141141
std::move(step), std::move(result));
142142
}
@@ -172,7 +172,7 @@ absl::optional<Expr> ExpandExistsOneMacro(MacroExprFactory& factory,
172172
auto result = factory.NewCall(CelOperator::EQUALS, factory.NewAccuIdent(),
173173
factory.NewIntConst(1));
174174
return factory.NewComprehension(args[0].ident_expr().name(),
175-
std::move(target), kAccumulatorVariableName,
175+
std::move(target), factory.AccuVarName(),
176176
std::move(init), std::move(condition),
177177
std::move(step), std::move(result));
178178
}
@@ -204,7 +204,7 @@ absl::optional<Expr> ExpandMap2Macro(MacroExprFactory& factory, Expr& target,
204204
CelOperator::ADD, factory.NewAccuIdent(),
205205
factory.NewList(factory.NewListElement(std::move(args[1]))));
206206
return factory.NewComprehension(args[0].ident_expr().name(),
207-
std::move(target), kAccumulatorVariableName,
207+
std::move(target), factory.AccuVarName(),
208208
std::move(init), std::move(condition),
209209
std::move(step), factory.NewAccuIdent());
210210
}
@@ -237,7 +237,7 @@ absl::optional<Expr> ExpandMap3Macro(MacroExprFactory& factory, Expr& target,
237237
step = factory.NewCall(CelOperator::CONDITIONAL, std::move(args[1]),
238238
std::move(step), factory.NewAccuIdent());
239239
return factory.NewComprehension(args[0].ident_expr().name(),
240-
std::move(target), kAccumulatorVariableName,
240+
std::move(target), factory.AccuVarName(),
241241
std::move(init), std::move(condition),
242242
std::move(step), factory.NewAccuIdent());
243243
}
@@ -272,7 +272,7 @@ absl::optional<Expr> ExpandFilterMacro(MacroExprFactory& factory, Expr& target,
272272
step = factory.NewCall(CelOperator::CONDITIONAL, std::move(args[1]),
273273
std::move(step), factory.NewAccuIdent());
274274
return factory.NewComprehension(std::move(name), std::move(target),
275-
kAccumulatorVariableName, std::move(init),
275+
factory.AccuVarName(), std::move(init),
276276
std::move(condition), std::move(step),
277277
factory.NewAccuIdent());
278278
}

parser/macro_expr_factory.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class MacroExprFactory : protected ExprFactory {
107107
return NewIdent(NextId(), std::move(name));
108108
}
109109

110+
absl::string_view AccuVarName() { return ExprFactory::AccuVarName(); }
111+
110112
ABSL_MUST_USE_RESULT Expr NewAccuIdent() { return NewAccuIdent(NextId()); }
111113

112114
template <typename Operand, typename Field,
@@ -282,6 +284,7 @@ class MacroExprFactory : protected ExprFactory {
282284
const Expr& expr, absl::string_view message) = 0;
283285

284286
protected:
287+
using ExprFactory::AccuVarName;
285288
using ExprFactory::NewAccuIdent;
286289
using ExprFactory::NewBoolConst;
287290
using ExprFactory::NewBytesConst;
@@ -316,7 +319,8 @@ class MacroExprFactory : protected ExprFactory {
316319
friend class ParserMacroExprFactory;
317320
friend class TestMacroExprFactory;
318321

319-
MacroExprFactory() : ExprFactory() {}
322+
explicit MacroExprFactory(absl::string_view accu_var)
323+
: ExprFactory(accu_var) {}
320324
};
321325

322326
} // namespace cel

parser/macro_expr_factory_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace cel {
2727

2828
class TestMacroExprFactory final : public MacroExprFactory {
2929
public:
30-
TestMacroExprFactory() : MacroExprFactory() {}
30+
TestMacroExprFactory() : MacroExprFactory(kAccumulatorVariableName) {}
3131

3232
ExprId id() const { return id_; }
3333

parser/options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct ParserOptions final {
5050

5151
// Disable standard macros (has, all, exists, exists_one, filter, map).
5252
bool disable_standard_macros = false;
53+
54+
// Enable hidden accumulator variable '@result' for builtin comprehensions.
55+
bool enable_hidden_accumulator_var = false;
5356
};
5457

5558
} // namespace cel

parser/parser.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "base/ast_internal/expr.h"
5656
#include "common/ast.h"
5757
#include "common/constant.h"
58+
#include "common/expr.h"
5859
#include "common/expr_factory.h"
5960
#include "common/operators.h"
6061
#include "common/source.h"
@@ -84,6 +85,8 @@ namespace cel {
8485

8586
namespace {
8687

88+
constexpr const char kHiddenAccumulatorVariableName[] = "@result";
89+
8790
std::any ExprPtrToAny(std::unique_ptr<Expr>&& expr) {
8891
return std::make_any<Expr*>(expr.release());
8992
}
@@ -158,8 +161,9 @@ SourceRange SourceRangeFromParserRuleContext(
158161

159162
class ParserMacroExprFactory final : public MacroExprFactory {
160163
public:
161-
explicit ParserMacroExprFactory(const cel::Source& source)
162-
: MacroExprFactory(), source_(source) {}
164+
explicit ParserMacroExprFactory(const cel::Source& source,
165+
absl::string_view accu_var)
166+
: MacroExprFactory(accu_var), source_(source) {}
163167

164168
void BeginMacro(SourceRange macro_position) {
165169
macro_position_ = macro_position;
@@ -601,6 +605,7 @@ class ParserVisitor final : public CelBaseVisitor,
601605
public antlr4::BaseErrorListener {
602606
public:
603607
ParserVisitor(const cel::Source& source, int max_recursion_depth,
608+
absl::string_view accu_var,
604609
const cel::MacroRegistry& macro_registry,
605610
bool add_macro_calls = false,
606611
bool enable_optional_syntax = false);
@@ -704,11 +709,12 @@ class ParserVisitor final : public CelBaseVisitor,
704709

705710
ParserVisitor::ParserVisitor(const cel::Source& source,
706711
const int max_recursion_depth,
712+
absl::string_view accu_var,
707713
const cel::MacroRegistry& macro_registry,
708714
const bool add_macro_calls,
709715
bool enable_optional_syntax)
710716
: source_(source),
711-
factory_(source_),
717+
factory_(source_, accu_var),
712718
macro_registry_(macro_registry),
713719
recursion_depth_(0),
714720
max_recursion_depth_(max_recursion_depth),
@@ -1617,8 +1623,12 @@ absl::StatusOr<ParseResult> ParseImpl(const cel::Source& source,
16171623
CommonTokenStream tokens(&lexer);
16181624
CelParser parser(&tokens);
16191625
ExprRecursionListener listener(options.max_recursion_depth);
1620-
ParserVisitor visitor(source, options.max_recursion_depth, registry,
1621-
options.add_macro_calls,
1626+
absl::string_view accu_var = cel::kAccumulatorVariableName;
1627+
if (options.enable_hidden_accumulator_var) {
1628+
accu_var = cel::kHiddenAccumulatorVariableName;
1629+
}
1630+
ParserVisitor visitor(source, options.max_recursion_depth, accu_var,
1631+
registry, options.add_macro_calls,
16221632
options.enable_optional_syntax);
16231633

16241634
lexer.removeErrorListeners();

0 commit comments

Comments
 (0)