[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser - queryparser.py:1.1.2.3

Andreas Jung andreas@digicool.com
Tue, 15 Jan 2002 21:24:01 -0500


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser
In directory cvs.zope.org:/tmp/cvs-serv11422

Added Files:
      Tag: ajung-textindexng-branch
	queryparser.py 
Log Message:
added


=== Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser/queryparser.py 1.1.2.2 => 1.1.2.3 ===
+#
+# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+# 
+##############################################################################
+
+"""
+QueryParser class
+"""
+
+__version__ = '$Id$'
+
+
+import TextIndexG
+
+
+
+class QueryParser:
+
+    """Query parser takes a TextIndexNG query strings and transforms it
+       into a Python expression to be evaluated.
+    """ 
+
+    ANDREGEX       = 'and'
+    ORREGEX        = 'or'
+    STRREGEX       = '[a-zA-Z]*'
+
+
+    def __init__(self):
+
+        self.TextIndexG = TextIndexG.GRAMMAR()
+
+        self.DeclareTerminals()
+        self.BindRules()
+        
+    
+    def DeclareTerminals(self):
+        self.TextIndexG.Addterm("and", self.ANDREGEX, self.operandToken)
+        self.TextIndexG.Addterm("or",  self.ORREGEX,  self.operandToken)
+        self.TextIndexG.Addterm("str", self.STRREGEX, self.wordFound)
+
+
+    def BindRules(self):
+        pass
+
+
+    def wordFound(self,word):
+        print 'word:',word
+
+
+    def operandToken(self,op):
+        print 'operand',op
+
+
+    def __call__(self, query):
+
+        res = self.TextIndexG.DoParse1(query, {} )
+        print res
+       
+        return res 
+
+
+def test():
+
+    import os, sys, re,traceback, atexit
+    import readline
+
+    histfile = os.path.expanduser('~/.pyhist')
+    try:
+        readline.read_history_file(histfile)
+    except IOError: pass
+    atexit.register(readline.write_history_file,histfile)
+
+    print "loading grammar as python"
+
+    Context = { }
+
+
+    while 1:
+
+        s = raw_input('> ')
+
+        print s  
+
+        try:
+            P = QueryParser()
+            res = P(s)        
+
+            print 'res:',res
+        except:
+            traceback.print_exc()
+
+if __name__ == '__main__':
+    test()
+