comparison cmd2.py @ 433:5a814cb007ae

new unit tests for better quote/terminator handling
author catherine.devlin@gmail.com
date Sun, 21 Aug 2011 02:22:13 -0400
parents e4d6edca469b
children 742fd308f0c9
comparison
equal deleted inserted replaced
432:e4d6edca469b 433:5a814cb007ae
646 - statement: ['multiline', 'command ends', '\n', '\n'] 646 - statement: ['multiline', 'command ends', '\n', '\n']
647 - args: command ends 647 - args: command ends
648 - multilineCommand: multiline 648 - multilineCommand: multiline
649 - terminator: ['\n', '\n'] 649 - terminator: ['\n', '\n']
650 - terminator: ['\n', '\n'] 650 - terminator: ['\n', '\n']
651 >>> print (c.parser.parseString('multiline command "with term; ends" now\n\n').dump())
652 ['multiline', 'command ends', '\n', '\n']
653 - args: command "with term; ends" now
654 - multilineCommand: multiline
655 - statement: ['multiline', '"with term; ends" now', '\n', '\n']
656 - args: command "with term; ends" now
657 - multilineCommand: multiline
658 - terminator: ['\n', '\n']
659 - terminator: ['\n', '\n']
660 >>> print (c.parser.parseString('what if "ending is in quotes" but').dump())
661 ['what', 'if "ending is in quotes" but']
662 - args: if "ending is in quotes" but
663 - command: what
664 - statement: ['what', 'if "ending is in quotes" but']
665 - args: if "ending is in quotes" but
666 - command: what
667 >>> print (c.parser.parseString('what if "ending is in quotes"').dump())
668 ['what', 'if "ending is in quotes"']
669 - args: if "ending is in quotes"
670 - command: what
671 - statement: ['what', 'if "ending is in quotes"']
672 - args: if "ending is in quotes"
673 - command: what
651 ''' 674 '''
652 outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output') 675 outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output')
653 676
654 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') 677 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator')
678 terminatorParser.ignore(pyparsing.quotedString) #fail
655 stringEnd = pyparsing.stringEnd ^ '\nEOF' 679 stringEnd = pyparsing.stringEnd ^ '\nEOF'
656 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand') 680 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand')
657 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command') 681 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command')
658 pipe = pyparsing.Keyword('|', identChars='|') 682 pipe = pyparsing.Keyword('|', identChars='|')
659 self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '') 683 self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '')
684 doNotParse = self.commentGrammars | commentInProgress
660 afterElements = \ 685 afterElements = \
661 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 686 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
662 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) 687 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
663 if self.case_insensitive: 688 if self.case_insensitive:
664 self.multilineCommand.setParseAction(lambda x: x[0].lower()) 689 self.multilineCommand.setParseAction(lambda x: x[0].lower())
682 self.multilineParser | 707 self.multilineParser |
683 self.singleLineParser | 708 self.singleLineParser |
684 self.blankLineTerminationParser | 709 self.blankLineTerminationParser |
685 self.multilineCommand + pyparsing.SkipTo(stringEnd) 710 self.multilineCommand + pyparsing.SkipTo(stringEnd)
686 ) 711 )
687 self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars) 712 #self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars)
713 self.parser.ignore(self.commentGrammars)
688 714
689 inputMark = pyparsing.Literal('<') 715 inputMark = pyparsing.Literal('<')
690 inputMark.setParseAction(lambda x: '') 716 inputMark.setParseAction(lambda x: '')
691 fileName = pyparsing.Word(self.legalChars + '/\\') 717 fileName = pyparsing.Word(self.legalChars + '/\\')
692 inputFrom = fileName('inputFrom') 718 inputFrom = fileName('inputFrom')
693 inputFrom.setParseAction(replace_with_file_contents) 719 inputFrom.setParseAction(replace_with_file_contents)
694 # a not-entirely-satisfactory way of distinguishing < as in "import from" from < 720 # a not-entirely-satisfactory way of distinguishing < as in "import from" from <
695 # as in "lesser than" 721 # as in "lesser than"
696 self.inputParser = inputMark + pyparsing.Optional(inputFrom) + pyparsing.Optional('>') + \ 722 self.inputParser = inputMark + pyparsing.Optional(inputFrom) + pyparsing.Optional('>') + \
697 pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|') 723 pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|')
698 self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 724 #self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
725 self.inputParser.ignore(self.commentGrammars).ignore(self.commentInProgress)
699 726
700 def preparse(self, raw, **kwargs): 727 def preparse(self, raw, **kwargs):
701 return raw 728 return raw
702 def postparse(self, parseResult): 729 def postparse(self, parseResult):
703 return parseResult 730 return parseResult