-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
52 lines (44 loc) · 1021 Bytes
/
parser.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
package goleri
import (
"regexp"
"unicode"
)
type parser struct {
s string
l int
kwc map[int]string
reKeywords *regexp.Regexp
expect *expecting
}
func newParser(s string, reKeywords *regexp.Regexp) *parser {
return &parser{
s: s,
l: len(s),
kwc: make(map[int]string),
reKeywords: reKeywords,
expect: newExpecting(),
}
}
func (p *parser) walk(parent *Node, elem Element, r *ruleStore, mode uint8) (*Node, error) {
for parent.end < p.l && unicode.IsSpace(rune(p.s[parent.end])) {
parent.end++
}
/* set expecting mode */
p.expect.setMode(parent.start, mode)
return elem.parse(p, parent, r)
}
func (p *parser) getKeyword(pos int) string {
if s, ok := p.kwc[pos]; ok {
return s
}
s := p.reKeywords.FindString(p.s[pos:])
p.kwc[pos] = s
return s
}
func (p *parser) appendChild(parent, child *Node) {
if child.end > p.expect.pos {
p.expect.empty()
}
parent.end = child.end
parent.children = append(parent.children, child)
}