-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsignators.py
264 lines (174 loc) · 6.44 KB
/
parsignators.py
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
from functools import partial
def foldl(func, start, seq):
if len(seq) == 0:
return start
else:
return foldl(func, func(start, seq[0]), seq[1:])
def foldr(f, b, seq):
if len(seq) == 0:
return b
else:
return [f(seq[0])] + foldr(f, b, seq[1:])
class ParserException(Exception):
pass
class Parser(object):
def __init__(self, *parsers):
self.parsers = parsers
if len(parsers) > 0:
self.parser = parsers[0]
else:
self.parser = None
def __le__(self, f):
""" Applies: p <= f """
return Apply(self, f)
def __lt__(self, other):
""" Ignores the right parser: p < q """
return SequentialCompositionIgnoreRight(self, other)
def __gt__(self, other):
""" Ignores the left parser: p > q """
return SequentialCompositionIgnoreLeft(self, other)
def __mul__(self, other):
""" Sequential Composition: p * q """
return SequentialComposition(self, other)
def __or__(self, other):
""" Choice: p | q """
return Choice(self, other)
def parse(self, stream):
pass
class Zero(Parser):
def parse(self, stream):
return []
class Symbol(Parser):
def __init__(self, symbol):
self.symbol = symbol
def parse(self, stream):
if len(stream) > 0 and stream[0] == self.symbol:
return [(stream[0], stream[1:])]
else:
return []
class Token(Parser):
def __init__(self, tok):
self.tok = tok
def parse(self, stream):
if self.tok >= len(stream) and self.tok == stream[0:len(self.tok)]:
return [(self.tok, stream[len(self.tok):])]
else:
return []
class Satisfy(Parser):
def __init__(self, predicate):
self.predicate = predicate
def parse(self, stream):
if len(stream) > 0 and self.predicate(stream[0]):
return [(stream[0], stream[1:])]
else:
return []
class Succeed(Parser):
def __init__(self, v, xs):
self.v = v
self.xs = xs
def parse(self, stream):
return [(self.v, self.xs)]
class Epsilon(Parser):
def parse(self, stream):
return Succeed((), stream).parse(stream)
class Fail(Parser):
def parse(self, stream):
return []
class SequentialComposition(Parser):
def __init__(self, parser1, parser2):
self.parser1 = parser1
self.parser2 = parser2
def parse(self, stream):
res = []
for v1, xs1 in self.parser1.parse(stream):
p2res = self.parser2.parse(xs1)
for v2, xs2 in p2res:
res.append(((v1, v2), xs2))
return res
class SequentialCompositionIgnoreLeft(Parser):
def __init__(self, parser1, parser2):
self.parser1 = parser1
self.parser2 = parser2
def parse(self, stream):
return (SequentialComposition(self.parser1, self.parser2) <= (lambda x: x[1])).parse(stream)
class SequentialCompositionIgnoreRight(Parser):
def __init__(self, parser1, parser2):
self.parser1 = parser1
self.parser2 = parser2
def parse(self, stream):
return (SequentialComposition(self.parser1, self.parser2) <= (lambda x: x[0])).parse(stream)
class Sequence(Parser):
def parse(self, stream):
if len(self.parsers) == 0:
return Epsilon()
else:
res = self.parsers[0].parse(stream)
for p in self.parsers[1:]:
res = Cons(res, p)
return res
class Choice(Parser):
def parse(self, stream):
results = []
for p in self.parsers:
results.extend(p.parse(stream))
return results
class Apply(Parser):
def __init__(self, parser, f):
self.parser = parser
self.f = f
def parse(self, stream):
return [ (self.f(v), xs) for v, xs, in self.parser.parse(stream)]
class Just(Parser):
def parse(self, stream):
return filter(lambda v, xs: xs == "", self.parser.parse(stream))
Digit = (Satisfy(lambda x: x in '0123456789') <= (lambda x: int(x)))
Pack = lambda open_, parser, close: (open_ > parser) < close
Parenthesized = lambda parser: Pack(Symbol('('), parser, Symbol(')'))
Bracketed = lambda parser: Pack(Symbol('['), parser, Symbol(']'))
cons_ = lambda pair: [pair[0]] + pair[1]
list_ = lambda x: [x]
nil_ = lambda x: []
Cons = lambda p, q: p * q <= cons_
class Many(Parser):
def parse(self, stream):
return (Cons(self.parser, self) | (Epsilon() <= nil_)).parse(stream)
Option = lambda parser : (parser <= list_) | (Epsilon() <= nil_)
class OptionDef(Parser):
def __init__(self, parser, absence, presence):
self.parser = parser
self.absence = absence
self.presence = presence
def parse(self, stream):
def f(xs):
if len(xs) == 0:
return self.absence
else:
return self.presence(xs[0])
return (Option(self.parser) <= f).parse(stream)
Many1 = lambda parser: Cons(parser, Many(parser))
Natural = Many1(Digit) <= partial(foldl, lambda a, b: a*10 + b, 0)
Integer = (OptionDef(Symbol('+') | Symbol('-'), 1, lambda x: {'-':-1, '+':1}[x]) * Natural) <= (lambda num: num[0] * num[1])
ListOf = lambda parser, separator: Cons(parser, Many(separator > parser)) | Epsilon()
CommaList = lambda parser: ListOf(parser, Symbol(','))
SemiColonList = lambda parser: ListOf(parser, Symbol(';'))
class Some(Parser):
def parse(self, stream):
return Just(self.parser).parse(stream)[0][1]
class First(Parser):
def parse(self, stream):
res = self.parser.parse(stream)
if len(res) == 0:
return []
else:
return [res[0]]
Greedy = lambda parser: First(Many(parser))
Greedy1 = lambda parser: First(Many1(parser))
Compulsion = lambda parser: First(Option(parser))
class Forward(Parser):
def set(self, parser):
self.parser = parser
def parse(self, stream):
if self.parser is None:
raise ParserException("Forward parser has not been initialized yet.")
else:
return self.parser.parse(stream)