-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist.go
84 lines (71 loc) · 1.74 KB
/
list.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
package goleri
import "fmt"
// List must match at least min and at most max times the element and delimiter.
type List struct {
element
elem Element
delimiter Element
min int
max int
optClose bool // when true the list may end with a delimiter
}
// NewList returns a new list object.
func NewList(gid int, elem, delimiter Element, min, max int, optClose bool) *List {
return &List{
element: element{gid},
elem: elem,
delimiter: delimiter,
min: min,
max: max,
optClose: optClose,
}
}
// GetMin returns the minimal number of elements
func (list *List) GetMin() int { return list.min }
// GetMax returns the maximal number of elements
func (list *List) GetMax() int { return list.max }
// IsOptClose returns a boolean of optClose
func (list *List) IsOptClose() bool { return list.optClose }
func (list *List) String() string {
return fmt.Sprintf("<List gid:%d elem:%v delimiter:%v>", list.gid, list.elem, list.delimiter)
}
func (list *List) Text() string {
return fmt.Sprintf("%v", list.elem)
}
func (list *List) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
nd := newNode(list, parent.end)
i := 0
j := 0
for {
n, err := p.walk(nd, list.elem, r, getMode(i < list.min))
if err != nil {
return nil, err
}
if n == nil {
break
}
i++
n, err = p.walk(nd, list.delimiter, r, getMode(i < list.min))
if err != nil {
return nil, err
}
if n == nil {
break
}
j++
}
if i < list.min ||
(list.max != 0 && i > list.max) ||
((!list.optClose) && i != 0 && i == j) {
nd = nil // invalid, make sure nd is nil
} else {
p.appendChild(parent, nd)
}
return nd, nil
}
func getMode(required bool) uint8 {
if required {
return modeRequired
}
return modeOptional
}