forked from luxifer/ical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.go
384 lines (330 loc) · 8.08 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
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
package ical
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// item represents a token or text string returned from the scanner.
type item struct {
typ itemType // The type of this item.
pos int // The starting position, in bytes, of this item in the input string.
val string // The value of this item.
}
func (i item) String() string {
switch {
case i.typ == itemEOF:
return "EOF"
case i.typ == itemError:
return i.val
case i.typ > itemKeyword:
return fmt.Sprintf("<%s>", i.val)
case len(i.val) > 10:
return fmt.Sprintf("%.10q...", i.val)
}
return fmt.Sprintf("%q", i.val)
}
// itemType identifies the type of lex items.
type itemType int
const (
// Special tokens
itemError itemType = iota
itemEOF
itemLineEnd
// Properties
itemName
itemParamName
itemParamValue
itemValue
// Punctuation
itemColon // :
itemSemiColon // ;
itemEqual // =
itemComma // ,
// Keyword
itemKeyword // delimit the keyword list
// Delimiters
itemBeginVCalendar
itemEndVCalendar
itemBeginVEvent
itemEndVEvent
itemBeginVAlarm
itemEndVAlarm
)
var key = map[string]itemType{
"BEGIN:VCALENDAR": itemBeginVCalendar,
"END:VCALENDAR": itemEndVCalendar,
"BEGIN:VEVENT": itemBeginVEvent,
"END:VEVENT": itemEndVEvent,
"BEGIN:VALARM": itemBeginVAlarm,
"END:VALARM": itemEndVAlarm,
}
const eof = -1
// stateFn represents the state of the scanner as a function that returns the next state.
type stateFn func(*lexer) stateFn
// lexer holds the state of the scanner.
type lexer struct {
input string // the string being scanned
state stateFn // the next lexing function to enter
start int // start position of this item
pos int // current position in the input
width int // width of last rune read from input
lastPos int // position of most recent item returned by nextItem
items chan item // channel of scanned items
}
// lex creates a new scanner for the input string.
func lex(input string) *lexer {
l := &lexer{
input: input,
items: make(chan item),
}
go l.run() // Concurrently run state machine.
return l
}
// run runs the state machine for the lexer.
func (l *lexer) run() {
for l.state = lexName; l.state != nil; {
l.state = l.state(l)
}
close(l.items) // No more tokens will be delivered.
}
// emit passes an item back to the client.
func (l *lexer) emit(t itemType) {
l.items <- item{t, l.start, l.input[l.start:l.pos]}
l.start = l.pos
}
// ignore skips over the pending input before this point.
func (l *lexer) ignore() {
l.start = l.pos
}
// next returns the next rune in the input.
func (l *lexer) next() rune {
if int(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
}
// peek returns but does not consume the next rune in the input.
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
// backup steps back one rune. Can only be called once per call of next.
func (l *lexer) backup() {
l.pos -= l.width
}
// errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.nextItem.
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.items <- item{itemError, l.start, fmt.Sprintf(format, args...)}
return nil
}
// nextItem returns the next item from the input.
// Called by the parser, not in the lexing goroutine.
func (l *lexer) nextItem() item {
item := <-l.items
l.lastPos = item.pos
return item
}
// State functions
const (
crlf = "\r\n"
beginVCalendar = "BEGIN:VCALENDAR"
endVCalendar = "END:VCALENDAR"
beginVEvent = "BEGIN:VEVENT"
endVEvent = "END:VEVENT"
beginValarm = "BEGIN:VALARM"
endVAlarm = "END:VALARM"
)
func lexContentLine(l *lexer) stateFn {
switch r := l.next(); {
case r == ';':
l.emit(itemSemiColon)
return lexParamName
case r == ':':
l.emit(itemColon)
return lexValue
case r == ',':
l.emit(itemComma)
return lexParamValue
default:
return l.errorf("unrecognized character in action: %#U", r)
}
}
// lexNewLine scans CRLF
func lexNewLine(l *lexer) stateFn {
if l.peek() == eof {
return nil
}
if !strings.HasPrefix(l.input[l.pos:], crlf) {
l.errorf("unable to find end of line \"CRLF\"")
}
l.pos += len(crlf)
l.emit(itemLineEnd)
if l.next() == eof {
l.emit(itemEOF)
return nil
}
l.backup()
return lexName
}
// lexName scans the name in the content line
//
// name = iana-token / x-name
// iana-token = 1*(ALPHA / DIGIT / "-") ; iCalendar identifier registered with IANA
// x-name = "X-" [vendorid "-"] 1*(ALPHA / DIGIT / "-") ; Reserved for experimental use.
// vendorid = 3*(ALPHA / DIGIT) ; Vendor identification
func lexName(l *lexer) stateFn {
if strings.HasPrefix(l.input[l.pos:], beginVCalendar) {
l.pos += len(beginVCalendar)
l.emit(itemBeginVCalendar)
return lexNewLine
}
if strings.HasPrefix(l.input[l.pos:], endVCalendar) {
l.pos += len(endVCalendar)
l.emit(itemEndVCalendar)
return lexNewLine
}
if strings.HasPrefix(l.input[l.pos:], beginVEvent) {
l.pos += len(beginVEvent)
l.emit(itemBeginVEvent)
return lexNewLine
}
if strings.HasPrefix(l.input[l.pos:], endVEvent) {
l.pos += len(endVEvent)
l.emit(itemEndVEvent)
return lexNewLine
}
if strings.HasPrefix(l.input[l.pos:], beginValarm) {
l.pos += len(beginValarm)
l.emit(itemBeginVAlarm)
return lexNewLine
}
if strings.HasPrefix(l.input[l.pos:], endVAlarm) {
l.pos += len(endVAlarm)
l.emit(itemEndVAlarm)
return lexNewLine
}
Loop:
for {
switch r := l.next(); {
case isName(r):
// absorb
default:
l.backup()
l.emit(itemName)
break Loop
}
}
return lexContentLine
}
// lexParamName scans the param-name in the content line
//
// param-name = iana-token / x-name
// iana-token = 1*(ALPHA / DIGIT / "-") ; iCalendar identifier registered with IANA
// x-name = "X-" [vendorid "-"] 1*(ALPHA / DIGIT / "-") ; Reserved for experimental use.
// vendorid = 3*(ALPHA / DIGIT) ; Vendor identification
func lexParamName(l *lexer) stateFn {
Loop:
for {
switch r := l.next(); {
case isName(r):
// absorb
default:
l.backup()
l.emit(itemParamName)
break Loop
}
}
r := l.next()
if r == '=' {
l.emit(itemEqual)
return lexParamValue
}
return l.errorf("missing \"=\" sign after param name, got %#U", r)
}
// lexParamValue scans the param-value in the content line
//
// param-value = paramtext / quoted-string
// paramtext = *SAFE-CHAR
// quoted-string = DQUOTE *QSAFE-CHAR DQUOTE
// QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-US-ASCII ; Any character except CONTROL and DQUOTE
// SAFE-CHAR = WSP / %x21 / %x23-2B / %x2D-39 / %x3C-7E / NON-US-ASCII ; Any character except CONTROL, DQUOTE, ";", ":", ","
func lexParamValue(l *lexer) stateFn {
r := l.next()
if r == '"' {
l.ignore()
QLoop:
for {
switch r := l.next(); {
case isQSafeChar(r):
// absorb
default:
l.backup()
l.emit(itemParamValue)
break QLoop
}
}
r := l.next()
if r != '"' {
l.errorf("Missing \" for closing value")
} else {
l.ignore()
}
} else {
l.backup()
Loop:
for {
switch r := l.next(); {
case isSafeChar(r):
// absorb
default:
l.backup()
l.emit(itemParamValue)
break Loop
}
}
}
return lexContentLine
}
// lexValue scans the value in the content line
//
// value = *VALUE-CHAR
// VALUE-CHAR = WSP / %x21-7E / NON-US-ASCII ; Any textual character
func lexValue(l *lexer) stateFn {
Loop:
for {
switch r := l.next(); {
case isValueChar(r):
// absorb
default:
l.backup()
l.emit(itemValue)
break Loop
}
}
return lexNewLine
}
// rune helpers
func isName(r rune) bool {
return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-'
}
func isQSafeChar(r rune) bool {
return !unicode.IsControl(r) && r != '"'
}
func isSafeChar(r rune) bool {
return !unicode.IsControl(r) && r != '"' && r != ';' && r != ':' && r != ','
}
func isValueChar(r rune) bool {
return r == '\t' || (!unicode.IsControl(r) && utf8.ValidRune(r))
}
// item helpers
// isItemName checks if the item is an ical name
func isItemName(i item) bool {
return i.typ == itemName
}