-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenizer.go
More file actions
126 lines (103 loc) · 2.23 KB
/
tokenizer.go
File metadata and controls
126 lines (103 loc) · 2.23 KB
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
package dateparser
type Timelex struct {
buffer []byte
idx int
}
func (t *Timelex) All() []*Token {
tokens := []*Token{}
for token := t.Next(); !token.IsEOF() ; token = t.Next() {
tokens = append(tokens, token)
}
return tokens
}
func (t *Timelex) Next() *Token {
token := []byte{}
state := ""
if t.idx >= len(t.buffer) {
return &Token{"", "EOF", 0}
}
L:
for t.idx < len(t.buffer) {
b := t.buffer[t.idx]
switch state {
case "":
// uninitialized state; determine if we're reading a word or a
// number
token = append(token, b)
if IsWord(b) {
state = "a"
} else if IsDigit(b) {
state = "0"
} else if b == ' ' {
token = []byte{} // reset the token
} else {
state = "?"
}
case "?":
break L
case "a":
if !IsWord(b) {
break L
}
token = append(token, b)
case "0":
if !IsDigit(b) {
break L
}
token = append(token, b)
}
t.idx += 1
}
return &Token{string(token), state, 0}
}
func IsWord(b byte) bool {
return (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z')
}
func IsDigit(b byte) bool {
return b >= '0' && b <= '9'
}
type Token struct {
V string
T string
N int
}
func (t *Token) IsEOF() bool {
return t.T == "EOF"
}
func (t *Token) IsNumber() bool {
return t.T == "0"
}
func (t *Token) Number() int {
if t.N > 0 {
return t.N
}
n := 0
for _, b := range t.V {
n *= 10
n += int(b) - 48
}
return n
}
func (t *Token) NumberYear() int {
year := t.Number()
// heuristic taken from python's dateutil.parser
if year >= 100 {
return year
} else if year > 65 {
return year + 1900 // 96 => 1996
} else {
return year + 2000 // 12 => 2012
}
}
func (t *Token) IsLen(vs ...int) bool {
if t.IsEOF() {
return false
}
l := len(t.V)
for _, v := range vs {
if l == v {
return true
}
}
return false
}