2003-04-17 18:31:33 -03:00
|
|
|
|
# -*- coding: iso-8859-1 -*-
|
2000-02-04 11:28:42 -04:00
|
|
|
|
"""A lexical analyzer class for simple shell-like syntaxes."""
|
|
|
|
|
|
2001-01-17 04:48:39 -04:00
|
|
|
|
# Module and documentation by Eric S. Raymond, 21 Dec 1998
|
2000-05-01 17:08:46 -03:00
|
|
|
|
# Input stacking and error message cleanup added by ESR, March 2000
|
2001-01-17 04:48:39 -04:00
|
|
|
|
# push_source() and pop_source() made explicit by ESR, January 2001.
|
2003-04-17 18:31:33 -03:00
|
|
|
|
# Posix compliance, split(), string arguments, and
|
|
|
|
|
# iterator interface by Gustavo Niemeyer, April 2003.
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
2000-07-03 06:56:23 -03:00
|
|
|
|
import os.path
|
1999-05-03 15:14:16 -03:00
|
|
|
|
import sys
|
2004-01-29 02:37:52 -04:00
|
|
|
|
from collections import deque
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
2003-04-17 18:31:33 -03:00
|
|
|
|
try:
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
except ImportError:
|
|
|
|
|
from StringIO import StringIO
|
|
|
|
|
|
|
|
|
|
__all__ = ["shlex", "split"]
|
2001-02-15 18:15:14 -04:00
|
|
|
|
|
1998-12-22 01:19:29 -04:00
|
|
|
|
class shlex:
|
2001-01-17 04:48:39 -04:00
|
|
|
|
"A lexical analyzer class for simple shell-like syntaxes."
|
2003-04-17 19:01:17 -03:00
|
|
|
|
def __init__(self, instream=None, infile=None, posix=False):
|
2003-04-17 20:09:08 -03:00
|
|
|
|
if isinstance(instream, basestring):
|
2003-04-17 18:31:33 -03:00
|
|
|
|
instream = StringIO(instream)
|
2002-06-01 21:40:05 -03:00
|
|
|
|
if instream is not None:
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.instream = instream
|
2000-05-01 17:08:46 -03:00
|
|
|
|
self.infile = infile
|
1998-12-22 01:19:29 -04:00
|
|
|
|
else:
|
|
|
|
|
self.instream = sys.stdin
|
2000-05-01 17:08:46 -03:00
|
|
|
|
self.infile = None
|
2003-04-17 18:31:33 -03:00
|
|
|
|
self.posix = posix
|
|
|
|
|
if posix:
|
|
|
|
|
self.eof = None
|
|
|
|
|
else:
|
|
|
|
|
self.eof = ''
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.commenters = '#'
|
2000-07-09 13:44:26 -03:00
|
|
|
|
self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
|
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.posix:
|
|
|
|
|
self.wordchars += ('<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'
|
|
|
|
|
'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>')
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.whitespace = ' \t\r\n'
|
2003-04-17 19:01:17 -03:00
|
|
|
|
self.whitespace_split = False
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.quotes = '\'"'
|
2003-04-17 18:31:33 -03:00
|
|
|
|
self.escape = '\\'
|
|
|
|
|
self.escapedquotes = '"'
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.state = ' '
|
2004-01-29 02:37:52 -04:00
|
|
|
|
self.pushback = deque()
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.lineno = 1
|
|
|
|
|
self.debug = 0
|
|
|
|
|
self.token = ''
|
2004-01-29 02:37:52 -04:00
|
|
|
|
self.filestack = deque()
|
2000-05-01 17:08:46 -03:00
|
|
|
|
self.source = None
|
|
|
|
|
if self.debug:
|
2000-07-03 06:56:23 -03:00
|
|
|
|
print 'shlex: reading from %s, line %d' \
|
|
|
|
|
% (self.instream, self.lineno)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
|
|
|
|
def push_token(self, tok):
|
|
|
|
|
"Push a token onto the stack popped by the get_token method"
|
2000-05-01 17:08:46 -03:00
|
|
|
|
if self.debug >= 1:
|
2004-02-12 13:35:32 -04:00
|
|
|
|
print "shlex: pushing token " + repr(tok)
|
2004-01-29 02:37:52 -04:00
|
|
|
|
self.pushback.appendleft(tok)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
2001-01-16 11:19:13 -04:00
|
|
|
|
def push_source(self, newstream, newfile=None):
|
|
|
|
|
"Push an input source onto the lexer's input source stack."
|
2003-04-17 20:09:08 -03:00
|
|
|
|
if isinstance(newstream, basestring):
|
2003-04-17 18:31:33 -03:00
|
|
|
|
newstream = StringIO(newstream)
|
2004-01-29 02:37:52 -04:00
|
|
|
|
self.filestack.appendleft((self.infile, self.instream, self.lineno))
|
2001-01-16 11:19:13 -04:00
|
|
|
|
self.infile = newfile
|
|
|
|
|
self.instream = newstream
|
|
|
|
|
self.lineno = 1
|
|
|
|
|
if self.debug:
|
2002-06-01 21:40:05 -03:00
|
|
|
|
if newfile is not None:
|
2001-01-16 11:19:13 -04:00
|
|
|
|
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()
|
2004-01-29 02:37:52 -04:00
|
|
|
|
(self.infile, self.instream, self.lineno) = self.filestack.popleft()
|
2001-01-16 11:19:13 -04:00
|
|
|
|
if self.debug:
|
|
|
|
|
print 'shlex: popping to %s, line %d' \
|
|
|
|
|
% (self.instream, self.lineno)
|
|
|
|
|
self.state = ' '
|
|
|
|
|
|
1998-12-22 01:19:29 -04:00
|
|
|
|
def get_token(self):
|
2000-05-01 17:08:46 -03:00
|
|
|
|
"Get a token from the input stream (or from stack if it's nonempty)"
|
1998-12-22 01:19:29 -04:00
|
|
|
|
if self.pushback:
|
2004-01-29 02:37:52 -04:00
|
|
|
|
tok = self.pushback.popleft()
|
2000-05-01 17:08:46 -03:00
|
|
|
|
if self.debug >= 1:
|
2004-02-12 13:35:32 -04:00
|
|
|
|
print "shlex: popping token " + repr(tok)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
return tok
|
2000-07-09 13:44:26 -03:00
|
|
|
|
# No pushback. Get a token.
|
2000-05-01 17:08:46 -03:00
|
|
|
|
raw = self.read_token()
|
|
|
|
|
# Handle inclusions
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.source is not None:
|
|
|
|
|
while raw == self.source:
|
|
|
|
|
spec = self.sourcehook(self.read_token())
|
|
|
|
|
if spec:
|
|
|
|
|
(newfile, newstream) = spec
|
|
|
|
|
self.push_source(newstream, newfile)
|
|
|
|
|
raw = self.get_token()
|
2000-05-01 17:08:46 -03:00
|
|
|
|
# Maybe we got EOF instead?
|
2003-04-17 18:31:33 -03:00
|
|
|
|
while raw == self.eof:
|
2003-04-17 19:01:17 -03:00
|
|
|
|
if not self.filestack:
|
2003-04-17 18:31:33 -03:00
|
|
|
|
return self.eof
|
2000-05-01 17:08:46 -03:00
|
|
|
|
else:
|
2001-01-16 11:19:13 -04:00
|
|
|
|
self.pop_source()
|
2000-05-01 17:08:46 -03:00
|
|
|
|
raw = self.get_token()
|
2003-04-17 18:31:33 -03:00
|
|
|
|
# Neither inclusion nor EOF
|
2000-05-01 17:08:46 -03:00
|
|
|
|
if self.debug >= 1:
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if raw != self.eof:
|
2004-02-12 13:35:32 -04:00
|
|
|
|
print "shlex: token=" + repr(raw)
|
2000-05-01 17:08:46 -03:00
|
|
|
|
else:
|
|
|
|
|
print "shlex: token=EOF"
|
|
|
|
|
return raw
|
|
|
|
|
|
|
|
|
|
def read_token(self):
|
2003-04-17 19:01:17 -03:00
|
|
|
|
quoted = False
|
2003-04-17 18:31:33 -03:00
|
|
|
|
escapedstate = ' '
|
2003-04-17 20:09:08 -03:00
|
|
|
|
while True:
|
2000-12-23 10:20:24 -04:00
|
|
|
|
nextchar = self.instream.read(1)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
if nextchar == '\n':
|
2013-02-23 18:02:55 -04:00
|
|
|
|
self.lineno = self.lineno + 1
|
1998-12-22 01:19:29 -04:00
|
|
|
|
if self.debug >= 3:
|
2000-07-03 06:56:23 -03:00
|
|
|
|
print "shlex: in state", repr(self.state), \
|
2001-01-17 04:48:39 -04:00
|
|
|
|
"I see character:", repr(nextchar)
|
2000-07-09 13:44:26 -03:00
|
|
|
|
if self.state is None:
|
2001-01-16 11:19:13 -04:00
|
|
|
|
self.token = '' # past end of file
|
2000-05-01 17:08:46 -03:00
|
|
|
|
break
|
1998-12-22 01:19:29 -04:00
|
|
|
|
elif self.state == ' ':
|
|
|
|
|
if not nextchar:
|
2001-01-16 11:19:13 -04:00
|
|
|
|
self.state = None # end of file
|
1998-12-22 01:19:29 -04:00
|
|
|
|
break
|
|
|
|
|
elif nextchar in self.whitespace:
|
|
|
|
|
if self.debug >= 2:
|
2000-05-01 17:08:46 -03:00
|
|
|
|
print "shlex: I see whitespace in whitespace state"
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.token or (self.posix and quoted):
|
2000-07-09 13:44:26 -03:00
|
|
|
|
break # emit current token
|
1998-12-22 01:19:29 -04:00
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
elif nextchar in self.commenters:
|
|
|
|
|
self.instream.readline()
|
|
|
|
|
self.lineno = self.lineno + 1
|
2003-04-17 18:31:33 -03:00
|
|
|
|
elif self.posix and nextchar in self.escape:
|
|
|
|
|
escapedstate = 'a'
|
|
|
|
|
self.state = nextchar
|
1998-12-22 01:19:29 -04:00
|
|
|
|
elif nextchar in self.wordchars:
|
|
|
|
|
self.token = nextchar
|
|
|
|
|
self.state = 'a'
|
|
|
|
|
elif nextchar in self.quotes:
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if not self.posix:
|
|
|
|
|
self.token = nextchar
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.state = nextchar
|
2003-04-17 18:31:33 -03:00
|
|
|
|
elif self.whitespace_split:
|
|
|
|
|
self.token = nextchar
|
|
|
|
|
self.state = 'a'
|
1998-12-22 01:19:29 -04:00
|
|
|
|
else:
|
|
|
|
|
self.token = nextchar
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.token or (self.posix and quoted):
|
2000-07-09 13:44:26 -03:00
|
|
|
|
break # emit current token
|
1998-12-22 01:19:29 -04:00
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
elif self.state in self.quotes:
|
2003-04-17 19:01:17 -03:00
|
|
|
|
quoted = True
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if not nextchar: # end of file
|
2001-01-08 23:01:15 -04:00
|
|
|
|
if self.debug >= 2:
|
|
|
|
|
print "shlex: I see EOF in quotes state"
|
|
|
|
|
# XXX what error should be raised here?
|
|
|
|
|
raise ValueError, "No closing quotation"
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if nextchar == self.state:
|
|
|
|
|
if not self.posix:
|
|
|
|
|
self.token = self.token + nextchar
|
|
|
|
|
self.state = ' '
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
self.state = 'a'
|
|
|
|
|
elif self.posix and nextchar in self.escape and \
|
|
|
|
|
self.state in self.escapedquotes:
|
|
|
|
|
escapedstate = self.state
|
|
|
|
|
self.state = nextchar
|
|
|
|
|
else:
|
|
|
|
|
self.token = self.token + nextchar
|
|
|
|
|
elif self.state in self.escape:
|
|
|
|
|
if not nextchar: # end of file
|
|
|
|
|
if self.debug >= 2:
|
|
|
|
|
print "shlex: I see EOF in escape state"
|
|
|
|
|
# XXX what error should be raised here?
|
|
|
|
|
raise ValueError, "No escaped character"
|
|
|
|
|
# In posix shells, only the quote itself or the escape
|
|
|
|
|
# character may be escaped within quotes.
|
|
|
|
|
if escapedstate in self.quotes and \
|
|
|
|
|
nextchar != self.state and nextchar != escapedstate:
|
|
|
|
|
self.token = self.token + self.state
|
|
|
|
|
self.token = self.token + nextchar
|
|
|
|
|
self.state = escapedstate
|
1998-12-22 01:19:29 -04:00
|
|
|
|
elif self.state == 'a':
|
|
|
|
|
if not nextchar:
|
2001-01-17 04:48:39 -04:00
|
|
|
|
self.state = None # end of file
|
1998-12-22 01:19:29 -04:00
|
|
|
|
break
|
|
|
|
|
elif nextchar in self.whitespace:
|
|
|
|
|
if self.debug >= 2:
|
2000-05-01 17:08:46 -03:00
|
|
|
|
print "shlex: I see whitespace in word state"
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.state = ' '
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.token or (self.posix and quoted):
|
2000-07-09 13:44:26 -03:00
|
|
|
|
break # emit current token
|
1998-12-22 01:19:29 -04:00
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
elif nextchar in self.commenters:
|
|
|
|
|
self.instream.readline()
|
|
|
|
|
self.lineno = self.lineno + 1
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.posix:
|
|
|
|
|
self.state = ' '
|
|
|
|
|
if self.token or (self.posix and quoted):
|
|
|
|
|
break # emit current token
|
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
elif self.posix and nextchar in self.quotes:
|
|
|
|
|
self.state = nextchar
|
|
|
|
|
elif self.posix and nextchar in self.escape:
|
|
|
|
|
escapedstate = 'a'
|
|
|
|
|
self.state = nextchar
|
|
|
|
|
elif nextchar in self.wordchars or nextchar in self.quotes \
|
|
|
|
|
or self.whitespace_split:
|
1998-12-22 01:19:29 -04:00
|
|
|
|
self.token = self.token + nextchar
|
|
|
|
|
else:
|
2004-01-29 02:37:52 -04:00
|
|
|
|
self.pushback.appendleft(nextchar)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
if self.debug >= 2:
|
2000-05-01 17:08:46 -03:00
|
|
|
|
print "shlex: I see punctuation in word state"
|
1999-03-22 11:28:08 -04:00
|
|
|
|
self.state = ' '
|
1998-12-22 01:19:29 -04:00
|
|
|
|
if self.token:
|
2000-07-09 13:44:26 -03:00
|
|
|
|
break # emit current token
|
1998-12-22 01:19:29 -04:00
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
result = self.token
|
|
|
|
|
self.token = ''
|
2003-04-17 18:31:33 -03:00
|
|
|
|
if self.posix and not quoted and result == '':
|
|
|
|
|
result = None
|
2000-05-01 17:08:46 -03:00
|
|
|
|
if self.debug > 1:
|
|
|
|
|
if result:
|
2004-02-12 13:35:32 -04:00
|
|
|
|
print "shlex: raw token=" + repr(result)
|
2000-05-01 17:08:46 -03:00
|
|
|
|
else:
|
|
|
|
|
print "shlex: raw token=EOF"
|
1998-12-22 01:19:29 -04:00
|
|
|
|
return result
|
|
|
|
|
|
2000-05-01 17:08:46 -03:00
|
|
|
|
def sourcehook(self, newfile):
|
|
|
|
|
"Hook called on a filename to be sourced."
|
|
|
|
|
if newfile[0] == '"':
|
|
|
|
|
newfile = newfile[1:-1]
|
2000-07-03 06:56:23 -03:00
|
|
|
|
# This implements cpp-like semantics for relative-path inclusion.
|
2003-04-17 20:09:08 -03:00
|
|
|
|
if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
|
2000-07-03 06:56:23 -03:00
|
|
|
|
newfile = os.path.join(os.path.dirname(self.infile), newfile)
|
2000-05-01 17:08:46 -03:00
|
|
|
|
return (newfile, open(newfile, "r"))
|
|
|
|
|
|
2000-05-01 17:14:12 -03:00
|
|
|
|
def error_leader(self, infile=None, lineno=None):
|
|
|
|
|
"Emit a C-compiler-like, Emacs-friendly error-message leader."
|
2002-06-01 21:40:05 -03:00
|
|
|
|
if infile is None:
|
2000-05-01 17:14:12 -03:00
|
|
|
|
infile = self.infile
|
2002-06-01 21:40:05 -03:00
|
|
|
|
if lineno is None:
|
2000-05-01 17:14:12 -03:00
|
|
|
|
lineno = self.lineno
|
|
|
|
|
return "\"%s\", line %d: " % (infile, lineno)
|
|
|
|
|
|
2003-04-17 18:31:33 -03:00
|
|
|
|
def __iter__(self):
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
|
token = self.get_token()
|
|
|
|
|
if token == self.eof:
|
|
|
|
|
raise StopIteration
|
|
|
|
|
return token
|
|
|
|
|
|
2007-05-24 13:49:29 -03:00
|
|
|
|
def split(s, comments=False, posix=True):
|
2007-05-24 14:33:33 -03:00
|
|
|
|
lex = shlex(s, posix=posix)
|
2003-04-19 22:57:03 -03:00
|
|
|
|
lex.whitespace_split = True
|
|
|
|
|
if not comments:
|
|
|
|
|
lex.commenters = ''
|
2003-04-17 18:31:33 -03:00
|
|
|
|
return list(lex)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
2001-01-17 04:48:39 -04:00
|
|
|
|
if __name__ == '__main__':
|
2000-07-03 06:56:23 -03:00
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
|
lexer = shlex()
|
|
|
|
|
else:
|
|
|
|
|
file = sys.argv[1]
|
|
|
|
|
lexer = shlex(open(file), file)
|
1998-12-22 01:19:29 -04:00
|
|
|
|
while 1:
|
|
|
|
|
tt = lexer.get_token()
|
2000-07-03 06:56:23 -03:00
|
|
|
|
if tt:
|
|
|
|
|
print "Token: " + repr(tt)
|
|
|
|
|
else:
|
1998-12-22 01:19:29 -04:00
|
|
|
|
break
|