github.com/expr-lang/expr@v1.16.9/parser/operator/operator.go (about)

     1  package operator
     2  
     3  type Associativity int
     4  
     5  const (
     6  	Left Associativity = iota + 1
     7  	Right
     8  )
     9  
    10  type Operator struct {
    11  	Precedence    int
    12  	Associativity Associativity
    13  }
    14  
    15  func Less(a, b string) bool {
    16  	return Binary[a].Precedence < Binary[b].Precedence
    17  }
    18  
    19  func IsBoolean(op string) bool {
    20  	return op == "and" || op == "or" || op == "&&" || op == "||"
    21  }
    22  
    23  func AllowedNegateSuffix(op string) bool {
    24  	switch op {
    25  	case "contains", "matches", "startsWith", "endsWith", "in":
    26  		return true
    27  	default:
    28  		return false
    29  	}
    30  }
    31  
    32  var Unary = map[string]Operator{
    33  	"not": {50, Left},
    34  	"!":   {50, Left},
    35  	"-":   {90, Left},
    36  	"+":   {90, Left},
    37  }
    38  
    39  var Binary = map[string]Operator{
    40  	"|":          {0, Left},
    41  	"or":         {10, Left},
    42  	"||":         {10, Left},
    43  	"and":        {15, Left},
    44  	"&&":         {15, Left},
    45  	"==":         {20, Left},
    46  	"!=":         {20, Left},
    47  	"<":          {20, Left},
    48  	">":          {20, Left},
    49  	">=":         {20, Left},
    50  	"<=":         {20, Left},
    51  	"in":         {20, Left},
    52  	"matches":    {20, Left},
    53  	"contains":   {20, Left},
    54  	"startsWith": {20, Left},
    55  	"endsWith":   {20, Left},
    56  	"..":         {25, Left},
    57  	"+":          {30, Left},
    58  	"-":          {30, Left},
    59  	"*":          {60, Left},
    60  	"/":          {60, Left},
    61  	"%":          {60, Left},
    62  	"**":         {100, Right},
    63  	"^":          {100, Right},
    64  	"??":         {500, Left},
    65  }
    66  
    67  func IsComparison(op string) bool {
    68  	return op == "<" || op == ">" || op == ">=" || op == "<="
    69  }