-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
383 lines (289 loc) · 9.49 KB
/
ast.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
package parser
import "net"
// Node is an interface implemented by all AST nodes
type Node interface {
children() []Node
}
// Branch Nodes have this struct embedded, as these fields can be used to pass
// evaluation results from children.
type BranchNode struct {
EvalResult bool
EvalResultSrc bool
EvalResultDst bool
}
// The overall structure of this grammar. Expressions are made up of statements
// in conjunction with more expressions. A statement consists of any matcher or
// a subexpression in parenthesis and is optionally negated.
type Expression struct {
BranchNode
Left *Statement `(@@ (`
Conjunction *String `@Conjunction`
Right *Expression `@@ )?)?`
}
func (o Expression) children() []Node {
return []Node{o.Left, o.Conjunction, o.Right}
}
type Statement struct {
BranchNode
Negated *Boolean `@Negation? (`
DirectionalMatch *DirectionalMatchGroup ` @@`
RegularMatch *RegularMatchGroup `| @@`
SubExpression *Expression `| "(" @@ ")" )`
}
func (o Statement) children() []Node {
return []Node{o.Negated, o.DirectionalMatch, o.RegularMatch,
o.SubExpression}
}
// Basic data type nodes which are mostly just aliases
type Address net.IP
func (o Address) children() []Node { return nil }
type Boolean bool
func (o Boolean) children() []Node { return nil }
type String string
func (o String) children() []Node { return nil }
type Number uint64
func (o Number) children() []Node { return nil }
// FIXME: pseudo "negative" Number
// used by the printer visitor only, for emitting a "-" between children
type RangeEnd Number
func (o RangeEnd) children() []Node { return nil }
type NumericRange struct {
BranchNode
Lower *Number `(@Number`
Upper *RangeEnd `"-" @Number) |`
Unary *String `( @Unary?`
Number *Number ` @Number )`
}
func (o NumericRange) children() []Node {
return []Node{o.Lower, o.Upper, o.Unary, o.Number}
}
// Match Nodes are the actual sub commands, without their command word
// They are in turn organized into MatchGroups. There are Regular Matches and
// Directional Matches.
// Regular Matches:
// * anything that has further sub commands or accepts fancy data
// * no direction
// * several dedicated Match structs for different data types and sub commands
type RegularMatchGroup struct {
BranchNode
Router *RouterMatch `"router" @@`
NextHop *NextHopMatch `| "nexthop" @@`
NextHopAsn *NextHopAsnMatch `| "nexthopasn" @@`
Bytes *ByteRangeMatch `| "bytes" @@`
Packets *PacketRangeMatch `| "packets" @@`
RemoteCountry *RemoteCountryMatch `| "country" @@`
FlowDirection *FlowDirectionMatch `| "direction"? @@`
Normalized *NormalizedMatch `| @@`
Duration *DurationRangeMatch `| "duration" @@`
Etype *EtypeMatch `| "etype" @@`
Proto *ProtoMatch `| "proto" @@`
Status *StatusMatch `| "status" @@`
TcpFlags *TcpFlagsMatch `| "tcpflags" @@`
IPTos *IPTosRangeMatch `| "iptos" @@`
Dscp *DscpMatch `| "dscp" @@`
Ecn *EcnMatch `| "ecn" @@`
SamplingRate *SamplingRateRangeMatch `| "samplingrate" @@`
Icmp *IcmpMatch `| "icmp" @@`
Bps *BpsRangeMatch `| "bps" @@`
Pps *PpsRangeMatch `| "pps" @@`
PassesThrough *PassesThroughListMatch `| "passes-through" @@`
Med *MedRangeMatch `| "med" @@`
LocalPref *LocalPrefRangeMatch `| "localpref" @@`
Rpki *RpkiMatch `| "rpki" @@`
}
func (o RegularMatchGroup) children() []Node {
return []Node{o.Router, o.NextHop, o.NextHopAsn, o.Bytes, o.Packets, o.RemoteCountry,
o.FlowDirection, o.Normalized, o.Duration, o.Etype, o.Proto,
o.Status, o.TcpFlags, o.IPTos, o.Dscp, o.Ecn, o.SamplingRate,
o.Icmp, o.Bps, o.Pps, o.PassesThrough, o.Med, o.LocalPref, o.Rpki}
}
type RouterMatch struct {
BranchNode
Address *net.IP `@Address`
}
func (o RouterMatch) children() []Node { return nil }
type NextHopMatch struct {
BranchNode
Address *net.IP `@Address`
}
func (o NextHopMatch) children() []Node { return nil }
type NextHopAsnMatch struct {
BranchNode
Asn *uint32 `@Number`
}
func (o NextHopAsnMatch) children() []Node { return nil }
type ByteRangeMatch struct{ NumericRange }
type PacketRangeMatch struct{ NumericRange }
type RemoteCountryMatch struct {
BranchNode
CountryCode *String `@CountryCode`
}
func (o RemoteCountryMatch) children() []Node { return nil }
type FlowDirectionMatch struct {
BranchNode
FlowDirection *String `@("incoming"|"outgoing")`
}
func (o FlowDirectionMatch) children() []Node { return nil }
type NormalizedMatch struct {
BranchNode
Normalized bool `@"normalized"`
}
func (o NormalizedMatch) children() []Node { return nil }
type DurationRangeMatch struct{ NumericRange }
type EtypeMatch struct {
BranchNode
Etype *Number ` @Number`
EtypeKey *EtypeKey `| @EtypeMagic`
}
func (o EtypeMatch) children() []Node {
return []Node{o.Etype, o.EtypeKey}
}
type EtypeKey Number
func (o EtypeKey) children() []Node { return nil }
func (o *EtypeKey) Capture(values []string) error {
*o = EtypeKey(EtypeMagicMap[values[0]])
return nil
}
type ProtoMatch struct {
BranchNode
Proto *Number ` @Number`
ProtoKey *ProtoKey `| @ProtoMagic`
}
func (o ProtoMatch) children() []Node {
return []Node{o.Proto, o.ProtoKey}
}
type ProtoKey Number
func (o ProtoKey) children() []Node { return nil }
func (o *ProtoKey) Capture(values []string) error {
*o = ProtoKey(ProtoMagicMap[values[0]])
return nil
}
type StatusMatch struct {
BranchNode
Status *Number ` @Number`
StatusKey *StatusKey `| @StatusMagic`
}
func (o StatusMatch) children() []Node {
return []Node{o.Status, o.StatusKey}
}
type StatusKey Number
func (o StatusKey) children() []Node { return nil }
func (o *StatusKey) Capture(values []string) error {
*o = StatusKey(StatusMagicMap[values[0]])
return nil
}
type TcpFlagsMatch struct {
BranchNode
TcpFlags *Number ` @Number`
TcpFlagsKey *TcpFlagsKey `| @TcpFlagsMagic`
}
func (o TcpFlagsMatch) children() []Node {
return []Node{o.TcpFlags, o.TcpFlagsKey}
}
type TcpFlagsKey Number
func (o TcpFlagsKey) children() []Node { return nil }
func (o *TcpFlagsKey) Capture(values []string) error {
*o = TcpFlagsKey(TcpFlagsMagicMap[values[0]])
return nil
}
type IPTosRangeMatch struct{ NumericRange }
type DscpMatch struct {
BranchNode
Dscp *Number ` @Number` // first 6 bits of iptos
DscpKey *DscpKey `| @DscpMagic`
}
func (o DscpMatch) children() []Node {
return []Node{o.Dscp, o.DscpKey}
}
type DscpKey Number
func (o DscpKey) children() []Node { return nil }
func (o *DscpKey) Capture(values []string) error {
*o = DscpKey(DscpMagicMap[values[0]])
return nil
}
type EcnMatch struct {
BranchNode
Ecn *Number ` @Number` // last 2 bits of iptos
EcnKey *EcnKey `| @EcnMagic`
}
func (o EcnMatch) children() []Node {
return []Node{o.Ecn, o.EcnKey}
}
type EcnKey Number
func (o EcnKey) children() []Node { return nil }
func (o *EcnKey) Capture(values []string) error {
*o = EcnKey(EcnMagicMap[values[0]])
return nil
}
type SamplingRateRangeMatch struct{ NumericRange }
type CidRangeMatch struct{ NumericRange }
type IcmpMatch struct {
BranchNode
Type *Number ` ( "type" @Number )`
Code *Number `| ( "code" @Number )`
}
func (o IcmpMatch) children() []Node {
return []Node{o.Type, o.Code}
}
type BpsRangeMatch struct{ NumericRange }
type PpsRangeMatch struct{ NumericRange }
type PassesThroughListMatch struct {
BranchNode
Numbers []Number `@Number @Number*`
}
func (o PassesThroughListMatch) children() []Node { return nil }
type MedRangeMatch struct{ NumericRange }
type LocalPrefRangeMatch struct{ NumericRange }
type RpkiMatch struct {
BranchNode
RpkiKey *RpkiKey `@RpkiMagic`
}
func (o RpkiMatch) children() []Node {
return []Node{o.RpkiKey}
}
type RpkiKey Number
func (o RpkiKey) children() []Node { return nil }
func (o *RpkiKey) Capture(values []string) error {
*o = RpkiKey(RpkiMagicMap[values[0]])
return nil
}
// Directional Matches:
// * anything that has further sub commands or accepts fancy data
// * no direction
// * several dedicated Match structs for different data types and sub commands
type DirectionalMatchGroup struct {
BranchNode
Direction *String `@Direction?`
Address *AddressMatch `( "address" @@`
Interface *InterfaceMatch `| ("iface"|"interface") @@`
Port *PortRangeMatch `| "port" @@`
Asn *AsnRangeMatch `| "asn" @@`
Netsize *NetsizeRangeMatch `| "netsize" @@`
Cid *CidRangeMatch `| "cid" @@`
Vrf *VrfRangeMatch `| "vrf" @@ )`
}
func (o DirectionalMatchGroup) children() []Node {
return []Node{o.Direction, o.Address, o.Interface, o.Port, o.Asn,
o.Netsize, o.Cid, o.Vrf}
}
type AddressMatch struct {
BranchNode
Address *net.IP `@Address`
Mask *Number `( "/" @Number)?`
}
func (o AddressMatch) children() []Node { return nil }
type InterfaceMatch struct {
BranchNode
SnmpId *Number ` ( "id"? @Number )`
Name *String `| ( "name" @String )`
Description *String `| ( "desc" @String )`
Speed *IfSpeedRangeMatch `| ("speed" @@)`
}
func (o InterfaceMatch) children() []Node {
return []Node{o.SnmpId, o.Name, o.Description, o.Speed}
}
type IfSpeedRangeMatch struct{ NumericRange }
type PortRangeMatch struct{ NumericRange }
type AsnRangeMatch struct{ NumericRange }
type NetsizeRangeMatch struct{ NumericRange }
type VrfRangeMatch struct{ NumericRange }