-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathParserCombinators.fs
288 lines (257 loc) · 7.47 KB
/
ParserCombinators.fs
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
module Spiral.ParserCombinators
let inline index d = (^a : (member Index: ^b) d)
let inline index_set i d = (^a : (member set_Index: ^b -> unit) (d,i))
let inline (.>>.) a b d =
match a d with
| Ok a ->
match b d with
| Ok b -> Ok (a,b)
| Error x -> Error x
| Error x -> Error x
let inline tuple3 a b c d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c -> Ok (a, b, c)
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline tuple4 a b c d' d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c ->
match d' d with
| Ok d' -> Ok (a, b, c, d')
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline tuple5 a b c d' e d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c ->
match d' d with
| Ok d' ->
match e d with
| Ok e -> Ok (a, b, c, d', e)
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline tuple6 a b c d' e f d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c ->
match d' d with
| Ok d' ->
match e d with
| Ok e ->
match f d with
| Ok f -> Ok (a, b, c, d', e, f)
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline tuple7 a b c d' e f g d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c ->
match d' d with
| Ok d' ->
match e d with
| Ok e ->
match f d with
| Ok f ->
match g d with
| Ok g -> Ok (a, b, c, d', e, f, g)
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline pipe2 a b f d =
match a d with
| Ok a ->
match b d with
| Ok b -> Ok (f a b)
| Error x -> Error x
| Error x -> Error x
let inline pipe3 a b c f d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c -> Ok (f a b c)
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline pipe4 a b c d' f d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c ->
match d' d with
| Ok d' -> Ok (f a b c d')
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline pipe5 a b c d' e f d =
match a d with
| Ok a ->
match b d with
| Ok b ->
match c d with
| Ok c ->
match d' d with
| Ok d' ->
match e d with
| Ok e -> Ok (f a b c d' e)
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
| Error x -> Error x
let inline (.>>) a b d =
match a d with
| Ok a ->
match b d with
| Ok b -> Ok a
| Error x -> Error x
| Error x -> Error x
let inline (>>.) a b d =
match a d with
| Ok a ->
match b d with
| Ok b -> Ok b
| Error x -> Error x
| Error x -> Error x
let inline opt a d =
let s = index d
match a d with
| Ok a -> Ok(Some a)
| Error x ->
if s = index d then Ok(None)
else Error x
let inline optional a d =
let s = index d
match a d with
| Ok a -> Ok()
| Error x ->
if s = index d then Ok()
else Error x
let inline (|>>) a b d =
match a d with
| Ok a -> Ok(b a)
| Error x -> Error x
let inline (>>%) a b d =
match a d with
| Ok a -> Ok(b)
| Error x -> Error x
let inline (>>=) a b d =
match a d with
| Ok a -> b a d
| Error x -> Error x
let inline (>>=?) a b d =
let i = index d
match a d with
| Ok a ->
let i' = index d
match b a d with
| Ok _ as x -> x
| Error _ as x -> (if i' = index d then index_set i d); x // Backtracks to the beginning if the parser state has not changed.
| Error x -> Error x
let inline many_iter f a d =
let rec loop () =
let s = index d
match a d with
| Ok _ when s = index d -> failwith "The parser succeeded without changing the parser index in `many`. Had an exception not been raised the parser would have diverged."
| Ok x -> f x; loop()
| Error er -> if s = index d then Ok() else Error er
loop ()
let inline many_resize_array a d =
let ar = ResizeArray()
match many_iter ar.Add a d with
| Ok() -> Ok(ar)
| Error er -> Error er
let inline many_array a d = many_resize_array a d |> Result.map (fun x -> x.ToArray())
let inline many a d = many_resize_array a d |> Result.map Seq.toList
let inline sepBy a b d =
let s = index d
match a d with
| Ok a' -> (many (b >>. a) |>> fun b -> a' :: b) d
| Error x -> if s = index d then Ok [] else Error x
let inline sepBy1 a b d =
match a d with
| Ok a' -> (many (b >>. a) |>> fun b -> a' :: b) d
| Error x -> Error x
let inline many1 a d =
match a d with
| Ok a' -> (many a |>> fun b -> a' :: b) d
| Error x -> Error x
let inline attempt a d =
let s = index d
match a d with
| Ok x -> Ok x
| Error a as a' -> index_set s d; a'
/// Restores the index on an error if at least i tokens have been consumed.
let inline restore i a d =
let s = index d
match a d with
| Ok x -> Ok x
| Error _ as er -> (if index d <= s + i then index_set s d); er
let inline alt s a b d =
match a d with
| Ok x -> Ok x
| Error a as a' ->
if s = index d then
match b d with
| Ok x -> Ok x
| Error b -> if s = index d then Error(List.append a b) else Error b
else
a'
let inline (<|>) a b d = let s = index d in alt s a b d
let inline (<|>%) a b d =
let s = index d
match a d with
| Ok x -> Ok x
| Error _ as a' -> if s = index d then Ok b else a'
let inline choice ar d =
let s = index d
let rec loop i =
if i < Array.length ar then
match ar.[i] d with
| Ok x -> Ok x
| Error a as a' ->
if s = index d then
match loop (i+1) with
| Ok x -> Ok x
| Error b -> Error(List.append a b)
else
a'
else
Error []
loop 0
let inline between a b c = a >>. c .>> b