forked from jasonmf/wowlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
237 lines (222 loc) · 5.13 KB
/
token.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
package wowlua
import (
"bufio"
"errors"
"fmt"
"strings"
"unicode"
)
const (
StateTokenNone = iota
StateTokenBareHyphen
StateTokenFindNewline
StateTokenString
StateTokenEscapedChar
StateTokenNumber
StateTokenIdentifier
StateTokenInvalid
)
const (
TokenTypeStartTable = iota
TokenTypeEndTable
TokenTypeStartKey
TokenTypeEndKey
TokenTypeEquals
TokenTypeComma
TokenTypeIgnore
TokenTypeString
TokenTypeNumber
TokenTypeIdentifier
)
var (
tokenTypeStrings = map[int]string{
TokenTypeStartTable: "Start Table",
TokenTypeEndTable: "End Table",
TokenTypeStartKey: "Start Key",
TokenTypeEndKey: "End Key",
TokenTypeEquals: "Equals",
TokenTypeComma: "Comma",
TokenTypeIgnore: "Ignore",
TokenTypeString: "String",
TokenTypeNumber: "Number",
TokenTypeIdentifier: "Identifier",
}
tokenStartTable = &Token{Type: TokenTypeStartTable}
tokenEndTable = &Token{Type: TokenTypeEndTable}
tokenStartKey = &Token{Type: TokenTypeStartKey}
tokenEndKey = &Token{Type: TokenTypeEndKey}
tokenEquals = &Token{Type: TokenTypeEquals}
tokenComma = &Token{Type: TokenTypeComma}
)
// A Token is a symbol identified by the tokenizer.
type Token struct {
Type int
Value string
}
// Create a new token.
func NewToken(nType int, v string) *Token {
t := &Token{
Type: nType,
Value: v,
}
return t
}
func (t Token) String() string {
s := tokenTypeStrings[t.Type]
if s == "" {
s = "INVALID TOKEN"
}
if t.Value != "" {
s += " (" + t.Value + ")"
}
return s
}
// A Tokenizer processes a string, yielding Tokens.
type Tokenizer struct {
buffer []rune
state int
scanner *bufio.Scanner
callback func(*Token) error
err error
}
// NewTokenizer creates a new Tokenizer to process the supplied string. It will
// provide each token to the supplied callback function. If the callback
// returns an error, tokenization will stop.
func NewTokenizer(s string, c func(*Token) error) *Tokenizer {
if c == nil {
c = func(tok *Token) error { fmt.Println(*tok); return nil }
}
t := &Tokenizer{
buffer: []rune{},
state: StateTokenNone,
scanner: bufio.NewScanner(strings.NewReader(s)),
callback: c,
}
t.scanner.Split(bufio.ScanRunes)
return t
}
// Add a rune to the buffer.
func (t *Tokenizer) Buffer(r rune) {
t.buffer = append(t.buffer, r)
}
// Create a new token from the buffer of the specified type, Emit() the token,
// then clear the buffer.
func (t *Tokenizer) Send(pType int) {
t.Emit(NewToken(pType, string(t.buffer)))
t.buffer = t.buffer[:0]
}
// If there's been no previous error, send a token to the callback and capture
// any returned error.
func (t *Tokenizer) Emit(tok *Token) {
if t.err != nil {
return
}
t.err = t.callback(tok)
}
// Set the current state. If the specified state is invalid, nothing happens.
func (t *Tokenizer) SetStateToken(state int) {
if state < StateTokenNone || state >= StateTokenInvalid {
return
}
t.state = state
}
// Process in the input stream until it's finished or an error is encountered.
func (t *Tokenizer) Tokenize() error {
for t.scanner.Scan() {
if t.err != nil {
return t.err
}
r := rune(t.scanner.Text()[0])
switch t.state {
case StateTokenNone:
switch {
case r == '{':
t.Emit(tokenStartTable)
case r == '}':
t.Emit(tokenEndTable)
case r == '[':
t.Emit(tokenStartKey)
case r == ']':
t.Emit(tokenEndKey)
case r == '-':
t.Buffer(r)
t.SetStateToken(StateTokenBareHyphen)
case r == '=':
t.Emit(tokenEquals)
case r == '"':
t.SetStateToken(StateTokenString)
case r == ',':
t.Emit(tokenComma)
case unicode.IsDigit(r):
t.Buffer(r)
t.SetStateToken(StateTokenNumber)
case unicode.IsSpace(r):
/* Do Nothing */
case unicode.IsLetter(r):
t.Buffer(r)
t.SetStateToken(StateTokenIdentifier)
default:
return errors.New("FECK None")
}
case StateTokenBareHyphen:
switch {
case r == '-':
t.Send(TokenTypeIgnore)
t.SetStateToken(StateTokenFindNewline)
case unicode.IsDigit(r):
t.Buffer(r)
t.SetStateToken(StateTokenNumber)
default:
return errors.New("FECK")
}
case StateTokenFindNewline:
if r == '\n' {
t.SetStateToken(StateTokenNone)
}
case StateTokenString:
switch r {
case '\\':
t.SetStateToken(StateTokenEscapedChar)
case '"':
t.Send(TokenTypeString)
t.SetStateToken(StateTokenNone)
default:
t.Buffer(r)
}
case StateTokenEscapedChar:
t.Buffer(r)
t.SetStateToken(StateTokenString)
case StateTokenNumber:
switch {
case r == ',':
t.Send(TokenTypeNumber)
t.SetStateToken(StateTokenNone)
t.Emit(tokenComma)
case unicode.IsDigit(r):
t.Buffer(r)
case r == '.':
t.Buffer(r)
case r == ']':
t.Send(TokenTypeNumber)
t.SetStateToken(StateTokenNone)
t.Emit(tokenEndKey)
default:
t.Send(TokenTypeNumber)
t.SetStateToken(StateTokenNone)
}
case StateTokenIdentifier:
switch {
case r == ',':
t.Send(TokenTypeIdentifier)
t.SetStateToken(StateTokenNone)
t.Emit(tokenComma)
case unicode.IsSpace(r):
t.Send(TokenTypeIdentifier)
t.SetStateToken(StateTokenNone)
default:
t.Buffer(r)
}
}
}
return nil
}