-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinators.py
251 lines (207 loc) · 8.15 KB
/
combinators.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
"""
A combinator is a function which produces a parser as its output, usually after
taking one or more parsers as input, hence the name "combinator". You can use
combinators to create a complete parser for a language by creating lots
of smaller parsers for parts of the language, then using combinators to build
the final parser.
"""
class Result(object):
def __init__(self, value, pos):
"""
Every parser will return a Result object on success or None on failure.
A Result comprises a value (part of the AST), and a position
(the index of the next token in the stream).
"""
self.value = value
self.pos = pos
def __repr__(self):
return 'Result({}, {})'.format(self.value, self.pos)
class Parser(object):
def __call__(self, tokens, pos):
"""
A parser object will behave as if it were a function, but we can also
provide additional functionality by defining some operators
:tokens full list of tokens (returned by the lexer)
:pos index into the list indicating the next token
"""
return None
def __add__(self, other):
"""Override default `+` operator"""
return Concat(self, other)
def __mul__(self, other):
"""Overrride default `*` operator"""
return Exp(self, other)
def __or__(self, other):
"""Override default `|` operator"""
return Alternate(self, other)
def __xor__(self, fn):
"""Override default `^` operator"""
return Process(self, fn)
class Reserved(Parser):
def __init__(self, value, tag):
"""
Accept a tokens that are used to parse `RESERVED` words and operators.
A token is a value-tag pair represented as a tuple: (value, tag)
"""
self.value = value
self.tag = tag
def __call__(self, tokens, pos):
logic = [
pos < len(tokens),
tokens[pos][0] == self.value,
tokens[pos][1] is self.tag
]
return Result(tokens[pos][0], pos + 1) if all(logic) else None
class Tag(Parser):
def __init__(self, tag):
"""Match any token which has a particular tag"""
self.tag = tag
def __call__(self, tokens, pos):
logic = [
pos < len(tokens),
tokens[pos][1] is self.tag
]
return Result(tokens[pos][0], pos + 1) if all(logic) else None
class Concat(Parser):
def __init__(self, left, right):
self.left = left
self.right = right
def __call__(self, tokens, pos):
"""
Apply the left parser followed by the right parser.
Useful for parsing specific sequeneces of tokens, e.g. `1 + 2` could be
written as:
1) `parser = Concat(Concat(Tag(INT), Reserved('+', RESERVED)), Tag(INT))`
2) `parser = Tag(INT) + Reserved('+', RESERVED) + Tag(INT)
"""
left_result = self.left(tokens, pos)
if left_result:
right_result = self.right(tokens, left_result.pos)
if right_result:
combined_value = (left_result.value, right_result.value)
return Result(combined_value, right_result.value)
return None
class Alternate(Parser):
def __init__(self, left, right):
self.left = left
self.right = right
def __call__(self, tokens, pos):
"""
Applies the left parser if successful, otherwise applies the right parser.
It is useful for choosing among several possible parsers, e.g.
parser = Reserved('+', RESERVED) |
Reserved('-', RESERVED) |
Reserved('*', RESERVED) |
Reserved('/', RESERVED)
"""
left_result = self.left(tokens, pos)
if not left_result:
right_result = self.right(tokens, pos)
return right_result
return left_result
class Opt(Parser):
def __init__(self, parser):
self.parser = parser
def __call__(self, tokens, pos):
"""
Opt is useful for optional text, such as the else-clause of an if-statement.
It takes one parser as input. If that parser is successful when applied,
the result is returned normally. If it fails, a successful result is
still returned, but the value of that result is None. No tokens are
consumed in the failure case; the result position is the same as the
input position.
"""
result = self.parser(tokens, pos)
return result if result else Result(None, pos)
class Rep(Parser):
def __init__(self, parser):
self.parser = parser
def __call__(self, tokens, pos):
"""
`Rep` is useful for generating lists of things. It applies its input
parser repeatedly until it fails, but note that `Rep` will successfully
match an empty list and consume no tokens if its parser fails the first
time it's applied.
"""
results = []
result = self.parser(tokens, pos)
while result:
results.append(result.value)
pos = result.pos
result = self.parser(tokens, pos)
return Result(results, pos)
class Process(Parser):
"""Combinator used to manipulate result values"""
def __init__(self, parser, fn):
self.parser = parser
self.fn = fn
def __call__(self, tokens, pos):
"""
Builds the AST nodes out of the pairs and lists that
`Concat` and `Rep` return.
"""
result = self.parser(tokens, pos)
if result:
result.value = self.fn(result.value)
return result
class Lazy(Parser):
def __init__(self, parser_fn):
self.parser = None
self.parser_fn = parser_fn
def __call__(self, tokens, pos):
"""
Takes a zero-argument function which returns a parser. Lazy will not
call the function to get the parser until it's applied. This is needed to
build recursive parsers (like how arithmetic expressions can include
arithmetic expressions). Since such a parser refers to itself, we can't
just define it by referencing it directly; at the time the parser's
defining expression is evaluated, the parser is not defined yet.
"""
if not self.parser:
self.parser = self.parser_fn()
return self.parser(tokens, pos)
class Phrase(Parser):
"""
Takes a single input parser, applies it, and returns its result normally.
The only catch is that it will fail if its input parser did not consume
all of the remaining tokens. The top level parser for "gwildor" will be a
`Phrase` parser. This prevents us from partially matching a program which
has garbage at the end.
"""
def __init__(self, parser):
self.parser = parser
def __call__(self, tokens, pos):
result = self.parser(tokens, pos)
if result and result.pos == len(tokens):
return result
return None
class Exp(Parser):
"""
Used to match an expression which consists of a list of elements
separated by something, e.g. a list of statements separated by semi-colons
a := 10;
b := 20;
c := 30
`Exp` provides a way to work around left recursion by matching a list,
similar to the way `Rep` does. `Exp` takes two parsers as input. The
first parser matches the actual elements of the list. The second matches
the separators. On success, the separator parser must return a function
which combines elements parsed on the left and right into a single value.
This value is accumulated for the whole list, left to right, and is
ultimately returned.
"""
def __init__(self, parser, separator):
self.parser = parser
self.separator = separator
def __call__(self, tokens, pos):
result = self.parser(tokens, pos)
def process_next(parsed):
(sepfn, right) = parsed
return sepfn(result.value, right)
next_parser = self.separator + self.parser ^ process_next
next_result = result
while next_result:
next_result = next_parser(tokens, result.pos)
if next_result:
result = next_result
return result