annotate Lib/tokenize.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents a0005d26773d
children 1de7ac729bfa
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
18609
59446d0f8341 Preliminary support for "from __future__ import generators" to enable
Guido van Rossum <guido@python.org>
parents: 18351
diff changeset
25 from __future__ import generators
59446d0f8341 Preliminary support for "from __future__ import generators" to enable
Guido van Rossum <guido@python.org>
parents: 18351
diff changeset
26
16899
320fa50a1a7d Add __author__ and __credits__ variables.
Ka-Ping Yee <ping@zesty.ca>
parents: 16889
diff changeset
27 __author__ = 'Ka-Ping Yee <ping@lfw.org>'
16901
572b654bf4ed Better __credits__.
Ka-Ping Yee <ping@zesty.ca>
parents: 16899
diff changeset
28 __credits__ = \
572b654bf4ed Better __credits__.
Ka-Ping Yee <ping@zesty.ca>
parents: 16899
diff changeset
29 '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
30
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
31 import string, re
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
32 from token import *
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16514
diff changeset
34 import token
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16514
diff changeset
35 __all__ = [x for x in dir(token) if x[0] != '_'] + ["COMMENT", "tokenize", "NL"]
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) + ')'
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
45 def any(*choices): return apply(group, choices) + '*'
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
46 def maybe(*choices): return apply(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 = '[][(){}]'
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
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
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
114 tabsize = 8
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
115
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
116 class TokenError(Exception): pass
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
117
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
118 class StopTokenizing(Exception): pass
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
119
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
120 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
121 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
122 (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
123
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
124 def tokenize(readline, tokeneater=printtoken):
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
125 """
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
126 The tokenize() function accepts two parameters: one representing the
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
127 input stream, and one providing an output mechanism for tokenize().
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23456
diff changeset
128
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
129 The first parameter, readline, must be a callable object which provides
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
130 the same interface as the readline() method of built-in file objects.
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23456
diff changeset
131 Each call to the function should return one line of input as a string.
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
132
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
133 The second parameter, tokeneater, must also be a callable object. It is
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
134 called once for each token, with five arguments, corresponding to the
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23456
diff changeset
135 tuples generated by generate_tokens().
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
136 """
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
137 try:
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
138 tokenize_loop(readline, tokeneater)
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
139 except StopTokenizing:
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
140 pass
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
141
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
142 # backwards compatible interface
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
143 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
144 for token_info in generate_tokens(readline):
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 17178
diff changeset
145 apply(tokeneater, token_info)
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 17178
diff changeset
146
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 17178
diff changeset
147 def generate_tokens(readline):
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
148 """
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
149 The generate_tokens() generator requires one argment, readline, which
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
150 must be a callable object which provides the same interface as the
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
151 readline() method of built-in file objects. Each call to the function
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
152 should return one line of input as a string.
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23456
diff changeset
153
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
154 The generator produces 5-tuples with these members: the token type; the
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
155 token string; a 2-tuple (srow, scol) of ints specifying the row and
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
156 column where the token begins in the source; a 2-tuple (erow, ecol) of
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
157 ints specifying the row and column where the token ends in the source;
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
158 and the line on which the token was found. The line passed is the
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23456
diff changeset
159 logical line; continuation lines are included.
23456
64eb119b816d Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents: 19621
diff changeset
160 """
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
161 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
162 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
163 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
164 contline = None
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
165 indents = [0]
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
166
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
167 while 1: # loop over lines in stream
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
168 line = readline()
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
169 lnum = lnum + 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
170 pos, max = 0, len(line)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
172 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
173 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
182 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
183 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
184 strstart, (lnum, len(line)), contline)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
185 contstr = ''
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
186 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
187 continue
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
188 else:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
189 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
190 contline = contline + line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
191 continue
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
192
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
193 elif parenlev == 0 and not continued: # new statement
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
194 if not line: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
195 column = 0
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
196 while pos < max: # measure leading whitespace
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
197 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
198 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
199 elif line[pos] == '\f': column = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
200 else: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
201 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
202 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
203
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
204 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
205 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
206 (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
207 continue
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
209 if column > indents[-1]: # count indents or dedents
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
210 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
211 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
212 while column < indents[-1]:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
213 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
214 yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
215
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
216 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
217 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
218 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
219 continued = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
220
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
221 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
222 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
223 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
224 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
225 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
226 token, initial = line[start:end], line[start]
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
227
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
228 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
229 (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
230 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
231 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
232 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
233 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
234 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
235 yield (COMMENT, token, spos, epos, line)
6532
d369629e8c27 New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents: 6531
diff changeset
236 elif token in ("'''", '"""', # triple-quoted
15898
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
237 "r'''", 'r"""', "R'''", 'R"""',
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
238 "u'''", 'u"""', "U'''", 'U"""',
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
239 "ur'''", 'ur"""', "Ur'''", 'Ur"""',
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
240 "uR'''", 'uR"""', "UR'''", 'UR"""'):
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
241 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
242 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
243 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
244 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
245 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
246 yield (STRING, token, spos, (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
247 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
248 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
249 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
250 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
251 break
6532
d369629e8c27 New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents: 6531
diff changeset
252 elif initial in ("'", '"') or \
15898
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
253 token[:2] in ("r'", 'r"', "R'", 'R"',
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
254 "u'", 'u"', "U'", 'U"') or \
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
255 token[:3] in ("ur'", 'ur"', "Ur'", 'Ur"',
8366fcfb6090 Add tokenizer support and tests for u'', U"", uR'', Ur"", etc.
Ka-Ping Yee <ping@zesty.ca>
parents: 15873
diff changeset
256 "uR'", 'uR"', "UR'", 'UR"' ):
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
257 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
258 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
259 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
260 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
261 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
262 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
263 break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
264 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
265 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
266 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
267 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
268 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
269 continued = 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
270 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
271 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
272 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
273 yield (OP, token, spos, epos, line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
274 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
275 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
276 (lnum, pos), (lnum, pos+1), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
277 pos = pos + 1
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
278
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
279 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
280 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
281 yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
282
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
283 if __name__ == '__main__': # testing
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
284 import sys
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
285 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
286 else: tokenize(sys.stdin.readline)