1 |
11
|
aaronmk
|
# A general recursive descent parser
|
2 |
|
|
|
3 |
|
|
import re
|
4 |
|
|
|
5 |
151
|
aaronmk
|
import term
|
6 |
|
|
|
7 |
11
|
aaronmk
|
class SyntaxException(Exception): pass
|
8 |
|
|
|
9 |
|
|
class Parser:
|
10 |
76
|
aaronmk
|
def __init__(self, str_):
|
11 |
|
|
self._str = str_
|
12 |
11
|
aaronmk
|
self._pos = 0
|
13 |
|
|
|
14 |
76
|
aaronmk
|
def end(self):
|
15 |
11
|
aaronmk
|
if not self._pos == len(self._str): self._syntax_err('End of string')
|
16 |
|
|
|
17 |
76
|
aaronmk
|
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 |
11
|
aaronmk
|
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 |
38
|
aaronmk
|
else: return None
|
32 |
11
|
aaronmk
|
|
33 |
|
|
def _syntax_err(self, token):
|
34 |
62
|
aaronmk
|
raise SyntaxException(self.__class__.__name__+' syntax error: '+token
|
35 |
151
|
aaronmk
|
+' expected in '+term.as_style('90', self._str[:self._pos])
|
36 |
|
|
+self._str[self._pos:]+'<END>')
|