[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser - TextIndexGgen.py:1.1.2.3
Andreas Jung
andreas@digicool.com
Tue, 15 Jan 2002 21:46:20 -0500
Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser
In directory cvs.zope.org:/tmp/cvs-serv16494
Modified Files:
Tag: ajung-textindexng-branch
TextIndexGgen.py
Log Message:
- code cleanup
- refactored grammer and QueryParser into own modules
=== Zope/lib/python/Products/PluginIndexes/TextIndexNG/queryparser/TextIndexGgen.py 1.1.2.2 => 1.1.2.3 ===
+
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
@@ -11,165 +13,42 @@
#
##############################################################################
-""" Grammer for TextIndex queries """
-
+__version__ = "$Id$"
-REGENERATEONLOAD = 1
-import string
GRAMMARSTRING ="""
- Value :: ## indicates Value is the root nonterminal for the grammar
- @R BaseRule :: Value >> Term
- @R Braces1Rule :: Term >> openp Term closep
- @R Braces2Rule :: Term >> openp Term closep Term
- @R DoubleTermRule :: Term >> Term Term
- @R AndTermRule :: Term >> Term and Term
- @R OrTermRule :: Term >> Term or Term
- @R Strrule :: Term >> str
-
-
-"""
-COMPILEDFILENAME = "TextIndexG.py"
-MARSHALLEDFILENAME = "TextIndexG.mar"
-OPENPREGEX = r'('
-CLOSEPREGEX = r')'
-ANDREGEX = 'and'
-ORREGEX = 'or'
-STRREGEX = '[a-zA-Z]*'
-
-
-OpMapping = {
- 'or': 'txUnion',
- 'and': 'txIntersection',
- 'near': 'txNear'
-}
-
-### declare interpretation functions and regex's for terminals
-
-class Stack:
-
- def __init__(self):
- self.lst = []
-
- def push(self,item):
- self.lst.append(item)
-
- def pop(self):
- item = self.lst[-1]
- self.lst = self.lst[:-1]
- return item
-
-
-class Collector(Stack):
-
- def __init__(self, default_op='and'):
- Stack.__init__(self)
-
- self._current = []
- self._op = []
- self._default_op = default_op
- self._result = ''
-
-
- def addWord(self,word):
- self._current.append( 'LL("%s")' % word)
-
- def setOp(self, op):
- self._op = op
-
-
- def newLevel(self):
-
- # pop current on the stack
- self.push( (self._current, self._op))
-
- self._current = []
-
-
- def endLevel(self):
-
- if not self._op:
- op = self._default_op
- else:
- op = self._op
-
- if self._current:
- r = ','.join(self._current)
- r = "%s(%s)" % (OpMapping[op], r)
-
- # get top element from stack
- topEl,topOp = self.pop()
- topEl.append( r )
- self.push( (topEl, topOp))
-
- self._current = []
-
- else:
- # merge two top level element of stack
- topEl,topOp = self.pop()
- top1El,top1Op = self.pop()
+ expr ::
- r = ','.join(topEl)
- r = "%s(%s)" % (OpMapping[topOp], r)
+ @R R1 :: expr >> factor
+ @R R2 :: expr >> factor or factor
+ @R R3 :: expr >> factor factor
- top1El.append(r)
-
- self.push( (top1El, top1Op) )
-
- def __str__(self):
- return "Collector: %s %s" % (self._current,self._op)
-
- __repr__ = __str__
-
- def getResult(self):
-
- if self._current:
- if not self._op: self._op = self._default_op
-
- r = ','.join(self._current)
- r = "%s(%s)" % (OpMapping[self._op], r)
-
- return r
-
- else:
-
- topEl,topOp = self.pop()
- if not topOp: topOp = self._default_op
-
- r = ','.join(topEl)
- r = "%s(%s)" % (OpMapping[topOp], r)
-
- return r
-
-C = Collector()
-
-def openParens(x):
- C.newLevel()
-
-def closeParens(x):
- C.endLevel()
+ @R R4 :: factor >> term
+ @R R5 :: factor >> term and term
+ @R R6 :: term >> str
+ @R R7 :: term >> ( expr )
+"""
-def wordFound(word):
- C.addWord( word )
- return word
-def operandToken(op):
- C.setOp(op)
+COMPILEDFILENAME = "TextIndexG.py"
+MARSHALLEDFILENAME = "TextIndexG.mar"
+ANDREGEX = 'and'
+ORREGEX = 'or'
+STRREGEX = '[a-zA-Z]*'
def DeclareTerminals(Grammar):
- Grammar.Addterm("and", ANDREGEX, operandToken)
- Grammar.Addterm("or", ORREGEX, operandToken)
- Grammar.Addterm("openp", OPENPREGEX, openParens)
- Grammar.Addterm("closep", CLOSEPREGEX, closeParens)
- Grammar.Addterm("str", STRREGEX, wordFound)
-
+ Grammar.Addterm("and", ANDREGEX, '')
+ Grammar.Addterm("or", ORREGEX, '')
+ Grammar.Addterm("str", STRREGEX, '')
def BindRules(Grammar):
- pass
+ pass
+
# This function generates the grammar and dumps it to a file.
+
def GrammarBuild():
import kjParseBuild
@@ -177,8 +56,8 @@
TextIndexG.SetCaseSensitivity(0) # grammar is not case sensitive for keywords
DeclareTerminals(TextIndexG)
# TextIndexG.Keywords("and or")
-# TextIndexG.punct("()")
- TextIndexG.Nonterms("Value Term")
+ TextIndexG.punct("()")
+ TextIndexG.Nonterms("expr term factor wordlist")
TextIndexG.Declarerules(GRAMMARSTRING)
TextIndexG.Compile()
@@ -193,50 +72,13 @@
outfile.close()
BindRules(TextIndexG)
- return TextIndexG
-
-# this function initializes the compiled grammar from the generated file.
-def LoadTextIndexG():
- import TextIndexG
- # reload to make sure we get the most recent version!
- # (only needed when debugging the grammar).
- reload(TextIndexG)
- TextIndexG = TextIndexG.GRAMMAR()
- DeclareTerminals(TextIndexG)
- BindRules(TextIndexG)
return TextIndexG
-def unMarshalTextIndexG():
- import kjParser
- infile = open(MARSHALLEDFILENAME, "r")
- TextIndexG = kjParser.UnMarshalGram(infile)
- infile.close()
- DeclareTerminals(TextIndexG)
- BindRules(TextIndexG)
- return TextIndexG
-
-
if __name__ == '__main__':
- ########## test the grammar generation
- if REGENERATEONLOAD:
- print "(re)generating the TextIndexG grammar in file TextIndexG.py"
- Dummy = GrammarBuild()
- print "(re)generation done."
-
- print "loading grammar as python"
- TextIndexG = LoadTextIndexG()
-
- Context = { }
-
- for item in ['a','(a)','(a) (b)','(a b)','(a b)(c or d) ' ,
- '(a and b) or (c and dd)','a and (b or c or (x and y))']:
-
- print '-'*78
- print item
- test = TextIndexG.DoParse1( item, Context)
-
- print 'res:',C.getResult()
+ print "(re)generating the TextIndexG grammar in file TextIndexG.py"
+ Dummy = GrammarBuild()
+ print "(re)generation done."