/
buffer.go
91 lines (78 loc) · 1.63 KB
/
buffer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package parser
import (
"fmt"
"github.com/saintfish/trie.go"
"regexp"
"strings"
"unicode/utf8"
)
type Run struct {
Start, End int
}
type Buffer struct {
input string
pos int
backPos []int
}
func newBuffer(input string, pos int) *Buffer {
if pos > len(input) {
pos = len(input)
}
return &Buffer{
input: input,
pos: pos,
backPos: make([]int, 0, 10),
}
}
func (b *Buffer) Backup() {
b.backPos = append(b.backPos, b.pos)
}
func (b *Buffer) Restore() error {
err := fmt.Errorf(
"parse error at %d, restore to %d",
b.pos, b.backPos[len(b.backPos)-1])
b.pos = b.backPos[len(b.backPos)-1]
b.backPos = b.backPos[:len(b.backPos)-1]
return err
}
func (b *Buffer) End() bool {
return b.pos >= len(b.input)
}
func (b *Buffer) Commit() Run {
run := Run{b.backPos[len(b.backPos)-1], b.pos}
b.backPos = b.backPos[:len(b.backPos)-1]
return run
}
func (b *Buffer) Run(r Run) string {
return b.input[r.Start:r.End]
}
func (b *Buffer) ConsumePrefix(prefix string) bool {
if strings.HasPrefix(b.input[b.pos:], prefix) {
b.pos += len(prefix)
return true
}
return false
}
func (b *Buffer) ConsumeRune() (rune, bool) {
if b.End() {
return 0, false
}
r, size := utf8.DecodeRuneInString(b.input[b.pos:])
b.pos += size
return r, true
}
func (b *Buffer) ConsumeTrie(t *trie.Trie) (trie.Value, bool) {
if m, found := t.MatchLongestPrefixString(b.input[b.pos:]); found {
b.pos += m.PrefixLength
return m.Value, true
}
return nil, false
}
func (b *Buffer) ConsumeRegexp(r *regexp.Regexp) bool {
index := r.FindStringIndex(b.input[b.pos:])
if index == nil {
return false
}
b.pos += (index[1] - index[0])
return true
}