Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 1 | \section{\module{shlex} --- |
Fred Drake | 184e836 | 1999-05-11 15:14:15 +0000 | [diff] [blame] | 2 | Simple lexical analysis} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{standard}{shlex} |
Fred Drake | c116b82 | 2001-05-09 15:50:17 +0000 | [diff] [blame] | 5 | \modulesynopsis{Simple lexical analysis for \UNIX\ shell-like languages.} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 6 | \moduleauthor{Eric S. Raymond}{esr@snark.thyrsus.com} |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 7 | \moduleauthor{Gustavo Niemeyer}{niemeyer@conectiva.com} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 8 | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com} |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 9 | \sectionauthor{Gustavo Niemeyer}{niemeyer@conectiva.com} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 10 | |
Fred Drake | 292b9eb | 1998-12-22 18:40:50 +0000 | [diff] [blame] | 11 | \versionadded{1.5.2} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 12 | |
| 13 | The \class{shlex} class makes it easy to write lexical analyzers for |
| 14 | simple syntaxes resembling that of the \UNIX{} shell. This will often |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 15 | be useful for writing minilanguages, (e.g. in run control files for |
| 16 | Python applications) or for parsing quoted strings. |
Fred Drake | 184e836 | 1999-05-11 15:14:15 +0000 | [diff] [blame] | 17 | |
| 18 | \begin{seealso} |
| 19 | \seemodule{ConfigParser}{Parser for configuration files similar to the |
| 20 | Windows \file{.ini} files.} |
| 21 | \end{seealso} |
| 22 | |
| 23 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 24 | \subsection{Module Contents} |
| 25 | |
| 26 | The \module{shlex} module defines the following functions: |
| 27 | |
| 28 | \begin{funcdesc}{split}{s\optional{, posix=\code{True}\optional{, |
| 29 | spaces=\code{True}}}} |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 30 | Split the string \var{s} using shell-like syntax. If \var{posix} is |
| 31 | \code{True}, operate in \POSIX{} mode. If \var{spaces} is \code{True}, it |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 32 | will only split words in whitespaces (setting the |
| 33 | \member{whitespace_split} member of the \class{shlex} instance). |
| 34 | \versionadded{2.3} |
| 35 | \end{funcdesc} |
| 36 | |
| 37 | The \module{shlex} module defines the following classes: |
| 38 | |
| 39 | \begin{classdesc}{shlex}{\optional{instream=\code{sys.stdin}\optional{, |
| 40 | infile=\code{None}\optional{, |
| 41 | posix=\code{False}}}}} |
| 42 | A \class{shlex} instance or subclass instance is a lexical analyzer |
| 43 | object. The initialization argument, if present, specifies where to |
| 44 | read characters from. It must be a file-/stream-like object with |
| 45 | \method{read()} and \method{readline()} methods, or a string (strings |
| 46 | are accepted since Python 2.3). If no argument is given, input will be |
| 47 | taken from \code{sys.stdin}. The second optional argument is a filename |
| 48 | string, which sets the initial value of the \member{infile} member. If |
| 49 | the \var{instream} argument is omitted or equal to \code{sys.stdin}, |
| 50 | this second argument defaults to ``stdin''. The \var{posix} argument |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 51 | was introduced in Python 2.3, and defines the operational mode. When |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 52 | \var{posix} is not true (default), the \class{shlex} instance will |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 53 | operate in compatibility mode. When operating in \POSIX{} mode, |
| 54 | \class{shlex} will try to be as close as possible to the \POSIX{} shell |
| 55 | parsing rules. See~\ref{shlex-objects}. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 56 | \end{classdesc} |
| 57 | |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 58 | \subsection{shlex Objects \label{shlex-objects}} |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 59 | |
| 60 | A \class{shlex} instance has the following methods: |
| 61 | |
| 62 | \begin{methoddesc}{get_token}{} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 63 | Return a token. If tokens have been stacked using |
| 64 | \method{push_token()}, pop a token off the stack. Otherwise, read one |
| 65 | from the input stream. If reading encounters an immediate |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 66 | end-of-file, \member{self.eof} is returned (the empty string (\code{''}) |
| 67 | in non-\POSIX{} mode, and \code{None} in \POSIX{} mode). |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 68 | \end{methoddesc} |
| 69 | |
| 70 | \begin{methoddesc}{push_token}{str} |
| 71 | Push the argument onto the token stack. |
| 72 | \end{methoddesc} |
| 73 | |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 74 | \begin{methoddesc}{read_token}{} |
| 75 | Read a raw token. Ignore the pushback stack, and do not interpret source |
| 76 | requests. (This is not ordinarily a useful entry point, and is |
| 77 | documented here only for the sake of completeness.) |
| 78 | \end{methoddesc} |
| 79 | |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 80 | \begin{methoddesc}{sourcehook}{filename} |
| 81 | When \class{shlex} detects a source request (see |
| 82 | \member{source} below) this method is given the following token as |
| 83 | argument, and expected to return a tuple consisting of a filename and |
| 84 | an open file-like object. |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 85 | |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 86 | Normally, this method first strips any quotes off the argument. If |
| 87 | the result is an absolute pathname, or there was no previous source |
| 88 | request in effect, or the previous source was a stream |
| 89 | (e.g. \code{sys.stdin}), the result is left alone. Otherwise, if the |
| 90 | result is a relative pathname, the directory part of the name of the |
| 91 | file immediately before it on the source inclusion stack is prepended |
| 92 | (this behavior is like the way the C preprocessor handles |
Eric S. Raymond | bd1a489 | 2001-01-16 14:18:55 +0000 | [diff] [blame] | 93 | \code{\#include "file.h"}). |
| 94 | |
| 95 | The result of the manipulations is treated as a filename, and returned |
| 96 | as the first component of the tuple, with |
| 97 | \function{open()} called on it to yield the second component. (Note: |
| 98 | this is the reverse of the order of arguments in instance initialization!) |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 99 | |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 100 | This hook is exposed so that you can use it to implement directory |
| 101 | search paths, addition of file extensions, and other namespace hacks. |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 102 | There is no corresponding `close' hook, but a shlex instance will call |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 103 | the \method{close()} method of the sourced input stream when it |
| 104 | returns \EOF. |
Eric S. Raymond | bd1a489 | 2001-01-16 14:18:55 +0000 | [diff] [blame] | 105 | |
Fred Drake | 25be193 | 2001-01-16 20:52:41 +0000 | [diff] [blame] | 106 | For more explicit control of source stacking, use the |
| 107 | \method{push_source()} and \method{pop_source()} methods. |
Eric S. Raymond | bd1a489 | 2001-01-16 14:18:55 +0000 | [diff] [blame] | 108 | \end{methoddesc} |
| 109 | |
| 110 | \begin{methoddesc}{push_source}{stream\optional{, filename}} |
| 111 | Push an input source stream onto the input stack. If the filename |
| 112 | argument is specified it will later be available for use in error |
| 113 | messages. This is the same method used internally by the |
Fred Drake | 25be193 | 2001-01-16 20:52:41 +0000 | [diff] [blame] | 114 | \method{sourcehook} method. |
| 115 | \versionadded{2.1} |
Eric S. Raymond | bd1a489 | 2001-01-16 14:18:55 +0000 | [diff] [blame] | 116 | \end{methoddesc} |
| 117 | |
Fred Drake | 25be193 | 2001-01-16 20:52:41 +0000 | [diff] [blame] | 118 | \begin{methoddesc}{pop_source}{} |
Eric S. Raymond | bd1a489 | 2001-01-16 14:18:55 +0000 | [diff] [blame] | 119 | Pop the last-pushed input source from the input stack. |
| 120 | This is the same method used internally when the lexer reaches |
Fred Drake | 25be193 | 2001-01-16 20:52:41 +0000 | [diff] [blame] | 121 | \EOF on a stacked input stream. |
| 122 | \versionadded{2.1} |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 123 | \end{methoddesc} |
| 124 | |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 125 | \begin{methoddesc}{error_leader}{\optional{file\optional{, line}}} |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 126 | This method generates an error message leader in the format of a |
Fred Drake | 25be193 | 2001-01-16 20:52:41 +0000 | [diff] [blame] | 127 | \UNIX{} C compiler error label; the format is \code{'"\%s", line \%d: '}, |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 128 | where the \samp{\%s} is replaced with the name of the current source |
| 129 | file and the \samp{\%d} with the current input line number (the |
| 130 | optional arguments can be used to override these). |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 131 | |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 132 | This convenience is provided to encourage \module{shlex} users to |
| 133 | generate error messages in the standard, parseable format understood |
| 134 | by Emacs and other \UNIX{} tools. |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 135 | \end{methoddesc} |
| 136 | |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 137 | Instances of \class{shlex} subclasses have some public instance |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 138 | variables which either control lexical analysis or can be used for |
| 139 | debugging: |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 140 | |
| 141 | \begin{memberdesc}{commenters} |
| 142 | The string of characters that are recognized as comment beginners. |
| 143 | All characters from the comment beginner to end of line are ignored. |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 144 | Includes just \character{\#} by default. |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 145 | \end{memberdesc} |
| 146 | |
| 147 | \begin{memberdesc}{wordchars} |
| 148 | The string of characters that will accumulate into multi-character |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 149 | tokens. By default, includes all \ASCII{} alphanumerics and |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 150 | underscore. |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 151 | \end{memberdesc} |
| 152 | |
| 153 | \begin{memberdesc}{whitespace} |
| 154 | Characters that will be considered whitespace and skipped. Whitespace |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 155 | bounds tokens. By default, includes space, tab, linefeed and |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 156 | carriage-return. |
| 157 | \end{memberdesc} |
| 158 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 159 | \begin{memberdesc}{escape} |
| 160 | Characters that will be considered as escape. This will be only used |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 161 | in \POSIX{} mode, and includes just \character{\textbackslash} by default. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 162 | \versionadded{2.3} |
| 163 | \end{memberdesc} |
| 164 | |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 165 | \begin{memberdesc}{quotes} |
| 166 | Characters that will be considered string quotes. The token |
| 167 | accumulates until the same quote is encountered again (thus, different |
Fred Drake | 184e836 | 1999-05-11 15:14:15 +0000 | [diff] [blame] | 168 | quote types protect each other as in the shell.) By default, includes |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 169 | \ASCII{} single and double quotes. |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 170 | \end{memberdesc} |
| 171 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 172 | \begin{memberdesc}{escapedquotes} |
| 173 | Characters in \member{quotes} that will interpret escape characters |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 174 | defined in \member{escape}. This is only used in \POSIX{} mode, and |
| 175 | includes just \character{"} by default. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 176 | \versionadded{2.3} |
| 177 | \end{memberdesc} |
| 178 | |
| 179 | \begin{memberdesc}{whitespace_split} |
Neal Norwitz | 10cf218 | 2003-04-17 23:09:08 +0000 | [diff] [blame^] | 180 | If \code{True}, tokens will only be split in whitespaces. This is useful, for |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 181 | example, for parsing command lines with \class{shlex}, getting tokens |
| 182 | in a similar way to shell arguments. |
| 183 | \versionadded{2.3} |
| 184 | \end{memberdesc} |
| 185 | |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 186 | \begin{memberdesc}{infile} |
| 187 | The name of the current input file, as initially set at class |
| 188 | instantiation time or stacked by later source requests. It may |
| 189 | be useful to examine this when constructing error messages. |
| 190 | \end{memberdesc} |
| 191 | |
| 192 | \begin{memberdesc}{instream} |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 193 | The input stream from which this \class{shlex} instance is reading |
| 194 | characters. |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 195 | \end{memberdesc} |
| 196 | |
| 197 | \begin{memberdesc}{source} |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 198 | This member is \code{None} by default. If you assign a string to it, |
| 199 | that string will be recognized as a lexical-level inclusion request |
| 200 | similar to the \samp{source} keyword in various shells. That is, the |
| 201 | immediately following token will opened as a filename and input taken |
| 202 | from that stream until \EOF, at which point the \method{close()} |
| 203 | method of that stream will be called and the input source will again |
| 204 | become the original input stream. Source requests may be stacked any |
| 205 | number of levels deep. |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 206 | \end{memberdesc} |
| 207 | |
| 208 | \begin{memberdesc}{debug} |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 209 | If this member is numeric and \code{1} or more, a \class{shlex} |
| 210 | instance will print verbose progress output on its behavior. If you |
| 211 | need to use this, you can read the module source code to learn the |
| 212 | details. |
Guido van Rossum | d67ddbb | 2000-05-01 20:14:47 +0000 | [diff] [blame] | 213 | \end{memberdesc} |
| 214 | |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 215 | \begin{memberdesc}{lineno} |
| 216 | Source line number (count of newlines seen so far plus one). |
| 217 | \end{memberdesc} |
| 218 | |
| 219 | \begin{memberdesc}{token} |
Fred Drake | 1189fa9 | 1998-12-22 18:24:13 +0000 | [diff] [blame] | 220 | The token buffer. It may be useful to examine this when catching |
| 221 | exceptions. |
Guido van Rossum | 5e97c9d | 1998-12-22 05:18:24 +0000 | [diff] [blame] | 222 | \end{memberdesc} |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 223 | |
| 224 | \begin{memberdesc}{eof} |
| 225 | Token used to determine end of file. This will be set to the empty |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 226 | string (\code{''}), in non-\POSIX{} mode, and to \code{None} in |
| 227 | \POSIX{} mode. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 228 | \versionadded{2.3} |
| 229 | \end{memberdesc} |
| 230 | |
| 231 | \subsection{Parsing Rules\label{shlex-parsing-rules}} |
| 232 | |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 233 | When operating in non-\POSIX{} mode, \class{shlex} will try to obey to |
| 234 | the following rules. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 235 | |
| 236 | \begin{itemize} |
| 237 | \item Quote characters are not recognized within words |
| 238 | (\code{Do"Not"Separate} is parsed as the single word |
| 239 | \code{Do"Not"Separate}); |
| 240 | \item Escape characters are not recognized; |
| 241 | \item Enclosing characters in quotes preserve the literal value of |
| 242 | all characters within the quotes; |
| 243 | \item Closing quotes separate words (\code{"Do"Separate} is parsed |
| 244 | as \code{"Do"} and \code{Separate}); |
| 245 | \item If \member{whitespace_split} is \code{False}, any character not |
| 246 | declared to be a word character, whitespace, or a quote will be |
| 247 | returned as a single-character token. If it is \code{True}, |
| 248 | \class{shlex} will only split words in whitespaces; |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 249 | \item EOF is signaled with an empty string (\code{''}); |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 250 | \item It's not possible to parse empty strings, even if quoted. |
| 251 | \end{itemize} |
| 252 | |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 253 | When operating in \POSIX{} mode, \class{shlex} will try to obey to the |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 254 | following parsing rules. |
| 255 | |
| 256 | \begin{itemize} |
| 257 | \item Quotes are stripped out, and do not separate words |
| 258 | (\code{"Do"Not"Separate"} is parsed as the single word |
| 259 | \code{DoNotSeparate}); |
| 260 | \item Non-quoted escape characters (e.g. \character{\textbackslash}) |
| 261 | preserve the literal value of the next character that follows; |
| 262 | \item Enclosing characters in quotes which are not part of |
| 263 | \member{escapedquotes} (e.g. \character{'}) preserve the literal |
| 264 | value of all characters within the quotes; |
| 265 | \item Enclosing characters in quotes which are part of |
| 266 | \member{escapedquotes} (e.g. \character{"}) preserves the literal |
| 267 | value of all characters within the quotes, with the exception of |
| 268 | the characters mentioned in \member{escape}. The escape characters |
| 269 | retain its special meaning only when followed by the quote in use, |
| 270 | or the escape character itself. Otherwise the escape character |
| 271 | will be considered a normal character. |
| 272 | \item EOF is signaled with a \code{None} value; |
Fred Drake | aa3b5d2 | 2003-04-17 21:49:04 +0000 | [diff] [blame] | 273 | \item Quoted empty strings (\code{''}) are allowed; |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 274 | \end{itemize} |
| 275 | |