1
|
# A general recursive descent parser
|
2
|
|
3
|
import re
|
4
|
|
5
|
import term
|
6
|
|
7
|
class SyntaxError(Exception): pass
|
8
|
|
9
|
class Parser:
|
10
|
def __init__(self, str_):
|
11
|
self._str = str_
|
12
|
self._pos = 0
|
13
|
|
14
|
def end(self):
|
15
|
if not self._pos == len(self._str): self.syntax_err('End of string')
|
16
|
|
17
|
def str_(self, str_, required=False):
|
18
|
end_pos = self._pos + len(str_)
|
19
|
if self._str[self._pos:end_pos] == str_:
|
20
|
self._pos = end_pos
|
21
|
return True
|
22
|
elif required: self.syntax_err(str_)
|
23
|
else: return False
|
24
|
|
25
|
def re(self, pattern, required=False):
|
26
|
matcher = re.compile(pattern).match(self._str, self._pos)
|
27
|
if matcher:
|
28
|
self._pos = matcher.end(0)
|
29
|
return matcher.group(0)
|
30
|
elif required: self.syntax_err(pattern)
|
31
|
else: return None
|
32
|
|
33
|
def syntax_err(self, token):
|
34
|
after = self._str[self._pos:]
|
35
|
if after == '': after += '<END>'
|
36
|
raise SyntaxError(self.__class__.__name__+' syntax error: '+token
|
37
|
+' expected in '+ term.as_style('90', self._str[:self._pos]) +after)
|