-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptional.go
More file actions
40 lines (31 loc) · 774 Bytes
/
optional.go
File metadata and controls
40 lines (31 loc) · 774 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
package goleri
import "fmt"
// Optional can match one element.
type Optional struct {
element
elem Element
}
// NewOptional returns a new optional object.
func NewOptional(gid int, elem Element) *Optional {
return &Optional{
element: element{gid},
elem: elem,
}
}
func (optional *Optional) String() string {
return fmt.Sprintf("<Optional gid:%d elem:%v>", optional.gid, optional.elem)
}
func (optional *Optional) Text() string {
return fmt.Sprintf("%v", optional.elem)
}
func (optional *Optional) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
nd := newNode(optional, parent.end)
n, err := p.walk(nd, optional.elem, r, modeOptional)
if err != nil {
return nil, err
}
if n != nil {
p.appendChild(parent, nd)
}
return nd, nil
}