github.com/vedadiyan/sqlparser@v1.0.0/pkg/sqlparser/precedence.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sqlparser 18 19 // Precendence is used to know the precedence between operators, 20 // so we can introduce parens when needed in the String representation of the AST 21 type Precendence int 22 23 const ( 24 Syntactic Precendence = iota 25 P1 26 P2 27 P3 28 P4 29 P5 30 P6 31 P7 32 P8 33 P9 34 P10 35 P11 36 P12 37 P13 38 P14 39 P15 40 P16 41 P17 42 ) 43 44 // precedenceFor returns the precedence of an expression. 45 // 46 // - NOTE: If you change anything here, update sql.y to keep them consistent. 47 // Also make sure to add the new constructs to random_expr.go so we have test coverage for the new expressions * 48 func precedenceFor(in Expr) Precendence { 49 switch node := in.(type) { 50 case *OrExpr: 51 return P16 52 case *XorExpr: 53 return P15 54 case *AndExpr: 55 return P14 56 case *NotExpr: 57 return P13 58 case *BetweenExpr: 59 return P12 60 case *ComparisonExpr: 61 switch node.Operator { 62 case EqualOp, NotEqualOp, GreaterThanOp, GreaterEqualOp, LessThanOp, LessEqualOp, LikeOp, InOp, RegexpOp, NullSafeEqualOp: 63 return P11 64 } 65 case *IsExpr: 66 return P11 67 case *BinaryExpr: 68 switch node.Operator { 69 case BitOrOp: 70 return P10 71 case BitAndOp: 72 return P9 73 case ShiftLeftOp, ShiftRightOp: 74 return P8 75 case PlusOp, MinusOp: 76 return P7 77 case DivOp, MultOp, ModOp, IntDivOp: 78 return P6 79 case BitXorOp: 80 return P5 81 } 82 case *UnaryExpr: 83 switch node.Operator { 84 case UPlusOp, UMinusOp: 85 return P4 86 case BangOp: 87 return P3 88 } 89 case *IntervalExpr: 90 return P1 91 case *ExtractedSubquery: 92 return precedenceFor(node.alternative) 93 } 94 95 return Syntactic 96 }