[Python-checkins] CVS: python/dist/src/Lib shlex.py,1.12,1.13
Eric S. Raymond
esr@users.sourceforge.net
Tue, 16 Jan 2001 07:19:16 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv30043
Modified Files:
shlex.py
Log Message:
Make pop_source and push_source available, as documented.
Index: shlex.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/shlex.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -r1.12 -r1.13
*** shlex.py 2001/01/15 01:36:40 1.12
--- shlex.py 2001/01/16 15:19:13 1.13
***************
*** 1,13 ****
"""A lexical analyzer class for simple shell-like syntaxes."""
! # Module and documentation by Eric S. Raymond, 21 Dec 1998
# Input stacking and error message cleanup added by ESR, March 2000
import os.path
import sys
-
class shlex:
! "A lexical analyzer class for simple shell-like syntaxes."
def __init__(self, instream=None, infile=None):
if instream:
--- 1,13 ----
"""A lexical analyzer class for simple shell-like syntaxes."""
! # Module and documentation by Eric S. Raymond, 21 Dec 1998
# 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 sys
class shlex:
! "A lexical analyzer class for simple shell-like syntaxes."
def __init__(self, instream=None, infile=None):
if instream:
***************
*** 39,42 ****
--- 39,64 ----
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):
"Get a token from the input stream (or from stack if it's nonempty)"
***************
*** 51,61 ****
# Handle inclusions
while raw == self.source:
! (newfile, newstream) = self.sourcehook(self.read_token())
! self.filestack.insert(0, (self.infile, self.instream, self.lineno))
! self.infile = newfile
! self.instream = newstream
! self.lineno = 1
! if self.debug:
! print 'shlex: pushing to file %s' % (self.infile,)
raw = self.get_token()
# Maybe we got EOF instead?
--- 73,80 ----
# Handle inclusions
while raw == self.source:
! spec = self.sourcehook(self.read_token())
! if spec:
! (newfile, newstream) = spec
! self.push_source(newstream, newfile)
raw = self.get_token()
# Maybe we got EOF instead?
***************
*** 64,74 ****
return ""
else:
! 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 = ' '
raw = self.get_token()
# Neither inclusion nor EOF
--- 83,87 ----
return ""
else:
! self.pop_source()
raw = self.get_token()
# Neither inclusion nor EOF
***************
*** 89,99 ****
if self.debug >= 3:
print "shlex: in state", repr(self.state), \
! "I see character:", repr(nextchar)
if self.state is None:
! self.token = '' # past end of file
break
elif self.state == ' ':
if not nextchar:
! self.state = None # end of file
break
elif nextchar in self.whitespace:
--- 102,112 ----
if self.debug >= 3:
print "shlex: in state", repr(self.state), \
! "I see character:", repr(nextchar)
if self.state is None:
! self.token = '' # past end of file
break
elif self.state == ' ':
if not nextchar:
! self.state = None # end of file
break
elif nextchar in self.whitespace:
***************
*** 124,128 ****
self.state = ' '
break
! elif not nextchar: # end of file
if self.debug >= 2:
print "shlex: I see EOF in quotes state"
--- 137,141 ----
self.state = ' '
break
! elif not nextchar: # end of file
if self.debug >= 2:
print "shlex: I see EOF in quotes state"
***************
*** 131,135 ****
elif self.state == 'a':
if not nextchar:
! self.state = None # end of file
break
elif nextchar in self.whitespace:
--- 144,148 ----
elif self.state == 'a':
if not nextchar:
! self.state = None # end of file
break
elif nextchar in self.whitespace:
***************
*** 182,186 ****
! if __name__ == '__main__':
if len(sys.argv) == 1:
lexer = shlex()
--- 195,199 ----
! if __name__ == '__main__':
if len(sys.argv) == 1:
lexer = shlex()