mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Make pop_source and push_source available, as documented.
This commit is contained in:
parent
1360359bb9
commit
bddbaf7023
1 changed files with 36 additions and 23 deletions
43
Lib/shlex.py
43
Lib/shlex.py
|
@ -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:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue