Coverage Report

Created: 2026-09-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/parser/internal/ast_factory.h
Line
Count
Source
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
#ifndef THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_AST_FACTORY_H_
16
#define THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_AST_FACTORY_H_
17
18
#include <cstddef>
19
#include <cstdint>
20
#include <functional>
21
#include <optional>
22
#include <string>
23
#include <utility>
24
25
#include "absl/base/nullability.h"
26
#include "absl/functional/function_ref.h"
27
#include "absl/status/statusor.h"
28
#include "absl/strings/string_view.h"
29
#include "absl/types/span.h"
30
#include "common/expr.h"
31
#include "common/expr_factory.h"
32
#include "parser/internal/ast_factory_interface.h"
33
#include "parser/macro.h"
34
#include "parser/macro_expr_factory.h"
35
#include "parser/macro_registry.h"
36
37
namespace cel::parser_internal {
38
39
// Explicit specialization of `AstFactoryInterface` for `cel::Expr` AST nodes.
40
41
template <>
42
class ListNodeBuilder<cel::Expr> {
43
 public:
44
  explicit ListNodeBuilder(int64_t id);
45
46
  ListNodeBuilder& Add(cel::Expr element, bool optional = false);
47
48
  cel::Expr Build();
49
50
 private:
51
  cel::Expr expr_;
52
};
53
54
template <>
55
class MapNodeBuilder<cel::Expr> {
56
 public:
57
  explicit MapNodeBuilder(int64_t id);
58
59
  MapNodeBuilder& Add(int64_t id, cel::Expr key, cel::Expr value,
60
                      bool optional = false);
61
62
  cel::Expr Build();
63
64
 private:
65
  cel::Expr expr_;
66
};
67
68
template <>
69
class StructNodeBuilder<cel::Expr> {
70
 public:
71
  explicit StructNodeBuilder(int64_t id, std::string name);
72
73
  StructNodeBuilder& Add(int64_t id, std::string name, cel::Expr value,
74
                         bool optional = false);
75
76
  cel::Expr Build();
77
78
 private:
79
  cel::Expr expr_;
80
};
81
82
template <>
83
class AstFactoryInterface<cel::Expr>;
84
85
template <>
86
class MacroExprExpanderSupport<cel::Expr> : public cel::MacroExprFactory {};
87
88
template <>
89
class MacroExprExpander<cel::Expr> {
90
 public:
91
0
  explicit MacroExprExpander(cel::Macro macro) : macro_(std::move(macro)) {}
92
93
  std::optional<cel::Expr> Expand(
94
      std::optional<std::reference_wrapper<cel::Expr>> target,
95
      absl::Span<cel::Expr> args,
96
0
      MacroExprExpanderSupport<cel::Expr>& support) {
97
0
    return macro_.Expand(support, target, args);
98
0
  }
99
100
 private:
101
  cel::Macro macro_;
102
};
103
104
template <>
105
class AstFactoryInterface<cel::Expr> : public cel::ExprFactory {
106
 public:
107
  explicit AstFactoryInterface(
108
      const cel::MacroRegistry* absl_nullable macro_registry = nullptr)
109
0
      : macro_registry_(macro_registry) {}
110
111
  AstFactoryInterface(const AstFactoryInterface&) = delete;
112
  AstFactoryInterface(AstFactoryInterface&&) = delete;
113
  AstFactoryInterface& operator=(const AstFactoryInterface&) = delete;
114
  AstFactoryInterface& operator=(AstFactoryInterface&&) = delete;
115
116
  ~AstFactoryInterface() override = default;
117
118
  // Node inspection and encapsulation API
119
  int64_t GetId(const cel::Expr& expr) const;
120
121
  bool IsEmpty(const cel::Expr& expr) const;
122
123
  bool IsConst(const cel::Expr& expr) const;
124
125
  bool IsIdent(const cel::Expr& expr) const;
126
127
  absl::string_view GetIdentName(const cel::Expr& expr) const;
128
129
  bool IsSelect(const cel::Expr& expr) const;
130
131
  bool IsPresenceTest(const cel::Expr& expr) const;
132
133
  const cel::Expr* GetSelectOperand(const cel::Expr& expr) const;
134
135
  absl::string_view GetSelectField(const cel::Expr& expr) const;
136
137
  absl::StatusOr<cel::Expr> CopyAndReplace(
138
      const cel::Expr& expr,
139
      absl::FunctionRef<std::optional<cel::Expr>(const cel::Expr&)> replacer,
140
      int max_recursion_depth = 1000) const;
141
142
  // Node creation API
143
  using cel::ExprFactory::NewBoolConst;
144
  using cel::ExprFactory::NewBytesConst;
145
  using cel::ExprFactory::NewCall;
146
  using cel::ExprFactory::NewDoubleConst;
147
  using cel::ExprFactory::NewIdent;
148
  using cel::ExprFactory::NewIntConst;
149
  using cel::ExprFactory::NewMemberCall;
150
  using cel::ExprFactory::NewNullConst;
151
  using cel::ExprFactory::NewPresenceTest;
152
  using cel::ExprFactory::NewSelect;
153
  using cel::ExprFactory::NewStringConst;
154
  using cel::ExprFactory::NewUintConst;
155
  using cel::ExprFactory::NewUnspecified;
156
157
  ListNodeBuilder<cel::Expr> NewListBuilder(int64_t id);
158
159
  StructNodeBuilder<cel::Expr> NewStructBuilder(int64_t id, std::string name);
160
161
  MapNodeBuilder<cel::Expr> NewMapBuilder(int64_t id);
162
163
  std::optional<MacroExprExpander<cel::Expr>> NewMacroExprExpander(
164
      std::string_view name, size_t arg_count, bool receiver_style);
165
166
 private:
167
  const cel::MacroRegistry* absl_nullable macro_registry_ = nullptr;
168
};
169
170
using AstFactory = AstFactoryInterface<cel::Expr>;
171
172
}  // namespace cel::parser_internal
173
174
#endif  // THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_AST_FACTORY_H_