/proc/self/cwd/parser/macro_registry.cc
Line | Count | Source |
1 | | // Copyright 2024 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 | | #include "parser/macro_registry.h" |
16 | | |
17 | | #include <cstddef> |
18 | | #include <utility> |
19 | | #include <vector> |
20 | | |
21 | | #include "absl/status/status.h" |
22 | | #include "absl/strings/match.h" |
23 | | #include "absl/strings/str_cat.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "absl/types/optional.h" |
26 | | #include "absl/types/span.h" |
27 | | #include "parser/macro.h" |
28 | | |
29 | | namespace cel { |
30 | | |
31 | 0 | absl::Status MacroRegistry::RegisterMacro(const Macro& macro) { |
32 | 0 | if (!RegisterMacroImpl(macro)) { |
33 | 0 | return absl::AlreadyExistsError( |
34 | 0 | absl::StrCat("macro already exists: ", macro.key())); |
35 | 0 | } |
36 | 0 | return absl::OkStatus(); |
37 | 0 | } |
38 | | |
39 | 21.4k | absl::Status MacroRegistry::RegisterMacros(absl::Span<const Macro> macros) { |
40 | 171k | for (size_t i = 0; i < macros.size(); ++i) { |
41 | 150k | const auto& macro = macros[i]; |
42 | 150k | if (!RegisterMacroImpl(macro)) { |
43 | 0 | for (size_t j = 0; j < i; ++j) { |
44 | 0 | macros_.erase(macros[j].key()); |
45 | 0 | } |
46 | 0 | return absl::AlreadyExistsError( |
47 | 0 | absl::StrCat("macro already exists: ", macro.key())); |
48 | 0 | } |
49 | 150k | } |
50 | 21.4k | return absl::OkStatus(); |
51 | 21.4k | } |
52 | | |
53 | | absl::optional<Macro> MacroRegistry::FindMacro(absl::string_view name, |
54 | | size_t arg_count, |
55 | 2.33M | bool receiver_style) const { |
56 | | // <function>:<argument_count>:<receiver_style> |
57 | 2.33M | if (name.empty() || absl::StrContains(name, ':')) { |
58 | 0 | return absl::nullopt; |
59 | 0 | } |
60 | | // Try argument count specific key first. |
61 | 2.33M | auto key = absl::StrCat(name, ":", arg_count, ":", |
62 | 2.33M | receiver_style ? "true" : "false"); |
63 | 2.33M | if (auto it = macros_.find(key); it != macros_.end()) { |
64 | 41.4k | return it->second; |
65 | 41.4k | } |
66 | | // Next try variadic. |
67 | 2.28M | key = absl::StrCat(name, ":*:", receiver_style ? "true" : "false"); |
68 | 2.28M | if (auto it = macros_.find(key); it != macros_.end()) { |
69 | 0 | return it->second; |
70 | 0 | } |
71 | 2.28M | return absl::nullopt; |
72 | 2.28M | } |
73 | | |
74 | 0 | std::vector<Macro> MacroRegistry::ListMacros() const { |
75 | 0 | std::vector<Macro> macros; |
76 | 0 | macros.reserve(macros_.size()); |
77 | 0 | for (auto it = macros_.begin(); it != macros_.end(); ++it) { |
78 | 0 | macros.push_back(it->second); |
79 | 0 | } |
80 | 0 | return macros; |
81 | 0 | } |
82 | | |
83 | 150k | bool MacroRegistry::RegisterMacroImpl(const Macro& macro) { |
84 | 150k | return macros_.insert(std::pair{macro.key(), macro}).second; |
85 | 150k | } |
86 | | |
87 | | } // namespace cel |