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

Source Code for Module lepl.stream.factory

  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  from collections import Iterable 
 32   
 33  from lepl.stream.simple import SequenceHelper, StringHelper, ListHelper 
 34  from lepl.stream.iter import IterableHelper, Cons 
 35  from lepl.support.lib import basestring, fmt, add_defaults, file 
 36  from lepl.lexer.stream import TokenHelper 
 37   
 38   
39 -class StreamFactory(object):
40 ''' 41 Given a value (typically a sequence), generate a stream. 42 ''' 43
44 - def from_string(self, text, **kargs):
45 ''' 46 Provide a stream for the contents of the string. 47 ''' 48 add_defaults(kargs, {'factory': self}) 49 return (0, StringHelper(text, **kargs))
50
51 - def from_list(self, list_, **kargs):
52 ''' 53 Provide a stream for the contents of the list. 54 ''' 55 add_defaults(kargs, {'factory': self}) 56 return (0, ListHelper(list_, **kargs))
57
58 - def from_sequence(self, sequence, **kargs):
59 ''' 60 Return a generic stream for any indexable sequence. 61 ''' 62 add_defaults(kargs, {'factory': self}) 63 return (0, SequenceHelper(sequence, **kargs))
64
65 - def from_iterable(self, iterable, **kargs):
66 ''' 67 Provide a stream for the contents of the iterable. This assumes that 68 each value from the iterable is a "line" which will, in turn, be 69 passed to the stream factory. 70 ''' 71 add_defaults(kargs, {'factory': self}) 72 cons = Cons(iterable) 73 return ((cons, self(cons.head, **kargs)), IterableHelper(**kargs))
74
75 - def from_file(self, file_, **kargs):
76 ''' 77 Provide a stream for the contents of the file. There is no 78 corresponding `from_path` because the opening and closing of the 79 path must be done outside the parsing (or the contents will become 80 unavailable), so use instead: 81 with open(path) as f: 82 parser.parse_file(f) 83 which will close the file after parsing. 84 ''' 85 try: 86 gkargs = kargs.get('global_kargs', {}) 87 add_defaults(gkargs, {'filename': file_.name}) 88 add_defaults(kargs, {'global_kargs': gkargs}) 89 except AttributeError: 90 pass 91 return self.from_iterable(file_, **kargs)
92
93 - def to_token(self, iterable, **kargs):
94 ''' 95 Create a stream for tokens. The `iterable` is a source of 96 (token_ids, sub_stream) tuples, where `sub_stream` will be 97 matched within the token. 98 ''' 99 return (Cons(iterable), TokenHelper(**kargs))
100
101 - def __call__(self, sequence, **kargs):
102 ''' 103 Auto-detect type and wrap appropriately. 104 ''' 105 if isinstance(sequence, basestring): 106 return self.from_string(sequence, **kargs) 107 elif isinstance(sequence, list): 108 return self.from_list(sequence, **kargs) 109 elif isinstance(sequence, file): 110 return self.from_file(sequence, **kargs) 111 elif hasattr(sequence, '__getitem__') and hasattr(sequence, '__len__'): 112 return self.from_sequence(sequence, **kargs) 113 elif isinstance(sequence, Iterable): 114 return self.from_iterable(sequence, **kargs) 115 else: 116 raise TypeError(fmt('Cannot generate a stream for type {0}', 117 type(sequence)))
118 119 DEFAULT_STREAM_FACTORY = StreamFactory() 120