| 1 | // Copyright 2018 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 | // http://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 | // Package operators defines the internal function names of operators. |
| 16 | // |
| 17 | // All operators in the expression language are modelled as function calls. |
| 18 | package operators |
| 19 | |
| 20 | // String "names" for CEL operators. |
| 21 | const ( |
| 22 | // Symbolic operators. |
| 23 | Conditional = "_?_:_" |
| 24 | LogicalAnd = "_&&_" |
| 25 | LogicalOr = "_||_" |
| 26 | LogicalNot = "!_" |
| 27 | Equals = "_==_" |
| 28 | NotEquals = "_!=_" |
| 29 | Less = "_<_" |
| 30 | LessEquals = "_<=_" |
| 31 | Greater = "_>_" |
| 32 | GreaterEquals = "_>=_" |
| 33 | Add = "_+_" |
| 34 | Subtract = "_-_" |
| 35 | Multiply = "_*_" |
| 36 | Divide = "_/_" |
| 37 | Modulo = "_%_" |
| 38 | Negate = "-_" |
| 39 | Index = "_[_]" |
| 40 | OptIndex = "_[?_]" |
| 41 | OptSelect = "_?._" |
| 42 | |
| 43 | // Macros, must have a valid identifier. |
| 44 | Has = "has" |
| 45 | All = "all" |
| 46 | Exists = "exists" |
| 47 | ExistsOne = "exists_one" |
| 48 | Map = "map" |
| 49 | Filter = "filter" |
| 50 | |
| 51 | // Named operators, must not have be valid identifiers. |
| 52 | NotStrictlyFalse = "@not_strictly_false" |
| 53 | In = "@in" |
| 54 | |
| 55 | // Deprecated: named operators with valid identifiers. |
| 56 | OldNotStrictlyFalse = "__not_strictly_false__" |
| 57 | OldIn = "_in_" |
| 58 | ) |
| 59 | |
| 60 | var ( |
| 61 | operators = map[string]string{ |
| 62 | "+": Add, |
| 63 | "/": Divide, |
| 64 | "==": Equals, |
| 65 | ">": Greater, |
| 66 | ">=": GreaterEquals, |
| 67 | "in": In, |
| 68 | "<": Less, |
| 69 | "<=": LessEquals, |
| 70 | "%": Modulo, |
| 71 | "*": Multiply, |
| 72 | "!=": NotEquals, |
| 73 | "-": Subtract, |
| 74 | } |
| 75 | // operatorMap of the operator symbol which refers to a struct containing the display name, |
| 76 | // if applicable, the operator precedence, and the arity. |
| 77 | // |
| 78 | // If the symbol does not have a display name listed in the map, it is only because it requires |
| 79 | // special casing to render properly as text. |
| 80 | operatorMap = map[string]struct { |
| 81 | displayName string |
| 82 | precedence int |
| 83 | arity int |
| 84 | }{ |
| 85 | Conditional: {displayName: "", precedence: 8, arity: 3}, |
| 86 | LogicalOr: {displayName: "||", precedence: 7, arity: 2}, |
| 87 | LogicalAnd: {displayName: "&&", precedence: 6, arity: 2}, |
| 88 | Equals: {displayName: "==", precedence: 5, arity: 2}, |
| 89 | Greater: {displayName: ">", precedence: 5, arity: 2}, |
| 90 | GreaterEquals: {displayName: ">=", precedence: 5, arity: 2}, |
| 91 | In: {displayName: "in", precedence: 5, arity: 2}, |
| 92 | Less: {displayName: "<", precedence: 5, arity: 2}, |
| 93 | LessEquals: {displayName: "<=", precedence: 5, arity: 2}, |
| 94 | NotEquals: {displayName: "!=", precedence: 5, arity: 2}, |
| 95 | OldIn: {displayName: "in", precedence: 5, arity: 2}, |
| 96 | Add: {displayName: "+", precedence: 4, arity: 2}, |
| 97 | Subtract: {displayName: "-", precedence: 4, arity: 2}, |
| 98 | Divide: {displayName: "/", precedence: 3, arity: 2}, |
| 99 | Modulo: {displayName: "%", precedence: 3, arity: 2}, |
| 100 | Multiply: {displayName: "*", precedence: 3, arity: 2}, |
| 101 | LogicalNot: {displayName: "!", precedence: 2, arity: 1}, |
| 102 | Negate: {displayName: "-", precedence: 2, arity: 1}, |
| 103 | Index: {displayName: "", precedence: 1, arity: 2}, |
| 104 | OptIndex: {displayName: "", precedence: 1, arity: 2}, |
| 105 | OptSelect: {displayName: "", precedence: 1, arity: 2}, |
| 106 | } |
| 107 | ) |
| 108 | |
| 109 | // Find the internal function name for an operator, if the input text is one. |
| 110 | func Find(text string) (string, bool) { |
| 111 | op, found := operators[text] |
| 112 | return op, found |
| 113 | } |
| 114 | |
| 115 | // FindReverse returns the unmangled, text representation of the operator. |
| 116 | func FindReverse(symbol string) (string, bool) { |
| 117 | op, found := operatorMap[symbol] |
| 118 | if !found { |
| 119 | return "", false |
| 120 | } |
| 121 | return op.displayName, true |
| 122 | } |
| 123 | |
| 124 | // FindReverseBinaryOperator returns the unmangled, text representation of a binary operator. |
| 125 | // |
| 126 | // If the symbol does refer to an operator, but the operator does not have a display name the |
| 127 | // result is false. |
| 128 | func FindReverseBinaryOperator(symbol string) (string, bool) { |
| 129 | op, found := operatorMap[symbol] |
| 130 | if !found || op.arity != 2 { |
| 131 | return "", false |
| 132 | } |
| 133 | if op.displayName == "" { |
| 134 | return "", false |
| 135 | } |
| 136 | return op.displayName, true |
| 137 | } |
| 138 | |
| 139 | // Precedence returns the operator precedence, where the higher the number indicates |
| 140 | // higher precedence operations. |
| 141 | func Precedence(symbol string) int { |
| 142 | op, found := operatorMap[symbol] |
| 143 | if !found { |
| 144 | return 0 |
| 145 | } |
| 146 | return op.precedence |
| 147 | } |
| 148 | |
| 149 | // Arity returns the number of argument the operator takes |
| 150 | // -1 is returned if an undefined symbol is provided |
| 151 | func Arity(symbol string) int { |
| 152 | op, found := operatorMap[symbol] |
| 153 | if !found { |
| 154 | return -1 |
| 155 | } |
| 156 | return op.arity |
| 157 | } |