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. |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 6 | |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 7 | import os.path |
Guido van Rossum | 73898c7 | 1999-05-03 18:14:16 +0000 | [diff] [blame] | 8 | import sys |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 9 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 10 | __all__ = ["shlex"] |
| 11 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 12 | class shlex: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 13 | "A lexical analyzer class for simple shell-like syntaxes." |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 14 | def __init__(self, instream=None, infile=None): |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 15 | if instream: |
| 16 | self.instream = instream |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 17 | self.infile = infile |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 18 | else: |
| 19 | self.instream = sys.stdin |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 20 | self.infile = None |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 21 | self.commenters = '#' |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 22 | self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' |
| 23 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 24 | self.whitespace = ' \t\r\n' |
| 25 | self.quotes = '\'"' |
| 26 | self.state = ' ' |
Andrew M. Kuchling | 49d27c8 | 2000-12-23 14:20:24 +0000 | [diff] [blame] | 27 | self.pushback = [] |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 28 | self.lineno = 1 |
| 29 | self.debug = 0 |
| 30 | self.token = '' |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 31 | self.filestack = [] |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 32 | self.source = None |
| 33 | if self.debug: |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 34 | print 'shlex: reading from %s, line %d' \ |
| 35 | % (self.instream, self.lineno) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 36 | |
| 37 | def push_token(self, tok): |
| 38 | "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] | 39 | if self.debug >= 1: |
| 40 | print "shlex: pushing token " + `tok` |
Andrew M. Kuchling | 49d27c8 | 2000-12-23 14:20:24 +0000 | [diff] [blame] | 41 | self.pushback = [tok] + self.pushback |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 42 | |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 43 | def push_source(self, newstream, newfile=None): |
| 44 | "Push an input source onto the lexer's input source stack." |
| 45 | self.filestack.insert(0, (self.infile, self.instream, self.lineno)) |
| 46 | self.infile = newfile |
| 47 | self.instream = newstream |
| 48 | self.lineno = 1 |
| 49 | if self.debug: |
| 50 | if newfile: |
| 51 | print 'shlex: pushing to file %s' % (self.infile,) |
| 52 | else: |
| 53 | print 'shlex: pushing to stream %s' % (self.instream,) |
| 54 | |
| 55 | def pop_source(self): |
| 56 | "Pop the input source stack." |
| 57 | self.instream.close() |
| 58 | (self.infile, self.instream, self.lineno) = self.filestack[0] |
| 59 | self.filestack = self.filestack[1:] |
| 60 | if self.debug: |
| 61 | print 'shlex: popping to %s, line %d' \ |
| 62 | % (self.instream, self.lineno) |
| 63 | self.state = ' ' |
| 64 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 65 | def get_token(self): |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 66 | "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] | 67 | if self.pushback: |
| 68 | tok = self.pushback[0] |
| 69 | self.pushback = self.pushback[1:] |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 70 | if self.debug >= 1: |
| 71 | print "shlex: popping token " + `tok` |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 72 | return tok |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 73 | # No pushback. Get a token. |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 74 | raw = self.read_token() |
| 75 | # Handle inclusions |
| 76 | while raw == self.source: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 77 | spec = self.sourcehook(self.read_token()) |
| 78 | if spec: |
| 79 | (newfile, newstream) = spec |
| 80 | self.push_source(newstream, newfile) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 81 | raw = self.get_token() |
| 82 | # Maybe we got EOF instead? |
| 83 | while raw == "": |
| 84 | if len(self.filestack) == 0: |
| 85 | return "" |
| 86 | else: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 87 | self.pop_source() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 88 | raw = self.get_token() |
| 89 | # Neither inclusion nor EOF |
| 90 | if self.debug >= 1: |
| 91 | if raw: |
| 92 | print "shlex: token=" + `raw` |
| 93 | else: |
| 94 | print "shlex: token=EOF" |
| 95 | return raw |
| 96 | |
| 97 | def read_token(self): |
| 98 | "Read a token from the input stream (no pushback or inclusions)" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 99 | while 1: |
Andrew M. Kuchling | 49d27c8 | 2000-12-23 14:20:24 +0000 | [diff] [blame] | 100 | nextchar = self.instream.read(1) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 101 | if nextchar == '\n': |
| 102 | self.lineno = self.lineno + 1 |
| 103 | if self.debug >= 3: |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 104 | print "shlex: in state", repr(self.state), \ |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 105 | "I see character:", repr(nextchar) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 106 | if self.state is None: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 107 | self.token = '' # past end of file |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 108 | break |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 109 | elif self.state == ' ': |
| 110 | if not nextchar: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 111 | self.state = None # end of file |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 112 | break |
| 113 | elif nextchar in self.whitespace: |
| 114 | if self.debug >= 2: |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 115 | print "shlex: I see whitespace in whitespace state" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 116 | if self.token: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 117 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 118 | else: |
| 119 | continue |
| 120 | elif nextchar in self.commenters: |
| 121 | self.instream.readline() |
| 122 | self.lineno = self.lineno + 1 |
| 123 | elif nextchar in self.wordchars: |
| 124 | self.token = nextchar |
| 125 | self.state = 'a' |
| 126 | elif nextchar in self.quotes: |
| 127 | self.token = nextchar |
| 128 | self.state = nextchar |
| 129 | else: |
| 130 | self.token = nextchar |
| 131 | if self.token: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 132 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 133 | else: |
| 134 | continue |
| 135 | elif self.state in self.quotes: |
| 136 | self.token = self.token + nextchar |
| 137 | if nextchar == self.state: |
| 138 | self.state = ' ' |
| 139 | break |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 140 | elif not nextchar: # end of file |
Andrew M. Kuchling | 9d56cd1 | 2001-01-09 03:01:15 +0000 | [diff] [blame] | 141 | if self.debug >= 2: |
| 142 | print "shlex: I see EOF in quotes state" |
| 143 | # XXX what error should be raised here? |
| 144 | raise ValueError, "No closing quotation" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 145 | elif self.state == 'a': |
| 146 | if not nextchar: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 147 | self.state = None # end of file |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 148 | break |
| 149 | elif nextchar in self.whitespace: |
| 150 | if self.debug >= 2: |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 151 | print "shlex: I see whitespace in word state" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 152 | self.state = ' ' |
| 153 | if self.token: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 154 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 155 | else: |
| 156 | continue |
| 157 | elif nextchar in self.commenters: |
| 158 | self.instream.readline() |
| 159 | self.lineno = self.lineno + 1 |
| 160 | elif nextchar in self.wordchars or nextchar in self.quotes: |
| 161 | self.token = self.token + nextchar |
| 162 | else: |
| 163 | self.pushback = [nextchar] + self.pushback |
| 164 | if self.debug >= 2: |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 165 | print "shlex: I see punctuation in word state" |
Guido van Rossum | f247d75 | 1999-03-22 15:28:08 +0000 | [diff] [blame] | 166 | self.state = ' ' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 167 | if self.token: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 168 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 169 | else: |
| 170 | continue |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 171 | result = self.token |
| 172 | self.token = '' |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 173 | if self.debug > 1: |
| 174 | if result: |
| 175 | print "shlex: raw token=" + `result` |
| 176 | else: |
| 177 | print "shlex: raw token=EOF" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 178 | return result |
| 179 | |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 180 | def sourcehook(self, newfile): |
| 181 | "Hook called on a filename to be sourced." |
| 182 | if newfile[0] == '"': |
| 183 | newfile = newfile[1:-1] |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 184 | # This implements cpp-like semantics for relative-path inclusion. |
| 185 | if type(self.infile) == type("") and not os.path.isabs(newfile): |
| 186 | newfile = os.path.join(os.path.dirname(self.infile), newfile) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 187 | return (newfile, open(newfile, "r")) |
| 188 | |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 189 | def error_leader(self, infile=None, lineno=None): |
| 190 | "Emit a C-compiler-like, Emacs-friendly error-message leader." |
| 191 | if not infile: |
| 192 | infile = self.infile |
| 193 | if not lineno: |
| 194 | lineno = self.lineno |
| 195 | return "\"%s\", line %d: " % (infile, lineno) |
| 196 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 197 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 198 | if __name__ == '__main__': |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 199 | if len(sys.argv) == 1: |
| 200 | lexer = shlex() |
| 201 | else: |
| 202 | file = sys.argv[1] |
| 203 | lexer = shlex(open(file), file) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 204 | while 1: |
| 205 | tt = lexer.get_token() |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 206 | if tt: |
| 207 | print "Token: " + repr(tt) |
| 208 | else: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 209 | break |