Package lepl :: Package matchers :: Module error
[hide private]
[frames] | no frames]

Source Code for Module lepl.matchers.error

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Error handling (generating an error while parsing). 
 32   
 33  This is not that complete or well thought through; it needs to be revised. 
 34  ''' 
 35   
 36  from lepl.support.node import Node 
 37  from lepl.support.lib import fmt 
 38  from lepl.stream.core import s_kargs 
 39   
 40   
41 -def make_error(msg):
42 ''' 43 Create an error node using a fmt string. 44 45 Invoke as ``** make_error('bad results: {results}')``, for example. 46 ''' 47 def fun(stream_in, stream_out, results): 48 ''' 49 Create the error node when results are available. 50 ''' 51 kargs = syntax_error_kargs(stream_in, stream_out, results) 52 return Error(fmt(msg, **kargs), kargs)
53 return fun 54 55
56 -def syntax_error_kargs(stream_in, stream_out, results):
57 ''' 58 Helper function for constructing fmt dictionary. 59 ''' 60 kargs = s_kargs(stream_in, prefix='in_') 61 kargs = s_kargs(stream_out, prefix='out_', kargs=kargs) 62 kargs['results'] = results 63 return kargs
64 65
66 -def raise_error(msg):
67 ''' 68 As `make_error()`, but also raise the result. 69 ''' 70 def fun(stream_in, stream_out, results): 71 ''' 72 Delay raising the error until called in the parser. 73 ''' 74 raise make_error(msg)(stream_in, stream_out, results)
75 return fun 76 77
78 -class Error(Node, SyntaxError):
79 ''' 80 Subclass `Node` and Python's SyntaxError to provide an AST 81 node that can be raised as an error via `node_throw` or `sexpr_throw`. 82 83 Create with `make_error()`. 84 ''' 85
86 - def __init__(self, msg, kargs):
87 # pylint: disable-msg=W0142 88 Node.__init__(self, msg, kargs) 89 if 'in_all' in kargs: 90 SyntaxError.__init__(self, msg, 91 (kargs.get('in_filename', ''), 92 int(kargs.get('in_lineno', 0)), 93 int(kargs.get('in_char', 0)), 94 kargs.get('in_all'))) 95 else: 96 SyntaxError.__init__(self, msg, 97 (kargs.get('in_filename', ''), 98 int(kargs.get('in_lineno', -1)), 99 int(kargs.get('in_offset', 1)), 100 kargs.get('in_rest', '')))
101
102 - def __str__(self):
103 return SyntaxError.__str__(self)
104