annotate Lib/tokenize.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents c72bc8470f6c
children 14ec9cfd9760
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
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
3 generate_tokens(readline) is a generator that breaks a stream of
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
4 text into Python tokens. It accepts a readline-like method which is called
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
5 repeatedly to get the next line of input (or "" for EOF). It generates
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
6 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
7
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
8 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
9 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
10 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
11 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
12 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
13
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 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
15 that it produces COMMENT tokens for comments and gives type OP for all
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 operators
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
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 Older entry points
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
19 tokenize_loop(readline, tokeneater)
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
20 tokenize(readline, tokeneater=printtoken)
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
21 are the same, except instead of generating tokens, tokeneater is a callback
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
22 function to which the 5 fields described above are passed as 5 arguments,
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
23 each time a new token is found."""
5173
ff4f8fa8a273 Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents: 5172
diff changeset
24
16899
320fa50a1a7d Add __author__ and __credits__ variables.
Ka-Ping Yee <ping@zesty.ca>
parents: 16889
diff changeset
25 __author__ = 'Ka-Ping Yee <ping@lfw.org>'
16901
572b654bf4ed Better __credits__.
Ka-Ping Yee <ping@zesty.ca>
parents: 16899
diff changeset
26 __credits__ = \
572b654bf4ed Better __credits__.
Ka-Ping Yee <ping@zesty.ca>
parents: 16899
diff changeset
27 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'
5173
ff4f8fa8a273 Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents: 5172
diff changeset
28
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
29 import string, re
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
30 from token import *
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16514
diff changeset
32 import token
26032
225ea1f52de9 SF 633560: tokenize.__all__ needs "generate_tokens"
Raymond Hettinger <python@rcn.com>
parents: 25016
diff changeset
33 __all__ = [x for x in dir(token) if x[0] != '_'] + ["COMMENT", "tokenize",
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
34 "generate_tokens", "NL", "untokenize"]
22585
93daa4fde087 Cleanup x so it is not left in module
Neal Norwitz <nnorwitz@gmail.com>
parents: 19621
diff changeset
35 del x
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'
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16514
diff changeset
42 N_TOKENS += 2
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
43
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15898
diff changeset
44 def group(*choices): return '(' + '|'.join(choices) + ')'
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26032
diff changeset
45 def any(*choices): return group(*choices) + '*'
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26032
diff changeset
46 def maybe(*choices): return group(*choices) + '?'
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
48 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
49 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
50 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
51 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
52
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
53 Hexnumber = r'0[xX][\da-fA-F]*[lL]?'
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
54 Octnumber = r'0[0-7]*[lL]?'
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
55 Decnumber = r'[1-9]\d*[lL]?'
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
56 Intnumber = group(Hexnumber, 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
57 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
58 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
59 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
60 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
61 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
62 Number = group(Imagnumber, Floatnumber, Intnumber)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
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
64 # 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
65 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
66 # 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
67 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
68 # 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
69 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
70 # 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
71 Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
15898
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
72 Triple = group("[uU]?[rR]?'''", '[uU]?[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
73 # Single-line ' or " string.
15898
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
74 String = group(r"[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
75 r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
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
77 # 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
78 # 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
79 # recognized as two instances of =).
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 Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", r"!=",
19052
3969fcbf9500 Add new tokens // and //=, in support of PEP 238.
Guido van Rossum <guido@python.org>
parents: 18781
diff changeset
81 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
82 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
83 r"~")
14166
e9c2f830f9ba Update for augmented assignment, tested & approved by Guido.
Thomas Wouters <thomas@python.org>
parents: 14021
diff changeset
84
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 Bracket = '[][(){}]'
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 28089
diff changeset
86 Special = group(r'\r?\n', r'[:;.,`@]')
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
87 Funny = group(Operator, Bracket, Special)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
89 PlainToken = group(Number, Funny, String, Name)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
90 Token = Ignore + PlainToken
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
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
92 # First (or only) line of ' or " string.
15898
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
93 ContStr = group(r"[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
94 group("'", r'\\\r?\n'),
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
95 r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
96 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
97 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
98 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
99
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 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
101 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
102 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
103 "'''": 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
104 "r'''": single3prog, 'r"""': double3prog,
15898
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
105 "u'''": single3prog, 'u"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
106 "ur'''": single3prog, 'ur"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
107 "R'''": single3prog, 'R"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
108 "U'''": single3prog, 'U"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
109 "uR'''": single3prog, 'uR"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
110 "Ur'''": single3prog, 'Ur"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
111 "UR'''": single3prog, 'UR"""': double3prog,
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
112 'r': None, 'R': None, 'u': None, 'U': None}
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
113
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
114 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
115 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
116 "r'''", 'r"""', "R'''", 'R"""',
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 "u'''", 'u"""', "U'''", 'U"""',
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 "ur'''", 'ur"""', "Ur'''", 'Ur"""',
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 "uR'''", 'uR"""', "UR'''", 'UR"""'):
0ebec83eb3dd Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents: 23521
diff changeset
120 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
121 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
122 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
123 "r'", 'r"', "R'", 'R"',
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 "u'", 'u"', "U'", 'U"',
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 "ur'", 'ur"', "Ur'", 'Ur"',
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 "uR'", 'uR"', "UR'", 'UR"' ):
0ebec83eb3dd Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents: 23521
diff changeset
127 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
128
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
129 tabsize = 8
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
130
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
131 class TokenError(Exception): pass
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
132
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
133 class StopTokenizing(Exception): pass
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
134
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
135 def printtoken(type, token, (srow, scol), (erow, ecol), line): # for testing
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
136 print "%d,%d-%d,%d:\t%s\t%s" % \
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
137 (srow, scol, erow, ecol, tok_name[type], repr(token))
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
138
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
139 def tokenize(readline, tokeneater=printtoken):
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
140 """
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
141 The tokenize() function accepts two parameters: one representing the
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
142 input stream, and one providing an output mechanism for tokenize().
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
143
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
144 The first parameter, readline, must be a callable object which provides
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
145 the same interface as the readline() method of built-in file objects.
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
146 Each call to the function should return one line of input as a string.
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
147
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
148 The second parameter, tokeneater, must also be a callable object. It is
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
149 called once for each token, with five arguments, corresponding to the
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
150 tuples generated by generate_tokens().
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
151 """
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
152 try:
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
153 tokenize_loop(readline, tokeneater)
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
154 except StopTokenizing:
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
155 pass
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
156
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
157 # backwards compatible interface
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
158 def tokenize_loop(readline, tokeneater):
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
159 for token_info in generate_tokens(readline):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26032
diff changeset
160 tokeneater(*token_info)
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
161
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
162
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
163 def untokenize(iterable):
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
164 """Transform tokens back into Python source code.
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
165
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
166 Each element returned by the iterable must be a token sequence
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
167 with at least two elements, a token number and token value.
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
168
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
169 Round-trip invariant:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
170 # Output text will tokenize the back to the input
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
171 t1 = [tok[:2] for tok in generate_tokens(f.readline)]
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
172 newcode = untokenize(t1)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
173 readline = iter(newcode.splitlines(1)).next
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
174 t2 = [tok[:2] for tokin generate_tokens(readline)]
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
175 assert t1 == t2
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
176 """
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
177
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
178 startline = False
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
179 indents = []
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
180 toks = []
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
181 toks_append = toks.append
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
182 for tok in iterable:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
183 toknum, tokval = tok[:2]
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
184
37314
c72bc8470f6c Insert a safety space after numbers as well as names in untokenize().
Guido van Rossum <guido@python.org>
parents: 35119
diff changeset
185 if toknum in (NAME, NUMBER):
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
186 tokval += ' '
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
187
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
188 if toknum == INDENT:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
189 indents.append(tokval)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
190 continue
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
191 elif toknum == DEDENT:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
192 indents.pop()
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
193 continue
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
194 elif toknum in (NEWLINE, COMMENT, NL):
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
195 startline = True
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
196 elif startline and indents:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
197 toks_append(indents[-1])
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
198 startline = False
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
199 toks_append(tokval)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
200 return ''.join(toks)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
201
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
202
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
203 def generate_tokens(readline):
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
204 """
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
205 The generate_tokens() generator requires one argment, readline, which
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
206 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
207 readline() method of built-in file objects. Each call to the function
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
208 should return one line of input as a string. Alternately, readline
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
209 can be a callable function terminating with StopIteration:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
210 readline = open(myfile).next # Example of alternate readline
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
211
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
212 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
213 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
214 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
215 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
216 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
217 logical line; continuation lines are included.
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 22719
diff changeset
218 """
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
219 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
220 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
221 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
222 contline = None
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
223 indents = [0]
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
224
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
225 while 1: # 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
226 try:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
227 line = readline()
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
228 except StopIteration:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 32888
diff changeset
229 line = ''
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
230 lnum = lnum + 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
231 pos, max = 0, len(line)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
233 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
234 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
235 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
236 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
237 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
238 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
239 yield (STRING, contstr + line[:end],
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
240 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
241 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
242 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
243 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
244 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
245 strstart, (lnum, len(line)), contline)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
246 contstr = ''
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
247 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
248 continue
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
249 else:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
250 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
251 contline = contline + line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
252 continue
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
253
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
254 elif parenlev == 0 and not continued: # new statement
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
255 if not line: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
256 column = 0
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
257 while pos < max: # measure leading whitespace
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
258 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
259 elif line[pos] == '\t': column = (column/tabsize + 1)*tabsize
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
260 elif line[pos] == '\f': column = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
261 else: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
262 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
263 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
264
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
265 if line[pos] in '#\r\n': # skip comments or blank lines
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
266 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
267 (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
268 continue
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
270 if column > indents[-1]: # count indents or dedents
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
271 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
272 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
273 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
274 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
275 raise IndentationError(
04868959f1fe SF bug #1224621: tokenize module does not detect inconsistent dedents
Raymond Hettinger <python@rcn.com>
parents: 35058
diff changeset
276 "unindent does not match any outer indentation level")
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
277 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
278 yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
279
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
280 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
281 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
282 raise TokenError, ("EOF in multi-line statement", (lnum, 0))
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
283 continued = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
284
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
285 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
286 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
287 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
288 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
289 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
290 token, initial = line[start:end], line[start]
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
291
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
292 if initial in numchars or \
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
293 (initial == '.' and token != '.'): # ordinary number
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
294 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
295 elif initial in '\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
296 yield (parenlev > 0 and NL or NEWLINE,
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
297 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
298 elif initial == '#':
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
299 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
300 elif token in triple_quoted:
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
301 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
302 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
303 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
304 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
305 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
306 yield (STRING, token, spos, (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
307 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
308 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
309 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
310 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
311 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
312 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
313 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
314 token[:3] in single_quoted:
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
315 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
316 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
317 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
318 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
319 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
320 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
321 break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
322 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
323 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
324 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
325 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
326 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
327 continued = 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
328 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
329 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
330 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
331 yield (OP, token, spos, epos, line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
332 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
333 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
334 (lnum, pos), (lnum, pos+1), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
335 pos = pos + 1
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
336
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
337 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
338 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
339 yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
340
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
341 if __name__ == '__main__': # testing
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
342 import sys
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
343 if len(sys.argv) > 1: tokenize(open(sys.argv[1]).readline)
5721
138b176ec40f Correct typo in last line (test program invocation).
Guido van Rossum <guido@python.org>
parents: 5277
diff changeset
344 else: tokenize(sys.stdin.readline)