1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from libxyz.exceptions import ParseError
18
20 """
21 Common parser interface
22 """
23
24 error_unexpected = 1
25
26 - def parse(self, *args, **kwargs):
27 raise NotImplementedError(_(u"Must be implemented in child class"))
28
29
30
31 - def error(self, msg=None, etype=None):
32 """
33 Parsing error. Raise exception
34 """
35
36 _emsg = ""
37 if self._lexer:
38 _lineno = self._lexer.sdata.lineno
39 _pre = _(u"Parse error on line %d" % _lineno)
40 else:
41 _pre = _(u"Parse error")
42
43 if etype == self.error_unexpected and msg and len(msg) == 2:
44 _emsg = _(u"Unexpected token '%s'. Waiting for '%s'" % \
45 (msg[0].encode("unicode-escape"),
46 msg[1].encode("unicode-escape")))
47 elif msg:
48 _emsg = msg
49 else:
50 _emsg = _(u"Syntax error")
51
52 raise ParseError(u"%s: %s: %s" % (self._lexer.sdata.desc(),_pre, _emsg))
53
54
55
57 for _opt in default.keys():
58 setattr(self, _opt, opt.get(_opt, default[_opt]))
59