tokenize
index
/usr/local/lib/python2.3/tokenize.py

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.

 
Modules
       
re
string

 
Classes
       
exceptions.Exception
StopTokenizing
TokenError

 
class StopTokenizing(exceptions.Exception)
     Methods inherited from exceptions.Exception:
__getitem__(...)
__init__(...)
__str__(...)

 
class TokenError(exceptions.Exception)
     Methods inherited from exceptions.Exception:
__getitem__(...)
__init__(...)
__str__(...)

 
Functions
       
any(*choices)
generate_tokens(readline)
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.
 
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.
group(*choices)
maybe(*choices)
printtoken(type, token, (srow, scol), (erow, ecol), line)
tokenize(readline, tokeneater=<function printtoken>)
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().
tokenize_loop(readline, tokeneater)
# backwards compatible interface

 
Data
        AMPER = 19
AMPEREQUAL = 42
BACKQUOTE = 25
Bracket = '[][(){}]'
CIRCUMFLEX = 33
CIRCUMFLEXEQUAL = 44
COLON = 11
COMMA = 12
COMMENT = 52
Comment = r'#[^\r\n]*'
ContStr = '([uU]?[rR]?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*(\'|\\\\\\r?\\n)|[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*("|\\\\\\r?\\n))'
DEDENT = 6
DOT = 23
DOUBLESLASH = 48
DOUBLESLASHEQUAL = 49
DOUBLESTAR = 36
DOUBLESTAREQUAL = 47
Decnumber = r'[1-9]\d*[lL]?'
Double = r'[^"\\]*(?:\\.[^"\\]*)*"'
Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
ENDMARKER = 0
EQEQUAL = 28
EQUAL = 22
ERRORTOKEN = 51
Expfloat = r'\d+[eE][-+]?\d+'
Exponent = r'[eE][-+]?\d+'
Floatnumber = r'((\d+\.\d*|\.\d+)([eE][-+]?\d+)?|\d+[eE][-+]?\d+)'
Funny = r'((\*\*=?|>>=?|<<=?|<>|!=|//=?|[+\-*/%&|^=<>]=?|~)|[][(){}]|(\r?\n|[:;.,`]))'
GREATER = 21
GREATEREQUAL = 31
Hexnumber = r'0[xX][\da-fA-F]*[lL]?'
INDENT = 5
Ignore = r'[ \f\t]*(\\\r?\n[ \f\t]*)*(#[^\r\n]*)?'
Imagnumber = r'(\d+[jJ]|((\d+\.\d*|\.\d+)([eE][-+]?\d+)?|\d+[eE][-+]?\d+)[jJ])'
Intnumber = r'(0[xX][\da-fA-F]*[lL]?|0[0-7]*[lL]?|[1-9]\d*[lL]?)'
LBRACE = 26
LEFTSHIFT = 34
LEFTSHIFTEQUAL = 45
LESS = 20
LESSEQUAL = 30
LPAR = 7
LSQB = 9
MINEQUAL = 38
MINUS = 15
NAME = 1
NEWLINE = 4
NL = 53
NOTEQUAL = 29
NT_OFFSET = 256
NUMBER = 2
N_TOKENS = 54
Name = r'[a-zA-Z_]\w*'
Number = r'((\d+[jJ]|((\d+\.\d*|\.\d+)([eE][-+]?\d+)?|\d+[e...[xX][\da-fA-F]*[lL]?|0[0-7]*[lL]?|[1-9]\d*[lL]?))'
OP = 50
Octnumber = '0[0-7]*[lL]?'
Operator = r'(\*\*=?|>>=?|<<=?|<>|!=|//=?|[+\-*/%&|^=<>]=?|~)'
PERCENT = 24
PERCENTEQUAL = 41
PLUS = 14
PLUSEQUAL = 37
PlainToken = r'(((\d+[jJ]|((\d+\.\d*|\.\d+)([eE][-+]?\d+)?|\d+[...?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*")|[a-zA-Z_]\w*)'
Pointfloat = r'(\d+\.\d*|\.\d+)([eE][-+]?\d+)?'
PseudoExtras = '(\\\\\\r?\\n|#[^\\r\\n]*|([uU]?[rR]?\'\'\'|[uU]?[rR]?"""))'
PseudoToken = '[ \\f\\t]*((\\\\\\r?\\n|#[^\\r\\n]*|([uU]?[rR]?\'\'\'|[uU]?...n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*("|\\\\\\r?\\n))|[a-zA-Z_]\\w*)'
RBRACE = 27
RIGHTSHIFT = 35
RIGHTSHIFTEQUAL = 46
RPAR = 8
RSQB = 10
SEMI = 13
SLASH = 17
SLASHEQUAL = 40
STAR = 16
STAREQUAL = 39
STRING = 3
Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
Special = r'(\r?\n|[:;.,`])'
String = '([uU]?[rR]?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*\'|[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*")'
TILDE = 32
Token = r'[ \f\t]*(\\\r?\n[ \f\t]*)*(#[^\r\n]*)?(((\d+[jJ]...?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*")|[a-zA-Z_]\w*)'
Triple = '([uU]?[rR]?\'\'\'|[uU]?[rR]?""")'
VBAR = 18
VBAREQUAL = 43
Whitespace = r'[ \f\t]*'
__all__ = ['AMPER', 'AMPEREQUAL', 'BACKQUOTE', 'CIRCUMFLEX', 'CIRCUMFLEXEQUAL', 'COLON', 'COMMA', 'DEDENT', 'DOT', 'DOUBLESLASH', 'DOUBLESLASHEQUAL', 'DOUBLESTAR', 'DOUBLESTAREQUAL', 'ENDMARKER', 'EQEQUAL', 'EQUAL', 'ERRORTOKEN', 'GREATER', 'GREATEREQUAL', 'INDENT', ...]
__author__ = 'Ka-Ping Yee <ping@lfw.org>'
__credits__ = 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'
double3prog = <_sre.SRE_Pattern object>
endprogs = {'"': <_sre.SRE_Pattern object>, '"""': <_sre.SRE_Pattern object>, "'": <_sre.SRE_Pattern object>, "'''": <_sre.SRE_Pattern object>, 'R': None, 'R"""': <_sre.SRE_Pattern object>, "R'''": <_sre.SRE_Pattern object>, 'U': None, 'U"""': <_sre.SRE_Pattern object>, "U'''": <_sre.SRE_Pattern object>, ...}
pseudoprog = <_sre.SRE_Pattern object>
single3prog = <_sre.SRE_Pattern object>
single_quoted = {'"': '"', "'": "'", 'R"': 'R"', "R'": "R'", 'U"': 'U"', "U'": "U'", 'UR"': 'UR"', "UR'": "UR'", 'Ur"': 'Ur"', "Ur'": "Ur'", ...}
t = 'UR"'
tabsize = 8
tok_name = {0: 'ENDMARKER', 1: 'NAME', 2: 'NUMBER', 3: 'STRING', 4: 'NEWLINE', 5: 'INDENT', 6: 'DEDENT', 7: 'LPAR', 8: 'RPAR', 9: 'LSQB', ...}
tokenprog = <_sre.SRE_Pattern object>
triple_quoted = {'"""': '"""', "'''": "'''", 'R"""': 'R"""', "R'''": "R'''", 'U"""': 'U"""', "U'''": "U'''", 'UR"""': 'UR"""', "UR'''": "UR'''", 'Ur"""': 'Ur"""', "Ur'''": "Ur'''", ...}

 
Author
        Ka-Ping Yee <ping@lfw.org>

 
Credits
        GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro