comparison cmd2.py @ 169:429c8e984df4

all working, except \n\n terminators
author catherine@dellzilla
date Wed, 10 Dec 2008 14:50:42 -0500
parents 786ab5a83eea
children 310ebf4baa7a
comparison
equal deleted inserted replaced
168:786ab5a83eea 169:429c8e984df4
180 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } 180 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' }
181 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 181 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
182 noSpecialParse = 'set ed edit exit'.split() 182 noSpecialParse = 'set ed edit exit'.split()
183 defaultExtension = 'txt' 183 defaultExtension = 'txt'
184 defaultFileName = 'command.txt' 184 defaultFileName = 'command.txt'
185 singleQuotedStrings = True
186 doubleQuotedStrings = True
187
185 editor = os.environ.get('EDITOR') 188 editor = os.environ.get('EDITOR')
186 _STOP_AND_EXIT = 2 189 _STOP_AND_EXIT = 2
187 if not editor: 190 if not editor:
188 if sys.platform[:3] == 'win': 191 if sys.platform[:3] == 'win':
189 editor = 'notepad' 192 editor = 'notepad'
224 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 227 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
225 228
226 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) 229 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment])
227 commentGrammars.addParseAction(lambda x: '') 230 commentGrammars.addParseAction(lambda x: '')
228 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) 231 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd)
229 quotedStringInProgress = pyparsing.Literal('"') ^ pyparsing.Literal("'") + pyparsing.SkipTo(pyparsing.stringEnd) 232 #quotedStringInProgress = pyparsing.Literal('"') ^ pyparsing.Literal("'") + pyparsing.SkipTo(pyparsing.lineEnd)
230 terminators = [';', '\n\n'] 233 terminators = [';', '\n\n']
231 multilineCommands = [] 234 multilineCommands = []
232 235
233 def _init_parser(self): 236 def _init_parser(self):
234 ''' 237 '''
378 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') 381 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand')
379 oneLineCommand = pyparsing.Word(self.legalChars)('command') 382 oneLineCommand = pyparsing.Word(self.legalChars)('command')
380 pipe = pyparsing.Keyword('|', identChars='|') 383 pipe = pyparsing.Keyword('|', identChars='|')
381 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') 384 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
382 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) 385 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment)
383 self.quotedStringInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString) 386 #self.quotedStringInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString)
384 #for element in (terminatorParser, outputParser, pipe):
385 # element.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress).ignore(self.quotedStringInProgress)
386 afterElements = \ 387 afterElements = \
387 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 388 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
388 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) 389 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
389 if self.caseInsensitive: 390 if self.caseInsensitive:
390 multilineCommand.setParseAction(lambda x: x[0].lower()) 391 multilineCommand.setParseAction(lambda x: x[0].lower())
398 multilineCommand + pyparsing.SkipTo(pyparsing.stringEnd) 399 multilineCommand + pyparsing.SkipTo(pyparsing.stringEnd)
399 ^ 400 ^
400 ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + 401 ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') +
401 pyparsing.Optional(terminatorParser) + afterElements) 402 pyparsing.Optional(terminatorParser) + afterElements)
402 ) 403 )
403 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress).ignore(self.quotedStringInProgress) 404 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
404 405
405 inputMark = pyparsing.Literal('<') 406 inputMark = pyparsing.Literal('<')
406 inputMark.setParseAction(lambda x: '') 407 inputMark.setParseAction(lambda x: '')
407 inputFrom = pyparsing.Word(self.legalChars + '/\\')('inputFrom') 408 inputFrom = pyparsing.Word(self.legalChars + '/\\')('inputFrom')
408 inputFrom.setParseAction(lambda x: (x and open(x[0]).read()) or getPasteBuffer()) 409 inputFrom.setParseAction(lambda x: (x and open(x[0]).read()) or getPasteBuffer())
409 self.inputParser = inputMark + pyparsing.Optional(inputFrom) 410 self.inputParser = inputMark + pyparsing.Optional(inputFrom)