Guido van Rossum | b51eaa1 | 1997-03-07 00:21:55 +0000 | [diff] [blame] | 1 | """Tokenization help for Python programs. |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 2 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 3 | tokenize(readline) is a generator that breaks a stream of |
| 4 | bytes into Python tokens. It decodes the bytes according to |
| 5 | PEP-0263 for determining source file encoding. |
| 6 | |
| 7 | It accepts a readline-like method which is called |
| 8 | repeatedly to get the next line of input (or b"" for EOF). It generates |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 9 | 5-tuples with these members: |
| 10 | |
| 11 | the token type (see token.py) |
| 12 | the token (a string) |
| 13 | the starting (row, column) indices of the token (a 2-tuple of ints) |
| 14 | the ending (row, column) indices of the token (a 2-tuple of ints) |
| 15 | the original line (string) |
| 16 | |
| 17 | It is designed to match the working of the Python tokenizer exactly, except |
| 18 | that it produces COMMENT tokens for comments and gives type OP for all |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 19 | operators. Aditionally, all token lists start with an ENCODING token |
| 20 | which tells you which encoding was used to decode the bytes stream.""" |
Guido van Rossum | b51eaa1 | 1997-03-07 00:21:55 +0000 | [diff] [blame] | 21 | |
Ka-Ping Yee | 244c593 | 2001-03-01 13:56:40 +0000 | [diff] [blame] | 22 | __author__ = 'Ka-Ping Yee <ping@lfw.org>' |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 23 | __credits__ = ('GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, ' |
| 24 | 'Skip Montanaro, Raymond Hettinger, Trent Nelson, ' |
| 25 | 'Michael Foord') |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 26 | import re, string, sys |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 27 | from token import * |
Benjamin Peterson | 433f32c | 2008-12-12 01:25:05 +0000 | [diff] [blame] | 28 | from codecs import lookup, BOM_UTF8 |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 29 | cookie_re = re.compile("coding[:=]\s*([-\w.]+)") |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 30 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 31 | import token |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 32 | __all__ = [x for x in dir(token) if not x.startswith("_")] |
| 33 | __all__.extend(["COMMENT", "tokenize", "detect_encoding", "NL", "untokenize", |
| 34 | "ENCODING", "TokenInfo"]) |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 35 | del token |
| 36 | |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 37 | COMMENT = N_TOKENS |
| 38 | tok_name[COMMENT] = 'COMMENT' |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 39 | NL = N_TOKENS + 1 |
| 40 | tok_name[NL] = 'NL' |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 41 | ENCODING = N_TOKENS + 2 |
| 42 | tok_name[ENCODING] = 'ENCODING' |
| 43 | N_TOKENS += 3 |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 44 | |
Raymond Hettinger | aa17a7f | 2009-04-29 14:21:25 +0000 | [diff] [blame] | 45 | class TokenInfo(tuple): |
| 46 | 'TokenInfo(type, string, start, end, line)' |
| 47 | |
| 48 | __slots__ = () |
| 49 | |
| 50 | _fields = ('type', 'string', 'start', 'end', 'line') |
| 51 | |
| 52 | def __new__(cls, type, string, start, end, line): |
| 53 | return tuple.__new__(cls, (type, string, start, end, line)) |
| 54 | |
| 55 | @classmethod |
| 56 | def _make(cls, iterable, new=tuple.__new__, len=len): |
| 57 | 'Make a new TokenInfo object from a sequence or iterable' |
| 58 | result = new(cls, iterable) |
| 59 | if len(result) != 5: |
| 60 | raise TypeError('Expected 5 arguments, got %d' % len(result)) |
| 61 | return result |
| 62 | |
| 63 | def __repr__(self): |
| 64 | return 'TokenInfo(type=%r, string=%r, start=%r, end=%r, line=%r)' % self |
| 65 | |
| 66 | def _asdict(self): |
| 67 | 'Return a new dict which maps field names to their values' |
| 68 | return dict(zip(self._fields, self)) |
| 69 | |
| 70 | def _replace(self, **kwds): |
| 71 | 'Return a new TokenInfo object replacing specified fields with new values' |
| 72 | result = self._make(map(kwds.pop, ('type', 'string', 'start', 'end', 'line'), self)) |
| 73 | if kwds: |
| 74 | raise ValueError('Got unexpected field names: %r' % kwds.keys()) |
| 75 | return result |
| 76 | |
| 77 | def __getnewargs__(self): |
| 78 | return tuple(self) |
| 79 | |
| 80 | type = property(lambda t: t[0]) |
| 81 | string = property(lambda t: t[1]) |
| 82 | start = property(lambda t: t[2]) |
| 83 | end = property(lambda t: t[3]) |
| 84 | line = property(lambda t: t[4]) |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 85 | |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 86 | def group(*choices): return '(' + '|'.join(choices) + ')' |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 87 | def any(*choices): return group(*choices) + '*' |
| 88 | def maybe(*choices): return group(*choices) + '?' |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 90 | # Note: we use unicode matching for names ("\w") but ascii matching for |
| 91 | # number literals. |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 92 | Whitespace = r'[ \f\t]*' |
| 93 | Comment = r'#[^\r\n]*' |
| 94 | Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment) |
| 95 | Name = r'[a-zA-Z_]\w*' |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 97 | Hexnumber = r'0[xX][0-9a-fA-F]+' |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 98 | Binnumber = r'0[bB][01]+' |
| 99 | Octnumber = r'0[oO][0-7]+' |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 100 | Decnumber = r'(?:0+|[1-9][0-9]*)' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 101 | Intnumber = group(Hexnumber, Binnumber, Octnumber, Decnumber) |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 102 | Exponent = r'[eE][-+]?[0-9]+' |
| 103 | Pointfloat = group(r'[0-9]+\.[0-9]*', r'\.[0-9]+') + maybe(Exponent) |
| 104 | Expfloat = r'[0-9]+' + Exponent |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 105 | Floatnumber = group(Pointfloat, Expfloat) |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 106 | Imagnumber = group(r'[0-9]+[jJ]', Floatnumber + r'[jJ]') |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 107 | Number = group(Imagnumber, Floatnumber, Intnumber) |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 108 | |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 109 | # Tail end of ' string. |
| 110 | Single = r"[^'\\]*(?:\\.[^'\\]*)*'" |
| 111 | # Tail end of " string. |
| 112 | Double = r'[^"\\]*(?:\\.[^"\\]*)*"' |
| 113 | # Tail end of ''' string. |
| 114 | Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''" |
| 115 | # Tail end of """ string. |
| 116 | Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""' |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 117 | Triple = group("[bB]?[rR]?'''", '[bB]?[rR]?"""') |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 118 | # Single-line ' or " string. |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 119 | String = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'", |
| 120 | r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"') |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 121 | |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 122 | # Because of leftmost-then-longest match semantics, be sure to put the |
| 123 | # longest operators first (e.g., if = came before ==, == would get |
| 124 | # recognized as two instances of =). |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 125 | Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"!=", |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 126 | r"//=?", r"->", |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 127 | r"[+\-*/%&|^=<>]=?", |
| 128 | r"~") |
Thomas Wouters | e1519a1 | 2000-08-24 21:44:52 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 130 | Bracket = '[][(){}]' |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 131 | Special = group(r'\r?\n', r'\.\.\.', r'[:;.,@]') |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 132 | Funny = group(Operator, Bracket, Special) |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 134 | PlainToken = group(Number, Funny, String, Name) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 135 | Token = Ignore + PlainToken |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 136 | |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 137 | # First (or only) line of ' or " string. |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 138 | ContStr = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" + |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 139 | group("'", r'\\\r?\n'), |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 140 | r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' + |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 141 | group('"', r'\\\r?\n')) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 142 | PseudoExtras = group(r'\\\r?\n', Comment, Triple) |
| 143 | PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 145 | tokenprog, pseudoprog, single3prog, double3prog = map( |
| 146 | re.compile, (Token, PseudoToken, Single3, Double3)) |
Guido van Rossum | fefc922 | 1997-10-27 21:17:24 +0000 | [diff] [blame] | 147 | endprogs = {"'": re.compile(Single), '"': re.compile(Double), |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 148 | "'''": single3prog, '"""': double3prog, |
Guido van Rossum | fefc922 | 1997-10-27 21:17:24 +0000 | [diff] [blame] | 149 | "r'''": single3prog, 'r"""': double3prog, |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 150 | "b'''": single3prog, 'b"""': double3prog, |
| 151 | "br'''": single3prog, 'br"""': double3prog, |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 152 | "R'''": single3prog, 'R"""': double3prog, |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 153 | "B'''": single3prog, 'B"""': double3prog, |
| 154 | "bR'''": single3prog, 'bR"""': double3prog, |
| 155 | "Br'''": single3prog, 'Br"""': double3prog, |
| 156 | "BR'''": single3prog, 'BR"""': double3prog, |
| 157 | 'r': None, 'R': None, 'b': None, 'B': None} |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | 9d6897a | 2002-08-24 06:54:19 +0000 | [diff] [blame] | 159 | triple_quoted = {} |
| 160 | for t in ("'''", '"""', |
| 161 | "r'''", 'r"""', "R'''", 'R"""', |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 162 | "b'''", 'b"""', "B'''", 'B"""', |
| 163 | "br'''", 'br"""', "Br'''", 'Br"""', |
| 164 | "bR'''", 'bR"""', "BR'''", 'BR"""'): |
Guido van Rossum | 9d6897a | 2002-08-24 06:54:19 +0000 | [diff] [blame] | 165 | triple_quoted[t] = t |
| 166 | single_quoted = {} |
| 167 | for t in ("'", '"', |
| 168 | "r'", 'r"', "R'", 'R"', |
Guido van Rossum | 4fe72f9 | 2007-11-12 17:40:10 +0000 | [diff] [blame] | 169 | "b'", 'b"', "B'", 'B"', |
| 170 | "br'", 'br"', "Br'", 'Br"', |
| 171 | "bR'", 'bR"', "BR'", 'BR"' ): |
Guido van Rossum | 9d6897a | 2002-08-24 06:54:19 +0000 | [diff] [blame] | 172 | single_quoted[t] = t |
| 173 | |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 174 | tabsize = 8 |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 175 | |
Ka-Ping Yee | 28c62bb | 2001-03-23 05:22:49 +0000 | [diff] [blame] | 176 | class TokenError(Exception): pass |
| 177 | |
| 178 | class StopTokenizing(Exception): pass |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 179 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 180 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 181 | class Untokenizer: |
| 182 | |
| 183 | def __init__(self): |
| 184 | self.tokens = [] |
| 185 | self.prev_row = 1 |
| 186 | self.prev_col = 0 |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 187 | self.encoding = None |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 188 | |
| 189 | def add_whitespace(self, start): |
| 190 | row, col = start |
| 191 | assert row <= self.prev_row |
| 192 | col_offset = col - self.prev_col |
| 193 | if col_offset: |
| 194 | self.tokens.append(" " * col_offset) |
| 195 | |
| 196 | def untokenize(self, iterable): |
| 197 | for t in iterable: |
| 198 | if len(t) == 2: |
| 199 | self.compat(t, iterable) |
| 200 | break |
| 201 | tok_type, token, start, end, line = t |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 202 | if tok_type == ENCODING: |
| 203 | self.encoding = token |
| 204 | continue |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 205 | self.add_whitespace(start) |
| 206 | self.tokens.append(token) |
| 207 | self.prev_row, self.prev_col = end |
| 208 | if tok_type in (NEWLINE, NL): |
| 209 | self.prev_row += 1 |
| 210 | self.prev_col = 0 |
| 211 | return "".join(self.tokens) |
| 212 | |
| 213 | def compat(self, token, iterable): |
| 214 | startline = False |
| 215 | indents = [] |
| 216 | toks_append = self.tokens.append |
| 217 | toknum, tokval = token |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 218 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 219 | if toknum in (NAME, NUMBER): |
| 220 | tokval += ' ' |
| 221 | if toknum in (NEWLINE, NL): |
| 222 | startline = True |
Christian Heimes | ba4af49 | 2008-03-28 00:55:15 +0000 | [diff] [blame] | 223 | prevstring = False |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 224 | for tok in iterable: |
| 225 | toknum, tokval = tok[:2] |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 226 | if toknum == ENCODING: |
| 227 | self.encoding = tokval |
| 228 | continue |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 229 | |
| 230 | if toknum in (NAME, NUMBER): |
| 231 | tokval += ' ' |
| 232 | |
Christian Heimes | ba4af49 | 2008-03-28 00:55:15 +0000 | [diff] [blame] | 233 | # Insert a space between two consecutive strings |
| 234 | if toknum == STRING: |
| 235 | if prevstring: |
| 236 | tokval = ' ' + tokval |
| 237 | prevstring = True |
| 238 | else: |
| 239 | prevstring = False |
| 240 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 241 | if toknum == INDENT: |
| 242 | indents.append(tokval) |
| 243 | continue |
| 244 | elif toknum == DEDENT: |
| 245 | indents.pop() |
| 246 | continue |
| 247 | elif toknum in (NEWLINE, NL): |
| 248 | startline = True |
| 249 | elif startline and indents: |
| 250 | toks_append(indents[-1]) |
| 251 | startline = False |
| 252 | toks_append(tokval) |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 253 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 254 | |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 255 | def untokenize(iterable): |
| 256 | """Transform tokens back into Python source code. |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 257 | It returns a bytes object, encoded using the ENCODING |
| 258 | token, which is the first token sequence output by tokenize. |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 259 | |
| 260 | Each element returned by the iterable must be a token sequence |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 261 | with at least two elements, a token number and token value. If |
| 262 | only two tokens are passed, the resulting output is poor. |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 263 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 264 | Round-trip invariant for full input: |
| 265 | Untokenized source will match input source exactly |
| 266 | |
| 267 | Round-trip invariant for limited intput: |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 268 | # Output bytes will tokenize the back to the input |
| 269 | t1 = [tok[:2] for tok in tokenize(f.readline)] |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 270 | newcode = untokenize(t1) |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 271 | readline = BytesIO(newcode).readline |
| 272 | t2 = [tok[:2] for tok in tokenize(readline)] |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 273 | assert t1 == t2 |
| 274 | """ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 275 | ut = Untokenizer() |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 276 | out = ut.untokenize(iterable) |
| 277 | if ut.encoding is not None: |
| 278 | out = out.encode(ut.encoding) |
| 279 | return out |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 280 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 281 | |
Benjamin Peterson | d3afada | 2009-10-09 21:43:09 +0000 | [diff] [blame] | 282 | def _get_normal_name(orig_enc): |
| 283 | """Imitates get_normal_name in tokenizer.c.""" |
| 284 | # Only care about the first 12 characters. |
| 285 | enc = orig_enc[:12].lower().replace("_", "-") |
| 286 | if enc == "utf-8" or enc.startswith("utf-8-"): |
| 287 | return "utf-8" |
| 288 | if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \ |
| 289 | enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")): |
| 290 | return "iso-8859-1" |
| 291 | return orig_enc |
| 292 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 293 | def detect_encoding(readline): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 294 | """ |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 295 | The detect_encoding() function is used to detect the encoding that should |
| 296 | be used to decode a Python source file. It requires one argment, readline, |
| 297 | in the same way as the tokenize() generator. |
| 298 | |
| 299 | It will call readline a maximum of twice, and return the encoding used |
| 300 | (as a string) and a list of any lines (left as bytes) it has read |
| 301 | in. |
| 302 | |
| 303 | It detects the encoding from the presence of a utf-8 bom or an encoding |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 304 | cookie as specified in pep-0263. If both a bom and a cookie are present, but |
| 305 | disagree, a SyntaxError will be raised. If the encoding cookie is an invalid |
| 306 | charset, raise a SyntaxError. Note that if a utf-8 bom is found, |
| 307 | 'utf-8-sig' is returned. |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 308 | |
| 309 | If no encoding is specified, then the default of 'utf-8' will be returned. |
| 310 | """ |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 311 | bom_found = False |
| 312 | encoding = None |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 313 | default = 'utf-8' |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 314 | def read_or_stop(): |
| 315 | try: |
| 316 | return readline() |
| 317 | except StopIteration: |
| 318 | return b'' |
| 319 | |
| 320 | def find_cookie(line): |
| 321 | try: |
| 322 | line_string = line.decode('ascii') |
| 323 | except UnicodeDecodeError: |
Benjamin Peterson | 433f32c | 2008-12-12 01:25:05 +0000 | [diff] [blame] | 324 | return None |
| 325 | |
| 326 | matches = cookie_re.findall(line_string) |
| 327 | if not matches: |
| 328 | return None |
Benjamin Peterson | d3afada | 2009-10-09 21:43:09 +0000 | [diff] [blame] | 329 | encoding = _get_normal_name(matches[0]) |
Benjamin Peterson | 433f32c | 2008-12-12 01:25:05 +0000 | [diff] [blame] | 330 | try: |
| 331 | codec = lookup(encoding) |
| 332 | except LookupError: |
| 333 | # This behaviour mimics the Python interpreter |
| 334 | raise SyntaxError("unknown encoding: " + encoding) |
| 335 | |
Benjamin Peterson | 1613ed8 | 2010-03-18 22:34:15 +0000 | [diff] [blame] | 336 | if bom_found: |
| 337 | if codec.name != 'utf-8': |
| 338 | # This behaviour mimics the Python interpreter |
| 339 | raise SyntaxError('encoding problem: utf-8') |
| 340 | encoding += '-sig' |
Benjamin Peterson | 433f32c | 2008-12-12 01:25:05 +0000 | [diff] [blame] | 341 | return encoding |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 342 | |
| 343 | first = read_or_stop() |
Benjamin Peterson | 433f32c | 2008-12-12 01:25:05 +0000 | [diff] [blame] | 344 | if first.startswith(BOM_UTF8): |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 345 | bom_found = True |
| 346 | first = first[3:] |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 347 | default = 'utf-8-sig' |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 348 | if not first: |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 349 | return default, [] |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 350 | |
| 351 | encoding = find_cookie(first) |
| 352 | if encoding: |
| 353 | return encoding, [first] |
| 354 | |
| 355 | second = read_or_stop() |
| 356 | if not second: |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 357 | return default, [first] |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 358 | |
| 359 | encoding = find_cookie(second) |
| 360 | if encoding: |
| 361 | return encoding, [first, second] |
| 362 | |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 363 | return default, [first, second] |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 364 | |
| 365 | |
| 366 | def tokenize(readline): |
| 367 | """ |
| 368 | The tokenize() generator requires one argment, readline, which |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 369 | must be a callable object which provides the same interface as the |
| 370 | readline() method of built-in file objects. Each call to the function |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 371 | should return one line of input as bytes. Alternately, readline |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 372 | can be a callable function terminating with StopIteration: |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 373 | readline = open(myfile, 'rb').__next__ # Example of alternate readline |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 374 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 375 | The generator produces 5-tuples with these members: the token type; the |
| 376 | token string; a 2-tuple (srow, scol) of ints specifying the row and |
| 377 | column where the token begins in the source; a 2-tuple (erow, ecol) of |
| 378 | ints specifying the row and column where the token ends in the source; |
| 379 | and the line on which the token was found. The line passed is the |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 380 | logical line; continuation lines are included. |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 381 | |
| 382 | The first token sequence will always be an ENCODING token |
| 383 | which tells you which encoding was used to decode the bytes stream. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 384 | """ |
Benjamin Peterson | 21db77e | 2009-11-14 16:27:26 +0000 | [diff] [blame] | 385 | # This import is here to avoid problems when the itertools module is not |
| 386 | # built yet and tokenize is imported. |
Benjamin Peterson | 81dd8b9 | 2009-11-14 18:09:17 +0000 | [diff] [blame] | 387 | from itertools import chain, repeat |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 388 | encoding, consumed = detect_encoding(readline) |
Benjamin Peterson | 81dd8b9 | 2009-11-14 18:09:17 +0000 | [diff] [blame] | 389 | rl_gen = iter(readline, b"") |
| 390 | empty = repeat(b"") |
| 391 | return _tokenize(chain(consumed, rl_gen, empty).__next__, encoding) |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 392 | |
| 393 | |
| 394 | def _tokenize(readline, encoding): |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 395 | lnum = parenlev = continued = 0 |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 396 | namechars, numchars = string.ascii_letters + '_', '0123456789' |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 397 | contstr, needcont = '', 0 |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 398 | contline = None |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 399 | indents = [0] |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 400 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 401 | if encoding is not None: |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 402 | if encoding == "utf-8-sig": |
| 403 | # BOM will already have been stripped. |
| 404 | encoding = "utf-8" |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 405 | yield TokenInfo(ENCODING, encoding, (0, 0), (0, 0), '') |
Benjamin Peterson | 0fe1438 | 2008-06-05 23:07:42 +0000 | [diff] [blame] | 406 | while True: # loop over lines in stream |
Raymond Hettinger | 68c0453 | 2005-06-10 11:05:19 +0000 | [diff] [blame] | 407 | try: |
| 408 | line = readline() |
| 409 | except StopIteration: |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 410 | line = b'' |
| 411 | |
| 412 | if encoding is not None: |
| 413 | line = line.decode(encoding) |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 414 | lnum += 1 |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 415 | pos, max = 0, len(line) |
| 416 | |
| 417 | if contstr: # continued string |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 418 | if not line: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 419 | raise TokenError("EOF in multi-line string", strstart) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 420 | endmatch = endprog.match(line) |
| 421 | if endmatch: |
| 422 | pos = end = endmatch.end(0) |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 423 | yield TokenInfo(STRING, contstr + line[:end], |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 424 | strstart, (lnum, end), contline + line) |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 425 | contstr, needcont = '', 0 |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 426 | contline = None |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 427 | elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n': |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 428 | yield TokenInfo(ERRORTOKEN, contstr + line, |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 429 | strstart, (lnum, len(line)), contline) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 430 | contstr = '' |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 431 | contline = None |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 432 | continue |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 433 | else: |
| 434 | contstr = contstr + line |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 435 | contline = contline + line |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 436 | continue |
| 437 | |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 438 | elif parenlev == 0 and not continued: # new statement |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 439 | if not line: break |
| 440 | column = 0 |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 441 | while pos < max: # measure leading whitespace |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 442 | if line[pos] == ' ': |
| 443 | column += 1 |
| 444 | elif line[pos] == '\t': |
| 445 | column = (column//tabsize + 1)*tabsize |
| 446 | elif line[pos] == '\f': |
| 447 | column = 0 |
| 448 | else: |
| 449 | break |
| 450 | pos += 1 |
| 451 | if pos == max: |
| 452 | break |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 453 | |
| 454 | if line[pos] in '#\r\n': # skip comments or blank lines |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 455 | if line[pos] == '#': |
| 456 | comment_token = line[pos:].rstrip('\r\n') |
| 457 | nl_pos = pos + len(comment_token) |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 458 | yield TokenInfo(COMMENT, comment_token, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 459 | (lnum, pos), (lnum, pos + len(comment_token)), line) |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 460 | yield TokenInfo(NL, line[nl_pos:], |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 461 | (lnum, nl_pos), (lnum, len(line)), line) |
| 462 | else: |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 463 | yield TokenInfo((NL, COMMENT)[line[pos] == '#'], line[pos:], |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 464 | (lnum, pos), (lnum, len(line)), line) |
| 465 | continue |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 466 | |
| 467 | if column > indents[-1]: # count indents or dedents |
| 468 | indents.append(column) |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 469 | yield TokenInfo(INDENT, line[:pos], (lnum, 0), (lnum, pos), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 470 | while column < indents[-1]: |
Raymond Hettinger | da99d1c | 2005-06-21 07:43:58 +0000 | [diff] [blame] | 471 | if column not in indents: |
| 472 | raise IndentationError( |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 473 | "unindent does not match any outer indentation level", |
| 474 | ("<tokenize>", lnum, pos, line)) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 475 | indents = indents[:-1] |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 476 | yield TokenInfo(DEDENT, '', (lnum, pos), (lnum, pos), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 477 | |
| 478 | else: # continued statement |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 479 | if not line: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 480 | raise TokenError("EOF in multi-line statement", (lnum, 0)) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 481 | continued = 0 |
| 482 | |
| 483 | while pos < max: |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 484 | pseudomatch = pseudoprog.match(line, pos) |
| 485 | if pseudomatch: # scan for tokens |
| 486 | start, end = pseudomatch.span(1) |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 487 | spos, epos, pos = (lnum, start), (lnum, end), end |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 488 | token, initial = line[start:end], line[start] |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 489 | |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 490 | if (initial in numchars or # ordinary number |
| 491 | (initial == '.' and token != '.' and token != '...')): |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 492 | yield TokenInfo(NUMBER, token, spos, epos, line) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 493 | elif initial in '\r\n': |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 494 | yield TokenInfo(NL if parenlev > 0 else NEWLINE, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 495 | token, spos, epos, line) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 496 | elif initial == '#': |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 497 | assert not token.endswith("\n") |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 498 | yield TokenInfo(COMMENT, token, spos, epos, line) |
Guido van Rossum | 9d6897a | 2002-08-24 06:54:19 +0000 | [diff] [blame] | 499 | elif token in triple_quoted: |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 500 | endprog = endprogs[token] |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 501 | endmatch = endprog.match(line, pos) |
| 502 | if endmatch: # all on one line |
| 503 | pos = endmatch.end(0) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 504 | token = line[start:pos] |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 505 | yield TokenInfo(STRING, token, spos, (lnum, pos), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 506 | else: |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 507 | strstart = (lnum, start) # multiple lines |
| 508 | contstr = line[start:] |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 509 | contline = line |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 510 | break |
Guido van Rossum | 9d6897a | 2002-08-24 06:54:19 +0000 | [diff] [blame] | 511 | elif initial in single_quoted or \ |
| 512 | token[:2] in single_quoted or \ |
| 513 | token[:3] in single_quoted: |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 514 | if token[-1] == '\n': # continued string |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 515 | strstart = (lnum, start) |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 516 | endprog = (endprogs[initial] or endprogs[token[1]] or |
| 517 | endprogs[token[2]]) |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 518 | contstr, needcont = line[start:], 1 |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 519 | contline = line |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 520 | break |
| 521 | else: # ordinary string |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 522 | yield TokenInfo(STRING, token, spos, epos, line) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 523 | elif initial in namechars: # ordinary name |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 524 | yield TokenInfo(NAME, token, spos, epos, line) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 525 | elif initial == '\\': # continued stmt |
| 526 | continued = 1 |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 527 | else: |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 528 | if initial in '([{': |
| 529 | parenlev += 1 |
| 530 | elif initial in ')]}': |
| 531 | parenlev -= 1 |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 532 | yield TokenInfo(OP, token, spos, epos, line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 533 | else: |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 534 | yield TokenInfo(ERRORTOKEN, line[pos], |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 535 | (lnum, pos), (lnum, pos+1), line) |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 536 | pos += 1 |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 537 | |
| 538 | for indent in indents[1:]: # pop remaining indent levels |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 539 | yield TokenInfo(DEDENT, '', (lnum, 0), (lnum, 0), '') |
| 540 | yield TokenInfo(ENDMARKER, '', (lnum, 0), (lnum, 0), '') |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 541 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 542 | |
| 543 | # An undocumented, backwards compatible, API for all the places in the standard |
| 544 | # library that expect to be able to use tokenize with strings |
| 545 | def generate_tokens(readline): |
| 546 | return _tokenize(readline, None) |