-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyPEG.py
351 lines (306 loc) · 11.1 KB
/
pyPEG.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
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
# YPL parser 1.5
# written by VB.
import re
import sys, codecs
import exceptions
class keyword(unicode): pass
class code(unicode): pass
class ignore(object):
def __init__(self, regex_text, *args):
self.regex = re.compile(regex_text, *args)
class _and(object):
def __init__(self, something):
self.obj = something
class _not(_and): pass
class Name(unicode):
def __init__(self, *args):
self.line = 0
self.file = u""
class Symbol(list):
def __init__(self, name, what):
self.__name__ = name
self.append(name)
self.what = what
self.append(what)
def __call__(self):
return self.what
def __unicode__(self):
return u'Symbol(' + repr(self.__name__) + ', ' + repr(self.what) + u')'
def __repr__(self):
return unicode(self)
word_regex = re.compile(ur"\w+")
rest_regex = re.compile(ur".*")
print_trace = False
def u(text):
if isinstance(text, exceptions.BaseException):
text = text.args[0]
if type(text) is unicode:
return text
if isinstance(text, str):
if sys.stdin.encoding:
return codecs.decode(text, sys.stdin.encoding)
else:
return codecs.decode(text, "utf-8")
return unicode(text)
def skip(skipper, text, skipWS, skipComments):
if skipWS:
t = text.lstrip()
else:
t = text
if skipComments:
try:
while True:
skip, t = skipper.parseLine(t, skipComments, [], skipWS, None)
if skipWS:
t = t.lstrip()
except: pass
return t
class parser(object):
def __init__(self, another = False, p = False):
self.restlen = -1
if not(another):
self.skipper = parser(True, p)
self.skipper.packrat = p
else:
self.skipper = self
self.lines = None
self.textlen = 0
self.memory = {}
self.packrat = p
# parseLine():
# textline: text to parse
# pattern: pyPEG language description
# resultSoFar: parsing result so far (default: blank list [])
# skipWS: Flag if whitespace should be skipped (default: True)
# skipComments: Python functions returning pyPEG for matching comments
#
# returns: pyAST, textrest
#
# raises: SyntaxError(reason) if textline is detected not being in language
# described by pattern
#
# SyntaxError(reason) if pattern is an illegal language description
def parseLine(self, textline, pattern, resultSoFar = [], skipWS = True, skipComments = None):
name = None
_textline = textline
_pattern = pattern
def R(result, text):
if __debug__:
if print_trace:
try:
if _pattern.__name__ != "comment":
sys.stderr.write(u"match: " + _pattern.__name__ + u"\n")
except: pass
if self.restlen == -1:
self.restlen = len(text)
else:
self.restlen = min(self.restlen, len(text))
res = resultSoFar
if name and result:
name.line = self.lineNo()
res.append(Symbol(name, result))
elif name:
name.line = self.lineNo()
res.append(Symbol(name, []))
elif result:
if type(result) is type([]):
res.extend(result)
else:
res.extend([result])
if self.packrat:
self.memory[(len(_textline), id(_pattern))] = (res, text)
return res, text
def syntaxError():
if self.packrat:
self.memory[(len(_textline), id(_pattern))] = False
raise SyntaxError()
if self.packrat:
try:
result = self.memory[(len(textline), id(pattern))]
if result:
return result
else:
raise SyntaxError()
except: pass
if callable(pattern):
if __debug__:
if print_trace:
try:
if pattern.__name__ != "comment":
sys.stderr.write(u"testing with " + pattern.__name__ + u": " + textline[:40] + u"\n")
except: pass
if pattern.__name__[0] != "_":
name = Name(pattern.__name__)
pattern = pattern()
if callable(pattern):
pattern = (pattern,)
text = skip(self.skipper, textline, skipWS, skipComments)
pattern_type = type(pattern)
if pattern_type is str or pattern_type is unicode:
if text[:len(pattern)] == pattern:
text = skip(self.skipper, text[len(pattern):], skipWS, skipComments)
return R(None, text)
else:
syntaxError()
elif pattern_type is keyword:
m = word_regex.match(text)
if m:
if m.group(0) == pattern:
text = skip(self.skipper, text[len(pattern):], skipWS, skipComments)
return R(None, text)
else:
syntaxError()
else:
syntaxError()
elif pattern_type is _not:
try:
r, t = self.parseLine(text, pattern.obj, [], skipWS, skipComments)
except:
return resultSoFar, textline
syntaxError()
elif pattern_type is _and:
r, t = self.parseLine(text, pattern.obj, [], skipWS, skipComments)
return resultSoFar, textline
elif pattern_type is type(word_regex) or pattern_type is ignore:
if pattern_type is ignore:
pattern = pattern.regex
m = pattern.match(text)
if m:
text = skip(self.skipper, text[len(m.group(0)):], skipWS, skipComments)
if pattern_type is ignore:
return R(None, text)
else:
return R(m.group(0), text)
else:
syntaxError()
elif pattern_type is tuple:
result = []
n = 1
for p in pattern:
if type(p) is type(0):
n = p
else:
if n>0:
for i in range(n):
result, text = self.parseLine(text, p, result, skipWS, skipComments)
elif n==0:
if text == "":
pass
else:
try:
newResult, newText = self.parseLine(text, p, result, skipWS, skipComments)
result, text = newResult, newText
except SyntaxError:
pass
elif n<0:
found = False
while True:
try:
newResult, newText = self.parseLine(text, p, result, skipWS, skipComments)
result, text, found = newResult, newText, True
except SyntaxError:
break
if n == -2 and not(found):
syntaxError()
n = 1
return R(result, text)
elif pattern_type is list:
result = []
found = False
for p in pattern:
try:
result, text = self.parseLine(text, p, result, skipWS, skipComments)
found = True
except SyntaxError:
pass
if found:
break
if found:
return R(result, text)
else:
syntaxError()
else:
raise SyntaxError(u"illegal type in grammar: " + u(pattern_type))
def lineNo(self):
if not(self.lines): return u""
if self.restlen == -1: return u""
parsed = self.textlen - self.restlen
left, right = 0, len(self.lines)
while True:
mid = int((right + left) / 2)
if self.lines[mid][0] <= parsed:
try:
if self.lines[mid + 1][0] >= parsed:
try:
return u(self.lines[mid + 1][1]) + u":" + u(self.lines[mid + 1][2])
except:
return u""
else:
left = mid + 1
except:
try:
return u(self.lines[mid + 1][1]) + u":" + u(self.lines[mid + 1][2])
except:
return u""
else:
right = mid - 1
if left > right:
return u""
# plain module API
def parseLine(textline, pattern, resultSoFar = [], skipWS = True, skipComments = None, packrat = False):
p = parser(p=packrat)
text = skip(p.skipper, textline, skipWS, skipComments)
ast, text = p.parseLine(text, pattern, resultSoFar, skipWS, skipComments)
return ast, text
# parse():
# language: pyPEG language description
# lineSource: a fileinput.FileInput object
# skipWS: Flag if whitespace should be skipped (default: True)
# skipComments: Python function which returns pyPEG for matching comments
# packrat: use memoization
# lineCount: add line number information to AST
#
# returns: pyAST
#
# raises: SyntaxError(reason), if a parsed line is not in language
# SyntaxError(reason), if the language description is illegal
def parse(language, lineSource, skipWS = True, skipComments = None, packrat = False, lineCount = True):
lines, lineNo = [], 0
while callable(language):
language = language()
orig, ld = u"", 0
for line in lineSource:
if lineSource.isfirstline():
ld = 1
else:
ld += 1
lines.append((len(orig), lineSource.filename(), lineSource.lineno() - 1))
orig += u(line)
textlen = len(orig)
try:
p = parser(p=packrat)
p.textlen = len(orig)
if lineCount:
p.lines = lines
else:
p.line = None
text = skip(p.skipper, orig, skipWS, skipComments)
result, text = p.parseLine(text, language, [], skipWS, skipComments)
if text:
raise SyntaxError()
except SyntaxError, msg:
parsed = textlen - p.restlen
textlen = 0
nn, lineNo, file = 0, 0, u""
for n, ld, l in lines:
if n >= parsed:
break
else:
lineNo = l
nn += 1
file = ld
lineNo += 1
nn -= 1
lineCont = orig.splitlines()[nn]
raise SyntaxError(u"syntax error in " + u(file) + u":" + u(lineNo) + u": " + lineCont)
return result