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