-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.go
50 lines (39 loc) · 901 Bytes
/
stack.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
package gqlyzer
// this file containing stack operation
import "errors"
func (l *Lexer) pop() rune {
tail := l.parseStack[len(l.parseStack)-1]
l.parseStack = l.parseStack[:len(l.parseStack)-1]
return tail
}
func (l *Lexer) popFlush() (string, error) {
result := ""
c := l.pop()
for c != '#' {
if len(l.parseStack) == 0 {
return "", errors.New("flush not found")
}
result = string(c) + result
c = l.pop()
}
return result, nil
}
func (l *Lexer) popCond(c rune) error {
tail := l.parseStack[len(l.parseStack)-1]
if tail != c {
return errors.New("invalid stack pop")
}
l.parseStack = l.parseStack[:len(l.parseStack)-1]
return nil
}
func (l *Lexer) push(c rune) {
l.parseStack = append(l.parseStack, c)
}
func (l *Lexer) pushFlush() {
l.parseStack = append(l.parseStack, '#')
}
//func (l *Lexer) pushString(s string) {
// for _, c := range s {
// l.push(c)
// }
//}