My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# The contents of this file are subject to the Mozilla Public License
# (MPL) Version 1.1 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License
# at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and
# limitations under the License.
#
# The Original Code is LEPL (http://www.acooke.org/lepl)
# The Initial Developer of the Original Code is Andrew Cooke.
# Portions created by the Initial Developer are Copyright (C) 2009-2010
# Andrew Cooke (andrew@acooke.org). All Rights Reserved.
#
# Alternatively, the contents of this file may be used under the terms
# of the LGPL license (the GNU Lesser General Public License,
# http://www.gnu.org/licenses/lgpl.html), in which case the provisions
# of the LGPL License are applicable instead of those above.
#
# If you wish to allow use of your version of this file only under the
# terms of the LGPL License and not to allow others to use your version
# of this file under the MPL, indicate your decision by deleting the
# provisions above and replace them with the notice and other provisions
# required by the LGPL License. If you do not delete the provisions
# above, a recipient may use your version of this file under either the
# MPL or the LGPL License.

'''
Default implementation of the helper classes for sequences (strings and lists).

The state is an integer offset. Sequence and a possible delta for the
offset are stored in the helper.
'''

from itertools import chain

from lepl.support.lib import fmt, add_defaults, str, LogMixin
from lepl.stream.core import StreamHelper, OFFSET, LINE_NO, CHAR, HashKey


class BaseHelper(LogMixin, StreamHelper):

def __init__(self, id=None, factory=None, max=None, global_kargs=None,
cache_level=None, delta=None):
super(BaseHelper, self).__init__(id=id, factory=factory, max=max,
global_kargs=global_kargs, cache_level=cache_level)
self._delta = delta if delta else (0,1,1)


class SequenceHelper(BaseHelper):

def __init__(self, sequence, id=None, factory=None, max=None,
global_kargs=None, cache_level=None, delta=None):
super(SequenceHelper, self).__init__(id=id, factory=factory, max=max,
global_kargs=global_kargs, cache_level=cache_level, delta=delta)
self._sequence = sequence
type_ = self._typename(sequence)
add_defaults(self.global_kargs, {
'global_type': type_,
'filename': type_})
self._kargs = dict(self.global_kargs)
add_defaults(self._kargs, {'type': type_})

def key(self, state, other):
# avoid confusion with incremental ids
offset = (state + self._delta[OFFSET]) << 16
key = HashKey(self.id ^ offset ^ hash(other), (self.id, hash(other)))
#self._debug(fmt('Hash at offset {0}: {1}', offset, hash(key)))
return key

def _fmt(self, sequence, offset, max_len=60, left='', right='', index=True):
'''fmt a possibly long subsection of data.'''
if not sequence:
if index:
return fmt('{0!r}[{1:d}]', sequence, offset)
else:
return fmt('{0!r}', sequence)
if offset >= 0 and offset < len(sequence):
centre = offset
elif offset > 0:
centre = len(sequence) - 1
else:
centre = 0
begin, end = centre, centre+1
longest = None
while True:
if begin > 0:
if end < len(sequence):
template = '{0!s}...{1!s}...{2!s}'
else:
template = '{0!s}...{1!s}{2!s}'
else:
if end < len(sequence):
template = '{0!s}{1!s}...{2!s}'
else:
template = '{0!s}{1!s}{2!s}'
body = repr(sequence[begin:end])[len(left):]
if len(right):
body = body[:-len(right)]
text = fmt(template, left, body, right, offset)
if index:
text = fmt('{0!s}[{1:d}:]', text, offset)
if longest is None or len(text) <= max_len:
longest = text
if len(text) > max_len:
return longest
begin -= 1
end += 1
if begin < 0 and end > len(sequence):
return longest
begin = max(begin, 0)
end = min(end, len(sequence))

def _location(self, kargs, prefix):
'''Location (separate method so subclasses can replace).'''
return fmt('offset {' + prefix + 'global_offset}, value {' + prefix + 'repr}',
**kargs)

def _typename(self, instance):
if isinstance(instance, list) and instance:
return fmt('<list{0}>', self._typename(instance[0]))
else:
try:
return fmt('<{0}>', instance.__class__.__name__)
except:
return '<unknown>'

def kargs(self, state, prefix='', kargs=None):
'''
Generate a dictionary of values that describe the stream. These
may be extended by subclasses. They are provided to
`syntax_error_kargs`, for example.

Note: Calculating this can be expensive; use only for error messages,
not debug messages (that may be discarded).

Implementation note: Because some values are
'''
offset = state + self._delta[OFFSET]
if kargs is None: kargs = {}
add_defaults(kargs, self._kargs, prefix=prefix)
within = offset > -1 and offset < len(self._sequence)
data = self._fmt(self._sequence, state)
text = self._fmt(self._sequence, state, index=False)
# some values below may be already present in self._global_kargs
defaults = {'data': data,
'global_data': data,
'text': text,
'global_text': text,
'offset': state,
'global_offset': offset,
'rest': self._fmt(self._sequence[offset:], 0, index=False),
'repr': repr(self._sequence[offset]) if within else '<EOS>',
'str': str(self._sequence[offset]) if within else '',
'line_no': 1,
'char': offset+1}
add_defaults(kargs, defaults, prefix=prefix)
add_defaults(kargs, {prefix + 'location': self._location(kargs, prefix)})
return kargs

