-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
180 lines (162 loc) · 4.26 KB
/
scanner.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
package syrup
import (
"fmt"
"math/big"
"strings"
)
type scanState uint8
const (
scanFindToken scanState = iota
scanTokenLen
scanString
scanFirstInt
scanInt
scanFloat64
scanFloat32
scanSymbol
scanByteArr
)
type op uint8
const (
noop op = iota
valBoolop
valByteArrOp
valSymbolOp
valStringOp
valIntOp
valFloat32Op
valFloat64Op
openListOp
openDictOp
openSetOp
openRecordOp
closeListOp
closeDictOp
closeSetOp
closeRecordOp
)
type scanner struct {
enc *Encoding
s scanState
buf strings.Builder
nlen uint64
}
// Process handles one byte of input at a time, processing the syrup encoding
// into a value.
//
// If 'oper' is a noop, the client code can proceed to call process again.
//
// If 'oper' is a 'val' op, the client code must obtain the accumulated value
// using the correct accessor.
//
// If 'oper' is an 'open' or 'close' op, the client code is recommended to
// create or finish populating an appropriate container.
func (s *scanner) Process(b byte) (oper op, err error) {
var include bool
next := s.s
// 1. Determine State Transition
//
// Find out what the next state of our byte-interpretation (scanState)
// is. Determine the high level operation that needs to happen, if any
// need to. Determine whether to include the examined byte as a value
// in our buffer: not all semantic bytes that are examined have value,
// for example the prototypical 't' for true is both a semantic and
// value byte, 'i' denoting an int is only semantically meaningful but
// provides no value to the integer being described.
switch s.s {
case scanFindToken:
next, oper, include, err = s.enc.mustFindToken(b)
case scanTokenLen:
next, oper, include, err = s.enc.scanTokenLen(b)
case scanSymbol:
next, oper, include = s.processLengthDeterminedType(valSymbolOp)
case scanString:
next, oper, include = s.processLengthDeterminedType(valStringOp)
case scanByteArr:
next, oper, include = s.processLengthDeterminedType(valByteArrOp)
case scanInt:
next, oper, include, err = s.enc.scanIntToken(b)
case scanFirstInt:
next, oper, include, err = s.enc.scanFirstIntToken(b)
case scanFloat64:
next, oper, include, err = s.enc.scanFloat64Token(b)
case scanFloat32:
next, oper, include, err = s.enc.scanFloat32Token(b)
default:
err = fmt.Errorf("syrup unknown scanstate: %d", s.s)
}
if err != nil {
return
}
// 2. Include the bytes into the buffer if necessary.
if include {
// strings.Builder.WriteByte always returns 'nil'
_ = s.buf.WriteByte(b)
}
// 3. In the special case of parsing a token-length, set our internal
// buffer and length counters appropriately.
//
// Unfortunately this is a leak between the encoding and this generic
// scanner.
if s.s == scanTokenLen && next != scanTokenLen {
if oper, err = s.processParsedLen(next); err != nil {
return
}
}
// 4. Finally, transition to the next state.
s.s = next
// 5. If a value-op was returned, it is up to the caller to ensure they
// call one of the value functions, which has the side effect of
// clearing the internal buffer.
return
}
func (s *scanner) processParsedLen(next scanState) (oper op, err error) {
oper, s.nlen, err = s.enc.parseLen(s.buf.String(), next)
s.buf.Reset()
return
}
func (s *scanner) processLengthDeterminedType(maybeOp op) (next scanState, oper op, include bool) {
next = s.s
include = true
s.nlen--
if s.nlen == 0 {
next = scanFindToken
oper = maybeOp
}
return
}
func (s *scanner) Bool() (bool, error) {
b, err := s.enc.boolVal([]byte(s.buf.String()))
s.buf.Reset()
return b, err
}
func (s *scanner) Bytes() ([]byte, error) {
b := []byte(s.buf.String())
s.buf.Reset()
return b, nil
}
func (s *scanner) Symbol() (Symbol, error) {
sym, err := s.enc.symbolVal([]byte(s.buf.String()))
s.buf.Reset()
return sym, err
}
func (s *scanner) String() (string, error) {
str, err := s.enc.stringVal([]byte(s.buf.String()))
s.buf.Reset()
return str, err
}
func (s *scanner) Int64() (int64, *big.Int, error) {
i, b, err := s.enc.int64Val([]byte(s.buf.String()))
s.buf.Reset()
return i, b, err
}
func (s *scanner) Float32() (float32, error) {
f, err := s.enc.float32Val([]byte(s.buf.String()))
s.buf.Reset()
return f, err
}
func (s *scanner) Float64() (float64, error) {
f, err := s.enc.float64Val([]byte(s.buf.String()))
s.buf.Reset()
return f, err
}