-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokenring.go
122 lines (103 loc) · 2 KB
/
tokenring.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package scss
import (
"github.com/thijzert/go-scss/lexer"
)
// It's actually more of a buffer, but this sounds way cooler
type TokenRing struct {
l *lexer.L
buffer []*lexer.Token
index int
bts backtrackStack
eof bool
}
func NewTokenRing(l *lexer.L) *TokenRing {
rv := &TokenRing{l, make([]*lexer.Token, 0, 10), 0, newBacktrackStack(), false}
return rv
}
func (t *TokenRing) Next() *lexer.Token {
if t.eof {
return nil
} else if t.index == len(t.buffer) {
n, _ := t.l.NextToken()
if n == nil {
t.eof = true
return nil
}
t.buffer = append(t.buffer, n)
}
rv := t.buffer[t.index]
t.index++
return rv
}
func (t *TokenRing) Rewind() {
t.index--
}
// Advance the stream until we find a node that isn't one of the listed types to ignore
func (t *TokenRing) Ignore(types ...lexer.TokenType) *lexer.Token {
rv := t.Next()
for rv != nil {
ignored := false
for _, t := range types {
if rv.Type == t {
ignored = true
}
}
if !ignored {
return rv
}
rv = t.Next()
}
return rv
}
// Mark a position in the stream for later use
func (t *TokenRing) Mark() {
t.bts.push(t.index)
}
// Return to the last marked location
func (t *TokenRing) Backtrack() {
t.index = t.bts.pop()
}
// Remove the last made mark
func (t *TokenRing) Unmark() {
t.bts.pop()
}
func (t *TokenRing) EOF() bool {
return t.eof
}
func (t *TokenRing) Peek() *lexer.Token {
rv := t.Next()
t.Rewind()
return rv
}
// A stack that keeps track of your marks
type backtrackNode struct {
index int
next *backtrackNode
}
type backtrackStack struct {
start *backtrackNode
}
func newBacktrackStack() backtrackStack {
return backtrackStack{}
}
func (s *backtrackStack) push(index int) {
node := &backtrackNode{index: index}
if s.start == nil {
s.start = node
} else {
node.next = s.start
s.start = node
}
}
func (s *backtrackStack) pop() int {
if s.start == nil {
return -1
} else {
n := s.start
s.start = n.next
return n.index
}
}
func (s *backtrackStack) clear() {
s.start = nil
}