forked from svelterust/vom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.v
329 lines (297 loc) · 7.27 KB
/
multi.v
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
module vom
// Based on https://docs.rs/nom/7.1.3/nom/multi/index.html
// count runs the embedded parser `f` a specified number `count` of times.
pub fn count(f Fn, count int) FnMany {
return fn [f, count] (input string) !(string, []string, int) {
mut temp := input
mut output := []string{}
mut total_len := 0
for _ in 0 .. count {
rest, capture, len := f(temp)!
temp = rest
output << capture
total_len += len
}
return temp, output, total_len
}
}
// fill runs the embedded parser `f` repeatedly, filling the given slice `buf` with results.
pub fn fill(f Fn, mut buf []string) FnMany {
return fn [f, mut buf] (input string) !(string, []string, int) {
mut temp := input
mut total_len := 0
mut len := 0
for mut b in buf {
temp, b, len = f(temp)!
total_len += len
}
return temp, []string{}, total_len
}
}
// fold_many0 repeats the embedded parser `f`, calling `init` to init, and calling `g` to gather the results.
pub fn fold_many0(f Fn, init fn () []string, g fn (string, mut []string)) FnMany {
return fn [f, init, g] (input string) !(string, []string, int) {
mut y := init()
mut a := input
mut b := ''
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
if b == '' {
break
}
g(b, mut y)
total_len += len
}
return a, y, total_len
}
}
// fold_many1 repeats the embedded parser `f` at least one time, calling `init` to init, and calling `g` to gather the results.
pub fn fold_many1(f Fn, init fn () []string, g fn (string, mut []string)) FnMany {
return fn [f, init, g] (input string) !(string, []string, int) {
mut y := init()
mut a := input
mut b := ''
mut first_flag := true
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
first_flag = false
if b == '' {
break
}
g(b, mut y)
total_len += len
}
if first_flag == true {
return error('`fold_many1` fail.')
} else {
return a, y, total_len
}
}
}
// fold_many_m_n repeats the embedded parser `f` `m`..=`n` times, calling `init` to init, and calling `g` to gather the results.
pub fn fold_many_m_n(m usize, n usize, f Fn, init fn () []string, g fn (string, mut []string)) FnMany {
return fn [m, n, f, init, g] (input string) !(string, []string, int) {
mut y := init()
mut a := input
mut b := ''
mut first_flag := true
mut times := usize(0)
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
if b == '' {
break
}
g(b, mut y)
total_len += len
if times >= m {
first_flag = false
}
if times + 1 == n {
break
}
times += 1
}
if first_flag == true && m > 0 {
return error('`fold_many_m_n` fail.')
} else {
return a, y, total_len
}
}
}
// length_count gets a number from the first parser `f`, then applies the second parser `g` that many times.
pub fn length_count(f Fn, g Fn) FnMany {
return fn [f, g] (input string) !(string, []string, int) {
a, b, len1 := f(input)!
times := b.u8()
mut result := []string{}
mut c := a
mut d := ''
mut total_len := len1
mut len := 0
for _ in 0 .. times {
c, d, len = g(c) or { return error('`length_count` fail.') }
result << d
total_len += len
}
return c, result, total_len
}
}
// many0 repeats the embedded parser `f`, gathering the results in a []string.
pub fn many0(f Fn) FnMany {
return fn [f] (input string) !(string, []string, int) {
mut a := input
mut b := ''
mut result := []string{}
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
result << b
total_len += len
}
return a, result, total_len
}
}
// many0_count repeats the embedded parser `f`, counting the results
pub fn many0_count(f Fn) FnCount {
return fn [f] (input string) !(string, usize, int) {
mut a := input
mut count := usize(0)
mut total_len := 0
mut len := 0
for {
a, _, len = f(a) or { break }
count += 1
total_len += len
}
return a, count, total_len
}
}
// many1 repeats the embedded parser `f`, gathering the results in a []string.
pub fn many1(f Fn) FnMany {
return fn [f] (input string) !(string, []string, int) {
mut a := input
mut b := ''
mut result := []string{}
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
result << b
total_len += len
}
if result.len == 0 {
return error('`many1` fail.')
}
return a, result, total_len
}
}
// many1_count repeats the embedded parser `f`, counting the results
pub fn many1_count(f Fn) FnCount {
return fn [f] (input string) !(string, usize, int) {
mut a := input
mut count := usize(0)
mut total_len := 0
mut len := 0
for {
a, _, len = f(a) or { break }
count += 1
total_len += len
}
if count == 0 {
return error('`many1_count` fail.')
}
return a, count, total_len
}
}
// many_m_n repeats the embedded parser `f` `m`..=`n` times
pub fn many_m_n(m usize, n usize, f Fn) FnMany {
return fn [m, n, f] (input string) !(string, []string, int) {
mut a := input
mut b := ''
mut result := []string{}
mut first_flag := true
mut times := usize(0)
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
result << b
total_len += len
if times >= m {
first_flag = false
}
if times + 1 == n {
break
}
times += 1
}
if first_flag == true && m > 0 {
return error('`many_m_n` fail.')
} else {
return a, result, total_len
}
}
}
// many_till applies the parser `f` until the parser `g` produces a result.
pub fn many_till(f Fn, g Fn) FnResult {
return fn [f, g] (input string) !(string, []string, string, int) {
mut a := input
mut b := ''
mut result := []string{}
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break }
result << b
total_len += len
}
if result.len == 0 {
return error('`many_till` fail with first parser.')
}
a, b, len = g(a) or { return error('many_till` fail with second parser.') }
total_len += len
return a, result, b, total_len
}
}
// separated_list0 alternates between two parsers to produce a list of elements.
// `sep` Parses the separator between list elements.
// `f` Parses the elements of the list.
pub fn separated_list0(sep Fn, f Fn) FnMany {
return fn [sep, f] (input string) !(string, []string, int) {
mut a := input
mut b := ''
mut result := []string{}
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break } // { a, '' } workaround a bug of vlang
total_len += len
if len > 0 {
result << b
}
a, b, len = sep(a) or { break }
total_len += len
}
for {
a, b, len = sep(a) or { break }
total_len += len
}
return a, result, total_len
}
}
// separated_list1 alternates between two parsers to produce a list of elements.
// `sep` Parses the separator between list elements.
// `f` Parses the elements of the list.
pub fn separated_list1(sep Fn, f Fn) FnMany {
return fn [sep, f] (input string) !(string, []string, int) {
mut a := input
mut b := ''
mut result := []string{}
mut total_len := 0
mut len := 0
for {
a, b, len = f(a) or { break } // { a, '' } workaround a bug of vlang
total_len += len
if len > 0 {
result << b
}
a, b, len = sep(a) or { break }
total_len += len
}
for {
a, b, len = sep(a) or { break }
total_len += len
}
if result.len == 0 {
return error('`separated_list1` fail.')
}
return a, result, total_len
}
}