Thanks Alex for your response ! I could put together something and get it working. However, since i am using python 2.6 in our environment that i cannot change because of some restrictions in our proudct's python libraries , i have the following two questions:
1. Do you have any backport of modgrammar 0.10 that will work with python 2.6 ?
{code}
from modgrammar import *
class PredicateKeywordAttribute (Grammar):
"""
Defines the grammar for keyword search attributes. (method, module, package, class)
"""
grammar = (LITERAL("method") | LITERAL("module") | LITERAL("package") | LITERAL("class"))
class PredicateOperator (Grammar):
"""
Operators that we support for predicates. (=, !=)
"""
grammar = (LITERAL("=") | LITERAL("!="))
class Predicate (Grammar):
"""
Defines the grammar for a predicate.
Eg: module = test*, package=a.b.*, method=test_smoke, class=Test*Smoke*Tests
"""
grammar = (PredicateKeywordAttribute, PredicateOperator, OPTIONAL(LITERAL("\"") | LITERAL("'")),
WORD("*A-Z0-9a-z._"), OPTIONAL(LITERAL("\"") | LITERAL("'")))
class Operator (Grammar):
"""
Operator supported between predicates. (and, or)
"""
grammar_whitespace = False
grammar = (LITERAL("and") | LITERAL("or") | LITERAL("AND") | LITERAL("OR"))
class Query (Grammar):
"""
Grammar for the final query expression which will just be a list of predicates separated by the operator.
"""
grammar = (LIST_OF(Predicate, sep=Operator),";")
{code}
However this one seems to pass the query text "class = test*AND module = test*;* even when i set grammar_whitespace = False or grammar_whitespace = True for the grammar 'Operator'.
myparser = Query.parser()
result = myparser.parse_string("class = test*AND module = test*;")
The above test gives me back the following with both grammar_whitespace set to True or False.
class = test*AND module = test*;: Parsed elements - (<LIST><'class = test*', 'AND', 'module = test*'>, L(';')<';'>)
How can i force the grammar to require whitespace around 'AND' and 'OR'.
And thanks for the wonderful easy library !
Ravi