/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 | | |
20 | | #include "absl/status/status.h" |
21 | | #include "absl/strings/match.h" |
22 | | #include "absl/strings/str_cat.h" |
23 | | #include "absl/strings/string_view.h" |
24 | | #include "absl/types/optional.h" |
25 | | #include "absl/types/span.h" |
26 | | #include "parser/macro.h" |
27 | | |
28 | | namespace cel { |
29 | | |
30 | 0 | absl::Status MacroRegistry::RegisterMacro(const Macro& macro) { |
31 | 0 | if (!RegisterMacroImpl(macro)) { |
32 | 0 | return absl::AlreadyExistsError( |
33 | 0 | absl::StrCat("macro already exists: ", macro.key())); |
34 | 0 | } |
35 | 0 | return absl::OkStatus(); |
36 | 0 | } |
37 | | |
38 | 4.20k | absl::Status MacroRegistry::RegisterMacros(absl::Span<const Macro> macros) { |
39 | 33.6k | for (size_t i = 0; i < macros.size(); ++i) { |
40 | 29.4k | const auto& macro = macros[i]; |
41 | 29.4k | if (!RegisterMacroImpl(macro)) { |
42 | 0 | for (size_t j = 0; j < i; ++j) { |
43 | 0 | macros_.erase(macros[j].key()); |
44 | 0 | } |
45 | 0 | return absl::AlreadyExistsError( |
46 | 0 | absl::StrCat("macro already exists: ", macro.key())); |
47 | 0 | } |
48 | 29.4k | } |
49 | 4.20k | return absl::OkStatus(); |
50 | 4.20k | } |
51 | | |
52 | | absl::optional<Macro> MacroRegistry::FindMacro(absl::string_view name, |
53 | | size_t arg_count, |
54 | 856k | bool receiver_style) const { |
55 | | // <function>:<argument_count>:<receiver_style> |
56 | 856k | if (name.empty() || absl::StrContains(name, ':')) { |
57 | 0 | return absl::nullopt; |
58 | 0 | } |
59 | | // Try argument count specific key first. |
60 | 856k | auto key = absl::StrCat(name, ":", arg_count, ":", |
61 | 856k | receiver_style ? "true" : "false"); |
62 | 856k | if (auto it = macros_.find(key); it != macros_.end()) { |
63 | 3.48k | return it->second; |
64 | 3.48k | } |
65 | | // Next try variadic. |
66 | 853k | key = absl::StrCat(name, ":*:", receiver_style ? "true" : "false"); |
67 | 853k | if (auto it = macros_.find(key); it != macros_.end()) { |
68 | 0 | return it->second; |
69 | 0 | } |
70 | 853k | return absl::nullopt; |
71 | 853k | } |
72 | | |
73 | 29.4k | bool MacroRegistry::RegisterMacroImpl(const Macro& macro) { |
74 | 29.4k | return macros_.insert(std::pair{macro.key(), macro}).second; |
75 | 29.4k | } |
76 | | |
77 | | } // namespace cel |