-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsers1.pls
More file actions
46 lines (39 loc) · 783 Bytes
/
parsers1.pls
File metadata and controls
46 lines (39 loc) · 783 Bytes
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
fn parsestr(str) {
fn parsestr(s, str) {
if strleft(s, len(str)) == str then {
yield ([str, strmid(s, len(str))])
}
}
return bind(parsestr, str)
}
fn concatp(parsers) {
fn concatp(s, parsers) {
def l = []
for parser in parsers do {
def x = parser(s)
l = concat(l, [x[0]])
s = x[1]
}
return ([l, s])
}
return bind(concatp, parsers)
}
fn alternate(parsers) {
fn alternate(s, parsers) {
for parser in parsers do {
for x in gcall(parser, s) do {
yield x
}
}
}
return bind(alternate, parsers)
}
def b = print(1)
for a in gcall(alternate([parsestr("j"), parsestr("jj")]), "jj") do {
def b = print(a)
}
def b = print(1)
def p = alternate([parsestr("j"), parsestr("jj")])
for a in gcall(concatp([p, p]), "jjj") do {
def b = print(a)
}