-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail-like-filter.php
282 lines (253 loc) · 8.77 KB
/
gmail-like-filter.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/*
* 实现一个类似于gmail filter的过滤器
*
* gmail filter的语法见:https://support.google.com/mail/answer/7190
*/
define('openParen', 0);
define('closeParen', 1);
define('exclude', 2);
define('or_', 3);
define('and_', 4);
define('whitespace', 5);
define('operator', 6);
define('word', 7);
define('quotedWord', 8);
function last($stack, $n=0) {
$size = count($stack);
return ($size - $n) ? $stack[$size - $n - 1] : null;
}
class SyntaxError extends Exception {
public function __construct($col, $message) {
parent::__construct("col: $col, $message");
}
}
class Filter {
public function __construct($str, $default_operator) {
$this->str = $str;
if ($str == '*') return;
$this->default_operator = $default_operator;
$this->rule = $this->parse($str);
}
public function __toString() {
return $this->str;
}
public function match($dict) {
if ($this->str == '*') return True;
return $this->_match($dict, $this->rule);
}
public function _match($dict, $rule) {
if (is_string($rule)) {
if (is_string($dict)) {
return strstr(@$dict, $rule) !== False;
} else {
$op = $this->default_operator;
return strstr(@$dict[$op], $rule) !== False;
}
}
if (is_array($rule)) {
if (isset($rule[or_])) {
$v = False;
foreach ($rule[or_] as $r) {
$v = $this->_match($dict, $r);
if ($v) break;
}
return $v;
} else if (isset($rule[and_])) {
foreach ($rule[and_] as $r) {
if (!$this->_match($dict, $r)) {
return False;
}
}
return True;
} else if (isset($rule[exclude])) {
return !$this->_match($dict, $rule[exclude]);
}
assert(count($rule) == 1);
$operator = key($rule);
return $this->_match(@$dict[$operator], $rule[$operator]);
}
assert(0, "should not be here :(");
}
private function parse($string) {
$tokens = $this->tokenize($string);
$stack = Array();
$valStack = Array();
$valStackTopLevel = Array();
$expectedExpr = False;
foreach ($tokens as $token) {
list($type, $col, $value) = $token;
$canAnd = False;
if ($type == openParen) {
array_push($stack, $token);
array_push($valStackTopLevel, $valStack);
$valStack = Array();
} else if ($type == closeParen) {
while (last($stack) && current(last($stack)) !== openParen) {
$token = array_pop($stack);
array_push($valStack, $this->processOp($token, $valStack));
}
if (!last($stack) || current(last($stack)) !== openParen) {
throw new SyntaxError($col, 'no matching open paren');
}
// get the open paren out
array_pop($stack);
// the current valStack should now have only one element, otherwise
// something went wrong
if (count($valStack) !== 1) {
throw new SyntaxError($col, 'not enough operators in this sub expr');
}
$value = last($valStack);
//$valStack = last($valStackTopLevel);
if (!$valStack) {
throw new SyntaxError(
$col, 'not enought stacks for exprs?'
);
}
$valStack = array_pop($valStackTopLevel);
array_push($valStack, $value);
$canAnd = true;
} else if ($type == whitespace) {
if ($expectedExpr) {
throw new SyntaxError(
$col,
"argument should be followed immediately for operator: $value"
);
}
} else if ($type === or_) {
while (last($stack) &&
$this->priority(last($stack)) >= $this->priority($token)) {
$token = array_pop($stack);
array_push($valStack, $this->translate($token));
}
array_push($stack, $token);
} else if ($type == word) {
array_push($valStack, $value);
$canAnd = True;
$expectedExpr = False;
} else if ($type == quotedWord) {
array_push($valStack, substr($value, 1, strlen($value) - 2));
$canAnd = True;
$expectedExpr = False;
} else {
array_push($stack, $token);
$expectedExpr = $token;
}
if ($canAnd) {
// insert AND between two rules
$token = Array(and_, $col - 1, '');
while (last($stack) &&
$this->priority(last($stack)) >= $this->priority($token)) {
$token1 = array_pop($stack);
array_push($valStack, $this->processOp($token1, $valStack));
}
// check if two items on top of the stack are rules
if (last($valStack) && last($valStack, 1)) {
array_push($stack, $token);
}
}
}
while (count($stack)) {
$token = array_pop($stack);
if ($token[0] === openParen) {
throw new SyntaxError(
$token[1], 'no matching close paren'
);
}
array_push($valStack, $this->processOp($token, $valStack));
}
if (count($valStack) !== 1) {
throw new SyntaxError(0, 'unknown :(');
}
return $valStack[0];
}
private function priority($token) {
$type = $token[0];
if ($type === openParen)
return -1;
else if ($type === and_)
return 1;
else if ($type === or_)
return 2;
else
return 3;
}
private function processOp($token, &$valStack) {
list($type, $col, $value) = $token;
if ($type == or_ || $type == and_) {
$r = array_pop($valStack);
$l = array_pop($valStack);
if (!$r || !$l) {
throw new SyntaxError(
$col,
"missing required arguments for operator $value"
);
}
if ($type === or_) {
if (isset($l[or_]) && is_array($l[or_])) {
$l[or_][] = $r;
return $l;
}
if (isset($r[or_]) && is_array($r[or_])) {
array_unshift($r[or_], $l);
return $r;
}
return Array(or_ => Array($l, $r));
} else {
if (isset($l[and_]) && is_array($l[and_])) {
$l[and_][] = $r;
return $l;
}
if (isset($r[and_]) && is_array($r[and_])) {
array_unshift($r[and_], $l);
return $r;
}
return Array(and_=> Array($l, $r));
}
} else {
$r = array_pop($valStack);
if (!$r) {
throw new SyntaxError(
$col,
"missing the required argument for operator $value"
);
}
if ($type != exclude) {
$type = substr($value, 0, strlen($value)-1);
}
return Array($type => $r);
}
}
private function tokenize($string) {
$tokens = Array();
$col = 0;
while ($col < strlen($string)) {
$token = $this->next_($string, $col);
if ($token === False) {
throw Exception("Unexpected token at col: $col");
}
$tokens[] = $token;
$col += strlen($token[2]);
}
return $tokens;
}
private function next_($string, $i) {
$string = substr($string, $i);
foreach (self::$lexRegexStrings as $n => $regex) {
if (preg_match("/^($regex)/", $string, $m)) {
return Array($n, $i, $m[1]);
}
}
return False;
}
private static $lexRegexStrings = Array(
openParen => '\(',
closeParen => '\)',
exclude => '-',
or_ => 'OR',
whitespace => '\s+',
operator => '[a-zA-Z]+:',
quotedWord => '"[^"]*"',
word => '[^\s()]+',
);
}