/proc/self/cwd/common/operators.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "common/operators.h" |
2 | | |
3 | | #include <memory> |
4 | | #include <string> |
5 | | |
6 | | namespace google { |
7 | | namespace api { |
8 | | namespace expr { |
9 | | namespace common { |
10 | | |
11 | | namespace { |
12 | | // These functions provide access to reverse mappings for operators. |
13 | | // Functions generally map from text expression to Expr representation, |
14 | | // e.g., from "&&" to "_&&_". Reverse operators provides a mapping from |
15 | | // Expr to textual mapping, e.g., from "_&&_" to "&&". |
16 | | |
17 | 0 | const std::map<std::string, std::string>& UnaryOperators() { |
18 | 0 | static std::shared_ptr<std::map<std::string, std::string>> unaries_map = |
19 | 0 | [&]() { |
20 | 0 | auto u = std::make_shared<std::map<std::string, std::string>>( |
21 | 0 | std::map<std::string, std::string>{ |
22 | 0 | {CelOperator::NEGATE, "-"}, {CelOperator::LOGICAL_NOT, "!"}}); |
23 | 0 | return u; |
24 | 0 | }(); |
25 | 0 | return *unaries_map; |
26 | 0 | } |
27 | | |
28 | 0 | const std::map<std::string, std::string>& BinaryOperators() { |
29 | 0 | static std::shared_ptr<std::map<std::string, std::string>> binops_map = |
30 | 0 | [&]() { |
31 | 0 | auto c = std::make_shared<std::map<std::string, std::string>>( |
32 | 0 | std::map<std::string, std::string>{ |
33 | 0 | {CelOperator::LOGICAL_OR, "||"}, |
34 | 0 | {CelOperator::LOGICAL_AND, "&&"}, |
35 | 0 | {CelOperator::LESS_EQUALS, "<="}, |
36 | 0 | {CelOperator::LESS, "<"}, |
37 | 0 | {CelOperator::GREATER_EQUALS, ">="}, |
38 | 0 | {CelOperator::GREATER, ">"}, |
39 | 0 | {CelOperator::EQUALS, "=="}, |
40 | 0 | {CelOperator::NOT_EQUALS, "!="}, |
41 | 0 | {CelOperator::IN_DEPRECATED, "in"}, |
42 | 0 | {CelOperator::IN, "in"}, |
43 | 0 | {CelOperator::ADD, "+"}, |
44 | 0 | {CelOperator::SUBTRACT, "-"}, |
45 | 0 | {CelOperator::MULTIPLY, "*"}, |
46 | 0 | {CelOperator::DIVIDE, "/"}, |
47 | 0 | {CelOperator::MODULO, "%"}}); |
48 | 0 | return c; |
49 | 0 | }(); |
50 | 0 | return *binops_map; |
51 | 0 | } |
52 | | |
53 | 607k | const std::map<std::string, std::string>& ReverseOperators() { |
54 | 607k | static std::shared_ptr<std::map<std::string, std::string>> operators_map = |
55 | 607k | [&]() { |
56 | 1 | auto c = std::make_shared<std::map<std::string, std::string>>( |
57 | 1 | std::map<std::string, std::string>{ |
58 | 1 | {"+", CelOperator::ADD}, |
59 | 1 | {"-", CelOperator::SUBTRACT}, |
60 | 1 | {"*", CelOperator::MULTIPLY}, |
61 | 1 | {"/", CelOperator::DIVIDE}, |
62 | 1 | {"%", CelOperator::MODULO}, |
63 | 1 | {"==", CelOperator::EQUALS}, |
64 | 1 | {"!=", CelOperator::NOT_EQUALS}, |
65 | 1 | {">", CelOperator::GREATER}, |
66 | 1 | {">=", CelOperator::GREATER_EQUALS}, |
67 | 1 | {"<", CelOperator::LESS}, |
68 | 1 | {"<=", CelOperator::LESS_EQUALS}, |
69 | 1 | {"&&", CelOperator::LOGICAL_AND}, |
70 | 1 | {"!", CelOperator::LOGICAL_NOT}, |
71 | 1 | {"||", CelOperator::LOGICAL_OR}, |
72 | 1 | {"in", CelOperator::IN}, |
73 | 1 | }); |
74 | 1 | return c; |
75 | 1 | }(); |
76 | 607k | return *operators_map; |
77 | 607k | } |
78 | | |
79 | 0 | const std::map<std::string, std::string>& Operators() { |
80 | 0 | static std::shared_ptr<std::map<std::string, std::string>> operators_map = |
81 | 0 | [&]() { |
82 | 0 | auto c = std::make_shared<std::map<std::string, std::string>>( |
83 | 0 | std::map<std::string, std::string>{ |
84 | 0 | {CelOperator::ADD, "+"}, |
85 | 0 | {CelOperator::SUBTRACT, "-"}, |
86 | 0 | {CelOperator::MULTIPLY, "*"}, |
87 | 0 | {CelOperator::DIVIDE, "/"}, |
88 | 0 | {CelOperator::MODULO, "%"}, |
89 | 0 | {CelOperator::EQUALS, "=="}, |
90 | 0 | {CelOperator::NOT_EQUALS, "!="}, |
91 | 0 | {CelOperator::GREATER, ">"}, |
92 | 0 | {CelOperator::GREATER_EQUALS, ">="}, |
93 | 0 | {CelOperator::LESS, "<"}, |
94 | 0 | {CelOperator::LESS_EQUALS, "<="}, |
95 | 0 | {CelOperator::LOGICAL_AND, "&&"}, |
96 | 0 | {CelOperator::LOGICAL_NOT, "!"}, |
97 | 0 | {CelOperator::LOGICAL_OR, "||"}, |
98 | 0 | {CelOperator::IN, "in"}, |
99 | 0 | {CelOperator::IN_DEPRECATED, "in"}, |
100 | 0 | {CelOperator::NEGATE, "-"}}); |
101 | 0 | return c; |
102 | 0 | }(); |
103 | 0 | return *operators_map; |
104 | 0 | } |
105 | | |
106 | | // precedence of the operator, where the higher value means higher. |
107 | 0 | const std::map<std::string, int>& Precedences() { |
108 | 0 | static std::shared_ptr<std::map<std::string, int>> precedence_map = [&]() { |
109 | 0 | auto c = std::make_shared<std::map<std::string, int>>( |
110 | 0 | std::map<std::string, int>{{CelOperator::CONDITIONAL, 8}, |
111 | |
|
112 | 0 | {CelOperator::LOGICAL_OR, 7}, |
113 | |
|
114 | 0 | {CelOperator::LOGICAL_AND, 6}, |
115 | |
|
116 | 0 | {CelOperator::EQUALS, 5}, |
117 | 0 | {CelOperator::GREATER, 5}, |
118 | 0 | {CelOperator::GREATER_EQUALS, 5}, |
119 | 0 | {CelOperator::IN, 5}, |
120 | 0 | {CelOperator::LESS, 5}, |
121 | 0 | {CelOperator::LESS_EQUALS, 5}, |
122 | 0 | {CelOperator::NOT_EQUALS, 5}, |
123 | 0 | {CelOperator::IN_DEPRECATED, 5}, |
124 | |
|
125 | 0 | {CelOperator::ADD, 4}, |
126 | 0 | {CelOperator::SUBTRACT, 4}, |
127 | |
|
128 | 0 | {CelOperator::DIVIDE, 3}, |
129 | 0 | {CelOperator::MODULO, 3}, |
130 | 0 | {CelOperator::MULTIPLY, 3}, |
131 | |
|
132 | 0 | {CelOperator::LOGICAL_NOT, 2}, |
133 | 0 | {CelOperator::NEGATE, 2}, |
134 | |
|
135 | 0 | {CelOperator::INDEX, 1}}); |
136 | 0 | return c; |
137 | 0 | }(); |
138 | 0 | return *precedence_map; |
139 | 0 | } |
140 | | |
141 | | } // namespace |
142 | | |
143 | | const char* CelOperator::CONDITIONAL = "_?_:_"; |
144 | | const char* CelOperator::LOGICAL_AND = "_&&_"; |
145 | | const char* CelOperator::LOGICAL_OR = "_||_"; |
146 | | const char* CelOperator::LOGICAL_NOT = "!_"; |
147 | | const char* CelOperator::IN_DEPRECATED = "_in_"; |
148 | | const char* CelOperator::EQUALS = "_==_"; |
149 | | const char* CelOperator::NOT_EQUALS = "_!=_"; |
150 | | const char* CelOperator::LESS = "_<_"; |
151 | | const char* CelOperator::LESS_EQUALS = "_<=_"; |
152 | | const char* CelOperator::GREATER = "_>_"; |
153 | | const char* CelOperator::GREATER_EQUALS = "_>=_"; |
154 | | const char* CelOperator::ADD = "_+_"; |
155 | | const char* CelOperator::SUBTRACT = "_-_"; |
156 | | const char* CelOperator::MULTIPLY = "_*_"; |
157 | | const char* CelOperator::DIVIDE = "_/_"; |
158 | | const char* CelOperator::MODULO = "_%_"; |
159 | | const char* CelOperator::NEGATE = "-_"; |
160 | | const char* CelOperator::INDEX = "_[_]"; |
161 | | const char* CelOperator::HAS = "has"; |
162 | | const char* CelOperator::ALL = "all"; |
163 | | const char* CelOperator::EXISTS = "exists"; |
164 | | const char* CelOperator::EXISTS_ONE = "exists_one"; |
165 | | const char* CelOperator::MAP = "map"; |
166 | | const char* CelOperator::FILTER = "filter"; |
167 | | const char* CelOperator::NOT_STRICTLY_FALSE = "@not_strictly_false"; |
168 | | const char* CelOperator::IN = "@in"; |
169 | | |
170 | 0 | int LookupPrecedence(const std::string& op) { |
171 | 0 | auto precs = Precedences(); |
172 | 0 | auto p = precs.find(op); |
173 | 0 | if (p != precs.end()) { |
174 | 0 | return p->second; |
175 | 0 | } |
176 | 0 | return 0; |
177 | 0 | } |
178 | | |
179 | 0 | absl::optional<std::string> LookupUnaryOperator(const std::string& op) { |
180 | 0 | auto unary_ops = UnaryOperators(); |
181 | 0 | auto o = unary_ops.find(op); |
182 | 0 | if (o == unary_ops.end()) { |
183 | 0 | return absl::optional<std::string>(); |
184 | 0 | } |
185 | 0 | return o->second; |
186 | 0 | } |
187 | | |
188 | 0 | absl::optional<std::string> LookupBinaryOperator(const std::string& op) { |
189 | 0 | auto bin_ops = BinaryOperators(); |
190 | 0 | auto o = bin_ops.find(op); |
191 | 0 | if (o == bin_ops.end()) { |
192 | 0 | return absl::optional<std::string>(); |
193 | 0 | } |
194 | 0 | return o->second; |
195 | 0 | } |
196 | | |
197 | 0 | absl::optional<std::string> LookupOperator(const std::string& op) { |
198 | 0 | auto ops = Operators(); |
199 | 0 | auto o = ops.find(op); |
200 | 0 | if (o == ops.end()) { |
201 | 0 | return absl::optional<std::string>(); |
202 | 0 | } |
203 | 0 | return o->second; |
204 | 0 | } |
205 | | |
206 | 607k | absl::optional<std::string> ReverseLookupOperator(const std::string& op) { |
207 | 607k | auto rev_ops = ReverseOperators(); |
208 | 607k | auto o = rev_ops.find(op); |
209 | 607k | if (o == rev_ops.end()) { |
210 | 0 | return absl::optional<std::string>(); |
211 | 0 | } |
212 | 607k | return o->second; |
213 | 607k | } |
214 | | |
215 | | bool IsOperatorSamePrecedence(const std::string& op, |
216 | 0 | const google::api::expr::v1alpha1::Expr& expr) { |
217 | 0 | if (!expr.has_call_expr()) { |
218 | 0 | return false; |
219 | 0 | } |
220 | 0 | return LookupPrecedence(op) == LookupPrecedence(expr.call_expr().function()); |
221 | 0 | } |
222 | | |
223 | | bool IsOperatorLowerPrecedence(const std::string& op, |
224 | 0 | const google::api::expr::v1alpha1::Expr& expr) { |
225 | 0 | if (!expr.has_call_expr()) { |
226 | 0 | return false; |
227 | 0 | } |
228 | 0 | return LookupPrecedence(op) < LookupPrecedence(expr.call_expr().function()); |
229 | 0 | } |
230 | | |
231 | 0 | bool IsOperatorLeftRecursive(const std::string& op) { |
232 | 0 | return op != CelOperator::LOGICAL_AND && op != CelOperator::LOGICAL_OR; |
233 | 0 | } |
234 | | |
235 | | } // namespace common |
236 | | } // namespace expr |
237 | | } // namespace api |
238 | | } // namespace google |