blob: fd7535a3c630efa3c1cfb76083b7ca895a6bdbb0 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""A lexical analyzer class for simple shell-like syntaxes."""
2
Guido van Rossum9c30c241998-12-22 05:19:29 +00003# Module and documentation by Eric S. Raymond, 21 Dec 1998
Guido van Rossumeb4e11a2000-05-01 20:08:46 +00004# Input stacking and error message cleanup added by ESR, March 2000
Guido van Rossum9c30c241998-12-22 05:19:29 +00005
Fred Drake52dc76c2000-07-03 09:56:23 +00006import os.path
Guido van Rossum73898c71999-05-03 18:14:16 +00007import sys
Guido van Rossum9c30c241998-12-22 05:19:29 +00008
Fred Drake52dc76c2000-07-03 09:56:23 +00009
Guido van Rossum9c30c241998-12-22 05:19:29 +000010class shlex:
11 "A lexical analyzer class for simple shell-like syntaxes."
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000012 def __init__(self, instream=None, infile=None):
Guido van Rossum9c30c241998-12-22 05:19:29 +000013 if instream:
14 self.instream = instream
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000015 self.infile = infile
Guido van Rossum9c30c241998-12-22 05:19:29 +000016 else:
17 self.instream = sys.stdin
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000018 self.infile = None
Guido van Rossum9c30c241998-12-22 05:19:29 +000019 self.commenters = '#'
Fred Drakedbbf76b2000-07-09 16:44:26 +000020 self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
21 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
Guido van Rossum9c30c241998-12-22 05:19:29 +000022 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 Drakedbbf76b2000-07-09 16:44:26 +000029 self.filestack = []
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000030 self.source = None
31 if self.debug:
Fred Drake52dc76c2000-07-03 09:56:23 +000032 print 'shlex: reading from %s, line %d' \
33 % (self.instream, self.lineno)
Guido van Rossum9c30c241998-12-22 05:19:29 +000034
35 def push_token(self, tok):
36 "Push a token onto the stack popped by the get_token method"
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000037 if self.debug >= 1:
38 print "shlex: pushing token " + `tok`
Guido van Rossum9c30c241998-12-22 05:19:29 +000039 self.pushback = [tok] + self.pushback;
40
41 def get_token(self):
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000042 "Get a token from the input stream (or from stack if it's nonempty)"
Guido van Rossum9c30c241998-12-22 05:19:29 +000043 if self.pushback:
44 tok = self.pushback[0]
45 self.pushback = self.pushback[1:]
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000046 if self.debug >= 1:
47 print "shlex: popping token " + `tok`
Guido van Rossum9c30c241998-12-22 05:19:29 +000048 return tok
Fred Drakedbbf76b2000-07-09 16:44:26 +000049 # No pushback. Get a token.
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000050 raw = self.read_token()
51 # Handle inclusions
52 while raw == self.source:
53 (newfile, newstream) = self.sourcehook(self.read_token())
Fred Drake52dc76c2000-07-03 09:56:23 +000054 self.filestack.insert(0, (self.infile, self.instream, self.lineno))
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000055 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 Drake52dc76c2000-07-03 09:56:23 +000070 print 'shlex: popping to %s, line %d' \
71 % (self.instream, self.lineno)
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000072 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 Rossum9c30c241998-12-22 05:19:29 +000084 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 Drake52dc76c2000-07-03 09:56:23 +000090 print "shlex: in state", repr(self.state), \
91 "I see character:", repr(nextchar)
Fred Drakedbbf76b2000-07-09 16:44:26 +000092 if self.state is None:
93 self.token = ''; # past end of file
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000094 break
Guido van Rossum9c30c241998-12-22 05:19:29 +000095 elif self.state == ' ':
96 if not nextchar:
Fred Drakedbbf76b2000-07-09 16:44:26 +000097 self.state = None; # end of file
Guido van Rossum9c30c241998-12-22 05:19:29 +000098 break
99 elif nextchar in self.whitespace:
100 if self.debug >= 2:
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000101 print "shlex: I see whitespace in whitespace state"
Guido van Rossum9c30c241998-12-22 05:19:29 +0000102 if self.token:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000103 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000104 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 Drakedbbf76b2000-07-09 16:44:26 +0000118 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000119 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 Drakedbbf76b2000-07-09 16:44:26 +0000128 self.state = None; # end of file
Guido van Rossum9c30c241998-12-22 05:19:29 +0000129 break
130 elif nextchar in self.whitespace:
131 if self.debug >= 2:
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000132 print "shlex: I see whitespace in word state"
Guido van Rossum9c30c241998-12-22 05:19:29 +0000133 self.state = ' '
134 if self.token:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000135 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000136 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 Rossumeb4e11a2000-05-01 20:08:46 +0000146 print "shlex: I see punctuation in word state"
Guido van Rossumf247d751999-03-22 15:28:08 +0000147 self.state = ' '
Guido van Rossum9c30c241998-12-22 05:19:29 +0000148 if self.token:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000149 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000150 else:
151 continue
Guido van Rossum9c30c241998-12-22 05:19:29 +0000152 result = self.token
153 self.token = ''
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000154 if self.debug > 1:
155 if result:
156 print "shlex: raw token=" + `result`
157 else:
158 print "shlex: raw token=EOF"
Guido van Rossum9c30c241998-12-22 05:19:29 +0000159 return result
160
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000161 def sourcehook(self, newfile):
162 "Hook called on a filename to be sourced."
163 if newfile[0] == '"':
164 newfile = newfile[1:-1]
Fred Drake52dc76c2000-07-03 09:56:23 +0000165 # 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 Rossumeb4e11a2000-05-01 20:08:46 +0000168 return (newfile, open(newfile, "r"))
169
Guido van Rossum4b83ecb2000-05-01 20:14:12 +0000170 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 Rossum9c30c241998-12-22 05:19:29 +0000178
Fred Drake52dc76c2000-07-03 09:56:23 +0000179if __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 Rossum9c30c241998-12-22 05:19:29 +0000185 while 1:
186 tt = lexer.get_token()
Fred Drake52dc76c2000-07-03 09:56:23 +0000187 if tt:
188 print "Token: " + repr(tt)
189 else:
Guido van Rossum9c30c241998-12-22 05:19:29 +0000190 break