generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fsm.go
109 lines (90 loc) · 2.91 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
package schema
import (
"fmt"
"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"`
}
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),
}
}
var _ Decl = (*FSM)(nil)
var _ Symbol = (*FSM)(nil)
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{}
fmt.Fprintf(w, "fsm %s {\n", f.Name)
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
}),
}
}
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)
}
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 }