annotate Lib/tokenize.py @ 17322:bd110eadc19a legacy-trunk

One-liner critical fix from Jim Fulton: Eric's string-method crusade got the order backwards in a line (for .find()).
author Tim Peters <tim.peters@gmail.com>
date Thu, 05 Apr 2001 22:26:23 +0000
parents 707f95d05e69
children 7bc473b2c654
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
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
3 This module exports a function called 'tokenize()' that breaks a stream of
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
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
5 repeatedly to get the next line of input (or "" for EOF) and a "token-eater"
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
6 function which is called once for each token found. The latter function is
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
7 passed the token type, a string containing the token, the starting and
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
8 ending (row, column) coordinates of the token, and the original line. It is
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
9 designed to match the working of the Python tokenizer exactly, except that
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
10 it produces COMMENT tokens for comments and gives type OP for all operators."""
5173
ff4f8fa8a273 Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents: 5172
diff changeset
11
16899
320fa50a1a7d Add __author__ and __credits__ variables.
Ka-Ping Yee <ping@zesty.ca>
parents: 16889
diff changeset
12 __author__ = 'Ka-Ping Yee <ping@lfw.org>'
16901
572b654bf4ed Better __credits__.
Ka-Ping Yee <ping@zesty.ca>
parents: 16899
diff changeset
13 __credits__ = \
572b654bf4ed Better __credits__.
Ka-Ping Yee <ping@zesty.ca>
parents: 16899
diff changeset
14 '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
15
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
16 import string, re
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
17 from token import *
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16514
diff changeset
19 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
20 __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
21 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
22
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
23 COMMENT = N_TOKENS
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
24 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
25 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
26 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
27 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
28
16514
90c600a819e9 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15898
diff changeset
29 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
30 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
31 def maybe(*choices): return apply(group, choices) + '?'
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
33 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
34 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
35 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
36 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
37
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
38 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
39 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
40 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
41 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
42 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
43 Pointfloat = group(r'\d+\.\d*', r'\.\d+') + maybe(Exponent)
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
44 Expfloat = r'[1-9]\d*' + Exponent
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
45 Floatnumber = group(Pointfloat, Expfloat)
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
46 Imagnumber = group(r'0[jJ]', r'[1-9]\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
47 Number = group(Imagnumber, Floatnumber, Intnumber)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
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
49 # 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
50 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
51 # 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
52 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
53 # 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
54 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
55 # 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
56 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
57 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
58 # 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
59 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
60 r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
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
62 # 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
63 # 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
64 # 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
65 Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", 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 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
67 r"~")
14166
e9c2f830f9ba Update for augmented assignment, tested & approved by Guido.
Thomas Wouters <thomas@python.org>
parents: 14021
diff changeset
68
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 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
70 Special = group(r'\r?\n', r'[:;.,`]')
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
71 Funny = group(Operator, Bracket, Special)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
73 PlainToken = group(Number, Funny, String, Name)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
74 Token = Ignore + PlainToken
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
15080
0e0ea56e4be8 Possible fix for Skip's bug 116136 (sre recursion limit hit in tokenize.py).
Tim Peters <tim.peters@gmail.com>
parents: 14166
diff changeset
76 # 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
77 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
78 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
79 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
80 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
81 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
82 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
83
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
84 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
85 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
86 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
87 "'''": 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
88 "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
89 "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
90 "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
91 "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
92 "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
93 "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
94 "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
95 "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
96 'r': None, 'R': None, 'u': None, 'U': None}
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
97
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
98 tabsize = 8
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
99
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
100 class TokenError(Exception): pass
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
101
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
102 class StopTokenizing(Exception): pass
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
103
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
104 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
105 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
106 (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
107
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
108 def tokenize(readline, tokeneater=printtoken):
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
109 try:
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
110 tokenize_loop(readline, tokeneater)
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
111 except StopTokenizing:
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
112 pass
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
113
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
114 def tokenize_loop(readline, tokeneater):
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
115 lnum = parenlev = continued = 0
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
116 namechars, numchars = string.letters + '_', string.digits
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
117 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
118 contline = None
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
119 indents = [0]
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
120
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
121 while 1: # loop over lines in stream
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
122 line = readline()
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
123 lnum = lnum + 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
124 pos, max = 0, len(line)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
126 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
127 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
128 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
129 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
130 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
131 pos = end = 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
132 tokeneater(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
133 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
134 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
135 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
136 elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
137 tokeneater(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
138 strstart, (lnum, len(line)), contline)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
139 contstr = ''
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
140 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
141 continue
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
142 else:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
143 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
144 contline = contline + line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
145 continue
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
146
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
147 elif parenlev == 0 and not continued: # new statement
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
148 if not line: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
149 column = 0
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
150 while pos < max: # measure leading whitespace
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
151 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
152 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
153 elif line[pos] == '\f': column = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
154 else: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
155 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
156 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
157
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
158 if line[pos] in '#\r\n': # skip comments or blank lines
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
159 tokeneater((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
160 (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
161 continue
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
163 if column > indents[-1]: # count indents or dedents
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
164 indents.append(column)
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
165 tokeneater(INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
166 while column < indents[-1]:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
167 indents = indents[:-1]
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
168 tokeneater(DEDENT, '', (lnum, pos), (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
169
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
170 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
171 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
172 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
173 continued = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
174
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
175 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
176 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
177 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
178 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
179 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
180 token, initial = line[start:end], line[start]
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
181
17178
707f95d05e69 Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents: 16901
diff changeset
182 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
183 (initial == '.' and token != '.'): # ordinary number
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
184 tokeneater(NUMBER, token, spos, epos, line)
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
185 elif initial in '\r\n':
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
186 tokeneater(parenlev > 0 and NL or NEWLINE,
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
187 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
188 elif initial == '#':
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
189 tokeneater(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
190 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
191 "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
192 "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
193 "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
194 "uR'''", 'uR"""', "UR'''", 'UR"""'):
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
195 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
196 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
197 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
198 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
199 token = line[start:pos]
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
200 tokeneater(STRING, token, spos, (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
201 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
202 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
203 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
204 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
205 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
206 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
207 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
208 "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
209 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
210 "uR'", 'uR"', "UR'", 'UR"' ):
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
211 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
212 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
213 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
214 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
215 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
216 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
217 break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
218 else: # ordinary string
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
219 tokeneater(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
220 elif initial in namechars: # ordinary name
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
221 tokeneater(NAME, token, spos, epos, line)
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
222 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
223 continued = 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
224 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
225 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
226 elif 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
227 tokeneater(OP, token, spos, epos, line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
228 else:
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
229 tokeneater(ERRORTOKEN, line[pos],
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
230 (lnum, pos), (lnum, pos+1), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
231 pos = pos + 1
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
232
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
233 for indent in indents[1:]: # pop remaining indent levels
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
234 tokeneater(DEDENT, '', (lnum, 0), (lnum, 0), '')
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
235 tokeneater(ENDMARKER, '', (lnum, 0), (lnum, 0), '')
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
236
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
237 if __name__ == '__main__': # testing
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
238 import sys
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
239 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
240 else: tokenize(sys.stdin.readline)