|
11 | 11 | Basically, the comment string includes an arbitrary amount of |
12 | 12 | key=value pairs. If a value contains whitespaces, punctuation or any |
13 | 13 | non-word character, then it should be delimited with double quotes. If |
14 | | -a value contains quote character, then it should be escaped with the |
| 14 | +a value contains a quote character, then it should be escaped with the |
15 | 15 | backslash character (the backslash character can escape |
16 | 16 | itself). Properties that doesn't have values (or basically has a |
17 | 17 | property of a unit type, so called boolean properties) are represented |
|
96 | 96 | WORDCHARS = ''.join(['-:', string.ascii_letters, string.digits]) |
97 | 97 |
|
98 | 98 |
|
99 | | -def parse(comment): |
| 99 | +def parse(comment, debug=0): |
100 | 100 | """ Parse comment string. |
101 | 101 |
|
102 | 102 | Returns a dictionary that maps properties to their values. |
103 | 103 | Raises SyntaxError if the comment is syntactically incorrect. |
104 | 104 | Returns None if comment doesn't start with the `BAP:` prefix. |
105 | 105 | """ |
106 | | - lexer = shlex(comment) |
| 106 | + lexer = shlex(comment, posix=True) |
107 | 107 | lexer.wordchars = WORDCHARS |
| 108 | + lexer.debug = debug |
| 109 | + lexer.quotes = '"' |
108 | 110 | result = {} |
109 | 111 | key = '' |
110 | 112 | values = [] |
@@ -193,14 +195,9 @@ def quote(token): |
193 | 195 | >>> quote('hello, world') |
194 | 196 | '"hello, world"' |
195 | 197 | """ |
196 | | - if set(token) - set(WORDCHARS): |
197 | | - if "'" not in token: |
198 | | - return "'{}'".format(token) |
199 | | - elif '"' not in token: |
200 | | - return '"{}"'.format(token) |
201 | | - else: # we ran out of quotes, so we need |
202 | | - return "'{}'".format(''.join('\\'+c if c == "'" else c |
203 | | - for c in token)) |
| 198 | + if not token.startswith('"') and set(token) - set(WORDCHARS): |
| 199 | + return '"{}"'.format(''.join('\\'+c if c == '"' else c |
| 200 | + for c in token)) |
204 | 201 | else: |
205 | 202 | return token |
206 | 203 |
|
|
0 commit comments