Mercurial > python-cmd2
diff lineend.py @ 61:82891de09dbf
before splicing new terminator parsing in
author | catherine@Elli.myhome.westell.com |
---|---|
date | Fri, 20 Jun 2008 20:41:19 -0400 |
parents | 682588392eaf |
children | 95e239c87010 |
line wrap: on
line diff
--- 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__':