25 Returns the same values as generate_tokens() |
25 Returns the same values as generate_tokens() |
26 |
26 |
27 """ |
27 """ |
28 last_line = None |
28 last_line = None |
29 last_lineno = -1 |
29 last_lineno = -1 |
30 last_ttype = None |
30 last_ttext = None |
31 for ttype, ttext, (slineno, scol), (elineno, ecol), ltext in toks: |
31 for ttype, ttext, (slineno, scol), (elineno, ecol), ltext in toks: |
32 if last_lineno != elineno: |
32 if last_lineno != elineno: |
33 if last_line and last_line.endswith("\\\n"): |
33 if last_line and last_line.endswith("\\\n"): |
34 # We are at the beginning of a new line, and the last line |
34 # We are at the beginning of a new line, and the last line |
35 # ended with a backslash. We probably have to inject a |
35 # ended with a backslash. We probably have to inject a |
45 # '"""\\\nHEY THERE\n"""' |
45 # '"""\\\nHEY THERE\n"""' |
46 # |
46 # |
47 # so we need to figure out if the backslash is already in the |
47 # so we need to figure out if the backslash is already in the |
48 # string token or not. |
48 # string token or not. |
49 inject_backslash = True |
49 inject_backslash = True |
50 if last_ttype == tokenize.COMMENT: |
50 if last_ttext.endswith("\\"): |
51 # Comments like this \ |
|
52 # should never result in a new token. |
|
53 inject_backslash = False |
51 inject_backslash = False |
54 elif ttype == token.STRING: |
52 elif ttype == token.STRING: |
55 if "\n" in ttext and ttext.split('\n', 1)[0][-1] == '\\': |
53 if "\n" in ttext and ttext.split('\n', 1)[0][-1] == '\\': |
56 # It's a multi-line string and the first line ends with |
54 # It's a multi-line string and the first line ends with |
57 # a backslash, so we don't need to inject another. |
55 # a backslash, so we don't need to inject another. |
64 99999, "\\\n", |
62 99999, "\\\n", |
65 (slineno, ccol), (slineno, ccol+2), |
63 (slineno, ccol), (slineno, ccol+2), |
66 last_line |
64 last_line |
67 ) |
65 ) |
68 last_line = ltext |
66 last_line = ltext |
69 last_ttype = ttype |
67 if ttype not in (tokenize.NEWLINE, tokenize.NL): |
|
68 last_ttext = ttext |
70 yield ttype, ttext, (slineno, scol), (elineno, ecol), ltext |
69 yield ttype, ttext, (slineno, scol), (elineno, ecol), ltext |
71 last_lineno = elineno |
70 last_lineno = elineno |
72 |
71 |
73 |
72 |
74 @contract(source='unicode') |
73 @contract(source='unicode') |