-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalternative.go
More file actions
54 lines (42 loc) · 934 Bytes
/
alternative.go
File metadata and controls
54 lines (42 loc) · 934 Bytes
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
package srgs
type Alternative struct {
items []Expansion
str string
currentInd int
}
func (a *Alternative) Copy(r RuleRefs) Expansion {
out := new(Alternative)
out.items = make([]Expansion, len(a.items))
for ind, e := range a.items {
out.items[ind] = e.Copy(r)
}
return out
}
func NewAlternative(items ...Expansion) *Alternative {
return &Alternative{items: items}
}
func (a *Alternative) Match(str string, mode MatchMode) {
a.str = str
a.currentInd = 0
for _, i := range a.items {
i.Match(str, mode)
}
}
func (a *Alternative) Next() (string, error) {
outErr := NoMatch
for i := a.currentInd; i < len(a.items); i++ {
var str string
var err error
str, err = a.items[i].Next()
if err == PrefixOnly {
outErr = PrefixOnly
} else if err == nil {
a.currentInd = i
return str, nil
}
}
return "", outErr
}
func (a *Alternative) Scan(p Processor) {
a.items[a.currentInd].Scan(p)
}