Mercurial > python-cmd2
comparison cmd2.py @ 136:67558b85ab38
comments working?
author | catherine@dellzilla |
---|---|
date | Mon, 10 Nov 2008 16:42:11 -0500 |
parents | 7c0a89fccf2b |
children | 308e99cebbb8 |
comparison
equal
deleted
inserted
replaced
135:7c0a89fccf2b | 136:67558b85ab38 |
---|---|
191 pass | 191 pass |
192 | 192 |
193 def __init__(self, *args, **kwargs): | 193 def __init__(self, *args, **kwargs): |
194 cmd.Cmd.__init__(self, *args, **kwargs) | 194 cmd.Cmd.__init__(self, *args, **kwargs) |
195 self.history = History() | 195 self.history = History() |
196 for p in (self.terminatorPattern, self.pipePattern, self.redirectInPattern, self.redirectOutPattern, self.punctuationPattern): | |
197 p.ignore(pyparsing.sglQuotedString) | |
198 p.ignore(pyparsing.dblQuotedString) | |
199 p.ignore(self.comments) | |
200 p.ignore(self.commentInProgress) | |
196 | 201 |
197 def do__comment(self, arg): | 202 def do__comment(self, arg): |
198 pass | 203 pass |
199 def do__multiline_comment(self, arg): | 204 def do__multiline_comment(self, arg): |
200 pass | 205 pass |
201 | 206 |
202 def do_shortcuts(self, args): | 207 def do_shortcuts(self, args): |
203 """Lists single-key shortcuts available.""" | 208 """Lists single-key shortcuts available.""" |
204 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) | 209 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) |
205 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) | 210 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) |
206 | 211 |
212 comments = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) | |
213 comments.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') | |
214 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) | |
215 commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) | |
216 | |
207 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') } | 217 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') } |
208 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n')) | 218 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n')) |
209 ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator') | 219 ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator') |
210 argSeparatorPattern = pyparsing.Word(pyparsing.printables)('command') \ | 220 argSeparatorPattern = pyparsing.Word(pyparsing.printables)('command') \ |
211 + pyparsing.SkipTo(pyparsing.StringEnd())('args') | 221 + pyparsing.SkipTo(pyparsing.StringEnd())('args') |
215 redirectOutPattern = (pyparsing.Literal('>>') ^ '>')('output') \ | 225 redirectOutPattern = (pyparsing.Literal('>>') ^ '>')('output') \ |
216 + pyparsing.Optional(filenamePattern)('outputTo') | 226 + pyparsing.Optional(filenamePattern)('outputTo') |
217 redirectInPattern = pyparsing.Literal('<')('input') \ | 227 redirectInPattern = pyparsing.Literal('<')('input') \ |
218 + pyparsing.Optional(filenamePattern)('inputFrom') | 228 + pyparsing.Optional(filenamePattern)('inputFrom') |
219 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern | 229 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern |
220 for p in (terminatorPattern, pipePattern, redirectInPattern, redirectOutPattern, punctuationPattern): | |
221 p.ignore(pyparsing.sglQuotedString) | |
222 p.ignore(pyparsing.dblQuotedString) | |
223 p.ignore(pyparsing.Or(commentGrammars)) | |
224 | 230 |
225 def parsed(self, s): | 231 def parsed(self, s): |
226 ''' | 232 ''' |
227 >>> c = Cmd() | 233 >>> c = Cmd() |
228 >>> r = c.parsed('quotes "are > ignored" < inp.txt') | 234 >>> r = c.parsed('quotes "are > ignored" < inp.txt') |
243 >>> r.statement, r.input, r.inputFrom | 249 >>> r.statement, r.input, r.inputFrom |
244 ('got from ', '<', 'thisfile.txt') | 250 ('got from ', '<', 'thisfile.txt') |
245 ''' | 251 ''' |
246 if isinstance(s, pyparsing.ParseResults): | 252 if isinstance(s, pyparsing.ParseResults): |
247 return s | 253 return s |
254 s = self.comments.transformString(s) | |
248 result = (pyparsing.SkipTo(pyparsing.StringEnd()))("fullStatement").parseString(s) | 255 result = (pyparsing.SkipTo(pyparsing.StringEnd()))("fullStatement").parseString(s) |
249 command = s.split()[0] | 256 command = s.split()[0] |
250 if self.caseInsensitive: | 257 if self.caseInsensitive: |
251 command = command.lower() | 258 command = command.lower() |
259 result['command'] = command | |
252 if command in self.noSpecialParse: | 260 if command in self.noSpecialParse: |
253 result['command'] = command | |
254 result['statement'] = result.fullStatement | 261 result['statement'] = result.fullStatement |
255 return result | 262 return result |
256 | 263 |
257 if s[0] in self.shortcuts: | 264 if s[0] in self.shortcuts: |
258 s = self.shortcuts[s[0]] + ' ' + s[1:] | 265 s = self.shortcuts[s[0]] + ' ' + s[1:] |
259 result['statement'] = s | 266 result['statement'] = s |
260 result['parseable'] = s | 267 result['parseable'] = s |
261 # terminator = self.specialTerminators.get(command) or self.terminatorPattern | 268 # terminator = self.specialTerminators.get(command) or self.terminatorPattern |
269 self.terminatorPattern.ignore(self.commentInProgress) | |
262 result += parseSearchResults(self.terminatorPattern, s) | 270 result += parseSearchResults(self.terminatorPattern, s) |
263 if result.terminator: | 271 if result.terminator: |
264 result['statement'] = result.upToIncluding | 272 result['statement'] = result.upToIncluding |
265 result['unterminated'] = result.before | 273 result['unterminated'] = result.before |
266 result['parseable'] = result.after | 274 result['parseable'] = result.after |
267 else: | 275 else: |
276 if command in self.multilineCommands: | |
277 return result # don't bother with the rest, we're still collecting input | |
268 result += parseSearchResults(self.punctuationPattern, s) | 278 result += parseSearchResults(self.punctuationPattern, s) |
269 result['statement'] = result['unterminated'] = result.before | 279 result['statement'] = result['unterminated'] = result.before |
270 result += parseSearchResults(self.pipePattern, result.parseable) | 280 result += parseSearchResults(self.pipePattern, result.parseable) |
271 result += parseSearchResults(self.redirectInPattern, result.parseable) | 281 result += parseSearchResults(self.redirectInPattern, result.parseable) |
272 result += parseSearchResults(self.redirectOutPattern, result.parseable) | 282 result += parseSearchResults(self.redirectOutPattern, result.parseable) |