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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
#!/usr/bin/env python
"""
Defines LineSplitter and helper functions.
-----
Permission to use, modify, and distribute this software is given under the
terms of the NumPy License. See http://scipy.org.
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
Author: Pearu Peterson <pearu@cens.ioc.ee>
Created: May 2006
-----
"""
__all__ = ['String','string_replace_map','splitquote','splitparen']
import re
class String(str): pass
class ParenString(str): pass
def split2(line, lower=False):
"""
Split line into non-string part and into a start of a string part.
Returns 2-tuple. The second item either is empty string or start
of a string part.
"""
return LineSplitter(line,lower=lower).split2()
_f2py_str_findall = re.compile(r"_F2PY_STRING_CONSTANT_\d+_").findall
_is_name = re.compile(r'\w*\Z',re.I).match
_is_simple_str = re.compile(r'\w*\Z',re.I).match
_f2py_findall = re.compile(r'(_F2PY_STRING_CONSTANT_\d+_|F2PY_EXPR_TUPLE_\d+)').findall
class string_replace_dict(dict):
"""
Dictionary object that is callable for applying map returned
by string_replace_map() function.
"""
def __call__(self, line):
for k in _f2py_findall(line):
line = line.replace(k, self[k])
return line
def string_replace_map(line, lower=False,
_cache={'index':0,'pindex':0}):
"""
1) Replaces string constants with symbol `'_F2PY_STRING_CONSTANT_<index>_'`
2) Replaces (expression) with symbol `(F2PY_EXPR_TUPLE_<index>)`
Returns a new line and the replacement map.
"""
items = []
string_map = string_replace_dict()
rev_string_map = {}
for item in splitquote(line, lower=lower)[0]:
if isinstance(item, String) and not _is_simple_str(item[1:-1]):
key = rev_string_map.get(item)
if key is None:
_cache['index'] += 1
index = _cache['index']
key = "_F2PY_STRING_CONSTANT_%s_" % (index)
it = item[1:-1]
string_map[key] = it
rev_string_map[it] = key
items.append(item[0]+key+item[-1])
else:
items.append(item)
newline = ''.join(items)
items = []
expr_keys = []
for item in splitparen(newline):
if isinstance(item, ParenString) and not _is_name(item[1:-1]):
key = rev_string_map.get(item)
if key is None:
_cache['pindex'] += 1
index = _cache['pindex']
key = 'F2PY_EXPR_TUPLE_%s' % (index)
it = item[1:-1].strip()
string_map[key] = it
rev_string_map[it] = key
expr_keys.append(key)
items.append(item[0]+key+item[-1])
else:
items.append(item)
found_keys = set()
for k in expr_keys:
v = string_map[k]
l = _f2py_str_findall(v)
if l:
found_keys = found_keys.union(l)
for k1 in l:
v = v.replace(k1, string_map[k1])
string_map[k] = v
for k in found_keys:
del string_map[k]
return ''.join(items), string_map
def splitquote(line, stopchar=None, lower=False, quotechars = '"\''):
"""
Fast LineSplitter
"""
items = []
i = 0
while 1:
try:
char = line[i]; i += 1
except IndexError:
break
l = []
l_append = l.append
nofslashes = 0
if stopchar is None:
# search for string start
while 1:
if char in quotechars and not nofslashes % 2:
stopchar = char
i -= 1
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
l_append(char)
try:
char = line[i]; i += 1
except IndexError:
break
if not l: continue
item = ''.join(l)
if lower: item = item.lower()
items.append(item)
continue
if char==stopchar:
# string starts with quotechar
l_append(char)
try:
char = line[i]; i += 1
except IndexError:
if l:
item = String(''.join(l))
items.append(item)
break
# else continued string
while 1:
if char==stopchar and not nofslashes % 2:
l_append(char)
stopchar = None
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
l_append(char)
try:
char = line[i]; i += 1
except IndexError:
break
if l:
item = String(''.join(l))
items.append(item)
return items, stopchar
class LineSplitterBase:
def __iter__(self):
return self
def next(self):
item = ''
while not item:
item = self.get_item() # get_item raises StopIteration
return item
class LineSplitter(LineSplitterBase):
""" Splits a line into non strings and strings. E.g.
abc=\"123\" -> ['abc=','\"123\"']
Handles splitting lines with incomplete string blocks.
"""
def __init__(self, line,
quotechar = None,
lower=False,
):
self.fifo_line = [c for c in line]
self.fifo_line.reverse()
self.quotechar = quotechar
self.lower = lower
def split2(self):
"""
Split line until the first start of a string.
"""
try:
item1 = self.get_item()
except StopIteration:
return '',''
i = len(item1)
l = self.fifo_line[:]
l.reverse()
item2 = ''.join(l)
return item1,item2
def get_item(self):
fifo_pop = self.fifo_line.pop
try:
char = fifo_pop()
except IndexError:
raise StopIteration
fifo_append = self.fifo_line.append
quotechar = self.quotechar
l = []
l_append = l.append
nofslashes = 0
if quotechar is None:
# search for string start
while 1:
if char in '"\'' and not nofslashes % 2:
self.quotechar = char
fifo_append(char)
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
l_append(char)
try:
char = fifo_pop()
except IndexError:
break
item = ''.join(l)
if self.lower: item = item.lower()
return item
if char==quotechar:
# string starts with quotechar
l_append(char)
try:
char = fifo_pop()
except IndexError:
return String(''.join(l))
# else continued string
while 1:
if char==quotechar and not nofslashes % 2:
l_append(char)
self.quotechar = None
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
l_append(char)
try:
char = fifo_pop()
except IndexError:
break
return String(''.join(l))
def splitparen(line,paren='()'):
"""
Fast LineSplitterParen.
"""
stopchar = None
startchar, endchar = paren[0],paren[1]
items = []
i = 0
while 1:
try:
char = line[i]; i += 1
except IndexError:
break
nofslashes = 0
l = []
l_append = l.append
if stopchar is None:
# search for parenthesis start
while 1:
if char==startchar and not nofslashes % 2:
stopchar = endchar
i -= 1
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
l_append(char)
try:
char = line[i]; i += 1
except IndexError:
break
item = ''.join(l)
else:
nofstarts = 0
while 1:
if char==stopchar and not nofslashes % 2 and nofstarts==1:
l_append(char)
stopchar = None
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
if char==startchar:
nofstarts += 1
elif char==endchar:
nofstarts -= 1
l_append(char)
try:
char = line[i]; i += 1
except IndexError:
break
item = ParenString(''.join(l))
items.append(item)
return items
class LineSplitterParen(LineSplitterBase):
""" Splits a line into strings and strings with parenthesis. E.g.
a(x) = b(c,d) -> ['a','(x)',' = b','(c,d)']
"""
def __init__(self, line, paren = '()'):
self.fifo_line = [c for c in line]
self.fifo_line.reverse()
self.startchar = paren[0]
self.endchar = paren[1]
self.stopchar = None
def get_item(self):
fifo_pop = self.fifo_line.pop
try:
char = fifo_pop()
except IndexError:
raise StopIteration
fifo_append = self.fifo_line.append
startchar = self.startchar
endchar = self.endchar
stopchar = self.stopchar
l = []
l_append = l.append
nofslashes = 0
if stopchar is None:
# search for parenthesis start
while 1:
if char==startchar and not nofslashes % 2:
self.stopchar = endchar
fifo_append(char)
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
l_append(char)
try:
char = fifo_pop()
except IndexError:
break
item = ''.join(l)
return item
nofstarts = 0
while 1:
if char==stopchar and not nofslashes % 2 and nofstarts==1:
l_append(char)
self.stopchar = None
break
if char=='\\':
nofslashes += 1
else:
nofslashes = 0
if char==startchar:
nofstarts += 1
elif char==endchar:
nofstarts -= 1
l_append(char)
try:
char = fifo_pop()
except IndexError:
break
return ParenString(''.join(l))
def test():
splitter = LineSplitter('abc\\\' def"12\\"3""56"dfad\'a d\'')
l = [item for item in splitter]
assert l==['abc\\\' def','"12\\"3"','"56"','dfad','\'a d\''],`l`
assert splitter.quotechar is None
l,stopchar=splitquote('abc\\\' def"12\\"3""56"dfad\'a d\'')
assert l==['abc\\\' def','"12\\"3"','"56"','dfad','\'a d\''],`l`
assert stopchar is None
splitter = LineSplitter('"abc123&')
l = [item for item in splitter]
assert l==['"abc123&'],`l`
assert splitter.quotechar=='"'
l,stopchar = splitquote('"abc123&')
assert l==['"abc123&'],`l`
assert stopchar=='"'
splitter = LineSplitter(' &abc"123','"')
l = [item for item in splitter]
assert l==[' &abc"','123']
assert splitter.quotechar is None
l,stopchar = splitquote(' &abc"123','"')
assert l==[' &abc"','123']
assert stopchar is None
l = split2('')
assert l==('',''),`l`
l = split2('12')
assert l==('12',''),`l`
l = split2('1"a"//"b"')
assert l==('1','"a"//"b"'),`l`
l = split2('"ab"')
assert l==('','"ab"'),`l`
splitter = LineSplitterParen('a(b) = b(x,y(1)) b\((a)\)')
l = [item for item in splitter]
assert l==['a', '(b)', ' = b', '(x,y(1))', ' b\\(', '(a)', '\\)'],`l`
l = splitparen('a(b) = b(x,y(1)) b\((a)\)')
assert l==['a', '(b)', ' = b', '(x,y(1))', ' b\\(', '(a)', '\\)'],`l`
l = string_replace_map('a()')
print l
print 'ok'
if __name__ == '__main__':
test()
|