-
Notifications
You must be signed in to change notification settings - Fork 6
/
lex.go
295 lines (266 loc) · 5.84 KB
/
lex.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
package ini
import (
"fmt"
"unicode"
"unicode/utf8"
)
// unexpectedCharErr describes an invalid rune given the lexer's current state.
type unexpectedCharErr struct {
got rune
msg string
}
func (e unexpectedCharErr) Error() string {
return fmt.Sprintf("unexpected character: %q, %v", e.got, e.msg)
}
type tokenType int
const (
tokenError tokenType = iota
tokenPropKey
tokenMapKey
tokenAssignment
tokenPropValue
tokenSection
tokenComment
tokenEOF
)
const (
eof = rune(0)
comment = ';'
sectionStart = '['
sectionEnd = ']'
mapKeyStart = '['
mapKeyEnd = ']'
assignment = '='
eol = '\n'
escape = rune(92) // backslash
space = ' '
tab = '\t'
numberSign = '#'
)
type stateFunc func(l *lexer) stateFunc
type token struct {
typ tokenType
val string
}
type lexerOptions struct {
allowMultilineEscapeNewline bool // support escaped newlines
allowMultilineWhitespacePrefix bool // support space-prefixed lines
allowEmptyValues bool // accept empty values as valid
allowNumberSignComments bool // treat lines beginning with the number sign (#) as a comment
}
type lexer struct {
input string // the string being scanned.
start int // start position of current token.
pos int // current position in the input.
width int // width of last rune read.
state stateFunc
tokens chan token
opts lexerOptions
}
func lex(input string) *lexer {
l := &lexer{
input: input,
state: lexLineStart,
tokens: make(chan token, 2),
}
return l
}
// next returns the next rune in the input and advances the position of the lexer
// ahead by the width of the rune.
func (l *lexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
// prev returns the previous rune from the input and moves the position back by
// the rune width.
func (l *lexer) prev() rune {
l.pos -= l.width
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
return r
}
// peek returns the next rune from the input without advancing the position
func (l *lexer) peek() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, _ := utf8.DecodeRuneInString(l.input[l.pos:])
return r
}
// rpeek returns the previous rune from the input without moving the position
func (l *lexer) rpeek() rune {
if l.pos <= 0 {
l.pos = l.width
}
r, _ := utf8.DecodeRuneInString(l.input[l.pos-l.width:])
return r
}
// current returns the value of the current token
func (l *lexer) current() string {
return l.input[l.start:l.pos]
}
// emit emits a token of type t, resetting the start position of the lexer to
// the current position.
func (l *lexer) emit(t tokenType) {
l.tokens <- token{t, l.current()}
l.start = l.pos
}
// ignore resets the start position of the lexer to the current position, but
// does not emit a token.
func (l *lexer) ignore() {
l.start = l.pos
}
// error returns an error in the form of a stateFunc.
func (l *lexer) error(err error) stateFunc {
l.tokens <- token{
tokenError,
err.Error(),
}
return nil
}
// nextToken receives the next token emitted by the lexer.
func (l *lexer) nextToken() token {
for {
select {
case token := <-l.tokens:
return token
default:
l.state = l.state(l)
}
}
}
func lexLineStart(l *lexer) stateFunc {
r := l.next()
switch {
case r == eof:
l.emit(tokenEOF)
return lexLineStart
case r == comment:
return lexComment
case r == numberSign:
if l.opts.allowNumberSignComments {
return lexComment
}
return l.error(&unexpectedCharErr{r, "comments cannot begin with '#'; consider enabling Options.AllowNumberSignComments"})
case r == sectionStart:
return lexSection
case unicode.IsSpace(r):
l.ignore()
return lexLineStart
case unicode.IsLetter(r) || unicode.IsDigit(r):
return lexPropKey
default:
return l.error(&unexpectedCharErr{r, "lines can only begin with '[', ';', or alphanumeric characters"})
}
}
func lexComment(l *lexer) stateFunc {
var r rune
for {
r = l.peek()
if r == eol || r == eof {
break
}
l.next()
}
l.emit(tokenComment)
return lexLineStart
}
func lexSection(l *lexer) stateFunc {
var r rune
l.ignore()
for {
r = l.peek()
if r == eol || r == eof {
return l.error(&unexpectedCharErr{r, "sections must be closed with a ']'"})
}
if r == sectionEnd {
break
}
l.next()
}
l.emit(tokenSection)
l.next()
l.ignore()
return lexLineStart
}
func lexPropKey(l *lexer) stateFunc {
var r rune
for {
r = l.peek()
if r == eol || r == eof {
return l.error(&unexpectedCharErr{r, "a property key must be followed by the assignment character ('=')"})
}
if r == assignment || r == mapKeyStart {
break
}
l.next()
}
l.emit(tokenPropKey)
if r == mapKeyStart {
return lexMapKey
}
return lexAssignment
}
func lexMapKey(l *lexer) stateFunc {
var r rune
l.next()
l.ignore()
for {
r = l.peek()
if r == eol || r == eof {
return l.error(&unexpectedCharErr{r, "subkeys must be closed with a ']'"})
}
if r == mapKeyEnd {
break
}
l.next()
}
l.emit(tokenMapKey)
l.next()
l.ignore()
return lexAssignment
}
func lexAssignment(l *lexer) stateFunc {
r := l.next()
if r != assignment {
panic("lexer: invalid state encountered")
}
l.emit(tokenAssignment)
return lexPropValue
}
func lexPropValue(l *lexer) stateFunc {
var r rune
for {
r = l.peek()
if r == eol || r == eof {
break
}
l.next()
}
if !l.opts.allowEmptyValues && len(l.current()) == 0 {
l.error(&unexpectedCharErr{r, "an assignment must be followed by one or more alphanumeric characters"})
}
if l.opts.allowMultilineWhitespacePrefix {
l.next()
if unicode.IsSpace(l.peek()) {
return lexPropValue
}
l.prev()
}
if l.opts.allowMultilineEscapeNewline {
r := l.rpeek()
if r == escape {
l.next()
return lexPropValue
}
}
l.emit(tokenPropValue)
return lexLineStart
}