Lichen

errors.py

807:16f3e450a70b
2017-04-10 Paul Boddie Represent exception flags within a single byte.
     1 #!/usr/bin/env python     2      3 """     4 Error classes.     5      6 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012     7               2014, 2016, 2017 Paul Boddie <paul@boddie.org.uk>     8      9 This program is free software; you can redistribute it and/or modify it under    10 the terms of the GNU General Public License as published by the Free Software    11 Foundation; either version 3 of the License, or (at your option) any later    12 version.    13     14 This program is distributed in the hope that it will be useful, but WITHOUT    15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    16 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    17 details.    18     19 You should have received a copy of the GNU General Public License along with    20 this program.  If not, see <http://www.gnu.org/licenses/>.    21 """    22     23 class ProcessingError(Exception):    24     25     "A processing error."    26     27     pass    28     29 class ProgramError(ProcessingError):    30     31     "A general program processing error."    32     33     def __init__(self, message):    34         self.message = message    35     36     def __repr__(self):    37         return "%s(%r)" % (self.__class__.__name__, self.message)    38     39     def __str__(self):    40         return self.message    41     42 class NodeProcessingError(ProcessingError):    43     44     "A processing error associated with a particular program node."    45     46     def __init__(self, message, unit_name=None, astnode=None):    47         self.message = message    48         self.unit_name = unit_name    49         self.astnode = astnode    50     51     def get_lineno(self, node):    52     53         "Search for line number information associated with 'node'."    54     55         if node is None:    56             return None    57     58         lineno = node.lineno    59         if lineno is not None:    60             return lineno    61         else:    62             for child in node.getChildNodes():    63                 lineno = self.get_lineno(child)    64                 if lineno is not None:    65                     return lineno    66         return None    67     68     def __repr__(self):    69         return "%s(%r, %r, %r)" % (self.__class__.__name__, self.message, self.unit_name, self.astnode)    70     71     def __str__(self):    72         lineno = self.get_lineno(self.astnode)    73         return "Error in %s%s: %s%s" % (    74             self.unit_name, lineno and (" at line %s" % lineno) or "",    75             self.message, self.astnode and "\n\n%s" % self.astnode or "")    76     77 class InspectError(NodeProcessingError):    78     79     "An error during the module inspection process."    80     81     pass    82     83 class DeduceError(ProgramError):    84     85     "An error during the deduction process."    86     87     def __str__(self):    88         return "Error in deduction: %s" % self.message    89     90 class OptimiseError(ProgramError):    91     92     "An error during optimisation."    93     94     def __str__(self):    95         return "Error in optimisation: %s" % self.message    96     97 class TranslateError(NodeProcessingError):    98     99     "An error during the module translation process."   100    101     pass   102    103 # vim: tabstop=4 expandtab shiftwidth=4