-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
681 lines (578 loc) · 21.4 KB
/
parser.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// Package parser implements simple, yet expressive mechanisms for [combinatorial parsing] in Go.
//
// [combinatorial parsing]: https://en.wikipedia.org/wiki/Parser_combinator
package parser
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
// Parser is the core parsing function that all parser functions return, they can be combined and composed
// to parse complex grammars.
//
// Each Parser is generic over type T and returns the parsed value from the input, the remaining unparsed input and an error.
type Parser[T any] func(input string) (value T, remainder string, err error)
// Take returns a [Parser] that consumes n utf-8 chars from the input.
//
// If n is less than or equal to 0, or greater than the number of utf-8 chars in the input, an error will be returned.
func Take(n int) Parser[string] {
return func(input string) (string, string, error) {
if n <= 0 {
return "", "", fmt.Errorf("Take: n must be a non-zero positive integer, got %d", n)
}
if input == "" {
return "", "", errors.New("Take: cannot take from empty input")
}
if !utf8.ValidString(input) {
return "", "", errors.New("Take: input not valid utf-8")
}
runes := 0 // How many runes we've seen
end := 0 // The starting byte position of the nth rune
for pos, char := range input {
runes++
if runes == n {
// We've hit our limit, pos is the starting byte of the nth rune
// but we want to return the entire string *including* the nth rune
// so the actual end is pos + the byte length of the nth rune
end = pos + utf8.RuneLen(char)
break
}
}
if runes < n {
// We've exhausted the entire input before scanning n runes i.e the input
// was not long enough
return "", "", fmt.Errorf("Take: requested n (%d) chars but input had only %d utf-8 chars", n, runes)
}
return input[:end], input[end:], nil
}
}
// Exact returns a [Parser] that consumes an exact, case-sensitive string from the input.
//
// If the string is not present at the beginning of the input, an error will be returned.
//
// An empty match string or empty input (i.e. "") will also return an error.
//
// Exact is case-sensitive, if you need a case-insensitive match, use [ExactCaseInsensitive] instead.
func Exact(match string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("Exact: cannot match on empty input")
}
if !utf8.ValidString(input) {
return "", "", errors.New("Exact: input not valid utf-8")
}
if match == "" {
return "", "", errors.New("Exact: match must not be empty")
}
start := strings.Index(input, match)
if start != 0 {
return "", "", fmt.Errorf("Exact: match (%s) not in input", match)
}
return match, input[len(match):], nil
}
}
// ExactCaseInsensitive returns a [Parser] that consumes an exact, case-insensitive string from the input.
//
// If the string is not present at the beginning of the input, an error will be returned.
//
// An empty match string or empty input (i.e. "") will also return an error.
//
// ExactCaseInsensitive is case-insensitive, if you need a case-sensitive match, use [Exact] instead.
func ExactCaseInsensitive(match string) Parser[string] {
return func(input string) (string, string, error) {
inputLen := len(input)
if inputLen == 0 {
return "", "", errors.New("ExactCaseInsensitive: cannot match on empty input")
}
if !utf8.ValidString(input) {
return "", "", errors.New("ExactCaseInsensitive: input not valid utf-8")
}
matchLen := len(match)
if matchLen == 0 {
return "", "", errors.New("ExactCaseInsensitive: match must not be empty")
}
// Serves two purposes: It's a quick check that we'd never find a match and it guards
// the input slicing below
if matchLen > inputLen {
return "", "", fmt.Errorf("ExactCaseInsensitive: match (%s) not in input", match)
}
// The beginning of input where the match string could possibly be
potentialMatch := input[:matchLen]
if !strings.EqualFold(potentialMatch, match) {
return "", "", fmt.Errorf("ExactCaseInsensitive: match (%s) not in input", match)
}
return potentialMatch, input[matchLen:], nil
}
}
// Char returns a [Parser] that consumes a single exact, case-sensitive utf-8 character from the input.
//
// If the first char in the input is not the requested char, an error will be returned.
func Char(char rune) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("Char: input text is empty")
}
r, width := utf8.DecodeRuneInString(input)
if r == utf8.RuneError {
return "", "", errors.New("Char: input not valid utf-8")
}
if r != char {
return "", "", fmt.Errorf("Char: requested char (%s) not found in input", string(char))
}
return input[:width], input[width:], nil
}
}
// TakeWhile returns a [Parser] that continues consuming characters so long as the predicate returns true,
// the parsing stops as soon as the predicate returns false for a particular character. The last character
// for which the predicate returns true is captured; that is, TakeWhile is inclusive.
//
// TakeWhile can be thought of as the inverse of [TakeUntil].
//
// If the input is empty or predicate == nil, an error will be returned.
//
// If the predicate doesn't return false for any char in the input, the entire input is returned as the value
// with no remainder.
//
// A predicate that never returns true will return an error.
func TakeWhile(predicate func(r rune) bool) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("TakeWhile: input text is empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("TakeWhile: input not valid utf-8")
}
if predicate == nil {
return "", "", errors.New("TakeWhile: predicate must be a non-nil function")
}
if strings.IndexFunc(input, predicate) == -1 {
return "", "", errors.New("TakeWhile: predicate never returned true")
}
end := 0 // Byte position of last rune that the predicate returns true for
broken := false // Whether the predicate ever returned false so we broke the loop
for pos, char := range input {
end = pos
if !predicate(char) {
broken = true
break
}
}
if !broken {
// If we didn't break the loop, the predicate never returned false
// so just return the whole input
return input, "", nil
}
return input[:end], input[end:], nil
}
}
// TakeUntil returns a [Parser] that continues taking characters until the predicate returns true,
// the parsing stops as soon as the predicate returns true for a particular character. The last character
// for which the predicate returns false is captured; that is, TakeUntil is inclusive.
//
// TakeUntil can be thought of as the inverse of [TakeWhile].
//
// If the input is empty or predicate == nil, an error will be returned.
//
// If the predicate never returns true, the entire input will be returned as the value
// with no remainder.
//
// A predicate that never returns false will return an error.
func TakeUntil(predicate func(r rune) bool) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("TakeUntil: input text is empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("TakeUntil: input not valid utf-8")
}
if predicate == nil {
return "", "", errors.New("TakeUntil: predicate must be a non-nil function")
}
// strings.IndexFunc returns the index of the first char for which the predicate
// returns true, we want the opposite to test if it ever returns false
notPred := func(r rune) bool { return !predicate(r) }
if strings.IndexFunc(input, notPred) == -1 {
return "", "", errors.New("TakeUntil: predicate never returned false")
}
end := 0 // Byte position of last rune that the predicate returns false for
broken := false // Whether the predicate ever returned true so we broke the loop
for pos, char := range input {
end = pos
if predicate(char) {
broken = true
break
}
}
if !broken {
return input, "", nil
}
return input[:end], input[end:], nil
}
}
// TakeWhileBetween returns a [Parser] that recognises the longest (lower <= len <= upper) sequence
// of utf-8 characters for which the predicate returns true.
//
// Any of the following conditions will return an error:
// - input is empty
// - input is not valid utf-8
// - predicate is nil
// - lower < 0
// - lower > upper
// - predicate never returns true
// - predicate matched some chars but less than lower limit
func TakeWhileBetween(lower, upper int, predicate func(r rune) bool) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("TakeWhileBetween: input text is empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("TakeWhileBetween: input not valid utf-8")
}
if predicate == nil {
return "", "", errors.New("TakeWhileBetween: predicate must be a non-nil function")
}
if lower < 0 {
return "", "", fmt.Errorf("TakeWhileBetween: lower limit (%d) not allowed, must be positive integer", lower)
}
if lower > upper {
return "", "", fmt.Errorf("TakeWhileBetween: invalid range, lower (%d) must be < upper (%d)", lower, upper)
}
// Does the predicate ever return true? Quick failure case
if strings.IndexFunc(input, predicate) == -1 {
return "", "", errors.New("TakeWhileBetween: predicate never returned true")
}
index := 0 // Index of last char for which predicate returns true
for pos, char := range input {
if !predicate(char) {
break
}
// Add the byte width of the char in question because the next char is the
// first one where predicate(char) == false, that's where we want to cut
// the string
runeLen := utf8.RuneLen(char)
if runeLen != -1 {
index = pos + utf8.RuneLen(char)
}
}
// If we have an index, our job now is to return whichever is longest out of
// the sequence for which the predicate returned true, or the entire input
// up to the upper limit of chars
startToIndex := input[:index]
n := utf8.RuneCountInString(startToIndex)
if n < lower {
// The number of chars for which the predicate returned true is less
// than our lower limit, which is an error
return "", "", fmt.Errorf("TakeWhileBetween: predicate matched only %d chars (%s), below lower limit (%d)", n, startToIndex, lower)
}
if n > upper {
// The sequence of chars for which the predicate returned true is
// longer than our upper limit, so cut if off at upper utf-8 chars
runes := 0 // How many runes we've scanned through
cutOff := 0 // Index of where to cut the string off
for pos, char := range startToIndex {
runes++
if runes == upper {
// Add the byte width of the char in question because we want to
// include it in the slice and pos is just the starting byte position
cutOff = pos + utf8.RuneLen(char)
}
}
return input[:cutOff], input[cutOff:], nil
}
// If we get here, we know that the number of utf-8 chars for which the predicate
// returned true is already less than our upper limit, so we can just use the
// index from earlier
return input[:index], input[index:], nil
}
}
// TakeTo returns a [Parser] that consumes characters until it first hits an exact string.
//
// If the input is empty or the exact string is not in the input, an error will be returned.
//
// The value will contain everything from the start of the input up to the first occurrence of
// match, and the remainder will contain the match and everything thereafter.
func TakeTo(match string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("TakeTo: input text is empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("TakeTo: input not valid utf-8")
}
if match == "" {
return "", "", errors.New("TakeTo: match must not be empty")
}
start := strings.Index(input, match)
if start == -1 {
return "", "", fmt.Errorf("TakeTo: match (%s) not in input", match)
}
return input[:start], input[start:], nil
}
}
// OneOf returns a [Parser] that recognises one of the provided characters from the start of input.
//
// If you want to match anything other than the provided char set, use [NoneOf].
//
// If the input or chars is empty, an error will be returned.
// Likewise if none of the chars was recognised.
func OneOf(chars string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("OneOf: input text is empty")
}
if chars == "" {
return "", "", errors.New("OneOf: chars must not be empty")
}
r, width := utf8.DecodeRuneInString(input)
if r == utf8.RuneError {
return "", "", errors.New("OneOf: input not valid utf-8")
}
found := false // Whether we've actually found a match
for _, char := range chars {
if char == r {
// Found it
found = true
break
}
}
// If we get here and found is still false, the first char in the input didn't match
// any of our given chars
if !found {
return "", "", fmt.Errorf("OneOf: no requested char (%s) found in input", chars)
}
return input[:width], input[width:], nil
}
}
// NoneOf returns a [Parser] that recognises any char other than any of the provided characters
// from the start of input.
//
// It can be considered as the opposite to [OneOf].
//
// If the input or chars is empty, an error will be returned.
// Likewise if one of the chars was recognised.
func NoneOf(chars string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("NoneOf: input text is empty")
}
if chars == "" {
return "", "", errors.New("NoneOf: chars must not be empty")
}
r, width := utf8.DecodeRuneInString(input)
if r == utf8.RuneError {
return "", "", errors.New("NoneOf: input not valid utf-8")
}
found := false
for _, char := range chars {
if char == r {
// Found one that's not a match
found = true
break
}
}
// If we get here and found is true, the first char in the input matched one
// of the requested chars, which for NoneOf is bad
if found {
return "", "", fmt.Errorf("NoneOf: found match (%s) in input", string(r))
}
return input[:width], input[width:], nil
}
}
// AnyOf returns a [Parser] that continues taking characters so long as they are contained in the
// passed in set of chars.
//
// Parsing stops at the first occurrence of a character not contained in the argument and the
// offending character is not included in the parsed value, but will be in the remainder.
//
// AnyOf is the opposite to [NotAnyOf].
//
// If the input or chars is empty, an error will be returned.
// Likewise if none of the chars are present at the start of the input.
func AnyOf(chars string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("AnyOf: input text is empty")
}
if chars == "" {
return "", "", errors.New("AnyOf: chars must not be empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("AnyOf: input not valid utf-8")
}
end := 0 // The end of the matching sequence
for pos, char := range input {
if !strings.ContainsRune(chars, char) {
end = pos
break
}
}
// If we've broken the loop but end is still 0, there were no matches
// in the entire input
if end == 0 {
return "", "", fmt.Errorf("AnyOf: no match for any char in (%s) found in input", chars)
}
return input[:end], input[end:], nil
}
}
// NotAnyOf returns a [Parser] that continues taking characters so long as they are not contained
// in the passed in set of chars.
//
// Parsing stops at the first occurrence of a character contained in the argument and the
// offending character is not included in the parsed value, but will be in the remainder.
//
// NotAnyOf is the opposite of [AnyOf].
//
// If the input or chars is empty, an error will be returned.
// Likewise if any of the chars are present at the start of the input.
func NotAnyOf(chars string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("NotAnyOf: input text is empty")
}
if chars == "" {
return "", "", errors.New("NotAnyOf: chars must not be empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("NotAnyOf: input not valid utf-8")
}
end := 0 // The end of the matching sequence
for pos, char := range input {
if strings.ContainsRune(chars, char) {
end = pos
break
}
}
// If we've broken the loop but end is still 0, there were no matches
// in the entire input
if end == 0 {
return "", "", fmt.Errorf("NotAnyOf: match found for char in (%s)", chars)
}
return input[:end], input[end:], nil
}
}
// Optional returns a [Parser] that recognises an optional exact string from the start of input.
//
// If the match is there, it is returned as the value with the remainder being the remaining input,
// if the match is not there, the entire input is returned as the remainder with no value and no error.
//
// If the input is empty or invalid utf-8, then an error will be returned.
func Optional(match string) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
return "", "", errors.New("Optional: input text is empty")
}
if !utf8.ValidString(input) {
return "", "", errors.New("Optional: input not valid utf-8")
}
if match == "" {
return "", "", errors.New("Optional: match must not be empty")
}
start := strings.Index(input, match)
if start != 0 {
// The optional match isn't at the start of the string
return "", input, nil
}
return match, input[len(match):], nil
}
}
// Map returns a [Parser] that applies a function to the result of another parser.
//
// It is particularly useful for parsing a section of string input, then converting
// that captured string to another type.
//
// If the provided parser or the mapping function 'fn' return an error, Map will
// bubble up this error to the caller.
func Map[T1, T2 any](parser Parser[T1], fn func(T1) (T2, error)) Parser[T2] {
return func(input string) (T2, string, error) {
var zero T2
// Note: Since we're applying the function to another parser
// we don't need to check for empty input or invalid utf-8
// because the other parser will enforce it's own invariants
if fn == nil {
return zero, "", errors.New("Map: fn must be a non-nil function")
}
// Apply the parser to the input
value, remainder, err := parser(input)
if err != nil {
return zero, "", fmt.Errorf("Map: parser returned error: %w", err)
}
// Now apply the map function to the value returned from that
newValue, err := fn(value)
if err != nil {
return zero, "", fmt.Errorf("Map: fn returned error: %w", err)
}
return newValue, remainder, nil
}
}
// Try returns a [Parser] that attempts a series of sub-parsers, returning the output from the
// first successful one.
//
// If all parsers fail, an error will be returned.
//
// Note: Because Try takes a variadic argument, it is one of the only parser functions
// to allocate on the heap.
func Try[T any](parsers ...Parser[T]) Parser[T] {
return func(input string) (T, string, error) {
var zero T
for _, parser := range parsers {
// Try the parser
value, remainder, err := parser(input)
if err != nil {
// Try the next parser
continue
}
// We have a successful parser
return value, remainder, nil
}
// None of the parsers were successful
return zero, "", errors.New("Try: all parsers failed")
}
}
// Chain returns a [Parser] that calls a series of sub-parsers, passing the remainder from one
// as input to the next and returning a slice of values; one from each parser, and any remaining input
// after applying all the parsers.
//
// If any of the parsers fail, an error will be returned.
//
// Note: Because Chain takes a variadic argument and returns a slice, it is one of the only parser functions
// to allocate on the heap.
func Chain[T any](parsers ...Parser[T]) Parser[[]T] {
return func(input string) ([]T, string, error) {
values := make([]T, 0, len(parsers))
nextInput := input // The input to the next parser in the loop, starts as our overall input
var finalRemainder string // The final remainder to eventually return, updated with each parser
for _, parser := range parsers {
value, remainder, err := parser(nextInput)
if err != nil {
return nil, "", fmt.Errorf("Many: sub parser failed: %w", err)
}
values = append(values, value)
nextInput = remainder
finalRemainder = remainder
}
return values, finalRemainder, nil
}
}
// Count returns a [Parser] that applies another parser a certain number of times,
// returning the values in a slice along with any remaining input.
//
// If the parser fails or the input is exhausted before the parser has been applied
// the requested number of times, an error will be returned.
func Count[T any](parser Parser[T], count int) Parser[[]T] {
return func(input string) ([]T, string, error) {
values := make([]T, 0, count)
nextInput := input // The input to the next parser in the loop, starts as our overall input
var finalRemainder string // The final remainder to eventually return, updated with each parser
for i := 0; i < count; i++ {
value, remainder, err := parser(nextInput)
if err != nil {
return nil, "", fmt.Errorf("Count: parser failed: %w", err)
}
values = append(values, value)
nextInput = remainder
finalRemainder = remainder
}
return values, finalRemainder, nil
}
}