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

Andreas Jung andreas@digicool.com
Fri, 11 Jan 2002 13:21:29 -0500


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

Added Files:
      Tag: ajung-textindexng-branch
	Makefile queryparser.g queryparser.py 
Log Message:
added prelim. new parser for TextIndex queries


=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser/Makefile ===
all: queryparser.py test

queryparser.py: queryparser.g
	python2.1 yapps2/yapps2.py queryparser.g

test: queryparser.py
	python2.1 queryparser.py



=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser/queryparser.g ===
# dumb parser for TextIndex queries


def quote(s,p):
    return '%s("%s")' % (p,s)

OpDict = {
    '&':      'difference',
    '|':      'union',
    '...':    'near'
}

res= ''

%%
parser TextIndexQueryParser:

    ignore:      "[ \r\t\n]+"
    token END:   "$"
    token NUM:   "[0-9]+"
    token VAR:   "[a-zA-Z_]+"
    token AND:   "\\&"
    token OR:    "\\|"
    token NEAR:  "\\.\\.\\."


    rule expr: "(\\(){0,1}"      {{ words = []; op=[] }} 
                subexpr<<words,op>>*
               "(\\)){0,1}" {{ if res: words.append(res) }}
                            {{ global res;c = ("%s(%s)" % (op[0], ','.join(words))) }}
                            {{ res = c }}
                            {{ return words,op }}


    rule subexpr<<words,op>>:      
                       OR   {{ op.append(OpDict[OR]) }}
                     | AND  {{ op.append(OpDict[AND]) }}
                     | NEAR {{ op.append(OpDict[NEAR]) }}
                     | VAR  {{ words.append(quote(VAR,'LL')) }}
                     | expr
%%
                  

def test():
    global res    
    for s in ['(a | b | d)', '(a | (b | c))', '(a &(b|c)', '(zope...rocks) and mailman' ]:
        print '-'*78
        print s
        res =  ''
        print parse('expr', s)
        print "result=",res
    


if __name__=='__main__':
    test()


=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser/queryparser.py ===
# dumb parser for TextIndex queries


def quote(s,p):
    return '%s("%s")' % (p,s)

OpDict = {
    '&':      'difference',
    '|':      'union',
    '...':    'near'
}

res= ''


from string import *
import re
from yappsrt import *

class TextIndexQueryParserScanner(Scanner):
    patterns = [
        ('"(\\\\)){0,1}"', re.compile('(\\)){0,1}')),
        ('"(\\\\(){0,1}"', re.compile('(\\(){0,1}')),
        ('[ \r\t\n]+', re.compile('[ \r\t\n]+')),
        ('END', re.compile('$')),
        ('NUM', re.compile('[0-9]+')),
        ('VAR', re.compile('[a-zA-Z_]+')),
        ('AND', re.compile('\\&')),
        ('OR', re.compile('\\|')),
        ('NEAR', re.compile('\\.\\.\\.')),
    ]
    def __init__(self, str):
        Scanner.__init__(self,None,['[ \r\t\n]+'],str)

class TextIndexQueryParser(Parser):
    def expr(self):
        self._scan('"(\\\\(){0,1}"')
        words = []; op=[]
        while self._peek('"(\\\\)){0,1}"', 'OR', 'AND', 'NEAR', 'VAR', '"(\\\\(){0,1}"') != '"(\\\\)){0,1}"':
            subexpr = self.subexpr(words,op)
        self._scan('"(\\\\)){0,1}"')
        if res: words.append(res)
        global res;c = ("%s(%s)" % (op[0], ','.join(words)))
        res = c
        return words,op

    def subexpr(self, words,op):
        _token_ = self._peek('OR', 'AND', 'NEAR', 'VAR', '"(\\\\(){0,1}"')
        if _token_ == 'OR':
            OR = self._scan('OR')
            op.append(OpDict[OR])
        elif _token_ == 'AND':
            AND = self._scan('AND')
            op.append(OpDict[AND])
        elif _token_ == 'NEAR':
            NEAR = self._scan('NEAR')
            op.append(OpDict[NEAR])
        elif _token_ == 'VAR':
            VAR = self._scan('VAR')
            words.append(quote(VAR,'LL'))
        else: # == '"(\\\\(){0,1}"'
            expr = self.expr()


def parse(rule, text):
    P = TextIndexQueryParser(TextIndexQueryParserScanner(text))
    return wrap_error_reporter(P, rule)



                  

def test():
    global res    
    for s in ['(a | b | d)', '(a | (b | c))', '(a &(b|c)', '(zope...rocks) and mailman' ]:
        print '-'*78
        print s
        res =  ''
        print parse('expr', s)
        print "result=",res
    


if __name__=='__main__':
    test()