1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| { package parser
type Conditions struct { Group *Conditions `json:"group,omitempty"` Value *Condition `json:"value,omitempty"` Logical string `json:"logical,omitempty"` Next *Conditions `json:"next,omitempty"` }
type Condition struct { Key string `json:"key,omitempty"` Operator string `json:"operator,omitempty"` Value string `json:"value,omitempty"` Values []string `json:"values,omitempty"` }
}
Grammar <- values:Conditions+ EOF { return values.([]interface{})[0].(*Conditions), nil } / SyntaxError
SyntaxError <- . { return nil, errors.New("parser: syntax error") }
ConditionGroup <- '(' __ values: Conditions* __ ')' { if len(values.([]interface{})) == 0 { return nil, nil } return values.([]interface{})[0].(*Conditions), nil }
Conditions <- values:( ( Condition / ConditionGroup ) __ LogicalOperator? __ )+ { head := &Conditions{} cur := head lastIndex := len(values.([]interface{})) - 1 for index, value := range values.([]interface{}) { args := value.([]interface{}) switch arg0 := args[0].(type) { case *Condition: cur.Value = arg0 case *Conditions: cur.Group = arg0 } cur.Logical, _ = args[2].(string) if index == lastIndex { break } cur.Next = &Conditions{} cur = cur.Next } return head, nil }
LogicalOperator <- ( "and" / "or" ) { return string(c.text), nil }
Condition <- key:Identifier __ op:Operator __ value:( Double / Integer / String / List) { ret := &Condition{ Key: key.(string), Operator: op.(string), } if vs, isOK := value.([]string); isOK { ret.Values = vs } if v, isOK := value.(string); isOK { ret.Value = v } return ret, nil }
Integer <- '-'? Digit+ { if _, err := strconv.ParseInt(string(c.text), 10, 64); err != nil { return nil, err } return string(c.text), nil }
Double ← [+-]? Digit+ '.' Digit+ { if _, err := strconv.ParseFloat(string(c.text), 64); err != nil { return nil, err } return string(c.text), nil }
String <- Literal
List <- '(' __ values:(Literal __ ListSeparator? __)* __ ')' { result := make([]string, 0) for _, value := range values.([]interface{}) { args, _ := value.([]interface{}) result = append(result, args[0].(string)) } return result, nil }
Literal <- (('"' (`\"` / [^"])* '"') / ('\'' (`\'` / [^'])* '\'')) { if len(c.text) == 0 { return "", nil } switch c.text[0] { case '\'': return strings.Replace(string(c.text[1:len(c.text)-1]), `\'`, `'`, -1), nil case '"': return strings.Replace(string(c.text[1:len(c.text)-1]), `\"`, `"`, -1), nil } return string(c.text) ,nil }
Operator <- ( "in" / ">=" / "<=" / "!=" / "=" / ">" / "<" ) { return string(c.text), nil }
Identifier <- (Letter / '_')+ (Letter / Digit / '.' / '_' / '[' / ']' )* { return string(c.text), nil }
ListSeparator <- [,] Letter <- [A-Za-z] Digit <- [0-9]
__ <- (_)* _ <- [ \n\t\r]+
EOF <- !.
|