Mercurial > cpython-lru_cache-weakref
annotate Lib/tokenize.py @ 48416:fb02fedfe647
Merged revisions 64677 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64677 | brett.cannon | 2008-07-02 14:52:42 -0700 (Wed, 02 Jul 2008) | 2 lines
Revert r64673 and instead just change the file encoding.
........
| author | Brett Cannon <bcannon@gmail.com> |
|---|---|
| date | Wed, 02 Jul 2008 22:06:12 +0000 |
| parents | 0d7ccb50007c |
| children | 83b2d07b475b |
| rev | line source |
|---|---|
|
5173
ff4f8fa8a273
Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents:
5172
diff
changeset
|
1 """Tokenization help for Python programs. |
| 558 | 2 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
3 tokenize(readline) is a generator that breaks a stream of |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
4 bytes into Python tokens. It decodes the bytes according to |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
5 PEP-0263 for determining source file encoding. |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
6 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
7 It accepts a readline-like method which is called |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
8 repeatedly to get the next line of input (or b"" for EOF). It generates |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
9 5-tuples with these members: |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
10 |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
11 the token type (see token.py) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
12 the token (a string) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
13 the starting (row, column) indices of the token (a 2-tuple of ints) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
14 the ending (row, column) indices of the token (a 2-tuple of ints) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
15 the original line (string) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
16 |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
17 It is designed to match the working of the Python tokenizer exactly, except |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
18 that it produces COMMENT tokens for comments and gives type OP for all |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
19 operators. Aditionally, all token lists start with an ENCODING token |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
20 which tells you which encoding was used to decode the bytes stream.""" |
|
5173
ff4f8fa8a273
Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents:
5172
diff
changeset
|
21 |
|
16899
320fa50a1a7d
Add __author__ and __credits__ variables.
Ka-Ping Yee <ping@zesty.ca>
parents:
16889
diff
changeset
|
22 __author__ = 'Ka-Ping Yee <ping@lfw.org>' |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
23 __credits__ = ('GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, ' |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
24 'Skip Montanaro, Raymond Hettinger, Trent Nelson, ' |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
25 'Michael Foord') |
|
5173
ff4f8fa8a273
Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents:
5172
diff
changeset
|
26 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
27 import re, string, sys |
| 5172 | 28 from token import * |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
29 from codecs import lookup |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
30 from itertools import chain, repeat |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
31 cookie_re = re.compile("coding[:=]\s*([-\w.]+)") |
| 558 | 32 |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
33 import token |
|
26032
225ea1f52de9
SF 633560: tokenize.__all__ needs "generate_tokens"
Raymond Hettinger <python@rcn.com>
parents:
25016
diff
changeset
|
34 __all__ = [x for x in dir(token) if x[0] != '_'] + ["COMMENT", "tokenize", |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
35 "detect_encoding", "NL", "untokenize", "ENCODING"] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
36 del token |
|
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
37 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
38 COMMENT = N_TOKENS |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
39 tok_name[COMMENT] = 'COMMENT' |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
40 NL = N_TOKENS + 1 |
|
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
41 tok_name[NL] = 'NL' |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
42 ENCODING = N_TOKENS + 2 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
43 tok_name[ENCODING] = 'ENCODING' |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
44 N_TOKENS += 3 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
45 |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15898
diff
changeset
|
46 def group(*choices): return '(' + '|'.join(choices) + ')' |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26032
diff
changeset
|
47 def any(*choices): return group(*choices) + '*' |
|
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26032
diff
changeset
|
48 def maybe(*choices): return group(*choices) + '?' |
| 558 | 49 |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
50 Whitespace = r'[ \f\t]*' |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
51 Comment = r'#[^\r\n]*' |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
52 Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment) |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
53 Name = r'[a-zA-Z_]\w*' |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
54 |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43833
diff
changeset
|
55 Hexnumber = r'0[xX][\da-fA-F]+' |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43833
diff
changeset
|
56 Binnumber = r'0[bB][01]+' |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43833
diff
changeset
|
57 Octnumber = r'0[oO][0-7]+' |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
58 Decnumber = r'(?:0+|[1-9]\d*)' |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
59 Intnumber = group(Hexnumber, Binnumber, Octnumber, Decnumber) |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
60 Exponent = r'[eE][-+]?\d+' |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
61 Pointfloat = group(r'\d+\.\d*', r'\.\d+') + maybe(Exponent) |
|
19621
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19052
diff
changeset
|
62 Expfloat = r'\d+' + Exponent |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
63 Floatnumber = group(Pointfloat, Expfloat) |
|
19621
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19052
diff
changeset
|
64 Imagnumber = group(r'\d+[jJ]', Floatnumber + r'[jJ]') |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
65 Number = group(Imagnumber, Floatnumber, Intnumber) |
| 558 | 66 |
|
15080
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
67 # Tail end of ' string. |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
68 Single = r"[^'\\]*(?:\\.[^'\\]*)*'" |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
69 # Tail end of " string. |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
70 Double = r'[^"\\]*(?:\\.[^"\\]*)*"' |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
71 # Tail end of ''' string. |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
72 Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''" |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
73 # Tail end of """ string. |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
74 Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""' |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
75 Triple = group("[bB]?[rR]?'''", '[bB]?[rR]?"""') |
|
15080
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
76 # Single-line ' or " string. |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
77 String = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'", |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
78 r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"') |
| 558 | 79 |
|
15080
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
80 # Because of leftmost-then-longest match semantics, be sure to put the |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
81 # longest operators first (e.g., if = came before ==, == would get |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
82 # recognized as two instances of =). |
|
39648
efe3f2c0da5e
Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
83 Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"!=", |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
84 r"//=?", r"->", |
|
15080
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
85 r"[+\-*/%&|^=<>]=?", |
|
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
86 r"~") |
|
14166
e9c2f830f9ba
Update for augmented assignment, tested & approved by Guido.
Thomas Wouters <thomas@python.org>
parents:
14021
diff
changeset
|
87 |
| 558 | 88 Bracket = '[][(){}]' |
|
41230
b1a18c7ed5ca
Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.
Georg Brandl <georg@python.org>
parents:
40775
diff
changeset
|
89 Special = group(r'\r?\n', r'\.\.\.', r'[:;.,@]') |
| 5172 | 90 Funny = group(Operator, Bracket, Special) |
| 558 | 91 |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
92 PlainToken = group(Number, Funny, String, Name) |
| 5172 | 93 Token = Ignore + PlainToken |
| 558 | 94 |
|
15080
0e0ea56e4be8
Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents:
14166
diff
changeset
|
95 # First (or only) line of ' or " string. |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
96 ContStr = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" + |
|
15898
8366fcfb6090
Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents:
15873
diff
changeset
|
97 group("'", r'\\\r?\n'), |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
98 r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' + |
|
15898
8366fcfb6090
Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents:
15873
diff
changeset
|
99 group('"', r'\\\r?\n')) |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
100 PseudoExtras = group(r'\\\r?\n', Comment, Triple) |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
101 PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name) |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
102 |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
103 tokenprog, pseudoprog, single3prog, double3prog = map( |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
104 re.compile, (Token, PseudoToken, Single3, Double3)) |
|
6532
d369629e8c27
New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents:
6531
diff
changeset
|
105 endprogs = {"'": re.compile(Single), '"': re.compile(Double), |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
106 "'''": single3prog, '"""': double3prog, |
|
6532
d369629e8c27
New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents:
6531
diff
changeset
|
107 "r'''": single3prog, 'r"""': double3prog, |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
108 "b'''": single3prog, 'b"""': double3prog, |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
109 "br'''": single3prog, 'br"""': double3prog, |
|
15898
8366fcfb6090
Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents:
15873
diff
changeset
|
110 "R'''": single3prog, 'R"""': double3prog, |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
111 "B'''": single3prog, 'B"""': double3prog, |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
112 "bR'''": single3prog, 'bR"""': double3prog, |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
113 "Br'''": single3prog, 'Br"""': double3prog, |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
114 "BR'''": single3prog, 'BR"""': double3prog, |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
115 'r': None, 'R': None, 'b': None, 'B': None} |
| 5172 | 116 |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
117 triple_quoted = {} |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
118 for t in ("'''", '"""', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
119 "r'''", 'r"""', "R'''", 'R"""', |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
120 "b'''", 'b"""', "B'''", 'B"""', |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
121 "br'''", 'br"""', "Br'''", 'Br"""', |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
122 "bR'''", 'bR"""', "BR'''", 'BR"""'): |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
123 triple_quoted[t] = t |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
124 single_quoted = {} |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
125 for t in ("'", '"', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
126 "r'", 'r"', "R'", 'R"', |
|
43833
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
127 "b'", 'b"', "B'", 'B"', |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
128 "br'", 'br"', "Br'", 'Br"', |
|
ce77d91ea1d0
Patch 1420 by Ron Adam.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
129 "bR'", 'bR"', "BR'", 'BR"' ): |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
130 single_quoted[t] = t |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
131 |
| 5172 | 132 tabsize = 8 |
|
14021
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
7839
diff
changeset
|
133 |
|
17178
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
134 class TokenError(Exception): pass |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
135 |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
136 class StopTokenizing(Exception): pass |
|
14021
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
7839
diff
changeset
|
137 |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
138 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
139 class Untokenizer: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
140 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
141 def __init__(self): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
142 self.tokens = [] |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
143 self.prev_row = 1 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
144 self.prev_col = 0 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
145 self.encoding = None |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
146 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
147 def add_whitespace(self, start): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
148 row, col = start |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
149 assert row <= self.prev_row |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
150 col_offset = col - self.prev_col |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
151 if col_offset: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
152 self.tokens.append(" " * col_offset) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
153 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
154 def untokenize(self, iterable): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
155 for t in iterable: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
156 if len(t) == 2: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
157 self.compat(t, iterable) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
158 break |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
159 tok_type, token, start, end, line = t |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
160 if tok_type == ENCODING: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
161 self.encoding = token |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
162 continue |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
163 self.add_whitespace(start) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
164 self.tokens.append(token) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
165 self.prev_row, self.prev_col = end |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
166 if tok_type in (NEWLINE, NL): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
167 self.prev_row += 1 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
168 self.prev_col = 0 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
169 return "".join(self.tokens) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
170 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
171 def compat(self, token, iterable): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
172 startline = False |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
173 indents = [] |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
174 toks_append = self.tokens.append |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
175 toknum, tokval = token |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
176 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
177 if toknum in (NAME, NUMBER): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
178 tokval += ' ' |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
179 if toknum in (NEWLINE, NL): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
180 startline = True |
|
46145
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
181 prevstring = False |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
182 for tok in iterable: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
183 toknum, tokval = tok[:2] |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
184 if toknum == ENCODING: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
185 self.encoding = tokval |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
186 continue |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
187 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
188 if toknum in (NAME, NUMBER): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
189 tokval += ' ' |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
190 |
|
46145
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
191 # Insert a space between two consecutive strings |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
192 if toknum == STRING: |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
193 if prevstring: |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
194 tokval = ' ' + tokval |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
195 prevstring = True |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
196 else: |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
197 prevstring = False |
|
da5597a5c195
Merged revisions 61964-61979 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45888
diff
changeset
|
198 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
199 if toknum == INDENT: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
200 indents.append(tokval) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
201 continue |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
202 elif toknum == DEDENT: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
203 indents.pop() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
204 continue |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
205 elif toknum in (NEWLINE, NL): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
206 startline = True |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
207 elif startline and indents: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
208 toks_append(indents[-1]) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
209 startline = False |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
210 toks_append(tokval) |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
211 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
212 |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
213 def untokenize(iterable): |
|
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
214 """Transform tokens back into Python source code. |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
215 It returns a bytes object, encoded using the ENCODING |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
216 token, which is the first token sequence output by tokenize. |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
217 |
|
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
218 Each element returned by the iterable must be a token sequence |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
219 with at least two elements, a token number and token value. If |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
220 only two tokens are passed, the resulting output is poor. |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
221 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
222 Round-trip invariant for full input: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
223 Untokenized source will match input source exactly |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
224 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
225 Round-trip invariant for limited intput: |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
226 # Output bytes will tokenize the back to the input |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
227 t1 = [tok[:2] for tok in tokenize(f.readline)] |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
228 newcode = untokenize(t1) |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
229 readline = BytesIO(newcode).readline |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
230 t2 = [tok[:2] for tok in tokenize(readline)] |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
231 assert t1 == t2 |
|
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
232 """ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
233 ut = Untokenizer() |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
234 out = ut.untokenize(iterable) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
235 if ut.encoding is not None: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
236 out = out.encode(ut.encoding) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
237 return out |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
238 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
239 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
240 def detect_encoding(readline): |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
241 """ |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
242 The detect_encoding() function is used to detect the encoding that should |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
243 be used to decode a Python source file. It requires one argment, readline, |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
244 in the same way as the tokenize() generator. |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
245 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
246 It will call readline a maximum of twice, and return the encoding used |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
247 (as a string) and a list of any lines (left as bytes) it has read |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
248 in. |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
249 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
250 It detects the encoding from the presence of a utf-8 bom or an encoding |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
251 cookie as specified in pep-0263. If both a bom and a cookie are present, |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
252 but disagree, a SyntaxError will be raised. |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
253 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
254 If no encoding is specified, then the default of 'utf-8' will be returned. |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
255 """ |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
256 utf8_bom = b'\xef\xbb\xbf' |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
257 bom_found = False |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
258 encoding = None |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
259 def read_or_stop(): |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
260 try: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
261 return readline() |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
262 except StopIteration: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
263 return b'' |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
264 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
265 def find_cookie(line): |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
266 try: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
267 line_string = line.decode('ascii') |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
268 except UnicodeDecodeError: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
269 pass |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
270 else: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
271 matches = cookie_re.findall(line_string) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
272 if matches: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
273 encoding = matches[0] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
274 if bom_found and lookup(encoding).name != 'utf-8': |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
275 # This behaviour mimics the Python interpreter |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
276 raise SyntaxError('encoding problem: utf-8') |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
277 return encoding |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
278 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
279 first = read_or_stop() |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
280 if first.startswith(utf8_bom): |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
281 bom_found = True |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
282 first = first[3:] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
283 if not first: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
284 return 'utf-8', [] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
285 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
286 encoding = find_cookie(first) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
287 if encoding: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
288 return encoding, [first] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
289 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
290 second = read_or_stop() |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
291 if not second: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
292 return 'utf-8', [first] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
293 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
294 encoding = find_cookie(second) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
295 if encoding: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
296 return encoding, [first, second] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
297 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
298 return 'utf-8', [first, second] |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
299 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
300 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
301 def tokenize(readline): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
302 """ |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
303 The tokenize() generator requires one argment, readline, which |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
304 must be a callable object which provides the same interface as the |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
305 readline() method of built-in file objects. Each call to the function |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
306 should return one line of input as bytes. Alternately, readline |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
307 can be a callable function terminating with StopIteration: |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
308 readline = open(myfile, 'rb').__next__ # Example of alternate readline |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
309 |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
310 The generator produces 5-tuples with these members: the token type; the |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
311 token string; a 2-tuple (srow, scol) of ints specifying the row and |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
312 column where the token begins in the source; a 2-tuple (erow, ecol) of |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
313 ints specifying the row and column where the token ends in the source; |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
314 and the line on which the token was found. The line passed is the |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
315 logical line; continuation lines are included. |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
316 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
317 The first token sequence will always be an ENCODING token |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
318 which tells you which encoding was used to decode the bytes stream. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
319 """ |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
320 encoding, consumed = detect_encoding(readline) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
321 def readline_generator(): |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
322 while True: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
323 try: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
324 yield readline() |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
325 except StopIteration: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
326 return |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
327 chained = chain(consumed, readline_generator()) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
328 return _tokenize(chained.__next__, encoding) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
329 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
330 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
331 def _tokenize(readline, encoding): |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
332 lnum = parenlev = continued = 0 |
|
18781
01ae962df885
Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents:
18609
diff
changeset
|
333 namechars, numchars = string.ascii_letters + '_', '0123456789' |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
334 contstr, needcont = '', 0 |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
335 contline = None |
| 5172 | 336 indents = [0] |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
337 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
338 if encoding is not None: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
339 yield (ENCODING, encoding, (0, 0), (0, 0), '') |
|
47481
0d7ccb50007c
use the more idomatic (and Py3k faster) while True
Benjamin Peterson <benjamin@python.org>
parents:
46145
diff
changeset
|
340 while True: # loop over lines in stream |
|
35058
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
341 try: |
|
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
342 line = readline() |
|
8076424d455d
Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents:
32888
diff
changeset
|
343 except StopIteration: |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
344 line = b'' |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
345 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
346 if encoding is not None: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
347 line = line.decode(encoding) |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
348 lnum = lnum + 1 |
| 5172 | 349 pos, max = 0, len(line) |
| 558 | 350 |
| 5172 | 351 if contstr: # continued string |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
352 if not line: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
353 raise TokenError("EOF in multi-line string", strstart) |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
354 endmatch = endprog.match(line) |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
355 if endmatch: |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
356 pos = end = endmatch.end(0) |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
357 yield (STRING, contstr + line[:end], |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
358 strstart, (lnum, end), contline + line) |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
359 contstr, needcont = '', 0 |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
360 contline = None |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
361 elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n': |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
362 yield (ERRORTOKEN, contstr + line, |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
363 strstart, (lnum, len(line)), contline) |
| 5172 | 364 contstr = '' |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
365 contline = None |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
366 continue |
| 5172 | 367 else: |
| 368 contstr = contstr + line | |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
369 contline = contline + line |
| 5172 | 370 continue |
| 371 | |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
372 elif parenlev == 0 and not continued: # new statement |
| 5172 | 373 if not line: break |
| 374 column = 0 | |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
375 while pos < max: # measure leading whitespace |
| 5172 | 376 if line[pos] == ' ': column = column + 1 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
377 elif line[pos] == '\t': column = (column/tabsize + 1)*tabsize |
| 5172 | 378 elif line[pos] == '\f': column = 0 |
| 379 else: break | |
| 380 pos = pos + 1 | |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
381 if pos == max: break |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
382 |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
383 if line[pos] in '#\r\n': # skip comments or blank lines |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
384 if line[pos] == '#': |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
385 comment_token = line[pos:].rstrip('\r\n') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
386 nl_pos = pos + len(comment_token) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
387 yield (COMMENT, comment_token, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
388 (lnum, pos), (lnum, pos + len(comment_token)), line) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
389 yield (NL, line[nl_pos:], |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
390 (lnum, nl_pos), (lnum, len(line)), line) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
391 else: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
392 yield ((NL, COMMENT)[line[pos] == '#'], line[pos:], |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
393 (lnum, pos), (lnum, len(line)), line) |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
394 continue |
| 558 | 395 |
| 5172 | 396 if column > indents[-1]: # count indents or dedents |
| 397 indents.append(column) | |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
398 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line) |
| 5172 | 399 while column < indents[-1]: |
|
35119
04868959f1fe
SF bug #1224621: tokenize module does not detect inconsistent dedents
Raymond Hettinger <python@rcn.com>
parents:
35058
diff
changeset
|
400 if column not in indents: |
|
04868959f1fe
SF bug #1224621: tokenize module does not detect inconsistent dedents
Raymond Hettinger <python@rcn.com>
parents:
35058
diff
changeset
|
401 raise IndentationError( |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
402 "unindent does not match any outer indentation level", |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
403 ("<tokenize>", lnum, pos, line)) |
| 5172 | 404 indents = indents[:-1] |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
405 yield (DEDENT, '', (lnum, pos), (lnum, pos), line) |
| 5172 | 406 |
| 407 else: # continued statement | |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
408 if not line: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
409 raise TokenError("EOF in multi-line statement", (lnum, 0)) |
| 5172 | 410 continued = 0 |
| 411 | |
| 412 while pos < max: | |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
413 pseudomatch = pseudoprog.match(line, pos) |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
414 if pseudomatch: # scan for tokens |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
415 start, end = pseudomatch.span(1) |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
416 spos, epos, pos = (lnum, start), (lnum, end), end |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
417 token, initial = line[start:end], line[start] |
| 5172 | 418 |
|
41230
b1a18c7ed5ca
Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.
Georg Brandl <georg@python.org>
parents:
40775
diff
changeset
|
419 if (initial in numchars or # ordinary number |
|
b1a18c7ed5ca
Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.
Georg Brandl <georg@python.org>
parents:
40775
diff
changeset
|
420 (initial == '.' and token != '.' and token != '...')): |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
421 yield (NUMBER, token, spos, epos, line) |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
422 elif initial in '\r\n': |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
423 yield (NL if parenlev > 0 else NEWLINE, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
424 token, spos, epos, line) |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
425 elif initial == '#': |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39676
diff
changeset
|
426 assert not token.endswith("\n") |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
427 yield (COMMENT, token, spos, epos, line) |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
428 elif token in triple_quoted: |
| 5172 | 429 endprog = endprogs[token] |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
430 endmatch = endprog.match(line, pos) |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
431 if endmatch: # all on one line |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
432 pos = endmatch.end(0) |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
433 token = line[start:pos] |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
434 yield (STRING, token, spos, (lnum, pos), line) |
| 5172 | 435 else: |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
436 strstart = (lnum, start) # multiple lines |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
437 contstr = line[start:] |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
438 contline = line |
| 5172 | 439 break |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
440 elif initial in single_quoted or \ |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
441 token[:2] in single_quoted or \ |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
442 token[:3] in single_quoted: |
| 5172 | 443 if token[-1] == '\n': # continued string |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
444 strstart = (lnum, start) |
|
15898
8366fcfb6090
Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents:
15873
diff
changeset
|
445 endprog = (endprogs[initial] or endprogs[token[1]] or |
|
8366fcfb6090
Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents:
15873
diff
changeset
|
446 endprogs[token[2]]) |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
447 contstr, needcont = line[start:], 1 |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
448 contline = line |
| 5172 | 449 break |
| 450 else: # ordinary string | |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
451 yield (STRING, token, spos, epos, line) |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
452 elif initial in namechars: # ordinary name |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
453 yield (NAME, token, spos, epos, line) |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
454 elif initial == '\\': # continued stmt |
|
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
455 continued = 1 |
| 5172 | 456 else: |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
457 if initial in '([{': parenlev = parenlev + 1 |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
458 elif initial in ')]}': parenlev = parenlev - 1 |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
459 yield (OP, token, spos, epos, line) |
| 5172 | 460 else: |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
461 yield (ERRORTOKEN, line[pos], |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
462 (lnum, pos), (lnum, pos+1), line) |
| 5172 | 463 pos = pos + 1 |
| 464 | |
| 465 for indent in indents[1:]: # pop remaining indent levels | |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
466 yield (DEDENT, '', (lnum, 0), (lnum, 0), '') |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
467 yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '') |
| 5172 | 468 |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
469 |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
470 # An undocumented, backwards compatible, API for all the places in the standard |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
471 # library that expect to be able to use tokenize with strings |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
472 def generate_tokens(readline): |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
44782
diff
changeset
|
473 return _tokenize(readline, None) |