def next(self, state, count=1):
new_state = state+count
if new_state <= len(self._sequence):
stream = (new_state, self)
self.max.update(self._delta[OFFSET] + new_state - 1, stream)
return (self._sequence[state:new_state], stream)
else:
raise StopIteration

def join(self, state, *values):
assert values, 'Cannot join zero general sequences'
result = values[0]
for value in values[1:]:
result += value
return result

def empty(self, state):
return state >= len(self._sequence)

def line(self, state, empty_ok):
'''Returns the rest of the data.'''
new_state = len(self._sequence)
if state < new_state or (empty_ok and state == new_state):
stream = (new_state, self)
self.max.update(self._delta[OFFSET] + new_state, stream)
return (self._sequence[state:new_state], stream)
else:
raise StopIteration

def len(self, state):
return len(self._sequence) - state

def stream(self, state, value, id_=None, max=None):
id_ = self.id if id_ is None else id_
max = max if max else self.max
# increment the cache level to expose lower level streams
return self.factory(value, id=id_, factory=self.factory,
max=max, global_kargs=self.global_kargs,
cache_level=self.cache_level+1,
delta=self.delta(state))

def deepest(self):
return self.max.get()

def debug(self, state):
try:
return fmt('{0:d}:{1!r}', state, self._sequence[state])
except IndexError:
return fmt('{0:d}:<EOS>', state)

def delta(self, state):
offset = state + self._delta[OFFSET]
return (offset, 1, offset+1)

def new_max(self, state):
return (self.max,
(state, type(self)(self._sequence, id=self.id,
factory=self.factory, max=None,
global_kargs=self.global_kargs,
delta=self._delta)))



class StringHelper(SequenceHelper):
'''
String-specific formatting and location.
'''

__counter = 0

def __init__(self, sequence, id=None, factory=None, max=None,
global_kargs=None, cache_level=None, delta=None):
# avoid duplicating processing on known strings
if id is None:
try:
id = hash(sequence)
except TypeError:
self.__counter += 1
id = self.__counter
super(StringHelper, self).__init__(sequence, id=id, factory=factory,
max=max, global_kargs=global_kargs, cache_level=cache_level,
delta=delta)

def _fmt(self, sequence, offset, max_len=60, left="'", right="'", index=True):
return super(StringHelper, self)._fmt(sequence, offset, max_len=max_len,
left=left, right=right, index=index)

def _location(self, kargs, prefix):
return fmt('line {' + prefix + 'line_no:d}, character {' + prefix + 'char:d}', **kargs)

def delta(self, state):
offset = self._delta[OFFSET] + state
line_no = self._delta[LINE_NO] + self._sequence.count('\n', 0, state)
start = self._sequence.rfind('\n', 0, state)
if start > -1:
char = state - start
else:
char = self._delta[CHAR] + state
return (offset, line_no, char)

def kargs(self, state, prefix='', kargs=None):
if kargs is None: kargs = {}
(_, line_no, char) = self.delta(state)
start = self._sequence.rfind('\n', 0, state) + 1 # omit \n
end = self._sequence.find('\n', state) # omit \n
# all is str() because passed to SyntaxError constructor
if end < 0:
rest = repr(self._sequence[state:])
all = str(self._sequence[start:])
else:
rest = repr(self._sequence[state:end])
all = str(self._sequence[start:end])
add_defaults(kargs, {
'type': '<string>',
'filename': '<string>',
'rest': rest,
'all': all,
'line_no': line_no,
'char': char}, prefix=prefix)
return super(StringHelper, self).kargs(state, prefix=prefix, kargs=kargs)

def join(self, state, *values):
return str().join(values)

def line(self, state, empty_ok):
'''Returns up to, and including then next \n'''
max_len = len(self._sequence)
if state < max_len or (empty_ok and state == max_len):
end = self._sequence.find('\n', state) + 1
if not end: end = len(self._sequence)
return (self._sequence[state:end], (end, self))
else:
raise StopIteration

def stream(self, state, value, id_=None, max=None):
id_ = self.id if id_ is None else id_
max = max if max else self.max
return self.factory(value, id=id_, factory=self.factory,
max=max, global_kargs=self.global_kargs,
delta=self.delta(state))


class ListHelper(SequenceHelper):
'''
List-specific formatting
'''

def _fmt(self, sequence, offset, max_len=60, left="[", right="]", index=True):
return super(ListHelper, self)._fmt(sequence, offset, max_len=max_len,
left=left, right=right, index=index)

def join(self, state, *values):
return list(chain(*values))


Change log

8c1f54a55b94 by and...@acooke.org on Jul 3, 2011   Diff
all simple tests working (excluding
pickle)
Go to: 
Project members, sign in to write a code review

Older revisions

c7bdff29546a by and...@acooke.org on Jun 23, 2011   Diff
getting tests running
8f2692346ba5 by andrew on Mar 19, 2011   Diff
docs without api links
4e7ed779a9fe by andrew on Mar 16, 2011   Diff
docs + fixes
All revisions of this file

File info

Size: 12548 bytes, 316 lines
Powered by Google Project Hosting