Mercurial > cpython-lru_cache-weakref
annotate Lib/tokenize.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 225ea1f52de9 |
| children | 14f65d661084 |
| rev | line source |
|---|---|
|
5173
ff4f8fa8a273
Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents:
5172
diff
changeset
|
1 """Tokenization help for Python programs. |
| 558 | 2 |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
3 generate_tokens(readline) is a generator that breaks a stream of |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
4 text into Python tokens. It accepts a readline-like method which is called |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
5 repeatedly to get the next line of input (or "" for EOF). It generates |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
6 5-tuples with these members: |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
7 |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
8 the token type (see token.py) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
9 the token (a string) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
10 the starting (row, column) indices of the token (a 2-tuple of ints) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
11 the ending (row, column) indices of the token (a 2-tuple of ints) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
12 the original line (string) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
13 |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
14 It is designed to match the working of the Python tokenizer exactly, except |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
15 that it produces COMMENT tokens for comments and gives type OP for all |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
16 operators |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
17 |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
18 Older entry points |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
19 tokenize_loop(readline, tokeneater) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
20 tokenize(readline, tokeneater=printtoken) |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
21 are the same, except instead of generating tokens, tokeneater is a callback |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
22 function to which the 5 fields described above are passed as 5 arguments, |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
23 each time a new token is found.""" |
|
5173
ff4f8fa8a273
Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents:
5172
diff
changeset
|
24 |
|
16899
320fa50a1a7d
Add __author__ and __credits__ variables.
Ka-Ping Yee <ping@zesty.ca>
parents:
16889
diff
changeset
|
25 __author__ = 'Ka-Ping Yee <ping@lfw.org>' |
| 16901 | 26 __credits__ = \ |
| 27 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro' | |
|
5173
ff4f8fa8a273
Fixed doc string, added __version__, fixed 1 bug.
Guido van Rossum <guido@python.org>
parents:
5172
diff
changeset
|
28 |
|
6531
e497a7fc0aa8
Redone (by Ka-Ping) using the new re module, and adding recognition
Guido van Rossum <guido@python.org>
parents:
5721
diff
changeset
|
29 import string, re |
| 5172 | 30 from token import * |
| 558 | 31 |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
32 import token |
|
26032
225ea1f52de9
SF 633560: tokenize.__all__ needs "generate_tokens"
Raymond Hettinger <python@rcn.com>
parents:
25016
diff
changeset
|
33 __all__ = [x for x in dir(token) if x[0] != '_'] + ["COMMENT", "tokenize", |
|
225ea1f52de9
SF 633560: tokenize.__all__ needs "generate_tokens"
Raymond Hettinger <python@rcn.com>
parents:
25016
diff
changeset
|
34 "generate_tokens", "NL"] |
|
22585
93daa4fde087
Cleanup x so it is not left in module
Neal Norwitz <nnorwitz@gmail.com>
parents:
19621
diff
changeset
|
35 del x |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
36 del token |
|
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
37 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
38 COMMENT = N_TOKENS |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
39 tok_name[COMMENT] = 'COMMENT' |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
40 NL = N_TOKENS + 1 |
|
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
41 tok_name[NL] = 'NL' |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16514
diff
changeset
|
42 N_TOKENS += 2 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
43 |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15898
diff
changeset
|
44 def group(*choices): return '(' + '|'.join(choices) + ')' |
|
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 | 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 | 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 | 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 | 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 | 87 Funny = group(Operator, Bracket, Special) |
| 558 | 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 | 90 Token = Ignore + PlainToken |
| 558 | 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 | 113 |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
114 triple_quoted = {} |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
115 for t in ("'''", '"""', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
116 "r'''", 'r"""', "R'''", 'R"""', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
117 "u'''", 'u"""', "U'''", 'U"""', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
118 "ur'''", 'ur"""', "Ur'''", 'Ur"""', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
119 "uR'''", 'uR"""', "UR'''", 'UR"""'): |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
120 triple_quoted[t] = t |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
121 single_quoted = {} |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
122 for t in ("'", '"', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
123 "r'", 'r"', "R'", 'R"', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
124 "u'", 'u"', "U'", 'U"', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
125 "ur'", 'ur"', "Ur'", 'Ur"', |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
126 "uR'", 'uR"', "UR'", 'UR"' ): |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
127 single_quoted[t] = t |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
128 |
| 5172 | 129 tabsize = 8 |
|
14021
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
7839
diff
changeset
|
130 |
|
17178
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
131 class TokenError(Exception): pass |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
132 |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
133 class StopTokenizing(Exception): pass |
|
14021
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
7839
diff
changeset
|
134 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
135 def printtoken(type, token, (srow, scol), (erow, ecol), line): # for testing |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
136 print "%d,%d-%d,%d:\t%s\t%s" % \ |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
137 (srow, scol, erow, ecol, tok_name[type], repr(token)) |
| 5172 | 138 |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
139 def tokenize(readline, tokeneater=printtoken): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
140 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
141 The tokenize() function accepts two parameters: one representing the |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
142 input stream, and one providing an output mechanism for tokenize(). |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
143 |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
144 The first parameter, readline, must be a callable object which provides |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
145 the same interface as the readline() method of built-in file objects. |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
146 Each call to the function should return one line of input as a string. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
147 |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
148 The second parameter, tokeneater, must also be a callable object. It is |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
149 called once for each token, with five arguments, corresponding to the |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
150 tuples generated by generate_tokens(). |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
151 """ |
|
17178
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
152 try: |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
153 tokenize_loop(readline, tokeneater) |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
154 except StopTokenizing: |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
155 pass |
|
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
156 |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
157 # backwards compatible interface |
|
17178
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
158 def tokenize_loop(readline, tokeneater): |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
159 for token_info in generate_tokens(readline): |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
160 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
|
161 |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17178
diff
changeset
|
162 def generate_tokens(readline): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
163 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
164 The generate_tokens() generator requires one argment, readline, which |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
165 must be a callable object which provides the same interface as the |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
166 readline() method of built-in file objects. Each call to the function |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
167 should return one line of input as a string. |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
168 |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
169 The generator produces 5-tuples with these members: the token type; the |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
170 token string; a 2-tuple (srow, scol) of ints specifying the row and |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
171 column where the token begins in the source; a 2-tuple (erow, ecol) of |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
172 ints specifying the row and column where the token ends in the source; |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
173 and the line on which the token was found. The line passed is the |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
174 logical line; continuation lines are included. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22719
diff
changeset
|
175 """ |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 contline = None |
| 5172 | 180 indents = [0] |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
181 |
| 5172 | 182 while 1: # loop over lines in stream |
| 183 line = readline() | |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
184 lnum = lnum + 1 |
| 5172 | 185 pos, max = 0, len(line) |
| 558 | 186 |
| 5172 | 187 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
|
188 if not line: |
|
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 contline = None |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
197 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
|
198 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
|
199 strstart, (lnum, len(line)), contline) |
| 5172 | 200 contstr = '' |
|
7839
d4f5321b9d9e
Differentiate between NEWLINE token (an official newline) and NL token
Guido van Rossum <guido@python.org>
parents:
6532
diff
changeset
|
201 contline = None |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
202 continue |
| 5172 | 203 else: |
| 204 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
|
205 contline = contline + line |
| 5172 | 206 continue |
| 207 | |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
208 elif parenlev == 0 and not continued: # new statement |
| 5172 | 209 if not line: break |
| 210 column = 0 | |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
211 while pos < max: # measure leading whitespace |
| 5172 | 212 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
|
213 elif line[pos] == '\t': column = (column/tabsize + 1)*tabsize |
| 5172 | 214 elif line[pos] == '\f': column = 0 |
| 215 else: break | |
| 216 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
|
217 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
|
218 |
|
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
219 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
|
220 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
|
221 (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
|
222 continue |
| 558 | 223 |
| 5172 | 224 if column > indents[-1]: # count indents or dedents |
| 225 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
|
226 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line) |
| 5172 | 227 while column < indents[-1]: |
| 228 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
|
229 yield (DEDENT, '', (lnum, pos), (lnum, pos), line) |
| 5172 | 230 |
| 231 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
|
232 if not line: |
|
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
233 raise TokenError, ("EOF in multi-line statement", (lnum, 0)) |
| 5172 | 234 continued = 0 |
| 235 | |
| 236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 token, initial = line[start:end], line[start] |
| 5172 | 242 |
|
17178
707f95d05e69
Provide a StopTokenizing exception for conveniently exiting the loop.
Ka-Ping Yee <ping@zesty.ca>
parents:
16901
diff
changeset
|
243 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
|
244 (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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 yield (COMMENT, token, spos, epos, line) |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
251 elif token in triple_quoted: |
| 5172 | 252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 yield (STRING, token, spos, (lnum, pos), line) |
| 5172 | 258 else: |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
259 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
|
260 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
|
261 contline = line |
| 5172 | 262 break |
|
25016
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
263 elif initial in single_quoted or \ |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
264 token[:2] in single_quoted or \ |
|
0ebec83eb3dd
Speed up the most egregious "if token in (long tuple)" cases by using
Guido van Rossum <guido@python.org>
parents:
23521
diff
changeset
|
265 token[:3] in single_quoted: |
| 5172 | 266 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 contline = line |
| 5172 | 272 break |
| 273 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 continued = 1 |
| 5172 | 279 else: |
|
5266
1efc4273fdb7
Ka-Ping's muich improved version of March 26, 1997:
Guido van Rossum <guido@python.org>
parents:
5178
diff
changeset
|
280 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
|
281 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
|
282 yield (OP, token, spos, epos, line) |
| 5172 | 283 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
|
284 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
|
285 (lnum, pos), (lnum, pos+1), line) |
| 5172 | 286 pos = pos + 1 |
| 287 | |
| 288 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
|
289 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
|
290 yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '') |
| 5172 | 291 |
| 292 if __name__ == '__main__': # testing | |
| 293 import sys | |
|
5277
7df5a0d1f523
Ping's latest. Fixes triple quoted strings ending in odd
Guido van Rossum <guido@python.org>
parents:
5266
diff
changeset
|
294 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
|
295 else: tokenize(sys.stdin.readline) |
