annotate Lib/shlex.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 6f8f82280545
children e54ee8d2c16b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
1 # -*- coding: iso-8859-1 -*-
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10829
diff changeset
2 """A lexical analyzer class for simple shell-like syntaxes."""
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10829
diff changeset
3
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15916
diff changeset
4 # Module and documentation by Eric S. Raymond, 21 Dec 1998
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
5 # Input stacking and error message cleanup added by ESR, March 2000
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15916
diff changeset
6 # push_source() and pop_source() made explicit by ESR, January 2001.
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
7 # Posix compliance, split(), string arguments, and
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
8 # iterator interface by Gustavo Niemeyer, April 2003.
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
10 import os.path
10829
391a9cb2b072 No need to import os or string. (Andrew Dalke & kjpylint)
Guido van Rossum <guido@python.org>
parents: 10556
diff changeset
11 import sys
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
12 from collections import deque
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
14 try:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
15 from cStringIO import StringIO
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
16 except ImportError:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
17 from StringIO import StringIO
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
18
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
19 __all__ = ["shlex", "split"]
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15930
diff changeset
20
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 class shlex:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15916
diff changeset
22 "A lexical analyzer class for simple shell-like syntaxes."
28659
ee165fe1b42f - use Tue/False for booleans
Fred Drake <fdrake@acm.org>
parents: 28656
diff changeset
23 def __init__(self, instream=None, infile=None, posix=False):
28666
a938b13e0291 Use True in a few more places.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28659
diff changeset
24 if isinstance(instream, basestring):
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
25 instream = StringIO(instream)
23627
a6a765c7d33b Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 17557
diff changeset
26 if instream is not None:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 self.instream = instream
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
28 self.infile = infile
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 self.instream = sys.stdin
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
31 self.infile = None
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
32 self.posix = posix
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
33 if posix:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
34 self.eof = None
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
35 else:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
36 self.eof = ''
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 self.commenters = '#'
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
38 self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
39 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
40 if self.posix:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
41 self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
42 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ')
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 self.whitespace = ' \t\r\n'
28659
ee165fe1b42f - use Tue/False for booleans
Fred Drake <fdrake@acm.org>
parents: 28656
diff changeset
44 self.whitespace_split = False
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 self.quotes = '\'"'
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
46 self.escape = '\\'
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
47 self.escapedquotes = '"'
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 self.state = ' '
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
49 self.pushback = deque()
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 self.lineno = 1
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 self.debug = 0
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 self.token = ''
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
53 self.filestack = deque()
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
54 self.source = None
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
55 if self.debug:
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
56 print 'shlex: reading from %s, line %d' \
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
57 % (self.instream, self.lineno)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def push_token(self, tok):
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 "Push a token onto the stack popped by the get_token method"
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
61 if self.debug >= 1:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31436
diff changeset
62 print "shlex: pushing token " + repr(tok)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
63 self.pushback.appendleft(tok)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
65 def push_source(self, newstream, newfile=None):
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
66 "Push an input source onto the lexer's input source stack."
28666
a938b13e0291 Use True in a few more places.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28659
diff changeset
67 if isinstance(newstream, basestring):
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
68 newstream = StringIO(newstream)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
69 self.filestack.appendleft((self.infile, self.instream, self.lineno))
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
70 self.infile = newfile
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
71 self.instream = newstream
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
72 self.lineno = 1
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
73 if self.debug:
23627
a6a765c7d33b Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 17557
diff changeset
74 if newfile is not None:
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
75 print 'shlex: pushing to file %s' % (self.infile,)
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
76 else:
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
77 print 'shlex: pushing to stream %s' % (self.instream,)
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
78
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
79 def pop_source(self):
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
80 "Pop the input source stack."
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
81 self.instream.close()
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
82 (self.infile, self.instream, self.lineno) = self.filestack.popleft()
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
83 if self.debug:
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
84 print 'shlex: popping to %s, line %d' \
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
85 % (self.instream, self.lineno)
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
86 self.state = ' '
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
87
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def get_token(self):
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
89 "Get a token from the input stream (or from stack if it's nonempty)"
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if self.pushback:
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
91 tok = self.pushback.popleft()
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
92 if self.debug >= 1:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31436
diff changeset
93 print "shlex: popping token " + repr(tok)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return tok
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
95 # No pushback. Get a token.
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
96 raw = self.read_token()
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
97 # Handle inclusions
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
98 if self.source is not None:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
99 while raw == self.source:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
100 spec = self.sourcehook(self.read_token())
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
101 if spec:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
102 (newfile, newstream) = spec
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
103 self.push_source(newstream, newfile)
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
104 raw = self.get_token()
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
105 # Maybe we got EOF instead?
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
106 while raw == self.eof:
28659
ee165fe1b42f - use Tue/False for booleans
Fred Drake <fdrake@acm.org>
parents: 28656
diff changeset
107 if not self.filestack:
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
108 return self.eof
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
109 else:
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
110 self.pop_source()
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
111 raw = self.get_token()
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
112 # Neither inclusion nor EOF
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
113 if self.debug >= 1:
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
114 if raw != self.eof:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31436
diff changeset
115 print "shlex: token=" + repr(raw)
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
116 else:
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
117 print "shlex: token=EOF"
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
118 return raw
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
119
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
120 def read_token(self):
28659
ee165fe1b42f - use Tue/False for booleans
Fred Drake <fdrake@acm.org>
parents: 28656
diff changeset
121 quoted = False
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
122 escapedstate = ' '
28666
a938b13e0291 Use True in a few more places.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28659
diff changeset
123 while True:
15655
83d599652b62 Remove superfluous semicolons
Andrew M. Kuchling <amk@amk.ca>
parents: 13475
diff changeset
124 nextchar = self.instream.read(1)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 if nextchar == '\n':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 self.lineno = self.lineno + 1
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if self.debug >= 3:
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
128 print "shlex: in state", repr(self.state), \
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15916
diff changeset
129 "I see character:", repr(nextchar)
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
130 if self.state is None:
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
131 self.token = '' # past end of file
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
132 break
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 elif self.state == ' ':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if not nextchar:
15916
ae636b8ac480 Make pop_source and push_source available, as documented.
Eric S. Raymond <esr@thyrsus.com>
parents: 15872
diff changeset
135 self.state = None # end of file
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 break
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 elif nextchar in self.whitespace:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if self.debug >= 2:
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
139 print "shlex: I see whitespace in whitespace state"
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
140 if self.token or (self.posix and quoted):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
141 break # emit current token
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 continue
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 elif nextchar in self.commenters:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 self.instream.readline()
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 self.lineno = self.lineno + 1
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
147 elif self.posix and nextchar in self.escape:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
148 escapedstate = 'a'
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
149 self.state = nextchar
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 elif nextchar in self.wordchars:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 self.token = nextchar
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 self.state = 'a'
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 elif nextchar in self.quotes:
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
154 if not self.posix:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
155 self.token = nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
156 self.state = nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
157 elif self.whitespace_split:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 self.token = nextchar
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
159 self.state = 'a'
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 self.token = nextchar
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
162 if self.token or (self.posix and quoted):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
163 break # emit current token
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 continue
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 elif self.state in self.quotes:
28659
ee165fe1b42f - use Tue/False for booleans
Fred Drake <fdrake@acm.org>
parents: 28656
diff changeset
167 quoted = True
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
168 if not nextchar: # end of file
15769
fb143fb236d0 Patch #102953: Fix bug #125452, where shlex.shlex hangs when it
Andrew M. Kuchling <amk@amk.ca>
parents: 15655
diff changeset
169 if self.debug >= 2:
fb143fb236d0 Patch #102953: Fix bug #125452, where shlex.shlex hangs when it
Andrew M. Kuchling <amk@amk.ca>
parents: 15655
diff changeset
170 print "shlex: I see EOF in quotes state"
fb143fb236d0 Patch #102953: Fix bug #125452, where shlex.shlex hangs when it
Andrew M. Kuchling <amk@amk.ca>
parents: 15655
diff changeset
171 # XXX what error should be raised here?
fb143fb236d0 Patch #102953: Fix bug #125452, where shlex.shlex hangs when it
Andrew M. Kuchling <amk@amk.ca>
parents: 15655
diff changeset
172 raise ValueError, "No closing quotation"
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
173 if nextchar == self.state:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
174 if not self.posix:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
175 self.token = self.token + nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
176 self.state = ' '
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
177 break
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
178 else:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
179 self.state = 'a'
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
180 elif self.posix and nextchar in self.escape and \
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
181 self.state in self.escapedquotes:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
182 escapedstate = self.state
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
183 self.state = nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
184 else:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
185 self.token = self.token + nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
186 elif self.state in self.escape:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
187 if not nextchar: # end of file
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
188 if self.debug >= 2:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
189 print "shlex: I see EOF in escape state"
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
190 # XXX what error should be raised here?
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
191 raise ValueError, "No escaped character"
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
192 # In posix shells, only the quote itself or the escape
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
193 # character may be escaped within quotes.
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
194 if escapedstate in self.quotes and \
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
195 nextchar != self.state and nextchar != escapedstate:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
196 self.token = self.token + self.state
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
197 self.token = self.token + nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
198 self.state = escapedstate
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 elif self.state == 'a':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 if not nextchar:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15916
diff changeset
201 self.state = None # end of file
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 break
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 elif nextchar in self.whitespace:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 if self.debug >= 2:
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
205 print "shlex: I see whitespace in word state"
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 self.state = ' '
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
207 if self.token or (self.posix and quoted):
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
208 break # emit current token
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 continue
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 elif nextchar in self.commenters:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 self.instream.readline()
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 self.lineno = self.lineno + 1
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
214 if self.posix:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
215 self.state = ' '
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
216 if self.token or (self.posix and quoted):
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
217 break # emit current token
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
218 else:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
219 continue
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
220 elif self.posix and nextchar in self.quotes:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
221 self.state = nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
222 elif self.posix and nextchar in self.escape:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
223 escapedstate = 'a'
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
224 self.state = nextchar
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
225 elif nextchar in self.wordchars or nextchar in self.quotes \
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
226 or self.whitespace_split:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 self.token = self.token + nextchar
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 else:
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 28703
diff changeset
229 self.pushback.appendleft(nextchar)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 if self.debug >= 2:
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
231 print "shlex: I see punctuation in word state"
10438
ee6fa9b86885 Bug reported by Tobias Thelen: missing "self." in assignment target.
Guido van Rossum <guido@python.org>
parents: 9689
diff changeset
232 self.state = ' '
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 if self.token:
13475
2ff2785e5291 Make tabnanny happy.
Fred Drake <fdrake@acm.org>
parents: 13318
diff changeset
234 break # emit current token
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 continue
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 result = self.token
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 self.token = ''
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
239 if self.posix and not quoted and result == '':
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
240 result = None
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
241 if self.debug > 1:
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
242 if result:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31436
diff changeset
243 print "shlex: raw token=" + repr(result)
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
244 else:
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
245 print "shlex: raw token=EOF"
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 return result
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
248 def sourcehook(self, newfile):
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
249 "Hook called on a filename to be sourced."
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
250 if newfile[0] == '"':
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
251 newfile = newfile[1:-1]
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
252 # This implements cpp-like semantics for relative-path inclusion.
28666
a938b13e0291 Use True in a few more places.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28659
diff changeset
253 if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
254 newfile = os.path.join(os.path.dirname(self.infile), newfile)
12540
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
255 return (newfile, open(newfile, "r"))
81a121d21340 Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
256
12541
c34ba3e3cfcd Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 12540
diff changeset
257 def error_leader(self, infile=None, lineno=None):
c34ba3e3cfcd Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 12540
diff changeset
258 "Emit a C-compiler-like, Emacs-friendly error-message leader."
23627
a6a765c7d33b Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 17557
diff changeset
259 if infile is None:
12541
c34ba3e3cfcd Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 12540
diff changeset
260 infile = self.infile
23627
a6a765c7d33b Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 17557
diff changeset
261 if lineno is None:
12541
c34ba3e3cfcd Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 12540
diff changeset
262 lineno = self.lineno
c34ba3e3cfcd Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 12540
diff changeset
263 return "\"%s\", line %d: " % (infile, lineno)
c34ba3e3cfcd Eric Raymond:
Guido van Rossum <guido@python.org>
parents: 12540
diff changeset
264
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
265 def __iter__(self):
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
266 return self
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
267
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
268 def next(self):
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
269 token = self.get_token()
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
270 if token == self.eof:
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
271 raise StopIteration
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
272 return token
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
273
41848
cf151967eea3 shlex.split() now has an optional "posix" parameter.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
274 def split(s, comments=False, posix=True):
41849
6f8f82280545 Fix parameter passing.
Georg Brandl <georg@python.org>
parents: 41848
diff changeset
275 lex = shlex(s, posix=posix)
28703
2ce39a7be79d - Changed shlex.split() method to have more useful and
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28666
diff changeset
276 lex.whitespace_split = True
2ce39a7be79d - Changed shlex.split() method to have more useful and
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28666
diff changeset
277 if not comments:
2ce39a7be79d - Changed shlex.split() method to have more useful and
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28666
diff changeset
278 lex.commenters = ''
28656
4d2259cb77a0 Implemented posix-mode parsing support in shlex.py, as dicussed in
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23627
diff changeset
279 return list(lex)
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
280
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15916
diff changeset
281 if __name__ == '__main__':
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
282 if len(sys.argv) == 1:
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
283 lexer = shlex()
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
284 else:
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
285 file = sys.argv[1]
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
286 lexer = shlex(open(file), file)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 while 1:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 tt = lexer.get_token()
13318
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
289 if tt:
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
290 print "Token: " + repr(tt)
cd8a41bf4ac2 Eric S. Raymond <esr@thyrsus.com>:
Fred Drake <fdrake@acm.org>
parents: 12541
diff changeset
291 else:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 break