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