Fred Drake | 6b103f1 | 1999-02-18 21:06:50 +0000 | [diff] [blame] | 1 | \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 | |
| 10 | The \module{tokenize} module provides a lexical scanner for Python |
| 11 | source code, implemented in Python. The scanner in this module |
| 12 | returns comments as tokens as well, making it useful for implementing |
| 13 | ``pretty-printers,'' including colorizers for on-screen displays. |
| 14 | |
Fred Drake | 16214fb | 1999-04-23 20:00:53 +0000 | [diff] [blame] | 15 | The scanner is exposed by a single function: |
Fred Drake | 6b103f1 | 1999-02-18 21:06:50 +0000 | [diff] [blame] | 16 | |
| 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 Drake | 16214fb | 1999-04-23 20:00:53 +0000 | [diff] [blame] | 24 | provides the same interface as the \method{readline()} method of |
Fred Drake | 6b103f1 | 1999-02-18 21:06:50 +0000 | [diff] [blame] | 25 | 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 | |
| 38 | All constants from the \refmodule{token} module are also exported from |
Skip Montanaro | 58177b9 | 2001-02-28 22:05:41 +0000 | [diff] [blame] | 39 | \module{tokenize}, as are two additional token type values that might be |
Fred Drake | 6b103f1 | 1999-02-18 21:06:50 +0000 | [diff] [blame] | 40 | passed 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 Montanaro | 58177b9 | 2001-02-28 22:05:41 +0000 | [diff] [blame] | 45 | \begin{datadesc}{NL} |
Ka-Ping Yee | ce7298a | 2001-03-23 05:22:12 +0000 | [diff] [blame] | 46 | 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 Montanaro | 58177b9 | 2001-02-28 22:05:41 +0000 | [diff] [blame] | 50 | \end{datadesc} |