-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyword.go
57 lines (47 loc) · 1.21 KB
/
keyword.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
package goleri
import (
"fmt"
"strings"
)
// Keyword matches a keyword.
type Keyword struct {
element
keyword string
ignCase bool
}
// NewKeyword returns a new keyword object.
func NewKeyword(gid int, keyword string, ignCase bool) *Keyword {
return &Keyword{
element: element{gid},
keyword: keyword,
ignCase: ignCase,
}
}
// GetKeyword returns the keyword
func (keyword *Keyword) GetKeyword() string { return keyword.keyword }
// IsIgnCase returns a boolean ignore case
func (keyword *Keyword) IsIgnCase() bool { return keyword.ignCase }
func (keyword *Keyword) String() string {
return fmt.Sprintf("<Keyword gid:%d keyword:%v>", keyword.gid, keyword.keyword)
}
func (keyword *Keyword) Text() string {
return fmt.Sprintf(keyword.keyword)
}
func (keyword *Keyword) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var match bool
var nd *Node
s := p.getKeyword(parent.end)
if keyword.ignCase {
match = strings.EqualFold(s, keyword.keyword)
} else {
match = strings.Compare(s, keyword.keyword) == 0
}
if match {
nd = newNode(keyword, parent.end)
nd.end = parent.end + len(keyword.keyword)
p.appendChild(parent, nd)
} else {
p.expect.update(keyword, parent.end)
}
return nd, nil
}