-
Notifications
You must be signed in to change notification settings - Fork 1
/
characters.go
452 lines (367 loc) · 12.5 KB
/
characters.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package gomme
import (
"strconv"
)
// Char parses a single character and matches it with
// a provided candidate.
func Char[Input Bytes](character rune) Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 || rune(input[0]) != character {
return Failure[Input, rune](NewError(input, string(character)), input)
}
return Success(rune(input[0]), input[1:])
}
}
// AnyChar parses any single character.
func AnyChar[Input Bytes]() Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 {
return Failure[Input, rune](NewError(input, "AnyChar"), input)
}
return Success(rune(input[0]), input[1:])
}
}
// Alpha0 parses a zero or more lowercase or uppercase alphabetic characters: a-z, A-Z.
// In the cases where the input is empty, or no terminating character is found, the parser
// returns the input as is.
func Alpha0[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Success(input, input)
}
lastAlphaPos := 0
for idx := 0; idx < len(input); idx++ {
if !IsAlpha(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastAlphaPos++
}
return Success(input[:lastAlphaPos], input[lastAlphaPos:])
}
}
// Alpha1 parses one or more lowercase or uppercase alphabetic characters: a-z, A-Z.
// In the cases where the input doesn't hold enough data, or a terminating character
// is found before any matching ones were, the parser returns an error result.
func Alpha1[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[Input, Input](NewError(input, "Alpha1"), input)
}
if !IsAlpha(rune(input[0])) {
return Failure[Input, Input](NewError(input, "Alpha1"), input)
}
lastAlphaPos := 1
for idx := 1; idx < len(input); idx++ {
if !IsAlpha(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastAlphaPos++
}
return Success(input[:lastAlphaPos], input[lastAlphaPos:])
}
}
// Alphanumeric0 parses zero or more ASCII alphabetical or numerical characters: a-z, A-Z, 0-9.
// In the cases where the input is empty, or no terminating character is found, the parser
// returns the input as is.
func Alphanumeric0[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Success(input, input)
}
lastDigitPos := 0
for idx := 0; idx < len(input); idx++ {
if !IsAlphanumeric(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastDigitPos++
}
return Success(input[:lastDigitPos], input[lastDigitPos:])
}
}
// Alphanumeric1 parses one or more alphabetical or numerical characters: a-z, A-Z, 0-9.
// In the cases where the input doesn't hold enough data, or a terminating character
// is found before any matching ones were, the parser returns an error result.
func Alphanumeric1[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[Input, Input](NewError(input, "Digit1"), input)
}
if !IsAlphanumeric(rune(input[0])) {
return Failure[Input, Input](NewError(input, "Digit1"), input)
}
lastDigitPos := 1
for idx := 1; idx < len(input); idx++ {
if !IsAlphanumeric(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastDigitPos++
}
return Success(input[:lastDigitPos], input[lastDigitPos:])
}
}
// Digit0 parses zero or more ASCII numerical characters: 0-9.
// In the cases where the input is empty, or no terminating character is found, the parser
// returns the input as is.
func Digit0[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Success(input, input)
}
lastDigitPos := 0
for idx := 0; idx < len(input); idx++ {
if !IsDigit(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastDigitPos++
}
return Success(input[:lastDigitPos], input[lastDigitPos:])
}
}
// Digit1 parses one or more numerical characters: 0-9.
// In the cases where the input doesn't hold enough data, or a terminating character
// is found before any matching ones were, the parser returns an error result.
func Digit1[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[Input, Input](NewError(input, "Digit1"), input)
}
if !IsDigit(rune(input[0])) {
return Failure[Input, Input](NewError(input, "Digit1"), input)
}
lastDigitPos := 1
for idx := 1; idx < len(input); idx++ {
if !IsDigit(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastDigitPos++
}
return Success(input[:lastDigitPos], input[lastDigitPos:])
}
}
// HexDigit0 parses zero or more ASCII hexadecimal characters: a-f, A-F, 0-9.
// In the cases where the input is empty, or no terminating character is found, the parser
// returns the input as is.
func HexDigit0[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Success(input, input)
}
lastDigitPos := 0
for idx := 0; idx < len(input); idx++ {
if !IsHexDigit(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastDigitPos++
}
return Success(input[:lastDigitPos], input[lastDigitPos:])
}
}
// HexDigit1 parses one or more ASCII hexadecimal characters: a-f, A-F, 0-9.
// In the cases where the input doesn't hold enough data, or a terminating character
// is found before any matching ones were, the parser returns an error result.
func HexDigit1[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[Input, Input](NewError(input, "HexDigit1"), input)
}
if !IsHexDigit(rune(input[0])) {
return Failure[Input, Input](NewError(input, "HexDigit1"), input)
}
lastDigitPos := 1
for idx := 1; idx < len(input); idx++ {
if !IsHexDigit(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastDigitPos++
}
return Success(input[:lastDigitPos], input[lastDigitPos:])
}
}
// Whitespace0 parses zero or more whitespace characters: ' ', '\t', '\n', '\r'.
// In the cases where the input is empty, or no terminating character is found, the parser
// returns the input as is.
func Whitespace0[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Success(input, input)
}
lastPos := 0
for idx := 0; idx < len(input); idx++ {
if !IsWhitespace(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastPos++
}
return Success(input[:lastPos], input[lastPos:])
}
}
// Whitespace1 parses one or more whitespace characters: ' ', '\t', '\n', '\r'.
// In the cases where the input doesn't hold enough data, or a terminating character
// is found before any matching ones were, the parser returns an error result.
func Whitespace1[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[Input, Input](NewError(input, "WhiteSpace1"), input)
}
if !IsWhitespace(rune(input[0])) {
return Failure[Input, Input](NewError(input, "WhiteSpace1"), input)
}
lastPos := 1
for idx := 1; idx < len(input); idx++ {
if !IsWhitespace(rune(input[idx])) {
return Success(input[:idx], input[idx:])
}
lastPos++
}
return Success(input[:lastPos], input[lastPos:])
}
}
// LF parses a line feed `\n` character.
func LF[Input Bytes]() Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 || input[0] != '\n' {
return Failure[Input, rune](NewError(input, "LF"), input)
}
return Success(rune(input[0]), input[1:])
}
}
// CR parses a carriage return `\r` character.
func CR[Input Bytes]() Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 || input[0] != '\r' {
return Failure[Input, rune](NewError(input, "CR"), input)
}
return Success(rune(input[0]), input[1:])
}
}
// CRLF parses the string `\r\n`.
func CRLF[Input Bytes]() Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) < 2 || (input[0] != '\r' || input[1] != '\n') {
return Failure[Input, Input](NewError(input, "CRLF"), input)
}
return Success(input[:2], input[2:])
}
}
// OneOf parses a single character from the given set of characters.
func OneOf[Input Bytes](collection ...rune) Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 {
return Failure[Input, rune](NewError(input, "OneOf"), input)
}
for _, c := range collection {
if rune(input[0]) == c {
return Success(rune(input[0]), input[1:])
}
}
return Failure[Input, rune](NewError(input, "OneOf"), input)
}
}
// Satisfy parses a single character, and ensures that it satisfies the given predicate.
func Satisfy[Input Bytes](predicate func(rune) bool) Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 {
return Failure[Input, rune](NewError(input, "Satisfy"), input)
}
if !predicate(rune(input[0])) {
return Failure[Input, rune](NewError(input, "Satisfy"), input)
}
return Success(rune(input[0]), input[1:])
}
}
// Space parses a space character.
func Space[Input Bytes]() Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 || input[0] != ' ' {
return Failure[Input, rune](NewError(input, "Space"), input)
}
return Success(rune(input[0]), input[1:])
}
}
// Tab parses a tab character.
func Tab[Input Bytes]() Parser[Input, rune] {
return func(input Input) Result[rune, Input] {
if len(input) == 0 || input[0] != '\t' {
return Failure[Input, rune](NewError(input, "Tab"), input)
}
return Success(rune(input[0]), input[1:])
}
}
// Int64 parses an integer from the input, and returns the part of the input that
// matched the integer.
func Int64[Input Bytes]() Parser[Input, int64] {
return func(input Input) Result[int64, Input] {
parser := Recognize(Sequence(Optional(Token[Input]("-")), Digit1[Input]()))
result := parser(input)
if result.Err != nil {
return Failure[Input, int64](NewError(input, "Int64"), input)
}
n, err := strconv.ParseInt(string(result.Output), 10, 64)
if err != nil {
return Failure[Input, int64](NewError(input, "Int64"), input)
}
return Success(n, result.Remaining)
}
}
// Int8 parses an 8-bit integer from the input,
// and returns the part of the input that matched the integer.
func Int8[Input Bytes]() Parser[Input, int8] {
return func(input Input) Result[int8, Input] {
parser := Recognize(Sequence(Optional(Token[Input]("-")), Digit1[Input]()))
result := parser(input)
if result.Err != nil {
return Failure[Input, int8](NewError(input, "Int8"), input)
}
n, err := strconv.ParseInt(string(result.Output), 10, 8)
if err != nil {
return Failure[Input, int8](NewError(input, "Int8"), input)
}
return Success(int8(n), result.Remaining)
}
}
// UInt8 parses an 8-bit integer from the input,
// and returns the part of the input that matched the integer.
func UInt8[Input Bytes]() Parser[Input, uint8] {
return func(input Input) Result[uint8, Input] {
result := Digit1[Input]()(input)
if result.Err != nil {
return Failure[Input, uint8](NewError(input, "UInt8"), input)
}
n, err := strconv.ParseUint(string(result.Output), 10, 8)
if err != nil {
return Failure[Input, uint8](NewError(input, "UInt8"), input)
}
return Success(uint8(n), result.Remaining)
}
}
// IsAlpha returns true if the rune is an alphabetic character.
func IsAlpha(c rune) bool {
return IsLowAlpha(c) || IsUpAlpha(c)
}
// IsLowAlpha returns true if the rune is a lowercase alphabetic character.
func IsLowAlpha(c rune) bool {
return c >= 'a' && c <= 'z'
}
// IsUpAlpha returns true if the rune is an uppercase alphabetic character.
func IsUpAlpha(c rune) bool {
return c >= 'A' && c <= 'Z'
}
// IsDigit returns true if the rune is a digit.
func IsDigit(c rune) bool {
return c >= '0' && c <= '9'
}
// IsAlphanumeric returns true if the rune is an alphanumeric character.
func IsAlphanumeric(c rune) bool {
return IsAlpha(c) || IsDigit(c)
}
// IsHexDigit returns true if the rune is a hexadecimal digit.
func IsHexDigit(c rune) bool {
return IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
}
// IsControl returns true if the rune is a control character.
func IsControl(c rune) bool {
return (c >= 0 && c < 32) || c == 127
}
func IsWhitespace(c rune) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}