tokenize
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tokenize.py
Module Docs

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

 
Functions
       
ISEOF(x)
ISNONTERMINAL(x)
ISTERMINAL(x)
generate_tokens(readline)
The generate_tokens() generator requires one argument, 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.
main()
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().
untokenize(iterable)
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.  If
only two tokens are passed, the resulting output is poor.
 
Round-trip invariant for full input:
    Untokenized source will match input source exactly
 
Round-trip invariant for limited intput:
    # 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 tok in generate_tokens(readline)]
    assert t1 == t2

 
Data
        AMPER = 19
AMPEREQUAL = 42
AT = 50
BACKQUOTE = 25
CIRCUMFLEX = 33
CIRCUMFLEXEQUAL = 44
COLON = 11
COMMA = 12
COMMENT = 53
DEDENT = 6
DOT = 23
DOUBLESLASH = 48
DOUBLESLASHEQUAL = 49
DOUBLESTAR = 36
DOUBLESTAREQUAL = 47
ENDMARKER = 0
EQEQUAL = 28
EQUAL = 22
ERRORTOKEN = 52
GREATER = 21
GREATEREQUAL = 31
INDENT = 5
LBRACE = 26
LEFTSHIFT = 34
LEFTSHIFTEQUAL = 45
LESS = 20
LESSEQUAL = 30
LPAR = 7
LSQB = 9
MINEQUAL = 38
MINUS = 15
NAME = 1
NEWLINE = 4
NL = 54
NOTEQUAL = 29
NT_OFFSET = 256
NUMBER = 2
N_TOKENS = 55
OP = 51
PERCENT = 24
PERCENTEQUAL = 41
PLUS = 14
PLUSEQUAL = 37
RBRACE = 27
RIGHTSHIFT = 35
RIGHTSHIFTEQUAL = 46
RPAR = 8
RSQB = 10
SEMI = 13
SLASH = 17
SLASHEQUAL = 40
STAR = 16
STAREQUAL = 39
STRING = 3
TILDE = 32
VBAR = 18
VBAREQUAL = 43
__all__ = ['AMPER', 'AMPEREQUAL', 'AT', 'BACKQUOTE', 'CIRCUMFLEX', 'CIRCUMFLEXEQUAL', 'COLON', 'COMMA', 'DEDENT', 'DOT', 'DOUBLESLASH', 'DOUBLESLASHEQUAL', 'DOUBLESTAR', 'DOUBLESTAREQUAL', 'ENDMARKER', 'EQEQUAL', 'EQUAL', 'ERRORTOKEN', 'GREATER', 'GREATEREQUAL', ...]
__author__ = 'Ka-Ping Yee <ping@lfw.org>'
__credits__ = 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro, Raymond Hettinger'
tok_name = {0: 'ENDMARKER', 1: 'NAME', 2: 'NUMBER', 3: 'STRING', 4: 'NEWLINE', 5: 'INDENT', 6: 'DEDENT', 7: 'LPAR', 8: 'RPAR', 9: 'LSQB', ...}

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

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