# HG changeset patch # User catherine@Elli.myhome.westell.com # Date 1214005959 14400 # Node ID 682588392eafc3d3ad70b5d7cbca0a864f850648 # Parent 37f471fb29b152a8906335014b3538e49420e850 experiments with parsing in lineend diff -r 37f471fb29b1 -r 682588392eaf cmd2.py --- a/cmd2.py Fri Jun 20 15:49:06 2008 -0400 +++ b/cmd2.py Fri Jun 20 19:52:39 2008 -0400 @@ -292,9 +292,10 @@ statementEndPattern = re.compile(r'[%s]\s*$' % terminators) lineEndFinder = pyparsing.Optional(pyparsing.CharsNotIn(';')) + ';' def statementHasEnded(self, lines): + #import pdb; pdb.set_trace() try: - lineEndFinder.parseString(lines) - return True` + self.lineEndFinder.parseString(lines) + return True except pyparsing.ParseException: return False ''' diff -r 37f471fb29b1 -r 682588392eaf lineend.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lineend.py Fri Jun 20 19:52:39 2008 -0400 @@ -0,0 +1,41 @@ +import pyparsing, sys, doctest + +pattern = pyparsing.Optional(pyparsing.CharsNotIn(';')) + ';' +pattern.ignore(pyparsing.sglQuotedString) +pattern.ignore(pyparsing.sglQuotedString) + +redirect_pattern = pyparsing.Optional(pyparsing.CharsNotIn('<>')) + pyparsing.oneOf('< >') + pyparsing.Word(pyparsing.printables) +redirect_pattern.ignore(pyparsing.sglQuotedString) +redirect_pattern.ignore(pyparsing.sglQuotedString) + +def parse(txt, mustBeTerminated=False): + """ + >>> sorted(parse('select * from dual;', True).items()) + [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] + >>> sorted(parse('select * from', True).items()) + [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] + >>> sorted(parse('select * from dual; > result.txt', True).items()) + [('complete', True), ('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None)] + """ + result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None} + if mustBeTerminated: + try: + pattern.parseString(txt) + result['complete'] = True + except pyparsing.ParseException: + pass + + if result['complete']: + try: + parsed = redirect_pattern.parseString(txt) + if parsed[1] == '>': + result['outputTo'] = parsed[2] + else: + result['inputFrom'] = parsed[2] + except pyparsing.ParseException: + pass + return result + +if __name__ == '__main__': + doctest.testmod() + \ No newline at end of file