-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathParser.cs
199 lines (166 loc) · 7.14 KB
/
Parser.cs
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
using System;
using LanguageExt;
using LanguageExt.Parsec;
using static LanguageExt.Prelude;
using static LanguageExt.Parsec.Prim;
using static LanguageExt.Parsec.Common;
using static LanguageExt.Parsec.ParserResult;
using static LanguageExt.Parsec.ParserResultIO;
using System.Diagnostics;
namespace LanguageExt.Parsec
{
/// <summary>
/// Parser delegate type - Parses an input PString and returns a ParserResult
/// </summary>
/// <typeparam name="T">Parsed value result type</typeparam>
/// <param name="input">Input string</param>
/// <returns>Parsed value or error report</returns>
public delegate ParserResult<T> Parser<T>(PString input);
}
public static class ParserExtensions
{
/// <summary>
/// A label for the parser
/// </summary>
/// <param name="expected">What was expected</param>
public static Parser<T> label<T>(this Parser<T> p, string expected) =>
inp =>
{
var res = p(inp);
if (res.Tag == ResultTag.Consumed)
{
return res;
}
if (res.Reply.Tag == ReplyTag.Error)
{
return EmptyError<T>(ParserError.Expect(inp.Pos, res.Reply.Error?.Msg ?? "<error>", expected));
}
if (res.Reply.Error is null || res.Reply.Error.Tag == ParserErrorTag.Unknown)
{
return res;
}
else
{
return EmptyOK(res.Reply.Result!, res.Reply.State!, ParserError.Expect(inp.Pos, res.Reply.Error.Msg, expected));
}
};
public static ParserResult<T> Parse<T>(this Parser<T> self, PString input) =>
self(input);
public static ParserResult<T> Parse<T>(this Parser<T> self, string input) =>
self(PString.Zero.SetValue(input));
public static Parser<T> Filter<T>(this Parser<T> self, Func<T, bool> pred) =>
self.Where(pred);
public static Parser<T> Where<T>(this Parser<T> self, Func<T, bool> pred) =>
inp =>
self(inp).Match(
EmptyOK: (x, rem, msg) => pred(x) ? EmptyOK(x, rem, msg) : EmptyError<T>(ParserError.SysUnexpect(inp.Pos, $"\"{x}\"")),
EmptyError: EmptyError<T>,
ConsumedOK: (x, rem, msg) => pred(x) ? ConsumedOK(x, rem, msg) : EmptyError<T>(ParserError.SysUnexpect(inp.Pos, $"\"{x}\"")),
ConsumedError: ConsumedError<T>);
public static Parser<U> Map<T, U>(this Parser<T> self, Func<T, U> map) =>
self.Select(map);
public static Parser<U> Select<T, U>(this Parser<T> self, Func<T, U> map) =>
inp => self(inp).Select(map);
public static Parser<B> Bind<A, B>(this Parser<A> self, Func<A, Parser<B>> f) =>
self.SelectMany(f);
public static Parser<B> SelectMany<A, B>(
this Parser<A> self,
Func<A, Parser<B>> f) =>
inp =>
{
Debug.Assert(inp != null);
var t = self(inp);
// cok
if (t.Tag == ResultTag.Consumed && t.Reply.Tag == ReplyTag.OK)
{
return f(t.Reply.Result)(t.Reply.State);
}
// eok
if (t.Tag == ResultTag.Empty && t.Reply.Tag == ReplyTag.OK)
{
return f(t.Reply.Result)(t.Reply.State);
}
// cerr
if (t.Tag == ResultTag.Consumed && t.Reply.Tag == ReplyTag.Error)
{
return ConsumedError<B>(t.Reply.Error);
}
// eerr
return EmptyError<B>(t.Reply.Error);
};
public static Parser<V> SelectMany<T, U, V>(
this Parser<T> self,
Func<T, Parser<U>> bind,
Func<T, U, V> project) =>
inp =>
{
Debug.Assert(inp != null);
var t = self(inp);
// cok
if (t.Tag == ResultTag.Consumed && t.Reply.Tag == ReplyTag.OK)
{
var u = bind(t.Reply.Result)(t.Reply.State);
if (u.Tag == ResultTag.Consumed && u.Reply.Tag == ReplyTag.OK)
{
// cok, cok -> cok
var v = project(t.Reply.Result, u.Reply.Result);
return ConsumedOK(v, u.Reply.State, u.Reply.Error);
}
if (u.Tag == ResultTag.Consumed && u.Reply.Tag == ReplyTag.Error)
{
// cok, cerr -> cerr
return ConsumedError<V>(u.Reply.Error);
}
if (u.Tag == ResultTag.Empty && u.Reply.Tag == ReplyTag.OK)
{
// cok, eok -> cok (not a typo, this should be -> cok)
var v = project(t.Reply.Result, u.Reply.Result);
return ConsumedOK(v, u.Reply.State, mergeError(t.Reply.Error, u.Reply.Error));
}
// cok, eerr
return ConsumedError<V>(mergeError(t.Reply.Error, u.Reply.Error));
}
// eok
if (t.Tag == ResultTag.Empty && t.Reply.Tag == ReplyTag.OK)
{
var u = bind(t.Reply.Result)(t.Reply.State);
if (u.Tag == ResultTag.Consumed && u.Reply.Tag == ReplyTag.OK)
{
// eok, cok -> cok
var v = project(t.Reply.Result, u.Reply.Result);
return ConsumedOK(v, u.Reply.State, u.Reply.Error);
}
if (u.Tag == ResultTag.Empty && u.Reply.Tag == ReplyTag.OK)
{
// eok, eok -> eok
var v = project(t.Reply.Result, u.Reply.Result);
return EmptyOK(v, u.Reply.State, mergeError(t.Reply.Error, u.Reply.Error));
}
if (u.Tag == ResultTag.Consumed && u.Reply.Tag == ReplyTag.Error)
{
// eok, cerr -> cerr
return ConsumedError<V>(u.Reply.Error);
}
// eok, eerr
return EmptyError<V>(mergeError(t.Reply.Error, u.Reply.Error));
}
// cerr
if (t.Tag == ResultTag.Consumed && t.Reply.Tag == ReplyTag.Error)
{
return ConsumedError<V>(t.Reply.Error);
}
// eerr
return EmptyError<V>(t.Reply.Error);
};
public static Parser<A> Flatten<A>(this Parser<Parser<A>> mma) =>
mma.Bind(identity);
public static Parser<T> Flatten<T>(this Parser<Option<T>> p, Func<string> failureText) =>
from value in p
from returnValue in value.Match(result, compose(failureText, failure<T>))
select returnValue;
public static Parser<R> Flatten<L, R>(this Parser<Either<L, R>> p, Func<L, string> failureText) =>
from value in p
from returnValue in value.Match(compose(failureText, failure<R>), result)
select returnValue;
public static Parser<R> Flatten<R>(this Parser<Either<string,R>> p) => Flatten(p, identity);
}