| 2973 | } |
| 2974 | |
| 2975 | func TestParsePrecedence(t *testing.T) { |
| 2976 | // Precedence levels (highest first): |
| 2977 | // 0: - ~ |
| 2978 | // 1: * / // % |
| 2979 | // 2: + - |
| 2980 | // 3: << >> |
| 2981 | // 4: & |
| 2982 | // 5: ^ |
| 2983 | // 6: | |
| 2984 | // 7: = != > >= < <= |
| 2985 | // 8: NOT |
| 2986 | // 9: AND |
| 2987 | // 10: OR |
| 2988 | |
| 2989 | unary := func(op tree.UnaryOperator, expr tree.Expr) tree.Expr { |
| 2990 | return &tree.UnaryExpr{Operator: op, Expr: expr} |
| 2991 | } |
| 2992 | binary := func(op tree.BinaryOperator, left, right tree.Expr) tree.Expr { |
| 2993 | return &tree.BinaryExpr{Operator: op, Left: left, Right: right} |
| 2994 | } |
| 2995 | cmp := func(op tree.ComparisonOperator, left, right tree.Expr) tree.Expr { |
| 2996 | return &tree.ComparisonExpr{Operator: op, Left: left, Right: right} |
| 2997 | } |
| 2998 | not := func(expr tree.Expr) tree.Expr { |
| 2999 | return &tree.NotExpr{Expr: expr} |
| 3000 | } |
| 3001 | and := func(left, right tree.Expr) tree.Expr { |
| 3002 | return &tree.AndExpr{Left: left, Right: right} |
| 3003 | } |
| 3004 | or := func(left, right tree.Expr) tree.Expr { |
| 3005 | return &tree.OrExpr{Left: left, Right: right} |
| 3006 | } |
| 3007 | concat := func(left, right tree.Expr) tree.Expr { |
| 3008 | return &tree.BinaryExpr{Operator: tree.Concat, Left: left, Right: right} |
| 3009 | } |
| 3010 | regmatch := func(left, right tree.Expr) tree.Expr { |
| 3011 | return &tree.ComparisonExpr{Operator: tree.RegMatch, Left: left, Right: right} |
| 3012 | } |
| 3013 | regimatch := func(left, right tree.Expr) tree.Expr { |
| 3014 | return &tree.ComparisonExpr{Operator: tree.RegIMatch, Left: left, Right: right} |
| 3015 | } |
| 3016 | |
| 3017 | one := tree.NewNumVal(constant.MakeInt64(1), "1", false /* negative */) |
| 3018 | minusone := tree.NewNumVal(constant.MakeInt64(1), "1", true /* negative */) |
| 3019 | two := tree.NewNumVal(constant.MakeInt64(2), "2", false /* negative */) |
| 3020 | minustwo := tree.NewNumVal(constant.MakeInt64(2), "2", true /* negative */) |
| 3021 | three := tree.NewNumVal(constant.MakeInt64(3), "3", false /* negative */) |
| 3022 | a := tree.NewStrVal("a") |
| 3023 | b := tree.NewStrVal("b") |
| 3024 | c := tree.NewStrVal("c") |
| 3025 | |
| 3026 | testData := []struct { |
| 3027 | sql string |
| 3028 | expected tree.Expr |
| 3029 | }{ |
| 3030 | // Unary plus and complement. |
| 3031 | {`~-1`, unary(tree.UnaryComplement, minusone)}, |
| 3032 | {`-~1`, unary(tree.UnaryMinus, unary(tree.UnaryComplement, one))}, |