comparison cmd2.py @ 135:7c0a89fccf2b

broken; midway through comments
author catherine@Elli.myhome.westell.com
date Mon, 10 Nov 2008 13:10:38 -0500
parents c28ae4f75c15
children 67558b85ab38
comparison
equal deleted inserted replaced
134:c28ae4f75c15 135:7c0a89fccf2b
150 150
151 class Cmd(cmd.Cmd): 151 class Cmd(cmd.Cmd):
152 echo = False 152 echo = False
153 caseInsensitive = True 153 caseInsensitive = True
154 multilineCommands = ['_multiline_comment'] 154 multilineCommands = ['_multiline_comment']
155 commentGrammars = [pyparsing.cStyleComment, pyparsing.pythonStyleComment]
155 continuationPrompt = '> ' 156 continuationPrompt = '> '
156 shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '/*': '_multiline_comment', '#': '_comment'} 157 shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '/*': '_multiline_comment', '#': '_comment'}
157 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 158 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
158 noSpecialParse = 'set ed edit exit'.split() 159 noSpecialParse = 'set ed edit exit'.split()
159 defaultExtension = 'txt' 160 defaultExtension = 'txt'
201 def do_shortcuts(self, args): 202 def do_shortcuts(self, args):
202 """Lists single-key shortcuts available.""" 203 """Lists single-key shortcuts available."""
203 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 204 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
204 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 205 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
205 206
206 commentRemover = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement')
207 commentRemover.suppress(pyparsing.pythonStyleComment)
208 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') } 207 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') }
209 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n')) 208 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n'))
210 ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator') 209 ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator')
211 argSeparatorPattern = pyparsing.Word(pyparsing.printables)('command') \ 210 argSeparatorPattern = pyparsing.Word(pyparsing.printables)('command') \
212 + pyparsing.SkipTo(pyparsing.StringEnd())('args') 211 + pyparsing.SkipTo(pyparsing.StringEnd())('args')
218 redirectInPattern = pyparsing.Literal('<')('input') \ 217 redirectInPattern = pyparsing.Literal('<')('input') \
219 + pyparsing.Optional(filenamePattern)('inputFrom') 218 + pyparsing.Optional(filenamePattern)('inputFrom')
220 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern 219 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern
221 for p in (terminatorPattern, pipePattern, redirectInPattern, redirectOutPattern, punctuationPattern): 220 for p in (terminatorPattern, pipePattern, redirectInPattern, redirectOutPattern, punctuationPattern):
222 p.ignore(pyparsing.sglQuotedString) 221 p.ignore(pyparsing.sglQuotedString)
223 p.ignore(pyparsing.dblQuotedString) 222 p.ignore(pyparsing.dblQuotedString)
223 p.ignore(pyparsing.Or(commentGrammars))
224 224
225 def parsed(self, s): 225 def parsed(self, s):
226 ''' 226 '''
227 >>> c = Cmd() 227 >>> c = Cmd()
228 >>> r = c.parsed('quotes "are > ignored" < inp.txt') 228 >>> r = c.parsed('quotes "are > ignored" < inp.txt')
243 >>> r.statement, r.input, r.inputFrom 243 >>> r.statement, r.input, r.inputFrom
244 ('got from ', '<', 'thisfile.txt') 244 ('got from ', '<', 'thisfile.txt')
245 ''' 245 '''
246 if isinstance(s, pyparsing.ParseResults): 246 if isinstance(s, pyparsing.ParseResults):
247 return s 247 return s
248 result = self.commentRemover.parseString(s) 248 result = (pyparsing.SkipTo(pyparsing.StringEnd()))("fullStatement").parseString(s)
249 command = s.split()[0] 249 command = s.split()[0]
250 if self.caseInsensitive: 250 if self.caseInsensitive:
251 command = command.lower() 251 command = command.lower()
252 if command in self.noSpecialParse: 252 if command in self.noSpecialParse:
253 result['command'] = command 253 result['command'] = command
254 result['statement'] = result.fullStatement 254 result['statement'] = result.fullStatement
255 return result 255 return result
256
256 if s[0] in self.shortcuts: 257 if s[0] in self.shortcuts:
257 s = self.shortcuts[s[0]] + ' ' + s[1:] 258 s = self.shortcuts[s[0]] + ' ' + s[1:]
258 result['statement'] = s 259 result['statement'] = s
259 result['parseable'] = s 260 result['parseable'] = s
260 terminator = self.specialTerminators.get(command) or self.terminatorPattern 261 # terminator = self.specialTerminators.get(command) or self.terminatorPattern
261 result += parseSearchResults(terminator, s) 262 result += parseSearchResults(self.terminatorPattern, s)
262 if result.terminator: 263 if result.terminator:
263 result['statement'] = result.upToIncluding 264 result['statement'] = result.upToIncluding
264 result['unterminated'] = result.before 265 result['unterminated'] = result.before
265 result['parseable'] = result.after 266 result['parseable'] = result.after
266 else: 267 else:
294 commands by the interpreter should stop. 295 commands by the interpreter should stop.
295 296
296 """ 297 """
297 line = line.strip() 298 line = line.strip()
298 if not line: 299 if not line:
300 return
301 if not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line):
299 return 302 return
300 statement = self.parsed(line) 303 statement = self.parsed(line)
301 while (statement.command in self.multilineCommands) and not \ 304 while (statement.command in self.multilineCommands) and not \
302 (statement.terminator or assumeComplete): 305 (statement.terminator or assumeComplete):
303 statement = self.parsed('%s\n%s' % (statement.fullStatement, 306 statement = self.parsed('%s\n%s' % (statement.fullStatement,