comparison cmd2.py @ 288:e743cf74c518

hooray, fixed bad comment parser - all unit tests pass
author catherine@bothari
date Thu, 29 Oct 2009 13:31:41 -0400
parents 1cd23003e8d5
children 327ace491fa9
comparison
equal deleted inserted replaced
287:1cd23003e8d5 288:e743cf74c518
385 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 385 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
386 386
387 prefixParser = pyparsing.Empty() 387 prefixParser = pyparsing.Empty()
388 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) 388 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment])
389 commentGrammars.addParseAction(lambda x: '') 389 commentGrammars.addParseAction(lambda x: '')
390 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) 390 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(
391 pyparsing.stringEnd ^ '*/')
391 terminators = [';'] 392 terminators = [';']
392 blankLinesAllowed = False 393 blankLinesAllowed = False
393 multilineCommands = [] 394 multilineCommands = []
394 395
395 def _init_parser(self): 396 def _init_parser(self):
548 - args: command ends 549 - args: command ends
549 - multilineCommand: multiline 550 - multilineCommand: multiline
550 - terminator: ['\n', '\n'] 551 - terminator: ['\n', '\n']
551 - terminator: ['\n', '\n'] 552 - terminator: ['\n', '\n']
552 ''' 553 '''
554 tstr = 'multiline command /* with comment complete */ is done;'
553 outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output') 555 outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output')
554 556
555 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') 557 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator')
556 stringEnd = pyparsing.stringEnd ^ '\nEOF' 558 stringEnd = pyparsing.stringEnd ^ '\nEOF'
557 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand') 559 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand')
573 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + SkipToLast(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + 575 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + SkipToLast(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') +
574 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) 576 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements)
575 self.multilineParser.ignore(self.commentInProgress) 577 self.multilineParser.ignore(self.commentInProgress)
576 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + 578 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') +
577 pyparsing.Optional(terminatorParser) + afterElements) 579 pyparsing.Optional(terminatorParser) + afterElements)
578 self.multilineParser = self.multilineParser.setResultsName('multilineParser') 580 #self.multilineParser = self.multilineParser.setResultsName('multilineParser')
579 self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser') 581 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser')
580 self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('blankLineTerminatorParser') 582 self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('statement')
581 self.parser = self.prefixParser + ( 583 self.parser = self.prefixParser + (
582 stringEnd | 584 stringEnd |
583 self.multilineParser | 585 self.multilineParser |
584 self.singleLineParser | 586 self.singleLineParser |
585 self.blankLineTerminationParser | 587 self.blankLineTerminationParser |
986 self.pfeedback('Saved to %s' % (fname)) 988 self.pfeedback('Saved to %s' % (fname))
987 except Exception, e: 989 except Exception, e:
988 self.perror('Error saving %s: %s' % (fname, str(e))) 990 self.perror('Error saving %s: %s' % (fname, str(e)))
989 991
990 def read_file_or_url(self, fname): 992 def read_file_or_url(self, fname):
993 # TODO: not working on localhost
991 if isinstance(fname, file): 994 if isinstance(fname, file):
992 result = open(fname, 'r') 995 result = open(fname, 'r')
993 else: 996 else:
994 match = self.urlre.match(fname) 997 match = self.urlre.match(fname)
995 if match: 998 if match: