-
Notifications
You must be signed in to change notification settings - Fork 0
/
querystring.y
137 lines (126 loc) · 2.29 KB
/
querystring.y
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
132
133
134
135
136
137
%{
package querystring
import (
"github.com/MichealJl/querystring/condition"
)
%}
%union {
s string
n int
q condition.Condition
}
%left tNOT
%left tOR
%left tAND
%token tSTRING tPHRASE tNUMBER
%token tTO tCOLON
%token tLEFTBRACKET tRIGHTBRACKET tLEFTRANGE tRIGHTRANGE
%type <s> tSTRING
%type <s> tPHRASE
%type <s> tNUMBER
%type <q> baseCondition searchRange searchBase searchLogicPart searchParts
%%
// 这里输入 开始
input:
searchParts {
yylex.(*lexerWrapper).query = $1
};
// 这里要递归拼接条件
searchParts:
searchLogicPart {
$$ = $1
};
// 递归这一层
searchLogicPart:
searchBase {
$$ = $1
}
|
tLEFTBRACKET searchLogicPart tRIGHTBRACKET {
$$ = $2
}
|
searchLogicPart tAND searchLogicPart {
$$ = condition.NewAndCondition($1, $3)
}
|
searchLogicPart tOR searchLogicPart {
$$ = condition.NewOrCondition($1, $3)
}
|
tNOT searchBase {
$$ = condition.NewNotCondition(nil, $2)
}
|
searchLogicPart tNOT searchLogicPart {
$$ = condition.NewNotCondition($1, $3)
};
searchBase:
baseCondition {
$$ = $1
}
|
tSTRING tCOLON tLEFTBRACKET searchLogicPart tRIGHTBRACKET {
q := $4
q.SetField($1)
$$ = q
}
|
tSTRING tCOLON searchRange {
q := $3
q.SetField($1)
$$ = q
};
// 范围类型
searchRange:
tLEFTRANGE tSTRING tRIGHTRANGE {
min := $2
$$ = condition.NewRangeCondition(&min, nil)
}
|
tLEFTRANGE tTO tSTRING tRIGHTRANGE {
max := $3
$$ = condition.NewRangeCondition(nil, &max)
}
|
tLEFTRANGE tSTRING tTO tSTRING tRIGHTRANGE {
min := $2
max := $4
$$ = condition.NewRangeCondition(&min, &max)
}
|tLEFTRANGE tNUMBER tRIGHTRANGE {
min := $2
$$ = condition.NewRangeCondition(&min, nil)
}
|
tLEFTRANGE tTO tNUMBER tRIGHTRANGE {
max := $3
$$ = condition.NewRangeCondition(nil, &max)
}
|
tLEFTRANGE tNUMBER tTO tNUMBER tRIGHTRANGE {
min := $2
max := $4
$$ = condition.NewRangeCondition(&min, &max)
};
// 基础类型
baseCondition:
tSTRING {
$$ = condition.NewStringCondition($1)
}
|
tSTRING tSTRING {
$$ = condition.NewOrCondition(condition.NewStringCondition($1), condition.NewStringCondition($2))
}
|
tPHRASE {
$$ = condition.NewPhraseCondition($1)
}
|
tPHRASE tPHRASE {
$$ = condition.NewOrCondition(condition.NewPhraseCondition($1), condition.NewPhraseCondition($2))
}
|
tNUMBER {
$$ = condition.NewNumberCondition($1)
};