-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
522 lines (488 loc) · 8.84 KB
/
lexer.go
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package when
import (
"errors"
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type tokenType int
const (
tokenAgo tokenType = iota
tokenBefore
tokenColon
tokenDate
tokenDateSeparator
tokenDigit
tokenEOF
tokenError
tokenFrom
tokenKeyword
tokenMonth
tokenNow
tokenOperatorAdd
tokenOperatorSub
tokenOrdinal
tokenTime
tokenTwelveHour
tokenUnit
tokenWeekday
)
const eof = rune(-1)
type token struct {
typ tokenType
val string
}
func (t token) String() string {
switch t.typ {
case tokenError:
return t.val
case tokenEOF:
return "EOF"
}
return fmt.Sprintf("%q", t.val)
}
type stateFn func(*lexer) stateFn
type lexer struct {
input string
i, j int // position within input
width int // width of last rune
tokens []token
}
func lex(s string) ([]token, error) {
l := &lexer{
input: s,
tokens: make([]token, 0),
}
for state := readExpr; state != nil; {
state = state(l)
}
if len(l.tokens) > 0 {
last := l.tokens[len(l.tokens)-1]
if last.typ == tokenError {
return nil, errors.New(last.val)
}
}
return l.tokens, nil
}
func (l *lexer) emit(typ tokenType) {
v := l.value()
l.emitAs(typ, v)
}
func (l *lexer) emitAs(typ tokenType, value string) {
l.tokens = append(l.tokens, token{typ, value})
l.i = l.j
}
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.tokens = append(l.tokens, token{tokenError, fmt.Sprintf(format, args...)})
return nil
}
func (l *lexer) ignore() {
l.i = l.j
}
func (l *lexer) read() rune {
if l.j >= len(l.input) {
l.width = 0
return eof
}
r, width := utf8.DecodeRuneInString(l.input[l.j:])
l.j += width
l.width = width
return r
}
func (l *lexer) readFn(fn func(rune) bool) {
for {
r := l.read()
if r == eof || !fn(r) {
l.unread()
break
}
}
}
func (l *lexer) readRune(ch rune) bool {
r := l.read()
return r == ch
}
func (l *lexer) readString(s string) bool {
for _, r := range s {
if !l.readRune(r) {
l.unread()
return false
}
}
r := l.peek()
return unicode.IsSpace(r) || r == eof
}
func (l *lexer) peek() rune {
r := l.read()
l.unread()
return r
}
func (l *lexer) peekFn(fn func(rune) bool) string {
s := ""
width := l.width
for {
r := l.read()
if r == eof || !fn(r) {
l.unread()
break
}
s += string(r)
}
for i := range s {
_, width := utf8.DecodeRuneInString(s[i:])
l.j -= width
}
l.width = width
return s
}
func (l *lexer) unread() {
l.j -= l.width
}
func (l *lexer) value() string {
return l.input[l.i:l.j]
}
func readExpr(l *lexer) stateFn {
l.readFn(unicode.IsSpace)
l.ignore()
r := l.peek()
switch {
case r == eof:
return nil
case r == '@':
return readAtSymbol
case r == '+':
l.read()
l.emit(tokenOperatorAdd)
return readExpr
case r == '-':
l.read()
l.emit(tokenOperatorSub)
return readExpr
case unicode.IsDigit(r):
return readDigit
case unicode.IsLetter(r):
return readLetter
}
return l.errorf("invalid character")
}
func readAtSymbol(l *lexer) stateFn {
ok := l.readRune('@')
if !ok {
return l.errorf("invalid character")
}
l.emit(tokenKeyword)
return readExpr
}
func readColon(l *lexer) stateFn {
l.read()
l.emit(tokenColon)
r := l.peek()
if !unicode.IsDigit(r) {
return l.errorf("colon must be followed by a digit")
}
return readDigit
}
func readDigit(l *lexer) stateFn {
l.readFn(unicode.IsDigit)
l.emit(tokenDigit)
r := l.peek()
switch r {
case eof:
return nil
case 's':
return readDurationUnitSecondsOrOrdinal
case 'm', 'h', 'd', 'w', 'M', 'y':
l.read()
l.emit(tokenUnit)
return readDurationNextShort
case 'n', 'r', 't':
return readOrdinal
case 'a', 'p':
return readTwelveHour
case ':':
return readColon
case '-', '/':
l.read()
l.emit(tokenDateSeparator)
return readDigit
}
return readExpr
}
func readDurationNext(l *lexer) stateFn {
r := l.peek()
switch r {
case eof:
return nil
case ',':
fallthrough
case '+':
fallthrough
case '&':
l.read()
l.emit(tokenOperatorAdd)
return readExpr
case '-':
l.read()
l.emit(tokenOperatorSub)
return readExpr
}
if !unicode.IsSpace(r) {
return l.errorf("invalid character")
}
return readDurationSpace
}
func readDurationNextShort(l *lexer) stateFn {
r := l.peek()
switch {
case r == eof:
return nil
case r == ',':
fallthrough
case r == '+':
fallthrough
case r == '&':
l.read()
l.emit(tokenOperatorAdd)
return readExpr
case r == '-':
l.read()
l.emit(tokenOperatorSub)
return readExpr
case unicode.IsSpace(r):
return readDurationSpace
}
l.emit(tokenOperatorAdd)
return readExpr
}
func readDurationSpace(l *lexer) stateFn {
l.readFn(unicode.IsSpace)
r := l.peek()
switch {
case r == eof:
l.ignore()
return nil
case r == '+':
fallthrough
case r == '&':
l.ignore()
l.read()
l.emit(tokenOperatorAdd)
return readExpr
case r == '-':
l.ignore()
l.read()
l.emit(tokenOperatorSub)
return readExpr
case unicode.IsDigit(r):
l.emit(tokenOperatorAdd)
return readExpr
}
return readDurationSpaceNext
}
func readDurationSpaceNext(l *lexer) stateFn {
space := l.value()
l.ignore()
v := l.peekFn(unicode.IsLetter)
v = strings.ToLower(v)
switch v {
case "ago":
l.readString("ago")
l.emit(tokenAgo)
case "before":
l.readString("before")
l.emit(tokenBefore)
case "after":
l.readString("after")
l.emit(tokenFrom)
case "from":
l.readString("from")
l.emit(tokenFrom)
case "and":
l.readString("and")
l.emit(tokenOperatorAdd)
case "one", "a":
fallthrough
case "two":
fallthrough
case "three":
fallthrough
case "four":
fallthrough
case "five":
fallthrough
case "six":
fallthrough
case "seven":
fallthrough
case "eight":
fallthrough
case "nine":
fallthrough
case "ten":
fallthrough
case "eleven":
fallthrough
case "twelve":
l.emitAs(tokenOperatorAdd, space)
}
return readExpr
}
func readDurationUnitSecondsOrOrdinal(l *lexer) stateFn {
l.read()
r := l.peek()
if r == 't' {
l.read()
l.emit(tokenOrdinal)
return readExpr
}
l.emit(tokenUnit)
return readDurationNextShort
}
func readLetter(l *lexer) stateFn {
l.readFn(isTimeRune)
v := l.value()
v = strings.ToLower(v)
switch v {
case "now":
l.emit(tokenNow)
return readExpr
case "today", "tomorrow", "yesterday":
l.emit(tokenDate)
return readExpr
case "midnight", "noon":
l.emit(tokenTime)
return readExpr
case "one", "a":
l.emitAs(tokenDigit, "1")
return readExpr
case "two":
l.emitAs(tokenDigit, "2")
return readExpr
case "three":
l.emitAs(tokenDigit, "3")
return readExpr
case "four":
l.emitAs(tokenDigit, "4")
return readExpr
case "five":
l.emitAs(tokenDigit, "5")
return readExpr
case "six":
l.emitAs(tokenDigit, "6")
return readExpr
case "seven":
l.emitAs(tokenDigit, "7")
return readExpr
case "eight":
l.emitAs(tokenDigit, "8")
return readExpr
case "nine":
l.emitAs(tokenDigit, "9")
return readExpr
case "ten":
l.emitAs(tokenDigit, "10")
return readExpr
case "eleven":
l.emitAs(tokenDigit, "11")
return readExpr
case "twelve":
l.emitAs(tokenDigit, "12")
return readExpr
case "am", "pm":
l.emit(tokenTwelveHour)
return readExpr
case "year", "years":
fallthrough
case "month", "months":
fallthrough
case "week", "weeks":
fallthrough
case "day", "days":
fallthrough
case "hour", "hours":
fallthrough
case "minute", "minutes":
fallthrough
case "second", "seconds":
l.emit(tokenUnit)
return readDurationNext
case "sun", "sunday":
fallthrough
case "mon", "monday":
fallthrough
case "tue", "tuesday":
fallthrough
case "wed", "wednesday":
fallthrough
case "thu", "thursday":
fallthrough
case "fri", "friday":
fallthrough
case "sat", "saturday":
l.emit(tokenWeekday)
return readExpr
case "jan", "january":
fallthrough
case "feb", "february":
fallthrough
case "mar", "march":
fallthrough
case "apr", "april":
fallthrough
case "may":
fallthrough
case "jun", "june":
fallthrough
case "jul", "july":
fallthrough
case "aug", "august":
fallthrough
case "sep", "september":
fallthrough
case "oct", "october":
fallthrough
case "nov", "november":
fallthrough
case "dec", "december":
l.emit(tokenMonth)
return readExpr
case "in", "of", "on", "the", "next", "last", "upcoming":
fallthrough
case "at", "quarter", "half", "past", "to", "after":
fallthrough
case "oclock", "o'clock", "morning", "afternoon", "evening":
l.emit(tokenKeyword)
return readExpr
}
return l.errorf("invalid character")
}
func readOrdinal(l *lexer) stateFn {
var ok bool
r := l.read()
switch r {
case 'n', 'r':
ok = l.readRune('d') // 2nd, 3rd
case 't':
ok = l.readRune('h') // 4th-9th
}
if !ok {
return l.errorf("invalid ordinal")
}
l.emit(tokenOrdinal)
return readExpr
}
func readTwelveHour(l *lexer) stateFn {
if !l.readString("am") && !l.readString("pm") {
return l.errorf("expected twelve hour am/pm marker")
}
l.emit(tokenTwelveHour)
return readExpr
}
func isTimeRune(r rune) bool {
switch r {
case '\'':
return true
}
return unicode.IsLetter(r)
}