Project

General

Profile

1
# A general recursive descent parser
2

    
3
import re
4

    
5
import term
6

    
7
class SyntaxException(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
        raise SyntaxException(self.__class__.__name__+' syntax error: '+token
35
            +' expected in '+term.as_style('90', self._str[:self._pos])
36
            +self._str[self._pos:]+'<END>')
(1-1/11)