-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepeat.go
58 lines (47 loc) · 1.14 KB
/
repeat.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
package goleri
import "fmt"
// Repeat must match at least min and at most max times the element.
type Repeat struct {
element
elem Element
min int
max int
}
// NewRepeat returns a new repeat object.
func NewRepeat(gid int, elem Element, min, max int) *Repeat {
return &Repeat{
element: element{gid},
elem: elem,
min: min,
max: max,
}
}
// GetMin returns the min
func (repeat *Repeat) GetMin() int { return repeat.min }
// GetMax returns the max
func (repeat *Repeat) GetMax() int { return repeat.max }
func (repeat *Repeat) String() string {
return fmt.Sprintf("<Repeat gid:%d elem:%v>", repeat.gid, repeat.elem)
}
func (repeat *Repeat) Text() string {
return fmt.Sprintf("%v", repeat.elem)
}
func (repeat *Repeat) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
nd := newNode(repeat, parent.end)
var i int
for i = 0; repeat.max == 0 || i < repeat.max; i++ {
n, err := p.walk(nd, repeat.elem, r, modeRequired)
if err != nil {
return nil, err
}
if n == nil {
break
}
}
if i < repeat.min {
nd = nil // invalid, make sure nd is nil
} else {
p.appendChild(parent, nd)
}
return nd, nil
}