1 """A lexical analyzer class for simple shell-like syntaxes."""
13 "A lexical analyzer class for simple shell-like syntaxes."
14 def __init__(self, instream=None, infile=None):
23 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
34 print 'shlex: reading from %s, line %d' \
38 "Push a token onto the stack popped by the get_token method"
40 print "shlex: pushing token " + `tok`
44 "Push an input source onto the lexer's input source stack."
51 print 'shlex: pushing to file %s' % (self.
infile,)
53 print 'shlex: pushing to stream %s' % (self.
instream,)
56 "Pop the input source stack."
61 print 'shlex: popping to %s, line %d' \
66 "Get a token from the input stream (or from stack if it's nonempty)"
71 print "shlex: popping token " + `tok`
79 (newfile, newstream) = spec
92 print "shlex: token=" + `raw`
94 print "shlex: token=EOF"
98 "Read a token from the input stream (no pushback or inclusions)"
100 nextchar = self.instream.read(1)
104 print "shlex: in state",
repr(self.
state), \
105 "I see character:",
repr(nextchar)
106 if self.
state is None:
109 elif self.
state ==
' ':
115 print "shlex: I see whitespace in whitespace state"
121 self.instream.readline()
124 self.
token = nextchar
126 elif nextchar
in self.
quotes:
127 self.
token = nextchar
128 self.
state = nextchar
130 self.
token = nextchar
137 if nextchar == self.
state:
142 print "shlex: I see EOF in quotes state"
144 raise ValueError,
"No closing quotation"
145 elif self.
state ==
'a':
151 print "shlex: I see whitespace in word state"
158 self.instream.readline()
165 print "shlex: I see punctuation in word state"
175 print "shlex: raw token=" + `result`
177 print "shlex: raw token=EOF"
181 "Hook called on a filename to be sourced."
182 if newfile[0] ==
'"':
183 newfile = newfile[1:-1]
185 if type(self.
infile) == type(
"")
and not os.path.isabs(newfile):
186 newfile = os.path.join(os.path.dirname(self.
infile), newfile)
187 return (newfile,
open(newfile,
"r"))
190 "Emit a C-compiler-like, Emacs-friendly error-message leader."
195 return "\"%s\", line %d: " % (infile, lineno)
198 if __name__ ==
'__main__':
199 if len(sys.argv) == 1:
205 tt = lexer.get_token()
207 print "Token: " +
repr(tt)