-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlexer_slice.go
136 lines (115 loc) · 2.36 KB
/
lexer_slice.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package jsonpath
import (
"errors"
"fmt"
)
type sliceLexer struct {
lex
input []byte // the []byte being scanned.
start Pos // start position of this Item.
pos Pos // current position in the input
}
func NewSliceLexer(input []byte, initial stateFn) *sliceLexer {
l := &sliceLexer{
lex: newLex(initial),
input: input,
}
return l
}
func (l *sliceLexer) take() int {
if int(l.pos) >= len(l.input) {
return eof
}
r := int(l.input[l.pos])
l.pos += 1
return r
}
func (l *sliceLexer) takeString() error {
curPos := l.pos
inputLen := len(l.input)
if int(curPos) >= inputLen {
return errors.New("End of file where string expected")
}
cur := int(l.input[curPos])
curPos++
if cur != '"' {
l.pos = curPos
return fmt.Errorf("Expected \" as start of string instead of %#U", cur)
}
var previous, backslashCount int
for {
if int(curPos) >= inputLen {
l.pos = curPos
return errors.New("End of file where string expected")
}
cur := int(l.input[curPos])
curPos++
if cur == '"' {
if previous == noValue || backslashCount % 2 == 0 {
break
} else {
l.take()
}
}
if cur == '\\' {
backslashCount++
} else {
backslashCount = 0
}
previous = cur
}
l.pos = curPos
return nil
}
func (l *sliceLexer) peek() int {
if int(l.pos) >= len(l.input) {
return eof
}
return int(l.input[l.pos])
}
func (l *sliceLexer) emit(t int) {
l.setItem(t, l.start, l.input[l.start:l.pos])
l.hasItem = true
// Ignore whitespace after this token
for int(l.pos) < len(l.input) {
r := l.input[l.pos]
if r == ' ' || r == '\t' || r == '\r' || r == '\n' {
l.pos++
} else {
break
}
}
l.start = l.pos
}
func (l *sliceLexer) setItem(typ int, pos Pos, val []byte) {
l.item.typ = typ
l.item.pos = pos
l.item.val = val
}
func (l *sliceLexer) ignore() {
l.start = l.pos
}
func (l *sliceLexer) next() (*Item, bool) {
for {
if l.currentStateFn == nil {
break
}
l.currentStateFn = l.currentStateFn(l, &l.stack)
if l.hasItem {
l.hasItem = false
return &l.item, true
}
}
return &l.item, false
}
func (l *sliceLexer) errorf(format string, args ...interface{}) stateFn {
l.setItem(lexError, l.start, []byte(fmt.Sprintf(format, args...)))
l.start = l.pos
l.hasItem = true
return nil
}
func (l *sliceLexer) reset() {
l.start = 0
l.pos = 0
l.lex = newLex(l.initialState)
}