blob: 205407c0202f4d02618e7e0f7584f617f2b3307a [file] [log] [blame]
Fred Drake6b103f11999-02-18 21:06:50 +00001\section{\module{tokenize} ---
2 Tokenizer for Python source}
3
4\declaremodule{standard}{tokenize}
5\modulesynopsis{Lexical scanner for Python source code.}
6\moduleauthor{Ka Ping Yee}{}
7\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
8
9
10The \module{tokenize} module provides a lexical scanner for Python
11source code, implemented in Python. The scanner in this module
12returns comments as tokens as well, making it useful for implementing
13``pretty-printers,'' including colorizers for on-screen displays.
14
Fred Drake16214fb1999-04-23 20:00:53 +000015The scanner is exposed by a single function:
Fred Drake6b103f11999-02-18 21:06:50 +000016
17
18\begin{funcdesc}{tokenize}{readline\optional{, tokeneater}}
19 The \function{tokenize()} function accepts two parameters: one
20 representing the input stream, and one providing an output mechanism
21 for \function{tokenize()}.
22
23 The first parameter, \var{readline}, must be a callable object which
Fred Drake16214fb1999-04-23 20:00:53 +000024 provides the same interface as the \method{readline()} method of
Fred Drake6b103f11999-02-18 21:06:50 +000025 built-in file objects (see section~\ref{bltin-file-objects}). Each
26 call to the function should return one line of input as a string.
27
28 The second parameter, \var{tokeneater}, must also be a callable
29 object. It is called with five parameters: the token type, the
30 token string, a tuple \code{(\var{srow}, \var{scol})} specifying the
31 row and column where the token begins in the source, a tuple
32 \code{(\var{erow}, \var{ecol})} giving the ending position of the
33 token, and the line on which the token was found. The line passed
34 is the \emph{logical} line; continuation lines are included.
35\end{funcdesc}
36
37
38All constants from the \refmodule{token} module are also exported from
Skip Montanaro58177b92001-02-28 22:05:41 +000039\module{tokenize}, as are two additional token type values that might be
Fred Drake6b103f11999-02-18 21:06:50 +000040passed to the \var{tokeneater} function by \function{tokenize()}:
41
42\begin{datadesc}{COMMENT}
43 Token value used to indicate a comment.
44\end{datadesc}
Skip Montanaro58177b92001-02-28 22:05:41 +000045\begin{datadesc}{NL}
Ka-Ping Yeece7298a2001-03-23 05:22:12 +000046 Token value used to indicate a non-terminating newline. The NEWLINE
47 token indicates the end of a logical line of Python code; NL tokens
48 are generated when a logical line of code is continued over multiple
49 physical lines.
Skip Montanaro58177b92001-02-28 22:05:41 +000050\end{datadesc}