diff options
author | Pearu Peterson <pearu.peterson@gmail.com> | 2006-05-25 10:54:57 +0000 |
---|---|---|
committer | Pearu Peterson <pearu.peterson@gmail.com> | 2006-05-25 10:54:57 +0000 |
commit | a12cb7b1dab184bae0f1ae9931292778afc93f5d (patch) | |
tree | e8a85c422d9352314078f82c55d7996ebe7900a1 /numpy/f2py/lib/splitline.py | |
parent | 4de4ac85272818496e6bd4868b66e1690ee26f65 (diff) | |
download | numpy-a12cb7b1dab184bae0f1ae9931292778afc93f5d.tar.gz |
Cont. impl. fortran parser, added do and ifthen blocks.
Diffstat (limited to 'numpy/f2py/lib/splitline.py')
-rw-r--r-- | numpy/f2py/lib/splitline.py | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/numpy/f2py/lib/splitline.py b/numpy/f2py/lib/splitline.py index 295929dc6..f5be43e9b 100644 --- a/numpy/f2py/lib/splitline.py +++ b/numpy/f2py/lib/splitline.py @@ -13,10 +13,41 @@ $Date: 2000/07/31 07:04:03 $ Pearu Peterson """ -__all__ = ['LineSplitter','String'] +__all__ = ['LineSplitter','String','split2','string_replace_map'] class String(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() + +def string_replace_map(line, lower=False, _cache={'index':0}): + """ + Replaces string constants with name _F2PY_STRING_CONSTANT_<index> + and returns a new line and a map + {_F2PY_STRING_CONSTANT_<index>: <original string constant>} + """ + items = [] + string_map = {} + rev_string_map = {} + for item in LineSplitter(line, lower=lower): + if isinstance(item, String): + key = rev_string_map.get(item) + if key is None: + _cache['index'] += 1 + index = _cache['index'] + key = '_F2PY_STRING_CONSTANT_%s' % (index) + string_map[key] = item + rev_string_map[item] = key + items.append(key) + else: + items.append(item) + return ''.join(items),string_map + class LineSplitter: """ Splits a line into non strings and strings. E.g. abc=\"123\" -> ['abc=','\"123\"'] @@ -37,6 +68,20 @@ class LineSplitter: item = self.get_item() # get_item raises StopIteration return item + 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: @@ -109,6 +154,14 @@ def test(): assert l==[' &abc"','123'] assert splitter.quotechar 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` if __name__ == '__main__': test() |