Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """A lexical analyzer class for simple shell-like syntaxes.""" |
| 2 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 3 | # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 4 | # Input stacking and error message cleanup added by ESR, March 2000 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 5 | # push_source() and pop_source() made explicit by ESR, January 2001. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 6 | # Posix compliance, split(), string arguments, and |
| 7 | # iterator interface by Gustavo Niemeyer, April 2003. |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 8 | |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 9 | import os |
| 10 | import re |
Guido van Rossum | 73898c7 | 1999-05-03 18:14:16 +0000 | [diff] [blame] | 11 | import sys |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 12 | from collections import deque |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 68937b4 | 2007-05-18 00:51:22 +0000 | [diff] [blame] | 14 | from io import StringIO |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 15 | |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 16 | __all__ = ["shlex", "split", "quote"] |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 18 | class shlex: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 19 | "A lexical analyzer class for simple shell-like syntaxes." |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 20 | def __init__(self, instream=None, infile=None, posix=False): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 21 | if isinstance(instream, str): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 22 | instream = StringIO(instream) |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 23 | if instream is not None: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 24 | self.instream = instream |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 25 | self.infile = infile |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 26 | else: |
| 27 | self.instream = sys.stdin |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 28 | self.infile = None |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 29 | self.posix = posix |
| 30 | if posix: |
| 31 | self.eof = None |
| 32 | else: |
| 33 | self.eof = '' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 34 | self.commenters = '#' |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 35 | self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' |
| 36 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 37 | if self.posix: |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 38 | self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' |
| 39 | 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ') |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 40 | self.whitespace = ' \t\r\n' |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 41 | self.whitespace_split = False |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 42 | self.quotes = '\'"' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 43 | self.escape = '\\' |
| 44 | self.escapedquotes = '"' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 45 | self.state = ' ' |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 46 | self.pushback = deque() |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 47 | self.lineno = 1 |
| 48 | self.debug = 0 |
| 49 | self.token = '' |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 50 | self.filestack = deque() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 51 | self.source = None |
| 52 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 53 | print('shlex: reading from %s, line %d' \ |
| 54 | % (self.instream, self.lineno)) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 55 | |
| 56 | def push_token(self, tok): |
| 57 | "Push a token onto the stack popped by the get_token method" |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 58 | if self.debug >= 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 59 | print("shlex: pushing token " + repr(tok)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 60 | self.pushback.appendleft(tok) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 61 | |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 62 | def push_source(self, newstream, newfile=None): |
| 63 | "Push an input source onto the lexer's input source stack." |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 64 | if isinstance(newstream, str): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 65 | newstream = StringIO(newstream) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 66 | self.filestack.appendleft((self.infile, self.instream, self.lineno)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 67 | self.infile = newfile |
| 68 | self.instream = newstream |
| 69 | self.lineno = 1 |
| 70 | if self.debug: |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 71 | if newfile is not None: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 72 | print('shlex: pushing to file %s' % (self.infile,)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 73 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 74 | print('shlex: pushing to stream %s' % (self.instream,)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 75 | |
| 76 | def pop_source(self): |
| 77 | "Pop the input source stack." |
| 78 | self.instream.close() |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 79 | (self.infile, self.instream, self.lineno) = self.filestack.popleft() |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 80 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 81 | print('shlex: popping to %s, line %d' \ |
| 82 | % (self.instream, self.lineno)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 83 | self.state = ' ' |
| 84 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 85 | def get_token(self): |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 86 | "Get a token from the input stream (or from stack if it's nonempty)" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 87 | if self.pushback: |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 88 | tok = self.pushback.popleft() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 89 | if self.debug >= 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 90 | print("shlex: popping token " + repr(tok)) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 91 | return tok |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 92 | # No pushback. Get a token. |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 93 | raw = self.read_token() |
| 94 | # Handle inclusions |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 95 | if self.source is not None: |
| 96 | while raw == self.source: |
| 97 | spec = self.sourcehook(self.read_token()) |
| 98 | if spec: |
| 99 | (newfile, newstream) = spec |
| 100 | self.push_source(newstream, newfile) |
| 101 | raw = self.get_token() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 102 | # Maybe we got EOF instead? |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 103 | while raw == self.eof: |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 104 | if not self.filestack: |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 105 | return self.eof |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 106 | else: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 107 | self.pop_source() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 108 | raw = self.get_token() |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 109 | # Neither inclusion nor EOF |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 110 | if self.debug >= 1: |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 111 | if raw != self.eof: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 112 | print("shlex: token=" + repr(raw)) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 113 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 114 | print("shlex: token=EOF") |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 115 | return raw |
| 116 | |
| 117 | def read_token(self): |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 118 | quoted = False |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 119 | escapedstate = ' ' |
Neal Norwitz | 10cf218 | 2003-04-17 23:09:08 +0000 | [diff] [blame] | 120 | while True: |
Andrew M. Kuchling | 49d27c8 | 2000-12-23 14:20:24 +0000 | [diff] [blame] | 121 | nextchar = self.instream.read(1) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 122 | if nextchar == '\n': |
Petri Lehtinen | 0362b54 | 2013-02-23 23:03:15 +0100 | [diff] [blame] | 123 | self.lineno = self.lineno + 1 |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 124 | if self.debug >= 3: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 125 | print("shlex: in state", repr(self.state), \ |
| 126 | "I see character:", repr(nextchar)) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 127 | if self.state is None: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 128 | self.token = '' # past end of file |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 129 | break |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 130 | elif self.state == ' ': |
| 131 | if not nextchar: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 132 | self.state = None # end of file |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 133 | break |
| 134 | elif nextchar in self.whitespace: |
| 135 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 136 | print("shlex: I see whitespace in whitespace state") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 137 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 138 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 139 | else: |
| 140 | continue |
| 141 | elif nextchar in self.commenters: |
| 142 | self.instream.readline() |
| 143 | self.lineno = self.lineno + 1 |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 144 | elif self.posix and nextchar in self.escape: |
| 145 | escapedstate = 'a' |
| 146 | self.state = nextchar |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 147 | elif nextchar in self.wordchars: |
| 148 | self.token = nextchar |
| 149 | self.state = 'a' |
| 150 | elif nextchar in self.quotes: |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 151 | if not self.posix: |
| 152 | self.token = nextchar |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 153 | self.state = nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 154 | elif self.whitespace_split: |
| 155 | self.token = nextchar |
| 156 | self.state = 'a' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 157 | else: |
| 158 | self.token = nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 159 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 160 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 161 | else: |
| 162 | continue |
| 163 | elif self.state in self.quotes: |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 164 | quoted = True |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 165 | if not nextchar: # end of file |
Andrew M. Kuchling | 9d56cd1 | 2001-01-09 03:01:15 +0000 | [diff] [blame] | 166 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 167 | print("shlex: I see EOF in quotes state") |
Andrew M. Kuchling | 9d56cd1 | 2001-01-09 03:01:15 +0000 | [diff] [blame] | 168 | # XXX what error should be raised here? |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 169 | raise ValueError("No closing quotation") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 170 | if nextchar == self.state: |
| 171 | if not self.posix: |
| 172 | self.token = self.token + nextchar |
| 173 | self.state = ' ' |
| 174 | break |
| 175 | else: |
| 176 | self.state = 'a' |
| 177 | elif self.posix and nextchar in self.escape and \ |
| 178 | self.state in self.escapedquotes: |
| 179 | escapedstate = self.state |
| 180 | self.state = nextchar |
| 181 | else: |
| 182 | self.token = self.token + nextchar |
| 183 | elif self.state in self.escape: |
| 184 | if not nextchar: # end of file |
| 185 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 186 | print("shlex: I see EOF in escape state") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 187 | # XXX what error should be raised here? |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 188 | raise ValueError("No escaped character") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 189 | # In posix shells, only the quote itself or the escape |
| 190 | # character may be escaped within quotes. |
| 191 | if escapedstate in self.quotes and \ |
| 192 | nextchar != self.state and nextchar != escapedstate: |
| 193 | self.token = self.token + self.state |
| 194 | self.token = self.token + nextchar |
| 195 | self.state = escapedstate |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 196 | elif self.state == 'a': |
| 197 | if not nextchar: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 198 | self.state = None # end of file |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 199 | break |
| 200 | elif nextchar in self.whitespace: |
| 201 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 202 | print("shlex: I see whitespace in word state") |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 203 | self.state = ' ' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 204 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 205 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 206 | else: |
| 207 | continue |
| 208 | elif nextchar in self.commenters: |
| 209 | self.instream.readline() |
| 210 | self.lineno = self.lineno + 1 |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 211 | if self.posix: |
| 212 | self.state = ' ' |
| 213 | if self.token or (self.posix and quoted): |
| 214 | break # emit current token |
| 215 | else: |
| 216 | continue |
| 217 | elif self.posix and nextchar in self.quotes: |
| 218 | self.state = nextchar |
| 219 | elif self.posix and nextchar in self.escape: |
| 220 | escapedstate = 'a' |
| 221 | self.state = nextchar |
| 222 | elif nextchar in self.wordchars or nextchar in self.quotes \ |
| 223 | or self.whitespace_split: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 224 | self.token = self.token + nextchar |
| 225 | else: |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 226 | self.pushback.appendleft(nextchar) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 227 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 228 | print("shlex: I see punctuation in word state") |
Guido van Rossum | f247d75 | 1999-03-22 15:28:08 +0000 | [diff] [blame] | 229 | self.state = ' ' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 230 | if self.token: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 231 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 232 | else: |
| 233 | continue |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 234 | result = self.token |
| 235 | self.token = '' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 236 | if self.posix and not quoted and result == '': |
| 237 | result = None |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 238 | if self.debug > 1: |
| 239 | if result: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 240 | print("shlex: raw token=" + repr(result)) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 241 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 242 | print("shlex: raw token=EOF") |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 243 | return result |
| 244 | |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 245 | def sourcehook(self, newfile): |
| 246 | "Hook called on a filename to be sourced." |
| 247 | if newfile[0] == '"': |
| 248 | newfile = newfile[1:-1] |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 249 | # This implements cpp-like semantics for relative-path inclusion. |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 250 | if isinstance(self.infile, str) and not os.path.isabs(newfile): |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 251 | newfile = os.path.join(os.path.dirname(self.infile), newfile) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 252 | return (newfile, open(newfile, "r")) |
| 253 | |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 254 | def error_leader(self, infile=None, lineno=None): |
| 255 | "Emit a C-compiler-like, Emacs-friendly error-message leader." |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 256 | if infile is None: |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 257 | infile = self.infile |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 258 | if lineno is None: |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 259 | lineno = self.lineno |
| 260 | return "\"%s\", line %d: " % (infile, lineno) |
| 261 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 262 | def __iter__(self): |
| 263 | return self |
| 264 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 265 | def __next__(self): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 266 | token = self.get_token() |
| 267 | if token == self.eof: |
| 268 | raise StopIteration |
| 269 | return token |
| 270 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 271 | def split(s, comments=False, posix=True): |
| 272 | lex = shlex(s, posix=posix) |
Gustavo Niemeyer | 48f3dcc | 2003-04-20 01:57:03 +0000 | [diff] [blame] | 273 | lex.whitespace_split = True |
| 274 | if not comments: |
| 275 | lex.commenters = '' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 276 | return list(lex) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 277 | |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 278 | |
Ezio Melotti | 67321cc | 2011-08-16 19:03:41 +0300 | [diff] [blame] | 279 | _find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 280 | |
| 281 | def quote(s): |
| 282 | """Return a shell-escaped version of the string *s*.""" |
| 283 | if not s: |
| 284 | return "''" |
| 285 | if _find_unsafe(s) is None: |
| 286 | return s |
| 287 | |
| 288 | # use single quotes, and put single quotes into double quotes |
| 289 | # the string $'b is then quoted as '$'"'"'b' |
| 290 | return "'" + s.replace("'", "'\"'\"'") + "'" |
| 291 | |
| 292 | |
R David Murray | 838f2c4 | 2014-10-17 20:28:47 -0400 | [diff] [blame] | 293 | def _print_tokens(lexer): |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 294 | while 1: |
| 295 | tt = lexer.get_token() |
R David Murray | 838f2c4 | 2014-10-17 20:28:47 -0400 | [diff] [blame] | 296 | if not tt: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 297 | break |
R David Murray | 838f2c4 | 2014-10-17 20:28:47 -0400 | [diff] [blame] | 298 | print("Token: " + repr(tt)) |
| 299 | |
| 300 | if __name__ == '__main__': |
| 301 | if len(sys.argv) == 1: |
| 302 | _print_tokens(shlex()) |
| 303 | else: |
| 304 | fn = sys.argv[1] |
| 305 | with open(fn) as f: |
| 306 | _print_tokens(shlex(f, fn)) |