-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathlex.go
431 lines (388 loc) · 8.83 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
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
/* Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gonids
import (
"errors"
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// item represents a token or text string returned from the lexer.
type item struct {
typ itemType // The type of this item.
value string // The value of this item.
}
// String returns a string describing an item.
func (i item) String() string {
switch i.typ {
case itemEOF:
return "EOF"
case itemError:
return i.value
}
return fmt.Sprintf("%q: %s", i.typ, i.value)
}
type itemType int
const (
itemError itemType = iota
itemComment
itemAction
itemProtocol
itemSourceAddress
itemSourcePort
itemDirection
itemDestinationAddress
itemDestinationPort
itemNot
itemOptionKey
itemOptionValue
itemOptionNoValue
itemOptionValueString
itemEOR
itemEOF
)
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
pos int // current position in the input
start int // start position of this item
width int // width of last rune read from input
items chan item // channel of scanned items
}
// next returns the next rune in the input.
func (l *lexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
if r == utf8.RuneError && w == 1 {
// The whole input string has been validated at init.
panic("invalid UTF-8 character")
}
l.width = w
l.pos += l.width
return r
}
// skipNext skips over the next rune in the input.
func (l *lexer) skipNext() {
l.next()
l.ignore()
}
// len returns the current length of the item in processing.
func (l *lexer) len() int {
if l.pos >= len(l.input) {
return -1
}
return l.pos - l.start
}
// backup steps back one rune. Can only be called once per call of next.
func (l *lexer) backup() {
if l.width == -1 {
panic("double backup")
}
l.pos -= l.width
l.width = -1
}
// emit passes an item back to the client, trimSpaces can be used to trim spaces around item
// value before emiting.
func (l *lexer) emit(t itemType, trimSpaces bool) {
input := l.input[l.start:l.pos]
if trimSpaces {
input = strings.TrimSpace(input)
}
// This is a bit of a hack. We lex until `;` now so we end up with extra `"`.
input = strings.TrimSuffix(input, `"`)
l.items <- item{t, input}
l.start = l.pos
}
// ignore skips over the pending input before this point.
func (l *lexer) ignore() {
l.start = l.pos
}
// acceptRun consumes a run of runes from the valid set.
func (l *lexer) acceptRun(valid string) {
for strings.ContainsRune(valid, l.next()) {
}
l.backup()
}
// ignoreSpaces ignores all spaces at the start of the input.
func (l *lexer) ignoreSpaces() {
for unicode.IsSpace(l.next()) {
l.ignore()
}
l.backup()
}
// 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, fmt.Sprintf(format, args...)}
return nil
}
func (l *lexer) unexpectedEOF() stateFn {
return nil
}
// nextItem returns the next item from the input.
func (l *lexer) nextItem() item {
r, more := <-l.items
if !more {
return item{itemError, "unexpected EOF"}
}
return r
}
// lex initializes and runs a new scanner for the input string.
func lex(input string) (*lexer, error) {
if !utf8.ValidString(input) {
return nil, errors.New("input is not a valid UTF-8 string")
}
l := &lexer{
input: input,
items: make(chan item, 0x1000),
}
go l.run()
return l, nil
}
// TODO: handle error and corner case in all states.
// run runs the state machine for the lexer.
func (l *lexer) run() {
for l.state = lexRule; l.state != nil; {
l.state = l.state(l)
}
close(l.items)
}
func (l *lexer) close() {
// Reads all items until channel close to be sure goroutine has ended.
more := true
for more {
_, more = <-l.items
}
}
// lexRule starts the scan of a rule.
func lexRule(l *lexer) stateFn {
r := l.next()
switch {
case unicode.IsSpace(r):
l.ignore()
return lexRule
case r == '#':
return lexComment
case r == eof:
l.emit(itemEOF, false)
return nil
}
return lexAction
}
// lexComment consumes a commented rule.
func lexComment(l *lexer) stateFn {
// Ignore leading spaces and #.
l.ignore()
for {
r := l.next()
if unicode.IsSpace(r) || r == '#' {
l.ignore()
} else {
break
}
}
l.backup()
for {
switch l.next() {
case '\r', '\n':
l.emit(itemComment, false)
return lexRule
case eof:
l.backup()
l.emit(itemComment, false)
return lexRule
}
}
}
// lexAction consumes a rule action.
func lexAction(l *lexer) stateFn {
for {
r := l.next()
switch {
case r == ' ':
l.emit(itemAction, true)
return lexProtocol
case !unicode.IsLetter(r):
return l.errorf("invalid character %q for a rule action", r)
}
}
}
// lexProtocol consumes a rule protocol.
func lexProtocol(l *lexer) stateFn {
l.ignoreSpaces()
for {
r := l.next()
switch {
case r == ' ':
l.emit(itemProtocol, true)
return lexSourceAddress
case !(unicode.IsLetter(r) || unicode.IsDigit(r) || (l.len() > 0 && r == '-')):
return l.errorf("invalid character %q for a rule protocol", r)
}
}
}
// lexSourceAddress consumes a source address.
func lexSourceAddress(l *lexer) stateFn {
l.ignoreSpaces()
for {
switch l.next() {
case ' ':
l.emit(itemSourceAddress, true)
return lexSourcePort
case eof:
return l.unexpectedEOF()
}
}
}
// lexSourcePort consumes a source port.
func lexSourcePort(l *lexer) stateFn {
l.ignoreSpaces()
for {
switch l.next() {
case ' ':
l.emit(itemSourcePort, true)
return lexDirection
case eof:
return l.unexpectedEOF()
}
}
}
// lexDirection consumes a rule direction.
func lexDirection(l *lexer) stateFn {
l.ignoreSpaces()
l.acceptRun("<->")
if r := l.next(); r != ' ' {
return l.errorf("invalid character %q for a rule direction", r)
}
l.emit(itemDirection, true)
return lexDestinationAddress
}
// lexDestinationAddress consumes a destination address.
func lexDestinationAddress(l *lexer) stateFn {
l.ignoreSpaces()
for {
switch l.next() {
case ' ':
l.emit(itemDestinationAddress, true)
return lexDestinationPort
case eof:
return l.unexpectedEOF()
}
}
}
// lexDestinationPort consumes a destination port.
func lexDestinationPort(l *lexer) stateFn {
for {
switch l.next() {
case '(':
l.backup()
l.emit(itemDestinationPort, true)
l.skipNext()
return lexOptionKey
case eof:
return l.unexpectedEOF()
}
}
}
// lexOptionKey scans a key from the rule options.
func lexOptionKey(l *lexer) stateFn {
for {
switch l.next() {
case ':':
l.backup()
l.emit(itemOptionKey, true)
l.skipNext()
return lexOptionValueBegin
case ';':
l.backup()
if l.pos > l.start {
l.emit(itemOptionKey, true)
l.emit(itemOptionNoValue, true)
}
l.skipNext()
return lexOptionKey
case ')':
l.backup()
if l.pos > l.start {
l.emit(itemOptionKey, true)
}
l.skipNext()
return lexRuleEnd
case eof:
return l.unexpectedEOF()
}
}
}
// lexOptionValueBegin scans the beginning of a value from the rule option.
func lexOptionValueBegin(l *lexer) stateFn {
switch l.next() {
case '"':
l.ignore()
return lexOptionValueString
case ' ':
l.ignore()
return lexOptionValueBegin
case '!':
l.emit(itemNot, true)
return lexOptionValueBegin
}
return lexOptionValue
}
// lexOptionValueString consumes the inner content of a string value from the rule options.
func lexOptionValueString(l *lexer) stateFn {
escaped := false
for {
switch l.next() {
case ';':
l.backup()
l.emit(itemOptionValueString, false)
l.skipNext()
return lexOptionKey
case '\\':
escaped = !escaped
if l.next() != ';' || !escaped {
l.backup()
}
case eof:
return l.unexpectedEOF()
default:
escaped = false
}
}
}
// lexOptionValue scans a value from the rule options.
func lexOptionValue(l *lexer) stateFn {
for {
switch l.next() {
case ';':
l.backup()
l.emit(itemOptionValue, true)
l.skipNext()
return lexOptionKey
case eof:
return l.unexpectedEOF()
}
}
}
// lexOptionEnd marks the end of a rule.
func lexRuleEnd(l *lexer) stateFn {
l.emit(itemEOR, false)
return lexRule
}