changeset 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
files cmd2.py
diffstat 1 files changed, 30 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Sun Jul 31 16:57:15 2011 -0400
+++ b/cmd2.py	Sun Aug 21 02:22:13 2011 -0400
@@ -648,15 +648,40 @@
           - multilineCommand: multiline
           - terminator: ['\n', '\n']
         - terminator: ['\n', '\n']
+        >>> print (c.parser.parseString('multiline command "with term; ends" now\n\n').dump())
+        ['multiline', 'command ends', '\n', '\n']
+        - args: command "with term; ends" now
+        - multilineCommand: multiline
+        - statement: ['multiline', '"with term; ends" now', '\n', '\n']
+          - args: command "with term; ends" now
+          - multilineCommand: multiline
+          - terminator: ['\n', '\n']
+        - terminator: ['\n', '\n']
+        >>> print (c.parser.parseString('what if  "ending is in quotes" but').dump())
+        ['what', 'if  "ending is in quotes" but']
+        - args: if  "ending is in quotes" but
+        - command: what
+        - statement: ['what', 'if  "ending is in quotes" but']
+          - args: if  "ending is in quotes" but
+          - command: what
+        >>> print (c.parser.parseString('what if  "ending is in quotes"').dump())
+        ['what', 'if  "ending is in quotes"']
+        - args: if  "ending is in quotes"
+        - command: what
+        - statement: ['what', 'if  "ending is in quotes"']
+          - args: if  "ending is in quotes"
+          - command: what
         '''
         outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output')
         
         terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator')
+        terminatorParser.ignore(pyparsing.quotedString) #fail
         stringEnd = pyparsing.stringEnd ^ '\nEOF'
         self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand')
         oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command')
         pipe = pyparsing.Keyword('|', identChars='|')
-        self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '')        
+        self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '')
+        doNotParse = self.commentGrammars | commentInProgress 
         afterElements = \
             pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
             pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
@@ -684,7 +709,8 @@
             self.blankLineTerminationParser | 
             self.multilineCommand + pyparsing.SkipTo(stringEnd)
             )
-        self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars)
+        #self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars)
+        self.parser.ignore(self.commentGrammars)
         
         inputMark = pyparsing.Literal('<')
         inputMark.setParseAction(lambda x: '')
@@ -695,7 +721,8 @@
         # as in "lesser than"
         self.inputParser = inputMark + pyparsing.Optional(inputFrom) + pyparsing.Optional('>') + \
                            pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|')
-        self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)               
+        #self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)               
+        self.inputParser.ignore(self.commentGrammars).ignore(self.commentInProgress)               
     
     def preparse(self, raw, **kwargs):
         return raw