-
Notifications
You must be signed in to change notification settings - Fork 3
/
reader.go
333 lines (292 loc) · 8.56 KB
/
reader.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package brigodier
import (
"errors"
"fmt"
"strconv"
"strings"
)
// StringReader is a string reader used for input parsing.
type StringReader struct {
Cursor int
String string
}
// ReaderError indicates a StringReader error.
type ReaderError struct {
Err error
Reader *StringReader
}
// ReaderInvalidValueError indicates an invalid value error.
type ReaderInvalidValueError struct {
Type ArgumentType // The expected value type
Value string
Err error // Optional underlying error
}
// Unwrap implements errors.Unwrap.
func (e *ReaderInvalidValueError) Unwrap() error { return e.Err }
// Error implements error.
func (e *ReaderInvalidValueError) Error() string {
if e.Err != nil {
return e.Err.Error()
}
return fmt.Sprintf("read invalid value %q for type %q", e.Value, e.Type)
}
// Unwrap implements errors.Unwrap.
func (e *ReaderError) Unwrap() error { return e.Err }
func (e *ReaderError) Error() string { return e.Err.Error() }
// CanRead indicates whether a next rune can be read to a call to Read.
func (r *StringReader) CanRead() bool { return r.CanReadLen(1) }
// CanReadLen indicates whether the next length runes can be read.
func (r *StringReader) CanReadLen(length int) bool { return r.Cursor+length <= len(r.String) }
// Peek returns the next rune without incrementing the Cursor.
func (r *StringReader) Peek() rune { return rune(r.String[r.Cursor]) }
// Skip increments the Cursor.
func (r *StringReader) Skip() { r.Cursor++ }
// ReadBool tries to read a bool.
func (r *StringReader) ReadBool() (bool, error) {
start := r.Cursor
value, err := r.ReadString()
if err != nil {
return false, err
}
if len(value) == 0 {
return false, &CommandSyntaxError{Err: &ReaderError{
Err: ErrReaderExpectedBool,
Reader: r,
}}
}
if strings.EqualFold(value, "true") {
return true, nil
} else if strings.EqualFold(value, "false") {
return false, nil
}
r.Cursor = start
return false, &CommandSyntaxError{Err: &ReaderError{
Err: &ReaderInvalidValueError{
Type: Bool,
Value: value,
},
Reader: r,
}}
}
// Read returns the next rune.
func (r *StringReader) Read() rune {
c := r.String[r.Cursor]
r.Cursor++
return rune(c)
}
// ReadString returns the next quoted or unquoted string.
func (r *StringReader) ReadString() (string, error) {
if !r.CanRead() {
return "", nil
}
next := r.Peek()
if IsQuotedStringStart(next) {
r.Skip()
return r.ReadStringUntil(next)
}
return r.ReadUnquotedString(), nil
}
var (
// ErrReaderInvalidEscape indicates an invalid escape error.
ErrReaderInvalidEscape = errors.New("read invalid escape character")
// ErrReaderExpectedStartOfQuote occurs when a start quote is missing.
ErrReaderExpectedStartOfQuote = errors.New("reader expected start of quote")
// ErrReaderExpectedEndOfQuote occurs when an end quote is missing.
ErrReaderExpectedEndOfQuote = errors.New("reader expected end of quote")
)
// ReadStringUntil reads a string until the terminator rune.
func (r *StringReader) ReadStringUntil(terminator rune) (string, error) {
var (
result strings.Builder
escaped = false
)
for r.CanRead() {
c := r.Read()
if escaped {
if c == terminator || c == SyntaxEscape {
result.WriteRune(c)
escaped = false
} else {
r.Cursor = r.Cursor - 1
return "", &CommandSyntaxError{Err: &ReaderError{
Err: &ReaderInvalidValueError{
Value: string(c),
Err: ErrReaderInvalidEscape,
},
Reader: r,
}}
}
} else if c == SyntaxEscape {
escaped = true
} else if c == terminator {
return result.String(), nil
} else {
result.WriteRune(c)
}
}
return "", &CommandSyntaxError{Err: &ReaderError{
Err: ErrReaderExpectedEndOfQuote,
Reader: r,
}}
}
// ReadUnquotedString reads an unquoted string.
func (r *StringReader) ReadUnquotedString() string {
start := r.Cursor
for r.CanRead() && IsAllowedInUnquotedString(r.Peek()) {
r.Skip()
}
return r.String[start:r.Cursor]
}
// ReadQuotedString reads a quoted string.
func (r *StringReader) ReadQuotedString() (string, error) {
if !r.CanRead() {
return "", nil
}
next := r.Peek()
if !IsQuotedStringStart(next) {
return "", &CommandSyntaxError{Err: &ReaderError{
Err: ErrReaderExpectedStartOfQuote,
Reader: r,
}}
}
r.Skip()
return r.ReadStringUntil(next)
}
var (
// ErrReaderExpectedBool occurs when the reader expected a bool.
ErrReaderExpectedBool = errors.New("reader expected bool")
// ErrReaderExpectedFloat occurs when the reader expected a float.
ErrReaderExpectedFloat = errors.New("reader expected float")
// ErrReaderExpectedInt occurs when the reader expected a int.
ErrReaderExpectedInt = errors.New("reader expected int")
// ErrReaderInvalidInt occurs when the reader read an invalid int value.
ErrReaderInvalidInt = errors.New("read invalid int")
// ErrReaderInvalidFloat occurs when the reader read an invalid int float.
ErrReaderInvalidFloat = errors.New("read invalid float")
)
// ReadInt tries to read an int32.
func (r *StringReader) ReadInt() (int, error) {
i, err := r.ReadInt32()
return int(i), err
}
// ReadInt32 tries to read an int32.
func (r *StringReader) ReadInt32() (int32, error) {
i, err := r.readInt(32)
return int32(i), err
}
// ReadInt64 tries to read an int64.
func (r *StringReader) ReadInt64() (int64, error) { return r.readInt(64) }
func (r *StringReader) readInt(bitSize int) (int64, error) {
start := r.Cursor
for r.CanRead() && IsAllowedNumber(r.Peek()) {
r.Skip()
}
number := r.String[start:r.Cursor]
if number == "" {
return 0, &CommandSyntaxError{Err: &ReaderError{
Err: ErrReaderExpectedInt,
Reader: r,
}}
}
i, err := strconv.ParseInt(number, 0, bitSize)
if err != nil {
r.Cursor = start
return 0, &CommandSyntaxError{Err: &ReaderError{
Err: &ReaderInvalidValueError{
Value: number,
Err: fmt.Errorf("%w (%q): %v", ErrReaderInvalidInt, number, err),
},
Reader: r,
}}
}
return i, nil
}
// ReadFloat32 tries to read a float32.
func (r *StringReader) ReadFloat32() (float32, error) {
f, err := r.readFloat(32)
return float32(f), err
}
// ReadFloat64 tries to read a float64.
func (r *StringReader) ReadFloat64() (float64, error) {
return r.readFloat(64)
}
func (r *StringReader) readFloat(bitSize int) (float64, error) {
start := r.Cursor
for r.CanRead() && IsAllowedNumber(r.Peek()) {
r.Skip()
}
number := r.String[start:r.Cursor]
if number == "" {
return 0, &CommandSyntaxError{Err: &ReaderError{
Err: ErrReaderExpectedFloat,
Reader: r,
}}
}
f, err := strconv.ParseFloat(number, bitSize)
if err != nil {
r.Cursor = start
return 0, &CommandSyntaxError{Err: &ReaderError{
Err: &ReaderInvalidValueError{
Value: number,
Err: fmt.Errorf("%w (%q): %v", ErrReaderInvalidFloat, number, err),
},
Reader: r,
}}
}
return f, nil
}
// Remaining returns the remaining string beginning at the current Cursor
func (r *StringReader) Remaining() string { return r.String[r.Cursor:] }
// RemainingLen returns the remaining string length beginning at the current Cursor
func (r *StringReader) RemainingLen() int { return len(r.String) - r.Cursor }
const (
// SyntaxDoubleQuote is a double quote.
SyntaxDoubleQuote rune = '"'
// SyntaxSingleQuote is a single quote.
SyntaxSingleQuote rune = '\''
// SyntaxEscape is an escape.
SyntaxEscape rune = '\\'
)
// IsAllowedNumber indicated whether c is an allowed number rune.
func IsAllowedNumber(c rune) bool { return c >= '0' && c <= '9' || c == '.' || c == '-' }
// IsQuotedStringStart indicated whether c is the start of a quoted string.
func IsQuotedStringStart(c rune) bool {
return c == SyntaxDoubleQuote || c == SyntaxSingleQuote
}
// IsAllowedInUnquotedString indicated whether c is an allowed rune in an unquoted string.
func IsAllowedInUnquotedString(c rune) bool {
return c >= '0' && c <= '9' ||
c >= 'A' && c <= 'Z' ||
c >= 'a' && c <= 'z' ||
c == '_' || c == '-' ||
c == '.' || c == '+'
}
// StringRange stores a range indicating the start and end of a string
type StringRange struct{ Start, End int }
// IsEmpty indicated whether Start and End is equal.
func (r *StringRange) IsEmpty() bool {
return r.Start == r.End
}
// Copy copies the StringRange.
func (r StringRange) Copy() StringRange { return r }
// Get returns the substring of s from Start to End.
func (r *StringRange) Get(s string) string { return s[r.Start:r.End] }
// EncompassingRange returns the min and max StringRange of two ranges.
func EncompassingRange(r1, r2 *StringRange) *StringRange {
return &StringRange{
Start: min(r1.Start, r2.Start),
End: max(r1.End, r2.End),
}
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func max(x, y int) int {
if x > y {
return x
}
return y
}