-
Notifications
You must be signed in to change notification settings - Fork 1
/
sel.go
227 lines (203 loc) · 4.73 KB
/
sel.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package lzjson
import (
"fmt"
"strings"
"unicode/utf8"
)
// selItem contains information of token in a selector
type selItem struct {
typ selItemType
val string
}
// selItemType represents token type
type selItemType int
const (
selItemError selItemType = iota // lex error
selItemDot // dot symbol
selItemSpace // space
selItemRightBrac // ']'
selItemLeftBrac // '['
selItemProp // property name of json object
selItemNumber // numeric value / array key
selItemString // string
selItemEnd // end of selector string
)
const (
charCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
charSmallCap = "abcdefghijklmnoperstuvwxyz"
charNumeric = "0123456789"
)
const eof = -1
// lexSel returns a lexer for the selector string
func lexSel(input string) *selLexer {
return &selLexer{
input: input,
items: make(chan selItem),
}
}
// selLexer helps tokenize a selector string
type selLexer struct {
input string
state selStateFn
pos int
start int
width int
items chan selItem
}
// next returns the next rune in the input.
func (l *selLexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
// backup steps back one rune. Can only be called once per call of next.
func (l *selLexer) backup() {
l.pos -= l.width
}
// peek returns but does not consume the next rune in the input.
func (l *selLexer) peek() rune {
r := l.next()
l.backup()
return r
}
// emit passes an item back to the client.
func (l *selLexer) emit(t selItemType) {
l.items <- selItem{t, l.input[l.start:l.pos]}
l.start = l.pos
}
// ignore skips over the pending input before this point.
func (l *selLexer) ignore() {
l.start = l.pos
}
// errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.nextItem.
func (l *selLexer) errorf(format string, args ...interface{}) selStateFn {
l.items <- selItem{selItemError, fmt.Sprintf(format, args...)}
return nil
}
// run runs the state machine for the lexer.
func (l *selLexer) run() {
for l.state = selLexText; l.state != nil; {
l.state = l.state(l)
}
close(l.items)
}
// nextItem returns the next item from the input.
// Called by the parser, not in the lexing goroutine.
func (l *selLexer) nextItem() selItem {
item := <-l.items
return item
}
// accept consumes the next rune if it's from the valid set.
func (l *selLexer) accept(valid string) bool {
if strings.IndexRune(valid, l.next()) >= 0 {
return true
}
l.backup()
return false
}
// acceptRun consumes a run of runes from the valid set.
func (l *selLexer) acceptRun(valid string) {
for strings.IndexRune(valid, l.next()) >= 0 {
}
l.backup()
}
// selStateFn represents the state of the scanner as a function that returns the next state.
type selStateFn func(*selLexer) selStateFn
// selLexText process selector and determine what selStateFn
// to process the upcoming string
func selLexText(l *selLexer) selStateFn {
next := l.next()
switch next {
case '.':
l.emit(selItemDot)
return selLexProc
case '[':
l.emit(selItemLeftBrac)
return selLexInsideBrac
case ']':
l.emit(selItemRightBrac)
return selLexText
case eof:
break
default:
return selLexProc
}
if l.pos > l.start {
l.emit(selItemProp)
}
l.emit(selItemEnd)
return nil
}
// selLexProc process selector string like object property
// name until reaching non-alphanumerical character
func selLexProc(l *selLexer) selStateFn {
l.acceptRun(charCap + charSmallCap + charNumeric)
l.emit(selItemProp)
return selLexText
}
func selLexInsideBrac(l *selLexer) selStateFn {
for {
switch l.next() {
case ']':
l.backup()
if l.pos > l.start {
l.emit(selItemNumber)
}
return selLexText
case '"':
l.ignore()
return selLexInsideDoubleQuoteString
case '\'':
l.ignore()
return selLexInsideQuoteString
case eof:
return l.errorf("unclosed bracket")
}
}
}
func selLexInsideQuoteString(l *selLexer) selStateFn {
for {
switch l.next() {
case '\\':
if l.peek() == '\'' {
l.next()
}
case '\'':
l.backup()
if l.pos > l.start {
l.emit(selItemString)
}
l.next()
l.ignore()
return selLexText
case eof:
return l.errorf("unclosed single quoted string")
}
}
}
func selLexInsideDoubleQuoteString(l *selLexer) selStateFn {
for {
switch l.next() {
case '\\':
if l.peek() == '"' {
l.next()
}
case '"':
l.backup()
if l.pos > l.start {
l.emit(selItemString)
}
l.next()
l.ignore()
return selLexText
case eof:
return l.errorf("unclosed single quoted string")
}
}
}