-
Notifications
You must be signed in to change notification settings - Fork 10
/
sequence.go
444 lines (348 loc) · 11.9 KB
/
sequence.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package sequences
import (
"strconv"
"github.com/zimmski/tavor"
"github.com/zimmski/tavor/token"
"github.com/zimmski/tavor/token/lists"
"github.com/zimmski/tavor/token/primitives"
)
// Sequence implements a general sequence token which can generate Item tokens to use the internal sequence
// The sequence starts its numeration at the given start value and increases with every new sequence numeration its current value by the given step value.
type Sequence struct {
start int
step int
value int
}
// NewSequence returns a new instance of a Sequence token with a start value and a step value
func NewSequence(start, step int) *Sequence {
return &Sequence{
start: start,
step: step,
value: start,
}
}
func init() {
token.RegisterTyped("Sequence", func(argParser token.ArgumentsTypedParser) (token.Token, error) {
start := argParser.GetInt("start", 1)
step := argParser.GetInt("step", 1)
if err := argParser.Err(); err != nil {
return nil, err
}
return NewSequence(start, step), nil
})
}
func (s *Sequence) existing(r uint, except []token.Token) (int, error) {
n := s.value - s.start
if n == 0 {
return 0, tavor.ErrNoSequenceValue
}
n /= s.step
if len(except) == 0 {
return int(r)*s.step + s.start, nil
}
checked := make(map[int]struct{})
exceptLookup := make(map[int]struct{})
for i := 0; i < len(except); i++ {
tok := except[i]
if v, ok := tok.(*primitives.Scope); ok {
tok = v.Get()
}
switch t := tok.(type) {
case token.ListToken:
for j := 0; j < t.Len(); j++ {
tj, err := t.Get(j)
if err != nil {
panic(err) // TODO
}
ex, err := strconv.Atoi(tj.String())
if err != nil {
// TODO
continue
}
exceptLookup[ex] = struct{}{}
}
default:
ex, err := strconv.Atoi(except[i].String())
if err != nil {
// TODO
continue
}
exceptLookup[ex] = struct{}{}
}
}
for n != len(checked) {
i := (int(r)%n)*s.step + s.start
if _, ok := checked[i]; ok {
r++
continue
}
checked[i] = struct{}{}
if _, ok := exceptLookup[i]; !ok {
return i, nil
}
}
return 0, tavor.ErrNoSequenceValue
}
// ExistingItem returns a new instance of a SequenceExistingItem token referencing the sequence and holding the starting value of the sequence as its current value
func (s *Sequence) ExistingItem(except []token.Token) *SequenceExistingItem {
v := -1 // TODO there should be some kind of real nil value
if s.value != s.start {
v = s.start
}
return &SequenceExistingItem{
sequence: s,
value: v,
except: except,
}
}
// Item returns a new instance of a SequenceItem token referencing the sequence and generating and holding a new sequence numeration
func (s *Sequence) Item() *SequenceItem {
return &SequenceItem{
sequence: s,
value: s.Next(),
}
}
// Next generates a new sequence numeration
func (s *Sequence) Next() int {
c := s.value
s.value += s.step
return c
}
// ResetToken interface methods
// Reset resets the (internal) state of this token and its dependences, returns an error if the reseted state should not be used for a generation.
func (s *Sequence) Reset() error {
s.value = s.start
return nil
}
// ResetItem returns a new intsance of a SequenceResetItem token referencing the sequence
func (s *Sequence) ResetItem() *SequenceResetItem {
return &SequenceResetItem{
sequence: s,
}
}
// Sequence is an unusable token
// Clone returns a copy of the token and all its children
func (s *Sequence) Clone() token.Token { panic("unusable token") }
// Parse tries to parse the token beginning from the current position in the parser data.
// If the parsing is successful the error argument is nil and the next current position after the token is returned.
func (s *Sequence) Parse(pars *token.InternalParser, cur int) (int, []error) {
panic("unusable token")
}
// Permutation sets a specific permutation for this token
func (s *Sequence) Permutation(i uint) error { panic("unusable token") }
// Permutations returns the number of permutations for this token
func (s *Sequence) Permutations() uint { panic("unusable token") }
// PermutationsAll returns the number of all possible permutations for this token including its children
func (s *Sequence) PermutationsAll() uint { panic("unusable token") }
func (s *Sequence) String() string { panic("unusable token") }
// SequenceItem implements a sequence item token which holds one distinct value of the sequence
// A new sequence value is generated on every token permutation.
type SequenceItem struct {
sequence *Sequence
value int
}
// Clone returns a copy of the token and all its children
func (s *SequenceItem) Clone() token.Token {
return &SequenceItem{
sequence: s.sequence,
value: s.value,
}
}
// Parse tries to parse the token beginning from the current position in the parser data.
// If the parsing is successful the error argument is nil and the next current position after the token is returned.
func (s *SequenceItem) Parse(pars *token.InternalParser, cur int) (int, []error) {
panic("TODO implement")
}
func (s *SequenceItem) permutation(i uint) error {
s.value = s.sequence.Next()
return nil
}
// Permutation sets a specific permutation for this token
func (s *SequenceItem) Permutation(i uint) error {
permutations := s.Permutations()
if i >= permutations {
return &token.PermutationError{
Type: token.PermutationErrorIndexOutOfBound,
}
}
return s.permutation(i)
}
// Permutations returns the number of permutations for this token
func (s *SequenceItem) Permutations() uint {
return 1
}
// PermutationsAll returns the number of all possible permutations for this token including its children
func (s *SequenceItem) PermutationsAll() uint {
return s.Permutations()
}
func (s *SequenceItem) String() string {
return strconv.Itoa(s.value)
}
// ResetToken interface methods
// Reset resets the (internal) state of this token and its dependences, returns an error if the reseted state should not be used for a generation.
func (s *SequenceItem) Reset() error {
return s.permutation(0)
}
// SequenceExistingItem implements a sequence item token which holds one existing value of the sequence
// A new existing sequence value is choosen on every token permutation.
type SequenceExistingItem struct {
sequence *Sequence
value int
except []token.Token
}
// Clone returns a copy of the token and all its children
func (s *SequenceExistingItem) Clone() token.Token {
c := SequenceExistingItem{
sequence: s.sequence,
value: s.value, // TODO FIXME apply except
except: make([]token.Token, len(s.except)),
}
for i, tok := range s.except {
c.except[i] = tok.Clone()
}
return &c
}
// Parse tries to parse the token beginning from the current position in the parser data.
// If the parsing is successful the error argument is nil and the next current position after the token is returned.
func (s *SequenceExistingItem) Parse(pars *token.InternalParser, cur int) (int, []error) {
panic("TODO implement")
}
func (s *SequenceExistingItem) permutation(i uint) error {
s.value = -1 // TODO set this token to a default value so we do not get confused when it is looked up
v, err := s.sequence.existing(i, s.except)
s.value = v
return err
}
// Permutation sets a specific permutation for this token
func (s *SequenceExistingItem) Permutation(i uint) error {
permutations := s.Permutations()
if permutations == 0 {
// TODO FIXME ignore this for now
return nil
}
if i >= permutations {
return &token.PermutationError{
Type: token.PermutationErrorIndexOutOfBound,
}
}
return s.permutation(i)
}
// Permutations returns the number of permutations for this token
func (s *SequenceExistingItem) Permutations() uint {
// TODO FIXME we need to include the except-tokens here too, as well as in Permutation()
return uint((s.sequence.value - s.sequence.start) / s.sequence.step)
}
// PermutationsAll returns the number of all possible permutations for this token including its children
func (s *SequenceExistingItem) PermutationsAll() uint {
return s.Permutations()
}
func (s *SequenceExistingItem) String() string {
return strconv.Itoa(s.value)
}
// ForwardToken interface methods
// Get returns the current referenced token at the given index. The error return argument is not nil, if the index is out of bound.
func (s *SequenceExistingItem) Get(i int) (token.Token, error) {
return nil, &lists.ListError{
Type: lists.ListErrorOutOfBound,
}
}
// Len returns the number of the current referenced tokens
func (s *SequenceExistingItem) Len() int {
return 0
}
// InternalGet returns the current referenced internal token at the given index. The error return argument is not nil, if the index is out of bound.
func (s *SequenceExistingItem) InternalGet(i int) (token.Token, error) {
if i < 0 || i >= len(s.except) {
return nil, &lists.ListError{
Type: lists.ListErrorOutOfBound,
}
}
return s.except[i], nil
}
// InternalLen returns the number of referenced internal tokens
func (s *SequenceExistingItem) InternalLen() int {
return len(s.except)
}
// InternalLogicalRemove removes the referenced internal token and returns the replacement for the current token or nil if the current token should be removed.
func (s *SequenceExistingItem) InternalLogicalRemove(tok token.Token) token.Token {
for i := 0; i < len(s.except); i++ {
if s.except[i] == tok {
if i == len(s.except)-1 {
s.except = s.except[:i]
} else {
s.except = append(s.except[:i], s.except[i+1:]...)
}
i--
}
}
return s
}
// InternalReplace replaces an old with a new internal token if it is referenced by this token. The error return argument is not nil, if the replacement is not suitable.
func (s *SequenceExistingItem) InternalReplace(oldToken, newToken token.Token) error {
for i := 0; i < len(s.except); i++ {
if s.except[i] == oldToken {
s.except[i] = newToken
}
}
return nil
}
// ResetToken interface methods
// Reset resets the (internal) state of this token and its dependences, returns an error if the reseted state should not be used for a generation.
func (s *SequenceExistingItem) Reset() error {
return s.permutation(0)
}
// ScopeToken interface methods
// SetScope sets the scope of the token
func (s *SequenceExistingItem) SetScope(variableScope *token.VariableScope) {
if len(s.except) != 0 {
for i := 0; i < len(s.except); i++ {
if tok, ok := s.except[i].(token.ScopeToken); ok {
tok.SetScope(variableScope)
}
}
}
}
// SequenceResetItem implements a sequence token item which resets its referencing sequence on every permutation
type SequenceResetItem struct {
sequence *Sequence
}
// Clone returns a copy of the token and all its children
func (s *SequenceResetItem) Clone() token.Token {
return &SequenceResetItem{
sequence: s.sequence,
}
}
// Parse tries to parse the token beginning from the current position in the parser data.
// If the parsing is successful the error argument is nil and the next current position after the token is returned.
func (s *SequenceResetItem) Parse(pars *token.InternalParser, cur int) (int, []error) {
panic("TODO implement")
}
func (s *SequenceResetItem) permutation(i uint) error {
return s.sequence.Reset()
}
// Permutation sets a specific permutation for this token
func (s *SequenceResetItem) Permutation(i uint) error {
permutations := s.Permutations()
if i >= permutations {
return &token.PermutationError{
Type: token.PermutationErrorIndexOutOfBound,
}
}
return s.permutation(i)
}
// Permutations returns the number of permutations for this token
func (s *SequenceResetItem) Permutations() uint {
return 1
}
// PermutationsAll returns the number of all possible permutations for this token including its children
func (s *SequenceResetItem) PermutationsAll() uint {
return s.Permutations()
}
func (s *SequenceResetItem) String() string {
return ""
}
// ResetToken interface methods
// Reset resets the (internal) state of this token and its dependences, returns an error if the reseted state should not be used for a generation.
func (s *SequenceResetItem) Reset() error {
return s.permutation(0)
}