-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
355 lines (311 loc) · 8.97 KB
/
sequence.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
package chomp
// Pair will scan the input text and match each [Combinator] in turn.
// Both combinators must match.
//
// chomp.Pair(chomp.Tag("Hello,"), chomp.Tag(" World"))("Hello, World!")
// // ("!", []string{"Hello,", " World"}, nil)
func Pair[T, U Result](c1 Combinator[T], c2 Combinator[U]) Combinator[[]string] {
return func(s string) (string, []string, error) {
rem, out1, err := c1(s)
if err != nil {
return rem, nil, ParserError{Err: err, Type: "pair"}
}
rem, out2, err := c2(rem)
if err != nil {
return rem, nil, ParserError{Err: err, Type: "pair"}
}
var ext []string
ext = combine(ext, out1, out2)
return rem, ext, nil
}
}
func combine(in []string, elems ...any) []string {
for _, e := range elems {
switch t := e.(type) {
case string:
in = append(in, t)
case []string:
in = append(in, t...)
}
}
return in
}
// SepPair will scan the input text and match each [Combinator], discarding
// the separator's output. All combinators must match.
//
// chomp.SepPair(
// chomp.Tag("Hello"),
// chomp.Tag(", "),
// chomp.Tag("World"))("Hello, World!")
// // ("!", []string{"Hello", "World"}, nil)
func SepPair[T, U, V Result](c1 Combinator[T], sep Combinator[U], c2 Combinator[V]) Combinator[[]string] {
return func(s string) (string, []string, error) {
rem, out1, err := c1(s)
if err != nil {
return rem, nil, ParserError{Err: err, Type: "sep_pair"}
}
rem, _, err = sep(rem)
if err != nil {
return rem, nil, ParserError{Err: err, Type: "sep_pair"}
}
rem, out2, err := c2(rem)
if err != nil {
return rem, nil, ParserError{Err: err, Type: "sep_pair"}
}
var ext []string
ext = combine(ext, out1, out2)
return rem, ext, nil
}
}
// Repeat will scan the input text and match the combinator the defined
// number of times. Every execution must match.
//
// chomp.Repeat(chomp.Parentheses(), 2)("(Hello)(World)(!)")
// // ("(!)", []string{"(Hello)", "(World)"}, nil)
func Repeat[T Result](c Combinator[T], n uint) Combinator[[]string] {
return func(s string) (string, []string, error) {
var ext []string
var err error
rem := s
for i := uint(0); i < n; i++ {
var out T
if rem, out, err = c(rem); err != nil {
return rem, nil, RangedParserError{
Err: err,
Exec: RangeExecution(i, n),
Type: "repeat",
}
}
ext = combine(ext, out)
}
return rem, ext, nil
}
}
// RepeatRange will scan the input text and match the [Combinator] between
// a minimum and maximum number of times. It must match the expected minimum
// number of times.
//
// chomp.RepeatRange(chomp.OneOf("Hleo"), 1, 8)("Hello, World!")
// // (", World!", []string{"H", "e", "l", "l", "o"}, nil)
func RepeatRange[T Result](c Combinator[T], n, m uint) Combinator[[]string] {
return func(s string) (string, []string, error) {
var ext []string
var err error
if n > m {
n, m = m, n
}
rem := s
for i := uint(0); i < m; i++ {
var out T
if rem, out, err = c(rem); err != nil {
if i+1 > n {
break
}
return rem, nil, RangedParserError{
Err: err,
Exec: RangeExecution(i, n, m),
Type: "repeat_range",
}
}
ext = combine(ext, out)
}
return rem, ext, nil
}
}
// Delimited will match a series of combinators against the input text. All
// must match, with the delimiters being discarded.
//
// chomp.Delimited(
// chomp.Tag("'"),
// chomp.Tag("Hello, World!"),
// chomp.Tag("'"))("'Hello, World!'")
// // ("", "Hello, World!", nil)
func Delimited[T, U, V Result](left Combinator[T], str Combinator[U], right Combinator[V]) Combinator[U] {
return func(s string) (string, U, error) {
var def U
rem, _, err := left(s)
if err != nil {
return s, def, ParserError{Err: err, Type: "delimited"}
}
rem, ext, err := str(rem)
if err != nil {
return rem, def, ParserError{Err: err, Type: "delimited"}
}
rem, _, err = right(rem)
if err != nil {
return rem, def, ParserError{Err: err, Type: "delimited"}
}
return rem, ext, nil
}
}
// QuoteDouble will match any text delimited (or surrounded) by a
// pair of "double quotes".
//
// chomp.DoubleQuote()(`"Hello, World!"`)
// // ("", "Hello, World!", nil)
func QuoteDouble() Combinator[string] {
return func(s string) (string, string, error) {
return Delimited(Tag("\""), Until("\""), Tag("\""))(s)
}
}
// QuoteSingle will match any text delimited (or surrounded) by a
// pair of 'single quotes'.
//
// chomp.QuoteSingle()("'Hello, World!'")
// // ("", "Hello, World!", nil)
func QuoteSingle() Combinator[string] {
return func(s string) (string, string, error) {
return Delimited(Tag("'"), Until("'"), Tag("'"))(s)
}
}
// BracketSquare will match any text delimited (or surrounded) by
// a pair of [square brackets].
//
// chomp.BracketSquare()("[Hello, World!]")
// // ("", "Hello, World!", nil)
func BracketSquare() Combinator[string] {
return func(s string) (string, string, error) {
return Delimited(Tag("["), Until("]"), Tag("]"))(s)
}
}
// Parentheses will match any text delimited (or surrounded) by
// a pair of (parentheses).
//
// chomp.Parentheses()("(Hello, World!)")
// // ("", "Hello, World!", nil)
func Parentheses() Combinator[string] {
return func(s string) (string, string, error) {
return Delimited(Tag("("), Until(")"), Tag(")"))(s)
}
}
// BracketAngled will match any text delimited (or surrounded) by
// a pair of <angled brackets>.
//
// chomp.BracketAngled()("<Hello, World!>")
// // ("", "Hello, World!", nil)
func BracketAngled() Combinator[string] {
return func(s string) (string, string, error) {
return Delimited(Tag("<"), Until(">"), Tag(">"))(s)
}
}
// First will match the input text against a series of [Combinator]s.
// Matching stops as soon as the first combinator succeeds. One [Combinator]
// must match. For better performance, try and order the combinators from
// most to least likely to match.
//
// chomp.First(
// chomp.Tag("Good Morning"),
// chomp.Tag("Hello"))("Good Morning, World!")
// // (" ,World!", "Good Morning", nil)
func First[T Result](c ...Combinator[T]) Combinator[T] {
return func(s string) (string, T, error) {
for _, comb := range c {
if rem, ext, err := comb(s); err == nil {
return rem, ext, nil
}
}
var out T
return s, out, CombinatorParseError{Text: s, Type: "first"}
}
}
// All will match the input text against a series of [Combinator]s.
// All combinators must match in the order provided.
//
// chomp.All(
// chomp.Tag("Hello"),
// chomp.Until("W"),
// chomp.Tag("World!"))("Hello, World!")
// // ("", []string{"Hello", ", ", "World!"}, nil)
func All[T Result](c ...Combinator[T]) Combinator[[]string] {
return func(s string) (string, []string, error) {
var ext []string
var err error
rem := s
for _, comb := range c {
var out T
if rem, out, err = comb(rem); err != nil {
return rem, nil, ParserError{Err: err, Type: "all"}
}
ext = combine(ext, out)
}
return rem, ext, nil
}
}
// Many will scan the input text, and it must match the [Combinator] at least
// once. This [Combinator] is greedy and will continuously execute until the first
// failed match. It is the equivalent of calling [ManyN] with an argument of 1.
//
// chomp.Many(one.Of("Ho"))("Hello, World!")
// // ("ello, World!", []string{"H"}, nil)
func Many[T Result](c Combinator[T]) Combinator[[]string] {
return ManyN(c, 1)
}
// ManyN will scan the input text and match the [Combinator] a minimum number
// of times. This [Combinator] is greedy and will continuously execute until
// the first failed match.
//
// chomp.ManyN(chomp.OneOf("W"), 0)("Hello, World!")
// // ("Hello, World!", nil, nil)
func ManyN[T Result](c Combinator[T], n uint) Combinator[[]string] {
return func(s string) (string, []string, error) {
var ext []string
var err error
var count uint
rem := s
for {
var out T
var tmpRem string
if tmpRem, out, err = c(rem); err != nil {
break
}
rem = tmpRem
ext = combine(ext, out)
count++
}
if count < n {
return rem, nil, RangedParserError{
Err: err,
Exec: RangeExecution(count, n),
Type: "many_n",
}
}
return rem, ext, nil
}
}
// Prefixed will scan the input text for a defined prefix and discard it
// before matching the remaining text against the [Combinator]. Both
// combinators must match.
//
// chomp.Prefixed(
// chomp.Tag("Hello"),
// chomp.Tag(`"`))(`"Hello, World!"`)
// // (`, World!"`, "Hello", nil)
func Prefixed(c, pre Combinator[string]) Combinator[string] {
return func(s string) (string, string, error) {
rem, _, err := pre(s)
if err != nil {
return rem, "", err
}
return c(rem)
}
}
// Suffixed will scan the input text against the [Combinator] before matching a
// suffix and discarding it. Both combinators must match.
//
// chomp.Suffixed(
// chomp.Tag("Hello"),
// chomp.Tag(", "))("Hello, World!")
// // ("World!", "Hello", nil)
func Suffixed(c, suf Combinator[string]) Combinator[string] {
return func(s string) (string, string, error) {
rem, ext, err := c(s)
if err != nil {
return rem, "", err
}
rem, _, err = suf(rem)
if err != nil {
return rem, "", err
}
return rem, ext, nil
}
}