Package lepl :: Package stream :: Module maxdepth
[hide private]
[frames] | no frames]

Source Code for Module lepl.stream.maxdepth

 1  # The contents of this file are subject to the Mozilla Public License 
 2  # (MPL) Version 1.1 (the "License"); you may not use this file except 
 3  # in compliance with the License. You may obtain a copy of the License 
 4  # at http://www.mozilla.org/MPL/ 
 5  # 
 6  # Software distributed under the License is distributed on an "AS IS" 
 7  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 8  # the License for the specific language governing rights and 
 9  # limitations under the License. 
10  # 
11  # The Original Code is LEPL (http://www.acooke.org/lepl) 
12  # The Initial Developer of the Original Code is Andrew Cooke. 
13  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
14  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
15  # 
16  # Alternatively, the contents of this file may be used under the terms 
17  # of the LGPL license (the GNU Lesser General Public License, 
18  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
19  # of the LGPL License are applicable instead of those above. 
20  # 
21  # If you wish to allow use of your version of this file only under the 
22  # terms of the LGPL License and not to allow others to use your version 
23  # of this file under the MPL, indicate your decision by deleting the 
24  # provisions above and replace them with the notice and other provisions 
25  # required by the LGPL License.  If you do not delete the provisions 
26  # above, a recipient may use your version of this file under either the 
27  # MPL or the LGPL License. 
28   
29  ''' 
30  Raise an exception if the stream is not consumed entirely. 
31  ''' 
32   
33  from lepl.stream.core import s_empty, s_fmt, s_deepest, s_next 
34  from lepl.matchers.support import trampoline_matcher_factory 
35 36 37 @trampoline_matcher_factory() 38 -def FullFirstMatch(matcher, eos=True):
39 ''' 40 Raise an exception if the first match fails (if eos=False) or does not 41 consume the entire input stream (eos=True). The exception includes 42 information about the location of the deepest match. 43 44 This only works for the first match because we cannot reset the stream 45 facade for subsequent matches (also, if you want multiple matches you 46 probably want more sophisticated error handling than this). 47 ''' 48 49 def _matcher(support, stream1): 50 # set default maxdepth 51 s_next(stream1, count=0) 52 # first match 53 generator = matcher._match(stream1) 54 try: 55 (result2, stream2) = yield generator 56 if eos and not s_empty(stream2): 57 raise FullFirstMatchException(stream2) 58 else: 59 yield (result2, stream2) 60 except StopIteration: 61 raise FullFirstMatchException(stream1) 62 63 # subsequent matches: 64 while True: 65 result = yield generator 66 yield result
67 68 return _matcher 69
70 71 -class FullFirstMatchException(Exception):
72 ''' 73 The exception raised by `FullFirstMatch`. This includes information 74 about the deepest point read in the stream. 75 ''' 76
77 - def __init__(self, stream):
78 super(FullFirstMatchException, self).__init__( 79 s_fmt(s_deepest(stream), 80 'The match failed in {filename} at {rest} ({location}).'))
81