def atom(src, token):
if token == "(":
- out = []
+ #XXX names, use separate func?
+ out = target = []
for token in src:
if token == ")":
+ if target is not out:
+ out[-1] = tuple(out[-1])
+ return out
return tuple(out)
- out.append(atom(src, token))
+ elif token == ')(':
+ if target is out:
+ target = []
+ out = [tuple(out), target]
+ else:
+ out[-1] = tuple(out[-1])
+ target = []
+ out.append(target)
+ else:
+ target.append(atom(src, token))
# oops - no terminator!
preceeding = ' '.join(str(val) for val in out)
raise ParseError('Tuple incomplete before "(%s"' % preceeding)
self._test('(123 "foo" ((0 1 2) "more" NIL) 66)',
(123, "foo", ((0, 1, 2), "more", None), 66))
- def test_backed_up_tuples(self):
+ def test_juxtatposed_tuples(self):
+ # Tuples with no whitespace between them should be returned as
+ # a list of tuples
self._test('((12 "foo")(34 NIL 0) "foo")',
([(12, "foo"), (34, None, 0)], "foo"))
+ self._test('((12 "foo")(34 NIL 0)(4 55 6) "foo")',
+ ([(12, "foo"), (34, None, 0), (4, 55, 6)], "foo"))
+ self._test('((12 "foo")(34 (NIL 1 2) 0)(4 55 6) "foo")',
+ ([(12, "foo"), (34, (None, 1, 2), 0), (4, 55, 6)], "foo"))
def test_complex_mixed(self):
- self._test('((FOO "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)'
+ self._test('((FOO "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23) '
'("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff") '
'"<hi.there>" "foo" "BASE64" 4554 73) "MIXED")',
(('FOO', 'PLAIN', ('CHARSET', 'US-ASCII'), None, None, '7BIT', 1152, 23),