-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.go
54 lines (45 loc) · 985 Bytes
/
token.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
package goleri
import (
"fmt"
)
// Token matches a token.
type Token struct {
element
token string
l int
}
// NewToken returns a new token object.
func NewToken(gid int, token string) *Token {
return &Token{
element: element{gid},
token: token,
l: len(token),
}
}
// GetToken returns the token
func (token *Token) GetToken() string { return token.token }
func (token *Token) String() string {
return fmt.Sprintf("<Token gid:%d token:%v>", token.gid, token.token)
}
func (token *Token) Text() string {
return fmt.Sprintf(token.token)
}
func (token *Token) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var nd *Node
match := true
for i, j := 0, parent.end; i < token.l; i++ {
if j == p.l || p.s[j] != token.token[i] {
match = false
break
}
j++
}
if match {
nd = newNode(token, parent.end)
nd.end = parent.end + token.l
p.appendChild(parent, nd)
} else {
p.expect.update(token, parent.end)
}
return nd, nil
}