Project

General

Profile

1 11 aaronmk
# A general recursive descent parser
2
3
import re
4
5 151 aaronmk
import term
6
7 1719 aaronmk
class SyntaxError(Exception): pass
8 11 aaronmk
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 1397 aaronmk
        if not self._pos == len(self._str): self.syntax_err('End of string')
16 11 aaronmk
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 1397 aaronmk
        elif required: self.syntax_err(str_)
23 76 aaronmk
        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 1397 aaronmk
        elif required: self.syntax_err(pattern)
31 38 aaronmk
        else: return None
32 11 aaronmk
33 1397 aaronmk
    def syntax_err(self, token):
34 752 aaronmk
        after = self._str[self._pos:]
35
        if after == '': after += '<END>'
36 1719 aaronmk
        raise SyntaxError(self.__class__.__name__+' syntax error: '+token
37 752 aaronmk
            +' expected in '+ term.as_style('90', self._str[:self._pos]) +after)