-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.go
180 lines (162 loc) · 3.53 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
package parser
import (
"fmt"
"io"
"unicode/utf8"
)
//type Parser func[T any](rs io.ReadSeeker) (T, error)
type fatalError struct {
err error
}
func (fe fatalError) Error() string {
return fmt.Sprintf("Fatal match error: %s", fe.err)
}
type StatefulReader interface {
io.Reader
State() any
Restore(any)
}
type SimpleReader struct {
r io.ReadSeeker
}
func (sr SimpleReader) Read(p []byte) (n int, err error) {
return sr.r.Read(p)
}
func (sr SimpleReader) State() any {
s, _ := sr.r.Seek(0, 1)
return s
}
func (sr SimpleReader) Restore(s any) {
sr.r.Seek(s.(int64), 0)
}
func Lit(text string) func(sr StatefulReader) (string, error) {
return func(sr StatefulReader) (string, error) {
s := sr.State()
b := make([]byte, len(text))
c, _ := io.ReadFull(sr, b)
if c < len(text) {
sr.Restore(s)
return "", fmt.Errorf("Unexpected EOF")
}
if string(b) == text {
return text, nil
}
sr.Restore(s)
return "", fmt.Errorf("Expected %q, got %q", text, string(b))
}
}
func readRune(sr StatefulReader) (rune, error) {
b := make([]byte, 1, 4)
_, err := sr.Read(b)
for !utf8.FullRune(b) && err != nil {
b = b[:len(b)+1]
_, err = sr.Read(b[len(b)-1:])
}
r, _ := utf8.DecodeRune(b)
return r, err
}
func Set(text string) func(sr StatefulReader) (string, error) {
//expand 0-9 to 0123456789
final := []rune{}
rawtext := []rune(text)
for i := range rawtext {
if rawtext[i] == '-' && i > 0 && i < len(rawtext)-1 {
for j := rawtext[i-1] + 1; j < rawtext[i+1]; j++ {
final = append(final, j)
}
} else {
final = append(final, rawtext[i])
}
}
return func(sr StatefulReader) (string, error) {
s := sr.State()
r, err := readRune(sr)
if err != nil {
sr.Restore(s)
return "", err
}
for _, tr := range final {
if r == tr {
return string(r), nil
}
}
sr.Restore(s)
return "", fmt.Errorf("Expected %q, got %q", text, string(r))
}
}
func Or[T any](ps ...func(sr StatefulReader) (T, error)) func(sr StatefulReader) (T, error) {
return func(sr StatefulReader) (T, error) {
s := sr.State()
for _, p := range ps {
v, err := p(sr)
if err == nil {
return v, nil
}
sr.Restore(s)
}
var t T
return t, fmt.Errorf("No match")
}
}
func And[T any](ps ...func(sr StatefulReader) (T, error)) func(sr StatefulReader) ([]T, error) {
return func(sr StatefulReader) ([]T, error) {
vs := []T{}
s := sr.State()
for _, p := range ps {
v, err := p(sr)
if err != nil {
sr.Restore(s)
return nil, err
}
vs = append(vs, v)
}
return vs, nil
}
}
func Optional[T any](p func(sr StatefulReader) (T, error)) func(sr StatefulReader) (T, error) {
return func(sr StatefulReader) (T, error) {
s := sr.State()
p, err := p(sr)
if err != nil {
sr.Restore(s)
}
if _, isFE := err.(fatalError); isFE {
return p, err
}
return p, nil
}
}
func Mult[T any](n, m int, p func(sr StatefulReader) (T, error)) func(sr StatefulReader) ([]T, error) {
if m == 0 {
m = int(^uint(0) >> 1)
}
return func(sr StatefulReader) ([]T, error) {
s := sr.State()
ms := []T{}
for i := 0; i < m; i++ {
match, err := p(sr)
if err != nil {
if _, isFE := err.(fatalError); isFE {
return nil, err
}
if i < n {
sr.Restore(s)
return nil, err
}
return ms, nil
}
ms = append(ms, match)
}
return ms, nil
}
}
func Convert[T, U any](p func(sr StatefulReader) (T, error), f func(T) (U, error)) func(sr StatefulReader) (U, error) {
return func(sr StatefulReader) (U, error) {
v, err := p(sr)
if err != nil {
var u U
return u, err
}
return f(v)
}
}