comparison cmd2.py @ 186:bee79220382c

unit tests passing
author catherine@Elli.myhome.westell.com
date Wed, 28 Jan 2009 09:08:33 -0500
parents 0e9ecf8f5520
children 183dd2fffec5
comparison
equal deleted inserted replaced
185:0e9ecf8f5520 186:bee79220382c
235 r''' 235 r'''
236 >>> c = Cmd() 236 >>> c = Cmd()
237 >>> c.multilineCommands = ['multiline'] 237 >>> c.multilineCommands = ['multiline']
238 >>> c.caseInsensitive = True 238 >>> c.caseInsensitive = True
239 >>> c._init_parser() 239 >>> c._init_parser()
240 >>> print c.parser.parseString('multiline command ends\n\n').dump()
241 ['multiline', 'command ends', ';', '']
242 - args: command ends
243 - multilineCommand: multiline
244 - statement: ['multiline', 'command ends', ';']
245 - args: command ends
246 - multilineCommand: multiline
247 - terminator: ;
248 - terminator: ;
249 >>> print c.parser.parseString('').dump() 240 >>> print c.parser.parseString('').dump()
250 [] 241 []
251 >>> print c.parser.parseString('/* empty command */').dump() 242 >>> print c.parser.parseString('/* empty command */').dump()
252 [] 243 []
253 >>> print c.parser.parseString('plainword').dump() 244 >>> print c.parser.parseString('plainword').dump()
379 - statement: ['multiline', 'command /* with comment complete */ is done', ';'] 370 - statement: ['multiline', 'command /* with comment complete */ is done', ';']
380 - args: command /* with comment complete */ is done 371 - args: command /* with comment complete */ is done
381 - multilineCommand: multiline 372 - multilineCommand: multiline
382 - terminator: ; 373 - terminator: ;
383 - terminator: ; 374 - terminator: ;
375 >>> print c.parser.parseString('multiline command ends\n\n').dump()
376 ['multiline', 'command ends', '\n', '\n']
377 - args: command ends
378 - multilineCommand: multiline
379 - statement: ['multiline', 'command ends', '\n', '\n']
380 - args: command ends
381 - multilineCommand: multiline
382 - terminator: ['\n', '\n']
383 - terminator: ['\n', '\n']
384 ''' 384 '''
385 outputParser = pyparsing.oneOf(['>>','>'])('output') 385 outputParser = pyparsing.oneOf(['>>','>'])('output')
386 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') 386 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator')
387 stringEnd = pyparsing.stringEnd ^ '\nEOF' 387 stringEnd = pyparsing.stringEnd ^ '\nEOF'
388 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') 388 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand')
389 oneLineCommand = pyparsing.Word(self.legalChars)('command') 389 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command')
390 pipe = pyparsing.Keyword('|', identChars='|') 390 pipe = pyparsing.Keyword('|', identChars='|')
391 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') 391 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
392 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) 392 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment)
393 afterElements = \ 393 afterElements = \
394 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 394 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
397 self.multilineCommand.setParseAction(lambda x: x[0].lower()) 397 self.multilineCommand.setParseAction(lambda x: x[0].lower())
398 oneLineCommand.setParseAction(lambda x: x[0].lower()) 398 oneLineCommand.setParseAction(lambda x: x[0].lower())
399 if self.blankLinesAllowed: 399 if self.blankLinesAllowed:
400 self.blankLineTerminationParser = pyparsing.NoMatch 400 self.blankLineTerminationParser = pyparsing.NoMatch
401 else: 401 else:
402 self.blankLineTerminator = (pyparsing.lineEnd + pyparsing.lineEnd + pyparsing.lineEnd + pyparsing.lineEnd)('terminator') 402 self.blankLineTerminator = (pyparsing.lineEnd + pyparsing.lineEnd)('terminator')
403 self.blankLineTerminator.setResultsName('terminator') 403 self.blankLineTerminator.setResultsName('terminator')
404 self.blankLineTerminationParser = ((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(self.blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + self.blankLineTerminator)('statement') 404 self.blankLineTerminationParser = ((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(self.blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + self.blankLineTerminator)('statement')
405 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + 405 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') +
406 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) 406 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements)
407 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + 407 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') +
408 pyparsing.Optional(terminatorParser) + afterElements) 408 pyparsing.Optional(terminatorParser) + afterElements)
409 self.multilineParser = self.multilineParser.setParseName('multilineParser') 409 #self.multilineParser = self.multilineParser.setResultsName('multilineParser')
410 self.singleLineParser = self.singleLineParser.setParseName('singleLineParser') 410 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser')
411 self.blankLineTerminationParser = self.blankLineTerminationParser.setParseName('blankLineTerminatorParser') 411 #self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('blankLineTerminatorParser')
412 self.parser = ( 412 self.parser = (
413 stringEnd | 413 stringEnd |
414 self.multilineParser | 414 self.multilineParser |
415 self.singleLineParser | 415 self.singleLineParser |
416 self.blankLineTerminationParser | 416 self.blankLineTerminationParser |