...
1
2
3
4
5
6
7
8
9
10
11
12
13 package properties
14
15 import (
16 "fmt"
17 "strconv"
18 "strings"
19 "unicode/utf8"
20 )
21
22
23 type item struct {
24 typ itemType
25 pos int
26 val string
27 }
28
29 func (i item) String() string {
30 switch {
31 case i.typ == itemEOF:
32 return "EOF"
33 case i.typ == itemError:
34 return i.val
35 case len(i.val) > 10:
36 return fmt.Sprintf("%.10q...", i.val)
37 }
38 return fmt.Sprintf("%q", i.val)
39 }
40
41
42 type itemType int
43
44 const (
45 itemError itemType = iota
46 itemEOF
47 itemKey
48 itemValue
49 itemComment
50 )
51
52
53 const eof = -1
54
55
56 const whitespace = " \f\t"
57
58
59 type stateFn func(*lexer) stateFn
60
61
62 type lexer struct {
63 input string
64 state stateFn
65 pos int
66 start int
67 width int
68 lastPos int
69 runes []rune
70 items chan item
71 }
72
73
74 func (l *lexer) next() rune {
75 if l.pos >= len(l.input) {
76 l.width = 0
77 return eof
78 }
79 r, w := utf8.DecodeRuneInString(l.input[l.pos:])
80 l.width = w
81 l.pos += l.width
82 return r
83 }
84
85
86 func (l *lexer) peek() rune {
87 r := l.next()
88 l.backup()
89 return r
90 }
91
92
93 func (l *lexer) backup() {
94 l.pos -= l.width
95 }
96
97
98 func (l *lexer) emit(t itemType) {
99 i := item{t, l.start, string(l.runes)}
100 l.items <- i
101 l.start = l.pos
102 l.runes = l.runes[:0]
103 }
104
105
106 func (l *lexer) ignore() {
107 l.start = l.pos
108 }
109
110
111 func (l *lexer) appendRune(r rune) {
112 l.runes = append(l.runes, r)
113 }
114
115
116 func (l *lexer) accept(valid string) bool {
117 if strings.ContainsRune(valid, l.next()) {
118 return true
119 }
120 l.backup()
121 return false
122 }
123
124
125 func (l *lexer) acceptRun(valid string) {
126 for strings.ContainsRune(valid, l.next()) {
127 }
128 l.backup()
129 }
130
131
132
133
134 func (l *lexer) lineNumber() int {
135 return 1 + strings.Count(l.input[:l.lastPos], "\n")
136 }
137
138
139
140 func (l *lexer) errorf(format string, args ...interface{}) stateFn {
141 l.items <- item{itemError, l.start, fmt.Sprintf(format, args...)}
142 return nil
143 }
144
145
146 func (l *lexer) nextItem() item {
147 i := <-l.items
148 l.lastPos = i.pos
149 return i
150 }
151
152
153 func lex(input string) *lexer {
154 l := &lexer{
155 input: input,
156 items: make(chan item),
157 runes: make([]rune, 0, 32),
158 }
159 go l.run()
160 return l
161 }
162
163
164 func (l *lexer) run() {
165 for l.state = lexBeforeKey(l); l.state != nil; {
166 l.state = l.state(l)
167 }
168 }
169
170
171
172
173 func lexBeforeKey(l *lexer) stateFn {
174 switch r := l.next(); {
175 case isEOF(r):
176 l.emit(itemEOF)
177 return nil
178
179 case isEOL(r):
180 l.ignore()
181 return lexBeforeKey
182
183 case isComment(r):
184 return lexComment
185
186 case isWhitespace(r):
187 l.ignore()
188 return lexBeforeKey
189
190 default:
191 l.backup()
192 return lexKey
193 }
194 }
195
196
197 func lexComment(l *lexer) stateFn {
198 l.acceptRun(whitespace)
199 l.ignore()
200 for {
201 switch r := l.next(); {
202 case isEOF(r):
203 l.ignore()
204 l.emit(itemEOF)
205 return nil
206 case isEOL(r):
207 l.emit(itemComment)
208 return lexBeforeKey
209 default:
210 l.appendRune(r)
211 }
212 }
213 }
214
215
216 func lexKey(l *lexer) stateFn {
217 var r rune
218
219 Loop:
220 for {
221 switch r = l.next(); {
222
223 case isEscape(r):
224 err := l.scanEscapeSequence()
225 if err != nil {
226 return l.errorf(err.Error())
227 }
228
229 case isEndOfKey(r):
230 l.backup()
231 break Loop
232
233 case isEOF(r):
234 break Loop
235
236 default:
237 l.appendRune(r)
238 }
239 }
240
241 if len(l.runes) > 0 {
242 l.emit(itemKey)
243 }
244
245 if isEOF(r) {
246 l.emit(itemEOF)
247 return nil
248 }
249
250 return lexBeforeValue
251 }
252
253
254
255
256 func lexBeforeValue(l *lexer) stateFn {
257 l.acceptRun(whitespace)
258 l.accept(":=")
259 l.acceptRun(whitespace)
260 l.ignore()
261 return lexValue
262 }
263
264
265 func lexValue(l *lexer) stateFn {
266 for {
267 switch r := l.next(); {
268 case isEscape(r):
269 if isEOL(l.peek()) {
270 l.next()
271 l.acceptRun(whitespace)
272 } else {
273 err := l.scanEscapeSequence()
274 if err != nil {
275 return l.errorf(err.Error())
276 }
277 }
278
279 case isEOL(r):
280 l.emit(itemValue)
281 l.ignore()
282 return lexBeforeKey
283
284 case isEOF(r):
285 l.emit(itemValue)
286 l.emit(itemEOF)
287 return nil
288
289 default:
290 l.appendRune(r)
291 }
292 }
293 }
294
295
296
297 func (l *lexer) scanEscapeSequence() error {
298 switch r := l.next(); {
299
300 case isEscapedCharacter(r):
301 l.appendRune(decodeEscapedCharacter(r))
302 return nil
303
304 case atUnicodeLiteral(r):
305 return l.scanUnicodeLiteral()
306
307 case isEOF(r):
308 return fmt.Errorf("premature EOF")
309
310
311 default:
312 l.appendRune(r)
313 return nil
314 }
315 }
316
317
318 func (l *lexer) scanUnicodeLiteral() error {
319
320 d := make([]rune, 4)
321 for i := 0; i < 4; i++ {
322 d[i] = l.next()
323 if d[i] == eof || !strings.ContainsRune("0123456789abcdefABCDEF", d[i]) {
324 return fmt.Errorf("invalid unicode literal")
325 }
326 }
327
328
329 r, err := strconv.ParseInt(string(d), 16, 0)
330 if err != nil {
331 return err
332 }
333
334 l.appendRune(rune(r))
335 return nil
336 }
337
338
339 func decodeEscapedCharacter(r rune) rune {
340 switch r {
341 case 'f':
342 return '\f'
343 case 'n':
344 return '\n'
345 case 'r':
346 return '\r'
347 case 't':
348 return '\t'
349 default:
350 return r
351 }
352 }
353
354
355
356 func atUnicodeLiteral(r rune) bool {
357 return r == 'u'
358 }
359
360
361 func isComment(r rune) bool {
362 return r == '#' || r == '!'
363 }
364
365
366 func isEndOfKey(r rune) bool {
367 return strings.ContainsRune(" \f\t\r\n:=", r)
368 }
369
370
371 func isEOF(r rune) bool {
372 return r == eof
373 }
374
375
376 func isEOL(r rune) bool {
377 return r == '\n' || r == '\r'
378 }
379
380
381
382 func isEscape(r rune) bool {
383 return r == '\\'
384 }
385
386
387
388 func isEscapedCharacter(r rune) bool {
389 return strings.ContainsRune(" :=fnrt", r)
390 }
391
392
393 func isWhitespace(r rune) bool {
394 return strings.ContainsRune(whitespace, r)
395 }
396
View as plain text