root/cly/trunk/cly/exceptions.py

Revision 576, 2.4 kB (checked in by athomas, 2 months ago)

Delegated all attribute casting to cast_attribute(), including transformation
to callbacks and interpretation of EVAL_NS.

Line 
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2006-2007 Alec Thomas <alec@swapoff.org>
4 #
5 # This software is licensed as described in the file COPYING, which
6 # you should have received as part of this distribution.
7 #
8
9 """CLY exception hierarchy."""
10
11
12 import string
13
14
15 __all__ = ['Error', 'InvalidHelp', 'InvalidNodePath', 'InvalidAnonymousNode',
16            'ParseError', 'UnexpectedEOL', 'InvalidToken', 'ValidationError',
17            'XMLParseError']
18 __docformat__ = 'restructuredtext en'
19
20
21 class Error(Exception):
22     """The base of all CLY exceptions."""
23
24
25 class InvalidHelp(Error):
26     """Thrown when the help provided to a Node is of an invalid type."""
27
28
29 class InvalidNodePath(Error):
30     """Thrown when an attempt is made to reference an invalid node path. This
31     can occur when an Alias target is invalid."""
32
33
34 class InvalidAnonymousNode(Error):
35     """When Node is used as a callable to add child nodes, positional arguments
36     are treated as anonymous child nodes. This exception is thrown if an object
37     that is not a Node is passed."""
38
39
40 class XMLParseError(Error):
41     """Report an XML grammar parsing error."""
42
43
44 class ParseError(Error):
45     """Report a parse error. Output is formatted using string templates,
46     where template variables are passed as arguments to the constructor.
47
48     >>> from cly.parser import Context
49     >>> print ParseError(Context(None, 'foo bar'), "remaining=$remaining, time=$time", time=123)
50     remaining=foo bar, time=123
51     """
52     message = 'parse error'
53
54     def __init__(self, context, message=None, **kwargs):
55         template = string.Template(message or self.message)
56         message = template.safe_substitute(remaining=context.remaining,
57                                            **kwargs)
58         Error.__init__(self, message)
59         self.context = context
60
61
62 class UnexpectedEOL(ParseError):
63     """Raised when all input is consumed and no terminal (``Action``) node is
64     reached."""
65     message = 'more input required'
66
67
68 class InvalidToken(ParseError):
69     """Raised when a token is reached that is invalid at the current grammar
70     branch."""
71     message = "invalid token '$remaining'"
72
73
74 class ValidationError(ParseError):
75     """Raised when a variable fails to parse. In practise this is rare, as the
76     regex for a variable is usually sufficient to rule it out of selection
77     before parsing occurs."""
78     message = "validation of '$token' failed; $exception"
79
80
81 if __name__ == '__main__':
82     import doctest
83     doctest.testmod()
Note: See TracBrowser for help on using the browser.