Show More
@@ -278,6 +278,25 b' _help_end_re = re.compile(r"""(%{0,2}' | |||||
278 | """, |
|
278 | """, | |
279 | re.VERBOSE) |
|
279 | re.VERBOSE) | |
280 |
|
280 | |||
|
281 | # Extra pseudotokens for multiline strings and data structures | |||
|
282 | _MULTILINE_STRING = object() | |||
|
283 | _MULTILINE_STRUCTURE = object() | |||
|
284 | ||||
|
285 | def _line_tokens(line): | |||
|
286 | """Helper for has_comment and ends_in_comment_or_string.""" | |||
|
287 | readline = StringIO(line).readline | |||
|
288 | toktypes = set() | |||
|
289 | try: | |||
|
290 | for t in generate_tokens(readline): | |||
|
291 | toktypes.add(t[0]) | |||
|
292 | except TokenError as e: | |||
|
293 | # There are only two cases where a TokenError is raised. | |||
|
294 | if 'multi-line string' in e.args[0]: | |||
|
295 | toktypes.add(_MULTILINE_STRING) | |||
|
296 | else: | |||
|
297 | toktypes.add(_MULTILINE_STRUCTURE) | |||
|
298 | return toktypes | |||
|
299 | ||||
281 | def has_comment(src): |
|
300 | def has_comment(src): | |
282 | """Indicate whether an input line has (i.e. ends in, or is) a comment. |
|
301 | """Indicate whether an input line has (i.e. ends in, or is) a comment. | |
283 |
|
302 | |||
@@ -293,21 +312,31 b' def has_comment(src):' | |||||
293 | comment : bool |
|
312 | comment : bool | |
294 | True if source has a comment. |
|
313 | True if source has a comment. | |
295 | """ |
|
314 | """ | |
296 | readline = StringIO(src).readline |
|
315 | return (tokenize2.COMMENT in _line_tokens(src)) | |
297 | toktypes = set() |
|
|||
298 | try: |
|
|||
299 | for t in generate_tokens(readline): |
|
|||
300 | toktypes.add(t[0]) |
|
|||
301 | except TokenError: |
|
|||
302 | pass |
|
|||
303 | return(tokenize2.COMMENT in toktypes) |
|
|||
304 |
|
316 | |||
|
317 | def ends_in_comment_or_string(src): | |||
|
318 | """Indicates whether or not an input line ends in a comment or within | |||
|
319 | a multiline string. | |||
|
320 | ||||
|
321 | Parameters | |||
|
322 | ---------- | |||
|
323 | src : string | |||
|
324 | A single line input string. | |||
|
325 | ||||
|
326 | Returns | |||
|
327 | ------- | |||
|
328 | comment : bool | |||
|
329 | True if source ends in a comment or multiline string. | |||
|
330 | """ | |||
|
331 | toktypes = _line_tokens(src) | |||
|
332 | return (tokenize2.COMMENT in toktypes) or (_MULTILINE_STRING in toktypes) | |||
|
333 | ||||
305 |
|
334 | |||
306 | @StatelessInputTransformer.wrap |
|
335 | @StatelessInputTransformer.wrap | |
307 | def help_end(line): |
|
336 | def help_end(line): | |
308 | """Translate lines with ?/?? at the end""" |
|
337 | """Translate lines with ?/?? at the end""" | |
309 | m = _help_end_re.search(line) |
|
338 | m = _help_end_re.search(line) | |
310 |
if m is None or |
|
339 | if m is None or ends_in_comment_or_string(line): | |
311 | return line |
|
340 | return line | |
312 | target = m.group(1) |
|
341 | target = m.group(1) | |
313 | esc = m.group(3) |
|
342 | esc = m.group(3) |
@@ -250,6 +250,12 b' syntax_ml = \\' | |||||
250 | ], |
|
250 | ], | |
251 | ], |
|
251 | ], | |
252 |
|
252 | |||
|
253 | multiline_string = | |||
|
254 | [ [("'''foo?", None), | |||
|
255 | ("bar'''", "'''foo?\nbar'''"), | |||
|
256 | ], | |||
|
257 | ], | |||
|
258 | ||||
253 | leading_indent = |
|
259 | leading_indent = | |
254 | [ [(' print "hi"','print "hi"'), |
|
260 | [ [(' print "hi"','print "hi"'), | |
255 | ], |
|
261 | ], |
General Comments 0
You need to be logged in to leave comments.
Login now