Module tokenize
[hide private]
[frames] | no frames]

Module tokenize

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>

Classes [hide private]
TokenError
StopTokenizing
Functions [hide private]
 
ISTERMINAL(x)
 
main()
 
ISEOF(x)
 
ISNONTERMINAL(x)
 
group(*choices)
 
any(*choices)
 
maybe(*choices)
 
printtoken(type, token, (srow, scol), (erow, ecol), line)
 
tokenize(readline, tokeneater=<function printtoken at 0x40613aac>)
The tokenize() function accepts two parameters: one representing the input stream, and one providing an output mechanism for tokenize().
 
tokenize_loop(readline, tokeneater)
 
untokenize(iterable)
Transform tokens back into Python source code.
 
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.
Variables [hide private]
  __credits__ = 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drak...
  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 = {0: 'ENDMARKER', 1: 'NAME', 2: 'NUMBER', 3: 'STRING...
  VBAREQUAL = 43
  LEFTSHIFT = 34
  COMMA = 12
  OP = 51
  COMMENT = 53
  NL = 54
  Whitespace = '[ \\f\\t]*'
  Comment = '#[^\\r\\n]*'
  Ignore = '[ \\f\\t]*(\\\\\\r?\\n[ \\f\\t]*)*(#[^\\r\\n]*)?'
  Name = '[a-zA-Z_]\\w*'
  Hexnumber = '0[xX][\\da-fA-F]*[lL]?'
  Octnumber = '0[0-7]*[lL]?'
  Decnumber = '[1-9]\\d*[lL]?'
  Intnumber = '(0[xX][\\da-fA-F]*[lL]?|0[0-7]*[lL]?|[1-9]\\d*[lL...
  Exponent = '[eE][-+]?\\d+'
  Pointfloat = '(\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?'
  Expfloat = '\\d+[eE][-+]?\\d+'
  Floatnumber = '((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?|\\d+[eE]...
  Imagnumber = '(\\d+[jJ]|((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?...
  Number = '((\\d+[jJ]|((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?|\\...
  Single = '[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''
  Double = '[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'
  Single3 = '[^\'\\\\]*(?:(?:\\\\.|\'(?!\'\'))[^\'\\\\]*)*\'\'\''
  Double3 = '[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""'
  Triple = '([uU]?[rR]?\'\'\'|[uU]?[rR]?""")'
  String = '([uU]?[rR]?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*\'|...
  Operator = '(\\*\\*=?|>>=?|<<=?|<>|!=|//=?|[+\\-*/%&|^=<>]=?|~)'
  Bracket = '[][(){}]'
  Special = '(\\r?\\n|[:;.,`@])'
  Funny = '((\\*\\*=?|>>=?|<<=?|<>|!=|//=?|[+\\-*/%&|^=<>]=?|~)|...
  PlainToken = '(((\\d+[jJ]|((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+...
  Token = '[ \\f\\t]*(\\\\\\r?\\n[ \\f\\t]*)*(#[^\\r\\n]*)?(((\\...
  ContStr = '([uU]?[rR]?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*(\...
  PseudoExtras = '(\\\\\\r?\\n|#[^\\r\\n]*|([uU]?[rR]?\'\'\'|[uU...
  PseudoToken = '[ \\f\\t]*((\\\\\\r?\\n|#[^\\r\\n]*|([uU]?[rR]?...
  endprogs = {'"': re.compile(r'[^"\\]*(?:\\.[^"\\]*)*"'), '"""'...
  triple_quoted = {'"""': '"""', '\'\'\'': '\'\'\'', 'R"""': 'R"...
  single_quoted = {'"': '"', '\'': '\'', 'R"': 'R"', 'R\'': 'R\'...
  tabsize = 8
  double3prog = re.compile(r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"...
  pseudoprog = re.compile(r'[ \f\t]*((\\\r?\n|#[^\r\n]*|([uU]?[r...
  single3prog = re.compile(r'[^\'\\]*(?:(?:\\.|\'(?!\'\'))[^\'\\...
  t = 'UR"'
  tokenprog = re.compile(r'[ \f\t]*(\\\r?\n[ \f\t]*)*(#[^\r\n]*)...

Imports: string, re


Function Details [hide private]

tokenize(readline, tokeneater=<function printtoken at 0x40613aac>)

 

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.

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

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.  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.


Variables Details [hide private]

__credits__

Value:
'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'

tok_name

Value:
{0: 'ENDMARKER',
 1: 'NAME',
 2: 'NUMBER',
 3: 'STRING',
 4: 'NEWLINE',
 5: 'INDENT',
 6: 'DEDENT',
 7: 'LPAR',
...

Intnumber

Value:
'(0[xX][\\da-fA-F]*[lL]?|0[0-7]*[lL]?|[1-9]\\d*[lL]?)'

Floatnumber

Value:
'((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?|\\d+[eE][-+]?\\d+)'

Imagnumber

Value:
'(\\d+[jJ]|((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?|\\d+[eE][-+]?\\d+)[j\
J])'

Number

Value:
'((\\d+[jJ]|((\\d+\\.\\d*|\\.\\d+)([e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[][(){}]|(\\r?\\n|[:;.,`@]))|([uU]?[rR]?\'[^\\n\'\\\\]*(?:\\\\
\.[^\\n\'\\\\]*)*\'|[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*")|[a\
-zA-Z_]\\w*)'

ContStr

Value:
'([uU]?[rR]?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*(\'|\\\\\\r?\\n)|[uU\
]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*("|\\\\\\r?\\n))'

PseudoExtras

Value:
'(\\\\\\r?\\n|#[^\\r\\n]*|([uU]?[rR]?\'\'\'|[uU]?[rR]?"""))'

PseudoToken

Value:
'[ \\f\\t]*((\\\\\\r?\\n|#[^\\r\\n]*|([uU]?[rR]?\'\'\'|[uU]?[rR]?"""))\
|((\\d+[jJ]|((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?|\\d+[eE][-+]?\\d+)[\
jJ])|((\\d+\\.\\d*|\\.\\d+)([eE][-+]?\\d+)?|\\d+[eE][-+]?\\d+)|(0[xX][\
\\da-fA-F]*[lL]?|0[0-7]*[lL]?|[1-9]\\d*[lL]?))|((\\*\\*=?|>>=?|<<=?|<>\
|!=|//=?|[+\\-*/%&|^=<>]=?|~)|[][(){}]|(\\r?\\n|[:;.,`@]))|([uU]?[rR]?\
\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*(\'|\\\\\\r?\\n)|[uU]?[rR]?"[^\\\
n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*("|\\\\\\r?\\n))|[a-zA-Z_]\\w*)'

endprogs

Value:
{'"': re.compile(r'[^"\\]*(?:\\.[^"\\]*)*"'),
 '"""': re.compile(r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'),
 '\'': re.compile(r'[^\'\\]*(?:\\.[^\'\\]*)*\''),
 '\'\'\'': re.compile(r'[^\'\\]*(?:(?:\\.|\'(?!\'\'))[^\'\\]*)*\'\'\''\
),
 'R': None,
 'R"""': re.compile(r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'),
 'R\'\'\'': re.compile(r'[^\'\\]*(?:(?:\\.|\'(?!\'\'))[^\'\\]*)*\'\'\'\
...

triple_quoted

Value:
{'"""': '"""',
 '\'\'\'': '\'\'\'',
 'R"""': 'R"""',
 'R\'\'\'': 'R\'\'\'',
 'U"""': 'U"""',
 'U\'\'\'': 'U\'\'\'',
 'UR"""': 'UR"""',
 'UR\'\'\'': 'UR\'\'\'',
...

single_quoted

Value:
{'"': '"',
 '\'': '\'',
 'R"': 'R"',
 'R\'': 'R\'',
 'U"': 'U"',
 'U\'': 'U\'',
 'UR"': 'UR"',
 'UR\'': 'UR\'',
...

double3prog

Value:
re.compile(r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""')

pseudoprog

Value:
re.compile(r'[ \f\t]*((\\\r?\n|#[^\r\n]*|([uU]?[rR]?\'\'\'|[uU]?[rR]?"\
""))|((\d+[jJ]|((\d+\.\d*|\.\d+)([eE][-\+]?\d+)?|\d+[eE][-\+]?\d+)[jJ]\
)|((\d+\.\d*|\.\d+)([eE][-\+]?\d+)?|\d+[eE][-\+]?\d+)|(0[xX][\da-fA-F]\
*[lL]?|0[0-7]*[lL]?|[1-9]\d*[lL]?))|((\*\*=?|>>=?|<<=?|<>|!=|//=?|[\+-\
\*/%&\|\^=<>]=?|~)|[\]\[\(\)\{\}]|(\r?\n|[:;\.,`@]))|([uU]?[rR]?\'[^\n\
\'\\]*(?:\\.[^\n\'\\]*)*(\'|\\\r?\n)|[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\\
]*)*("|\\\r?\n))|[a-zA-Z_]\w*)')

single3prog

Value:
re.compile(r'[^\'\\]*(?:(?:\\.|\'(?!\'\'))[^\'\\]*)*\'\'\'')

tokenprog

Value:
re.compile(r'[ \f\t]*(\\\r?\n[ \f\t]*)*(#[^\r\n]*)?(((\d+[jJ]|((\d+\.\\
d*|\.\d+)([eE][-\+]?\d+)?|\d+[eE][-\+]?\d+)[jJ])|((\d+\.\d*|\.\d+)([eE\
][-\+]?\d+)?|\d+[eE][-\+]?\d+)|(0[xX][\da-fA-F]*[lL]?|0[0-7]*[lL]?|[1-\
9]\d*[lL]?))|((\*\*=?|>>=?|<<=?|<>|!=|//=?|[\+-\*/%&\|\^=<>]=?|~)|[\]\\
[\(\)\{\}]|(\r?\n|[:;\.,`@]))|([uU]?[rR]?\'[^\n\'\\]*(?:\\.[^\n\'\\]*)\
*\'|[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*")|[a-zA-Z_]\w*)')