self.sources = None
self.current_source = None
- def parse_quote(self, stream_i, quoted, token):
+ def read_until(self, stream_i, end_char, escape=True):
+ token = ''
try:
for nextchar in stream_i:
- if nextchar == "\\":
+ if escape and nextchar == "\\":
escaper = nextchar
nextchar = stream_i.next()
- if nextchar != escaper and nextchar != quoted:
+ if nextchar != escaper and nextchar != end_char:
token += escaper
- elif nextchar == quoted:
+ elif nextchar == end_char:
break
token += nextchar
else:
- raise ValueError("No closing quotation")
+ raise ValueError("No closing %r" % end_char)
except StopIteration:
- # escaped char...
- raise ValueError("No closing quotation")
-
- return quoted + token + quoted
+ raise ValueError("No closing %r" % end_char)
+ return token + end_char
def read_token_stream(self, stream_i):
whitespace = self.WHITESPACE
wordchars = self.NON_SPECIALS
- parse_quote = self.parse_quote
+ read_until = self.read_until
while True:
token = ''
+ # process whitespace
for nextchar in stream_i:
if nextchar in whitespace:
continue
token = nextchar
elif nextchar == '"':
- chunk = parse_quote(stream_i, nextchar, token)
- token += chunk
- yield token
- token = ''
+ assert not token
+ yield nextchar + read_until(stream_i, nextchar)
continue
else:
# non whitespace appending
for nextchar in stream_i:
+ if nextchar == '[':
+ yield token + nextchar + read_until(stream_i, ']', escape=False)
+ break
+
if nextchar in wordchars:
token += nextchar
continue
break
elif nextchar == '"':
- chunk = parse_quote(stream_i, nextchar, token)
- token = chunk
- yield token
+ assert not token
+ yield nextchar + read_until(stream_i, nextchar)
break
else:
def test_bad_quoting(self):
- self._test_parse_error('"abc next', 'No closing quotation:')
+ self._test_parse_error('"abc next', "No closing '\"'")
def _test(self, to_parse, expected, wrap=True):
def test_BODY_HEADER_FIELDS(self):
header_text = 'Subject: A subject\r\nFrom: Some one <someone@mail.com>\r\n\r\n'
self.assertEquals(parse_fetch_response(
- [('123 (UID 31710 BODY[HEADER.FIELDS (FROM SUBJECT)] {57}', header_text), ')']),
+ [('123 (UID 31710 BODY[HEADER.FIELDS (from subject)] {57}', header_text), ')']),
{ 31710: {'BODY[HEADER.FIELDS (FROM SUBJECT)]': header_text,
'SEQ': 123}})