Trees | Indices | Help |
|
---|
|
Tokenization help for Python programs. generate_tokens(readline) is a generator that breaks a stream of text into Python tokens. It accepts a readline-like method which is called repeatedly to get the next line of input (or "" for EOF). It generates 5-tuples with these members: the token type (see token.py) the token (a string) the starting (row, column) indices of the token (a 2-tuple of ints) the ending (row, column) indices of the token (a 2-tuple of ints) the original line (string) It is designed to match the working of the Python tokenizer exactly, except that it produces COMMENT tokens for comments and gives type OP for all operators Older entry points tokenize_loop(readline, tokeneater) tokenize(readline, tokeneater=printtoken) are the same, except instead of generating tokens, tokeneater is a callback function to which the 5 fields described above are passed as 5 arguments, each time a new token is found.
Author: Ka-Ping Yee <ping@lfw.org>
|
|||
TokenError | |||
StopTokenizing |
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|
|||
__credits__ =
|
|||
DEDENT = 6
|
|||
LPAR = 7
|
|||
STAR = 16
|
|||
SLASH = 17
|
|||
LESS = 20
|
|||
SLASHEQUAL = 40
|
|||
NUMBER = 2
|
|||
RPAR = 8
|
|||
CIRCUMFLEX = 33
|
|||
NOTEQUAL = 29
|
|||
VBAR = 18
|
|||
BACKQUOTE = 25
|
|||
DOUBLESTAR = 36
|
|||
ENDMARKER = 0
|
|||
MINUS = 15
|
|||
DOT = 23
|
|||
RBRACE = 27
|
|||
STAREQUAL = 39
|
|||
GREATEREQUAL = 31
|
|||
MINEQUAL = 38
|
|||
LEFTSHIFTEQUAL = 45
|
|||
SEMI = 13
|
|||
CIRCUMFLEXEQUAL = 44
|
|||
NEWLINE = 4
|
|||
DOUBLESLASHEQUAL = 49
|
|||
COLON = 11
|
|||
PERCENTEQUAL = 41
|
|||
TILDE = 32
|
|||
PLUS = 14
|
|||
ERRORTOKEN = 52
|
|||
RSQB = 10
|
|||
EQEQUAL = 28
|
|||
AMPEREQUAL = 42
|
|||
RIGHTSHIFT = 35
|
|||
STRING = 3
|
|||
NT_OFFSET = 256
|
|||
PERCENT = 24
|
|||
DOUBLESLASH = 48
|
|||
DOUBLESTAREQUAL = 47
|
|||
EQUAL = 22
|
|||
PLUSEQUAL = 37
|
|||
AT = 50
|
|||
AMPER = 19
|
|||
LESSEQUAL = 30
|
|||
LSQB = 9
|
|||
N_TOKENS = 55
|
|||
RIGHTSHIFTEQUAL = 46
|
|||
NAME = 1
|
|||
LBRACE = 26
|
|||
INDENT = 5
|
|||
GREATER = 21
|
|||
tok_name =
|
|||
VBAREQUAL = 43
|
|||
LEFTSHIFT = 34
|
|||
COMMA = 12
|
|||
OP = 51
|
|||
COMMENT = 53
|
|||
NL = 54
|
|||
Whitespace =
|
|||
Comment =
|
|||
Ignore =
|
|||
Name =
|
|||
Hexnumber =
|
|||
Octnumber =
|
|||
Decnumber =
|
|||
Intnumber =
|
|||
Exponent =
|
|||
Pointfloat =
|
|||
Expfloat =
|
|||
Floatnumber =
|
|||
Imagnumber =
|
|||
Number =
|
|||
Single =
|
|||
Double =
|
|||
Single3 =
|
|||
Double3 =
|
|||
Triple =
|
|||
String =
|
|||
Operator =
|
|||
Bracket =
|
|||
Special =
|
|||
Funny =
|
|||
PlainToken =
|
|||
Token =
|
|||
ContStr =
|
|||
PseudoExtras =
|
|||
PseudoToken =
|
|||
endprogs =
|
|||
triple_quoted =
|
|||
single_quoted =
|
|||
tabsize = 8
|
|||
double3prog = re.compile(r'
|
|||
pseudoprog = re.compile(r'
|
|||
single3prog = re.compile(r'
|
|||
t =
|
|||
tokenprog = re.compile(r'
|
|
The tokenize() function accepts two parameters: one representing the input stream, and one providing an output mechanism for tokenize(). The first parameter, readline, must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string. The second parameter, tokeneater, must also be a callable object. It is called once for each token, with five arguments, corresponding to the tuples generated by generate_tokens(). |
Transform tokens back into Python source code. Each element returned by the iterable must be a token sequence with at least two elements, a token number and token value. Round-trip invariant: # Output text will tokenize the back to the input t1 = [tok[:2] for tok in generate_tokens(f.readline)] newcode = untokenize(t1) readline = iter(newcode.splitlines(1)).next t2 = [tok[:2] for tokin generate_tokens(readline)] assert t1 == t2 |
The generate_tokens() generator requires one argment, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string. Alternately, readline can be a callable function terminating with StopIteration: readline = open(myfile).next # Example of alternate readline The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed is the logical line; continuation lines are included. |
|
__credits__
|
tok_name
|
Intnumber
|
Floatnumber
|
Imagnumber
|
Number
|
String
|
Funny
|
PlainToken
|
Token
|
ContStr
|
PseudoExtras
|
PseudoToken
|
endprogs
|
triple_quoted
|
single_quoted
|
double3prog
|
pseudoprog
|
single3prog
|
tokenprog
|
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0 on Tue Jan 29 22:41:29 2008 | http://epydoc.sourceforge.net |