mirror of https://github.com/python/cpython.git
parent
dbd6503e97
commit
243152384b
17
Lib/shlex.py
17
Lib/shlex.py
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
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, posix=0):
|
def __init__(self, instream=None, infile=None, posix=False):
|
||||||
if type(instream) in StringTypes:
|
if type(instream) in StringTypes:
|
||||||
instream = StringIO(instream)
|
instream = StringIO(instream)
|
||||||
if instream is not None:
|
if instream is not None:
|
||||||
|
@ -42,7 +42,7 @@ def __init__(self, instream=None, infile=None, posix=0):
|
||||||
self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
|
self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
|
||||||
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ')
|
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ')
|
||||||
self.whitespace = ' \t\r\n'
|
self.whitespace = ' \t\r\n'
|
||||||
self.whitespace_split = 0
|
self.whitespace_split = False
|
||||||
self.quotes = '\'"'
|
self.quotes = '\'"'
|
||||||
self.escape = '\\'
|
self.escape = '\\'
|
||||||
self.escapedquotes = '"'
|
self.escapedquotes = '"'
|
||||||
|
@ -61,7 +61,7 @@ def push_token(self, tok):
|
||||||
"Push a token onto the stack popped by the get_token method"
|
"Push a token onto the stack popped by the get_token method"
|
||||||
if self.debug >= 1:
|
if self.debug >= 1:
|
||||||
print "shlex: pushing token " + `tok`
|
print "shlex: pushing token " + `tok`
|
||||||
self.pushback = [tok] + self.pushback
|
self.pushback.insert(0, tok)
|
||||||
|
|
||||||
def push_source(self, newstream, newfile=None):
|
def push_source(self, newstream, newfile=None):
|
||||||
"Push an input source onto the lexer's input source stack."
|
"Push an input source onto the lexer's input source stack."
|
||||||
|
@ -90,8 +90,7 @@ def pop_source(self):
|
||||||
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:
|
||||||
tok = self.pushback[0]
|
tok = self.pushback.pop(0)
|
||||||
self.pushback = self.pushback[1:]
|
|
||||||
if self.debug >= 1:
|
if self.debug >= 1:
|
||||||
print "shlex: popping token " + `tok`
|
print "shlex: popping token " + `tok`
|
||||||
return tok
|
return tok
|
||||||
|
@ -107,7 +106,7 @@ def get_token(self):
|
||||||
raw = self.get_token()
|
raw = self.get_token()
|
||||||
# Maybe we got EOF instead?
|
# Maybe we got EOF instead?
|
||||||
while raw == self.eof:
|
while raw == self.eof:
|
||||||
if len(self.filestack) == 0:
|
if not self.filestack:
|
||||||
return self.eof
|
return self.eof
|
||||||
else:
|
else:
|
||||||
self.pop_source()
|
self.pop_source()
|
||||||
|
@ -121,7 +120,7 @@ def get_token(self):
|
||||||
return raw
|
return raw
|
||||||
|
|
||||||
def read_token(self):
|
def read_token(self):
|
||||||
quoted = 0
|
quoted = False
|
||||||
escapedstate = ' '
|
escapedstate = ' '
|
||||||
while 1:
|
while 1:
|
||||||
nextchar = self.instream.read(1)
|
nextchar = self.instream.read(1)
|
||||||
|
@ -167,7 +166,7 @@ def read_token(self):
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
elif self.state in self.quotes:
|
elif self.state in self.quotes:
|
||||||
quoted = 1
|
quoted = True
|
||||||
if not nextchar: # end of file
|
if 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"
|
||||||
|
@ -229,7 +228,7 @@ def read_token(self):
|
||||||
or self.whitespace_split:
|
or self.whitespace_split:
|
||||||
self.token = self.token + nextchar
|
self.token = self.token + nextchar
|
||||||
else:
|
else:
|
||||||
self.pushback = [nextchar] + self.pushback
|
self.pushback.insert(0, nextchar)
|
||||||
if self.debug >= 2:
|
if self.debug >= 2:
|
||||||
print "shlex: I see punctuation in word state"
|
print "shlex: I see punctuation in word state"
|
||||||
self.state = ' '
|
self.state = ' '
|
||||||
|
|
Loading…
Reference in New Issue