-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregex.go
47 lines (40 loc) · 946 Bytes
/
regex.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
package goleri
import (
"fmt"
"regexp"
)
// Regex matches a regular expression.
type Regex struct {
element
regex *regexp.Regexp
}
// NewRegex returns a new keyword object.
func NewRegex(gid int, regex *regexp.Regexp) *Regex {
return &Regex{
element: element{gid},
regex: regex,
}
}
// GetRegex returns the regular expression
func (regex *Regex) GetRegex() *regexp.Regexp {
return regex.regex
}
func (regex *Regex) String() string {
return fmt.Sprintf("<Regex gid:%d regex:%v>", regex.gid, regex.regex)
}
func (regex *Regex) Text() string {
return fmt.Sprintf("%v", regex.regex)
}
func (regex *Regex) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var nd *Node
s := p.s[parent.end:]
m := regex.regex.FindStringIndex(s)
if m != nil && m[0] == 0 {
nd = newNode(regex, parent.end)
nd.end = parent.end + m[1]
p.appendChild(parent, nd)
} else {
p.expect.update(regex, parent.end)
}
return nd, nil
}