-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
filter_parser.y
398 lines (358 loc) · 8.2 KB
/
filter_parser.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
%{
package main
import (
"fmt"
"os"
"strconv"
"strings"
"text/scanner"
"time"
)
type Expression interface{}
type Token struct {
token int
literal string
}
type VoidExpr struct {}
type StringExpr struct {
literal string
}
type BoolInfixOpExpr struct {
left Expression
operator rune
right Expression
}
type ProjectExpr struct {
isAll bool
name string
}
type LabelExpr struct {
name string
}
type NotOpExpr struct {
expr Expression
}
const (
DUE_ON int = iota
DUE_BEFORE
DUE_AFTER
NO_DUE_DATE
)
type DateExpr struct {
operation int
datetime time.Time
allDay bool
}
func atoi(a string) (i int) {
i, _ = strconv.Atoi(a)
return
}
var now = time.Now
var today = func() time.Time {
return time.Date(now().Year(), now().Month(), now().Day(), 0, 0, 0, 0, now().Location())
}
var timezone = func() *time.Location {
return now().Location()
}
%}
%union{
token Token
expr Expression
}
%type<expr> filter
%type<expr> expr
%type<expr> s_datetime
%type<expr> s_date
%type<expr> s_date_year
%type<expr> s_overdue s_nodate s_project_key s_project_all_key s_label_key s_no_labels
%type<expr> s_time
%token<token> STRING NUMBER
%token<token> MONTH_IDENT TWELVE_CLOCK_IDENT
%token<token> TODAY_IDENT TOMORROW_IDENT YESTERDAY_IDENT
%token<token> DUE BEFORE AFTER OVER OVERDUE NO DATE LABELS '#' '@'
%left '&' '|'
%%
filter
:
{
$$ = VoidExpr{}
}
| expr
{
$$ = $1
yylex.(*Lexer).result = $$
}
expr
: expr '|' expr
{
$$ = BoolInfixOpExpr{left: $1, operator: '|', right: $3}
}
| expr '&' expr
{
$$ = BoolInfixOpExpr{left: $1, operator: '&', right: $3}
}
| STRING
{
$$ = StringExpr{literal: $1.literal}
}
| s_project_key STRING
{
$$ = ProjectExpr{isAll: false, name: $2.literal}
}
| s_project_all_key STRING
{
$$ = ProjectExpr{isAll: true, name: $2.literal}
}
| s_label_key STRING
{
$$ = LabelExpr{name: $2.literal}
}
| s_no_labels
{
$$ = LabelExpr{name: ""}
}
| '(' expr ')'
{
$$ = $2
}
| '!' expr
{
$$ = NotOpExpr{expr: $2}
}
| s_overdue
{
$$ = DateExpr{allDay: false, datetime: now(), operation: DUE_BEFORE}
}
| s_nodate
{
$$ = DateExpr{operation: NO_DUE_DATE}
}
| DUE BEFORE ':' s_datetime
{
e := $4.(DateExpr)
e.operation = DUE_BEFORE
$$ = e
}
| DUE AFTER ':' s_datetime
{
e := $4.(DateExpr)
e.operation = DUE_AFTER
$$ = e
}
| s_datetime
s_project_all_key
: '#' '#'
{
$$ = $1
}
s_project_key
: '#'
{
$$ = $1
}
s_label_key
: '@'
{
$$ = $1
}
s_no_labels
: NO LABELS
{
$$ = $1
}
s_nodate
: NO DATE
{
$$ = $1
}
| NO DUE DATE
{
$$ = $1
}
s_overdue
: OVER DUE
{
$$ = $1
}
| OVERDUE
{
$$ = $1
}
s_datetime
: s_date_year s_time
{
date := $1.(time.Time)
time := $2.(time.Duration)
$$ = DateExpr{allDay: false, datetime: date.Add(time)}
}
| s_date_year
{
$$ = DateExpr{allDay: true, datetime: $1.(time.Time)}
}
| s_time
{
nd := now().Sub(today())
d := $1.(time.Duration)
if (d <= nd) {
d = d + time.Duration(int64(time.Hour) * 24)
}
$$ = DateExpr{allDay: false, datetime: today().Add(d)}
}
s_date_year
: NUMBER '/' NUMBER '/' NUMBER
{
$$ = time.Date(atoi($5.literal), time.Month(atoi($1.literal)), atoi($3.literal), 0, 0, 0, 0, timezone())
}
| MONTH_IDENT NUMBER NUMBER
{
$$ = time.Date(atoi($3.literal), MonthIdentHash[strings.ToLower($1.literal)], atoi($2.literal), 0, 0, 0, 0, timezone())
}
| NUMBER MONTH_IDENT NUMBER
{
$$ = time.Date(atoi($3.literal), MonthIdentHash[strings.ToLower($2.literal)], atoi($1.literal), 0, 0, 0, 0, timezone())
}
| s_date
{
tod := today()
date := $1.(time.Time)
if date.Before(tod) {
date = date.AddDate(1, 0, 0)
}
$$ = date
}
| TODAY_IDENT
{
$$ = today()
}
| TOMORROW_IDENT
{
$$ = today().AddDate(0, 0, 1)
}
| YESTERDAY_IDENT
{
$$ = today().AddDate(0, 0, -1)
}
s_date
: MONTH_IDENT NUMBER
{
$$ = time.Date(today().Year(), MonthIdentHash[strings.ToLower($1.literal)], atoi($2.literal), 0, 0, 0, 0, timezone())
}
| NUMBER MONTH_IDENT
{
$$ = time.Date(today().Year(), MonthIdentHash[strings.ToLower($2.literal)], atoi($1.literal), 0, 0, 0, 0, timezone())
}
| NUMBER '/' NUMBER
{
$$ = time.Date(now().Year(), time.Month(atoi($3.literal)), atoi($1.literal), 0, 0, 0, 0, timezone())
}
s_time
: NUMBER ':' NUMBER
{
$$ = time.Duration(int64(time.Hour) * int64(atoi($1.literal)) + int64(time.Minute) * int64(atoi($3.literal)))
}
| NUMBER ':' NUMBER ':' NUMBER
{
$$ = time.Duration(int64(time.Hour) * int64(atoi($1.literal)) + int64(time.Minute) * int64(atoi($3.literal)) + int64(time.Second) * int64(atoi($5.literal)))
}
| NUMBER TWELVE_CLOCK_IDENT
{
hour := atoi($1.literal)
if TwelveClockIdentHash[$2.literal] {
hour = hour + 12
}
$$ = time.Duration(int64(time.Hour) * int64(hour))
}
%%
type Lexer struct {
scanner.Scanner
result Expression
}
var MonthIdentHash = map[string]time.Month{
"jan": time.January,
"feb": time.February,
"mar": time.March,
"apr": time.April,
"may": time.May,
"june": time.June,
"july": time.July,
"aug": time.August,
"sept": time.September,
"oct": time.October,
"nov": time.November,
"dec": time.December,
"january": time.January,
"february": time.February,
"march": time.March,
"april": time.April,
"august": time.August,
"september": time.September,
"october": time.October,
"november": time.November,
"december": time.December,
}
var TwelveClockIdentHash = map[string]bool{
"am": false,
"pm": true,
}
var TodayIdentHash = map[string]bool {
"today": true,
"tod": true,
}
var TomorrowIdentHash = map[string]bool {
"tomorrow": true,
"tom": true,
}
var OverDueHash = map[string]bool {
"overdue": true,
"od": true,
}
func (l *Lexer) Lex(lval *yySymType) int {
token := int(l.Scan())
switch token {
case scanner.Ident:
lowerToken := strings.ToLower(l.TokenText())
if _, ok := MonthIdentHash[lowerToken]; ok {
token = MONTH_IDENT
} else if _, ok := TwelveClockIdentHash[lowerToken]; ok {
token = TWELVE_CLOCK_IDENT
} else if _, ok := TodayIdentHash[lowerToken]; ok {
token = TODAY_IDENT
} else if _, ok := TomorrowIdentHash[lowerToken]; ok {
token = TOMORROW_IDENT
} else if lowerToken == "yesterday" {
token = YESTERDAY_IDENT
} else if lowerToken == "due" {
token = DUE
} else if lowerToken == "before" {
token = BEFORE
} else if lowerToken == "after" {
token = AFTER
} else if lowerToken == "over" {
token = OVER
} else if _, ok := OverDueHash[lowerToken]; ok {
token = OVERDUE
} else if lowerToken == "no" {
token = NO
} else if lowerToken == "date" {
token = DATE
} else if lowerToken == "labels" {
token = LABELS
} else {
token = STRING
}
case scanner.Int:
token = NUMBER
}
lval.token = Token{token: token, literal: l.TokenText()}
return token
}
func (l *Lexer) Error(e string) {
fmt.Fprintf(os.Stderr, "Filter error: %s \nFor proper filter syntax see https://support.todoist.com/hc/en-us/articles/205248842-Filters\n", e)
os.Exit(1)
}
func Filter(f string) (e Expression) {
l := new(Lexer)
l.Init(strings.NewReader(f))
yyParse(l)
return l.result
}