generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fsm.go
147 lines (127 loc) · 3.86 KB
/
fsm.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
package schema
import (
"fmt"
"sort"
"strings"
"google.golang.org/protobuf/reflect/protoreflect"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/internal/slices"
)
type FSM struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"2"`
Name string `parser:"'fsm' @Ident '{'" protobuf:"3"`
Start []*Ref `parser:"('start' @@)*" protobuf:"4"` // Start states.
Transitions []*FSMTransition `parser:"('transition' @@)* '}'" protobuf:"5"`
Metadata []Metadata `parser:"@@*" protobuf:"6"`
}
func FSMFromProto(pb *schemapb.FSM) *FSM {
return &FSM{
Pos: posFromProto(pb.Pos),
Name: pb.Name,
Start: slices.Map(pb.Start, RefFromProto),
Transitions: slices.Map(pb.Transitions, FSMTransitionFromProto),
Metadata: metadataListToSchema(pb.Metadata),
}
}
var _ Decl = (*FSM)(nil)
var _ Symbol = (*FSM)(nil)
// TerminalStates returns the terminal states of the FSM.
func (f *FSM) TerminalStates() []*Ref {
var out []*Ref
all := map[string]struct{}{}
in := map[string]struct{}{}
for _, t := range f.Transitions {
all[t.From.String()] = struct{}{}
all[t.To.String()] = struct{}{}
in[t.From.String()] = struct{}{}
}
for key := range all {
if _, ok := in[key]; !ok {
ref, err := ParseRef(key)
if err != nil {
panic(err) // key must be valid
}
out = append(out, ref)
}
}
sort.SliceStable(out, func(i, j int) bool {
return out[i].String() < out[j].String()
})
return out
}
func (f *FSM) GetName() string { return f.Name }
func (f *FSM) IsExported() bool { return false }
func (f *FSM) Position() Position { return f.Pos }
func (f *FSM) schemaDecl() {}
func (f *FSM) schemaSymbol() {}
func (f *FSM) String() string {
w := &strings.Builder{}
if len(f.Metadata) == 0 {
fmt.Fprintf(w, "fsm %s {\n", f.Name)
} else {
fmt.Fprintf(w, "fsm %s", f.Name)
fmt.Fprint(w, indent(encodeMetadata(f.Metadata)))
fmt.Fprintf(w, "\n{\n")
}
for _, s := range f.Start {
fmt.Fprintf(w, " start %s\n", s)
}
for _, t := range f.Transitions {
fmt.Fprintf(w, " transition %s\n", t)
}
fmt.Fprintf(w, "}")
return w.String()
}
func (f *FSM) ToProto() protoreflect.ProtoMessage {
return &schemapb.FSM{
Pos: posToProto(f.Pos),
Name: f.Name,
Start: slices.Map(f.Start, func(r *Ref) *schemapb.Ref {
return r.ToProto().(*schemapb.Ref) //nolint: forcetypeassert
}),
Transitions: slices.Map(f.Transitions, func(t *FSMTransition) *schemapb.FSMTransition {
return t.ToProto().(*schemapb.FSMTransition) //nolint: forcetypeassert
}),
Metadata: metadataListToProto(f.Metadata),
}
}
func (f *FSM) schemaChildren() []Node {
out := make([]Node, 0, len(f.Transitions))
for _, s := range f.Start {
out = append(out, s)
}
for _, t := range f.Transitions {
out = append(out, t)
}
for _, m := range f.Metadata {
out = append(out, m)
}
return out
}
type FSMTransition struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"2"`
From *Ref `parser:"@@" protobuf:"3,optional"`
To *Ref `parser:"'to' @@" protobuf:"4"`
}
func FSMTransitionFromProto(pb *schemapb.FSMTransition) *FSMTransition {
return &FSMTransition{
Pos: posFromProto(pb.Pos),
From: RefFromProto(pb.From),
To: RefFromProto(pb.To),
}
}
var _ Node = (*FSMTransition)(nil)
func (f *FSMTransition) Position() Position { return f.Pos }
func (f *FSMTransition) String() string {
return fmt.Sprintf("%s to %s", f.From, f.To)
}
func (f *FSMTransition) ToProto() protoreflect.ProtoMessage {
return &schemapb.FSMTransition{
Pos: posToProto(f.Pos),
From: f.From.ToProto().(*schemapb.Ref), //nolint: forcetypeassert
To: f.To.ToProto().(*schemapb.Ref), //nolint: forcetypeassert
}
}
func (f *FSMTransition) schemaChildren() []Node { return nil }