Show More
@@ -11,19 +11,21 b' esctable = {' | |||||
11 | 'v': '\v', |
|
11 | 'v': '\v', | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | def parsestring(s): |
|
14 | def parsestring(s, quoted=True): | |
15 | fp = cStringIO.StringIO() |
|
15 | fp = cStringIO.StringIO() | |
|
16 | if quoted: | |||
|
17 | first = s[0] | |||
|
18 | if len(s) < 2: raise SyntaxError(_('string too short')) | |||
|
19 | if first not in "'\"": raise SyntaxError(_('invalid quote')) | |||
|
20 | if s[-1] != first: raise SyntaxError(_('unmatched quotes')) | |||
|
21 | s = s[1:-1] | |||
16 | escape = False |
|
22 | escape = False | |
17 | first = s[0] |
|
23 | for c in s: | |
18 | if len(s) < 2: raise SyntaxError(_('string too short')) |
|
|||
19 | if first not in "'\"": raise SyntaxError(_('invalid quote')) |
|
|||
20 | if s[-1] != first: raise SyntaxError(_('unmatched quotes')) |
|
|||
21 | for c in s[1:-1]: |
|
|||
22 | if escape: |
|
24 | if escape: | |
23 | fp.write(esctable.get(c, c)) |
|
25 | fp.write(esctable.get(c, c)) | |
24 | escape = False |
|
26 | escape = False | |
25 | elif c == '\\': escape = True |
|
27 | elif c == '\\': escape = True | |
26 | elif c == first: raise SyntaxError(_('string ends early')) |
|
28 | elif quoted and c == first: raise SyntaxError(_('string ends early')) | |
27 | else: fp.write(c) |
|
29 | else: fp.write(c) | |
28 | if escape: raise SyntaxError(_('unterminated escape')) |
|
30 | if escape: raise SyntaxError(_('unterminated escape')) | |
29 | return fp.getvalue() |
|
31 | return fp.getvalue() |
General Comments 0
You need to be logged in to leave comments.
Login now