Make pop_source and push_source available, as documented.

This commit is contained in:
Eric S. Raymond 2001-01-16 15:19:13 +00:00
parent 1360359bb9
commit bddbaf7023

View file

@ -2,11 +2,11 @@
# Module and documentation by Eric S. Raymond, 21 Dec 1998 # Module and documentation by Eric S. Raymond, 21 Dec 1998
# Input stacking and error message cleanup added by ESR, March 2000 # Input stacking and error message cleanup added by ESR, March 2000
# push_source() and pop_source() made explicit by ESR, January 2001.
import os.path import os.path
import sys import sys
class shlex: class shlex:
"A lexical analyzer class for simple shell-like syntaxes." "A lexical analyzer class for simple shell-like syntaxes."
def __init__(self, instream=None, infile=None): def __init__(self, instream=None, infile=None):
@ -38,6 +38,28 @@ class shlex:
print "shlex: pushing token " + `tok` print "shlex: pushing token " + `tok`
self.pushback = [tok] + self.pushback self.pushback = [tok] + self.pushback
def push_source(self, newstream, newfile=None):
"Push an input source onto the lexer's input source stack."
self.filestack.insert(0, (self.infile, self.instream, self.lineno))
self.infile = newfile
self.instream = newstream
self.lineno = 1
if self.debug:
if newfile:
print 'shlex: pushing to file %s' % (self.infile,)
else:
print 'shlex: pushing to stream %s' % (self.instream,)
def pop_source(self):
"Pop the input source stack."
self.instream.close()
(self.infile, self.instream, self.lineno) = self.filestack[0]
self.filestack = self.filestack[1:]
if self.debug:
print 'shlex: popping to %s, line %d' \
% (self.instream, self.lineno)
self.state = ' '
def get_token(self): def get_token(self):
"Get a token from the input stream (or from stack if it's nonempty)" "Get a token from the input stream (or from stack if it's nonempty)"
if self.pushback: if self.pushback:
@ -50,26 +72,17 @@ class shlex:
raw = self.read_token() raw = self.read_token()
# Handle inclusions # Handle inclusions
while raw == self.source: while raw == self.source:
(newfile, newstream) = self.sourcehook(self.read_token()) spec = self.sourcehook(self.read_token())
self.filestack.insert(0, (self.infile, self.instream, self.lineno)) if spec:
self.infile = newfile (newfile, newstream) = spec
self.instream = newstream self.push_source(newstream, newfile)
self.lineno = 1
if self.debug:
print 'shlex: pushing to file %s' % (self.infile,)
raw = self.get_token() raw = self.get_token()
# Maybe we got EOF instead? # Maybe we got EOF instead?
while raw == "": while raw == "":
if len(self.filestack) == 0: if len(self.filestack) == 0:
return "" return ""
else: else:
self.instream.close() self.pop_source()
(self.infile, self.instream, self.lineno) = self.filestack[0]
self.filestack = self.filestack[1:]
if self.debug:
print 'shlex: popping to %s, line %d' \
% (self.instream, self.lineno)
self.state = ' '
raw = self.get_token() raw = self.get_token()
# Neither inclusion nor EOF # Neither inclusion nor EOF
if self.debug >= 1: if self.debug >= 1:
@ -90,11 +103,11 @@ class shlex:
print "shlex: in state", repr(self.state), \ print "shlex: in state", repr(self.state), \
"I see character:", repr(nextchar) "I see character:", repr(nextchar)
if self.state is None: if self.state is None:
self.token = '' # past end of file self.token = '' # past end of file
break break
elif self.state == ' ': elif self.state == ' ':
if not nextchar: if not nextchar:
self.state = None # end of file self.state = None # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
if self.debug >= 2: if self.debug >= 2:
@ -123,14 +136,14 @@ class shlex:
if nextchar == self.state: if nextchar == self.state:
self.state = ' ' self.state = ' '
break break
elif not nextchar: # end of file elif not nextchar: # end of file
if self.debug >= 2: if self.debug >= 2:
print "shlex: I see EOF in quotes state" print "shlex: I see EOF in quotes state"
# XXX what error should be raised here? # XXX what error should be raised here?
raise ValueError, "No closing quotation" raise ValueError, "No closing quotation"
elif self.state == 'a': elif self.state == 'a':
if not nextchar: if not nextchar:
self.state = None # end of file self.state = None # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
if self.debug >= 2: if self.debug >= 2: