-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_sections.go
307 lines (240 loc) · 8.15 KB
/
file_sections.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
package nit
import (
"fmt"
"go/ast"
"go/token"
"github.com/pkg/errors"
)
type (
// FileSection represents a code section.
FileSection uint8
// FileSectionMachine represents the state machine determining the right
// order for the sections in file.
FileSectionMachine struct {
current FileSectionTransition
}
// FileSectionTransition represents one of the 6 valid sections in the
// current file, it defines the State Machine transition rules for that
// concrete section, the order to be expected is:
// "Imports" -> "Types" -> "Consts" -> "Vars" -> "Funcs" -> "Methods"
FileSectionTransition interface {
Imports() (FileSectionTransition, error)
Types() (FileSectionTransition, error)
Consts() (FileSectionTransition, error)
Vars() (FileSectionTransition, error)
Funcs() (FileSectionTransition, error)
Methods() (FileSectionTransition, error)
}
constsFileSection struct{}
funcsFileSection struct{}
importsFileSection struct{}
methodsFileSection struct{}
typesFileSection struct{}
varsFileSection struct{}
)
const (
// FileSectionImports defines the `imports` state.
FileSectionImports FileSection = iota // FIXME kill
// FileSectionTypes defines the `type` state.
FileSectionTypes
// FileSectionConsts defines the `const` state.
FileSectionConsts
// FileSectionVars defines the `var` state.
FileSectionVars
// FileSectionFuncs defines the `func` state.
FileSectionFuncs
// SectionMethods defines the `method` state.
FileSectionMethods
)
// NewFileSectionTransition returns a new transition corresponding to the
// received value.
func NewFileSectionMachine(start FileSection) (*FileSectionMachine, error) {
// FIXME: implement tests
c, err := NewFileSectionTransition(start)
if err != nil {
return nil, err
}
return &FileSectionMachine{current: c}, nil
}
// NewFileSectionTransition returns a new transition corresponding to the
// received value.
func NewFileSectionTransition(s FileSection) (FileSectionTransition, error) {
switch s {
case FileSectionConsts:
return constsFileSection{}, nil
case FileSectionFuncs:
return funcsFileSection{}, nil
case FileSectionImports:
return importsFileSection{}, nil
case FileSectionMethods:
return methodsFileSection{}, nil
case FileSectionTypes:
return typesFileSection{}, nil
case FileSectionVars:
return varsFileSection{}, nil
}
return nil, errors.New("invalid file section value")
}
// NewFuncDeclFileSection returns a new State that matches the decl type.
func NewFuncDeclFileSection(decl *ast.FuncDecl) (FileSection, error) {
if decl.Recv == nil {
return FileSectionFuncs, nil
}
return FileSectionMethods, nil
}
// NewGenDeclFileSection returns a new State that matches the decl type.
func NewGenDeclFileSection(decl *ast.GenDecl) (FileSection, error) {
switch decl.Tok {
case token.IMPORT:
return FileSectionImports, nil
case token.CONST:
return FileSectionConsts, nil
case token.TYPE:
return FileSectionTypes, nil
case token.VAR:
return FileSectionVars, nil
}
return FileSectionImports, fmt.Errorf("unknown generic declaration node")
}
// Transition updates the internal state.
func (v *FileSectionMachine) Transition(next FileSection) error { //nolint:gocyclo
var (
res FileSectionTransition
err error
)
switch next {
case FileSectionImports:
res, err = v.current.Imports()
case FileSectionTypes:
res, err = v.current.Types()
case FileSectionConsts:
res, err = v.current.Consts()
case FileSectionVars:
res, err = v.current.Vars()
case FileSectionFuncs:
res, err = v.current.Funcs()
case FileSectionMethods:
res, err = v.current.Methods()
default:
err = errors.Errorf("invalid file section value: %d", next)
}
if err != nil {
return err
}
v.current = res
return nil
}
//-
func (constsFileSection) Consts() (FileSectionTransition, error) {
return constsFileSection{}, nil
}
func (constsFileSection) Funcs() (FileSectionTransition, error) {
return funcsFileSection{}, nil
}
func (constsFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `var`, `functions` or `methods`")
}
func (constsFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}
func (constsFileSection) Types() (FileSectionTransition, error) {
return nil, errors.New("`types` is invalid, next one must be `var`, `functions` or `methods`")
}
func (constsFileSection) Vars() (FileSectionTransition, error) {
return varsFileSection{}, nil
}
//-
func (funcsFileSection) Consts() (FileSectionTransition, error) {
return nil, errors.New("`consts` is invalid, next one must be `functions` or `methods`")
}
func (funcsFileSection) Funcs() (FileSectionTransition, error) {
return funcsFileSection{}, nil
}
func (funcsFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `functions` or `methods`")
}
func (funcsFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}
func (funcsFileSection) Types() (FileSectionTransition, error) {
return nil, errors.New("`types` is invalid, next one must be `functions` or `methods`")
}
func (funcsFileSection) Vars() (FileSectionTransition, error) {
return nil, errors.New("`var` is invalid, next one must be `functions` or `methods`")
}
//-
func (importsFileSection) Consts() (FileSectionTransition, error) {
return constsFileSection{}, nil
}
func (importsFileSection) Funcs() (FileSectionTransition, error) {
return funcsFileSection{}, nil
}
func (importsFileSection) Imports() (FileSectionTransition, error) {
return importsFileSection{}, nil
}
func (importsFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}
func (importsFileSection) Types() (FileSectionTransition, error) {
return typesFileSection{}, nil
}
func (importsFileSection) Vars() (FileSectionTransition, error) {
return varsFileSection{}, nil
}
//-
func (methodsFileSection) Consts() (FileSectionTransition, error) {
return nil, errors.New("`consts` is invalid, next one must be `methods`")
}
func (methodsFileSection) Funcs() (FileSectionTransition, error) {
return nil, errors.New("`functions` is invalid, next one must be `methods`")
}
func (methodsFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `methods`")
}
func (methodsFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}
func (methodsFileSection) Types() (FileSectionTransition, error) {
return nil, errors.New("`types` is invalid, next one must be `methods`")
}
func (methodsFileSection) Vars() (FileSectionTransition, error) {
return nil, errors.New("`var` is invalid, next one must be `methods`")
}
//-
func (typesFileSection) Consts() (FileSectionTransition, error) {
return constsFileSection{}, nil
}
func (typesFileSection) Funcs() (FileSectionTransition, error) {
return funcsFileSection{}, nil
}
func (typesFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `const`, `var`, `functions` or `methods`")
}
func (typesFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}
func (typesFileSection) Types() (FileSectionTransition, error) {
return typesFileSection{}, nil
}
func (typesFileSection) Vars() (FileSectionTransition, error) {
return varsFileSection{}, nil
}
//-
func (varsFileSection) Consts() (FileSectionTransition, error) {
return nil, errors.New("`consts` is invalid, next one must be `functions` or `methods`")
}
func (varsFileSection) Funcs() (FileSectionTransition, error) {
return funcsFileSection{}, nil
}
func (varsFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `functions` or `methods`")
}
func (varsFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}
func (varsFileSection) Types() (FileSectionTransition, error) {
return nil, errors.New("`types` is invalid, next one must be `functions` or `methods`")
}
func (varsFileSection) Vars() (FileSectionTransition, error) {
return nil, errors.New("`var` is invalid, next one must be `functions` or `methods`")
}