-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequence.go
More file actions
70 lines (54 loc) · 1.15 KB
/
sequence.go
File metadata and controls
70 lines (54 loc) · 1.15 KB
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
package srgs
// Sequence is any sequence of legal expansions (see https://www.w3.org/TR/speech-grammar/#S2.3)
type Sequence struct {
exps []Expansion
str string
mode MatchMode
nextInd int
}
// Implements Expansion Copy method
func (s *Sequence) Copy(r RuleRefs) Expansion {
out := &Sequence{
exps: make([]Expansion, len(s.exps)),
str: s.str,
mode: s.mode,
nextInd: s.nextInd,
}
for ind, e := range s.exps {
out.exps[ind] = e.Copy(r)
}
return out
}
// Implements Expansion Match method
func (s *Sequence) Match(str string, mode MatchMode) {
s.str = str
s.mode = mode
s.nextInd = 0
s.exps[0].Match(str, mode)
}
// Implements Expansion Next method
func (s *Sequence) Next() (string, error) {
if s.nextInd < 0 {
return "", NoMatch
}
var str string
var err error
for i := s.nextInd; i < len(s.exps); i++ {
str, err = s.exps[i].Next()
if err != nil {
s.nextInd--
return s.Next()
}
if i+1 < len(s.exps) {
s.nextInd = i + 1
s.exps[s.nextInd].Match(str, s.mode)
}
}
return str, err
}
// Implements Expansion Scan method
func (s *Sequence) Scan(p Processor) {
for _, exp := range s.exps {
exp.Scan(p)
}
}