# HG changeset patch # User catherine@Elli.myhome.westell.com # Date 1214008879 14400 # Node ID 82891de09dbf12429f6dbba825466a3ee25b3c9d # Parent 682588392eafc3d3ad70b5d7cbca0a864f850648 before splicing new terminator parsing in diff -r 682588392eaf -r 82891de09dbf lineend.py --- a/lineend.py Fri Jun 20 19:52:39 2008 -0400 +++ b/lineend.py Fri Jun 20 20:41:19 2008 -0400 @@ -1,26 +1,39 @@ import pyparsing, sys, doctest -pattern = pyparsing.Optional(pyparsing.CharsNotIn(';')) + ';' -pattern.ignore(pyparsing.sglQuotedString) -pattern.ignore(pyparsing.sglQuotedString) +terminators = (';', 'EOF') + +#pattern = pyparsing.Optional(pyparsing.CharsNotIn(terminators)) + pyparsing.oneOf(';') +complete_pattern = pyparsing.SkipTo(pyparsing.oneOf(terminators)) +complete_pattern.ignore(pyparsing.sglQuotedString) +complete_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) +pipe_pattern = pyparsing.Optional(pyparsing.CharsNotIn('|')) + '|' + pyparsing.SkipTo(pyparsing.StringEnd()) +pipe_pattern.ignore(pyparsing.sglQuotedString) +pipe_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 dual E', True).items()) + [('complete', False), ('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)] + >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) + [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] + >>> sorted(parse('select * from dual; | wc -c', True).items()) + [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c')] """ result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None} if mustBeTerminated: try: - pattern.parseString(txt) + complete_pattern.parseString(txt) result['complete'] = True except pyparsing.ParseException: pass @@ -34,6 +47,12 @@ result['inputFrom'] = parsed[2] except pyparsing.ParseException: pass + try: + parsed = pipe_pattern.parseString(txt) + result['pipeTo'] = parsed[2] + except pyparsing.ParseException: + pass + return result if __name__ == '__main__':