Project

General

Profile

1
# A general recursive descent parser
2

    
3
import re
4

    
5
class SyntaxException(Exception): pass
6

    
7
class Parser:
8
    def __init__(self, string):
9
        self._str = string
10
        self._pos = 0
11
    
12
    def parse(self):
13
        tree = self._main()
14
        if not self._pos == len(self._str): self._syntax_err('End of string')
15
        return tree
16
    
17
    def _match_re(self, pattern, required=False):
18
        matcher = re.compile(pattern).match(self._str, self._pos)
19
        if matcher:
20
            self._pos = matcher.end(0)
21
            return matcher.group(0)
22
        elif required: self._syntax_err(pattern)
23
        else: return None
24
    
25
    def _match_str(self, string, required=False):
26
        end_pos = self._pos + len(string)
27
        if self._str[self._pos:end_pos] == string:
28
            self._pos = end_pos
29
            return True
30
        elif required: self._syntax_err(string)
31
        else: return False
32
    
33
    def _syntax_err(self, token):
34
        raise SyntaxException(token+' expected in '+self._str[self._pos:])
(1-1/8)