changeset 62:95e239c87010

use searchString
author catherine@Elli.myhome.westell.com
date Sat, 21 Jun 2008 19:45:50 -0400
parents 82891de09dbf
children a96f824d1a24
files lineend.py
diffstat 1 files changed, 32 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/lineend.py	Fri Jun 20 20:41:19 2008 -0400
+++ b/lineend.py	Sat Jun 21 19:45:50 2008 -0400
@@ -1,58 +1,52 @@
 import pyparsing, sys, doctest
 
-terminators = (';', 'EOF')
-
-#pattern = pyparsing.Optional(pyparsing.CharsNotIn(terminators)) + pyparsing.oneOf(';')
-complete_pattern = pyparsing.SkipTo(pyparsing.oneOf(terminators))
+intgr = pyparsing.Optional(pyparsing.Word(pyparsing.nums))
+terminators = [pyparsing.Literal(';') + intgr,
+               pyparsing.Literal('\\t') + intgr]
+complete_pattern = reduce(lambda x, y: x ^ y, terminators)
+#complete_pattern = (pyparsing.Literal(';') + intgr) ^ (pyparsing.Literal('\\t') + intgr)
 complete_pattern.ignore(pyparsing.sglQuotedString)
-complete_pattern.ignore(pyparsing.sglQuotedString)
+complete_pattern.ignore(pyparsing.dblQuotedString)
 
-redirect_pattern = pyparsing.Optional(pyparsing.CharsNotIn('<>')) + pyparsing.oneOf('< >') + pyparsing.Word(pyparsing.printables)
+redirect_pattern = pyparsing.oneOf('< >') + pyparsing.restOfLine
 redirect_pattern.ignore(pyparsing.sglQuotedString)
-redirect_pattern.ignore(pyparsing.sglQuotedString)
+redirect_pattern.ignore(pyparsing.dblQuotedString)
 
-pipe_pattern = pyparsing.Optional(pyparsing.CharsNotIn('|')) + '|' + pyparsing.SkipTo(pyparsing.StringEnd())
+pipe_pattern = pyparsing.Literal('|') + pyparsing.restOfLine
 pipe_pattern.ignore(pyparsing.sglQuotedString)
-pipe_pattern.ignore(pyparsing.sglQuotedString)
+pipe_pattern.ignore(pyparsing.dblQuotedString)
 
 def parse(txt, mustBeTerminated=False):
     """
     >>> sorted(parse('select * from dual;', True).items())
-    [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)]
+    [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', [';'])]
     >>> sorted(parse('select * from dual E', True).items())
-    [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)]
+    [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
     >>> sorted(parse('select * from', True).items())
-    [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)]
+    [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
     >>> sorted(parse('select * from dual; > result.txt', True).items())
-    [('complete', True), ('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None)]
+    [('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None), ('terminator', [';'])]
     >>> sorted(parse("select * from dual where val = 'x > y'", True).items())
-    [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)]
+    [('inputFrom', None), ('outputTo', None), ('pipeTo', None), ('terminator', None)]
     >>> sorted(parse('select * from dual; | wc -c', True).items())
-    [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c')]
+    [('inputFrom', None), ('outputTo', None), ('pipeTo', ' wc -c'), ('terminator', [';'])]
+    >>> sorted(parse('select * from dual; | sort > sorted.txt', True).items())
+    [('inputFrom', None), ('outputTo', None), ('pipeTo', 'sort > sorted.txt'), ('terminator', [';'])]    
     """
-    result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None}
-    if mustBeTerminated:
-        try:
-            complete_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
-        try:
-            parsed = pipe_pattern.parseString(txt)
-            result['pipeTo'] = parsed[2]
-        except pyparsing.ParseException:
-            pass
-        
+    result = {'inputFrom': None, 'outputTo': None, 'pipeTo': None, 'terminator': None}
+    found = complete_pattern.searchString(txt)
+    if found:
+        result['terminator'] = found[0][:]
+    found = redirect_pattern.searchString(txt)
+    if found:
+        if found[0][0] == '>':
+            result['outputTo'] = found[0][1].strip()
+        else:
+            result['inputFrom'] = found[0][1].strip()
+    found = pipe_pattern.searchString(txt)
+    if found:
+        result['pipeTo'] = found[0][1]
+
     return result
     
 if __name__ == '__main__':