Project

General

Profile

« Previous | Next » 

Revision 76

Refactored xpath.parse() to use a nested function instead of a class extending Parser

View differences:

Parser.py
5 5
class SyntaxException(Exception): pass
6 6

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

Also available in: Unified diff