Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-tokenize.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-tabnanny.html" />
<link rel="prev" href="module-keyword.html" />
<link rel="parent" href="language.html" />
<link rel="next" href="module-tabnanny.html" />
<meta name='aesop' content='information' />
<title>18.5 tokenize -- Tokenizer for Python source</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.4 keyword "
href="module-keyword.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="18. Python Language Services"
href="language.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="18.6 tabnanny "
href="module-tabnanny.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-keyword.html">18.4 keyword </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="language.html">18. Python Language Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-tabnanny.html">18.6 tabnanny </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0020500000000000000000">
18.5 <tt class="module">tokenize</tt> --
Tokenizer for Python source</A>
</H1>
<P>
<A NAME="module-tokenize"></A>
<P>
The <tt class="module">tokenize</tt> module provides a lexical scanner for Python
source code, implemented in Python. The scanner in this module
returns comments as tokens as well, making it useful for implementing
``pretty-printers,'' including colorizers for on-screen displays.
<P>
The primary entry point is a generator:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4969' xml:id='l2h-4969' class="function">generate_tokens</tt></b>(</nobr></td>
<td><var>readline</var>)</td></tr></table></dt>
<dd>
The <tt class="function">generate_tokens()</tt> generator requires one argment,
<var>readline</var>, which must be a callable object which
provides the same interface as the <tt class="method">readline()</tt> method of
built-in file objects (see section&nbsp;<A href="bltin-file-objects.html#bltin-file-objects">2.3.9</A>). Each
call to the function should return one line of input as a string.
<P>
The generator produces 5-tuples with these members:
the token type;
the token string;
a 2-tuple <code>(<var>srow</var>, <var>scol</var>)</code> of ints specifying the
row and column where the token begins in the source;
a 2-tuple <code>(<var>erow</var>, <var>ecol</var>)</code> 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 <em>logical</em> line;
continuation lines are included.
<span class="versionnote">New in version 2.2.</span>
</dl>
<P>
An older entry point is retained for backward compatibility:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4970' xml:id='l2h-4970' class="function">tokenize</tt></b>(</nobr></td>
<td><var>readline</var><big>[</big><var>, tokeneater</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
The <tt class="function">tokenize()</tt> function accepts two parameters: one
representing the input stream, and one providing an output mechanism
for <tt class="function">tokenize()</tt>.
<P>
The first parameter, <var>readline</var>, must be a callable object which
provides the same interface as the <tt class="method">readline()</tt> method of
built-in file objects (see section&nbsp;<A href="bltin-file-objects.html#bltin-file-objects">2.3.9</A>). Each
call to the function should return one line of input as a string.
<P>
The second parameter, <var>tokeneater</var>, must also be a callable
object. It is called once for each token, with five arguments,
corresponding to the tuples generated by <tt class="function">generate_tokens()</tt>.
</dl>
<P>
All constants from the <tt class="module"><a href="module-token.html">token</a></tt> module are also exported from
<tt class="module">tokenize</tt>, as are two additional token type values that might be
passed to the <var>tokeneater</var> function by <tt class="function">tokenize()</tt>:
<P>
<dl><dt><b><tt id='l2h-4971' xml:id='l2h-4971'>COMMENT</tt></b></dt>
<dd>
Token value used to indicate a comment.
</dd></dl>
<dl><dt><b><tt id='l2h-4972' xml:id='l2h-4972'>NL</tt></b></dt>
<dd>
Token value used to indicate a non-terminating newline. The NEWLINE
token indicates the end of a logical line of Python code; NL tokens
are generated when a logical line of code is continued over multiple
physical lines.
</dd></dl>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.4 keyword "
href="module-keyword.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="18. Python Language Services"
href="language.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="18.6 tabnanny "
href="module-tabnanny.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-keyword.html">18.4 keyword </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="language.html">18. Python Language Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-tabnanny.html">18.6 tabnanny </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>