-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.go
456 lines (386 loc) · 7.34 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
package modconfigobj
import (
"bytes"
"fmt"
"io"
"unicode"
)
type itemType int
const (
// ItemError is an invalid token
ItemError itemType = iota
// ItemComment is a comment token
// Note: includes the hash (#)
ItemComment
// ItemKey is a key from a key/value pair
ItemKey
// ItemValue is the value of a key/value pair
//
// Note: token value includes quotes (if those exist)
ItemValue
// ItemSection is a section
//
// unlike unix-style config files, sections may be nested
//
// Note: token value includes brackets
ItemSection
// ItemEOF is the final token returned when the lexer reaches the end of a file
ItemEOF
)
func (i itemType) String() string {
switch i {
case ItemError:
return "Error"
case ItemComment:
return "Comment"
case ItemKey:
return "Keyword"
case ItemValue:
return "Value"
case ItemSection:
return "Section"
case ItemEOF:
return "EOF"
default:
return "DOESNOTEXIST"
}
}
// Token is the representation of lexeme and category. Len and
// Position are also available for applications such as mutating a
// file in-place. Units for Len and Position are bytes.
type Token struct {
TokenType itemType
Position int64
Len int64
Value string
}
func (t Token) String() string {
return fmt.Sprintf("token %s at %d: \"%s\"", t.TokenType, t.Position, t.Value)
}
// Reader is an object that can emit single runes
type Reader interface {
ReadRune() (rune, int, error)
UnreadRune() error
}
// Buffer supports writing runes, emitting strings, and resetting its contents
type Buffer interface {
Truncate(n int)
Len() int
WriteRune(rune) (int, error)
String() string
Reset()
}
// Lexer tokenizes the configobj file
type Lexer struct {
input Reader
tokenValBuffer Buffer
prevRuneSize int
Position int64
start int64
tokenStream chan Token
state stateFn
}
// NewLexer initializes a Lexer for the given input
func NewLexer(input Reader) *Lexer {
return &Lexer{
state: lexGeneric,
input: input,
tokenValBuffer: bytes.NewBuffer(nil),
tokenStream: make(chan Token, 3),
}
}
// NextItem provides the next token from the lexer's stream. It is the
// caller's resposibility to check for a ItemEOF token which signals
// the end of the token stream.
func (l *Lexer) NextItem() Token {
for {
select {
case t := <-l.tokenStream:
return t
default:
l.state = l.state(l)
}
}
}
type stateFn func(*Lexer) stateFn
func lexGeneric(l *Lexer) stateFn {
l.skipWhitespace()
l.resetTokenBuffer()
var r rune
var err error
for {
r, err = l.next()
if err != nil {
l.resetTokenBuffer()
l.emit(ItemEOF)
return nil
}
switch r {
case '[':
l.backup()
return lexSection
case '#':
l.backup()
return lexComment
case '\n':
return lexGeneric
case '=':
l.emit(ItemError)
return lexGeneric
default:
l.backup()
return lexKey
}
}
}
func lexKey(l *Lexer) stateFn {
var r rune
var err error
l.resetTokenBuffer()
for {
r, err = l.next()
if err != nil {
l.emit(ItemError)
l.emit(ItemEOF)
return nil
}
switch r {
case '\n':
l.emit(ItemError)
return lexGeneric
case '=':
if l.Position-int64(l.prevRuneSize) == l.start { // empty key?
l.emit(ItemError)
return lexGeneric
}
l.backup()
l.emit(ItemKey)
l.next()
return lexValue
}
}
}
func lexValue(l *Lexer) stateFn {
l.skipWhitespace()
l.resetTokenBuffer()
var r rune
var err error
for {
r, err = l.next()
if err != nil {
l.emit(ItemValue)
l.emit(ItemEOF)
return nil
}
switch r {
case '"', '\'':
if l.Position-int64(l.prevRuneSize) == l.start {
l.backup()
return lexQuotedValue(r, l)
}
case '\n':
l.backup()
l.emit(ItemValue)
l.next()
return lexGeneric
}
}
}
func lexQuotedValue(quoteRune rune, l *Lexer) stateFn {
var err error
l.resetTokenBuffer()
numQuotes, err := l.takeRunes(quoteRune, 3)
if err != nil {
l.emit(ItemError)
l.emit(ItemEOF)
return nil
}
switch numQuotes {
case 1, 3:
for {
endQuotes, err := l.takeRunes(quoteRune, numQuotes)
if err != nil {
l.emit(ItemError)
l.emit(ItemEOF)
return nil
}
if endQuotes == numQuotes {
l.emit(ItemValue)
return lexGeneric
}
_, err = l.next()
if err != io.EOF {
l.emit(ItemError)
l.emit(ItemEOF)
return nil
}
}
default:
l.emit(ItemError)
return lexGeneric
}
}
func lexSingleQuote(l *Lexer) stateFn {
return lexQuotedValue('\'', l)
}
func lexDoubleQuote(l *Lexer) stateFn {
return lexQuotedValue('"', l)
}
func (l *Lexer) acceptRun(accept rune) (numRunes int, err error) {
var r rune
for {
r, err = l.next()
if err != nil {
return
}
if r != accept {
l.backup()
return
}
numRunes++
}
}
func (l *Lexer) handleUnexpectedEOF(n int) {
if l.Position == l.start {
l.emit(ItemError)
l.Position += int64(n)
l.emit(ItemEOF)
}
}
func lexComment(l *Lexer) stateFn {
var r rune
var n int
var err error
l.start = l.Position
for {
r, n, err = l.input.ReadRune()
if err == io.EOF {
if l.Position != l.start {
l.emit(ItemComment)
}
l.emit(ItemEOF)
return nil
} else if err != nil {
l.emit(ItemError)
panic(err)
}
switch r {
case '\n':
if l.Position != l.start {
l.emit(ItemComment)
}
l.Position += int64(n)
return lexGeneric
default:
l.consumeRune(r, n)
}
}
}
func lexSection(l *Lexer) stateFn {
var r rune
var err error
var sectionDepth int
l.resetTokenBuffer()
sectionDepth, err = l.acceptRun('[')
if sectionDepth == 0 || err != nil {
l.emit(ItemError)
return lexGeneric
}
var endSectionRun int
for {
endSectionRun, err = l.takeRunes(']', sectionDepth)
if err != nil {
l.emit(ItemError)
l.emit(ItemEOF)
return nil
}
if endSectionRun == sectionDepth {
l.emit(ItemSection)
return lexGeneric
}
r, err = l.next()
if err != nil {
l.emit(ItemError)
l.emit(ItemEOF)
return nil
}
if r == '\n' {
l.emit(ItemError)
return lexGeneric
}
}
}
func (l *Lexer) emit(t itemType) {
l.tokenStream <- Token{
TokenType: t,
Position: l.start,
Len: l.Position - l.start,
Value: l.tokenValBuffer.String(),
}
l.resetTokenBuffer()
}
func (l *Lexer) skipWhitespace() {
var r rune
var err error
for {
r, err = l.next()
if err != nil {
if err == io.EOF {
return
}
panic(err)
}
if !unicode.IsSpace(r) {
l.backup()
l.resetTokenBuffer()
return
}
}
}
func (l *Lexer) consumeRune(r rune, n int) {
l.Position += int64(n)
l.tokenValBuffer.WriteRune(r)
}
func (l *Lexer) next() (r rune, err error) {
var size int
r, size, err = l.input.ReadRune()
if err != io.EOF && err != nil {
l.emit(ItemError)
panic(err)
}
l.consumeRune(r, size)
l.prevRuneSize = size
return
}
func (l *Lexer) backup() {
if l.prevRuneSize == 0 {
panic("backup called before a call to next")
}
err := l.input.UnreadRune()
if err != nil {
l.emit(ItemError)
panic(err)
}
l.tokenValBuffer.Truncate(l.tokenValBuffer.Len() - l.prevRuneSize)
l.Position -= int64(l.prevRuneSize)
l.prevRuneSize = 0
}
func (l *Lexer) resetTokenBuffer() {
l.start = l.Position
l.tokenValBuffer.Reset()
}
func (l *Lexer) takeRunes(accept rune, max int) (taken int, err error) {
var r rune
for i := 0; i < max; i++ {
r, err = l.next()
if err != nil {
return
}
if r != accept {
l.backup()
return
}
taken++
}
return
}