comparison cmd2.py @ 184:d1a87c14675b

putting parsing variables under self
author catherine@Elli.myhome.westell.com
date Tue, 27 Jan 2009 17:04:59 -0500
parents 403e1c3ffc4a
children 0e9ecf8f5520
comparison
equal deleted inserted replaced
183:403e1c3ffc4a 184:d1a87c14675b
230 terminators = [';'] 230 terminators = [';']
231 blankLinesAllowed = False 231 blankLinesAllowed = False
232 multilineCommands = [] 232 multilineCommands = []
233 233
234 def _init_parser(self): 234 def _init_parser(self):
235 ''' 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: ;
240 >>> print c.parser.parseString('').dump() 249 >>> print c.parser.parseString('').dump()
241 [] 250 []
242 >>> print c.parser.parseString('/* empty command */').dump() 251 >>> print c.parser.parseString('/* empty command */').dump()
243 [] 252 []
244 >>> print c.parser.parseString('plainword').dump() 253 >>> print c.parser.parseString('plainword').dump()
374 - terminator: ; 383 - terminator: ;
375 ''' 384 '''
376 outputParser = pyparsing.oneOf(['>>','>'])('output') 385 outputParser = pyparsing.oneOf(['>>','>'])('output')
377 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')
378 stringEnd = pyparsing.stringEnd ^ '\nEOF' 387 stringEnd = pyparsing.stringEnd ^ '\nEOF'
379 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')
380 oneLineCommand = pyparsing.Word(self.legalChars)('command') 389 oneLineCommand = pyparsing.Word(self.legalChars)('command')
381 pipe = pyparsing.Keyword('|', identChars='|') 390 pipe = pyparsing.Keyword('|', identChars='|')
382 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') 391 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
383 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) 392 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment)
384 afterElements = \ 393 afterElements = \
385 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 394 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
386 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) 395 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
387 if self.caseInsensitive: 396 if self.caseInsensitive:
388 multilineCommand.setParseAction(lambda x: x[0].lower()) 397 self.multilineCommand.setParseAction(lambda x: x[0].lower())
389 oneLineCommand.setParseAction(lambda x: x[0].lower()) 398 oneLineCommand.setParseAction(lambda x: x[0].lower())
390 if self.blankLinesAllowed: 399 if self.blankLinesAllowed:
391 blankLineTerminationParser = pyparsing.NoMatch 400 self.blankLineTerminationParser = pyparsing.NoMatch
392 else: 401 else:
393 blankLineTerminator = (pyparsing.lineEnd('terminator') + stringEnd) 402 self.blankLineTerminator = (pyparsing.lineEnd + pyparsing.lineEnd + pyparsing.lineEnd + pyparsing.lineEnd)('terminator')
394 blankLineTerminationParser = ((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + blankLineTerminator)('statement') 403 self.blankLineTerminator.setResultsName('terminator')
395 multilineParser = (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + 404 self.blankLineTerminationParser = ((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(self.blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + self.blankLineTerminator)('statement')
396 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) 405 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') +
397 singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + 406 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements)
398 pyparsing.Optional(terminatorParser) + afterElements) 407 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') +
408 pyparsing.Optional(terminatorParser) + afterElements)
399 self.parser = ( 409 self.parser = (
400 stringEnd | 410 stringEnd |
401 multilineParser | 411 self.multilineParser |
402 blankLineTerminationParser | 412 self.singleLineParser |
403 multilineCommand + pyparsing.SkipTo(stringEnd) | 413 self.blankLineTerminationParser |
404 singleLineParser 414 self.multilineCommand + pyparsing.SkipTo(stringEnd)
405 ) 415 )
406 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 416 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
407 417
408 inputMark = pyparsing.Literal('<') 418 inputMark = pyparsing.Literal('<')
409 inputMark.setParseAction(lambda x: '') 419 inputMark.setParseAction(lambda x: '')