annotate Lib/tokenize.py @ 15842:f6cd36498a50 legacy-trunk

Document extensions to .pth files.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 13 Jan 2001 09:54:41 +0000
parents 0e0ea56e4be8
children 3dd217fbdd59
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
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
12 __version__ = "Ka-Ping Yee, 26 October 1997; patched, GvR 3/30/98"
5173
ff4f8fa8a273 Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents: 5172
diff changeset
13
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
14 import string, re
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
15 from token import *
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
17 COMMENT = N_TOKENS
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
18 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
19 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
20 tok_name[NL] = 'NL'
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
21
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
22
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
23 # Changes from 1.3:
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
24 # Ignore now accepts \f as whitespace. Operator now includes '**'.
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
25 # Ignore and Special now accept \n or \r\n at the end of a line.
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
26 # Imagnumber is new. Expfloat is corrected to reject '0e4'.
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
27 # Note: to quote a backslash in a regex, it must be doubled in a r'aw' string.
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
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 def group(*choices): return '(' + string.join(choices, '|') + ')'
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'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
6532
d369629e8c27 New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents: 6531
diff changeset
57 Triple = group("[rR]?'''", '[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.
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
59 String = group(r"[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
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
60 r'[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.
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 ContStr = group(r"[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" + group("'", r'\\\r?\n'),
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 r'[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' + 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
79 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
80 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
81
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
82 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
83 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
84 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
85 "'''": 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
86 "r'''": single3prog, 'r"""': double3prog,
d369629e8c27 New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents: 6531
diff changeset
87 "R'''": single3prog, 'R"""': double3prog, 'r': None, 'R': None}
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
88
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
89 tabsize = 8
14021
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
90
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
91 class TokenError(Exception):
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
92 pass
5b34b70ecf22 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 7839
diff changeset
93
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
94 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
95 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
96 (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
97
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
98 def tokenize(readline, tokeneater=printtoken):
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
99 lnum = parenlev = continued = 0
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
100 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
101 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
102 contline = None
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
103 indents = [0]
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
104
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
105 while 1: # loop over lines in stream
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
106 line = readline()
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
107 lnum = lnum + 1
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
108 pos, max = 0, len(line)
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
110 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
111 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
120 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
121 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
122 strstart, (lnum, len(line)), contline)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
123 contstr = ''
7839
d4f5321b9d9e Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents: 6532
diff changeset
124 contline = None
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
125 continue
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
126 else:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
127 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
128 contline = contline + line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
129 continue
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
130
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
131 elif parenlev == 0 and not continued: # new statement
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
132 if not line: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
133 column = 0
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
134 while pos < max: # measure leading whitespace
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
135 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
136 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
137 elif line[pos] == '\f': column = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
138 else: break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
139 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
140 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
141
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
142 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
143 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
144 (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
145 continue
558
cfb53322c9d1 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
147 if column > indents[-1]: # count indents or dedents
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
148 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
149 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
150 while column < indents[-1]:
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
151 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
152 tokeneater(DEDENT, '', (lnum, pos), (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
153
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
154 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
155 if not line:
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
156 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
157 continued = 0
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
158
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
159 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
160 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
161 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
162 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
163 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
164 token, initial = line[start:end], line[start]
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
165
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
166 if initial in numchars \
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
167 or (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
168 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
169 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
170 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
171 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
172 elif initial == '#':
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
173 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
174 elif token in ("'''", '"""', # triple-quoted
d369629e8c27 New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents: 6531
diff changeset
175 "r'''", 'r"""', "R'''", 'R"""'):
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
176 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
177 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
178 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
179 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
180 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
181 tokeneater(STRING, token, spos, (lnum, pos), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
182 else:
5266
1efc4273fdb7 Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents: 5178
diff changeset
183 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
184 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
185 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
186 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
187 elif initial in ("'", '"') or \
d369629e8c27 New, fixed version with proper r"..." and R"..." support from Ka-Ping.
Guido van Rossum <guido@python.org>
parents: 6531
diff changeset
188 token[:2] in ("r'", 'r"', "R'", 'R"'):
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
189 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
190 strstart = (lnum, start)
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
191 endprog = endprogs[initial] or endprogs[token[1]]
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
192 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
193 contline = line
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
194 break
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
195 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
196 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
197 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
198 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
199 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
200 continued = 1
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 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
203 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
204 tokeneater(OP, token, spos, epos, line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
205 else:
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
206 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
207 (lnum, pos), (lnum, pos+1), line)
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
208 pos = pos + 1
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
209
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
210 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
211 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
212 tokeneater(ENDMARKER, '', (lnum, 0), (lnum, 0), '')
5172
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
213
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
214 if __name__ == '__main__': # testing
31b0e5f0badf Ka-Ping's version.
Guido van Rossum <guido@python.org>
parents: 1407
diff changeset
215 import sys
5277
7df5a0d1f523 Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents: 5266
diff changeset
216 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
217 else: tokenize(sys.stdin.readline)
6531
e497a7fc0aa8 Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents: 5721
diff changeset
218