Package libxyz :: Package parser :: Module base |
[hide private]
[frames] | no frames]

# Source Code for Module libxyz.parser.base

     1  #-*- coding: utf8 -*
     2  #
     3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
     4  #
     5  # This file is part of XYZCommander.
     6  # XYZCommander is free software: you can redistribute it and/or modify
     7  # it under the terms of the GNU Lesser Public License as published by
     8  # the Free Software Foundation, either version 3 of the License, or
     9  # (at your option) any later version.
    10  # XYZCommander is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  # GNU Lesser Public License for more details.
    14  # You should have received a copy of the GNU Lesser Public License
    15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
    16
    17  from libxyz.exceptions import ParseError
    18
    19 -class BaseParser(object):
    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
    56 -    def set_opt(self, default, opt):
    57          for _opt in default.keys():
    58              setattr(self, _opt, opt.get(_opt, default[_opt]))
    59