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 | |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 3 | generate_tokens(readline) is a generator that breaks a stream of |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 4 | text into Python tokens. It accepts a readline-like method which is called |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 5 | repeatedly to get the next line of input (or "" for EOF). It generates |
| 6 | 5-tuples with these members: |
| 7 | |
| 8 | the token type (see token.py) |
| 9 | the token (a string) |
| 10 | the starting (row, column) indices of the token (a 2-tuple of ints) |
| 11 | the ending (row, column) indices of the token (a 2-tuple of ints) |
| 12 | the original line (string) |
| 13 | |
| 14 | It is designed to match the working of the Python tokenizer exactly, except |
| 15 | that it produces COMMENT tokens for comments and gives type OP for all |
| 16 | operators |
| 17 | |
| 18 | Older entry points |
| 19 | tokenize_loop(readline, tokeneater) |
| 20 | tokenize(readline, tokeneater=printtoken) |
| 21 | are the same, except instead of generating tokens, tokeneater is a callback |
| 22 | function to which the 5 fields described above are passed as 5 arguments, |
| 23 | each time a new token is found.""" |
Guido van Rossum | b51eaa1 | 1997-03-07 00:21:55 +0000 | [diff] [blame] | 24 | |
Ka-Ping Yee | 244c593 | 2001-03-01 13:56:40 +0000 | [diff] [blame] | 25 | __author__ = 'Ka-Ping Yee <ping@lfw.org>' |
Ka-Ping Yee | 4f64c13 | 2001-03-01 17:11:17 +0000 | [diff] [blame] | 26 | __credits__ = \ |
| 27 | 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro' |
Guido van Rossum | b51eaa1 | 1997-03-07 00:21:55 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 29 | import string, re |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 30 | from token import * |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 31 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 32 | import token |
| 33 | __all__ = [x for x in dir(token) if x[0] != '_'] + ["COMMENT", "tokenize", "NL"] |
Neal Norwitz | e98d16e | 2002-03-26 16:20:26 +0000 | [diff] [blame] | 34 | del x |
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' |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 41 | N_TOKENS += 2 |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 42 | |
Eric S. Raymond | b08b2d3 | 2001-02-09 11:10:16 +0000 | [diff] [blame] | 43 | def group(*choices): return '(' + '|'.join(choices) + ')' |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 44 | def any(*choices): return apply(group, choices) + '*' |
| 45 | def maybe(*choices): return apply(group, choices) + '?' |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 47 | Whitespace = r'[ \f\t]*' |
| 48 | Comment = r'#[^\r\n]*' |
| 49 | Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment) |
| 50 | Name = r'[a-zA-Z_]\w*' |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 52 | Hexnumber = r'0[xX][\da-fA-F]*[lL]?' |
| 53 | Octnumber = r'0[0-7]*[lL]?' |
| 54 | Decnumber = r'[1-9]\d*[lL]?' |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 55 | Intnumber = group(Hexnumber, Octnumber, Decnumber) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 56 | Exponent = r'[eE][-+]?\d+' |
| 57 | Pointfloat = group(r'\d+\.\d*', r'\.\d+') + maybe(Exponent) |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 58 | Expfloat = r'\d+' + Exponent |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 59 | Floatnumber = group(Pointfloat, Expfloat) |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 60 | Imagnumber = group(r'\d+[jJ]', Floatnumber + r'[jJ]') |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 61 | Number = group(Imagnumber, Floatnumber, Intnumber) |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 62 | |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 63 | # Tail end of ' string. |
| 64 | Single = r"[^'\\]*(?:\\.[^'\\]*)*'" |
| 65 | # Tail end of " string. |
| 66 | Double = r'[^"\\]*(?:\\.[^"\\]*)*"' |
| 67 | # Tail end of ''' string. |
| 68 | Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''" |
| 69 | # Tail end of """ string. |
| 70 | Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""' |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 71 | Triple = group("[uU]?[rR]?'''", '[uU]?[rR]?"""') |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 72 | # Single-line ' or " string. |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 73 | String = group(r"[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'", |
| 74 | r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"') |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 75 | |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 76 | # Because of leftmost-then-longest match semantics, be sure to put the |
| 77 | # longest operators first (e.g., if = came before ==, == would get |
| 78 | # recognized as two instances of =). |
| 79 | Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", r"!=", |
Guido van Rossum | 96204f5 | 2001-08-08 05:04:07 +0000 | [diff] [blame] | 80 | r"//=?", |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 81 | r"[+\-*/%&|^=<>]=?", |
| 82 | r"~") |
Thomas Wouters | e1519a1 | 2000-08-24 21:44:52 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 84 | Bracket = '[][(){}]' |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 85 | Special = group(r'\r?\n', r'[:;.,`]') |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 86 | Funny = group(Operator, Bracket, Special) |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 88 | PlainToken = group(Number, Funny, String, Name) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 89 | Token = Ignore + PlainToken |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 90 | |
Tim Peters | de49583 | 2000-10-07 05:09:39 +0000 | [diff] [blame] | 91 | # First (or only) line of ' or " string. |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 92 | ContStr = group(r"[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" + |
| 93 | group("'", r'\\\r?\n'), |
| 94 | r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' + |
| 95 | group('"', r'\\\r?\n')) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 96 | PseudoExtras = group(r'\\\r?\n', Comment, Triple) |
| 97 | PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 99 | tokenprog, pseudoprog, single3prog, double3prog = map( |
| 100 | re.compile, (Token, PseudoToken, Single3, Double3)) |
Guido van Rossum | fefc922 | 1997-10-27 21:17:24 +0000 | [diff] [blame] | 101 | endprogs = {"'": re.compile(Single), '"': re.compile(Double), |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 102 | "'''": single3prog, '"""': double3prog, |
Guido van Rossum | fefc922 | 1997-10-27 21:17:24 +0000 | [diff] [blame] | 103 | "r'''": single3prog, 'r"""': double3prog, |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 104 | "u'''": single3prog, 'u"""': double3prog, |
| 105 | "ur'''": single3prog, 'ur"""': double3prog, |
| 106 | "R'''": single3prog, 'R"""': double3prog, |
| 107 | "U'''": single3prog, 'U"""': double3prog, |
| 108 | "uR'''": single3prog, 'uR"""': double3prog, |
| 109 | "Ur'''": single3prog, 'Ur"""': double3prog, |
| 110 | "UR'''": single3prog, 'UR"""': double3prog, |
| 111 | 'r': None, 'R': None, 'u': None, 'U': None} |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 113 | tabsize = 8 |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 114 | |
Ka-Ping Yee | 28c62bb | 2001-03-23 05:22:49 +0000 | [diff] [blame] | 115 | class TokenError(Exception): pass |
| 116 | |
| 117 | class StopTokenizing(Exception): pass |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 119 | def printtoken(type, token, (srow, scol), (erow, ecol), line): # for testing |
| 120 | print "%d,%d-%d,%d:\t%s\t%s" % \ |
| 121 | (srow, scol, erow, ecol, tok_name[type], repr(token)) |
Guido van Rossum | 4d8e859 | 1992-01-01 19:34:47 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 123 | def tokenize(readline, tokeneater=printtoken): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 124 | """ |
| 125 | The tokenize() function accepts two parameters: one representing the |
| 126 | input stream, and one providing an output mechanism for tokenize(). |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 127 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 128 | The first parameter, readline, must be a callable object which provides |
| 129 | the same interface as the readline() method of built-in file objects. |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 130 | Each call to the function should return one line of input as a string. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 131 | |
| 132 | The second parameter, tokeneater, must also be a callable object. It is |
| 133 | called once for each token, with five arguments, corresponding to the |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 134 | tuples generated by generate_tokens(). |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 135 | """ |
Ka-Ping Yee | 28c62bb | 2001-03-23 05:22:49 +0000 | [diff] [blame] | 136 | try: |
| 137 | tokenize_loop(readline, tokeneater) |
| 138 | except StopTokenizing: |
| 139 | pass |
| 140 | |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 141 | # backwards compatible interface |
Ka-Ping Yee | 28c62bb | 2001-03-23 05:22:49 +0000 | [diff] [blame] | 142 | def tokenize_loop(readline, tokeneater): |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 143 | for token_info in generate_tokens(readline): |
| 144 | apply(tokeneater, token_info) |
| 145 | |
| 146 | def generate_tokens(readline): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 147 | """ |
| 148 | The generate_tokens() generator requires one argment, readline, which |
| 149 | must be a callable object which provides the same interface as the |
| 150 | readline() method of built-in file objects. Each call to the function |
| 151 | should return one line of input as a string. |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 152 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 153 | The generator produces 5-tuples with these members: the token type; the |
| 154 | token string; a 2-tuple (srow, scol) of ints specifying the row and |
| 155 | column where the token begins in the source; a 2-tuple (erow, ecol) of |
| 156 | ints specifying the row and column where the token ends in the source; |
| 157 | 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] | 158 | logical line; continuation lines are included. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 159 | """ |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 160 | lnum = parenlev = continued = 0 |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 161 | namechars, numchars = string.ascii_letters + '_', '0123456789' |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 162 | contstr, needcont = '', 0 |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 163 | contline = None |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 164 | indents = [0] |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 166 | while 1: # loop over lines in stream |
| 167 | line = readline() |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 168 | lnum = lnum + 1 |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 169 | pos, max = 0, len(line) |
| 170 | |
| 171 | if contstr: # continued string |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 172 | if not line: |
| 173 | raise TokenError, ("EOF in multi-line string", strstart) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 174 | endmatch = endprog.match(line) |
| 175 | if endmatch: |
| 176 | pos = end = endmatch.end(0) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 177 | yield (STRING, contstr + line[:end], |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 178 | strstart, (lnum, end), contline + line) |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 179 | contstr, needcont = '', 0 |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 180 | contline = None |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 181 | elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n': |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 182 | yield (ERRORTOKEN, contstr + line, |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 183 | strstart, (lnum, len(line)), contline) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 184 | contstr = '' |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 185 | contline = None |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 186 | continue |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 187 | else: |
| 188 | contstr = contstr + line |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 189 | contline = contline + line |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 190 | continue |
| 191 | |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 192 | elif parenlev == 0 and not continued: # new statement |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 193 | if not line: break |
| 194 | column = 0 |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 195 | while pos < max: # measure leading whitespace |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 196 | if line[pos] == ' ': column = column + 1 |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 197 | elif line[pos] == '\t': column = (column/tabsize + 1)*tabsize |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 198 | elif line[pos] == '\f': column = 0 |
| 199 | else: break |
| 200 | pos = pos + 1 |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 201 | if pos == max: break |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 202 | |
| 203 | if line[pos] in '#\r\n': # skip comments or blank lines |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 204 | yield ((NL, COMMENT)[line[pos] == '#'], line[pos:], |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 205 | (lnum, pos), (lnum, len(line)), line) |
| 206 | continue |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 207 | |
| 208 | if column > indents[-1]: # count indents or dedents |
| 209 | indents.append(column) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 210 | yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 211 | while column < indents[-1]: |
| 212 | indents = indents[:-1] |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 213 | yield (DEDENT, '', (lnum, pos), (lnum, pos), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 214 | |
| 215 | else: # continued statement |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 216 | if not line: |
| 217 | raise TokenError, ("EOF in multi-line statement", (lnum, 0)) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 218 | continued = 0 |
| 219 | |
| 220 | while pos < max: |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 221 | pseudomatch = pseudoprog.match(line, pos) |
| 222 | if pseudomatch: # scan for tokens |
| 223 | start, end = pseudomatch.span(1) |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 224 | spos, epos, pos = (lnum, start), (lnum, end), end |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 225 | token, initial = line[start:end], line[start] |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 226 | |
Ka-Ping Yee | 28c62bb | 2001-03-23 05:22:49 +0000 | [diff] [blame] | 227 | if initial in numchars or \ |
| 228 | (initial == '.' and token != '.'): # ordinary number |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 229 | yield (NUMBER, token, spos, epos, line) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 230 | elif initial in '\r\n': |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 231 | yield (parenlev > 0 and NL or NEWLINE, |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 232 | token, spos, epos, line) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 233 | elif initial == '#': |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 234 | yield (COMMENT, token, spos, epos, line) |
Guido van Rossum | fefc922 | 1997-10-27 21:17:24 +0000 | [diff] [blame] | 235 | elif token in ("'''", '"""', # triple-quoted |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 236 | "r'''", 'r"""', "R'''", 'R"""', |
| 237 | "u'''", 'u"""', "U'''", 'U"""', |
| 238 | "ur'''", 'ur"""', "Ur'''", 'Ur"""', |
| 239 | "uR'''", 'uR"""', "UR'''", 'UR"""'): |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 240 | endprog = endprogs[token] |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 241 | endmatch = endprog.match(line, pos) |
| 242 | if endmatch: # all on one line |
| 243 | pos = endmatch.end(0) |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 244 | token = line[start:pos] |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 245 | yield (STRING, token, spos, (lnum, pos), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 246 | else: |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 247 | strstart = (lnum, start) # multiple lines |
| 248 | contstr = line[start:] |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 249 | contline = line |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 250 | break |
Guido van Rossum | fefc922 | 1997-10-27 21:17:24 +0000 | [diff] [blame] | 251 | elif initial in ("'", '"') or \ |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 252 | token[:2] in ("r'", 'r"', "R'", 'R"', |
| 253 | "u'", 'u"', "U'", 'U"') or \ |
| 254 | token[:3] in ("ur'", 'ur"', "Ur'", 'Ur"', |
| 255 | "uR'", 'uR"', "UR'", 'UR"' ): |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 256 | if token[-1] == '\n': # continued string |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 257 | strstart = (lnum, start) |
Ka-Ping Yee | 1ff08b1 | 2001-01-15 22:04:30 +0000 | [diff] [blame] | 258 | endprog = (endprogs[initial] or endprogs[token[1]] or |
| 259 | endprogs[token[2]]) |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 260 | contstr, needcont = line[start:], 1 |
Guido van Rossum | a90c78b | 1998-04-03 16:05:38 +0000 | [diff] [blame] | 261 | contline = line |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 262 | break |
| 263 | else: # ordinary string |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 264 | yield (STRING, token, spos, epos, line) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 265 | elif initial in namechars: # ordinary name |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 266 | yield (NAME, token, spos, epos, line) |
Guido van Rossum | 3b63177 | 1997-10-27 20:44:15 +0000 | [diff] [blame] | 267 | elif initial == '\\': # continued stmt |
| 268 | continued = 1 |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 269 | else: |
Guido van Rossum | 1aec323 | 1997-04-08 14:24:39 +0000 | [diff] [blame] | 270 | if initial in '([{': parenlev = parenlev + 1 |
| 271 | elif initial in ')]}': parenlev = parenlev - 1 |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 272 | yield (OP, token, spos, epos, line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 273 | else: |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 274 | yield (ERRORTOKEN, line[pos], |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 275 | (lnum, pos), (lnum, pos+1), line) |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 276 | pos = pos + 1 |
| 277 | |
| 278 | for indent in indents[1:]: # pop remaining indent levels |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 279 | yield (DEDENT, '', (lnum, 0), (lnum, 0), '') |
| 280 | yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '') |
Guido van Rossum | fc6f533 | 1997-03-07 00:21:12 +0000 | [diff] [blame] | 281 | |
| 282 | if __name__ == '__main__': # testing |
| 283 | import sys |
Guido van Rossum | de65527 | 1997-04-09 17:15:54 +0000 | [diff] [blame] | 284 | if len(sys.argv) > 1: tokenize(open(sys.argv[1]).readline) |
Guido van Rossum | 2b1566b | 1997-06-03 22:05:15 +0000 | [diff] [blame] | 285 | else: tokenize(sys.stdin.readline) |