forked from getlantern/marionette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
319 lines (281 loc) · 6.66 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
package mar
import (
"math/rand"
)
// Node represents a node within the AST.
type Node interface {
node()
}
func (*Document) node() {}
func (*Transition) node() {}
func (*ActionBlock) node() {}
func (*Action) node() {}
func (*Arg) node() {}
func (*Pos) node() {}
type Document struct {
UUID int
Format string
Connection Pos
Lparen Pos
Transport string
TransportPos Pos
Comma Pos
Port string
PortPos Pos
Rparen Pos
Colon Pos
Transitions []*Transition
ActionBlocks []*ActionBlock
}
// FirstSender returns the party that initiates the protocol.
func (doc *Document) FirstSender() string {
if doc.Format == "ftp_pasv_transfer" {
return "server"
}
return "client"
}
// ActionBlock returns an action block by name.
func (doc *Document) ActionBlock(name string) *ActionBlock {
for _, blk := range doc.ActionBlocks {
if blk.Name == name {
return blk
}
}
return nil
}
// HasTransition returns true if there is a transition between src and dst.
func (doc *Document) HasTransition(src, dst string) bool {
for _, transition := range doc.Transitions {
if transition.Source == src && transition.Destination == dst {
return true
}
}
return false
}
// Normalize ensures document conforms to expected state.
func (doc *Document) Normalize() error {
// Add dead state transitions.
if !doc.HasTransition("end", "dead") {
doc.Transitions = append(doc.Transitions, &Transition{Source: "end", Destination: "dead", ActionBlock: "NULL", Probability: 1})
}
if !doc.HasTransition("dead", "dead") {
doc.Transitions = append(doc.Transitions, &Transition{Source: "dead", Destination: "dead", ActionBlock: "NULL", Probability: 1})
}
return nil
}
type Transition struct {
Source string
SourcePos Pos
Destination string
DestinationPos Pos
ActionBlock string
ActionBlockPos Pos
Probability float64
ProbabilityPos Pos
IsErrorTransition bool
}
func FilterTransitionsBySource(a []*Transition, name string) []*Transition {
other := make([]*Transition, 0, len(a))
for _, t := range a {
if t.Source == name {
other = append(other, t)
}
}
return other
}
func FilterTransitionsByDestination(a []*Transition, name string) []*Transition {
other := make([]*Transition, 0, len(a))
for _, t := range a {
if t.Destination == name {
other = append(other, t)
}
}
return other
}
func FilterProbableTransitions(a []*Transition) []*Transition {
other := make([]*Transition, 0, len(a))
for _, t := range a {
if t.Probability > 0 {
other = append(other, t)
}
}
return other
}
func FilterErrorTransitions(a []*Transition) []*Transition {
var other []*Transition
for _, t := range a {
if t.IsErrorTransition {
other = append(other, t)
}
}
return other
}
func FilterNonErrorTransitions(a []*Transition) []*Transition {
other := make([]*Transition, 0, len(a))
for _, t := range a {
if !t.IsErrorTransition {
other = append(other, t)
}
}
return other
}
// TransitionsDestinations returns the destination state names from the transitions.
func TransitionsDestinations(a []*Transition) []string {
other := make([]string, 0, len(a))
for _, t := range a {
other = append(other, t.Destination)
}
return other
}
// TransitionsErrorState returns the first error state in a list of transitions.
func TransitionsErrorState(a []*Transition) string {
for _, t := range a {
if t.IsErrorTransition {
return t.Destination
}
}
return ""
}
func ChooseTransitions(a []*Transition, rand *rand.Rand) []*Transition {
// If PRNG not available then return all transitions with a non-zero probability.
if rand == nil {
return FilterProbableTransitions(a)
}
// If there is only one transition then return it.
if len(a) == 1 {
return a
}
// Otherwise randomly choose a transition based on probability.
sum, coin := float64(0), rand.Float64()
for _, t := range a {
if t.Probability <= 0 {
continue
}
sum += t.Probability
if sum >= coin {
return []*Transition{t}
}
}
return []*Transition{a[len(a)-1]}
}
type ActionBlock struct {
Action Pos
Name string
NamePos Pos
Colon Pos
Actions []*Action
}
type Action struct {
Party string
PartyPos Pos
Module string
ModulePos Pos
Dot Pos
Method string
MethodPos Pos
Lparen Pos
Args []*Arg
Rparen Pos
If Pos
RegexMatchIncoming Pos
RegexMatchIncomingLparen Pos
Regex string
RegexPos Pos
RegexMatchIncomingRparen Pos
}
// Name returns the concatenation of the module & method.
func (a *Action) Name() string {
return a.Module + "." + a.Method
}
func (a *Action) ArgValues() []interface{} {
other := make([]interface{}, len(a.Args))
for i, arg := range a.Args {
other[i] = arg.Value
}
return other
}
// Transform converts the action to its complement depending on the party.
func (a *Action) Transform(party string) {
switch party {
case "client":
a.transform("server", "client")
case "server":
a.transform("client", "server")
}
}
func (a *Action) transform(from, to string) {
if a.Party == from {
switch a.Module {
case "fte", "tg":
if a.Method == "send" {
a.Method = "recv"
} else if a.Method == "send_async" {
a.Method = "recv_async"
}
a.Party = to
case "io":
if a.Method == "gets" {
a.Method = "puts"
} else if a.Method == "puts" {
a.Method = "gets"
}
a.Party = to
}
}
}
// FilterActionsByParty returns a slice of actions matching party.
func FilterActionsByParty(actions []*Action, party string) []*Action {
other := make([]*Action, 0, len(actions))
for _, action := range actions {
if action.Party == party {
other = append(other, action)
}
}
return other
}
type Arg struct {
Value interface{}
Pos Pos
EndPos Pos
}
// Pos specifies the line and character position of a token.
// The Char and Line are both zero-based indexes.
type Pos struct {
Char int
Line int
}
// Walk traverses an AST in depth-first order.
func Walk(v Visitor, node Node) {
if v = v.Visit(node); v == nil {
return
}
// Walk children.
switch node := node.(type) {
case *Document:
for _, transition := range node.Transitions {
Walk(v, transition)
}
for _, blk := range node.ActionBlocks {
Walk(v, blk)
}
case *ActionBlock:
for _, action := range node.Actions {
Walk(v, action)
}
case *Action:
for _, arg := range node.Args {
Walk(v, arg)
}
}
v.Visit(nil)
}
// Visitor represents an object for iterating over nodes using Walk().
type Visitor interface {
Visit(node Node) (w Visitor)
}
// VisitorFunc implements a type to use a function as a Visitor.
type VisitorFunc func(node Node)
func (fn VisitorFunc) Visit(node Node) Visitor {
fn(node)
return fn
}